/** * Check if a given file size (in bytes) will run over a user's quota limit on Alfresco. * * @uses $USER * @param unknown_type $size * @param unknown_type $user * @return bool True if the size will not run over the user's quota, False otherwise. */ function elis_files_quota_check($filesize, $user = null) { global $USER; if ($user == null) { $user = $USER; } if (($userdata = elis_files_quota_info($user->username)) === false) { return false; } // If the user has no quota set or the filesize will not run over their current quota, return true. return ($userdata->quota == -1 || ($userdata->current + $filesize <= $userdata->quota)); }
/** * Returns array('success'=>true) or array('error'=>'error message') */ function handleUpload($uploadDirectory, $replaceOldFile = FALSE){ global $USER; if (!is_writable($uploadDirectory)){ return array('error' => "Server error. Upload directory isn't writable."); } if (!$this->file){ return array('error' => 'No files were uploaded.'); } $size = $this->file->getSize(); if ($size == 0) { return array('error' => 'File is empty'); } if ($size > $this->sizeLimit) { return array('error' => 'File is too large'); } $pathinfo = pathinfo($this->file->getName()); $filename = $pathinfo['filename']; // logic for handling file extensions if (isset($pathinfo['extension'])) { $ext = $pathinfo['extension']; $filename .= '.'.$ext; } else { $ext = NULL; } if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){ $these = implode(', ', $this->allowedExtensions); return array('error' => 'File has an invalid extension, it should be one of '. $these . '.'); } if(!$replaceOldFile){ /// don't overwrite previous files that were uploaded /// this just handles temporary files - consider forcing uniqueness here in the future? while (file_exists($uploadDirectory . $filename . '.' . $ext)) { $filename .= rand(10, 99); } } // make sure we're not going over the user's quota if (!elis_files_quota_check($size, $USER)) { if ($quotadata = elis_files_quota_info($USER->username)) { //specific error message, if possible $a = new stdClass; $a->current = round($quotadata->current / 1048576 * 10, 1) / 10 . get_string('sizemb'); $a->max = round($quotadata->quota / 1048576 * 10, 1) / 10 . get_string('sizemb'); return array('error' => get_string('erroruploadquotasize', 'repository_elisfiles', $a)); } else { //non-specific error message return array('error' => get_string('erroruploadquota', 'repository_elisfiles')); } } // make sure we're not uploading a duplicate filename $test = $this->validate_unique_filename($filename); if ($test !== true) { return array('error' => $test); } if ($this->file->save($uploadDirectory . $filename)) { return array('success'=>true); } else { $config = get_config('elisfiles'); // ELIS-4982 -- If FTP is enabled, check that the port is set correctly if ($config->file_transfer_method == ELIS_FILES_XFER_FTP) { // Attempt to make a connection to the FTP server $uri = parse_url($config->server_host); if (ftp_connect($uri['host'], $config->ftp_port, 5) === false) { return array('error' => get_string('errorftpinvalidport', 'repository_elisfiles', $uri['host'].':'.$config->ftp_port)); } } // Unknown error occurred return array('error' => get_string('errorupload', 'repository_elisfiles')); } }