Article.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace Content\Lib;
  3. use Dever;
  4. class Article
  5. {
  6. # 根据文章id 获取文章信息
  7. public function get($data, $uid = false)
  8. {
  9. if (!is_array($data)) {
  10. $data = Dever::db('content/article')->getOne($data);
  11. }
  12. if (!$data) {
  13. Dever::alert('错误的文章信息');
  14. }
  15. $data = $this->getContent($data, $uid);
  16. return $data;
  17. }
  18. public function getContent($data, $uid = false)
  19. {
  20. //embed
  21. $data['content_array'] = array();
  22. $data['content'] = Dever::filter($data['content']);
  23. $content = $data['content'];
  24. $replace = array();
  25. # embed已废弃
  26. if (strstr($data['content'], 'embed')) {
  27. # 音频
  28. preg_match_all('/<embed src="(.*?)"(.*?)\/>/i', $content, $matches);
  29. if (isset($matches[1])) {
  30. foreach ($matches[1] as $k => $v) {
  31. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  32. if (strstr($v, '.mp4') || strstr($v, '.mov') || strstr($v, '.m3u8')) {
  33. $replace[] = array('type' => 6, 'content' => $v);
  34. } else {
  35. $replace[] = array('type' => 5, 'content' => $v);
  36. }
  37. }
  38. }
  39. }
  40. if (strstr($data['content'], 'data-applet')) {
  41. # 小程序
  42. preg_match_all('/<img(.*?)data-applet="(.*?)" \/>/', $content, $matches);
  43. if (isset($matches[2])) {
  44. foreach ($matches[2] as $k => $v) {
  45. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  46. $temp = explode('||', $v);
  47. $pic = $temp[0];
  48. $appid = $temp[1];
  49. $path = $temp[2];
  50. $appinfo = Dever::db('content/applet')->one(array('appid' => $appid));
  51. if (isset($temp[3]) && $temp[3]) {
  52. $appinfo['link'] = $temp[3];
  53. }
  54. $replace[] = array('type' => 7, 'pic_cover' => $pic, 'appid' => $appid, 'path' => $path, 'name' => $appinfo['name'], 'link' => $appinfo['link']);
  55. }
  56. }
  57. }
  58. if (strstr($data['content'], 'gif')) {
  59. # gif
  60. preg_match_all('/<img(.*?)src="(.*?)\.gif" (.*?)\/>/', $content, $matches);
  61. if (isset($matches[2])) {
  62. foreach ($matches[2] as $k => $v) {
  63. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  64. $pic = $v;
  65. $replace[] = array('type' => 8, 'pic' => $pic . '.gif');
  66. }
  67. }
  68. }
  69. if (strstr($data['content'], 'data-file')) {
  70. # 音频
  71. preg_match_all('/<img src="(.*?)" style="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  72. if (!isset($matches[1][0])) {
  73. preg_match_all('/<img style="(.*?)" src="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  74. $temp = array();
  75. if (isset($matches[2][0])) {
  76. $temp = $matches;
  77. $matches[1] = $temp[2];
  78. unset($temp);
  79. }
  80. }
  81. if (isset($matches[1])) {
  82. foreach ($matches[1] as $k => $v) {
  83. if (isset($matches[3][$k])) {
  84. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  85. $file = $matches[3][$k];
  86. $temp = explode('||', $file);
  87. $file = $temp[0];
  88. if (isset($temp[1])) {
  89. $name = $temp[1];
  90. } else {
  91. $name = '';
  92. }
  93. $cover = $v;
  94. if (strstr($v, '.mp4') || strstr($v, '.mov') || strstr($v, '.m3u8')) {
  95. $replace[] = array('type' => 6, 'content' => $file, 'cover' => $cover, 'name' => $name);
  96. } else {
  97. $replace[] = array('type' => 5, 'content' => $file, 'cover' => $cover, 'name' => $name);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. if (strstr($data['content'], 'data-id')) {
  104. # 视频+直播
  105. preg_match_all('/<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>/', $content, $matches);
  106. if (!isset($matches[2][0])) {
  107. preg_match_all('/<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>/', $content, $matches);
  108. $temp = array();
  109. if (isset($matches[2][0]) && isset($matches[3][0])) {
  110. $temp = $matches;
  111. $matches[2] = $temp[3];
  112. $matches[3] = $temp[2];
  113. unset($temp);
  114. }
  115. }
  116. if (isset($matches[2][0]) && isset($matches[3][0])) {
  117. foreach ($matches[2] as $k => $v) {
  118. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  119. if ($matches[3][$k] == 'video/lib/core.vod') {
  120. $method = 'video/lib/vod';
  121. $type = 2;
  122. } else {
  123. $type = 3;
  124. $method = 'video/lib/live';
  125. }
  126. $info = Dever::load($method)->get($v);
  127. if (isset($info['content'])) {
  128. unset($info['content']);
  129. }
  130. if ($type == 3) {
  131. # 查看是否有预约
  132. if ($uid > 0) {
  133. $info['user_act']['note'] = Dever::load('act/lib/note')->get($uid, $v, $type);
  134. } else {
  135. $info['user_act']['note'] = 2;
  136. }
  137. }
  138. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  139. }
  140. }
  141. }
  142. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  143. $content = explode('{replace}', $content);
  144. $data['content_array'] = array();
  145. //print_r($content);die;
  146. foreach ($content as $k => $v) {
  147. $v = trim($v);
  148. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  149. $data['content_array'][] = $replace[$v];
  150. } elseif ($v) {
  151. $data['content_array'][] = array
  152. (
  153. 'type' => 1,
  154. 'content' => $v,
  155. );
  156. }
  157. }
  158. if (!$data['content_array']) {
  159. $data['content_array'][] = array
  160. (
  161. 'type' => 1,
  162. 'content' => $data['content'],
  163. );
  164. }
  165. unset($data['content']);
  166. return $data;
  167. }
  168. # 获取相关推荐
  169. public function getRelation($info)
  170. {
  171. $where['noid'] = $info['id'];
  172. $where['cate_id'] = $info['cate_id'];
  173. return Dever::db('content/article')->getRelation($where);
  174. }
  175. # 增加浏览量
  176. public function addView($id)
  177. {
  178. Dever::db('content/article')->addView($id);
  179. }
  180. }