Merge branch 'MDL-54785-master' of git://github.com/FMCorz/moodle

This commit is contained in:
Andrew Nicols
2016-06-27 08:59:49 +08:00
9 changed files with 43 additions and 144 deletions
+30 -41
View File
@@ -6256,65 +6256,54 @@ function valid_uploaded_file($newfile) {
/**
* Returns the maximum size for uploading files.
*
* There are eight possible upload limits:
* 1. No limit, if the upload isn't using a post request and the user has permission to ignore limits.
* 2. in Apache using LimitRequestBody (no way of checking or changing this)
* 3. in php.ini for 'upload_max_filesize' (can not be changed inside PHP)
* 4. in .htaccess for 'upload_max_filesize' (can not be changed inside PHP)
* 5. in php.ini for 'post_max_size' (can not be changed inside PHP)
* 6. by the Moodle admin in $CFG->maxbytes
* 7. by the teacher in the current course $course->maxbytes
* 8. by the teacher for the current module, eg $assignment->maxbytes
* There are seven possible upload limits:
* 1. in Apache using LimitRequestBody (no way of checking or changing this)
* 2. in php.ini for 'upload_max_filesize' (can not be changed inside PHP)
* 3. in .htaccess for 'upload_max_filesize' (can not be changed inside PHP)
* 4. in php.ini for 'post_max_size' (can not be changed inside PHP)
* 5. by the Moodle admin in $CFG->maxbytes
* 6. by the teacher in the current course $course->maxbytes
* 7. by the teacher for the current module, eg $assignment->maxbytes
*
* These last two are passed to this function as arguments (in bytes).
* Anything defined as 0 is ignored.
* The smallest of all the non-zero numbers is returned.
*
* The php.ini settings are only used if $usespost is true. This allows repositories that do not use post requests, such as
* repository_filesystem, to copy in files that are larger than post_max_size if the user has permission.
* @todo Finish documenting this function
*
* @param int $sitebytes Set maximum size
* @param int $coursebytes Current course $course->maxbytes (in bytes)
* @param int $modulebytes Current module ->maxbytes (in bytes)
* @param bool $usespost Does the upload we're getting the max size for use a post request?
* @param bool $unused This parameter has been deprecated and is not used any more.
* @return int The maximum size for uploading files.
*/
function get_max_upload_file_size($sitebytes = 0, $coursebytes = 0, $modulebytes = 0, $usespost = true) {
$sizes = array();
function get_max_upload_file_size($sitebytes=0, $coursebytes=0, $modulebytes=0, $unused = false) {
if ($usespost) {
if (!$filesize = ini_get('upload_max_filesize')) {
$filesize = '5M';
}
$sizes[] = get_real_size($filesize);
if (! $filesize = ini_get('upload_max_filesize')) {
$filesize = '5M';
}
$minimumsize = get_real_size($filesize);
if ($postsize = ini_get('post_max_size')) {
$sizes[] = get_real_size($postsize);
}
if ($sitebytes > 0) {
$sizes[] = $sitebytes;
}
} else {
if ($sitebytes != 0) {
// It's for possible that $sitebytes == USER_CAN_IGNORE_FILE_SIZE_LIMITS (-1).
$sizes[] = $sitebytes;
if ($postsize = ini_get('post_max_size')) {
$postsize = get_real_size($postsize);
if ($postsize < $minimumsize) {
$minimumsize = $postsize;
}
}
if ($coursebytes > 0) {
$sizes[] = $coursebytes;
if (($sitebytes > 0) and ($sitebytes < $minimumsize)) {
$minimumsize = $sitebytes;
}
if ($modulebytes > 0) {
$sizes[] = $modulebytes;
if (($coursebytes > 0) and ($coursebytes < $minimumsize)) {
$minimumsize = $coursebytes;
}
if (empty($sizes)) {
throw new coding_exception('You must specify at least one filesize limit.');
if (($modulebytes > 0) and ($modulebytes < $minimumsize)) {
$minimumsize = $modulebytes;
}
return min($sizes);
return $minimumsize;
}
/**
@@ -6327,11 +6316,11 @@ function get_max_upload_file_size($sitebytes = 0, $coursebytes = 0, $modulebytes
* @param int $coursebytes Current course $course->maxbytes (in bytes)
* @param int $modulebytes Current module ->maxbytes (in bytes)
* @param stdClass $user The user
* @param bool $usespost Does the upload we're getting the max size for use a post request?
* @param bool $unused This parameter has been deprecated and is not used any more.
* @return int The maximum size for uploading files.
*/
function get_user_max_upload_file_size($context, $sitebytes = 0, $coursebytes = 0, $modulebytes = 0, $user = null,
$usespost = true) {
$unused = false) {
global $USER;
if (empty($user)) {
@@ -6339,10 +6328,10 @@ function get_user_max_upload_file_size($context, $sitebytes = 0, $coursebytes =
}
if (has_capability('moodle/course:ignorefilesizelimits', $context, $user)) {
return get_max_upload_file_size(USER_CAN_IGNORE_FILE_SIZE_LIMITS, 0, 0, $usespost);
return USER_CAN_IGNORE_FILE_SIZE_LIMITS;
}
return get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes, $usespost);
return get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes);
}
/**
-78
View File
@@ -2216,84 +2216,6 @@ class core_moodlelib_testcase extends advanced_testcase {
$this->assertArrayHasKey(get_max_upload_file_size(), $result);
}
/**
* Provider for get_max_upload_file_size.
*
* @return array
*/
public function get_max_upload_file_size_provider() {
$inisize = min(array(get_real_size(ini_get('post_max_size')), get_real_size(ini_get('upload_max_filesize'))));
return [
'POST: inisize smallest' => [
$inisize + 10,
$inisize + 20,
$inisize + 30,
true,
$inisize,
],
'POST: sitebytes smallest' => [
$inisize - 30,
$inisize - 20,
$inisize - 10,
true,
$inisize - 30,
],
'POST: coursebytes smallest' => [
$inisize - 20,
$inisize - 30,
$inisize - 10,
true,
$inisize - 30,
],
'POST: modulebytes smallest' => [
$inisize - 20,
$inisize - 10,
$inisize - 30,
true,
$inisize - 30,
],
'POST: User can ignore site limit (respect ini)' => [
USER_CAN_IGNORE_FILE_SIZE_LIMITS,
0,
0,
true,
$inisize,
],
'NOPOST: inisize smallest' => [
$inisize + 10,
$inisize + 20,
$inisize + 30,
false,
$inisize + 10,
],
'NOPOST: User can ignore site limit (no limit)' => [
USER_CAN_IGNORE_FILE_SIZE_LIMITS,
0,
0,
false,
USER_CAN_IGNORE_FILE_SIZE_LIMITS,
],
];
}
/**
* Test get_max_upload_file_size with various combinations.
*
* @dataProvider get_max_upload_file_size_provider
*/
public function test_get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes, $ispost, $expectation) {
$this->assertEquals($expectation, get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes, $ispost));
}
/**
* Test that when get_max_upload_file_size is called with no sizes, and no post, an exception is thrown.
*/
public function test_get_max_upload_file_size_no_sizes() {
// If not using post we have to provide at least one other limit.
$this->setExpectedException('coding_exception', 'You must specify at least one filesize limit.');
get_max_upload_file_size(0, 0, 0, false);
}
/**
* Test function password_is_legacy_hash().
*/
+3
View File
@@ -4,6 +4,9 @@ information provided here is intended especially for developers.
=== 3.2 ===
* New option 'blanktarget' added to format_text. This option adds target="_blank" to links
* The parameter $usepost of the following functions has been deprecated and is not used any more:
- get_max_upload_file_size()
- get_user_max_upload_file_size()
=== 3.1 ===
+1 -5
View File
@@ -77,8 +77,6 @@ if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
}
$PAGE->set_course($course);
$usespost = true;
if ($repo_id) {
// Get repository instance information
$repooptions = array(
@@ -89,14 +87,12 @@ if ($repo_id) {
// Check permissions
$repo->check_capability();
$usespost = $repo->uses_post_requests();
}
$context = context::instance_by_id($contextid);
// Make sure maxbytes passed is within site filesize limits.
$maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $course->maxbytes, $maxbytes, null, $usespost);
$maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $course->maxbytes, $maxbytes);
$params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'areamaxbytes'=>$areamaxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
$params['action'] = 'browse';
-11
View File
@@ -810,17 +810,6 @@ class repository_filesystem extends repository {
public function supports_relative_file() {
return $this->get_option('relativefiles');
}
/**
* Helper function to indicate if this repository uses post requests for uploading files.
*
* Files are copied from the filesystem so don't rely on POST requests.
*
* @return bool
*/
public function uses_post_requests() {
return false;
}
}
/**
+3 -4
View File
@@ -2782,13 +2782,12 @@ abstract class repository implements cacheable_object {
/**
* Helper function to indicate if this repository uses post requests for uploading files.
*
* If the respository doesn't rely on uploading via POST requests, this can be overridden to return false,
* allowing users with the right permissions to upload files of any size from this repository.
*
* @deprecated since Moodle 3.2, 3.1.1, 3.0.5
* @return bool
*/
public function uses_post_requests() {
return true;
debugging('The method repository::uses_post_requests() is deprecated and must not be used anymore.', DEBUG_DEVELOPER);
return false;
}
}
+1 -2
View File
@@ -86,8 +86,7 @@ if (!empty($course)) {
$coursemaxbytes = $course->maxbytes;
}
// Make sure maxbytes passed is within site filesize limits.
$maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $coursemaxbytes, $maxbytes,
null, $repo->uses_post_requests());
$maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $coursemaxbytes, $maxbytes);
// Wait as long as it takes for this script to finish
core_php_time_limit::raise();
+4
View File
@@ -3,6 +3,10 @@ information provided here is intended especially for developers. Full
details of the repository API are available on Moodle docs:
http://docs.moodle.org/dev/Repository_API
=== 3.2 ===
* The method repository::uses_post_requests() has been deprecated and must not be used anymore.
=== 3.1 ===
* The following functions, previously used (exclusively) by upgrade steps are not available
+1 -3
View File
@@ -151,9 +151,7 @@ class core_webservice_externallib_testcase extends externallib_advanced_testcase
$siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo);
$this->assertEquals(0, $siteinfo['userquota']);
// The max_size is dependant upon the post_max_size, and upload_max_filesize values in php.ini.
$this->assertEquals(get_max_upload_file_size(USER_CAN_IGNORE_FILE_SIZE_LIMITS), $siteinfo['usermaxuploadfilesize']);
$this->assertEquals(USER_CAN_IGNORE_FILE_SIZE_LIMITS, $siteinfo['usermaxuploadfilesize']);
$this->assertEquals(true, $siteinfo['usercanmanageownfiles']);
$this->assertEquals(HOMEPAGE_SITE, $siteinfo['userhomepage']);