phpcms编辑器自动下载外站的图片存在BUG
1、如果外站图片地址还参数如https://i1.hoopchina.com.cn/blogfile/201701/16/BbsImg148455553563159_2448x2448.jpg?x-oss-process=image/resize,w_800/format,jpg
地址后面有?后面一串字符, 正则去匹配 带参数的匹配 不到
2、https开头的图片也下载不了,phpcms没有对https的图片做判断
这两种情况只要出现一种就下载不下来了修改方法,直接上代码
1、/phpcms/libs/attachment.class.php
<?php
class attachment {
var $contentid;
var $module;
var $catid;
var $attachments;
var $field;
var $imageexts = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
var $uploadedfiles = array();
var $downloadedfiles = array();
var $error;
var $upload_root;
var $siteid;
var $site = array();
function __construct($module='', $catid = 0,$siteid = 0,$upload_dir = '') {
$this->catid = intval($catid);
$this->siteid = intval($siteid)== 0 ? 1 : intval($siteid);
$this->module = $module ? $module : 'content';
pc_base::load_sys_func('dir');
pc_base::load_sys_class('image','','0');
$this->upload_root = pc_base::load_config('system','upload_path');
$this->upload_func = 'copy';
$this->upload_dir = $upload_dir;
}
/**
* 附件上传方法
* @param $field 上传字段
* @param $alowexts 允许上传类型
* @param $maxsize 最大上传大小
* @param $overwrite 是否覆盖原有文件
* @param $thumb_setting 缩略图设置
* @param $watermark_enable 是否添加水印
*/
function upload($field, $alowexts = '', $maxsize = 0, $overwrite = 0,$thumb_setting = array(), $watermark_enable = 1) {
if(!isset($_FILES[$field])) {
$this->error = UPLOAD_ERR_OK;
return false;
}
if(empty($alowexts) || $alowexts == '') {
$site_setting = $this->_get_site_setting($this->siteid);
$alowexts = $site_setting['upload_allowext'];
}
$fn = $_GET['CKEditorFuncNum'] ? $_GET['CKEditorFuncNum'] : '1';
$this->field = $field;
$this->savepath = $this->upload_root.$this->upload_dir.date('Y/md/');
$this->alowexts = $alowexts;
$this->maxsize = $maxsize;
$this->overwrite = $overwrite;
$uploadfiles = array();
$description = isset($GLOBALS[$field.'_description']) ? $GLOBALS[$field.'_description'] : array();
if(is_array($_FILES[$field]['error'])) {
$this->uploads = count($_FILES[$field]['error']);
foreach($_FILES[$field]['error'] as $key => $error) {
if($error === UPLOAD_ERR_NO_FILE) continue;
if($error !== UPLOAD_ERR_OK) {
$this->error = $error;
return false;
}
$uploadfiles[$key] = array('tmp_name' => $_FILES[$field]['tmp_name'][$key], 'name' => $_FILES[$field]['name'][$key], 'type' => $_FILES[$field]['type'][$key], 'size' => $_FILES[$field]['size'][$key], 'error' => $_FILES[$field]['error'][$key], 'description'=>$description[$key],'fn'=>$fn);
}
} else {
$this->uploads = 1;
if(!$description) $description = '';
$uploadfiles[0] = array('tmp_name' => $_FILES[$field]['tmp_name'], 'name' => $_FILES[$field]['name'], 'type' => $_FILES[$field]['type'], 'size' => $_FILES[$field]['size'], 'error' => $_FILES[$field]['error'], 'description'=>$description,'fn'=>$fn);
}
if(!dir_create($this->savepath)) {
$this->error = '8';
return false;
}
if(!is_dir($this->savepath)) {
$this->error = '8';
return false;
}
@chmod($this->savepath, 0777);
if(!is_writeable($this->savepath)) {
$this->error = '9';
return false;
}
if(!$this->is_allow_upload()) {
$this->error = '13';
return false;
}
$aids = array();
foreach($uploadfiles as $k=>$file) {
$fileext = fileext($file['name']);
if($file['error'] != 0) {
$this->error = $file['error'];
return false;
}
if(!preg_match("/^(".$this->alowexts.")$/", $fileext)) {
$this->error = '10';
return false;
}
if($this->maxsize && $file['size'] > $this->maxsize) {
$this->error = '11';
return false;
}
if(!$this->isuploadedfile($file['tmp_name'])) {
$this->error = '12';
return false;
}
$temp_filename = $this->getname($fileext);
$savefile = $this->savepath.$temp_filename;
$savefile = preg_replace("/(php|phtml|php3|php4|jsp|exe|dll|asp|cer|asa|shtml|shtm|aspx|asax|cgi|fcgi|pl)(\.|$)/i", "_\\1\\2", $savefile);
$filepath = preg_replace(new_addslashes("|^".$this->upload_root."|"), "", $savefile);
if(!$this->overwrite && file_exists($savefile)) continue;
$upload_func = $this->upload_func;
if(@$upload_func($file['tmp_name'], $savefile)) {
$this->uploadeds++;
@chmod($savefile, 0644);
@unlink($file['tmp_name']);
$file['name'] = iconv("utf-8",CHARSET,$file['name']);
$file['name'] = safe_replace($file['name']);
$uploadedfile = array('filename'=>$file['name'], 'filepath'=>$filepath, 'filesize'=>$file['size'], 'fileext'=>$fileext, 'fn'=>$file['fn']);
$thumb_enable = is_array($thumb_setting) && ($thumb_setting[0] > 0 || $thumb_setting[1] > 0 ) ? 1 : 0;
$image = new image($thumb_enable,$this->siteid);
if($thumb_enable) {
$image->thumb($savefile,'',$thumb_setting[0],$thumb_setting[1]);
}
if($watermark_enable) {
$image->watermark($savefile, $savefile);
}
$aids[] = $this->add($uploadedfile);
}
}
return $aids;
}
/**
* 附件下载
* Enter description here ...
* @param $field 预留字段
* @param $value 传入下载内容
* @param $watermark 是否加入水印
* @param $ext 下载扩展名
* @param $absurl 绝对路径
* @param $basehref
*/
function download($field, $value,$watermark = '0',$ext = 'gif|jpg|jpeg|bmp|png', $absurl = '', $basehref = '')
{
global $image_d;
$this->att_db = pc_base::load_model('attachment_model');
$upload_url = pc_base::load_config('system','upload_url');
$this->field = $field;
$dir = date('Y/md/');
$uploadpath = $upload_url.$dir;
$uploaddir = $this->upload_root.$dir;
$string = new_stripslashes($value);
#if(!preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\.($ext))\\2/i", $string, $matches)) return $value;
if(!preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\.($ext))(\?\S*)?\\2/i", $string, $matches)) return $value;
$remotefileurls = array();
foreach($matches[3] as $matche)
{
if(strpos($matche, '://') === false) continue;
dir_create($uploaddir);
$remotefileurls[$matche] = $this->fillurl($matche, $absurl, $basehref);
}
unset($matches, $string);
$remotefileurls = array_unique($remotefileurls);
$oldpath = $newpath = array();
foreach($remotefileurls as $k=>$file) {
if(strpos($file, '://') === false || strpos($file, $upload_url) !== false) continue;
$filename = fileext($file);
if(!preg_match("/($ext)/is",$filename) || in_array($filename, array('php','phtml','php3','php4','jsp','dll','asp','cer','asa','shtml','shtm','aspx','asax','cgi','fcgi','pl'))){
continue;
}
$file_name = basename($file);
$filename = $this->getname($filename);
$newfile = $uploaddir.$filename;
$upload_func = $this->upload_func;
if($upload_func($file, $newfile)) {
$oldpath[] = $k;
$GLOBALS['downloadfiles'][] = $newpath[] = $uploadpath.$filename;
@chmod($newfile, 0777);
$fileext = fileext($filename);
if($watermark){
watermark($newfile, $newfile,$this->siteid);
}
$filepath = $dir.$filename;
$downloadedfile = array('filename'=>$filename, 'filepath'=>$filepath, 'filesize'=>filesize($newfile), 'fileext'=>$fileext);
$aid = $this->add($downloadedfile);
$this->downloadedfiles[$aid] = $filepath;
}
}
return str_replace($oldpath, $newpath, $value);
}
/**
* 附件删除方法
* @param $where 删除sql语句
*/
function delete($where) {
$this->att_db = pc_base::load_model('attachment_model');
$result = $this->att_db->select($where);
foreach($result as $r) {
$image = $this->upload_root.$r['filepath'];
@unlink($image);
$thumbs = glob(dirname($image).'/*'.basename($image));
if($thumbs) foreach($thumbs as $thumb) @unlink($thumb);
}
return $this->att_db->delete($where);
}
/**
* 附件添加如数据库
* @param $uploadedfile 附件信息
*/
function add($uploadedfile) {
$this->att_db = pc_base::load_model('attachment_model');
$uploadedfile['module'] = $this->module;
$uploadedfile['catid'] = $this->catid;
$uploadedfile['siteid'] = $this->siteid;
$uploadedfile['userid'] = $this->userid;
$uploadedfile['uploadtime'] = SYS_TIME;
$uploadedfile['uploadip'] = ip();
$uploadedfile['status'] = pc_base::load_config('system','attachment_stat') ? 0 : 1;
$uploadedfile['authcode'] = md5($uploadedfile['filepath']);
$uploadedfile['filename'] = strlen($uploadedfile['filename'])>49 ? $this->getname($uploadedfile['fileext']) : $uploadedfile['filename'];
$uploadedfile['isimage'] = in_array($uploadedfile['fileext'], $this->imageexts) ? 1 : 0;
$aid = $this->att_db->api_add($uploadedfile);
$this->uploadedfiles[] = $uploadedfile;
return $aid;
}
function set_userid($userid) {
$this->userid = $userid;
}
/**
* 获取缩略图地址..
* @param $image 图片路径
*/
function get_thumb($image){
return str_replace('.', '_thumb.', $image);
}
/**
* 获取附件名称
* @param $fileext 附件扩展名
*/
function getname($fileext){
return date('Ymdhis').rand(100, 999).'.'.$fileext;
}
/**
* 返回附件大小
* @param $filesize 图片大小
*/
function size($filesize) {
if($filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
} elseif($filesize >= 1048576) {
$filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
} elseif($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' KB';
} else {
$filesize = $filesize . ' Bytes';
}
return $filesize;
}
/**
* 判断文件是否是通过 HTTP POST 上传的
*
* @param string $file 文件地址
* @return bool 所给出的文件是通过 HTTP POST 上传的则返回 TRUE
*/
function isuploadedfile($file) {
return is_uploaded_file($file) || is_uploaded_file(str_replace('\\\\', '\\', $file));
}
/**
* 补全网址
*
* @param string $surl 源地址
* @param string $absurl 相对地址
* @param string $basehref 网址
* @return string 网址
*/
function fillurl($surl, $absurl, $basehref = '') {
if($basehref != '') {
$preurl = strtolower(substr($surl,0,6));
if($preurl=='http://' || $preurl=='ftp://' ||$preurl=='mms://' || $preurl=='rtsp://' || $preurl=='thunde' || $preurl=='emule://'|| $preurl=='ed2k://')
return $surl;
else
return $basehref.'/'.$surl;
}
$i = 0;
$dstr = '';
$pstr = '';
$okurl = '';
$pathStep = 0;
$surl = trim($surl);
if($surl=='') return '';
$urls = @parse_url(SITE_URL);
$HomeUrl = $urls['host'];
$BaseUrlPath = $HomeUrl.$urls['path'];
$BaseUrlPath = preg_replace("/\/([^\/]*)\.(.*)$/",'/',$BaseUrlPath);
$BaseUrlPath = preg_replace("/\/$/",'',$BaseUrlPath);
$pos = strpos($surl,'#');
if($pos>0) $surl = substr($surl,0,$pos);
if($surl[0]=='/') {
$okurl = 'http://'.$HomeUrl.'/'.$surl;
} elseif($surl[0] == '.') {
if(strlen($surl)<=2) return '';
elseif($surl[0]=='/') {
$okurl = 'http://'.$BaseUrlPath.'/'.substr($surl,2,strlen($surl)-2);
} else {
$urls = explode('/',$surl);
foreach($urls as $u) {
if($u=="..") $pathStep++;
else if($i<count($urls)-1) $dstr .= $urls[$i].'/';
else $dstr .= $urls[$i];
$i++;
}
$urls = explode('/', $BaseUrlPath);
if(count($urls) <= $pathStep)
return '';
else {
$pstr = 'http://';
for($i=0;$i<count($urls)-$pathStep;$i++) {
$pstr .= $urls[$i].'/';
}
$okurl = $pstr.$dstr;
}
}
} else {
$preurl = strtolower(substr($surl,0,6));
if(strlen($surl)<7)
$okurl = 'http://'.$BaseUrlPath.'/'.$surl;
elseif($preurl=="http:/"||$preurl=='ftp://' ||$preurl=='mms://' || $preurl=="rtsp://" || $preurl=='thunde' || $preurl=='emule:'|| $preurl=='ed2k:/')
$okurl = $surl;
elseif(strtolower(substr($surl,0,8))=='https://'){
$okurl = $surl;
}
else
$okurl = 'http://'.$BaseUrlPath.'/'.$surl;
}
$preurl = strtolower(substr($okurl,0,6));
if($preurl=='ftp://' || $preurl=='mms://' || $preurl=='rtsp://' || $preurl=='thunde' || $preurl=='emule:'|| $preurl=='ed2k:/') {
return $okurl;
}elseif(strtolower(substr($surl,0,8))=='https://'){
return $okurl;
}
else {
$okurl = preg_replace('/^(http:\/\/)/i','',$okurl);
$okurl = preg_replace('/\/{1,}/i','/',$okurl);
return 'http://'.$okurl;
}
}
/**
* 是否允许上传
*/
function is_allow_upload() {
if($_groupid == 1) return true;
$starttime = SYS_TIME-86400;
$site_setting = $this->_get_site_setting($this->siteid);
return ($uploads < $site_setting['upload_maxsize']);
}
/**
* 返回错误信息
*/
function error() {
$UPLOAD_ERROR = array(
0 => L('att_upload_succ'),
1 => L('att_upload_limit_ini'),
2 => L('att_upload_limit_filesize'),
3 => L('att_upload_limit_part'),
4 => L('att_upload_nofile'),
5 => '',
6 => L('att_upload_notemp'),
7 => L('att_upload_temp_w_f'),
8 => L('att_upload_create_dir_f'),
9 => L('att_upload_dir_permissions'),
10 => L('att_upload_limit_ext'),
11 => L('att_upload_limit_setsize'),
12 => L('att_upload_not_allow'),
13 => L('att_upload_limit_time'),
);
return iconv(CHARSET,"utf-8",$UPLOAD_ERROR[$this->error]);
}
/**
* ck编辑器返回
* @param $fn
* @param $fileurl 路径
* @param $message 显示信息
*/
function mkhtml($fn,$fileurl,$message) {
$str='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$fn.', \''.$fileurl.'\', \''.$message.'\');</script>';
exit($str);
}
/**
* flash上传调试方法
* @param $id
*/
function uploaderror($id = 0) {
file_put_contents(PHPCMS_PATH.'xxx.txt', $id);
}
/**
* 获取站点配置信息
* @param $siteid 站点id
*/
private function _get_site_setting($siteid) {
$siteinfo = getcache('sitelist', 'commons');
return string2array($siteinfo[$siteid]['setting']);
}
}
?>
修改两个地方一个是download方法中的正则图片的规则
if(!preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\.($ext))(\?\S*)?\\2/i", $string, $matches)) return $value;
一个是增加fillurl方法中对https的判断
2、修改 /phpcms/model/content_model.class.php 对自动缩略图的正则修改
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
if(!defined('CACHE_MODEL_PATH')) define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
/**
* 内容模型数据库操作类
*/
pc_base::load_sys_class('model', '', 0);
class content_model extends model {
public $table_name = '';
public $category = '';
public function __construct() {
$this->db_config = pc_base::load_config('database');
$this->db_setting = 'default';
parent::__construct();
$this->url = pc_base::load_app_class('url', 'content');
$this->siteid = get_siteid();
}
public function set_model($modelid) {
$this->model = getcache('model', 'commons');
$this->modelid = $modelid;
$this->table_name = $this->db_tablepre.$this->model[$modelid]['tablename'];
$this->model_tablename = $this->model[$modelid]['tablename'];
}
/**
* 添加内容
*
* @param $datas
* @param $isimport 是否为外部接口导入
*/
public function add_content($data,$isimport = 0) {
if($isimport) $data = new_addslashes($data);
$this->search_db = pc_base::load_model('search_model');
$modelid = $this->modelid;
require_once CACHE_MODEL_PATH.'content_input.class.php';
require_once CACHE_MODEL_PATH.'content_update.class.php';
$content_input = new content_input($this->modelid);
$inputinfo = $content_input->get($data,$isimport);
$systeminfo = $inputinfo['system'];
$modelinfo = $inputinfo['model'];
if($data['inputtime'] && !is_numeric($data['inputtime'])) {
$systeminfo['inputtime'] = strtotime($data['inputtime']);
} elseif(!$data['inputtime']) {
$systeminfo['inputtime'] = SYS_TIME;
} else {
$systeminfo['inputtime'] = $data['inputtime'];
}
//读取模型字段配置中,关于日期配置格式,来组合日期数据
$this->fields = getcache('model_field_'.$modelid,'model');
$setting = string2array($this->fields['inputtime']['setting']);
extract($setting);
if($fieldtype=='date') {
$systeminfo['inputtime'] = date('Y-m-d');
}elseif($fieldtype=='datetime'){
$systeminfo['inputtime'] = date('Y-m-d H:i:s');
}
if($data['updatetime'] && !is_numeric($data['updatetime'])) {
$systeminfo['updatetime'] = strtotime($data['updatetime']);
} elseif(!$data['updatetime']) {
$systeminfo['updatetime'] = SYS_TIME;
} else {
$systeminfo['updatetime'] = $data['updatetime'];
}
$inputinfo['system']['username'] = $systeminfo['username'] = $data['username'] ? $data['username'] : param::get_cookie('admin_username');
$systeminfo['sysadd'] = defined('IN_ADMIN') ? 1 : 0;
//自动提取摘要
if(isset($_POST['add_introduce']) && $systeminfo['description'] == '' && isset($modelinfo['content'])) {
$content = stripslashes($modelinfo['content']);
$introcude_length = intval($_POST['introcude_length']);
$systeminfo['description'] = str_cut(str_replace(array("'","\r\n","\t",'[page]','[/page]','“','”',' '), '', strip_tags($content)),$introcude_length);
$inputinfo['system']['description'] = $systeminfo['description'] = addslashes($systeminfo['description']);
}
//自动提取缩略图
if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) {
$content = $content ? $content : stripslashes($modelinfo['content']);
$auto_thumb_no = intval($_POST['auto_thumb_no'])-1;
if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))(\?\S*)?\\2/i", $content, $matches)) {
$systeminfo['thumb'] = $matches[3][$auto_thumb_no];
}
}
$systeminfo['description'] = str_replace(array('/','\\','#','.',"'"),' ',$systeminfo['description']);
$systeminfo['keywords'] = str_replace(array('/','\\','#','.',"'"),' ',$systeminfo['keywords']);
//主表
$tablename = $this->table_name = $this->db_tablepre.$this->model_tablename;
$id = $modelinfo['id'] = $this->insert($systeminfo,true);
$this->update($systeminfo,array('id'=>$id));
//更新URL地址
if($data['islink']==1) {
$urls[0] = trim_script($_POST['linkurl']);
$urls[0] = remove_xss($urls[0]);
$urls[0] = str_replace(array('select ',')','\\','#',"'"),' ',$urls[0]);
} else {
$urls = $this->url->show($id, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');
}
$this->table_name = $tablename;
$this->update(array('url'=>$urls[0]),array('id'=>$id));
//附属表
$this->table_name = $this->table_name.'_data';
$this->insert($modelinfo);
//添加统计
$this->hits_db = pc_base::load_model('hits_model');
$hitsid = 'c-'.$modelid.'-'.$id;
$this->hits_db->insert(array('hitsid'=>$hitsid,'catid'=>$systeminfo['catid'],'updatetime'=>SYS_TIME));
if($data['status']==99) {
//更新到全站搜索
$this->search_api($id,$inputinfo);
}
//更新栏目统计数据
$this->update_category_items($systeminfo['catid'],'add',1);
//调用 update
$content_update = new content_update($this->modelid,$id);
//合并后,调用update
$merge_data = array_merge($systeminfo,$modelinfo);
$merge_data['posids'] = $data['posids'];
$content_update->update($merge_data);
//发布到审核列表中
if(!defined('IN_ADMIN') || $data['status']!=99) {
$this->content_check_db = pc_base::load_model('content_check_model');
$check_data = array(
'checkid'=>'c-'.$id.'-'.$modelid,
'catid'=>$systeminfo['catid'],
'siteid'=>$this->siteid,
'title'=>$systeminfo['title'],
'username'=>$systeminfo['username'],
'inputtime'=>$systeminfo['inputtime'],
'status'=>$data['status'],
);
$this->content_check_db->insert($check_data);
}
//END发布到审核列表中
if(!$isimport) {
$html = pc_base::load_app_class('html', 'content');
$urls['data']['system']['id'] = $id;
if($urls['content_ishtml'] && $data['status']==99) $html->show($urls[1],$urls['data']);
$catid = $systeminfo['catid'];
}
//发布到其他栏目
if($id && isset($_POST['othor_catid']) && is_array($_POST['othor_catid'])) {
$linkurl = $urls[0];
$r = $this->get_one(array('id'=>$id));
foreach ($_POST['othor_catid'] as $cid=>$_v) {
$this->set_catid($cid);
$mid = $this->category[$cid]['modelid'];
if($modelid==$mid) {
//相同模型的栏目插入新的数据
$inputinfo['system']['catid'] = $systeminfo['catid'] = $cid;
$newid = $modelinfo['id'] = $this->insert($systeminfo,true);
$this->table_name = $tablename.'_data';
$this->insert($modelinfo);
if($data['islink']==1) {
$urls = $_POST['linkurl'];
$urls = str_replace(array('select ',')','\\','#',"'"),' ',$urls);
} else {
$urls = $this->url->show($newid, 0, $cid, $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');
}
$this->table_name = $tablename;
$this->update(array('url'=>$urls[0]),array('id'=>$newid));
//发布到审核列表中
if($data['status']!=99) {
$check_data = array(
'checkid'=>'c-'.$newid.'-'.$mid,
'catid'=>$cid,
'siteid'=>$this->siteid,
'title'=>$systeminfo['title'],
'username'=>$systeminfo['username'],
'inputtime'=>$systeminfo['inputtime'],
'status'=>1,
);
$this->content_check_db->insert($check_data);
}
if($urls['content_ishtml'] && $data['status']==99) $html->show($urls[1],$urls['data']);
} else {
//不同模型插入转向链接地址
$newid = $this->insert(
array('title'=>$systeminfo['title'],
'style'=>$systeminfo['style'],
'thumb'=>$systeminfo['thumb'],
'keywords'=>$systeminfo['keywords'],
'description'=>$systeminfo['description'],
'status'=>$systeminfo['status'],
'catid'=>$cid,'url'=>$linkurl,
'sysadd'=>1,
'username'=>$systeminfo['username'],
'inputtime'=>$systeminfo['inputtime'],
'updatetime'=>$systeminfo['updatetime'],
'islink'=>1
),true);
$this->table_name = $this->table_name.'_data';
$this->insert(array('id'=>$newid));
//发布到审核列表中
if($data['status']!=99) {
$check_data = array(
'checkid'=>'c-'.$newid.'-'.$mid,
'catid'=>$systeminfo['catid'],
'siteid'=>$this->siteid,
'title'=>$systeminfo['title'],
'username'=>$systeminfo['username'],
'inputtime'=>$systeminfo['inputtime'],
'status'=>1,
);
$this->content_check_db->insert($check_data);
}
}
$hitsid = 'c-'.$mid.'-'.$newid;
$this->hits_db->insert(array('hitsid'=>$hitsid,'catid'=>$cid,'updatetime'=>SYS_TIME));
}
}
//END 发布到其他栏目
//更新附件状态
if(pc_base::load_config('system','attachment_stat')) {
$this->attachment_db = pc_base::load_model('attachment_model');
$this->attachment_db->api_update('','c-'.$systeminfo['catid'].'-'.$id,2);
}
//生成静态
if(!$isimport && $data['status']==99) {
//在添加和修改内容处定义了 INDEX_HTML
if(defined('INDEX_HTML')) $html->index();
if(defined('RELATION_HTML')) $html->create_relation_html($catid);
}
return $id;
}
/**
* 修改内容
*
* @param $datas
*/
public function edit_content($data,$id) {
$model_tablename = $this->model_tablename;
//前台权限判断
if(!defined('IN_ADMIN')) {
$_username = param::get_cookie('_username');
$us = $this->get_one(array('id'=>$id,'username'=>$_username));
if(!$us) return false;
}
$this->search_db = pc_base::load_model('search_model');
require_once CACHE_MODEL_PATH.'content_input.class.php';
require_once CACHE_MODEL_PATH.'content_update.class.php';
$content_input = new content_input($this->modelid);
$inputinfo = $content_input->get($data);
$systeminfo = $inputinfo['system'];
$modelinfo = $inputinfo['model'];
if($data['inputtime'] && !is_numeric($data['inputtime'])) {
$systeminfo['inputtime'] = strtotime($data['inputtime']);
} elseif(!$data['inputtime']) {
$systeminfo['inputtime'] = SYS_TIME;
} else {
$systeminfo['inputtime'] = $data['inputtime'];
}
if($data['updatetime'] && !is_numeric($data['updatetime'])) {
$systeminfo['updatetime'] = strtotime($data['updatetime']);
} elseif(!$data['updatetime']) {
$systeminfo['updatetime'] = SYS_TIME;
} else {
$systeminfo['updatetime'] = $data['updatetime'];
}
//自动提取摘要
if(isset($_POST['add_introduce']) && $systeminfo['description'] == '' && isset($modelinfo['content'])) {
$content = stripslashes($modelinfo['content']);
$introcude_length = intval($_POST['introcude_length']);
$systeminfo['description'] = str_cut(str_replace(array("\r\n","\t",'[page]','[/page]','“','”',' '), '', strip_tags($content)),$introcude_length);
$inputinfo['system']['description'] = $systeminfo['description'] = addslashes($systeminfo['description']);
}
//自动提取缩略图
if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) {
$content = $content ? $content : stripslashes($modelinfo['content']);
$auto_thumb_no = intval($_POST['auto_thumb_no'])-1;
if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))(\?\S*)?\\2/i", $content, $matches)) {
$systeminfo['thumb'] = $matches[3][$auto_thumb_no];
}
}
if($data['islink']==1) {
$systeminfo['url'] = $_POST['linkurl'];
$systeminfo['url'] = str_replace(array('select ',')','\\','#',"'"),' ',$systeminfo['url']);
} else {
//更新URL地址
$urls = $this->url->show($id, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix'],$inputinfo,'edit');
$systeminfo['url'] = $urls[0];
}
$systeminfo['description'] = str_replace(array('/','\\','#','.',"'"),' ',$systeminfo['description']);
$systeminfo['keywords'] = str_replace(array('/','\\','#','.',"'"),' ',$systeminfo['keywords']);
//主表
$this->table_name = $this->db_tablepre.$model_tablename;
$this->update($systeminfo,array('id'=>$id));
//附属表
$this->table_name = $this->table_name.'_data';
$this->update($modelinfo,array('id'=>$id));
$this->search_api($id,$inputinfo);
//调用 update
$content_update = new content_update($this->modelid,$id);
$content_update->update($data);
//更新附件状态
if(pc_base::load_config('system','attachment_stat')) {
$this->attachment_db = pc_base::load_model('attachment_model');
$this->attachment_db->api_update('','c-'.$systeminfo['catid'].'-'.$id,2);
}
//更新审核列表
$this->content_check_db = pc_base::load_model('content_check_model');
$check_data = array(
'catid'=>$systeminfo['catid'],
'siteid'=>$this->siteid,
'title'=>$systeminfo['title'],
'status'=>$systeminfo['status'],
);
if(!isset($systeminfo['status'])) unset($check_data['status']);
$this->content_check_db->update($check_data,array('checkid'=>'c-'.$id.'-'.$this->modelid));
//生成静态
$html = pc_base::load_app_class('html', 'content');
if($urls['content_ishtml']) {
$html->show($urls[1],$urls['data']);
}
//在添加和修改内容处定义了 INDEX_HTML
if(defined('INDEX_HTML')) $html->index();
if(defined('RELATION_HTML')) $html->create_relation_html($systeminfo['catid']);
return true;
}
public function status($ids = array(), $status = 99) {
$this->content_check_db = pc_base::load_model('content_check_model');
$this->message_db = pc_base::load_model('message_model');
$this->set_model($this->modelid);
if(is_array($ids) && !empty($ids)) {
foreach($ids as $id) {
$this->update(array('status'=>$status),array('id'=>$id));
$del = false;
$r = $this->get_one(array('id'=>$id));
if($status==0) {
//退稿发送短消息、邮件
$message = L('reject_message_tips').$r['title']."<BR><a href=\'index.php?m=member&c=content&a=edit&catid={$r[catid]}&id={$r[id]}\'><font color=red>".L('click_edit')."</font></a><br>";
if(isset($_POST['reject_c']) && $_POST['reject_c'] != L('reject_msg')) {
$message .= $_POST['reject_c'];
} elseif(isset($_GET['reject_c']) && $_GET['reject_c'] != L('reject_msg')) {
$message .= $_GET['reject_c'];
}
$this->message_db->add_message($r['username'],'SYSTEM',L('reject_message'),$message);
} elseif($status==99 && $r['sysadd']) {
$this->content_check_db->delete(array('checkid'=>'c-'.$id.'-'.$this->modelid));
$del = true;
}
if(!$del) $this->content_check_db->update(array('status'=>$status),array('checkid'=>'c-'.$id.'-'.$this->modelid));
}
} else {
$this->update(array('status'=>$status),array('id'=>$ids));
$del = false;
$r = $this->get_one(array('id'=>$ids));
if($status==0) {
//退稿发送短消息、邮件
$message = L('reject_message_tips').$r['title']."<BR><a href=\'index.php?m=member&c=content&a=edit&catid={$r[catid]}&id={$r[id]}\'><font color=red>".L('click_edit')."</font></a><br>";
if(isset($_POST['reject_c']) && $_POST['reject_c'] != L('reject_msg')) {
$message .= $_POST['reject_c'];
} elseif(isset($_GET['reject_c']) && $_GET['reject_c'] != L('reject_msg')) {
$message .= $_GET['reject_c'];
}
$this->message_db->add_message($r['username'],'SYSTEM',L('reject_message'),$message);
} elseif($status==99 && $r['sysadd']) {
$this->content_check_db->delete(array('checkid'=>'c-'.$ids.'-'.$this->modelid));
$del = true;
}
if(!$del) $this->content_check_db->update(array('status'=>$status),array('checkid'=>'c-'.$ids.'-'.$this->modelid));
}
return true;
}
/**
* 删除内容
* @param $id 内容id
* @param $file 文件路径
* @param $catid 栏目id
*/
public function delete_content($id,$file,$catid = 0) {
//删除主表数据
$this->delete(array('id'=>$id));
//删除从表数据
$this->table_name = $this->table_name.'_data';
$this->delete(array('id'=>$id));
//重置默认表
$this->table_name = $this->db_tablepre.$this->model_tablename;
//更新栏目统计
$this->update_category_items($catid,'delete');
}
public function search_api($id = 0, $data = array(), $action = 'update') {
$type_arr = getcache('search_model_'.$this->siteid,'search');
$typeid = $type_arr[$this->modelid]['typeid'];
if($action == 'update') {
$fulltext_array = getcache('model_field_'.$this->modelid,'model');
foreach($fulltext_array AS $key=>$value){
if($value['isfulltext']) {
$fulltextcontent .= $data['system'][$key] ? $data['system'][$key] : $data['model'][$key];
}
}
$this->search_db->update_search($typeid ,$id, $fulltextcontent,addslashes($data['system']['title']).' '.addslashes($data['system']['keywords']),$data['system']['inputtime']);
} elseif($action == 'delete') {
$this->search_db->delete_search($typeid ,$id);
}
}
/**
* 获取单篇信息
*
* @param $catid
* @param $id
*/
public function get_content($catid,$id) {
$catid = intval($catid);
$id = intval($id);
if(!$catid || !$id) return false;
$siteids = getcache('category_content','commons');
$siteid = $siteids[$catid];
$this->category = getcache('category_content_'.$siteid,'commons');
if(isset($this->category[$catid]) && $this->category[$catid]['type'] == 0) {
$modelid = $this->category[$catid]['modelid'];
$this->set_model($modelid);
$r = $this->get_one(array('id'=>$id));
//附属表
$this->table_name = $this->table_name.'_data';
$r2 = $this->get_one(array('id'=>$id));
if($r2) {
return array_merge($r,$r2);
} else {
return $r;
}
}
return true;
}
/**
* 设置catid 所在的模型数据库
*
* @param $catid
*/
public function set_catid($catid) {
$catid = intval($catid);
if(!$catid) return false;
if(empty($this->category) || empty($this->category[$catid])) {
$siteids = getcache('category_content','commons');
$siteid = $siteids[$catid];
$this->category = getcache('category_content_'.$siteid,'commons');
}
if(isset($this->category[$catid]) && $this->category[$catid]['type'] == 0) {
$modelid = $this->category[$catid]['modelid'];
$this->set_model($modelid);
}
}
private function update_category_items($catid,$action = 'add',$cache = 0) {
$this->category_db = pc_base::load_model('category_model');
if($action=='add') {
$this->category_db->update(array('items'=>'+=1'),array('catid'=>$catid));
} else {
$this->category_db->update(array('items'=>'-=1'),array('catid'=>$catid));
}
if($cache) $this->cache_items();
}
public function cache_items() {
$datas = $this->category_db->select(array('modelid'=>$this->modelid),'catid,type,items',10000);
$array = array();
foreach ($datas as $r) {
if($r['type']==0) $array[$r['catid']] = $r['items'];
}
setcache('category_items_'.$this->modelid, $array,'commons');
}
}
?>