MDL-28701 Add make_temp_directory() and make_cache_directory().

This commit is contained in:
Tom Lanyon
2011-09-10 10:35:48 +02:00
committed by Petr Skoda
parent 365bec4c06
commit e695890739
+59 -13
View File
@@ -1105,25 +1105,15 @@ function check_dir_exists($dir, $create = true, $recursive = true) {
}
/**
* Create a directory in dataroot and make sure it is writable.
* Create a directory and make sure it is writable.
*
* @param string $directory a string of directory names under $CFG->dataroot eg temp/something
* @param string $dir the full path of the directory to be created
* @param bool $exceptiononerror throw exception if error encountered
* @return string|false Returns full path to directory if successful, false if not; may throw exception
*/
function make_upload_directory($directory, $exceptiononerror = true) {
function make_writable_directory($dir, $exceptiononerror = true) {
global $CFG;
// Make sure a .htaccess file is here, JUST IN CASE the files area is in the open and .htaccess is supported
if (!file_exists("$CFG->dataroot/.htaccess")) {
if ($handle = fopen("$CFG->dataroot/.htaccess", 'w')) { // For safety
@fwrite($handle, "deny from all\r\nAllowOverride None\r\nNote: this file is broken intentionally, we do not want anybody to undo it in subdirectory!\r\n");
@fclose($handle);
}
}
$dir = "$CFG->dataroot/$directory";
if (file_exists($dir) and !is_dir($dir)) {
if ($exceptiononerror) {
throw new coding_exception($dir.' directory can not be created, file with the same name already exists.');
@@ -1155,6 +1145,62 @@ function make_upload_directory($directory, $exceptiononerror = true) {
return $dir;
}
/**
* Protect a directory from web access.
* Could be extended in the future to support other mechanisms (e.g. other webservers).
*
* @param string $dir the full path of the directory to be protected
*/
function protect_directory($dir) {
// Make sure a .htaccess file is here, JUST IN CASE the files area is in the open and .htaccess is supported
if (!file_exists("$dir/.htaccess")) {
if ($handle = fopen("$dir/.htaccess", 'w')) { // For safety
@fwrite($handle, "deny from all\r\nAllowOverride None\r\nNote: this file is broken intentionally, we do not want anybody to undo it in subdirectory!\r\n");
@fclose($handle);
}
}
}
/**
* Create a directory under dataroot and make sure it is writable.
*
* @param string $directory the full path of the directory to be created under $CFG->dataroot
* @param bool $exceptiononerror throw exception if error encountered
* @return string|false Returns full path to directory if successful, false if not; may throw exception
*/
function make_upload_directory($directory, $exceptiononerror = true) {
global $CFG;
protect_directory($CFG->dataroot);
return make_writable_directory("$CFG->dataroot/$directory", $exceptiononerror);
}
/**
* Create a directory under tempdir and make sure it is writable.
*
* @param string $directory the full path of the directory to be created under $CFG->tempdir
* @param bool $exceptiononerror throw exception if error encountered
* @return string|false Returns full path to directory if successful, false if not; may throw exception
*/
function make_temp_directory($directory, $exceptiononerror = true) {
global $CFG;
protect_directory($CFG->tempdir);
return make_writable_directory("$CFG->tempdir/$directory", $exceptiononerror);
}
/**
* Create a directory under cachedir and make sure it is writable.
*
* @param string $directory the full path of the directory to be created under $CFG->cachedir
* @param bool $exceptiononerror throw exception if error encountered
* @return string|false Returns full path to directory if successful, false if not; may throw exception
*/
function make_cache_directory($directory, $exceptiononerror = true) {
global $CFG;
protect_directory($CFG->cachedir);
return make_writable_directory("$CFG->cachedir/$directory", $exceptiononerror);
}
function init_memcached() {
global $CFG, $MCACHE;