Content.php 13 KB

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