MDL-49329 admin: Convert install plugins tool to use new APIs
[moodle.git] / admin / tool / installaddon / index.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * The main screen of the tool.
20  *
21  * @package     tool_installaddon
22  * @copyright   2013 David Mudrak <david@moodle.com>
23  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 require(dirname(__FILE__) . '/../../../config.php');
27 require_once($CFG->libdir.'/adminlib.php');
29 admin_externalpage_setup('tool_installaddon_index');
31 if (!empty($CFG->disableonclickaddoninstall)) {
32     notice(get_string('featuredisabled', 'tool_installaddon'));
33 }
35 $pluginman = core_plugin_manager::instance();
36 $installer = tool_installaddon_installer::instance();
38 $output = $PAGE->get_renderer('tool_installaddon');
39 $output->set_installer_instance($installer);
41 // Handle the eventual request for installing from remote repository.
42 $remoterequest = optional_param('installaddonrequest', null, PARAM_RAW);
43 $installer->handle_remote_request($output, $remoterequest);
45 // Handle the confirmed installation request.
46 $installremote = optional_param('installremote', null, PARAM_COMPONENT);
47 $installremoteversion = optional_param('installremoteversion', null, PARAM_INT);
48 $installremoteconfirm = optional_param('installremoteconfirm', false, PARAM_BOOL);
50 if ($installremote and $installremoteversion) {
51     require_sesskey();
52     require_once($CFG->libdir.'/upgradelib.php');
54     $PAGE->set_pagelayout('maintenance');
55     $PAGE->set_popup_notification_allowed(false);
57     if ($pluginman->is_remote_plugin_installable($installremote, $installremoteversion)) {
58         $installable = array($pluginman->get_remote_plugin_info($installremote, $installremoteversion, true));
59         upgrade_install_plugins($installable, $installremoteconfirm,
60             get_string('installfromrepo', 'tool_installaddon'),
61             new moodle_url($PAGE->url, array('installremote' => $installremote,
62                 'installremoteversion' => $installremoteversion, 'installremoteconfirm' => 1)
63             )
64         );
65     }
66     // We should never get here.
67     throw new moodle_exception('installing_non_installable_component', 'tool_installaddon');
68 }
70 // Handle installation of a plugin from the ZIP file.
71 $installzipcomponent = optional_param('installzipcomponent', null, PARAM_COMPONENT);
72 $installzipstorage = optional_param('installzipstorage', null, PARAM_FILE);
73 $installzipconfirm = optional_param('installzipconfirm', false, PARAM_BOOL);
75 if ($installzipcomponent and $installzipstorage) {
76     require_sesskey();
77     require_once($CFG->libdir.'/upgradelib.php');
79     $PAGE->set_pagelayout('maintenance');
80     $PAGE->set_popup_notification_allowed(false);
82     $installable = array((object)array(
83         'component' => $installzipcomponent,
84         'zipfilepath' => make_temp_directory('tool_installaddon').'/'.$installzipstorage.'/plugin.zip',
85     ));
86     upgrade_install_plugins($installable, $installzipconfirm, get_string('installfromzip', 'tool_installaddon'),
87         new moodle_url($installer->index_url(), array('installzipcomponent' => $installzipcomponent,
88             'installzipstorage' => $installzipstorage, 'installzipconfirm' => 1)
89         )
90     );
91 }
93 $form = $installer->get_installfromzip_form();
95 if ($form->is_cancelled()) {
96     redirect($PAGE->url);
98 } else if ($data = $form->get_data()) {
99     $storage = $installer->make_installfromzip_storage();
100     $form->save_file('zipfile', $storage.'/plugin.zip');
102     $ziprootdir = $pluginman->get_plugin_zip_root_dir($storage.'/plugin.zip');
103     if (empty($ziprootdir)) {
104         echo $output->zip_not_valid_plugin_package_page($installer->index_url());
105         die();
106     }
108     $component = $installer->detect_plugin_component($storage.'/plugin.zip');
109     if (!empty($component) and !empty($data->plugintype)) {
110         // If the plugin type was explicitly set, make sure it matches the detected one.
111         list($detectedtype, $detectedname) = core_component::normalize_component($component);
112         if ($detectedtype !== $data->plugintype) {
113             $form->selected_plugintype_mismatch($detectedtype);
114             echo $output->index_page();
115             die();
116         }
117     }
118     if (empty($component)) {
119         // This should not happen as all plugins are supposed to declare their
120         // component. Still, let admins upload legacy packages if they want/need.
121         if (empty($data->plugintype)) {
122             $form->require_explicit_plugintype();
123             echo $output->index_page();
124             die();
125         }
126         if (!empty($data->rootdir)) {
127             $usepluginname = $data->rootdir;
128         } else {
129             $usepluginname = $ziprootdir;
130         }
131         $component = $data->plugintype.'_'.$usepluginname;
132     }
134     redirect($installer->index_url(array(
135         'installzipcomponent' => $component,
136         'installzipstorage' => basename($storage),
137         'sesskey' => sesskey(),
138     )));
141 // Display the tool main page.
142 echo $output->index_page();