Article.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-05-18 15:35
  7. */
  8. namespace console\models;
  9. use Yii;
  10. use common\models\ArticleContent;
  11. use yii\helpers\FileHelper;
  12. class Article extends \common\models\Article
  13. {
  14. public $articleOriginUrl = '';//article origin url
  15. public function beforeSave($insert)
  16. {
  17. if ($this->flag_headline == null) {
  18. $this->flag_headline = 0;
  19. }
  20. if ($this->flag_recommend == null) {
  21. $this->flag_recommend = 0;
  22. }
  23. if ($this->flag_slide_show == null) {
  24. $this->flag_slide_show = 0;
  25. }
  26. if ($this->flag_special_recommend == null) {
  27. $this->flag_special_recommend = 0;
  28. }
  29. if ($this->flag_roll == null) {
  30. $this->flag_roll = 0;
  31. }
  32. if ($this->flag_bold == null) {
  33. $this->flag_bold = 0;
  34. }
  35. if ($this->flag_picture == null) {
  36. $this->flag_picture = 0;
  37. }
  38. $this->tag = str_replace(',', ',', $this->tag);
  39. $this->seo_keywords = str_replace(',', ',', $this->seo_keywords);
  40. if ($insert) {
  41. $this->author_id = 0;
  42. $this->author_name = 'robot';
  43. } else {
  44. $this->updated_at = time();
  45. }
  46. $this->scrawThumb();
  47. return true;
  48. }
  49. public function afterSave($insert, $changedAttributes)
  50. {
  51. if ($insert) {
  52. $contentModel = new ArticleContent();
  53. $contentModel->aid = $this->id;
  54. } else {
  55. if ($this->content === null) {
  56. return;
  57. }
  58. $contentModel = ArticleContent::findOne(['aid' => $this->id]);
  59. if ($contentModel == null) {
  60. $contentModel = new ArticleContent();
  61. $contentModel->aid = $this->id;
  62. }
  63. }
  64. $this->scrawlPic();
  65. $contentModel->content = $this->content;
  66. $contentModel->save();
  67. }
  68. public function needScrawlPic($url)
  69. {
  70. if (strpos($url, "upaiyun.com")) {
  71. return true;
  72. }
  73. if (strpos($url, '/') === 0) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. public function scrawThumb()
  79. {
  80. if ($this->needScrawlPic($this->thumb)) {
  81. $path = Yii::getAlias("@thumb/robot/");
  82. if (! file_exists($path)) {
  83. FileHelper::createDirectory($path);
  84. }
  85. $ext = pathinfo($this->thumb)['extension'];
  86. $fileName = uniqid() . '.' . $ext;
  87. if (strpos($this->thumb, '/') === 0) {
  88. $temp = parse_url($this->articleOriginUrl);
  89. $this->thumb = $temp['scheme'] . '://' . $temp['host'] . $this->thumb;
  90. }
  91. $imgBin = file_get_contents($this->thumb);
  92. if (file_put_contents($path . $fileName, $imgBin)) {
  93. $temp = explode("uploads/", $path . $fileName);
  94. $this->thumb = Yii::$app->params['site']['sign'] . "/uploads/" . $temp[1];
  95. }
  96. }
  97. }
  98. public function scrawlPic()
  99. {
  100. preg_match_all("/<img.*?src=[\'\"](.*?)[\'\"]/", $this->content, $matches);
  101. if (count($matches[1]) <= 0) {
  102. return;
  103. }
  104. $replace = [];
  105. foreach ($matches[1] as $key => $val) {
  106. if (! $this->needScrawlPic($val)) {
  107. unset($matches[1][$key]);
  108. continue;
  109. }
  110. $path = Yii::getAlias("@article/robot/") . date('Y/m/');
  111. if (! file_exists($path)) {
  112. FileHelper::createDirectory($path);
  113. }
  114. $ext = pathinfo($val)['extension'];
  115. $fileName = uniqid() . '.' . $ext;
  116. if (strpos($val, '/') === 0) {
  117. $temp = parse_url($this->articleOriginUrl);
  118. $val = $temp['scheme'] . '://' . $temp['host'] . $val;
  119. }
  120. $imgBin = file_get_contents($val);
  121. if (! file_put_contents($path . $fileName, $imgBin)) {
  122. unset($matches[1][$key]);
  123. } else {
  124. $temp = explode("uploads/", $path . $fileName);
  125. array_push($replace, Yii::$app->params['site']['sign'] . "/uploads/" . $temp[1]);
  126. }
  127. }
  128. $this->content = str_replace($matches[1], $replace, $this->content);
  129. }
  130. }