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