From d9e28e44393ef2ca6badbb5c6b39bb8c60c3c21c Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov <ivantcholakov@gmail.com> Date: Sun, 20 Sep 2009 05:39:00 +0300 Subject: [PATCH] Feature #306 - Moving the function rmdirr() into the main API file. --- .../classes/CourseArchiver.class.php | 1 - .../classes/CourseRecycler.class.php | 1 - .../classes/CourseRestorer.class.php | 1 - .../classes/DummyCourseCreator.class.php | 1 - main/inc/lib/main_api.lib.php | 55 ++++++++++++++++++- main/inc/lib/rmdirr.lib.php | 53 ------------------ main/newscorm/lp_controller.php | 1 - tests/rmdirr.lib.test_standalone.php | 1 - 8 files changed, 54 insertions(+), 60 deletions(-) delete mode 100644 main/inc/lib/rmdirr.lib.php diff --git a/main/coursecopy/classes/CourseArchiver.class.php b/main/coursecopy/classes/CourseArchiver.class.php index 9cd035afe0..04f08a2eed 100644 --- a/main/coursecopy/classes/CourseArchiver.class.php +++ b/main/coursecopy/classes/CourseArchiver.class.php @@ -25,7 +25,6 @@ */ require_once 'Course.class.php'; -require_once api_get_path(LIBRARY_PATH).'rmdirr.lib.php'; require_once api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php'; /** diff --git a/main/coursecopy/classes/CourseRecycler.class.php b/main/coursecopy/classes/CourseRecycler.class.php index bb774aaa37..be0e651499 100644 --- a/main/coursecopy/classes/CourseRecycler.class.php +++ b/main/coursecopy/classes/CourseRecycler.class.php @@ -25,7 +25,6 @@ */ require_once 'Course.class.php'; -require_once api_get_path(LIBRARY_PATH).'rmdirr.lib.php'; /** * Class to delete items from a Dokeos-course diff --git a/main/coursecopy/classes/CourseRestorer.class.php b/main/coursecopy/classes/CourseRestorer.class.php index 1bc9c0ca78..a6a97eba4b 100644 --- a/main/coursecopy/classes/CourseRestorer.class.php +++ b/main/coursecopy/classes/CourseRestorer.class.php @@ -37,7 +37,6 @@ require_once 'Learnpath.class.php'; require_once 'Survey.class.php'; require_once 'SurveyQuestion.class.php'; require_once 'Glossary.class.php'; -require_once api_get_path(LIBRARY_PATH).'rmdirr.lib.php'; require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php'; define('FILE_SKIP', 1); diff --git a/main/coursecopy/classes/DummyCourseCreator.class.php b/main/coursecopy/classes/DummyCourseCreator.class.php index c46bfbae57..dfe6f285c6 100644 --- a/main/coursecopy/classes/DummyCourseCreator.class.php +++ b/main/coursecopy/classes/DummyCourseCreator.class.php @@ -36,7 +36,6 @@ require_once 'ForumPost.class.php'; require_once 'CourseDescription.class.php'; require_once 'Learnpath.class.php'; require_once 'CourseRestorer.class.php'; -require_once api_get_path(LIBRARY_PATH).'rmdirr.lib.php'; class DummyCourseCreator { diff --git a/main/inc/lib/main_api.lib.php b/main/inc/lib/main_api.lib.php index a8069ab802..35df0b0aa6 100644 --- a/main/inc/lib/main_api.lib.php +++ b/main/inc/lib/main_api.lib.php @@ -2355,7 +2355,7 @@ function api_time_to_hms($seconds) { return "$hours:$min:$sec"; } -// TODO: This function is to be simplified. File access modes to be implemented. rmdirr() from rmdirr.lib.php to be moved here, next to this function. +// TODO: This function is to be simplified. File access modes to be implemented. /** * function adapted from a php.net comment * copy recursively a folder @@ -2398,6 +2398,59 @@ function copyr($source, $dest, $exclude = array(), $copied_files = array()) { return $files; } +/** + * Deletes a file, or a folder and its contents + * + * @author Aidan Lister <aidan@php.net> + * @version 1.0.3 + * @param string $dirname Directory to delete + * @return bool Returns TRUE on success, FALSE on failure + * @link http://aidanlister.com/2004/04/recursively-deleting-a-folder-in-php/ + * @author Yannick Warnier, adaptation for the Dokeos LMS, April, 2008 + * @author Ivan Tcholakov, a sanity check about Directory class creation has been added, September, 2009 + */ +function rmdirr($dirname) { + // A sanity check + if (!file_exists($dirname)) { + return false; + } + + // Simple delete for a file + if (is_file($dirname) || is_link($dirname)) { + $res = unlink($dirname); + if ($res === false) { + error_log(__FILE__.' line '.__LINE__.': '.((bool)ini_get('track_errors') ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); + } + return $res; + } + + // Loop through the folder + $dir = dir($dirname); + // A sanity check + $is_object_dir = is_object($dir); + if ($is_object_dir) { + while (false !== $entry = $dir->read()) { + // Skip pointers + if ($entry == '.' || $entry == '..') { + continue; + } + + // Recurse + rmdirr("$dirname/$entry"); + } + } + + // Clean up + if ($is_object_dir) { + $dir->close(); + } + $res = rmdir($dirname); + if ($res === false) { + error_log(__FILE__.' line '.__LINE__.': '.((bool)ini_get('track_errors') ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); + } + return $res; +} + // TODO: chmodr() is a better name. Some corrections are needed. function api_chmod_R($path, $filemode) { if (!is_dir($path)) { diff --git a/main/inc/lib/rmdirr.lib.php b/main/inc/lib/rmdirr.lib.php deleted file mode 100644 index 21801d149f..0000000000 --- a/main/inc/lib/rmdirr.lib.php +++ /dev/null @@ -1,53 +0,0 @@ -<?php -/** - * Delete a file, or a folder and its contents - * - * @author Aidan Lister <aidan@php.net> - * @version 1.0.3 - * @param string $dirname Directory to delete - * @return bool Returns TRUE on success, FALSE on failure - * @link http://aidanlister.com/2004/04/recursively-deleting-a-folder-in-php/ - * @author Yannick Warnier, adaptation for the Dokeos LMS, April, 2008 - * @author Ivan Tcholakov, a sanity check about Directory class creation has been added, September, 2009 - */ -function rmdirr($dirname) { - // A sanity check - if (!file_exists($dirname)) { - return false; - } - - // Simple delete for a file - if (is_file($dirname) || is_link($dirname)) { - $res = unlink($dirname); - if ($res === false) { - error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); - } - return $res; - } - - // Loop through the folder - $dir = dir($dirname); - // A sanity check - $is_object_dir = is_object($dir); - if ($is_object_dir) { - while (false !== $entry = $dir->read()) { - // Skip pointers - if ($entry == '.' || $entry == '..') { - continue; - } - - // Recurse - rmdirr("$dirname/$entry"); - } - } - - // Clean up - if ($is_object_dir) { - $dir->close(); - } - $res = rmdir($dirname); - if ($res === false) { - error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); - } - return $res; -} diff --git a/main/newscorm/lp_controller.php b/main/newscorm/lp_controller.php index 84623f8fe7..fb5244ba1a 100644 --- a/main/newscorm/lp_controller.php +++ b/main/newscorm/lp_controller.php @@ -51,7 +51,6 @@ if ($is_allowed_in_course == false){ api_not_allowed(true); } -require_once api_get_path(LIBRARY_PATH).'rmdirr.lib.php'; require_once api_get_path(LIBRARY_PATH).'fckeditor/fckeditor.php'; $lpfound = false; diff --git a/tests/rmdirr.lib.test_standalone.php b/tests/rmdirr.lib.test_standalone.php index 0a21aa2f91..4b2d8b1b60 100644 --- a/tests/rmdirr.lib.test_standalone.php +++ b/tests/rmdirr.lib.test_standalone.php @@ -21,7 +21,6 @@ $_test_sys_library_path = $_test_sys_code_path.'inc/lib/'; require_once($_current_dir.'simpletest/unit_tester.php'); require_once($_sys_include_path.'global.inc.php'); -require_once($_sys_library_path.'rmdirr.lib.php'); //header('Content-Type: text/html; charset=' . $charset); header('Content-Type: text/html; charset=' . 'UTF-8');