public static function calculateRecommend($blog_id) { $post_info = Posts::findOne(['id' => $blog_id, "status" => 1]); if (!$post_info) { return self::_err("post:{$blog_id} not found"); } $post_list = Posts::find()->where(['status' => 1])->andWhere(['!=', "id", $blog_id])->orderBy("id asc")->all(); foreach ($post_list as $_relate_post_info) { similar_text(strip_tags($post_info['content']), strip_tags($_relate_post_info['content']), $tmp_content_percent); similar_text($post_info['title'], $_relate_post_info['title'], $tmp_title_percent); similar_text($post_info['tags'], $_relate_post_info['tags'], $tmp_tags_percent); $params = ["title_rate" => $tmp_title_percent, "content_rate" => $tmp_content_percent, "tags_rate" => $tmp_tags_percent]; self::setRecommend($post_info['id'], $_relate_post_info['id'], $params); } return true; }
public function actionInfo($id) { $id = intval($id); if (!$id) { return $this->goHome(); } $post_info = Posts::findOne(['id' => $id, 'status' => 1]); if (!$post_info) { return $this->goHome(); } $tags = explode(",", $post_info['tags']); $domain_m = Yii::$app->params['domains']['m']; $author = Yii::$app->params['author']; $content = preg_replace("/brush:(\\w+);toolbar:false/", "prettyprint linenums", $post_info['content']); $data = ["id" => $post_info['id'], "title" => $post_info['title'], 'view_count' => $post_info['view_count'], "content" => $content, "tags" => $tags, "author" => $author, 'updated_date' => date("Y.m.d", strtotime($post_info['updated_time'])), 'created_date' => date("Y.m.d", strtotime($post_info['created_time'])), 'url' => $domain_m . UrlService::buildWapUrl("/default/info", ["id" => $post_info['id']])]; $this->setTitle($post_info['title']); return $this->render("info", ["info" => $data, "recommend_blogs" => RecommendService::getRecommendBlog($id)]); }
public static function buildTags($post_id) { $post_info = Posts::findOne(['id' => $post_id]); if (!$post_info) { return; } $tags_arr = $post_info['tags'] ? explode(",", $post_info['tags']) : []; foreach ($tags_arr as $_tag) { $has_in = PostsTags::findOne(['posts_id' => $post_id, "tag" => $_tag]); if ($has_in) { continue; } $model_posts_tag = new PostsTags(); $model_posts_tag->posts_id = $post_id; $model_posts_tag->tag = $_tag; $model_posts_tag->save(0); } }
public function actionInfo($id) { $id = intval($id); if (!$id) { return $this->goHome(); } $post_info = Posts::findOne(['id' => $id, 'status' => 1]); if (!$post_info) { return $this->goHome(); } $author = Yii::$app->params['author']; $tags = explode(",", $post_info['tags']); $content = preg_replace("/brush:(\\w+);toolbar:false/", "prettyprint linenums", $post_info['content']); $content = str_replace("<pre>", "<pre class='prettyprint linenums'>", $content); $data = ["id" => $post_info['id'], "title" => $post_info['title'], "content" => $content, "original" => $post_info['original'], 'view_count' => $post_info['view_count'], "tags" => $tags, 'date' => date("Y.m.d", strtotime($post_info['updated_time'])), 'author' => $author, "url" => UrlService::buildUrl("/default/info", ["id" => $post_info['id']])]; $prev_info = Posts::find()->where(["<", "id", $id])->andWhere(['status' => 1])->orderBy("id desc")->one(); $next_info = Posts::find()->where([">", "id", $id])->andWhere(['status' => 1])->orderBy("id asc")->one(); $this->setTitle($post_info['title']); return $this->render("detail", ["info" => $data, "prev_info" => $prev_info, "next_info" => $next_info, "recommend_blogs" => RecommendService::getRecommendBlog($id)]); }
public function actionOps($id) { $id = intval($id); $act = trim($this->post("act", "online", "down-hot", "go-hot")); if (!$id) { return $this->renderJSON([], "操作的博文可能不是你的吧!!", -1); } $post_info = Posts::findOne(["id" => $id, 'uid' => [0, $this->current_user->uid]]); if (!$post_info) { return $this->renderJSON([], "操作的博文可能不是你的吧!!", -1); } switch ($act) { case "del": $post_info->status = 0; break; case "online": $post_info->status = 1; BlogService::buildTags($id); break; case "go-hot": $post_info->hot = 1; break; case "down-hot": $post_info->hot = 0; } $post_info->updated_time = date("Y-m-d H:i:s"); $post_info->update(0); switch ($act) { case "del": $this->afterDel($id); break; case "online": $this->afterOnline($id); break; } return $this->renderJSON([], "操作成功!!"); }
public static function doSync($type, $blog_id) { $charset = "utf-8"; $catlog = [1]; $limit_title_len = 0; //不限制 switch ($type) { case "51cto": $catlog = ["【创作类型:原创】", "开发技术"]; $charset = "gb2312"; break; case "csdn": $catlog = []; //return self::_err("目前还处理不了这个类型!!"); break; case "sina": break; case "163": break; case "oschina": break; case "cnblogs": break; case "chinaunix": $catlog = ["Web开发"]; $limit_title_len = 30; break; default: return self::_err("指定的类型无法处理!!"); break; } $metaweblog_blog_config = \Yii::$app->params['metaweblog']; if (!isset($metaweblog_blog_config[$type])) { return self::_err("指定的类型无法处理!!"); } $metaweblog_config = $metaweblog_blog_config[$type]; $blog_info = Posts::findOne(['id' => $blog_id, "status" => 1]); if (!$blog_info) { return self::_err("博客不存在哦!!"); } $domain_blog = \Yii::$app->params['domains']['blog']; $link = $domain_blog . "/default/{$blog_id}.html?source={$type}"; $content = $blog_info['content']; $title = $blog_info['title']; if ($limit_title_len) { //标题限制长度 $title = mb_substr($title, 0, $limit_title_len, "utf-8"); } $content .= "<br/>原文地址:<a href=\"{$link}\">{$blog_info['title']}</a>"; /*相关标签*/ if ($blog_info['tags']) { $content .= "<br/>标签:"; $tmp_tags = explode(",", $blog_info['tags']); foreach ($tmp_tags as $_tmp_tag) { $tmp_link = $domain_blog . "/search/do?kw={$_tmp_tag}&source_id={$blog_id}&source={$type}"; $content .= "<a href='{$tmp_link}'>{$_tmp_tag}</a> "; } } /*加入推荐*/ $recommend_blogs = RecommendService::getRecommendBlog($blog_id); if ($recommend_blogs) { $content .= "<br/><br/><h3>智能推荐</h3><ul>"; foreach ($recommend_blogs as $_recommend_blog) { $tmp_link = $domain_blog . "/default/{$_recommend_blog['id']}.html?flag=recommend&source={$type}&source_id=" . $_recommend_blog['source_id']; $content .= "<li><a href='{$tmp_link}'>{$_recommend_blog["title"]}</a></li>"; } $content .= "</ul>"; } $content = htmlspecialchars($content); $title = htmlspecialchars($title); if ($charset != "utf-8") { $content = mb_convert_encoding($content, $charset, "utf-8"); $title = mb_convert_encoding($title, $charset, "utf-8"); } $params = ['title' => $title, 'description' => $content, 'categories' => $catlog]; $target = new MetaWeblog($metaweblog_config['url'], $charset); $target->setAuth($metaweblog_config['username'], $metaweblog_config['passwd']); $date_now = date("Y-m-d H:i:s"); $sync_info = BlogSyncMapping::findOne(['blog_id' => $blog_id]); $is_edit = false; if ($sync_info && $sync_info[self::$type_mapping[$type]]) { if (!$target->editPost($sync_info[self::$type_mapping[$type]], $params)) { return self::_err($target->getErrorCode() . ":" . $target->getErrorMessage() . " xml data:" . $target->getResponse()); } $model_blog_sync_mapping = $sync_info; $is_edit = true; } else { if (!$target->newPost($params, $type == "csdn")) { return self::_err($target->getErrorCode() . ":" . $target->getErrorMessage() . " xml data:" . $target->getResponse()); } if ($sync_info) { $model_blog_sync_mapping = $sync_info; } else { $model_blog_sync_mapping = new BlogSyncMapping(); $model_blog_sync_mapping->blog_id = $blog_id; $model_blog_sync_mapping->created_time = $date_now; } } $sync_blog_id = $target->getBlogId(); if (!$is_edit) { $model_blog_sync_mapping[self::$type_mapping[$type]] = $sync_blog_id; } $model_blog_sync_mapping->updated_time = $date_now; if (!$model_blog_sync_mapping->save(0)) { return false; } return $target->getResponse(); }
public function actionAdd() { $referer = trim($this->get("referer", "")); $screen = trim($this->get("screen", "")); $target_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ""; if ($target_url) { $blog_id = 0; preg_match("/\\/default\\/(\\d+)(.html)?/", $target_url, $matches); if ($matches && count($matches) >= 2) { $blog_id = $matches[1]; } $tmp_source = 'direct'; if ($referer) { $tmp_source = parse_url($referer, PHP_URL_HOST); if (stripos($tmp_source, "www.google.") !== false) { $tmp_source = "www.google.com"; } } $uuid = $this->getUUID(); $uuid = ltrim($uuid, "{"); $uuid = rtrim($uuid, "}"); $model_ac_log = new AccessLogs(); $model_ac_log->referer = $referer; $model_ac_log->target_url = $target_url; $model_ac_log->blog_id = $blog_id; $model_ac_log->source = $tmp_source ? $tmp_source : ''; $model_ac_log->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ""; if ($model_ac_log->user_agent) { $tmp_browser = new Browser($model_ac_log->user_agent); $tmp_os = new Os($model_ac_log->user_agent); $tmp_device = new Device($model_ac_log->user_agent); $model_ac_log->client_browser = $tmp_browser->getName() ? $tmp_browser->getName() : ''; $model_ac_log->client_browser_version = $tmp_browser->getVersion() ? $tmp_browser->getVersion() : ''; $model_ac_log->client_os = $tmp_os->getName() ? $tmp_os->getName() : ''; $model_ac_log->client_os_version = $tmp_os->getVersion() ? $tmp_os->getVersion() : ''; $model_ac_log->client_device = $tmp_device->getName() ? $tmp_device->getName() : ''; if ($model_ac_log->client_device == "unknown" && UtilHelper::isPC()) { $model_ac_log->client_device = "pc"; } } $model_ac_log->ip = UtilHelper::getClientIP(); $model_ac_log->uuid = $uuid; if ($screen) { list($client_width, $client_height) = explode("/", $screen); if ($client_width) { $model_ac_log->client_width = $client_width; } if ($client_height) { $model_ac_log->client_height = $client_height; } } $model_ac_log->created_time_min = date("Y-m-d H:i"); $model_ac_log->created_time = date("Y-m-d H:i:s"); $model_ac_log->save(); /*更新文章阅读量*/ if ($blog_id) { $blog_info = Posts::findOne(['id' => $blog_id]); if ($blog_info) { $blog_info->view_count += 1; $blog_info->update(0); } } } }