这个代码是从别人那复制来的,有需要的随便用。
易优cms(eyoucms)免登陆发布插件php
<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
//version 1.0.2
class xiaofamao extends Controller
{
/********用户可以自己修改的地方 =======开始 ======= ***********/
//这个密码是发布验证用的.小发猫发布目标中的“发布插件密码”需设置和这里一样的密码....请注意一定需要修改.
const kds_password = "xiaofamao.com";//请把xiaofamao.com修改为自定义密码
//标题是否去重
const titleUnique = true;//标题默认去重 去重(不允许重复)true; 不去重(允许重复)false;
/********用户可以自己修改的地方 ======= 结束 ======= ***********/
public function insertArchives()
{
$_REQ = $this->xiaofamao_mergeRequest();
if (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == "on") {
$http = "https://";
} else {
$http = "http://";
}
$domain = $http.str_replace('\\', '/', $_SERVER['HTTP_HOST']);
if (isset($_REQ["__kds_flag"])){
if ($_REQ["__kds_flag"] == "post") {//提交
//密码校验
if (empty($_REQ['kds_password']) || $_REQ['kds_password'] != self::kds_password) {
$this->xiaofamao_failRsp(1403, "password error", "提交的发布密码错误");
}
//检查标题
$title = isset($_REQ['title']) ? addslashes($_REQ['title']) : '';//标题
if (empty($title)) {
$this->xiaofamao_failRsp(1404, "title is empty", "标题不能为空");
}
//检查内容
$content = isset($_REQ['content']) ? $_REQ['content'] : '';
if (empty($content)) {
$this->xiaofamao_failRsp(1404, "content is empty", "内容不能为空");
}
//检查栏目
$typeid = isset($_REQ['typeid']) ? addslashes($_REQ['typeid']) : '';
if (empty($typeid)) {
$this->xiaofamao_failRsp(1404, "typeid is empty", "栏目ID不能为空");
}
//检查频道模型
$channel = isset($_REQ['channel']) ? addslashes($_REQ['channel']) : '';
if (empty($channel)) {
$this->xiaofamao_failRsp(1404, "channel is empty", "频道模型不能为空");
}
if(self::titleUnique){
$archivesList=Db::name('archives')->where('title',$title)->select();
$existAid = $archivesList[0]['aid'];
if($existAid>0){
return $this->xiaofamao_successRsp(array("url" => $domain . "/index.php?m=home&c=View&a=index&aid=" . $existAid),'标题已存在');
}
}
$seo_keywords = $_REQ['seo_keywords'];
if (!empty($seo_keywords)) {
$seo_keywords = str_replace(',', ',', $seo_keywords);
}
//提取文章内容中第一张图片作为封面
//调整成小发猫的方法
//$litpic = get_html_first_imgurl($content) ?: '';
$litpic = $this->getThumb($_REQ);
//是否有封面图
if (empty($litpic)) {
$is_litpic = 0; // 无封面图
} else {
$is_litpic = 1; // 有封面图
}
//图片没有下载时,调用系统的取得正文内容第一张图片的方法
if (empty($litpic) && $_REQ['__kds_download_imgs_flag']!="true") {
$litpic = get_html_first_imgurl($content) ?: '';
if (empty($litpic)) {
$is_litpic = 0; // 无封面图
} else {
$is_litpic = 1; // 有封面图
}
}
// SEO描述
$seo_description = '';
if (empty($_REQ['seo_description']) && !empty($content)) {
$seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
} else {
$seo_description = $_REQ['seo_description'];
}
if (!empty($_REQ['add_time'])) {
$add_time = $_REQ['add_time'];
}else{
$add_time = time();
}
// --存储数据
$newData = array(
'typeid'=>$typeid,
'channel'=>$channel,
'is_b' => empty($_REQ['is_b']) ? 0 : $_REQ['is_b'],
'title' =>$title,
'litpic' =>$litpic,
'is_head' => empty($_REQ['is_head']) ? 0 : $_REQ['is_head'],//头条(0=否,1=是)
'is_special' => empty($_REQ['is_special']) ? 0 : $_REQ['is_special'],//特荐(0=否,1=是)
'is_top' => empty($_REQ['is_top']) ? 0 : $_REQ['is_top'],//置顶(0=否,1=是)
'is_recom' => empty($_REQ['is_recom']) ? 0 : $_REQ['is_recom'],//推荐(0=否,1=是)
'is_litpic' => $is_litpic,
'author'=>$_REQ['author'],
'click' => mt_rand(100, 300),
'arcrank'=>empty($_REQ['arcrank']) ? 0 : $_REQ['arcrank'],//阅读权限:0=开放浏览,-1=待审核稿件
'seo_title' =>$_REQ['seo_title'],
'seo_keywords'=> $seo_keywords,
'seo_description' => $seo_description,
'tempview' =>'view_article.htm',
'status' =>1,
'admin_id' => 1,
'lang' => $this->admin_lang,
'sort_order' => 100,
'add_time' => $add_time,
'update_time' => $add_time,
'content' => $content,
'tags' => $_REQ['tags'],
);
$aid = M('archives')->insertGetId($newData);
if ($aid) {
//添加文章内容
$this->afterSave($aid, $newData, 'add');
$docFinalUrl=$domain.'/index.php?m=home&c=View&a=index&aid='.$aid;
/图片http下载,不能用_POST
$this->downloadImages($_REQ);
$this->xiaofamao_successRsp(array("url" => $docFinalUrl));
} else {
$this->xiaofamao_failRsp(1403, "insert archives,article_content error", "文章发布错误");
}
}
}
}
/**
* 后置操作方法
* 自定义的一个函数 用于数据保存后做的相应处理操作, 使用时手动调用
* @param int $aid 产品id
* @param $data
* @param string $opt 操作
* @return void
*/
private function afterSave($aid, $data, $opt)
{
//写入文章内容
Db::name('article_content')->data(array(
'aid' => $aid,
'content' => $data['content'],
'add_time' => $data['add_time'],
'update_time' => $data['update_time']
))->insert();
// --处理TAG标签
model('Taglist')->savetags($aid, $data['typeid'], $data['tags']);
}
/**
* 获取文件完整路径
* @return string
*/
private function getFilePath(){
//$rootUrl=$this->options->siteUrl();
//使用php的方法试试
///uploads/ueditor/20200620/1-20062010343IR.jpeg
$rootUrl=dirname(dirname(dirname(dirname(__FILE__))));
return $rootUrl.'/uploads/ueditor';
}
/**
* 查找文件夹,如不存在就创建并授权
* @return string
*/
private function createFolders($dir){
return is_dir($dir) or ($this->createFolders(dirname($dir)) and mkdir($dir, 0777));
}
//取得文章正文内容的第一张图片做为缩略图
private function getThumb($post){
try{
$downloadFlag = isset($post['__kds_download_imgs_flag']) ? $post['__kds_download_imgs_flag'] : '';
if (!empty($downloadFlag) && $downloadFlag== "true") {
$docImgsStr = isset($post['__kds_docImgs']) ? $post['__kds_docImgs'] : '';
if (!empty($docImgsStr)) {
$docImgs = explode(',',$docImgsStr);
if (is_array($docImgs)) {
//
//次数计算
$i = 0;
foreach ($docImgs as $imgUrl) {
$i = $i+1;
$urlItemArr = explode('/',$imgUrl);
$itemLen=count($urlItemArr);
if($itemLen>=3){
//最后的相对路径,如 2018/06
$fileRelaPath=$urlItemArr[$itemLen-3].'/'.$urlItemArr[$itemLen-2];
$imgName=$urlItemArr[$itemLen-1];
$finalPath='/uploads/ueditor'. '/'.$fileRelaPath. '/' . $imgName;
if($i==1){
return $finalPath;
}
}
}//.for
}//..is_array
}
}
} catch (Exception $ex) {
//error_log('image download error:'. $e->getMessage(), 3, '/var/log/ecms_test.log');
}
}
图片http下载
private function downloadImages($post){
try{
$downloadFlag = isset($post['__kds_download_imgs_flag']) ? $post['__kds_download_imgs_flag'] : '';
if (!empty($downloadFlag) && $downloadFlag== "true") {
$docImgsStr = isset($post['__kds_docImgs']) ? $post['__kds_docImgs'] : '';
if (!empty($docImgsStr)) {
$docImgs = explode(',',$docImgsStr);
if (is_array($docImgs)) {
$uploadDir = $this->getFilePath();
foreach ($docImgs as $imgUrl) {
$urlItemArr = explode('/',$imgUrl);
$itemLen=count($urlItemArr);
if($itemLen>=3){
//最后的相对路径,如 2018/06
$fileRelaPath=$urlItemArr[$itemLen-3].'/'.$urlItemArr[$itemLen-2];
$imgName=$urlItemArr[$itemLen-1];
$finalPath=$uploadDir. '/'.$fileRelaPath;
if ($this->createFolders($finalPath)) {
$file = $finalPath . '/' . $imgName;
if(!file_exists($file)){
$doc_image_data = file_get_contents($imgUrl);
file_put_contents($file, $doc_image_data);
}
}
}
}
}
}
}
} catch (Exception $ex) {
//error_log('error:'.$e->
}
}
private function xiaofamao_mergeRequest() {
if (isset($_GET['__kds_flag'])) {
$_REQ = array_merge($_GET, $_POST);
} else {
$_REQ = $_POST;
}
return $_REQ ;
}
private function xiaofamao_successRsp($data = "", $msg = "") {
$this->xiaofamao_rsp(1,0, $data, $msg);
}
private function xiaofamao_failRsp($code = 0, $data = "", $msg = "") {
$this->xiaofamao_rsp(0,$code, $data, $msg);
}
private function xiaofamao_rsp($result = 1,$code = 0, $data = "", $msg = "") {
die(json_encode(array("rs" => $result, "code" => $code, "data" => $data, "msg" => urlencode($msg))));
}
}