From e69589073963506b6a5b92aff034b411100e6ef6 Mon Sep 17 00:00:00 2001 From: Tom Lanyon Date: Tue, 23 Aug 2011 11:26:46 +0930 Subject: [PATCH] MDL-28701 Add make_temp_directory() and make_cache_directory(). --- lib/setuplib.php | 72 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/lib/setuplib.php b/lib/setuplib.php index a128ca115ce..297ed934a98 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -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;