Content.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace Collection\Lib;
  3. use Dever;
  4. class Content
  5. {
  6. public function updateTypeId($id, $name, $data)
  7. {
  8. # 同步更新到content的type_id
  9. $update['type_id'] = $id;
  10. $content_id = Dever::param('content_id', $data);
  11. if ($content_id) {
  12. $update['where_id'] = $content_id;
  13. Dever::db('collection/content')->update($update);
  14. }
  15. }
  16. # 获取某一个合集的内容列表
  17. public function getList($info_id, $page_id)
  18. {
  19. $where['info_id'] = $info_id;
  20. $where['page_id'] = $page_id;
  21. $data = Dever::db('collection/content')->getAllPage($where);
  22. $result = array();
  23. if ($data) {
  24. $i = 0;
  25. # 获取章节信息
  26. $page = Dever::db('collection/page')->one($page_id);
  27. if ($page && $page['desc']) {
  28. $parent_page = Dever::db('collection/page')->one($page['page_id']);
  29. $page['parent_name'] = $parent_page['name'];
  30. $page['name'] = $page['name'];
  31. $info = Dever::db('collection/info')->one($info_id);
  32. if ($info && $info['uid'] > 0) {
  33. $user = Dever::load('passport/api')->info($info['uid']);
  34. $page['username'] = $user['username'];
  35. $page['copyright'] = "如不慎侵害您的权益,请告知,我们将责令创作者修改";
  36. } else {
  37. $page['username'] = '官方';
  38. $page['copyright'] = "所有内容仅供内部专用,禁止在互联网传播";
  39. }
  40. $result[$i] = $page;
  41. //$result[$i]['copyright'] = $page['copyright'];
  42. $result[$i]['data'] = -1;
  43. $result[$i]['type'] = -1;
  44. $result[$i]['times'] = $result[$i]['times_parent'] = '';
  45. if ($page['times_id_parent'] && $page['times_id']) {
  46. $times = Dever::db('collection/times')->one($page['times_id']);
  47. $result[$i]['times'] = $times['name'];
  48. $times = Dever::db('collection/times')->one($page['times_id_parent']);
  49. $result[$i]['times_parent'] = $times['name'];
  50. }
  51. $i = 1;
  52. }
  53. foreach ($data as $k => $v) {
  54. $data[$k] = $this->getOne($v);
  55. if ($data[$k]['data']) {
  56. $result[$i] = $data[$k];
  57. $i++;
  58. }
  59. }
  60. if ($page && $page['desc']) {
  61. $result[$i] = $result[0];
  62. if ($page['desc_end']) {
  63. $result[$i]['name'] .= ' 已完结';
  64. $result[$i]['desc'] = $page['desc_end'];
  65. } else {
  66. $result[$i]['name'] .= ' 连载中';
  67. }
  68. }
  69. }
  70. return $result;
  71. }
  72. public function getOne($data)
  73. {
  74. $type = Dever::db('collection/cate')->config['gettype'];
  75. $table = $type[$data['type']]['table'];
  76. if (!$table) {
  77. return $data;
  78. }
  79. $method = 'one';
  80. if (strstr($table, '-')) {
  81. $temp = explode('-', $table);
  82. $table = $temp[0];
  83. $method = $temp[1];
  84. }
  85. $data['data'] = Dever::db($table)->$method(array('content_id' => $data['id'], 'info_id' => $data['info_id']));
  86. if ($data['data']) {
  87. if (isset($data['data']['id'])) {
  88. $data['data'] = $this->getData($data['data']);
  89. } else {
  90. foreach ($data['data'] as $k => $v) {
  91. $data['data'][$k] = $this->getData($v);
  92. }
  93. }
  94. }
  95. return $data;
  96. }
  97. public function getData($data)
  98. {
  99. $data['num_view'] = 1;
  100. $data['date'] = '';
  101. if (isset($data['cdate'])) {
  102. $data['date'] = date('Y-m-d H:i:s', $data['cdate']);
  103. }
  104. if (isset($data['text'])) {
  105. $data['text'] = Dever::array_decode($data['text']);
  106. foreach ($data['text'] as $k => $v) {
  107. if (isset($v['name']) && $v['name']) {
  108. $data['text'][$k]['name_array'] = explode("\r\n", $v['name']);
  109. }
  110. if (isset($v['bgcolor_type']) && $v['bgcolor_type'][0] == 2) {
  111. $data['text'][$k]['bgcolor'] = '';
  112. }
  113. if (isset($v['content']) && $v['content']) {
  114. $data['text'][$k]['content_array'] = $this->getContent($v);
  115. }
  116. }
  117. }
  118. if (isset($data['author_id']) && $data['author_id'] > 0) {
  119. $data['author'] = Dever::db('store/author')->one($data['author_id']);
  120. }
  121. if (isset($data['content']) && $data['content']) {
  122. $data = $this->getContent($data);
  123. }
  124. if (isset($data['goods_id']) && $data['goods_id'] > 0) {
  125. $data['goods'] = Dever::load('goods/lib/info')->getInfo($data['goods_id']);
  126. $data['goods'] = $this->getContent($data['goods']);
  127. }
  128. unset($data['info_id']);
  129. unset($data['content_id']);
  130. unset($data['status']);
  131. unset($data['state']);
  132. unset($data['cdate']);
  133. unset($data['reorder']);
  134. unset($data['udate']);
  135. return $data;
  136. }
  137. public function getContent($data, $uid = false)
  138. {
  139. //embed
  140. $data['content_array'] = $data['content_pic'] = array();
  141. $data['content'] = Dever::filter($data['content']);
  142. $content = $data['content'];
  143. $replace = array();
  144. # 小程序
  145. if (strstr($data['content'], 'data-applet')) {
  146. preg_match_all('/<img(.*?)data-applet="(.*?)" \/>/', $content, $matches);
  147. if (isset($matches[2])) {
  148. foreach ($matches[2] as $k => $v) {
  149. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  150. $temp = explode('||', $v);
  151. $pic = $temp[0];
  152. $appid = $temp[1];
  153. $path = $temp[2];
  154. if ($appid) {
  155. $appinfo = Dever::db('content/applet')->one(array('appid' => $appid));
  156. } else {
  157. $appinfo['name'] = '';
  158. $appinfo['link'] = '';
  159. }
  160. if (isset($temp[3]) && $temp[3]) {
  161. $appinfo['link'] = $temp[3];
  162. }
  163. $replace[] = array('type' => 'applet', 'pic_cover' => $pic, 'appid' => $appid, 'path' => $path, 'name' => $appinfo['name'], 'link' => $appinfo['link']);
  164. }
  165. }
  166. }
  167. # 图片
  168. if (strstr($data['content'], '<img')) {
  169. preg_match_all('/<img(.*?)src="(.*?)" (.*?)\/>/', $content, $matches);
  170. if (isset($matches[2])) {
  171. foreach ($matches[2] as $k => $v) {
  172. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  173. $pic = $v;
  174. $replace[] = array('type' => 'pic', 'content' => $pic);
  175. $data['content_pic'][] = $pic;
  176. }
  177. }
  178. }
  179. # 音视频
  180. if (strstr($data['content'], 'data-file')) {
  181. preg_match_all('/<img src="(.*?)" style="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  182. if (!isset($matches[1][0])) {
  183. preg_match_all('/<img style="(.*?)" src="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  184. $temp = array();
  185. if (isset($matches[2][0])) {
  186. $temp = $matches;
  187. $matches[1] = $temp[2];
  188. unset($temp);
  189. }
  190. }
  191. if (isset($matches[1])) {
  192. foreach ($matches[1] as $k => $v) {
  193. if (isset($matches[3][$k])) {
  194. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  195. $file = $matches[3][$k];
  196. $temp = explode('||', $file);
  197. $file = $temp[0];
  198. if (isset($temp[1])) {
  199. $name = $temp[1];
  200. } else {
  201. $name = '';
  202. }
  203. $cover = $v;
  204. if (strstr($v, '.mp4') || strstr($v, '.mov') || strstr($v, '.m3u8')) {
  205. $replace[] = array('type' => 'video', 'content' => $file, 'cover' => $cover, 'name' => $name);
  206. } else {
  207. $replace[] = array('type' => 'audio', 'content' => $file, 'cover' => $cover, 'name' => $name);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. # 视频+直播
  214. if (strstr($data['content'], 'data-id')) {
  215. preg_match_all('/<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>/', $content, $matches);
  216. if (!isset($matches[2][0])) {
  217. preg_match_all('/<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>/', $content, $matches);
  218. $temp = array();
  219. if (isset($matches[2][0]) && isset($matches[3][0])) {
  220. $temp = $matches;
  221. $matches[2] = $temp[3];
  222. $matches[3] = $temp[2];
  223. unset($temp);
  224. }
  225. }
  226. if (isset($matches[2][0]) && isset($matches[3][0])) {
  227. foreach ($matches[2] as $k => $v) {
  228. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  229. if ($matches[3][$k] == 'video/lib/core.vod') {
  230. $method = 'video/lib/vod';
  231. $type = 'video';
  232. } else {
  233. $type = 'live';
  234. $method = 'video/lib/live';
  235. }
  236. $info = Dever::load($method)->get($v);
  237. if (isset($info['content'])) {
  238. unset($info['content']);
  239. }
  240. if ($type == 3) {
  241. # 查看是否有预约
  242. if ($uid > 0) {
  243. $info['user_act']['note'] = Dever::load('act/lib/note')->get($uid, $v, $type);
  244. } else {
  245. $info['user_act']['note'] = 2;
  246. }
  247. }
  248. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  249. }
  250. }
  251. }
  252. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  253. $content = explode('{replace}', $content);
  254. $data['content_array'] = array();
  255. //print_r($content);die;
  256. foreach ($content as $k => $v) {
  257. $v = trim($v);
  258. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  259. $data['content_array'][] = $replace[$v];
  260. } elseif ($v) {
  261. $data['content_array'][] = array
  262. (
  263. 'type' => 'html',
  264. 'content' => $v,
  265. );
  266. }
  267. }
  268. if (!$data['content_array']) {
  269. $data['content_array'][] = array
  270. (
  271. 'type' => 'html',
  272. 'content' => $data['content'],
  273. );
  274. }
  275. unset($data['content']);
  276. //print_r($data['content_array']);die;
  277. return $data;
  278. }
  279. }