The usual changes: forms fixed
[mailer.git] / inc / modules / admin / what-extensions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 03/22/2004 *
4  * ===================                          Last change: 12/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-extentions.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Extension management                             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Erweiterungen-Management                         *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if ((!defined('__SECURITY')) || (!isAdmin())) {
40         die();
41 } // END - if
42
43 // Add description as navigation point
44 addYouAreHereLink('admin', __FILE__);
45
46 // By default generate overview of all installed extensions
47 $do = 'overview';
48
49 if (isGetRequestElementSet('register_ext')) {
50         // We are about to register a new extension
51         $do = 'register';
52         $taskId = determineExtensionTaskId(getRequestElement('register_ext'));
53
54         // The id comes from task management and it is - of course - *not* the extension's name!
55 } elseif ((isFormSent('change')) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
56         // De-/activate extensions
57         foreach (postRequestElement('sel') as $ext_id => $ext_active) {
58                 // Shall we keep the extension always active?
59                 if ((isset($GLOBALS['cache_array']['always_active'][getExtensionName($ext_id)])) && ($GLOBALS['cache_array']['always_active'][getExtensionName($ext_id)] == 'Y') && ($ext_active == 'Y')) {
60                         // Keep this extension active!
61                         displayMessage('{%message,ADMIN_EXTENSION_ALWAYS_ACTIVE=' . $ext_id . '%}');
62                 } else {
63                         // Deactivation is default
64                         $active = 'N';
65                         setExtensionMode('deactivate');
66
67                         // Is the extension deactivated?
68                         if ($ext_active != 'Y') {
69                                 // Then we can activate it
70                                 $active = 'Y';
71                                 setExtensionMode('activate');
72                         } // END - if
73
74                         // Update database
75                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='%s' WHERE `id`=%s AND `ext_active`='%s' LIMIT 1",
76                                 array(
77                                         $active,
78                                         bigintval($ext_id),
79                                         $ext_active
80                                 ), __FILE__, __LINE__);
81
82                         // Run embeded SQL commands
83                         doExtensionSqls($ext_id, getExtensionMode());
84                 }
85         } // END - foreach
86
87         // Redirect to prevent missing cache in js.php
88         redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;changed=' . countPostSelection());
89 } elseif (((isFormSent('edit')) || (isPostRequestElementSet('do_edit'))) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
90         // Change settings like CSS file load
91         if (isPostRequestElementSet('do_edit')) {
92                 // Change entries
93                 $cache_update = '0';
94                 foreach (postRequestElement('sel') as $ext_id => $sel) {
95                         // Secure id
96                         $ext_id = bigintval($ext_id);
97
98                         // Change this extension?
99                         if ($sel == 1) {
100                                 // Load mode is modify
101                                 setExtensionMode('modify');
102
103                                 // Get entry for 'active'
104                                 $active = postRequestElement('active', $ext_id);
105
106                                 // Update extension's record
107                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
108                                         // Update also CSS column when extensions ext-sql_patches is newer or exact v0.0.6
109                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_has_css`='%s',`ext_active`='%s' WHERE `id`=%s LIMIT 1",
110                                                 array(postRequestElement('css', $ext_id), $active, $ext_id), __FILE__, __LINE__);
111                                 } else {
112                                         // When extension is older than v0.0.6 there is no column for the CSS information
113                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='%s' WHERE `id`=%s LIMIT 1",
114                                                 array($active, $ext_id), __FILE__, __LINE__);
115                                 }
116
117                                 // Run SQLs on activation / deactivation
118                                 switch ($active) {
119                                         case 'Y': setExtensionMode('activate');   break;
120                                         case 'N': setExtensionMode('deactivate'); break;
121                                 } // END - switch
122
123                                 // Run embeded SQL commands
124                                 doExtensionSqls($ext_id, getExtensionMode());
125                         } // END - if
126                 } // END - foreach
127
128                 // Redirect to prevent missing cache in js.php
129                 redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;edited=' . countPostSelection());
130         } else {
131                 // Edit selected entries
132                 $OUT = '';
133                 foreach (postRequestElement('sel') as $ext_id => $sel) {
134                         // Edit this extension?
135                         if (($sel == 'Y') || ($sel != 'Y')) {
136                                 // Default is no CSS for non-updated mailers
137                                 $css = 'N';
138
139                                 // Load required data
140                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
141                                         $result = SQL_QUERY_ESC("SELECT `ext_has_css`, `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
142                                                 array(bigintval($ext_id)), __FILE__, __LINE__);
143                                         list($css, $active) = SQL_FETCHROW($result);
144                                         SQL_FREERESULT($result);
145                                 } else {
146                                         $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
147                                                 array(bigintval($ext_id)), __FILE__, __LINE__);
148                                         list($active) = SQL_FETCHROW($result);
149                                         SQL_FREERESULT($result);
150                                         $css = 'X';
151                                 }
152
153                                 // Prepare CSS column
154                                 $cssSelection = '---';
155                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
156                                         $cssSelection = addSelectionBox('yn', $css, 'css', $ext_id);
157                                 } // END - if
158
159                                 // Prepare data for the row template
160                                 $content = array(
161                                         'id'     => $ext_id,
162                                         'name'   => getExtensionName($ext_id),
163                                         'active' => addSelectionBox('yn', $active, 'active', $ext_id),
164                                         'css'    => $cssSelection,
165                                 );
166
167                                 // Load row template and switch color
168                                 $OUT .= loadTemplate('admin_edit_extensions_row', TRUE, $content);
169                         } // END - if
170                 } // END - foreach
171
172                 // Load template
173                 loadTemplate('admin_edit_extensions', FALSE, $OUT);
174                 $do = 'edit';
175         }
176 } elseif ((isPostRequestElementSet('delete')) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
177         // List extensions and when verbose is enabled SQL statements which will be executed
178         $OUT = '';
179         foreach (postRequestElement('sel') as $ext_id => $sel) {
180                 // Init variables
181                 $VERBOSE_OUT = '';
182                 initSqls();
183
184                 // Secure id number
185                 $ext_id = bigintval($ext_id);
186
187                 // Is the id valid?
188                 if (isExtensionIdValid($ext_id)) {
189                         // Get extension name
190                         $ext_name = getExtensionName($ext_id);
191
192                         // Listing of SQLs enabled?
193                         if (isVerboseSqlEnabled()) {
194                                 // Load SQL commands in remove mode
195                                 if (loadExtension($ext_name, 'remove', '0.0.0', TRUE)) {
196                                         // Generate extra table with loaded SQL commands
197                                         $VERBOSE_OUT = addExtensionVerboseSqlTable();
198                                 } // END - if
199                         } // END - if
200
201                         // Prepare data for the row template
202                         $content = array(
203                                 'id'       => $ext_id,
204                                 'ext_name' => $ext_name,
205                                 'verbose'  => $VERBOSE_OUT
206                         );
207
208                         // Load row template and switch color
209                         $OUT .= loadTemplate('admin_delete_extensions_row', TRUE, $content);
210                 } else {
211                         // Prepare data for the row template
212                         $content = array(
213                                 'id'       => $ext_id
214                         );
215
216                         // Not valid!
217                         $OUT .= loadTemplate('admin_delete_extensions_row_404', TRUE, $content);
218                 }
219         } // END - foreach
220
221         // Load template
222         loadTemplate('admin_delete_extensions', FALSE, $OUT);
223         $do = 'delete';
224 } elseif ((isFormSent('do_delete')) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
225         // Remove extensions from DB (you have to delete all files manually!)
226         $cache_update = '0';
227         foreach (postRequestElement('sel') as $ext_id => $active) {
228                 // Secure id number
229                 $ext_id = bigintval($ext_id);
230
231                 // Is this extension selected and id valid?
232                 if (($active == 1) && (isExtensionIdValid($ext_id))) {
233                         // Run embeded SQL commands
234                         doExtensionSqls($ext_id, 'remove');
235                 } // END - if
236         } // END - foreach
237
238         // Redirect to prevent missing cache in js.php
239         redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;removed=' . countPostSelection());
240 } elseif ((isGetRequestElementSet('do')) && (!isDemoModeActive())) {
241         // Other things to do
242         $do = getRequestElement('do');
243 } elseif (isGetRequestElementSet('do')) {
244         // Demo mode active!
245         $do = 'demo';
246 } elseif (isGetRequestElementSet('registered')) {
247         // Extensions changed
248         displayMessage('{%message,ADMIN_EXTENSION_REGISTERED=' . getExtensionName(getRequestElement('registered')) . '%}');
249
250         // Show next link
251         if (isExtensionActive('task')) {
252                 loadTemplate('admin_next_link', FALSE, array(
253                         'url'   => 'modules.php?module=admin&amp;what=list_task',
254                         'title' => '{--ADMIN_EXTENSION_REGISTER_NEXT_LINK--}'
255                 ));
256         } else {
257                 loadTemplate('admin_next_link', FALSE, array(
258                         'url'   => 'modules.php?module=admin',
259                         'title' => '{--ADMIN_EXTENSION_REGISTER_NEXT_LINK--}'
260                 ));
261         }
262 } elseif (isGetRequestElementSet('changed')) {
263         // Extensions changed
264         displayMessage('{%message,ADMIN_EXTENSION_CHANGED=' . bigintval(getRequestElement('changed')) . '%}');
265 } elseif (isGetRequestElementSet('edited')) {
266         // Extensions changed
267         displayMessage('{%message,ADMIN_EXTENSION_EDITED=' . bigintval(getRequestElement('edited')) . '%}');
268 } elseif (isGetRequestElementSet('removed')) {
269         // Extensions changed
270         displayMessage('{%message,ADMIN_EXTENSIONS_REMOVED=' . bigintval(getRequestElement('removed')) . '%}');
271 }
272
273 // Shall we display active/inactive extensions?
274 $where = '';
275 if (isGetRequestElementSet('active')) {
276         $where = sprintf("WHERE `ext_active`='%s'", SQL_ESCAPE(getRequestElement('active')));
277 } // END - if
278
279 // Case selection
280 switch ($do) {
281         case 'overview': // List all registered extensions
282                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
283                         // Load extension data with CSS informations
284                         $result = SQL_QUERY('SELECT
285         `id` AS `ext_id`,
286         `ext_name`,
287         `ext_active`,
288         `ext_has_css` AS `ext_css`,
289         `ext_version`
290 FROM
291         `{?_MYSQL_PREFIX?}_extensions`
292 ' . $where . '
293 ORDER BY
294         `ext_name` ASC', __FILE__, __LINE__);
295                 } else {
296                         // Load extension data without CSS informations
297                         $result = SQL_QUERY('SELECT
298         `id` AS `ext_id`,
299         `ext_name`,
300         `ext_active`,
301         `ext_version`
302 FROM
303         `{?_MYSQL_PREFIX?}_extensions`
304 ' . $where . '
305 ORDER BY
306         `ext_name` ASC', __FILE__, __LINE__);
307                 }
308
309                 // Are there some entries?
310                 if (!SQL_HASZERONUMS($result)) {
311                         // Extensions are registered
312                         $OUT = '';
313                         while ($content = SQL_FETCHARRAY($result)) {
314                                 // Prepare CSS selection output
315                                 $cssSelection = '---';
316                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
317                                         $cssSelection = '{%pipe,translateYesNo=' . $content['ext_css'] . '%}';
318                                 } // END - if
319
320                                 // Prepare data for the row template
321                                 $content['ext_css'] = $cssSelection;
322
323                                 // Load row template and switch color
324                                 $OUT .= loadTemplate('admin_list_extensions_row', TRUE, $content);
325                         } // END - while
326
327                         // Free memory
328                         SQL_FREERESULT($result);
329
330                         // Load template
331                         loadTemplate('admin_list_extensions', FALSE, $OUT);
332                 } else {
333                         // No extensions are registered
334                         displayMessage('{--ADMIN_NO_EXTENSION_REGISTERED--}');
335                 }
336                 break;
337
338         case 'register': // Register new extension
339                 // Are there some tasks?
340                 $numTasks = countSumTotalData(getCurrentAdminId(), 'task_system', 'id', 'assigned_admin', TRUE, "AND `task_type`='EXTENSION'");
341
342                 // Is the id number valid and the task was found?
343                 if (($taskId > 0) && ($numTasks > 0)) {
344                         // id is valid so begin with registration, we first want to it's real name from task management (subject column)
345                         $result = SQL_QUERY_ESC("SELECT `subject` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s LIMIT 1",
346                                 array(bigintval($taskId)), __FILE__, __LINE__);
347                         list($subject) = SQL_FETCHROW($result);
348
349                         // Free result
350                         SQL_FREERESULT($result);
351
352                         // Disable cache update by default
353                         $cache_update = '0';
354                         if (!empty($subject)) {
355                                 // Extract extension's name from subject...
356                                 $ext_name = trim(substr($subject, 1, strpos($subject, ':') - 1));
357
358                                 // Test the extension for deprecation
359                                 loadExtension($ext_name, 'test');
360
361                                 // Is the extension deprecated?
362                                 if (!isExtensionDeprecated()) {
363                                         // ... so we can finally register and load it in registration mode
364                                         if (registerExtension($ext_name, $taskId)) {
365                                                 // Do we need to update cache file?
366                                                 // @TODO Rewrite this to a filter
367                                                 if (isExtensionActive('cache')) {
368                                                         // Remove cache file (will be auto-created again!)
369                                                         foreach (array('config', 'extension', 'filter', 'modules') as $cache) {
370                                                                 // Is the cache file there?
371                                                                 if ($GLOBALS['cache_instance']->loadCacheFile($cache)) {
372                                                                         // Remove cache file
373                                                                         $GLOBALS['cache_instance']->removeCacheFile();
374                                                                 } // END - if
375                                                         } // END - foreach
376                                                 } // END - if
377
378                                                 // Errors?
379                                                 if (!ifFatalErrorsDetected()) {
380                                                         // Extension was found and successfully registered
381                                                         redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;registered=' . getExtensionId($ext_name));
382                                                 } else {
383                                                         // Errors detected!
384                                                         displayMessage('{%message,ADMIN_EXTENSION_NOT_REGISTERED=' . $ext_name . '%}');
385                                                 }
386                                         } elseif (isExtensionInstalled($ext_name)) {
387                                                 // Notify the admin that we have a problem here...
388                                                 displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_ALREADY--}');
389                                         } else {
390                                                 // Notify the admin that we have a problem here...
391                                                 displayMessage('{%message,ADMIN_EXTENSION_REGISTRATION_FAILED_404=' . $ext_name . '%}');
392                                         }
393                                 } else {
394                                         // Motify the admin that we have a problem here...
395                                         displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_DEPRECATED--}');
396                                 }
397                         } else {
398                                 // Extension was not found in task management
399                                 displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_ID_404--}');
400                         }
401                 } elseif ($numTasks == '0') {
402                         // No longer assigned or old task
403                         displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_ASSIGED--}');
404                 } else {
405                         // id is invalid
406                         displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_INVALID--}');
407                 }
408                 break;
409
410         case 'demo':
411                 displayMessage('{--SETTINGS_NOT_SAVED--}');
412                 break;
413 } // END - switch
414
415 // [EOF]
416 ?>