Article.php 5.2 KB

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