Content.php 13 KB

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