From 7a8233020bdeb872dad6057b7799c43c1543aba4 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 13 Jan 2011 00:07:40 +0100 Subject: [PATCH] [feature/system-cron] Use a RecursiveDirectoryIterator instead of readdir. PHPBB3-9596 --- phpBB/common.php | 2 +- phpBB/cron.php | 2 +- phpBB/includes/cron/manager.php | 31 ++++++++++++------------------- tests/cron/manager_test.php | 2 +- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/phpBB/common.php b/phpBB/common.php index 00fc1a5cb7..e099a324bf 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -242,5 +242,5 @@ foreach ($cache->obtain_hooks() as $hook) if (!$config['use_system_cron']) { - $cron = new phpbb_cron_manager(); + $cron = new phpbb_cron_manager($phpbb_root_path, $phpEx); } diff --git a/phpBB/cron.php b/phpBB/cron.php index cdfd8d0fbe..9f13a9f462 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -63,7 +63,7 @@ if ($config['use_system_cron']) { $use_shutdown_function = false; - $cron = new phpbb_cron_manager(); + $cron = new phpbb_cron_manager($phpbb_root_path, $phpEx); } else { diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index d7d161793e..6be7d6ec0c 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -72,35 +72,28 @@ class phpbb_cron_manager */ public function find_cron_task_names() { - $tasks_root_path = $this->phpbb_root_path . 'includes/cron/task/'; + $task_root_path = $this->phpbb_root_path . 'includes/cron/task/'; $task_names = array(); $ext = '.' . $this->phpEx; $ext_length = strlen($ext); - $dh = opendir($tasks_root_path); - while (($mod = readdir($dh)) !== false) - { - // ignore ., .. and dot directories - // todo: change is_dir to account for symlinks - if ($mod[0] == '.' || !is_dir($tasks_root_path . $mod)) - { - continue; - } + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($task_root_path)); - $dh2 = opendir($tasks_root_path . $mod); - while (($file = readdir($dh2)) !== false) + foreach ($iterator as $fileinfo) + { + $file = preg_replace("#^$task_root_path#", '', $fileinfo->getPathname()); + + // skip directories and files direclty in the task root path + if ($fileinfo->isFile() && strpos($file, '/') !== false) { - $task_name = substr($file, 0, -$ext_length); - if (substr($file, -$ext_length) == $ext && $this->is_valid_name($mod) && $this->is_valid_name($task_name)) + $task_name = str_replace('/', '_', substr($file, 0, -$ext_length)); + if (substr($file, -$ext_length) == $ext && $this->is_valid_name($task_name)) { - $full_task_name = $mod . '_' . $task_name; - $task_names[] = $full_task_name; + $task_names[] = $task_name; } } - closedir($dh2); } - closedir($dh); return $task_names; } @@ -115,7 +108,7 @@ class phpbb_cron_manager */ public function is_valid_name($name) { - return (bool) preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); + return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); } /** diff --git a/tests/cron/manager_test.php b/tests/cron/manager_test.php index dcb3c6435a..39e052bd57 100644 --- a/tests/cron/manager_test.php +++ b/tests/cron/manager_test.php @@ -11,7 +11,7 @@ class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase { public function setUp() { - $this->manager = new phpbb_cron_manager(); + $this->manager = new phpbb_cron_manager(__DIR__ . '/../../phpBB/', 'php'); } public function test_manager_finds_shipped_tasks()