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