Article.php 7.0 KB

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