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