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