示例#1
0
 public function ajax_add()
 {
     if (!$this->RequestHandler->isPost()) {
         $this->error(ECode::$SYS_REQUESTERROR);
     }
     $this->_attOpInit();
     $u = User::getInstance();
     //get current file
     $isFile = false;
     if (isset($this->params['id'])) {
         $id = $this->params['id'];
         try {
             $article = Article::getInstance($id, $this->_board);
             if (!$article->hasEditPerm($u)) {
                 $this->error(ECode::$ARTICLE_NOEDIT);
             }
             $atts = $article->getAttList();
             $isFile = true;
         } catch (Exception $e) {
             $this->error(ECode::$ARTICLE_NONE);
         }
     } else {
         $atts = Forum::listAttach();
     }
     $upload = Configure::read("article");
     $num = count($atts);
     $ret = array();
     $exif = '';
     if ($num >= intval($upload['att_num'])) {
         $this->error(ECode::$ATT_NLIMIT);
     }
     //init upload file
     if (isset($this->params['url']['name'])) {
         //html5 mode
         $tmp_name = tempnam(CACHE, "upload_");
         file_put_contents($tmp_name, file_get_contents('php://input'));
         $file = array('tmp_name' => $tmp_name, 'name' => nforum_iconv('utf-8', $this->encoding, $this->params['url']['name']), 'size' => filesize($tmp_name), 'error' => 0);
     } else {
         if (isset($this->params['form']['file']) && is_array($this->params['form']['file'])) {
             //flash mode
             $file = $this->params['form']['file'];
             $file['name'] = nforum_iconv('utf-8', $this->encoding, $file['name']);
         } else {
             $this->error(ECode::$ATT_NONE);
         }
     }
     //check upload file
     $errno = isset($file['error']) ? $file['error'] : UPLOAD_ERR_NO_FILE;
     switch ($errno) {
         case UPLOAD_ERR_OK:
             $tmpFile = $file['tmp_name'];
             $tmpName = $file['name'];
             if (!isset($tmp_name) && !is_uploaded_file($tmpFile)) {
                 $this->error(ECode::$ATT_NONE);
             }
             $size = $file['size'];
             foreach ($atts as $v) {
                 if ($v['name'] == $tmpName) {
                     $this->error(ECode::$ATT_SAMENAME);
                 }
                 $size += intval($v['size']);
                 if ($size > $upload['att_size']) {
                     $this->error(ECode::$ATT_SLIMIT);
                 }
             }
             if (is_array(Configure::read("exif")) && in_array($this->_board->NAME, Configure::read("exif")) && @exif_imagetype($tmpFile) === 2) {
                 $exif = $this->Exif->format($tmpFile);
             }
             try {
                 if ($isFile) {
                     $article->addAttach($tmpFile, $tmpName);
                 } else {
                     Forum::addAttach($tmpFile, $tmpName);
                 }
                 if (isset($tmp_name)) {
                     @unlink($tmp_name);
                 }
                 $ret['no'] = $num + 1;
                 $ret['name'] = $tmpName;
                 $ret['size'] = $file['size'];
                 $ret['exif'] = $exif;
                 $this->set('no_html_data', $ret);
                 $this->set('ajax_code', ECode::$ATT_ADDOK);
             } catch (ArchiveAttException $e) {
                 $this->error($e->getMessage());
             } catch (AttException $e) {
                 $this->error($e->getMessage());
             }
             break;
         case UPLOAD_ERR_INI_SIZE:
         case UPLOAD_ERR_FORM_SIZE:
         case UPLOAD_ERR_PARTIAL:
             $this->error(ECode::$ATT_SLIMIT);
             break;
         case UPLOAD_ERR_NO_FILE:
             $this->error(ECode::$ATT_NONE);
             $msg = ECode::$ATT_NONE;
             break;
         default:
             $this->error(ECode::$SYS_ERROR);
     }
 }
示例#2
0
 public function add()
 {
     if (!$this->RequestHandler->isPost()) {
         $this->error(ECode::$SYS_REQUESTERROR);
     }
     $this->_attOpInit();
     $isFile = false;
     $u = User::getInstance();
     if (isset($this->params['id'])) {
         $id = $this->params['id'];
         try {
             $article = Article::getInstance($id, $this->_board);
             if (!$article->hasEditPerm($u)) {
                 $this->error(ECode::$ARTICLE_NOEDIT);
             }
             $atts = $article->getAttList();
             $isFile = true;
         } catch (Exception $e) {
             $this->error(ECode::$ARTICLE_NONE);
         }
     } else {
         $atts = Forum::listAttach();
     }
     $num = count($atts);
     $size = 0;
     foreach ($atts as $v) {
         $size += intval($v['size']);
     }
     $upload = Configure::read("article");
     if ($num >= intval($upload['att_num'])) {
         $this->error(ECode::$ATT_NLIMIT);
     }
     if (isset($this->params['form']['file'])) {
         $errno = $this->params['form']['file']['error'];
     } else {
         $errno = UPLOAD_ERR_PARTIAL;
     }
     switch ($errno) {
         case UPLOAD_ERR_OK:
             $tmpFile = $this->params['form']['file']['tmp_name'];
             $tmpName = $this->params['form']['file']['name'];
             if (!is_uploaded_file($tmpFile)) {
                 $msg = ECode::$ATT_NONE;
                 break;
             }
             if ($size + filesize($tmpFile) > intval($upload['att_size'])) {
                 $msg = ECode::$ATT_SLIMIT;
                 break;
             }
             try {
                 if ($isFile) {
                     $article->addAttach($tmpFile, $tmpName);
                     $article = Article::getInstance($id, $this->_board);
                 } else {
                     Forum::addAttach($tmpFile, $tmpName);
                     $article = Forum::listAttach();
                 }
                 $wrapper = Wrapper::getInstance();
                 $this->set('data', $wrapper->attachment($article));
                 return;
             } catch (ArticleNullException $e) {
                 $this->error(ECode::$ARTICLE_NONE);
             } catch (ArchiveAttException $e) {
                 $msg = $e->getMessage();
             } catch (AttException $e) {
                 $msg = $e->getMessage();
             }
             break;
         case UPLOAD_ERR_INI_SIZE:
         case UPLOAD_ERR_FORM_SIZE:
         case UPLOAD_ERR_PARTIAL:
             $msg = ECode::$ATT_SLIMIT;
             break;
         case UPLOAD_ERR_NO_FILE:
             $msg = ECode::$ATT_NONE;
             break;
         default:
             $msg = ECode::$SYS_ERROR;
     }
     $this->error($msg);
 }