Skip to content

Commit

Permalink
MDL-28701 Add make_temp_directory() and make_cache_directory().
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlanyon authored and skodak committed Sep 10, 2011
1 parent 365bec4 commit e695890
Showing 1 changed file with 59 additions and 13 deletions.
72 changes: 59 additions & 13 deletions lib/setuplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit e695890

Please sign in to comment.