示例#1
0
 /**
  * Handles an uploaded file, stores it to the correct folder, adds an entry
  * to the database and returns a TBGFile object
  * 
  * @param string $thefile The request parameter the file was sent as
  * 
  * @return TBGFile The TBGFile object
  */
 public function handleUpload($key, $file_name = null, $file_dir = null)
 {
     $apc_exists = self::CanGetUploadStatus();
     if ($apc_exists && !array_key_exists($this->getParameter('APC_UPLOAD_PROGRESS'), $_SESSION['__upload_status'])) {
         $_SESSION['__upload_status'][$this->getParameter('APC_UPLOAD_PROGRESS')] = array('id' => $this->getParameter('APC_UPLOAD_PROGRESS'), 'finished' => false, 'percent' => 0, 'total' => 0, 'complete' => 0);
     }
     try {
         $thefile = $this->getUploadedFile($key);
         if ($thefile !== null) {
             if ($thefile['error'] == UPLOAD_ERR_OK) {
                 Logging::log('No upload errors');
                 if (is_uploaded_file($thefile['tmp_name'])) {
                     Logging::log('Uploaded file is uploaded');
                     $files_dir = $file_dir === null ? Caspar::getUploadPath() : $file_dir;
                     $new_filename = $file_name === null ? Caspar::getUser()->getID() . '_' . NOW . '_' . basename($thefile['name']) : $file_name;
                     Logging::log('Moving uploaded file to ' . $new_filename);
                     if (!move_uploaded_file($thefile['tmp_name'], $files_dir . $new_filename)) {
                         Logging::log('Moving uploaded file failed!');
                         throw new \Exception(Caspar::getI18n()->__('An error occured when saving the file'));
                     } else {
                         Logging::log('Upload complete and ok');
                         return true;
                     }
                 } else {
                     Logging::log('Uploaded file was not uploaded correctly');
                     throw new \Exception(Caspar::getI18n()->__('The file was not uploaded correctly'));
                 }
             } else {
                 Logging::log('Upload error: ' . $thefile['error']);
                 switch ($thefile['error']) {
                     case UPLOAD_ERR_INI_SIZE:
                     case UPLOAD_ERR_FORM_SIZE:
                         throw new \Exception(Caspar::getI18n()->__('You cannot upload files bigger than %max_size% MB', array('%max_size%' => Settings::getUploadsMaxSize())));
                         break;
                     case UPLOAD_ERR_PARTIAL:
                         throw new \Exception(Caspar::getI18n()->__('The upload was interrupted, please try again'));
                         break;
                     case UPLOAD_ERR_NO_FILE:
                         throw new \Exception(Caspar::getI18n()->__('No file was uploaded'));
                         break;
                     default:
                         throw new \Exception(Caspar::getI18n()->__('An unhandled error occured') . ': ' . $thefile['error']);
                         break;
                 }
             }
             Logging::log('Uploaded file could not be uploaded');
             throw new \Exception(Caspar::getI18n()->__('The file could not be uploaded'));
         }
         Logging::log('Could not find uploaded file' . $key);
         throw new \Exception(Caspar::getI18n()->__('Could not find the uploaded file. Please make sure that it is not too big.'));
     } catch (Exception $e) {
         Logging::log('Upload exception: ' . $e->getMessage());
         if ($apc_exists) {
             $_SESSION['__upload_status'][$this->getParameter('APC_UPLOAD_PROGRESS')]['error'] = $e->getMessage();
             $_SESSION['__upload_status'][$this->getParameter('APC_UPLOAD_PROGRESS')]['finished'] = true;
             $_SESSION['__upload_status'][$this->getParameter('APC_UPLOAD_PROGRESS')]['percent'] = 100;
         }
         throw $e;
     }
 }
示例#2
0
 protected function getUser()
 {
     return Caspar::getUser();
 }