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