]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/PluginsController.php
Remove 'svn' lines
[galette.git] / galette / lib / Galette / Controllers / PluginsController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette plugins controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Controllers
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.9.4dev - 2020-05-02
35 */
36
37 namespace Galette\Controllers;
38
39 use Slim\Http\Request;
40 use Slim\Http\Response;
41 use Galette\Core\Install;
42 use Galette\Core\PluginInstall;
43 use Laminas\Db\Adapter\Adapter;
44 use Analog\Analog;
45
46 /**
47 * Galette plugins controller
48 *
49 * @category Controllers
50 * @name PluginsController
51 * @package Galette
52 * @author Johan Cwiklinski <johan@x-tnd.be>
53 * @copyright 2020 The Galette Team
54 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
55 * @link http://galette.tuxfamily.org
56 * @since Available since 0.9.4dev - 2020-05-02
57 */
58
59 class PluginsController extends AbstractController
60 {
61 /**
62 * Plugins page
63 *
64 * @param Request $request PSR Request
65 * @param Response $response PSR Response
66 *
67 * @return Response
68 */
69 public function showPlugins(Request $request, Response $response): Response
70 {
71 $plugins = $this->plugins;
72
73 $plugins_list = $plugins->getModules();
74 $disabled_plugins = $plugins->getDisabledModules();
75
76 // display page
77 $this->view->render(
78 $response,
79 'plugins.tpl',
80 array(
81 'page_title' => _T("Plugins"),
82 'plugins_list' => $plugins_list,
83 'plugins_disabled_list' => $disabled_plugins
84 )
85 );
86 return $response;
87 }
88
89 /**
90 * Plugins activation/desactivaion
91 *
92 * @param Request $request PSR Request
93 * @param Response $response PSR Response
94 * @param array $args Request arguments
95 *
96 * @return Response
97 */
98 public function togglePlugin(Request $request, Response $response, array $args = []): Response
99 {
100 if (GALETTE_MODE !== 'DEMO') {
101 $plugins = $this->plugins;
102 $action = $args['action'];
103 $reload_plugins = false;
104 if ($action == 'activate') {
105 try {
106 $plugins->activateModule($args['module_id']);
107 $this->flash->addMessage(
108 'success_detected',
109 str_replace(
110 '%name',
111 $args['module_id'],
112 _T("Plugin %name has been enabled")
113 )
114 );
115 $reload_plugins = true;
116 } catch (\Exception $e) {
117 $this->flash->addMessage(
118 'error_detected',
119 $e->getMessage()
120 );
121 }
122 } elseif ($args['action'] == 'deactivate') {
123 try {
124 $plugins->deactivateModule($args['module_id']);
125 $this->flash->addMessage(
126 'success_detected',
127 str_replace(
128 '%name',
129 $args['module_id'],
130 _T("Plugin %name has been disabled")
131 )
132 );
133 $reload_plugins = true;
134 } catch (\Exception $e) {
135 $this->flash->addMessage(
136 'error_detected',
137 $e->getMessage()
138 );
139 }
140 }
141
142 //If some plugins have been (de)activated, we have to reload
143 if ($reload_plugins === true) {
144 $plugins->loadModules($this->preferences, GALETTE_PLUGINS_PATH, $this->i18n->getLongID());
145 }
146 }
147
148 return $response
149 ->withStatus(301)
150 ->withHeader('Location', $this->router->pathFor('plugins'));
151 }
152
153 /**
154 * Plugins database activation
155 *
156 * @param Request $request PSR Request
157 * @param Response $response PSR Response
158 * @param array $args Request arguments
159 *
160 * @return Response
161 */
162 public function initPluginDb(Request $request, Response $response, array $args = []): Response
163 {
164 if (GALETTE_MODE === 'DEMO') {
165 Analog::log(
166 'Trying to access plugin database initialization in DEMO mode.',
167 Analog::WARNING
168 );
169 return $response->withStatus(403);
170 }
171
172 $params = [];
173 $warning_detected = [];
174 $error_detected = [];
175
176 $plugid = $args['id'];
177 $plugin = $this->plugins->getModules($plugid);
178
179 if ($plugin === null) {
180 Analog::log(
181 'Unable to load plugin `' . $plugid . '`!',
182 Analog::URGENT
183 );
184 $notFound = $this->notFoundHandler;
185 return $notFound($request, $response);
186 }
187
188 $install = null;
189 $mdplugin = md5($plugin['root']);
190 if (
191 isset($this->session->$mdplugin)
192 && !isset($_GET['raz'])
193 ) {
194 $install = $this->session->$mdplugin;
195 } else {
196 $install = new PluginInstall();
197 }
198
199 $post = $request->getParsedBody();
200
201 if (isset($post['stepback_btn'])) {
202 $install->atPreviousStep();
203 } elseif (isset($post['install_prefs_ok'])) {
204 $install->atEndStep();
205 } elseif (isset($_POST['previous_version'])) {
206 $install->setInstalledVersion($_POST['previous_version']);
207 $install->atDbUpgradeStep();
208 } elseif (isset($post['install_dbperms_ok'])) {
209 if ($install->isInstall()) {
210 $install->atDbInstallStep();
211 } elseif ($install->isUpgrade()) {
212 $install->atVersionSelection();
213 }
214 } elseif (isset($post['install_type'])) {
215 $install->setMode($post['install_type']);
216 $install->atDbStep();
217 }
218
219 $step = 1;
220 $istep = 1;
221
222 if (isset($post['install_type'])) {
223 $params['install_type'] = $post['install_type'];
224 $istep = 2;
225 }
226
227 if (isset($post['install_dbperms_ok'])) {
228 if ($post['install_type'] === PluginInstall::INSTALL) {
229 $istep = 4;
230 } else {
231 $istep = 3;
232 }
233 }
234
235 if (isset($post['previous_version'])) {
236 $istep = 4;
237 }
238
239 if (isset($post['install_dbwrite_ok'])) {
240 $istep = 5;
241 }
242
243 if (isset($post['install_type'])) {
244 if ($post['install_type'] == PluginInstall::INSTALL) {
245 $step = 'i' . $istep;
246 } elseif ($istep > 1 && $post['install_type'] == PluginInstall::UPDATE) {
247 $step = 'u' . $istep;
248 }
249 }
250
251 switch ($step) {
252 case '1':
253 //let's look for updates scripts
254 $update_scripts = $install::getUpdateScripts($plugin['root'], TYPE_DB);
255 if (count($update_scripts) > 0) {
256 $params['update_scripts'] = $update_scripts;
257 }
258 break;
259 case 'i2':
260 case 'u2':
261 if (!defined('GALETTE_THEME_DIR')) {
262 define('GALETTE_THEME_DIR', './themes/default/');
263 }
264
265 $install_plugin = true;
266 //not used here, but from include
267 $zdb = $this->zdb;
268 ob_start();
269 include_once __DIR__ . '/../../install/steps/db_checks.php';
270 $params['results'] = ob_get_contents();
271 ob_end_clean();
272 break;
273 case 'u3':
274 $update_scripts = Install::getUpdateScripts($plugin['root'], TYPE_DB);
275 $params['update_scripts'] = $update_scripts;
276 break;
277 case 'i4':
278 case 'u4':
279 $messages = [];
280
281 // begin : copyright (2002) the phpbb group (support@phpbb.com)
282 // load in the sql parser
283 include GALETTE_ROOT . 'includes/sql_parse.php';
284 if ($step == 'u4') {
285 $update_scripts = Install::getUpdateScripts(
286 $plugin['root'],
287 TYPE_DB,
288 $_POST['previous_version']
289 );
290 } else {
291 $update_scripts['current'] = TYPE_DB . '.sql';
292 }
293
294 $sql_query = '';
295 foreach ($update_scripts as $key => $val) {
296 $sql_query .= @fread(
297 @fopen($plugin['root'] . '/scripts/' . $val, 'r'),
298 @filesize($plugin['root'] . '/scripts/' . $val)
299 );
300 $sql_query .= "\n";
301 }
302
303 $sql_query = preg_replace('/galette_/', PREFIX_DB, $sql_query);
304 $sql_query = remove_remarks($sql_query);
305
306 $sql_query = split_sql_file($sql_query, ';');
307
308 for ($i = 0; $i < sizeof($sql_query); $i++) {
309 $query = trim($sql_query[$i]);
310 if ($query != '' && $query[0] != '-') {
311 //some output infos
312 @list($w1, $w2, $w3, $extra) = array_pad(explode(' ', $query, 4), 4, '');
313 if ($extra != '') {
314 $extra = '...';
315 }
316 try {
317 $this->zdb->db->query(
318 $query,
319 Adapter::QUERY_MODE_EXECUTE
320 );
321 $messages['success'][] = $w1 . ' ' . $w2 . ' ' . $w3 .
322 ' ' . $extra;
323 } catch (Exception $e) {
324 Analog::log(
325 'Error executing query | ' . $e->getMessage() .
326 ' | Query was: ' . $query,
327 Analog::WARNING
328 );
329 if (
330 (strcasecmp(trim($w1), 'drop') != 0)
331 && (strcasecmp(trim($w1), 'rename') != 0)
332 ) {
333 $error_detected[] = $w1 . ' ' . $w2 . ' ' . $w3 . ' ' . $extra;
334 $error_detected[] = $e->getMessage() . '<br/>(' . $query . ')';
335 } else {
336 //if error are on drop, DROP, rename or RENAME we can continue
337 $warning_detected[] = $w1 . ' ' . $w2 . ' ' . $w3 . ' ' . $extra;
338 $warning_detected[] = $e->getMessage() . '<br/>(' . $query . ')';
339 }
340 }
341 }
342 }
343 break;
344 }
345
346 $this->session->$mdplugin = $install;
347
348 $params += [
349 'page_title' => $install->getStepTitle(),
350 'step' => $step,
351 'istep' => $istep,
352 'plugid' => $plugid,
353 'plugin' => $plugin,
354 'mode' => ($request->isXhr() ? 'ajax' : ''),
355 'error_detected' => $error_detected
356 ];
357
358 // display page
359 $this->view->render(
360 $response,
361 'plugin_initdb.tpl',
362 $params
363 );
364 return $response;
365 }
366 }