function testComputerFileSize() { $result = OC_Helper::computerFileSize("0 B"); $expected = '0.0'; $this->assertEquals($result, $expected); $result = OC_Helper::computerFileSize("1 kB"); $expected = '1024.0'; $this->assertEquals($result, $expected); $result = OC_Helper::computerFileSize("9.5 MB"); $expected = '9961472.0'; $this->assertEquals($result, $expected); $result = OC_Helper::computerFileSize("465.7 GB"); $expected = '500041567436.8'; $this->assertEquals($result, $expected); }
/** * get the quota for the current user * @return int */ private function getQuota() { if ($this->userQuota != -1) { return $this->userQuota; } $userQuota = OC_Preferences::getValue(OC_User::getUser(), 'files', 'quota', 'default'); if ($userQuota == 'default') { $userQuota = OC_AppConfig::getValue('files', 'default_quota', 'none'); } if ($userQuota == 'none') { $this->userQuota = 0; } else { $this->userQuota = OC_Helper::computerFileSize($userQuota); } return $this->userQuota; }
/** * get the quota for the user * @param user * @return int */ private function getQuota($user) { if (in_array($user, $this->userQuota)) { return $this->userQuota[$user]; } $userQuota = OC_Preferences::getValue($user, 'files', 'quota', 'default'); if ($userQuota == 'default') { $userQuota = OC_AppConfig::getValue('files', 'default_quota', 'none'); } if ($userQuota == 'none') { $this->userQuota[$user] = -1; } else { $this->userQuota[$user] = OC_Helper::computerFileSize($userQuota); } return $this->userQuota[$user]; }
/** * set the maximum upload size limit for apache hosts using .htaccess * * @param int $size file size in bytes * @param array $files override '.htaccess' and '.user.ini' locations * @return bool false on failure, size on success */ public static function setUploadLimit($size, $files = []) { //don't allow user to break his config $size = intval($size); if ($size < self::UPLOAD_MIN_LIMIT_BYTES) { return false; } $size = OC_Helper::phpFileSize($size); $phpValueKeys = array('upload_max_filesize', 'post_max_size'); // default locations if not overridden by $files $files = array_merge(['.htaccess' => OC::$SERVERROOT . '/.htaccess', '.user.ini' => OC::$SERVERROOT . '/.user.ini'], $files); $updateFiles = [$files['.htaccess'] => ['pattern' => '/php_value %1$s (\\S)*/', 'setting' => 'php_value %1$s %2$s'], $files['.user.ini'] => ['pattern' => '/%1$s=(\\S)*/', 'setting' => '%1$s=%2$s']]; $success = true; foreach ($updateFiles as $filename => $patternMap) { // suppress warnings from fopen() $handle = @fopen($filename, 'r+'); if (!$handle) { \OCP\Util::writeLog('files', 'Can\'t write upload limit to ' . $filename . '. Please check the file permissions', \OCP\Util::WARN); $success = false; continue; // try to update as many files as possible } $content = ''; while (!feof($handle)) { $content .= fread($handle, 1000); } foreach ($phpValueKeys as $key) { $pattern = vsprintf($patternMap['pattern'], [$key]); $setting = vsprintf($patternMap['setting'], [$key, $size]); $hasReplaced = 0; $newContent = preg_replace($pattern, $setting, $content, 1, $hasReplaced); if ($newContent !== null) { $content = $newContent; } if ($hasReplaced === 0) { $content .= "\n" . $setting; } } // write file back ftruncate($handle, 0); rewind($handle); fwrite($handle, $content); fclose($handle); } if ($success) { return OC_Helper::computerFileSize($size); } return false; }
/** * Make a computer file size (2 kB to 2048) * @param string $str file size in a fancy format * @return int a file size in bytes * * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 * @since 4.0.0 */ public static function computerFileSize($str) { return \OC_Helper::computerFileSize($str); }
/** * set the maximum upload size limit for apache hosts using .htaccess * @param int size filesisze in bytes * @return false on failure, size on success */ static function setUploadLimit($size) { //don't allow user to break his config -- upper boundary if ($size > PHP_INT_MAX) { //max size is always 1 byte lower than computerFileSize returns if ($size > PHP_INT_MAX + 1) { return false; } $size -= 1; } else { $size = OC_Helper::humanFileSize($size); $size = substr($size, 0, -1); //strip the B $size = str_replace(' ', '', $size); //remove the space between the size and the postfix } //don't allow user to break his config -- broken or malicious size input if (intval($size) == 0) { return false; } $htaccess = @file_get_contents(OC::$SERVERROOT . '/.htaccess'); //supress errors in case we don't have permissions for if (!$htaccess) { return false; } $phpValueKeys = array('upload_max_filesize', 'post_max_size'); foreach ($phpValueKeys as $key) { $pattern = '/php_value ' . $key . ' (\\S)*/'; $setting = 'php_value ' . $key . ' ' . $size; $hasReplaced = 0; $content = preg_replace($pattern, $setting, $htaccess, 1, $hasReplaced); if ($content !== NULL) { $htaccess = $content; } if ($hasReplaced == 0) { $htaccess .= "\n" . $setting; } } //supress errors in case we don't have permissions for it if (@file_put_contents(OC::$SERVERROOT . '/.htaccess', $htaccess)) { return OC_Helper::computerFileSize($size); } return false; }
* * You should have received a copy of the GNU Affero General Public License, version 3, * along with this program. If not, see <http://www.gnu.org/licenses/> * */ OC_JSON::checkSubAdminUser(); OCP\JSON::callCheck(); $username = isset($_POST["username"]) ? (string) $_POST["username"] : ''; if ($username === '' && !OC_User::isAdminUser(OC_User::getUser()) || !OC_User::isAdminUser(OC_User::getUser()) && !OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username)) { $l = \OC::$server->getL10N('core'); OC_JSON::error(array('data' => array('message' => $l->t('Authentication error')))); exit; } //make sure the quota is in the expected format $quota = (string) $_POST["quota"]; if ($quota !== 'none' and $quota !== 'default') { $quota = OC_Helper::computerFileSize($quota); $quota = OC_Helper::humanFileSize($quota); } // Return Success story if ($username) { \OC::$server->getConfig()->setUserValue($username, 'files', 'quota', $quota); } else { //set the default quota when no username is specified if ($quota === 'default') { //'default' as default quota makes no sense $quota = 'none'; } OC_Appconfig::setValue('files', 'default_quota', $quota); } OC_JSON::success(array("data" => array("username" => $username, 'quota' => $quota)));
/** * set the maximum upload size limit for apache hosts using .htaccess * * @param int $size file size in bytes * @return bool false on failure, size on success */ static function setUploadLimit($size) { //don't allow user to break his config -- upper boundary if ($size > PHP_INT_MAX) { //max size is always 1 byte lower than computerFileSize returns if ($size > PHP_INT_MAX + 1) { return false; } $size -= 1; } else { $size = OC_Helper::phpFileSize($size); } //don't allow user to break his config -- broken or malicious size input if (intval($size) === 0) { return false; } //suppress errors in case we don't have permissions for $htaccess = @file_get_contents(OC::$SERVERROOT . '/.htaccess'); if (!$htaccess) { return false; } $phpValueKeys = array('upload_max_filesize', 'post_max_size'); foreach ($phpValueKeys as $key) { $pattern = '/php_value ' . $key . ' (\\S)*/'; $setting = 'php_value ' . $key . ' ' . $size; $hasReplaced = 0; $content = preg_replace($pattern, $setting, $htaccess, 1, $hasReplaced); if ($content !== null) { $htaccess = $content; } if ($hasReplaced === 0) { $htaccess .= "\n" . $setting; } } //check for write permissions if (is_writable(OC::$SERVERROOT . '/.htaccess')) { file_put_contents(OC::$SERVERROOT . '/.htaccess', $htaccess); return OC_Helper::computerFileSize($size); } else { OC_Log::write('files', 'Can\'t write upload limit to ' . OC::$SERVERROOT . '/.htaccess. Please check the file permissions', OC_Log::WARN); } return false; }
/** * Get the quota of a user * * @param string $user * @return int Quota bytes */ public static function getUserQuota($user) { $config = \OC::$server->getConfig(); $userQuota = $config->getUserValue($user, 'files', 'quota', 'default'); if ($userQuota === 'default') { $userQuota = $config->getAppValue('files', 'default_quota', 'none'); } if ($userQuota === 'none') { return \OCP\Files\FileInfo::SPACE_UNLIMITED; } else { return OC_Helper::computerFileSize($userQuota); } }
/** * @dataProvider providesComputerFileSize */ function testComputerFileSize($expected, $input) { $result = OC_Helper::computerFileSize($input); $this->assertEquals($expected, $result); }
$breadcrumb = array(); $pathtohere = ""; foreach (explode("/", $dir) as $i) { if ($i != "") { $pathtohere .= "/" . str_replace('+', '%20', urlencode($i)); $breadcrumb[] = array("dir" => $pathtohere, "name" => $i); } } // make breadcrumb und filelist markup $list = new OC_Template("files", "part.list", ""); $list->assign("files", $files); $list->assign("baseURL", OC_Helper::linkTo("files", "index.php") . "?dir="); $list->assign("downloadURL", OC_Helper::linkTo("files", "download.php") . "?file="); $breadcrumbNav = new OC_Template("files", "part.breadcrumb", ""); $breadcrumbNav->assign("breadcrumb", $breadcrumb); $breadcrumbNav->assign("baseURL", OC_Helper::linkTo("files", "index.php") . "?dir="); $upload_max_filesize = OC_Helper::computerFileSize(ini_get('upload_max_filesize')); $post_max_size = OC_Helper::computerFileSize(ini_get('post_max_size')); $maxUploadFilesize = min($upload_max_filesize, $post_max_size); $freeSpace = OC_Filesystem::free_space('/'); $freeSpace = max($freeSpace, 0); $maxUploadFilesize = min($maxUploadFilesize, $freeSpace); $tmpl = new OC_Template("files", "index", "user"); $tmpl->assign("fileList", $list->fetchPage()); $tmpl->assign("breadcrumb", $breadcrumbNav->fetchPage()); $tmpl->assign('dir', $dir); $tmpl->assign('readonly', !OC_Filesystem::is_writable($dir)); $tmpl->assign("files", $files); $tmpl->assign('uploadMaxFilesize', $maxUploadFilesize); $tmpl->assign('uploadMaxHumanFilesize', OC_Helper::humanFileSize($maxUploadFilesize)); $tmpl->printPage();
public static function getUserQuota($user) { $userQuota = OC_Preferences::getValue($user, 'files', 'quota', 'default'); if ($userQuota === 'default') { $userQuota = OC_AppConfig::getValue('files', 'default_quota', 'none'); } if ($userQuota === 'none') { return \OC\Files\SPACE_UNLIMITED; } else { return OC_Helper::computerFileSize($userQuota); } }
/** * Get the quota of a user * * @param string $user * @return int Quota bytes */ public static function getUserQuota($user) { $userQuota = \OC::$server->getUserManager()->get($user)->getQuota(); if ($userQuota === 'none') { return \OCP\Files\FileInfo::SPACE_UNLIMITED; } else { return OC_Helper::computerFileSize($userQuota); } }
<?php // Init owncloud require_once '../../lib/base.php'; OC_JSON::checkAdminUser(); $username = $_POST["username"]; $quota = OC_Helper::computerFileSize($_POST["quota"]); // Return Success story OC_Preferences::setValue($username, 'files', 'quota', $quota); OC_JSON::success(array("data" => array("username" => $username, 'quota' => $quota)));