其实对于修改百度编辑器的文章,在网路上一搜一大把,在没有深入读PHPcms代码的时候也照着这些文章改过,但来来去去这些文章都仿佛是一个人写的,连截图都是一模一样,这也是现在网络文章的一大现状,搜到很多的资料都是一样的。重要的是改得并不彻底,存在很多问题,基本上都仅仅只是解决了文字编辑而已。

去年的时候在基于PHPcms的框架上深入的DIY了一个网站,应该可以说是很完美的把两者结合了吧,系统可以在管理员的后台自由的切换管理员使用的编辑器和用户使用的编辑器,在UEditor和原本CKEditor之间方便的切换,依照这个方式也可以继续添加更多的编辑器,在你愿意的情况下。另外根据百度编辑器的上传功能,完善了附件的功能,使之每个用户只能看到自己上传的图片或者文件。当然,这在单一用户的网站上是毫无意义的。所以,我是为了记录下这个流程而已,免得下次再来又要研究一下,而各位就挑着看吧。

我下载的版本是ueditor.full.1.4.3.2,下载地址是http://ueditor.baidu.com/build/build_down.php?n=ueditor&v=1_4_3_2-src,现在最新版本已经是http://ueditor.baidu.com/build/build_down.php?n=ueditor&v=1_4_3_3-utf8-php,各位也可以试试最新版,我没对比过,相信应该变动不大。

下载之后解压放在./statics/js/下,系统的JS扩展包都在这里了;另外把config.json文件放在网站的根目录下,这个文件标注的是上传浏览之类动作后台的具体操作动作。

首先修改它的URL地址,打开ueditor.config.js文件,添加一句:

var URL = window.UEDITOR_HOME_URL || getUEBasePath();

在window.UEDITOR_CONFIG里添加一句:

UEDITOR_HOME_URL: URL

再接着修改上传后台接收地址:

serverUrl: '?m=attachment&c=ueditupload&a=upload'

注:attachment是系统附件管理的模块,ueditupload和upload都是我自建的,各位可以按照自己的喜好来

然后当然是在./phpcms/modules/attachment/目录下建立ueditupload.php文件了,代码如下:

<?defined('IN_PHPCMS') or exit('No permission resources.'); $session_storage = 'session_'.pc_base::load_config('system','session_storage');pc_base::load_sys_class($session_storage);class ueditupload {    private $userid,$isuser,$isadmin;    function __construct(){        $this->userid = $_SESSION['userid'] ? $_SESSION['userid'] : (param::get_cookie('_userid') ? param::get_cookie('_userid') : sys_auth($_POST['userid_flash'],'DECODE'));        $this->isuser = $_SESSION['roleid'] ? 0 : 1;        $this->isadmin =  $_SESSION['roleid'] ? 1 : 0;        $this->att_db = pc_base::load_model('attachment_model');    }    private function updata($fileinfo){        if($fileinfo['state'] == 'SUCCESS'){            $data['module'] = 'content';            $data['catid'] = 0;            $data['filename'] = $fileinfo['original'];            $data['filepath'] = $fileinfo['url'];            $data['filesize'] = $fileinfo['size'];            $data['fileext'] = substr($fileinfo['type'],1);            $data['isimage'] = in_array($fileinfo['type'],array(".png", ".jpg", ".jpeg", ".gif", ".bmp")) ? 1 : 0;            $data['isthumb'] = 0;            $data['downloads'] = 0;            $data['userid'] = $this->userid;            $data['uploadtime'] = time();            $data['uploadip'] = ip();            $data['status'] = 0;            $data['authcode'] = md5($fileinfo['url']);            $data['siteid'] = 1;            $data['isuser'] = $this->isuser;            $this->att_db->insert($data);        }    }    public function upload(){        error_reporting(E_ERROR);        header("Content-Type: text/html; charset=utf-8");        $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("config.json")), true);        $action = htmlspecialchars($_GET['action']);        $base64 = 'upload';        $uploader = pc_base::load_app_class('uploader');                switch ($action) {            case 'config':                $result =  json_encode($CONFIG);                break;            /* 上传图片 */            case 'uploadimage':                $config = array(                    "pathFormat" => $CONFIG['imagePathFormat'],                    "maxSize" => $CONFIG['imageMaxSize'],                    "allowFiles" => $CONFIG['imageAllowFiles']                );                $fieldName = $CONFIG['imageFieldName'];                $uploader->upload($fieldName, $config, $base64);                $result = json_encode($uploader->getFileInfo());                $this -> updata($uploader->getFileInfo());                break;            /* 上传涂鸦 */            case 'uploadscrawl':                $config = array(                    "pathFormat" => $CONFIG['scrawlPathFormat'],                    "maxSize" => $CONFIG['scrawlMaxSize'],                    "allowFiles" => $CONFIG['scrawlAllowFiles'],                    "oriName" => "scrawl.png"                );                $fieldName = $CONFIG['scrawlFieldName'];                $base64 = "base64";                $uploader->upload($fieldName, $config, $base64);                $result = json_encode($uploader->getFileInfo());                $this -> updata($uploader->getFileInfo());                break;            /* 上传视频 */            case 'uploadvideo':                $config = array(                    "pathFormat" => $CONFIG['videoPathFormat'],                    "maxSize" => $CONFIG['videoMaxSize'],                    "allowFiles" => $CONFIG['videoAllowFiles']                );                $fieldName = $CONFIG['videoFieldName'];                $uploader->upload($fieldName, $config, $base64);                $result = json_encode($uploader->getFileInfo());                $this -> updata($uploader->getFileInfo());                break;            /* 上传文件 */            case 'uploadfile':                $config = array(                    "pathFormat" => $CONFIG['filePathFormat'],                    "maxSize" => $CONFIG['fileMaxSize'],                    "allowFiles" => $CONFIG['fileAllowFiles']                );                $fieldName = $CONFIG['fileFieldName'];                $uploader->upload($fieldName, $config, $base64);                $result = json_encode($uploader->getFileInfo());                $this -> updata($uploader->getFileInfo());                break;            /* 列出图片 */            case 'listimage':                $pagesize = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : 20;                $startpage = isset($_GET['start']) ? intval(htmlspecialchars($_GET['start'])) / $pagesize + 1 : 0;                if($this->isadmin == 1){                    //如果是管理员                    $countwhere = 'isimage = 1';                    $listwhere = 'isimage = 1';                             }else{                    $countwhere = 'isimage = 1 AND isuser = 1 AND userid = '.$this->userid;                    $listwhere = 'isimage = 1 AND isuser = 1 AND userid = '.$this->userid;                }                $count = $this->att_db->count($countwhere);                $row = $this->att_db->listinfo($listwhere,'uploadtime DESC',$startpage,$pagesize);                $list = array();                foreach($row as $key => $val){                    $list[$key]['url'] = APP_PATH.$row[$key]['filepath'];                    $list[$key]['mtime'] = $row[$key]['uploadtime'];                }                $result = json_encode(array(                    "state" => "SUCCESS",                    "list" => $list,                    "start" => $_GET['start'] == 0 ? 0 : $pagesize * ($startpage - 1),                    "total" => $count                ));                break;            /* 列出文件 */            case 'listfile':                $pagesize = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : 20;                $startpage = isset($_GET['start']) ? intval(htmlspecialchars($_GET['start'])) / $pagesize + 1 : 0;                if($this->isadmin == 1){                    //如果是管理员                    $countwhere = 'isimage = 1';                    $listwhere = 'isimage = 1';                             }else{                    $countwhere = 'isimage = 0 AND isuser = 1 AND userid = '.$this->userid;                    $listwhere = 'isimage = 0 AND isuser = 1 AND userid = '.$this->userid;                }                $count = $this->att_db->count($countwhere);                $row = $this->att_db->listinfo($listwhere,'uploadtime DESC',$startpage,$pagesize);                $list = array();                foreach($row as $key => $val){                    $list[$key]['url'] = $row[$key]['filepath'];                    $list[$key]['mtime'] = $row[$key]['uploadtime'];                }                $result = json_encode(array(                    "state" => "SUCCESS",                    "list" => $list,                    "start" => $_GET['start'] == 0 ? 0 : $pagesize * ($startpage - 1),                    "total" => $count                ));                break;            /* 抓取远程文件 */            case 'catchimage':                $config = array(                    "pathFormat" => $CONFIG['catcherPathFormat'],                    "maxSize" => $CONFIG['catcherMaxSize'],                    "allowFiles" => $CONFIG['catcherAllowFiles'],                    "oriName" => "remote_".time().".png"                );                $fieldName = $CONFIG['catcherFieldName'];                /* 抓取远程图片 */                $list = array();                if (isset($_POST[$fieldName])) {                    $source = $_POST[$fieldName];                } else {                    $source = $_GET[$fieldName];                }                foreach ($source as $imgUrl) {                    $uploader->upload($imgUrl, $config, 'remote');                    $info = $uploader->getFileInfo();                    $this -> updata($uploader->getFileInfo());                    array_push($list, array(                        "state" => $info["state"],                        "url" => APP_PATH.$info["url"],                        "size" => $info["size"],                        "title" => htmlspecialchars($info["title"]),                        "original" => htmlspecialchars($info["original"]),                        "source" => htmlspecialchars($imgUrl)                    ));                }                /* 返回抓取数据 */                $result = json_encode(array(                    'state'=> count($list) ? 'SUCCESS':'ERROR',                    'list'=> $list                ));                break;            default:                $result = json_encode(array(                    'state'=> '请求地址出错'                ));                break;        }               /* 输出结果 */        if (isset($_GET["callback"])) {            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';            } else {                echo json_encode(array(                    'state'=> 'callback参数不合法'                ));            }        } else {            echo $result;        }        //print_r($_POST);    }}?>

晕,貌似提示说字数超出最大值了,只好分篇了吧,在完成上述的步骤后,上传已经分用户存入数据库了,获取也会按照会员分别列出。