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