예제 #1
0
파일: Upload.php 프로젝트: kvenkat971/FileZ
 /**
  * Create a new File object from posted values and store it into the database.
  *
  * @param array $post       ~= $_POST
  * @param array $files      ~= $_FILES
  * @return App_Model_File
  */
 private function saveFile($post, $uploadedFile)
 {
     // Computing default values
     $comment = array_key_exists('comment', $post) ? $post['comment'] : '';
     // Validating lifetime
     $lifetime = fz_config_get('app', 'default_file_lifetime', 10);
     if (array_key_exists('lifetime', $post) && is_numeric($post['lifetime'])) {
         $lifetime = intval($post['lifetime']);
         $maxLifetime = intval(fz_config_get('app', 'max_file_lifetime', 20));
         if ($lifetime > $maxLifetime) {
             $lifetime = $maxLifetime;
         }
     }
     $availableFrom = array_key_exists('start-from', $post) ? $post['start-from'] : null;
     $availableFrom = new Zend_Date($availableFrom, Zend_Date::DATE_SHORT);
     $availableUntil = clone $availableFrom;
     $availableUntil->add($lifetime, Zend_Date::DAY);
     $user = $this->getUser();
     // Storing values
     $file = new App_Model_File();
     $file->setFileInfo($uploadedFile);
     if (option('visitor')) {
         $file->setVisitorUploader();
     } else {
         $file->setUploader($user);
     }
     $file->setCreatedAt(new Zend_Date());
     $file->comment = substr($comment, 0, 199);
     $file->setAvailableFrom($availableFrom);
     $file->setAvailableUntil($availableUntil);
     $file->notify_uploader = isset($post['email-notifications']);
     if (!empty($post['password'])) {
         $file->setPassword($post['password']);
     }
     try {
         $file->save();
         if ($file->moveUploadedFile($uploadedFile)) {
             fz_log('Saved "' . $file->file_name . '"[' . $file->id . '] uploaded by ' . $user);
             return $file;
         } else {
             $file->delete();
             return null;
         }
     } catch (Exception $e) {
         fz_log('Can\'t save file "' . $uploadedFile['name'] . '" uploaded by ' . $user, FZ_LOG_ERROR);
         fz_log($e, FZ_LOG_ERROR);
         return null;
     }
 }
예제 #2
0
 /**
  * @param App_Model_File $file
  *
  * @return bool
  *
  * @throws App_Exception_FileIsNotWritable
  * @throws App_Exception_Forbidden
  */
 public function delete(App_Model_File $file)
 {
     if ($file->user != (string) self::$_user->id) {
         throw new App_Exception_Forbidden();
     }
     $filePath = $this->_getAbsoluteFilePath($file->identity, $file->ext);
     $res = false;
     if (file_exists($filePath)) {
         if (!is_writable($filePath)) {
             throw new App_Exception_FileIsNotWritable($filePath);
         }
         $res = (bool) unlink($filePath);
         $file->delete();
     }
     try {
         $folderRelativePath = $this->_getRelativeFolderPath($file->identity);
         $path = implode('/', [self::$_config['path'], $folderRelativePath]);
         $count = count(explode('/', $folderRelativePath));
         for ($i = $count - 1; $i > -1; $i--) {
             rmdir($path);
             $path = dirname($path);
         }
     } catch (\Exception $e) {
     }
     return $res;
 }