Content.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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']) && $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. } else {
  149. $data['text'] = array();
  150. }
  151. if (isset($data['author_id']) && $data['author_id'] > 0) {
  152. $data['author'] = Dever::db('store/author')->one($data['author_id']);
  153. }
  154. # 歌词
  155. if (isset($data['lrc']) && $data['lrc']) {
  156. $data['lrc'] = strip_tags($data['lrc']);
  157. $data['lrc'] = explode("\r\n", $data['lrc']);
  158. }
  159. if (isset($data['content']) && $data['content']) {
  160. $data = $this->getContent($data);
  161. }
  162. if (isset($data['goods_id']) && $data['goods_id'] > 0) {
  163. $data['goods'] = Dever::load('goods/lib/info')->getInfo($data['goods_id'], true, 'view_reorder', $user);
  164. $data['goods'] = $this->getContent($data['goods']);
  165. }
  166. unset($data['info_id']);
  167. unset($data['content_id']);
  168. unset($data['status']);
  169. unset($data['state']);
  170. unset($data['cdate']);
  171. unset($data['reorder']);
  172. unset($data['udate']);
  173. return $data;
  174. }
  175. public function getContent($data, $uid = false)
  176. {
  177. //embed
  178. $data['content_array'] = $data['content_pic'] = array();
  179. $data['content'] = Dever::filter($data['content']);
  180. $content = $data['content'];
  181. $replace = array();
  182. # 过滤宽度
  183. if (strstr($data['content'], 'width:')) {
  184. $content = preg_replace('/width:(.*?)px/', 'width:100%', $content);
  185. }
  186. # 小程序
  187. if (strstr($data['content'], 'data-applet')) {
  188. preg_match_all('/<img(.*?)data-applet="(.*?)" \/>/', $content, $matches);
  189. if (isset($matches[2])) {
  190. foreach ($matches[2] as $k => $v) {
  191. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  192. $temp = explode('||', $v);
  193. $pic = $temp[0];
  194. $appid = $temp[1];
  195. $path = $temp[2];
  196. if ($appid) {
  197. $appinfo = Dever::db('content/applet')->one(array('appid' => $appid));
  198. } else {
  199. $appinfo['name'] = '';
  200. $appinfo['link'] = '';
  201. }
  202. if (isset($temp[3]) && $temp[3]) {
  203. $appinfo['link'] = $temp[3];
  204. }
  205. $replace[] = array('type' => 'applet', 'pic_cover' => $pic, 'appid' => $appid, 'path' => $path, 'name' => $appinfo['name'], 'link' => $appinfo['link']);
  206. }
  207. }
  208. }
  209. # 图片
  210. if (strstr($data['content'], '<img')) {
  211. preg_match_all('/<img(.*?)src="(.*?)" (.*?)\/>/', $content, $matches);
  212. if (isset($matches[2])) {
  213. foreach ($matches[2] as $k => $v) {
  214. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  215. $pic = $v;
  216. $replace[] = array('type' => 'pic', 'content' => $pic);
  217. $data['content_pic'][] = $pic;
  218. }
  219. }
  220. }
  221. # 音视频
  222. if (strstr($data['content'], 'data-file')) {
  223. preg_match_all('/<img src="(.*?)" style="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  224. if (!isset($matches[1][0])) {
  225. preg_match_all('/<img style="(.*?)" src="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  226. $temp = array();
  227. if (isset($matches[2][0])) {
  228. $temp = $matches;
  229. $matches[1] = $temp[2];
  230. unset($temp);
  231. }
  232. }
  233. if (isset($matches[1])) {
  234. foreach ($matches[1] as $k => $v) {
  235. if (isset($matches[3][$k])) {
  236. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  237. $file = $matches[3][$k];
  238. $temp = explode('||', $file);
  239. $file = $temp[0];
  240. if (isset($temp[1])) {
  241. $name = $temp[1];
  242. } else {
  243. $name = '';
  244. }
  245. $cover = $v;
  246. if (strstr($v, '.mp4') || strstr($v, '.mov') || strstr($v, '.m3u8')) {
  247. $replace[] = array('type' => 'video', 'content' => $file, 'cover' => $cover, 'name' => $name);
  248. } else {
  249. $replace[] = array('type' => 'audio', 'content' => $file, 'cover' => $cover, 'name' => $name);
  250. }
  251. }
  252. }
  253. }
  254. }
  255. # 视频+直播
  256. if (strstr($data['content'], 'data-id')) {
  257. preg_match_all('/<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>/', $content, $matches);
  258. if (!isset($matches[2][0])) {
  259. preg_match_all('/<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>/', $content, $matches);
  260. $temp = array();
  261. if (isset($matches[2][0]) && isset($matches[3][0])) {
  262. $temp = $matches;
  263. $matches[2] = $temp[3];
  264. $matches[3] = $temp[2];
  265. unset($temp);
  266. }
  267. }
  268. if (isset($matches[2][0]) && isset($matches[3][0])) {
  269. foreach ($matches[2] as $k => $v) {
  270. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  271. if ($matches[3][$k] == 'video/lib/core.vod') {
  272. $method = 'video/lib/vod';
  273. $type = 'video';
  274. } else {
  275. $type = 'live';
  276. $method = 'video/lib/live';
  277. }
  278. $info = Dever::load($method)->get($v);
  279. if (isset($info['content'])) {
  280. unset($info['content']);
  281. }
  282. if ($type == 3) {
  283. # 查看是否有预约
  284. if ($uid > 0) {
  285. $info['user_act']['note'] = Dever::load('user/lib/note')->get($uid, $v, $type);
  286. } else {
  287. $info['user_act']['note'] = 2;
  288. }
  289. }
  290. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  291. }
  292. }
  293. }
  294. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  295. $content = explode('{replace}', $content);
  296. $data['content_array'] = array();
  297. //print_r($content);die;
  298. foreach ($content as $k => $v) {
  299. $v = trim($v);
  300. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  301. $data['content_array'][] = $replace[$v];
  302. } elseif ($v) {
  303. $data['content_array'][] = array
  304. (
  305. 'type' => 'html',
  306. 'content' => $v,
  307. );
  308. }
  309. }
  310. if (!$data['content_array']) {
  311. $data['content_array'][] = array
  312. (
  313. 'type' => 'html',
  314. 'content' => $data['content'],
  315. );
  316. }
  317. unset($data['content']);
  318. //print_r($data['content_array']);die;
  319. return $data;
  320. }
  321. }