From 817b32b067de6555dbbd79db7e5825c69bc6a389 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Sat, 9 Dec 2023 19:02:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=BE=E5=BC=83=E9=83=A8=E5=88=86=E5=9C=B0?= =?UTF-8?q?=E6=96=B9=E7=BC=93=E5=AD=98=E7=9A=84=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Caches/AppInfo.php | 8 ++--- app/Http/Admin/Services/Article.php | 4 +++ app/Http/Home/Views/article/edit.volt | 36 ++++++++++--------- app/Http/Home/Views/question/edit.volt | 38 +++++++++++--------- app/Library/Validators/Common.php | 9 +++++ app/Models/Article.php | 6 ++-- app/Models/Course.php | 4 +-- app/Models/Question.php | 18 ++++++++++ app/Services/Logic/Article/ArticleInfo.php | 2 +- app/Services/Logic/Article/ArticleList.php | 11 ++++++ app/Services/Logic/Comment/CommentInfo.php | 2 +- app/Services/Logic/Consult/ConsultInfo.php | 2 +- app/Services/Logic/Course/ResourceList.php | 2 +- app/Services/Logic/Question/QuestionInfo.php | 2 +- app/Services/Logic/Question/QuestionList.php | 7 ++++ app/Services/Logic/Trade/TradeInfo.php | 2 +- app/Validators/Article.php | 11 ++++++ app/Validators/Course.php | 2 +- app/Validators/Package.php | 2 +- app/Validators/PointGift.php | 2 +- app/Validators/Slide.php | 2 +- app/Validators/Topic.php | 2 +- app/Validators/Vip.php | 2 +- 23 files changed, 120 insertions(+), 56 deletions(-) diff --git a/app/Caches/AppInfo.php b/app/Caches/AppInfo.php index 0803bb9a..dd93d1b2 100644 --- a/app/Caches/AppInfo.php +++ b/app/Caches/AppInfo.php @@ -27,10 +27,10 @@ class AppInfo extends Cache $appInfo = new \App\Library\AppInfo(); return [ - 'name' => $appInfo->name, - 'alias' => $appInfo->alias, - 'link' => $appInfo->link, - 'version' => $appInfo->version, + 'name' => $appInfo->get('name'), + 'alias' => $appInfo->get('alias'), + 'link' => $appInfo->get('link'), + 'version' => $appInfo->get('version'), ]; } diff --git a/app/Http/Admin/Services/Article.php b/app/Http/Admin/Services/Article.php index 115d0f80..fb9c0b03 100644 --- a/app/Http/Admin/Services/Article.php +++ b/app/Http/Admin/Services/Article.php @@ -160,6 +160,10 @@ class Article extends Service $data['title'] = $validator->checkTitle($post['title']); } + if (isset($post['cover'])) { + $data['cover'] = $validator->checkCover($post['cover']); + } + if (isset($post['summary'])) { $data['summary'] = $validator->checkSummary($post['summary']); } diff --git a/app/Http/Home/Views/article/edit.volt b/app/Http/Home/Views/article/edit.volt index df148410..3049e516 100644 --- a/app/Http/Home/Views/article/edit.volt +++ b/app/Http/Home/Views/article/edit.volt @@ -28,24 +28,28 @@
-
- -
- + {% if category_options|length > 0 %} +
+ +
+ +
-
-
- -
-
+ {% endif %} + {% if xm_tags|length > 0 %} +
+ +
+
+
-
+ {% endif %}
diff --git a/app/Http/Home/Views/question/edit.volt b/app/Http/Home/Views/question/edit.volt index 385f34f6..d8b35ae8 100644 --- a/app/Http/Home/Views/question/edit.volt +++ b/app/Http/Home/Views/question/edit.volt @@ -2,7 +2,7 @@ {% block content %} - {% set title = question.id > 0 ? '编辑问题' : '提问题' %} + {% set title = question.id > 0 ? '编辑问题' : '发布问题' %} {% set action_url = question.id > 0 ? url({'for':'home.question.update','id':question.id}) : url({'for':'home.question.create'}) %}
-
- -
- + {% if category_options|length > 0 %} +
+ +
+ +
-
-
- -
-
+ {% endif %} + {% if xm_tags|length > 0 %} +
+ +
+
+
-
+ {% endif %}
diff --git a/app/Library/Validators/Common.php b/app/Library/Validators/Common.php index bfa7e8da..856592a2 100644 --- a/app/Library/Validators/Common.php +++ b/app/Library/Validators/Common.php @@ -114,4 +114,13 @@ class Common return $str == $date; } + public static function image($path) + { + $exts = ['png', 'gif', 'jpg', 'jpeg', 'webp']; + + $ext = pathinfo($path, PATHINFO_EXTENSION); + + return in_array(strtolower($ext), $exts); + } + } diff --git a/app/Models/Article.php b/app/Models/Article.php index cdc3e701..3ebfe3f7 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -249,9 +249,7 @@ class Article extends Model public function beforeSave() { - if (empty($this->cover)) { - $this->cover = kg_default_article_cover_path(); - } elseif (Text::startsWith($this->cover, 'http')) { + if (Text::startsWith($this->cover, 'http')) { $this->cover = self::getCoverPath($this->cover); } @@ -269,7 +267,7 @@ class Article extends Model public function afterFetch() { - if (!Text::startsWith($this->cover, 'http')) { + if (!empty($this->cover) && !Text::startsWith($this->cover, 'http')) { $this->cover = kg_cos_article_cover_url($this->cover); } diff --git a/app/Models/Course.php b/app/Models/Course.php index f72ca29d..6821c7b0 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -351,9 +351,7 @@ class Course extends Model public function beforeSave() { - if (empty($this->cover)) { - $this->cover = kg_default_course_cover_path(); - } elseif (Text::startsWith($this->cover, 'http')) { + if (Text::startsWith($this->cover, 'http')) { $this->cover = self::getCoverPath($this->cover); } diff --git a/app/Models/Question.php b/app/Models/Question.php index 66b8e05c..c06d35a3 100644 --- a/app/Models/Question.php +++ b/app/Models/Question.php @@ -11,6 +11,7 @@ use App\Caches\MaxQuestionId as MaxQuestionIdCache; use App\Services\Sync\QuestionIndex as QuestionIndexSync; use App\Services\Sync\QuestionScore as QuestionScoreSync; use Phalcon\Mvc\Model\Behavior\SoftDelete; +use Phalcon\Text; class Question extends Model { @@ -269,6 +270,10 @@ class Question extends Model public function beforeSave() { + if (Text::startsWith($this->cover, 'http')) { + $this->cover = self::getCoverPath($this->cover); + } + if (is_array($this->tags) || is_object($this->tags)) { $this->tags = kg_json_encode($this->tags); } @@ -283,11 +288,24 @@ class Question extends Model public function afterFetch() { + if (!empty($this->cover) && !Text::startsWith($this->cover, 'http')) { + $this->cover = kg_cos_article_cover_url($this->cover); + } + if (is_string($this->tags)) { $this->tags = json_decode($this->tags, true); } } + public static function getCoverPath($url) + { + if (Text::startsWith($url, 'http')) { + return parse_url($url, PHP_URL_PATH); + } + + return $url; + } + public static function publishTypes() { return [ diff --git a/app/Services/Logic/Article/ArticleInfo.php b/app/Services/Logic/Article/ArticleInfo.php index 6ad3bdbe..47c03f90 100644 --- a/app/Services/Logic/Article/ArticleInfo.php +++ b/app/Services/Logic/Article/ArticleInfo.php @@ -26,7 +26,7 @@ class ArticleInfo extends LogicService { $article = $this->checkArticle($id); - $user = $this->getCurrentUser(true); + $user = $this->getCurrentUser(); $result = $this->handleArticle($article, $user); diff --git a/app/Services/Logic/Article/ArticleList.php b/app/Services/Logic/Article/ArticleList.php index 615314f6..d34377b9 100644 --- a/app/Services/Logic/Article/ArticleList.php +++ b/app/Services/Logic/Article/ArticleList.php @@ -14,6 +14,7 @@ use App\Repos\Article as ArticleRepo; use App\Services\Category as CategoryService; use App\Services\Logic\Service as LogicService; use App\Validators\ArticleQuery as ArticleQueryValidator; +use Phalcon\Text; class ArticleList extends LogicService { @@ -77,10 +78,20 @@ class ArticleList extends LogicService $items = []; + $baseUrl = kg_cos_url(); + foreach ($articles as $article) { $article['tags'] = json_decode($article['tags'], true); + if (empty($article['cover'])) { + $article['cover'] = kg_default_article_cover_path(); + } + + if (!Text::startsWith($article['cover'], 'http')) { + $article['cover'] = $baseUrl . $article['cover']; + } + $category = $categories[$article['category_id']] ?? new \stdClass(); $owner = $users[$article['owner_id']] ?? new \stdClass(); diff --git a/app/Services/Logic/Comment/CommentInfo.php b/app/Services/Logic/Comment/CommentInfo.php index d0322310..04f873a6 100644 --- a/app/Services/Logic/Comment/CommentInfo.php +++ b/app/Services/Logic/Comment/CommentInfo.php @@ -25,7 +25,7 @@ class CommentInfo extends LogicService { $comment = $this->checkComment($id); - $user = $this->getCurrentUser(true); + $user = $this->getCurrentUser(); return $this->handleComment($comment, $user); } diff --git a/app/Services/Logic/Consult/ConsultInfo.php b/app/Services/Logic/Consult/ConsultInfo.php index 7370fef0..76177ccd 100644 --- a/app/Services/Logic/Consult/ConsultInfo.php +++ b/app/Services/Logic/Consult/ConsultInfo.php @@ -24,7 +24,7 @@ class ConsultInfo extends LogicService { $consult = $this->checkConsult($id); - $user = $this->getCurrentUser(true); + $user = $this->getCurrentUser(); return $this->handleConsult($consult, $user); } diff --git a/app/Services/Logic/Course/ResourceList.php b/app/Services/Logic/Course/ResourceList.php index abead04c..b16beda9 100644 --- a/app/Services/Logic/Course/ResourceList.php +++ b/app/Services/Logic/Course/ResourceList.php @@ -21,7 +21,7 @@ class ResourceList extends LogicService { $course = $this->checkCourse($id); - $user = $this->getCurrentUser(true); + $user = $this->getCurrentUser(); $this->setCourseUser($course, $user); diff --git a/app/Services/Logic/Question/QuestionInfo.php b/app/Services/Logic/Question/QuestionInfo.php index 939f0910..c72d8dcd 100644 --- a/app/Services/Logic/Question/QuestionInfo.php +++ b/app/Services/Logic/Question/QuestionInfo.php @@ -25,7 +25,7 @@ class QuestionInfo extends LogicService public function handle($id) { - $user = $this->getCurrentUser(true); + $user = $this->getCurrentUser(); $question = $this->checkQuestion($id); diff --git a/app/Services/Logic/Question/QuestionList.php b/app/Services/Logic/Question/QuestionList.php index 4664d9b5..1c44fd0b 100644 --- a/app/Services/Logic/Question/QuestionList.php +++ b/app/Services/Logic/Question/QuestionList.php @@ -14,6 +14,7 @@ use App\Repos\Question as QuestionRepo; use App\Services\Category as CategoryService; use App\Services\Logic\Service as LogicService; use App\Validators\QuestionQuery as QuestionQueryValidator; +use Phalcon\Text; class QuestionList extends LogicService { @@ -77,8 +78,14 @@ class QuestionList extends LogicService $items = []; + $cosUrl = kg_cos_url(); + foreach ($questions as $question) { + if (!empty($question['cover']) && !Text::startsWith($question['cover'], 'http')) { + $question['cover'] = $cosUrl . $question['cover']; + } + $question['tags'] = json_decode($question['tags'], true); $category = $categories[$question['category_id']] ?? new \stdClass(); diff --git a/app/Services/Logic/Trade/TradeInfo.php b/app/Services/Logic/Trade/TradeInfo.php index 73260166..bd06ffe4 100644 --- a/app/Services/Logic/Trade/TradeInfo.php +++ b/app/Services/Logic/Trade/TradeInfo.php @@ -26,7 +26,7 @@ class TradeInfo extends LogicService { $trade = $this->checkTradeBySn($sn); - $user = $this->getCurrentUser(true); + $user = $this->getCurrentUser(); return $this->handleTrade($trade, $user); } diff --git a/app/Validators/Article.php b/app/Validators/Article.php index 4d7598d4..2d29ec76 100644 --- a/app/Validators/Article.php +++ b/app/Validators/Article.php @@ -90,6 +90,17 @@ class Article extends Validator return $value; } + public function checkCover($cover) + { + $value = $this->filter->sanitize($cover, ['trim', 'string']); + + if (!CommonValidator::image($value)) { + throw new BadRequestException('article.invalid_cover'); + } + + return kg_cos_img_style_trim($value); + } + public function checkSummary($summary) { $value = $this->filter->sanitize($summary, ['trim', 'string']); diff --git a/app/Validators/Course.php b/app/Validators/Course.php index 43e38884..16bbae68 100644 --- a/app/Validators/Course.php +++ b/app/Validators/Course.php @@ -106,7 +106,7 @@ class Course extends Validator { $value = $this->filter->sanitize($cover, ['trim', 'string']); - if (!CommonValidator::url($value)) { + if (!CommonValidator::image($value)) { throw new BadRequestException('course.invalid_cover'); } diff --git a/app/Validators/Package.php b/app/Validators/Package.php index bcee13e3..8ccde670 100644 --- a/app/Validators/Package.php +++ b/app/Validators/Package.php @@ -69,7 +69,7 @@ class Package extends Validator { $value = $this->filter->sanitize($cover, ['trim', 'string']); - if (!CommonValidator::url($value)) { + if (!CommonValidator::image($value)) { throw new BadRequestException('package.invalid_cover'); } diff --git a/app/Validators/PointGift.php b/app/Validators/PointGift.php index 352f6504..99d7de0f 100644 --- a/app/Validators/PointGift.php +++ b/app/Validators/PointGift.php @@ -104,7 +104,7 @@ class PointGift extends Validator { $value = $this->filter->sanitize($cover, ['trim', 'string']); - if (!CommonValidator::url($value)) { + if (!CommonValidator::image($value)) { throw new BadRequestException('point_gift.invalid_cover'); } diff --git a/app/Validators/Slide.php b/app/Validators/Slide.php index 37874e42..ada286db 100644 --- a/app/Validators/Slide.php +++ b/app/Validators/Slide.php @@ -63,7 +63,7 @@ class Slide extends Validator { $value = $this->filter->sanitize($cover, ['trim', 'string']); - if (!CommonValidator::url($value)) { + if (!CommonValidator::image($value)) { throw new BadRequestException('slide.invalid_cover'); } diff --git a/app/Validators/Topic.php b/app/Validators/Topic.php index 5a146b02..d05ac0c5 100644 --- a/app/Validators/Topic.php +++ b/app/Validators/Topic.php @@ -86,7 +86,7 @@ class Topic extends Validator { $value = $this->filter->sanitize($cover, ['trim', 'string']); - if (!CommonValidator::url($value)) { + if (!CommonValidator::image($value)) { throw new BadRequestException('topic.invalid_cover'); } diff --git a/app/Validators/Vip.php b/app/Validators/Vip.php index 76a6e50e..67c04b65 100644 --- a/app/Validators/Vip.php +++ b/app/Validators/Vip.php @@ -48,7 +48,7 @@ class Vip extends Validator { $value = $this->filter->sanitize($cover, ['trim', 'string']); - if (!CommonValidator::url($value)) { + if (!CommonValidator::image($value)) { throw new BadRequestException('vip.invalid_cover'); }