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