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