Article.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if (strstr($data['content'], 'embed')) {
  26. # 音频
  27. preg_match_all('/<embed src="(.*?)"(.*?)\/>/i', $data['content'], $matches);
  28. if (isset($matches[1])) {
  29. foreach ($matches[1] as $k => $v) {
  30. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  31. if (strstr($v, '.mp4')) {
  32. $replace[] = array('type' => 6, 'content' => $v);
  33. } else {
  34. $replace[] = array('type' => 5, 'content' => $v);
  35. }
  36. }
  37. }
  38. }
  39. if (strstr($data['content'], 'data-id')) {
  40. # 视频
  41. preg_match_all('/<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>/', $data['content'], $matches);
  42. if (!isset($matches[2][0])) {
  43. preg_match_all('/<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>/', $data['content'], $matches);
  44. $temp = array();
  45. if (isset($matches[2][0]) && isset($matches[3][0])) {
  46. $temp = $matches;
  47. $matches[2] = $temp[3];
  48. $matches[3] = $temp[2];
  49. unset($temp);
  50. }
  51. }
  52. if (isset($matches[2][0]) && isset($matches[3][0])) {
  53. foreach ($matches[2] as $k => $v) {
  54. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  55. if ($matches[3][$k] == 'video/lib/core.vod') {
  56. $method = 'video/lib/vod';
  57. $type = 2;
  58. } else {
  59. $type = 3;
  60. $method = 'video/lib/live';
  61. }
  62. $info = Dever::load($method)->get($v);
  63. if (isset($info['content'])) {
  64. unset($info['content']);
  65. }
  66. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  67. }
  68. }
  69. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  70. $content = explode('{replace}', $content);
  71. $data['content_array'] = array();
  72. //print_r($content);die;
  73. foreach ($content as $k => $v) {
  74. $v = trim($v);
  75. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  76. $data['content_array'][] = $replace[$v];
  77. } elseif ($v) {
  78. $data['content_array'][] = array
  79. (
  80. 'type' => 1,
  81. 'content' => $v,
  82. );
  83. }
  84. }
  85. }
  86. if (!$data['content_array']) {
  87. $data['content_array'][] = array
  88. (
  89. 'type' => 1,
  90. 'content' => $data['content'],
  91. );
  92. }
  93. unset($data['content']);
  94. return $data;
  95. }
  96. # 获取相关推荐
  97. public function getRelation($info)
  98. {
  99. $where['noid'] = $info['id'];
  100. $where['cate_id'] = $info['cate_id'];
  101. return Dever::db('content/article')->getRelation($where);
  102. }
  103. # 增加浏览量
  104. public function addView($id)
  105. {
  106. Dever::db('content/article')->addView($id);
  107. }
  108. }