You are here

class qqFileUploader in TinyBrowser 7

Hierarchy

Expanded class hierarchy of qqFileUploader

File

tinybrowser/fileuploader.php, line 100

View source
class qqFileUploader {
  private $allowedExtensions = array();
  private $sizeLimit = 1048576;
  private $file;
  function __construct(array $allowedExtensions = array(), $sizeLimit = 1048576) {
    $allowedExtensions = array_map("strtolower", $allowedExtensions);
    $this->allowedExtensions = $allowedExtensions;
    $this->sizeLimit = $sizeLimit;
    if (isset($_GET['qqfile'])) {
      $this->file = new qqUploadedFileXhr();
    }
    elseif (isset($_FILES['qqfile'])) {
      $this->file = new qqUploadedFileForm();
    }
    else {
      $this->file = false;
    }
  }

  /**
   * Returns 'Success' or 'Error: (error message)' string
   */
  function handleUpload($uploadDirectory, $replaceOldFile = FALSE) {
    if (!is_writable($uploadDirectory)) {
      error_log("handleUpload: Server error. Upload directory [{$uploadDirectory}] os not writable.", 3, 'error.log');
      return 'Error: Upload directory ' . $uploadDirectory . ' is not writable';
    }
    if (!$this->file) {
      error_log("handleUpload: No files were uploaded.", 3, 'error.log');
      return 'Error: No files were uploaded';
    }
    $size = $this->file
      ->getSize();
    if ($size == 0) {
      error_log("handleUpload: File is empty.", 3, 'error.log');
      return 'Error: File is empty';
    }

    // if sizeLimit is 0, then we do not check max file size
    if ($this->sizeLimit > 0 && $size > $this->sizeLimit) {
      error_log("handleUpload: File is too large.", 3, 'error.log');
      return 'Error: File is too big';
    }

    // quota support with quick upload
    if ($tinybrowser['quota'][$typenow] > 0) {
      $ret = dirsize($tinybrowser['docroot'] . $tinybrowser['path'][$typenow]);
      $remain_space = $tinybrowser['quota'][$typenow] - $ret['size'];
      if ($remain_space < 0) {
        $remain_space = 0;
      }
      if ($remain_space < $size) {
        error_log("handleUpload: Quota error, allocated space is full", 3, 'error.log');
        return 'Error: Allocated disk space is full.';
      }
    }
    $pathinfo = pathinfo($this->file
      ->getName());
    $filename = $pathinfo['filename'];

    //$filename = md5(uniqid());
    $ext = $pathinfo['extension'];
    if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) {
      $these = implode(', ', $this->allowedExtensions);
      error_log("handleUpload: File has an invalid extension. it should be one of {$these} .", 3, 'error.log');
      return 'Error: File has an invalid extension';
    }
    if (file_exists($uploadDirectory . $filename . '.' . $ext)) {
      if ($tinybrowser['upload_mode'] == 3) {

        // keep the existing one, reject the new one
        return 'Error: The same file name already exists';
      }
      else {
        if ($tinybrowser['upload_mode'] == 2) {

          // keep the existing one, rename the new one
          for ($idx = 1;; $idx++) {
            $dest_filename = $uploadDirectory . $filename . '-' . $idx . '.' . $ext;
            if (!file_exists($dest_filename)) {
              $filename .= '-' . $idx;
              break;
            }
          }
        }
      }
    }

    // replace the exiting one with the new one
    if ($this->file
      ->save($uploadDirectory . $filename . '.' . $ext)) {
      return 'Success';
    }
    else {
      return 'Error: Could not save uploaded file.';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
qqFileUploader::$allowedExtensions private property
qqFileUploader::$file private property
qqFileUploader::$sizeLimit private property
qqFileUploader::handleUpload function Returns 'Success' or 'Error: (error message)' string
qqFileUploader::__construct function