Content.php 14 KB

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