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