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