From 30bec5ba8dfb1dc8b18aa085e2870700d9b6cf1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Fri, 22 Mar 2013 17:38:59 +0100 Subject: [PATCH] MDL-38509 Add new tool_installaddon_installer::get_plugintype_root() method --- admin/tool/installaddon/classes/installer.php | 34 ++++++++++++++++--- .../classes/installfromzip_form.php | 3 +- admin/tool/installaddon/permcheck.php | 15 +++----- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/admin/tool/installaddon/classes/installer.php b/admin/tool/installaddon/classes/installer.php index 2833254837e..60397cacf9b 100644 --- a/admin/tool/installaddon/classes/installer.php +++ b/admin/tool/installaddon/classes/installer.php @@ -115,13 +115,15 @@ class tool_installaddon_installer { } /** - * Is it possible to create a new plugin directory for the given plugin type? + * Returns the full path of the root of the given plugin type + * + * Null is returned if the plugin type is not known. False is returned if the plugin type + * root is expected but not found. Otherwise, string is returned. * - * @throws coding_exception for invalid plugin types or non-existing plugin type locations * @param string $plugintype - * @return boolean + * @return string|bool|null */ - public function is_plugintype_writable($plugintype) { + public function get_plugintype_root($plugintype) { $plugintypepath = null; foreach (get_plugin_types() as $type => $fullpath) { @@ -131,10 +133,32 @@ class tool_installaddon_installer { } } if (is_null($plugintypepath)) { - throw new coding_exception('Unknown plugin type!'); + return null; } if (!is_dir($plugintypepath)) { + return false; + } + + return $plugintypepath; + } + + /** + * Is it possible to create a new plugin directory for the given plugin type? + * + * @throws coding_exception for invalid plugin types or non-existing plugin type locations + * @param string $plugintype + * @return boolean + */ + public function is_plugintype_writable($plugintype) { + + $plugintypepath = $this->get_plugintype_root($plugintype); + + if (is_null($plugintypepath)) { + throw new coding_exception('Unknown plugin type!'); + } + + if ($plugintypepath === false) { throw new coding_exception('Plugin type location does not exist!'); } diff --git a/admin/tool/installaddon/classes/installfromzip_form.php b/admin/tool/installaddon/classes/installfromzip_form.php index aef05c7fcfb..9942679cb7d 100644 --- a/admin/tool/installaddon/classes/installfromzip_form.php +++ b/admin/tool/installaddon/classes/installfromzip_form.php @@ -78,8 +78,7 @@ class tool_installaddon_installfromzip extends moodleform { $errors = parent::validation($data, $files); if (!$installer->is_plugintype_writable($data['plugintype'])) { - $paths = get_plugin_types(true); - $path = $paths[$data['plugintype']]; + $path = $installer->get_plugintype_root($data['plugintype']); $errors['plugintype'] = get_string('permcheckresultno', 'tool_installaddon', array('path' => $path)); } diff --git a/admin/tool/installaddon/permcheck.php b/admin/tool/installaddon/permcheck.php index d2125bb6a56..f38d109f59a 100644 --- a/admin/tool/installaddon/permcheck.php +++ b/admin/tool/installaddon/permcheck.php @@ -53,20 +53,15 @@ if (is_null($plugintype)) { die(); } -$plugintypepath = null; -foreach (get_plugin_types() as $type => $fullpath) { - if ($type === $plugintype) { - $plugintypepath = $fullpath; - break; - } -} -if (is_null($plugintypepath)) { +$installer = tool_installaddon_installer::instance(); + +$plugintypepath = $installer->get_plugintype_root($plugintype); + +if (empty($plugintypepath)) { header('HTTP/1.1 400 Bad Request'); die(); } -$installer = tool_installaddon_installer::instance(); - $response = array('path' => $plugintypepath); if ($installer->is_plugintype_writable($plugintype)) {