Article.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace Content\Lib;
  3. use Dever;
  4. class Article
  5. {
  6. # 根据文章id 获取文章信息
  7. public function get($data)
  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);
  16. return $data;
  17. }
  18. public function getContent($data)
  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', $data['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-file')) {
  41. # 音频
  42. preg_match_all('/<img src="(.*?)" style="(.*?)" data-file="(.*?)" \/>/', $data['content'], $matches);
  43. if (!isset($matches[1][0])) {
  44. preg_match_all('/<img style="(.*?)" src="(.*?)" data-file="(.*?)" \/>/', $data['content'], $matches);
  45. $temp = array();
  46. if (isset($matches[2][0])) {
  47. $temp = $matches;
  48. $matches[1] = $temp[2];
  49. unset($temp);
  50. }
  51. }
  52. if (isset($matches[1])) {
  53. foreach ($matches[1] as $k => $v) {
  54. if (isset($matches[3][$k])) {
  55. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  56. $file = $matches[3][$k];
  57. $temp = explode('||', $file);
  58. $file = $temp[0];
  59. $name = $temp[1];
  60. $cover = $v;
  61. if (strstr($v, '.mp4') || strstr($v, '.mov') || strstr($v, '.m3u8')) {
  62. $replace[] = array('type' => 6, 'content' => $file, 'cover' => $cover, 'name' => $name);
  63. } else {
  64. $replace[] = array('type' => 5, 'content' => $file, 'cover' => $cover, 'name' => $name);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. if (strstr($data['content'], 'data-id')) {
  71. # 视频+直播
  72. preg_match_all('/<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>/', $data['content'], $matches);
  73. if (!isset($matches[2][0])) {
  74. preg_match_all('/<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>/', $data['content'], $matches);
  75. $temp = array();
  76. if (isset($matches[2][0]) && isset($matches[3][0])) {
  77. $temp = $matches;
  78. $matches[2] = $temp[3];
  79. $matches[3] = $temp[2];
  80. unset($temp);
  81. }
  82. }
  83. if (isset($matches[2][0]) && isset($matches[3][0])) {
  84. foreach ($matches[2] as $k => $v) {
  85. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  86. if ($matches[3][$k] == 'video/lib/core.vod') {
  87. $method = 'video/lib/vod';
  88. $type = 2;
  89. } else {
  90. $type = 3;
  91. $method = 'video/lib/live';
  92. }
  93. $info = Dever::load($method)->get($v);
  94. if (isset($info['content'])) {
  95. unset($info['content']);
  96. }
  97. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  98. }
  99. }
  100. }
  101. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  102. $content = explode('{replace}', $content);
  103. $data['content_array'] = array();
  104. //print_r($content);die;
  105. foreach ($content as $k => $v) {
  106. $v = trim($v);
  107. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  108. $data['content_array'][] = $replace[$v];
  109. } elseif ($v) {
  110. $data['content_array'][] = array
  111. (
  112. 'type' => 1,
  113. 'content' => $v,
  114. );
  115. }
  116. }
  117. if (!$data['content_array']) {
  118. $data['content_array'][] = array
  119. (
  120. 'type' => 1,
  121. 'content' => $data['content'],
  122. );
  123. }
  124. unset($data['content']);
  125. return $data;
  126. }
  127. # 获取相关推荐
  128. public function getRelation($info)
  129. {
  130. $where['noid'] = $info['id'];
  131. $where['cate_id'] = $info['cate_id'];
  132. return Dever::db('content/article')->getRelation($where);
  133. }
  134. # 增加浏览量
  135. public function addView($id)
  136. {
  137. Dever::db('content/article')->addView($id);
  138. }
  139. }