/**
  * @brief editPost 编辑一篇文章
  *
  * @return void
  */
 public function editPost()
 {
     $p = array();
     $p['pid'] = Request::P('pid');
     $p['title'] = htmlspecialchars(Request::P('title', 'string'));
     $p['content'] = Request::P('content', 'string');
     $p['category'] = Request::P('category', 'array');
     if (!$p['pid'] || !$p['title'] || !$p['content'] || count($p['category']) == 1 && !$p['category'][0]) {
         $r = array('success' => FALSE, 'message' => _t('Title, Content and Category can not be null.'));
         Response::ajaxReturn($r);
         return;
     }
     $p['allow_reply'] = Request::P('allowComment') ? 1 : 0;
     $p['top'] = Request::P('top') ? 1 : 0;
     $p['alias'] = '';
     $p['status'] = 1;
     // 编辑文章
     $post = new PostLibrary();
     $meta = new MetaLibrary();
     $post->editPost($p);
     // 删除原有的分类与标签
     $meta->setPID($p['pid']);
     $metas = $meta->getMeta(FALSE);
     foreach ($metas as $m) {
         if ($m['type'] == 1 || $m['type'] == 2) {
             $meta->delRelation($m['mid'], $p['pid']);
         }
     }
     $meta->setPID(0);
     // 处理分类
     foreach ($p['category'] as $c) {
         $meta->addRelation($c, $p['pid']);
     }
     // 处理标签
     if ($p['tags'] = Request::P('tags', 'string')) {
         $p['tags'] = str_replace(array(' ', ',', '、'), ',', $p['tags']);
         $p['tags'] = explode(',', $p['tags']);
         $meta->setType(2);
         foreach ($p['tags'] as $tag) {
             $meta->setName($tag);
             $t = $meta->getMeta();
             if (!$t) {
                 $t = $meta->addMeta(array('type' => 2, 'name' => $tag));
             } else {
                 $t = $t[0]['mid'];
             }
             $meta->addRelation($t, $p['pid']);
         }
     }
     // 处理新附件
     $meta = new MetaLibrary();
     $meta->setType(3);
     $meta->setPID(1000000000);
     $attachments = $meta->getMeta();
     foreach ($attachments as $a) {
         $meta->movRelation($a['mid'], 1000000000, $p['pid']);
     }
     $r = array('success' => TRUE, 'message' => _t('Edit post success.'));
     Response::ajaxReturn($r);
 }
 /**
  * @brief addMeta 添加 Meta
  *
  * @param $type Meta 类型
  *
  * @return void
  */
 public function addMeta($type)
 {
     $m['name'] = Request::P('name', 'string');
     $m['description'] = Request::P('description', 'string');
     $m['alias'] = Request::P('alias', 'string');
     $m['parent'] = Request::P('parent');
     $m['type'] = $type;
     $meta = new MetaLibrary();
     if ($meta->addMeta($m)) {
         $r = array('success' => TRUE, 'message' => _t('Add Meta complete.'));
     } else {
         $r = array('success' => FALSE, 'message' => _t('Add Meta failed.'));
     }
     Response::ajaxReturn($r);
 }
 public function execute()
 {
     // 返回的文件信息
     $files = array('ogname' => '', 'name' => '', 'path' => '', 'size' => '', 'type' => '', 'status' => 1);
     if (!count($_FILES)) {
         return false;
     }
     $field = $this->field;
     if ($_FILES[$field]['error'] != 0) {
         switch ($_FILES[$field]['error']) {
             case 1:
                 $files['status'] = -3;
                 break;
             case 2:
             case 3:
             case 4:
                 $files['status'] = -4;
                 break;
         }
         return $files;
     }
     // 获取文件扩展名
     $fileext = $this->fileext($_FILES[$field]['name']);
     // 生成文件名
     $filename = gmdate('YmdHis', $this->time) . mt_rand(1000000, 9999999);
     // 文件大小
     $filesize = $_FILES[$field]['size'];
     // 限制上传格式
     $disarr = array('asa', 'asax', 'ascx', 'asxh', 'asmx', 'asp', 'aspx', 'axd', 'cer', 'cs', 'java', 'php', 'php3', 'php4', 'shtm', 'shtml');
     if (in_array($fileext, $disarr) || !in_array($fileext, $this->allow_types)) {
         $files['status'] = -1;
         return $files;
     }
     //文件大小超出
     if ($filesize > $this->maxsize) {
         $files['status'] = -2;
         return $files;
     }
     $files['ogname'] = trim(htmlspecialchars($_FILES[$field]['name']));
     $files['name'] = $filename;
     $files['path'] = $this->path;
     $files['size'] = $filesize;
     $files['ext'] = $fileext;
     //修正addslashes后win下路径多出转义导致文件无法上传的问题
     $_FILES[$field]['tmp_name'] = str_replace('\\\\', '\\', $_FILES[$field]['tmp_name']);
     //保存上传文件并删除临时文件
     if (is_uploaded_file($_FILES[$field]['tmp_name'])) {
         move_uploaded_file($_FILES[$field]['tmp_name'], $this->dir . $filename . '.' . $fileext);
         //@unlink( $_FILES[$field]['tmp_name'] );
         if (function_exists("mime_content_type")) {
             $files['type'] = mime_content_type($this->dir . $filename . '.' . $fileext);
         } elseif (isset($this->filetype[$fileext])) {
             $files['type'] = $this->filetype[$fileext];
         } else {
             $files['type'] = "application/octet-stream";
         }
         //对图片进行加水印和生成缩略图
         if (in_array($fileext, array('jpg', 'png', 'jpeg'))) {
             if ($this->max_width || $this->max_height) {
                 $this->resize($this->dir . $filename . '.' . $fileext);
                 $files['size'] = filesize($this->dir . $filename . '.' . $fileext);
             }
             if ($this->thumb_width || $this->thumb_height) {
                 $this->create_thumb($this->dir . $filename . '.' . $fileext, $this->dir . 'thumb_' . $filename . '.' . $fileext);
             }
             if ($this->watermark_file) {
                 $this->create_watermark($this->dir . $filename . '.' . $fileext, $this->dir . 'watermark_' . $filename . '.' . $fileext);
             }
         }
     }
     // 将附件信息写入数据库
     $meta = new MetaLibrary();
     $m['name'] = $files['ogname'];
     $m['alias'] = $this->path . $filename . '.' . $fileext;
     $m['description'] = $files['type'];
     $m['type'] = 3;
     $mid = $meta->addMeta($m);
     // 把 pid 为 1000000000 的文章作为新上传的附件的暂居地。这样做并不好,但我想这应该够了。
     $meta->addRelation($mid, 1000000000);
     $files['mid'] = $mid;
     return $files;
 }