/** * 发布文章 * @param LJL_Request $input * @param LJL_Response $output */ public function doPublish(LJL_Request $input, LJL_Response $output) { $isPublish = intval($input->post('ispublish')); //0:预览,1:发布 $articleId = intval($input->post('articleId')); //0:插入,否则:修改 $cate = $input->post('cate'); $title = $input->post('title'); $source = $input->post('source'); $tags = $input->post('tags'); $content = htmlspecialchars(addslashes($input->post('content', 1))); $imgArr = $input->post('imgArr', 1); //首图id $firstImgUrl = $this->matchFirstPic($input->post('content', 1)); $firstImgName = $this->getFirtstPicName($firstImgUrl); $firstImgId = Helper_Blog::getPicId(array('picName' => $firstImgName)); //有无首图截取的是不一样的 $desc = $firstImgId ? API_Item_Base_String::getShort(array('str' => $input->post('content'), 'length' => 120)) : API_Item_Base_String::getShort(array('str' => $input->post('content'), 'length' => 125)); //数据集 $insertData = array('isPublished' => $isPublish, 'firstImgId' => $firstImgId, 'cate' => $cate, 'title' => $title, 'descript' => $desc, 'source' => $source, 'tags' => $tags, 'content' => $content, 'imgArr' => $imgArr, 'updateTime' => SYSTEM_TIME); //update if ($articleId) { $insertData['updateTime'] = SYSTEM_TIME; Helper_Blog::updateArticleInfo(array('articleId' => $articleId, 'updateData' => $insertData)); //insert } else { //先查看下数据库中是否已经有该文章的题目了(分类也要一致才是) if (Helper_Blog::ishasArticle($cate, $title)) { echo 'error'; die; } $insertData['insertTime'] = SYSTEM_TIME; $articleId = Helper_Blog::insertArticleInfo(array('insertData' => $insertData)); } echo $articleId; die; }
/** * 插入文章 */ private function insertArticle($url, $title, $content) { if (!$title || !$content) { return; } $db = Db_Blog::instance(null, $this->website); //已有该文章则退出 $title = htmlspecialchars(strip_tags(addslashes(str_replace("'", '"', $title)))); if (Helper_Blog::ishasArticle('', $title)) { return; } //echo $title;die; //对于有图片的文章进行搬运图片处理 preg_match_all("/<img src=\"(.*?)\"/", $content, $matches, PREG_SET_ORDER); $imgidArr = array(); if ($matches) { if (count($matches) > 10) { return; } foreach ((array) $matches as $imgsrc) { $imgInfo = $this->getImgNameDir(); if (!$imgsrc[1]) { continue; } //1、移动图片 exec('wget -q --tries=3 -O ' . rtrim($imgInfo[1], '/') . '/' . $imgInfo[0] . '.jpg ' . $imgsrc[1]); //2、向自己库中插入图片 $imgidArr[] = Helper_Blog::insertPic(array('picName' => $imgInfo[0], 'picExt' => 'jpg', 'time' => $imgInfo[3])); //3、图片地址字符串替换 $content = str_replace($imgsrc[1], $imgInfo[2] . '/' . $imgInfo[0] . '.jpg', $content); } } //插入文章 $firstImgId = $imgidArr ? $imgidArr[0] : 0; $desc = $firstImgId ? API_Item_Base_String::getShort(array('str' => strip_tags($content), 'length' => 120)) : API_Item_Base_String::getShort(array('str' => strip_tags($content), 'length' => 150)); $articleId = Helper_Blog::insertArticleInfo(array('insertData' => array('firstImgId' => $firstImgId, 'insertTime' => SYSTEM_TIME, 'isPublished' => 0, 'cate' => $this->cate, 'tags' => $this->tags, 'title' => $title, 'descript' => ' ' . strip_tags(addslashes(str_replace("'", '"', $desc))) . ' ', 'source' => $url, 'content' => ' ' . htmlspecialchars(addslashes(preg_replace("/<script[\\s]*.*?<\\/script>/si", '', $content))) . ' ', 'imgArr' => $imgidArr))); echo 'article ' . $articleId . ' is ok' . PHP_EOL; }