From c426ef3aa59ce0e890f5ab2676e8ad32d4cb23b9 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 29 Aug 2010 14:51:09 +0000 Subject: [PATCH] MDL-23984 improvements of check_dir_exists() - replacing by make_upload_directory() in cases where we want to be sure that the dir is writable; removning now default create and recursive params to make code easier to read --- admin/langimport.php | 8 ++++---- blocks/community/locallib.php | 2 +- files/externallib.php | 4 +--- filter/tex/latex.php | 2 +- lib/filestorage/file_storage.php | 2 +- lib/filestorage/zip_packer.php | 4 ++-- .../HTMLPurifier/DefinitionCache/Serializer.php | 2 +- lib/moodlelib.php | 10 +++------- lib/outputlib.php | 2 +- lib/simplepie/moodle_simplepie.php | 2 +- lib/weblib.php | 4 ++-- theme/image.php | 2 +- theme/javascript.php | 2 +- theme/styles.php | 2 +- 14 files changed, 21 insertions(+), 27 deletions(-) diff --git a/admin/langimport.php b/admin/langimport.php index a01068ed1c8..61a1b092a03 100755 --- a/admin/langimport.php +++ b/admin/langimport.php @@ -65,8 +65,8 @@ $notice_error = array(); if (($mode == INSTALLATION_OF_SELECTED_LANG) and confirm_sesskey() and !empty($pack)) { set_time_limit(0); - check_dir_exists($CFG->dataroot.'/temp/'); - check_dir_exists($CFG->dataroot.'/lang/'); + make_upload_directory('temp'); + make_upload_directory('lang'); if (is_array($pack)) { $packs = $pack; @@ -173,8 +173,8 @@ if ($mode == UPDATE_ALL_LANG) { } } - check_dir_exists($CFG->dataroot.'/temp/'); - check_dir_exists($CFG->dataroot.'/lang/'); + make_upload_directory('temp'); + make_upload_directory('lang'); $updated = false; // any packs updated? foreach ($neededlangs as $pack) { diff --git a/blocks/community/locallib.php b/blocks/community/locallib.php index 9cb8424ad5b..d2fce8bfb15 100644 --- a/blocks/community/locallib.php +++ b/blocks/community/locallib.php @@ -86,7 +86,7 @@ class block_community_manager { $params['courseid'] = $course->id; $params['filetype'] = HUB_BACKUP_FILE_TYPE; - check_dir_exists($CFG->dataroot.'/temp/backup'); + make_upload_directory('temp/backup'); $filename = md5(time() . '-' . $course->id . '-'. $USER->id . '-'. random_string(20)); diff --git a/files/externallib.php b/files/externallib.php index 4b1760fb609..40d7766a80b 100755 --- a/files/externallib.php +++ b/files/externallib.php @@ -207,9 +207,7 @@ class moodle_file_external extends external_api { throw new moodle_exception('nofile'); } // saving file - if (!check_dir_exists($CFG->dataroot.'/temp/wsupload')) { - throw new moodle_exception('cannotcreatetempdir'); - } + make_upload_directory('temp/wsupload'); if (empty($fileinfo['filename'])) { $filename = uniqid('wsupload').'_'.time().'.tmp'; diff --git a/filter/tex/latex.php b/filter/tex/latex.php index e758d125749..47c7c3bd364 100644 --- a/filter/tex/latex.php +++ b/filter/tex/latex.php @@ -20,7 +20,7 @@ // construct directory structure $this->temp_dir = $CFG->dataroot . "/temp/latex"; - check_dir_exists($this->temp_dir); + make_upload_directory('temp/latex'); } /** diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index 1db2e21dd84..974bce86543 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -625,7 +625,7 @@ class file_storage { $file_record->source = clean_param($source, PARAM_URL); if ($usetempfile) { - check_dir_exists($this->tempdir, true, true); + check_dir_exists($this->tempdir); $tmpfile = tempnam($this->tempdir, 'newfromurl'); $content = download_file_content($url, $headers, $postdata, $fullresponse, $timeout, $connecttimeout, $skipcertverify, $tmpfile); if ($content === false) { diff --git a/lib/filestorage/zip_packer.php b/lib/filestorage/zip_packer.php index cf4e4185b4f..a28adc6aa39 100644 --- a/lib/filestorage/zip_packer.php +++ b/lib/filestorage/zip_packer.php @@ -56,7 +56,7 @@ class zip_packer extends file_packer { $fs = get_file_storage(); - check_dir_exists($CFG->dataroot.'/temp/zip', true, true); + check_dir_exists($CFG->dataroot.'/temp/zip'); $tmpfile = tempnam($CFG->dataroot.'/temp/zip', 'zipstor'); if ($result = $this->archive_to_pathname($files, $tmpfile)) { @@ -279,7 +279,7 @@ class zip_packer extends file_packer { return $archivefile->extract_to_pathname($this, $contextid, $component, $filearea, $itemid, $pathbase, $userid); } - check_dir_exists($CFG->dataroot.'/temp/zip', true, true); + check_dir_exists($CFG->dataroot.'/temp/zip'); $pathbase = trim($pathbase, '/'); $pathbase = ($pathbase === '') ? '/' : '/'.$pathbase.'/'; diff --git a/lib/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php b/lib/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php index 43fe0454d1c..367a67c3ba1 100644 --- a/lib/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php +++ b/lib/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php @@ -109,7 +109,7 @@ class HTMLPurifier_DefinitionCache_Serializer extends */ private function _prepareDir($config) { $directory = $this->generateDirectoryPath($config); - return check_dir_exists($directory, true, true); //Moodle hack + return check_dir_exists($directory); //Moodle hack if (!is_dir($directory)) { $base = $this->generateBaseDirectoryPath($config); if (!is_dir($base)) { diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 883b92b6cf6..cd129a2029b 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -1079,12 +1079,8 @@ function purge_all_caches() { // purge all other caches: rss, simplepie, etc. remove_dir($CFG->dataroot.'/cache', true); - // some more diagnostics in case site is misconfigured - if (!check_dir_exists($CFG->dataroot.'/cache', true, true)) { - debugging('Can not create cache directory, please check permissions in dataroot.'); - } else if (!is_writeable($CFG->dataroot.'/cache')) { - debugging('Cache directory is not writeable, please verify permissions in dataroot.'); - } + // make sure cache dir is writable, throws exception if not + make_upload_directory('cache'); clearstatcache(); } @@ -5759,7 +5755,7 @@ class core_string_manager implements string_manager { // caches so we do not need to do all this merging and dependencies resolving again $this->cache[$lang][$component] = $string; if ($this->usediskcache) { - check_dir_exists("$this->cacheroot/$lang", true, true); + check_dir_exists("$this->cacheroot/$lang"); file_put_contents("$this->cacheroot/$lang/$component.php", "cache['$lang']['$component'] = ".var_export($string, true).";"); } return $string; diff --git a/lib/outputlib.php b/lib/outputlib.php index f5403dad703..9679120bd4b 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -627,7 +627,7 @@ class theme_config { $candidatesheet = "$CFG->dataroot/cache/theme/$this->name/designer.ser"; if (!file_exists($candidatesheet)) { $css = $this->css_content(); - check_dir_exists(dirname($candidatesheet), true, true); + check_dir_exists(dirname($candidatesheet)); file_put_contents($candidatesheet, serialize($css)); } else if (filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) { diff --git a/lib/simplepie/moodle_simplepie.php b/lib/simplepie/moodle_simplepie.php index 8c4d562350a..c6e1f6dafca 100644 --- a/lib/simplepie/moodle_simplepie.php +++ b/lib/simplepie/moodle_simplepie.php @@ -60,7 +60,7 @@ class moodle_simplepie extends SimplePie $this->file_class = 'moodle_simplepie_file'; $cachedir = moodle_simplepie::get_cache_directory(); - check_dir_exists($cachedir, true, true); + check_dir_exists($cachedir); parent::__construct(); // Match moodle encoding diff --git a/lib/weblib.php b/lib/weblib.php index 76deff665cc..500aa278cff 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1493,11 +1493,11 @@ function purify_html($text) { // this can not be done only once because we sometimes need to reset the cache $cachedir = $CFG->dataroot.'/cache/htmlpurifier'; - $status = check_dir_exists($cachedir, true, true); + check_dir_exists($cachedir); static $purifier = false; - static $config; if ($purifier === false) { + check_dir_exists($cachedir); require_once $CFG->libdir.'/htmlpurifier/HTMLPurifier.safe-includes.php'; $config = HTMLPurifier_Config::createDefault(); $config->set('Output.Newline', "\n"); diff --git a/theme/image.php b/theme/image.php index 1324ca7210a..350db3eaf6c 100644 --- a/theme/image.php +++ b/theme/image.php @@ -105,7 +105,7 @@ if ($rev > -1) { $pathinfo = pathinfo($imagefile); $cacheimage = "$candidatelocation/$image.".$pathinfo['extension']; if (!file_exists($cacheimage)) { - check_dir_exists(dirname($cacheimage), true, true); + check_dir_exists(dirname($cacheimage)); copy($imagefile, $cacheimage); } send_cached_image($cacheimage, $rev); diff --git a/theme/javascript.php b/theme/javascript.php index 6e1e11a0108..6e44df147cf 100644 --- a/theme/javascript.php +++ b/theme/javascript.php @@ -73,7 +73,7 @@ require_once('Minify.php'); $theme = theme_config::load($themename); if ($rev > -1) { - check_dir_exists(dirname($candidate), true, true); + check_dir_exists(dirname($candidate)); $fp = fopen($candidate, 'w'); fwrite($fp, minify($theme->javascript_files($type))); fclose($fp); diff --git a/theme/styles.php b/theme/styles.php index b64cad05a73..b68aa673873 100644 --- a/theme/styles.php +++ b/theme/styles.php @@ -109,7 +109,7 @@ send_cached_css($candidatesheet, $rev); function store_css(theme_config $theme, $csspath, $cssfiles) { $css = $theme->post_process(minify($cssfiles)); - check_dir_exists(dirname($csspath), true, true); + check_dir_exists(dirname($csspath)); $fp = fopen($csspath, 'w'); fwrite($fp, $css); fclose($fp);