Article.php 7.7 KB

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