Content.php 12 KB

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