Content.php 14 KB

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