Some more wrappers added, code cleanups:
[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'; $taskId = 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')) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
56         // De-/activate extensions
57         foreach (postRequestParameter('sel') as $taskId => $active) {
58                 // Shall we keep the extension always active?
59                 if ((isset($GLOBALS['cache_array']['always_active'][getExtensionName($taskId)])) && ($GLOBALS['cache_array']['always_active'][getExtensionName($taskId)] == 'Y') && ($active == 'Y')) {
60                         // Keep this extension active!
61                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_EXTENSION_ALWAYS_ACTIVE', getExtensionName($taskId)));
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($taskId), $active), __FILE__, __LINE__);
68
69                         // Run embeded SQL commands
70                         doExtensionSqls($taskId, 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'))) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
77         // Change settings like CSS file load
78         if (isPostRequestParameterSet('modify')) {
79                 // Change entries
80                 $cache_update = '0';
81                 foreach (postRequestParameter('sel') as $taskId => $sel) {
82                         // Secure id
83                         $taskId = bigintval($taskId);
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', $taskId);
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', $taskId), $active, $taskId), __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, $taskId), __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($taskId, 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                 $OUT = '';
120                 foreach (postRequestParameter('sel') as $taskId => $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($taskId)), __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($taskId)), __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', $taskId);
144                                 } // END - if
145
146                                 // Prepare data for the row template
147                                 $content = array(
148                                         'id'     => $taskId,
149                                         'name'   => getExtensionName($taskId),
150                                         'active' => addSelectionBox('yn', $active, 'active', $taskId),
151                                         'css'    => $cssSelection,
152                                 );
153
154                                 // Load row template and switch color
155                                 $OUT .= loadTemplate('admin_extensions_edit_row', true, $content);
156                         } // END - if
157                 } // END - foreach
158
159                 // Load template
160                 loadTemplate('admin_extensions_edit', false, $OUT);
161                 $do = 'edit';
162         }
163 } elseif ((isPostRequestParameterSet('delete')) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
164         // List extensions and when verbose is enabled SQL statements which will be executed
165         $OUT = '';
166         foreach (postRequestParameter('sel') as $taskId => $sel) {
167                 // Init variables
168                 $VERBOSE_OUT = '';
169                 initSqls();
170
171                 // Secure id number
172                 $taskId = bigintval($taskId);
173
174                 // Is the id valid?
175                 if (isExtensionIdValid($taskId)) {
176                         // Get extension name
177                         $ext_name = getExtensionName($taskId);
178
179                         if (isVerboseSqlEnabled()) {
180                                 // Load SQL commands in remove mode
181                                 if (loadExtension($ext_name, 'remove', '', true)) {
182                                         // Generate extra table with loaded SQL commands
183                                         $VERBOSE_OUT = addExtensionVerboseSqlTable();
184                                 } // END - if
185                         } // END - if
186
187                         // Prepare data for the row template
188                         $content = array(
189                                 'id'       => $taskId,
190                                 'ext_name' => $ext_name,
191                                 'ext_ver'  => getExtensionVersion($ext_name),
192                                 'verbose'  => $VERBOSE_OUT
193                         );
194
195                         // Load row template and switch color
196                         $OUT .= loadTemplate('admin_extensions_delete_row', true, $content);
197                 } else {
198                         // Prepare data for the row template
199                         $content = array(
200                                 'id'       => $taskId
201                         );
202
203                         // Not valid!
204                         $OUT .= loadTemplate('admin_extensions_delete_row_404', true, $content);
205                 }
206         } // END - foreach
207
208         // Load template
209         loadTemplate('admin_extensions_delete', false, $OUT);
210         $do = 'delete';
211 } elseif ((isFormSent('remove')) && (ifPostContainsSelections()) && (!isDemoModeActive())) {
212         // Remove extensions from DB (you have to delete all files manually!)
213         $cache_update = '0';
214         foreach (postRequestParameter('sel') as $taskId => $active) {
215                 // Secure id number
216                 $taskId = bigintval($taskId);
217
218                 // Is this extension selected and id valid?
219                 if (($active == 1) && (isExtensionIdValid($taskId))) {
220                         // Run embeded SQL commands
221                         doExtensionSqls($taskId, 'remove');
222                 } // END - if
223         } // END - foreach
224
225         // Redirect to prevent missing cache in js.php
226         redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;removed=' . countPostSelection());
227 } elseif ((isGetRequestParameterSet('do')) && (!isDemoModeActive())) {
228         // Other things to do
229         $do = SQL_ESCAPE(secureString(getRequestParameter('do')));
230 } elseif (isGetRequestParameterSet('do')) {
231         // Demo mode active!
232         $do = 'demo';
233 } elseif (isGetRequestParameterSet('registered')) {
234         // Extensions changed
235         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_EXTENSION_REGISTERED', getExtensionName(getRequestParameter('registered'))));
236
237         // Show next link
238         if (isExtensionActive('task')) {
239                 loadTemplate('admin_next_link', false, array(
240                         'url'   => 'modules.php?module=admin&amp;what=list_task',
241                         'title' => '{--ADMIN_EXTENSION_REGISTER_NEXT_LINK--}'
242                 ));
243         } else {
244                 loadTemplate('admin_next_link', false, array(
245                         'url'   => 'modules.php?module=admin',
246                         'title' => '{--ADMIN_EXTENSION_REGISTER_NEXT_LINK--}'
247                 ));
248         }
249 } elseif (isGetRequestParameterSet('changed')) {
250         // Extensions changed
251         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_EXTENSION_CHANGED', bigintval(getRequestParameter('changed'))));
252 } elseif (isGetRequestParameterSet('edited')) {
253         // Extensions changed
254         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_EXTENSION_EDITED', bigintval(getRequestParameter('edited'))));
255 } elseif (isGetRequestParameterSet('removed')) {
256         // Extensions changed
257         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_EXTENSIONS_REMOVED', bigintval(getRequestParameter('removed'))));
258 }
259
260 // Shall we display active/inactive extensions?
261 $where = '';
262 if (isGetRequestParameterSet('active')) {
263         $where = sprintf("WHERE `ext_active`='%s'", SQL_ESCAPE(secureString(getRequestParameter('active'))));
264 } // END - if
265
266 // Case selection
267 switch ($do) {
268         case 'overview': // List all registered extensions
269                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
270                         // Load extension data with CSS informations
271                         $result = SQL_QUERY("SELECT
272         `id` AS ext_id, `ext_name`, `ext_active`, `ext_has_css` AS ext_css, `ext_version`
273 FROM
274         `{?_MYSQL_PREFIX?}_extensions`
275 ".$where."
276 ORDER BY
277         `ext_name` ASC", __FILE__, __LINE__);
278                 } else {
279                         // Load extension data without CSS informations
280                         $result = SQL_QUERY("SELECT
281         `id` AS ext_id, `ext_name`, `ext_active`, `ext_version`
282 FROM
283         `{?_MYSQL_PREFIX?}_extensions`
284 ".$where."
285 ORDER BY
286         `ext_name` ASC", __FILE__, __LINE__);
287                 }
288
289                 // Are there some entries?
290                 if (!SQL_HASZERONUMS($result)) {
291                         // Extensions are registered
292                         $OUT = '';
293                         while ($content = SQL_FETCHARRAY($result)) {
294                                 // Prepare CSS selection output
295                                 $cssSelection = '---';
296                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) $cssSelection = translateYesNo($content['ext_css']);
297
298                                 // Prepare data for the row template
299                                 $content['ext_css'] = $cssSelection;
300
301                                 // Load row template and switch color
302                                 $OUT .= loadTemplate('admin_extensions_row', true, $content);
303                         } // END - while
304
305                         // Free memory
306                         SQL_FREERESULT($result);
307
308                         // Load template
309                         loadTemplate('admin_extensions', false, $OUT);
310                 } else {
311                         // No extensions are registered
312                         loadTemplate('admin_settings_saved', false, '{--ADMIN_NO_EXTENSION_REGISTERED--}');
313                 }
314                 break;
315
316         case 'register': // Register new extension
317                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `assigned_admin`=%s AND `task_type`='EXTENSION' LIMIT 1",
318                         array(getCurrentAdminId()), __FILE__, __LINE__);
319                 $task_found = SQL_NUMROWS($result);
320
321                 // Free result
322                 SQL_FREERESULT($result);
323
324                 // Is the id number valid and the task was found?
325                 if (($taskId > 0) && ($task_found == 1)) {
326                         // id is valid so begin with registration, we first want to it's real name from task management (subject column)
327                         $result = SQL_QUERY_ESC("SELECT `subject` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s LIMIT 1",
328                                 array(bigintval($taskId)), __FILE__, __LINE__);
329                         list($subject) = SQL_FETCHROW($result);
330
331                         // Free result
332                         SQL_FREERESULT($result);
333
334                         // Disable cache update by default
335                         $cache_update = '0';
336                         if (!empty($subject)) {
337                                 // Extract extension's name from subject...
338                                 $ext_name = trim(substr($subject, 1, strpos($subject, ':') - 1));
339
340                                 // Test the extension for deprecation
341                                 loadExtension($ext_name, 'test');
342
343                                 // Is the extension deprecated?
344                                 if (!isExtensionDeprecated()) {
345                                         // ... so we can finally register and load it in registration mode
346                                         if (registerExtension($ext_name, $taskId)) {
347                                                 // Errors?
348                                                 if (!ifFatalErrorsDetected()) {
349                                                         // Extension was found and successfully registered
350                                                         redirectToUrl('modules.php?module=admin&amp;what=extensions&amp;registered=' . getExtensionId($ext_name));
351                                                 } else {
352                                                         // Errors detected!
353                                                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_EXTENSION_NOT_REGISTERED', $ext_name));
354                                                 }
355
356                                                 // Do we need to update cache file?
357                                                 // @TODO Rewrite this to a filter
358                                                 if (isExtensionActive('cache')) {
359                                                         // Remove cache file (will be auto-created again!)
360                                                         if ($GLOBALS['cache_instance']->loadCacheFile('config'))    $GLOBALS['cache_instance']->removeCacheFile();
361                                                         if ($GLOBALS['cache_instance']->loadCacheFile('extension')) $GLOBALS['cache_instance']->removeCacheFile();
362                                                         if ($GLOBALS['cache_instance']->loadCacheFile('filter'))    $GLOBALS['cache_instance']->removeCacheFile();
363                                                         if ($GLOBALS['cache_instance']->loadCacheFile('modules'))   $GLOBALS['cache_instance']->removeCacheFile();
364                                                 } // END - if
365                                         } elseif (isExtensionInstalled($ext_name)) {
366                                                 // Notify the admin that we have a problem here...
367                                                 loadTemplate('admin_settings_saved', false, '{--ADMIN_EXTENSION_REGISTRATION_FAILED_ALREADY--}');
368                                         } else {
369                                                 // Notify the admin that we have a problem here...
370                                                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_EXTENSION_REGISTRATION_FAILED_404', $ext_name));
371                                         }
372                                 } else {
373                                         // Motify the admin that we have a problem here...
374                                         loadTemplate('admin_settings_saved', false, '{--ADMIN_EXTENSION_REGISTRATION_FAILED_DEPRECATED--}');
375                                 }
376                         } else {
377                                 // Extension was not found in task management
378                                 loadTemplate('admin_settings_saved', false, '{--ADMIN_EXTENSION_REGISTRATION_FAILED_ID_404--}');
379                         }
380                 } elseif ($task_found == '0') {
381                         // No longer assigned or old task
382                         loadTemplate('admin_settings_saved', false, '{--ADMIN_EXTENSION_REGISTRATION_FAILED_ASSIGED--}');
383                 } else {
384                         // id is invalid
385                         loadTemplate('admin_settings_saved', false, '{--ADMIN_EXTENSION_REGISTRATION_FAILED_INVALID--}');
386                 }
387                 break;
388
389         case 'demo':
390                 loadTemplate('admin_settings_saved', false, '{--SETTINGS_NOT_SAVED--}');
391                 break;
392 } // END - switch
393
394 // [EOF]
395 ?>