7a2415fdc9f33cc3ecfa60b5885b690191c6a3c1
[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 // Normally we want the overview of all registered extensions
47 $do = 'overview';
48
49 if (isGetRequestElementSet('reg_ext')) {
50         // We are about to register a new extension
51         $do = 'register';
52         $taskId = determineExtensionTaskId(getRequestElement('reg_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($active, bigintval($ext_id), $ext_active), __FILE__, __LINE__);
77
78                         // Run embeded SQL commands
79                         doExtensionSqls($ext_id, getExtensionMode());
80                 }
81         } // END - foreach
82
83         // Redirect to prevent missing cache in js.php
84         redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;changed=' . countPostSelection());
85 } elseif (((isFormSent('edit')) || (isPostRequestElementSet('modify'))) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
86         // Change settings like CSS file load
87         if (isPostRequestElementSet('modify')) {
88                 // Change entries
89                 $cache_update = '0';
90                 foreach (postRequestElement('sel') as $ext_id => $sel) {
91                         // Secure id
92                         $ext_id = bigintval($ext_id);
93
94                         // Change this extension?
95                         if ($sel == 1) {
96                                 // Load mode is modify
97                                 setExtensionMode('modify');
98
99                                 // Get entry for 'active'
100                                 $active = postRequestElement('active', $ext_id);
101
102                                 // Update extension's record
103                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
104                                         // Update also CSS column when extensions sql_patches is newer or exact v0.0.6
105                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_has_css`='%s',`ext_active`='%s' WHERE `id`=%s LIMIT 1",
106                                                 array(postRequestElement('css', $ext_id), $active, $ext_id), __FILE__, __LINE__);
107                                 } else {
108                                         // When extension is older than v0.0.6 there is no column for the CSS information
109                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='%s' WHERE `id`=%s LIMIT 1",
110                                                 array($active, $ext_id), __FILE__, __LINE__);
111                                 }
112
113                                 // Run SQLs on activation / deactivation
114                                 switch ($active) {
115                                         case 'Y': setExtensionMode('activate');   break;
116                                         case 'N': setExtensionMode('deactivate'); break;
117                                 } // END - switch
118
119                                 // Run embeded SQL commands
120                                 doExtensionSqls($ext_id, getExtensionMode());
121                         } // END - if
122                 } // END - foreach
123
124                 // Redirect to prevent missing cache in js.php
125                 redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;edited=' . countPostSelection());
126         } else {
127                 // Edit selected entries
128                 $OUT = '';
129                 foreach (postRequestElement('sel') as $ext_id => $sel) {
130                         // Edit this extension?
131                         if (($sel == 'Y') || ($sel != 'Y')) {
132                                 // Default is no CSS for non-updated mailers
133                                 $css = 'N';
134
135                                 // Load required data
136                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
137                                         $result = SQL_QUERY_ESC("SELECT `ext_has_css`,`ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
138                                                 array(bigintval($ext_id)), __FILE__, __LINE__);
139                                         list($css, $active) = SQL_FETCHROW($result);
140                                         SQL_FREERESULT($result);
141                                 } else {
142                                         $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
143                                                 array(bigintval($ext_id)), __FILE__, __LINE__);
144                                         list($active) = SQL_FETCHROW($result);
145                                         SQL_FREERESULT($result);
146                                         $css = 'X';
147                                 }
148
149                                 // Prepare CSS column
150                                 $cssSelection = '---';
151                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
152                                         $cssSelection = addSelectionBox('yn', $css, 'css', $ext_id);
153                                 } // END - if
154
155                                 // Prepare data for the row template
156                                 $content = array(
157                                         'id'     => $ext_id,
158                                         'name'   => getExtensionName($ext_id),
159                                         'active' => addSelectionBox('yn', $active, 'active', $ext_id),
160                                         'css'    => $cssSelection,
161                                 );
162
163                                 // Load row template and switch color
164                                 $OUT .= loadTemplate('admin_extensions_edit_row', true, $content);
165                         } // END - if
166                 } // END - foreach
167
168                 // Load template
169                 loadTemplate('admin_extensions_edit', false, $OUT);
170                 $do = 'edit';
171         }
172 } elseif ((isPostRequestElementSet('delete')) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
173         // List extensions and when verbose is enabled SQL statements which will be executed
174         $OUT = '';
175         foreach (postRequestElement('sel') as $ext_id => $sel) {
176                 // Init variables
177                 $VERBOSE_OUT = '';
178                 initSqls();
179
180                 // Secure id number
181                 $ext_id = bigintval($ext_id);
182
183                 // Is the id valid?
184                 if (isExtensionIdValid($ext_id)) {
185                         // Get extension name
186                         $ext_name = getExtensionName($ext_id);
187
188                         // Listing of SQLs enabled?
189                         if (isVerboseSqlEnabled()) {
190                                 // Load SQL commands in remove mode
191                                 if (loadExtension($ext_name, 'remove', '0.0.0', true)) {
192                                         // Generate extra table with loaded SQL commands
193                                         $VERBOSE_OUT = addExtensionVerboseSqlTable();
194                                 } // END - if
195                         } // END - if
196
197                         // Prepare data for the row template
198                         $content = array(
199                                 'id'       => $ext_id,
200                                 'ext_name' => $ext_name,
201                                 'verbose'  => $VERBOSE_OUT
202                         );
203
204                         // Load row template and switch color
205                         $OUT .= loadTemplate('admin_extensions_delete_row', true, $content);
206                 } else {
207                         // Prepare data for the row template
208                         $content = array(
209                                 'id'       => $ext_id
210                         );
211
212                         // Not valid!
213                         $OUT .= loadTemplate('admin_extensions_delete_row_404', true, $content);
214                 }
215         } // END - foreach
216
217         // Load template
218         loadTemplate('admin_extensions_delete', false, $OUT);
219         $do = 'delete';
220 } elseif ((isFormSent('do_delete')) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
221         // Remove extensions from DB (you have to delete all files manually!)
222         $cache_update = '0';
223         foreach (postRequestElement('sel') as $ext_id => $active) {
224                 // Secure id number
225                 $ext_id = bigintval($ext_id);
226
227                 // Is this extension selected and id valid?
228                 if (($active == 1) && (isExtensionIdValid($ext_id))) {
229                         // Run embeded SQL commands
230                         doExtensionSqls($ext_id, 'remove');
231                 } // END - if
232         } // END - foreach
233
234         // Redirect to prevent missing cache in js.php
235         redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;removed=' . countPostSelection());
236 } elseif ((isGetRequestElementSet('do')) && (!isDemoModeActive())) {
237         // Other things to do
238         $do = SQL_ESCAPE(secureString(getRequestElement('do')));
239 } elseif (isGetRequestElementSet('do')) {
240         // Demo mode active!
241         $do = 'demo';
242 } elseif (isGetRequestElementSet('registered')) {
243         // Extensions changed
244         displayMessage('{%message,ADMIN_EXTENSION_REGISTERED=' . getExtensionName(getRequestElement('registered')) . '%}');
245
246         // Show next link
247         if (isExtensionActive('task')) {
248                 loadTemplate('admin_next_link', false, array(
249                         'url'   => 'modules.php?module=admin&amp;what=list_task',
250                         'title' => '{--ADMIN_EXTENSION_REGISTER_NEXT_LINK--}'
251                 ));
252         } else {
253                 loadTemplate('admin_next_link', false, array(
254                         'url'   => 'modules.php?module=admin',
255                         'title' => '{--ADMIN_EXTENSION_REGISTER_NEXT_LINK--}'
256                 ));
257         }
258 } elseif (isGetRequestElementSet('changed')) {
259         // Extensions changed
260         displayMessage('{%message,ADMIN_EXTENSION_CHANGED=' . bigintval(getRequestElement('changed')) . '%}');
261 } elseif (isGetRequestElementSet('edited')) {
262         // Extensions changed
263         displayMessage('{%message,ADMIN_EXTENSION_EDITED=' . bigintval(getRequestElement('edited')) . '%}');
264 } elseif (isGetRequestElementSet('removed')) {
265         // Extensions changed
266         displayMessage('{%message,ADMIN_EXTENSIONS_REMOVED=' . bigintval(getRequestElement('removed')) . '%}');
267 }
268
269 // Shall we display active/inactive extensions?
270 $where = '';
271 if (isGetRequestElementSet('active')) {
272         $where = sprintf("WHERE `ext_active`='%s'", SQL_ESCAPE(secureString(getRequestElement('active'))));
273 } // END - if
274
275 // Case selection
276 switch ($do) {
277         case 'overview': // List all registered extensions
278                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
279                         // Load extension data with CSS informations
280                         $result = SQL_QUERY("SELECT
281         `id` AS `ext_id`,
282         `ext_name`,
283         `ext_active`,
284         `ext_has_css` AS `ext_css`,
285         `ext_version`
286 FROM
287         `{?_MYSQL_PREFIX?}_extensions`
288 ".$where."
289 ORDER BY
290         `ext_name` ASC", __FILE__, __LINE__);
291                 } else {
292                         // Load extension data without CSS informations
293                         $result = SQL_QUERY("SELECT
294         `id` AS `ext_id`,
295         `ext_name`,
296         `ext_active`,
297         `ext_version`
298 FROM
299         `{?_MYSQL_PREFIX?}_extensions`
300 ".$where."
301 ORDER BY
302         `ext_name` ASC", __FILE__, __LINE__);
303                 }
304
305                 // Are there some entries?
306                 if (!SQL_HASZERONUMS($result)) {
307                         // Extensions are registered
308                         $OUT = '';
309                         while ($content = SQL_FETCHARRAY($result)) {
310                                 // Prepare CSS selection output
311                                 $cssSelection = '---';
312                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
313                                         $cssSelection = '{%pipe,translateYesNo=' . $content['ext_css'] . '%}';
314                                 } // END - if
315
316                                 // Prepare data for the row template
317                                 $content['ext_css'] = $cssSelection;
318
319                                 // Load row template and switch color
320                                 $OUT .= loadTemplate('admin_extensions_row', true, $content);
321                         } // END - while
322
323                         // Free memory
324                         SQL_FREERESULT($result);
325
326                         // Load template
327                         loadTemplate('admin_extensions', false, $OUT);
328                 } else {
329                         // No extensions are registered
330                         displayMessage('{--ADMIN_NO_EXTENSION_REGISTERED--}');
331                 }
332                 break;
333
334         case 'register': // Register new extension
335                 // Do we have some tasks?
336                 $numTasks = countSumTotalData(getCurrentAdminId(), 'task_system', 'id', 'assigned_admin', true, "AND `task_type`='EXTENSION'");
337
338                 // Is the id number valid and the task was found?
339                 if (($taskId > 0) && ($numTasks > 0)) {
340                         // id is valid so begin with registration, we first want to it's real name from task management (subject column)
341                         $result = SQL_QUERY_ESC("SELECT `subject` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s LIMIT 1",
342                                 array(bigintval($taskId)), __FILE__, __LINE__);
343                         list($subject) = SQL_FETCHROW($result);
344
345                         // Free result
346                         SQL_FREERESULT($result);
347
348                         // Disable cache update by default
349                         $cache_update = '0';
350                         if (!empty($subject)) {
351                                 // Extract extension's name from subject...
352                                 $ext_name = trim(substr($subject, 1, strpos($subject, ':') - 1));
353
354                                 // Test the extension for deprecation
355                                 loadExtension($ext_name, 'test');
356
357                                 // Is the extension deprecated?
358                                 if (!isExtensionDeprecated()) {
359                                         // ... so we can finally register and load it in registration mode
360                                         if (registerExtension($ext_name, $taskId)) {
361                                                 // Do we need to update cache file?
362                                                 // @TODO Rewrite this to a filter
363                                                 if (isExtensionActive('cache')) {
364                                                         // Remove cache file (will be auto-created again!)
365                                                         foreach (array('config', 'extension', 'filter', 'modules') as $cache) {
366                                                                 // Is the cache file there?
367                                                                 if ($GLOBALS['cache_instance']->loadCacheFile($cache)) {
368                                                                         // Remove cache file
369                                                                         $GLOBALS['cache_instance']->removeCacheFile();
370                                                                 } // END - if
371                                                         } // END - foreach
372                                                 } // END - if
373
374                                                 // Errors?
375                                                 if (!ifFatalErrorsDetected()) {
376                                                         // Extension was found and successfully registered
377                                                         redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;registered=' . getExtensionId($ext_name));
378                                                 } else {
379                                                         // Errors detected!
380                                                         displayMessage('{%message,ADMIN_EXTENSION_NOT_REGISTERED=' . $ext_name . '%}');
381                                                 }
382                                         } elseif (isExtensionInstalled($ext_name)) {
383                                                 // Notify the admin that we have a problem here...
384                                                 displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_ALREADY--}');
385                                         } else {
386                                                 // Notify the admin that we have a problem here...
387                                                 displayMessage('{%message,ADMIN_EXTENSION_REGISTRATION_FAILED_404=' . $ext_name . '%}');
388                                         }
389                                 } else {
390                                         // Motify the admin that we have a problem here...
391                                         displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_DEPRECATED--}');
392                                 }
393                         } else {
394                                 // Extension was not found in task management
395                                 displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_ID_404--}');
396                         }
397                 } elseif ($numTasks == '0') {
398                         // No longer assigned or old task
399                         displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_ASSIGED--}');
400                 } else {
401                         // id is invalid
402                         displayMessage('{--ADMIN_EXTENSION_REGISTRATION_FAILED_INVALID--}');
403                 }
404                 break;
405
406         case 'demo':
407                 displayMessage('{--SETTINGS_NOT_SAVED--}');
408                 break;
409 } // END - switch
410
411 // [EOF]
412 ?>