Content.php 12 KB

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