示例#1
0
 public function processUploadedFile($pluginKey, array $fileInfo, $bundle = null, $validFileExtensions = array(), $maxUploadSize = null, $dimensions = null)
 {
     $language = OW::getLanguage();
     $error = false;
     if (!OW::getUser()->isAuthenticated()) {
         throw new InvalidArgumentException($language->text('base', 'user_is_not_authenticated'));
     }
     if (empty($fileInfo) || !is_uploaded_file($fileInfo['tmp_name'])) {
         throw new InvalidArgumentException($language->text('base', 'upload_file_fail'));
     }
     if ($fileInfo['error'] != UPLOAD_ERR_OK) {
         switch ($fileInfo['error']) {
             case UPLOAD_ERR_INI_SIZE:
                 $error = $language->text('base', 'upload_file_max_upload_filesize_error');
                 break;
             case UPLOAD_ERR_PARTIAL:
                 $error = $language->text('base', 'upload_file_file_partially_uploaded_error');
                 break;
             case UPLOAD_ERR_NO_FILE:
                 $error = $language->text('base', 'upload_file_no_file_error');
                 break;
             case UPLOAD_ERR_NO_TMP_DIR:
                 $error = $language->text('base', 'upload_file_no_tmp_dir_error');
                 break;
             case UPLOAD_ERR_CANT_WRITE:
                 $error = $language->text('base', 'upload_file_cant_write_file_error');
                 break;
             case UPLOAD_ERR_EXTENSION:
                 $error = $language->text('base', 'upload_file_invalid_extention_error');
                 break;
             default:
                 $error = $language->text('base', 'upload_file_fail');
         }
         throw new InvalidArgumentException($error);
     }
     if (empty($validFileExtensions)) {
         $validFileExtensions = json_decode(OW::getConfig()->getValue('base', 'attch_ext_list'), true);
     }
     if ($maxUploadSize === null) {
         $maxUploadSize = OW::getConfig()->getValue('base', 'attch_file_max_size_mb');
     }
     if (!empty($validFileExtensions) && !in_array(UTIL_File::getExtension($fileInfo['name']), $validFileExtensions)) {
         throw new InvalidArgumentException($language->text('base', 'upload_file_extension_is_not_allowed'));
     }
     // get all bundle upload size
     $bundleSize = floor($fileInfo['size'] / 1024);
     if ($bundle !== null) {
         $list = $this->attachmentDao->findAttahcmentByBundle($pluginKey, $bundle);
         /* @var $item BOL_Attachment */
         foreach ($list as $item) {
             $bundleSize += $item->getSize();
         }
     }
     if ($maxUploadSize > 0 && $bundleSize > $maxUploadSize * 1024) {
         throw new InvalidArgumentException($language->text('base', 'upload_file_max_upload_filesize_error'));
     }
     $attachDto = new BOL_Attachment();
     $attachDto->setUserId(OW::getUser()->getId());
     $attachDto->setAddStamp(time());
     $attachDto->setStatus(0);
     $attachDto->setSize(floor($fileInfo['size'] / 1024));
     $attachDto->setOrigFileName(htmlspecialchars($fileInfo['name']));
     $attachDto->setFileName(uniqid() . '_' . UTIL_File::sanitizeName($attachDto->getOrigFileName()));
     $attachDto->setPluginKey($pluginKey);
     if ($bundle !== null) {
         $attachDto->setBundle($bundle);
     }
     $this->attachmentDao->save($attachDto);
     $uploadPath = $this->getAttachmentsDir() . $attachDto->getFileName();
     $tempPath = $this->getAttachmentsDir() . 'temp_' . $attachDto->getFileName();
     if (in_array(UTIL_File::getExtension($fileInfo['name']), array('jpg', 'jpeg', 'gif', 'png'))) {
         try {
             $image = new UTIL_Image($fileInfo['tmp_name']);
             if (empty($dimensions)) {
                 $dimensions = array('width' => 1000, 'height' => 1000);
             }
             $image->resizeImage($dimensions['width'], $dimensions['height'])->orientateImage()->saveImage($tempPath);
             $image->destroy();
             @unlink($fileInfo['tmp_name']);
         } catch (Exception $e) {
             throw new InvalidArgumentException($language->text('base', 'upload_file_fail'));
         }
     } else {
         move_uploaded_file($fileInfo['tmp_name'], $tempPath);
     }
     OW::getStorage()->copyFile($tempPath, $uploadPath);
     OW::getStorage()->chmod($uploadPath, 0666);
     unlink($tempPath);
     return array('uid' => $attachDto->getBundle(), 'dto' => $attachDto, 'path' => $uploadPath, 'url' => $this->getAttachmentsUrl() . $attachDto->getFileName());
 }