Article.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. $replace[] = array('type' => 7, 'pic_cover' => $pic, 'appid' => $appid, 'path' => $path);
  51. }
  52. }
  53. }
  54. if (strstr($data['content'], 'data-file')) {
  55. # 音频
  56. preg_match_all('/<img src="(.*?)" style="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  57. if (!isset($matches[1][0])) {
  58. preg_match_all('/<img style="(.*?)" src="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  59. $temp = array();
  60. if (isset($matches[2][0])) {
  61. $temp = $matches;
  62. $matches[1] = $temp[2];
  63. unset($temp);
  64. }
  65. }
  66. if (isset($matches[1])) {
  67. foreach ($matches[1] as $k => $v) {
  68. if (isset($matches[3][$k])) {
  69. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  70. $file = $matches[3][$k];
  71. $temp = explode('||', $file);
  72. $file = $temp[0];
  73. if (isset($temp[1])) {
  74. $name = $temp[1];
  75. } else {
  76. $name = '';
  77. }
  78. $cover = $v;
  79. if (strstr($v, '.mp4') || strstr($v, '.mov') || strstr($v, '.m3u8')) {
  80. $replace[] = array('type' => 6, 'content' => $file, 'cover' => $cover, 'name' => $name);
  81. } else {
  82. $replace[] = array('type' => 5, 'content' => $file, 'cover' => $cover, 'name' => $name);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. if (strstr($data['content'], 'data-id')) {
  89. # 视频+直播
  90. preg_match_all('/<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>/', $content, $matches);
  91. if (!isset($matches[2][0])) {
  92. preg_match_all('/<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>/', $content, $matches);
  93. $temp = array();
  94. if (isset($matches[2][0]) && isset($matches[3][0])) {
  95. $temp = $matches;
  96. $matches[2] = $temp[3];
  97. $matches[3] = $temp[2];
  98. unset($temp);
  99. }
  100. }
  101. if (isset($matches[2][0]) && isset($matches[3][0])) {
  102. foreach ($matches[2] as $k => $v) {
  103. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  104. if ($matches[3][$k] == 'video/lib/core.vod') {
  105. $method = 'video/lib/vod';
  106. $type = 2;
  107. } else {
  108. $type = 3;
  109. $method = 'video/lib/live';
  110. }
  111. $info = Dever::load($method)->get($v);
  112. if (isset($info['content'])) {
  113. unset($info['content']);
  114. }
  115. if ($type == 3) {
  116. # 查看是否有预约
  117. if ($uid > 0) {
  118. $info['user_act']['note'] = Dever::load('act/lib/note')->get($uid, $v, $type);
  119. } else {
  120. $info['user_act']['note'] = 2;
  121. }
  122. }
  123. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  124. }
  125. }
  126. }
  127. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  128. $content = explode('{replace}', $content);
  129. $data['content_array'] = array();
  130. //print_r($content);die;
  131. foreach ($content as $k => $v) {
  132. $v = trim($v);
  133. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  134. $data['content_array'][] = $replace[$v];
  135. } elseif ($v) {
  136. $data['content_array'][] = array
  137. (
  138. 'type' => 1,
  139. 'content' => $v,
  140. );
  141. }
  142. }
  143. if (!$data['content_array']) {
  144. $data['content_array'][] = array
  145. (
  146. 'type' => 1,
  147. 'content' => $data['content'],
  148. );
  149. }
  150. unset($data['content']);
  151. return $data;
  152. }
  153. # 获取相关推荐
  154. public function getRelation($info)
  155. {
  156. $where['noid'] = $info['id'];
  157. $where['cate_id'] = $info['cate_id'];
  158. return Dever::db('content/article')->getRelation($where);
  159. }
  160. # 增加浏览量
  161. public function addView($id)
  162. {
  163. Dever::db('content/article')->addView($id);
  164. }
  165. }