More rewrites, and output-mode fixed (we should documentate this)
[mailer.git] / inc / modules / admin / what-extensions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    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  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if ((!defined('__SECURITY')) || (!isAdmin())) {
41         die();
42 } // END - if
43
44 // Add description as navigation point
45 addMenuDescription('admin', __FILE__);
46
47 // Normally we want the overview of all registered extensions
48 $do = 'overview';
49 $SEL = 0;
50
51 // Are some extensions submitted?
52 if (isPostRequestElementSet('sel')) {
53         // Then count all
54         $SEL = countPostSelection();
55 } // END - if
56
57 if (isGetRequestElementSet('reg_ext')) {
58         // We are about to register a new extension
59         $do = 'register'; $ext_id = bigintval(getRequestElement('reg_ext'));
60         // The ID comes from task management and it is - of course - *not* the extension's name!
61 } elseif ((isPostRequestElementSet('change')) && ($SEL > 0) && (!isDemoModeActive())) {
62         // De-/activate extensions
63         foreach (postRequestElement('sel') as $ext_id => $active) {
64                 // Shall we keep the extension always active?
65                 if ((isset($GLOBALS['cache_array']['active_extensions'][getExtensionName($ext_id)])) && ($GLOBALS['cache_array']['active_extensions'][getExtensionName($ext_id)] == 'Y') && ($active != 'Y')) {
66                         // Keep this extension active!
67                 } else {
68                         // De/activate extension
69                         $ACT = 'N'; getExtensionMode('deactivate');
70                         if ($active != 'Y') { $ACT = 'Y'; setExtensionMode('activate'); }
71                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='".$ACT."' WHERE `id`=%s AND `ext_active`='%s' LIMIT 1",
72                         array(bigintval($ext_id), $active), __FILE__, __LINE__);
73
74                         // Run embeded SQL commands
75                         doExtensionSqls($ext_id, getExtensionMode());
76                 }
77         }
78 } elseif (((isPostRequestElementSet('edit')) || (isPostRequestElementSet(('modify')))) && ($SEL > 0) && (!isDemoModeActive())) {
79         // Change settings like CSS file load
80         if (isPostRequestElementSet(('modify'))) {
81                 // Change entries
82                 $cache_update = 0;
83                 foreach (postRequestElement('sel') as $ext_id => $sel) {
84                         // Secure ID
85                         $ext_id = bigintval($ext_id);
86
87                         // Change this extension?
88                         if ($sel == 1) {
89                                 // Load mode is modify
90                                 getExtensionMode('modify');
91
92                                 // Get entry for 'active'
93                                 $active = postRequestElement('active', $ext_id);
94
95                                 // Update extension's record
96                                 if (getExtensionVersion('sql_patches') >= '0.0.6') {
97                                         // Update also CSS column when extensions sql_patches is newer or exact v0.0.6
98                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_has_css`='%s', `ext_active`='%s' WHERE `id`=%s LIMIT 1",
99                                                 array(postRequestElement('css', $ext_id), $active, $ext_id), __FILE__, __LINE__);
100                                 } else {
101                                         // When extension is older than v0.0.6 there is no column for the CSS information
102                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='%s' WHERE `id`=%s LIMIT 1",
103                                                 array($active, $ext_id), __FILE__, __LINE__);
104                                 }
105
106                                 // Run SQLs on activation / deactivation
107                                 switch ($active) {
108                                         case 'Y': setExtensionMode('activate');   break;
109                                         case 'N': setExtensionMode('deactivate'); break;
110                                 } // END - if
111
112                                 // Run embeded SQL commands
113                                 doExtensionSqls($ext_id, getExtensionMode());
114                         }
115                 }
116
117                 // Extensions changed
118                 loadTemplate('admin_settings_saved', false, getMessage('ADMIN_EXT_CHANGED'));
119         } else {
120                 // Edit selected entries
121                 $SW = 2; $OUT = '';
122                 foreach (postRequestElement('sel') as $ext_id => $sel) {
123                         // Edit this extension?
124                         if (($sel == 'Y') || ($sel != 'Y')) {
125                                 // Default is no CSS for non-updated mailers
126                                 $css = 'N';
127
128                                 // Load required data
129                                 if (getExtensionVersion('sql_patches') >= '0.0.6') {
130                                         $result = SQL_QUERY_ESC("SELECT ext_has_css, ext_active FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
131                                                 array(bigintval($ext_id)), __FILE__, __LINE__);
132                                         list($css, $active) = SQL_FETCHROW($result);
133                                         SQL_FREERESULT($result);
134                                 } else {
135                                         $result = SQL_QUERY_ESC("SELECT ext_active FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
136                                                 array(bigintval($ext_id)), __FILE__, __LINE__);
137                                         list($active) = SQL_FETCHROW($result);
138                                         SQL_FREERESULT($result);
139                                         $css = 'X';
140                                 }
141
142                                 // Output row
143                                 $cssSelection = '---';
144                                 if (getExtensionVersion('sql_patches') >= '0.0.6') $cssSelection = addSelectionBox('yn', $css, 'css', $ext_id);
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 ((isPostRequestElementSet('delete')) && ($SEL > 0) && (!isDemoModeActive())) {
166         // List extensions and when verbose is enabled SQL statements which will be executed
167         $OUT = ''; $SW = 2;
168         foreach (postRequestElement('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 ((isPostRequestElementSet('remove')) && ($SEL > 0) && (!isDemoModeActive())) {
217         // Remove extensions from DB (you have to delete all files manually!)
218         $cache_update = 0;
219         foreach (postRequestElement('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         // Extensions removed
231         loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_EXTENSIONS_REMOVED'), $SEL));
232 } elseif ((isGetRequestElementSet('do')) && (!isDemoModeActive())) {
233         // Other things to do
234         $do = SQL_ESCAPE(secureString(getRequestElement('do')));
235 } elseif (isGetRequestElementSet('do')) {
236         // Demo mode active!
237         $do = 'demo';
238 }
239
240 // Shall we display active/inactive extensions?
241 $where = '';
242 if (isGetRequestElementSet(('active'))) {
243         $where = sprintf("WHERE `ext_active`='%s'", SQL_ESCAPE(secureString(getRequestElement('active'))));
244 } // END - if
245
246 // Case selection
247 switch ($do) {
248         case 'overview': // List all registered extensions
249                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
250                         // Load extension data with CSS informations
251                         $result = SQL_QUERY("SELECT `id` AS ext_id, `ext_name`, `ext_active`, `ext_has_css` AS ext_css, `ext_version`
252 FROM `{?_MYSQL_PREFIX?}_extensions`
253 ".$where."
254 ORDER BY `ext_name` ASC", __FILE__, __LINE__);
255                 } else {
256                         // Load extension data without CSS informations
257                         $result = SQL_QUERY("SELECT `id` AS ext_id, `ext_name`, `ext_active`, `id`, `ext_version`
258 FROM `{?_MYSQL_PREFIX?}_extensions`
259 ".$where."
260 ORDER BY `ext_name` ASC", __FILE__, __LINE__);
261                 }
262
263                 // Are there some entries?
264                 if (SQL_NUMROWS($result) > 0) {
265                         // Extensions are registered
266                         $OUT = ''; $SW = 2;
267                         while ($content = SQL_FETCHARRAY($result)) {
268                                 // Prepare CSS selection output
269                                 $cssSelection = '---';
270                                 if (getExtensionVersion('sql_patches') >= '0.0.6') $cssSelection = translateYesNo($content['ext_css']);
271
272                                 // Prepare data for the row template
273                                 $content = array(
274                                         'sw'      => $SW,
275                                         'id'      => $content['ext_id'],
276                                         'name'    => $content['ext_name'],
277                                         'active'  => translateYesNo($content['ext_active']),
278                                         'act_val' => $content['ext_active'],
279                                         'css'     => $cssSelection,
280                                         'ver'     => $content['ext_version'],
281                                 );
282
283                                 // Load row template and switch color
284                                 $OUT .= loadTemplate('admin_extensions_row', true, $content);
285                                 $SW = 3 - $SW;
286                         }
287
288                         // Free memory
289                         SQL_FREERESULT($result);
290
291                         // Load template
292                         loadTemplate('admin_extensions', false, $OUT);
293                 } else {
294                         // No extensions are registered
295                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NO_EXTENSION_REGISTERED'));
296                 }
297                 break;
298
299         case 'register': // Register new extension
300                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `assigned_admin`=%s AND `task_type`='EXTENSION' LIMIT 1",
301                         array(getCurrentAdminId()), __FILE__, __LINE__);
302                 $task_found = SQL_NUMROWS($result);
303
304                 // Free result
305                 SQL_FREERESULT($result);
306
307                 // Is the ID number valid and the task was found?
308                 if (($ext_id > 0) && ($task_found == 1)) {
309                         // ID is valid so begin with registration, we first want to it's real name from task management (subject column)
310                         $result = SQL_QUERY_ESC("SELECT `subject` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s LIMIT 1",
311                                 array(bigintval($ext_id)), __FILE__, __LINE__);
312                         list($subj) = SQL_FETCHROW($result);
313                         SQL_FREERESULT($result);
314
315                         // Disable cache update by default
316                         $cache_update = 0;
317                         if (!empty($subj)) {
318                                 // Extract extension's name from subject...
319                                 $ext_name = trim(substr($subj, 1, strpos($subj, ':') - 1));
320
321                                 // Test the extension for deprecation
322                                 loadExtension($ext_name, 'test');
323
324                                 // Is the extension deprecated?
325                                 if (!isExtensionDeprecated()) {
326                                         // ... so we can finally register and load it in registration mode
327                                         if (registerExtension($ext_name, $ext_id)) {
328                                                 // Errors?
329                                                 if (getTotalFatalErrors() == 0) {
330                                                         // Extension was found and successfully registered
331                                                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_EXTENSION_REGISTERED'), $ext_name));
332
333                                                         // Show next link
334                                                         if (isExtensionActive('task')) {
335                                                                 loadTemplate('admin_next_link', false, array(
336                                                                         'url'   => 'modules.php?module=admin&amp;what=list_task',
337                                                                         'title' => getMessage('ADMIN_EXT_REGISTER_NEXT_LINK')
338                                                                 ));
339                                                         } else {
340                                                                 loadTemplate('admin_next_link', false, array(
341                                                                         'url'   => 'modules.php?module=admin',
342                                                                         'title' => getMessage('ADMIN_EXT_REGISTER_NEXT_LINK')
343                                                                 ));
344                                                         }
345                                                 } else {
346                                                         // Errors detected!
347                                                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_EXTENSION_NOT_REGISTERED'), $ext_name));
348                                                 }
349
350                                                 // Do we need to update cache file?
351                                                 // @TODO Rewrite this to a filter
352                                                 if (isExtensionActive('cache')) {
353                                                         // Remove cache file (will be auto-created again!)
354                                                         if ($GLOBALS['cache_instance']->loadCacheFile('config'))    $GLOBALS['cache_instance']->removeCacheFile();
355                                                         if ($GLOBALS['cache_instance']->loadCacheFile('extension')) $GLOBALS['cache_instance']->removeCacheFile();
356                                                         if ($GLOBALS['cache_instance']->loadCacheFile('modules'))   $GLOBALS['cache_instance']->removeCacheFile();
357                                                 } // END - if
358                                         } elseif (getExtensionVersion($ext_name) != '') {
359                                                 // Notify the admin that we have a problem here...
360                                                 loadTemplate('admin_settings_saved', false, getMessage('ADMIN_REG_FAILED_ALREADY'));
361                                         } else {
362                                                 // Notify the admin that we have a problem here...
363                                                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_REG_FAILED_404'), $ext_name));
364                                         }
365                                 } else {
366                                         // Motify the admin that we have a problem here...
367                                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_REG_FAILED_DEPRECATED'));
368                                 }
369                         } else {
370                                 // Extension was not found in task management
371                                 loadTemplate('admin_settings_saved', false, getMessage('ADMIN_REG_FAILED_ID_404'));
372                         }
373                 } elseif ($task_found == 0) {
374                         // No longer assigned or old task
375                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_REG_FAILED_ASSIGED'));
376                 } else {
377                         // ID is invalid
378                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_REG_FAILED_INVALID'));
379                 }
380                 break;
381
382         case 'demo':
383                 loadTemplate('admin_settings_saved', false, getMessage('SETTINGS_NOT_SAVED'));
384                 break;
385 }
386
387 // [EOF]
388 ?>