Article.php 6.7 KB

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