Skip to content

Commit

Permalink
Update .user.ini when setting upload size limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin McCorkell committed Jul 20, 2015
1 parent f573659 commit d3bcafe
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
3 changes: 2 additions & 1 deletion apps/files/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
}

$htaccessWritable=is_writable(OC::$SERVERROOT.'/.htaccess');
$userIniWritable=is_writable(OC::$SERVERROOT.'/.user.ini');

$tmpl = new OCP\Template( 'files', 'admin' );
$tmpl->assign( 'uploadChangable', $htaccessWorking and $htaccessWritable );
$tmpl->assign( 'uploadChangable', ($htaccessWorking and $htaccessWritable) or $userIniWritable );
$tmpl->assign( 'uploadMaxFilesize', $maxUploadFilesize);
// max possible makes only sense on a 32 bit system
$tmpl->assign( 'displayMaxPossibleUploadSize', PHP_INT_SIZE===4);
Expand Down
2 changes: 2 additions & 0 deletions apps/files/templates/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<br/>
<input type="hidden" value="<?php p($_['requesttoken']); ?>" name="requesttoken" />
<?php if($_['uploadChangable']): ?>
<?php p($l->t('With PHP-FPM this value may take up to 5 minutes to take effect after saving.')); ?>
<br/>
<input type="submit" name="submitFilesAdminSettings" id="submitFilesAdminSettings"
value="<?php p($l->t( 'Save' )); ?>"/>
<?php else: ?>
Expand Down
70 changes: 48 additions & 22 deletions lib/private/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @author Thomas Müller <[email protected]>
* @author Valerio Ponte <[email protected]>
* @author Vincent Petry <[email protected]>
* @author Robin McCorkell <[email protected]>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
Expand Down Expand Up @@ -288,38 +289,63 @@ static function setUploadLimit($size) {
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;
$updateFiles = [
OC::$SERVERROOT . '/.htaccess' => [
'pattern' => '/php_value %1$s (\S)*/',
'setting' => 'php_value %1$s %2$s'
],
OC::$SERVERROOT . '/.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);
}
if ($hasReplaced === 0) {
$htaccess .= "\n" . $setting;

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);
}

//check for write permissions
if (is_writable(OC::$SERVERROOT . '/.htaccess')) {
file_put_contents(OC::$SERVERROOT . '/.htaccess', $htaccess);
if ($success) {
return OC_Helper::computerFileSize($size);
} else {
\OCP\Util::writeLog('files',
'Can\'t write upload limit to ' . OC::$SERVERROOT . '/.htaccess. Please check the file permissions',
\OCP\Util::WARN);
}
return false;
}
Expand Down

0 comments on commit d3bcafe

Please sign in to comment.