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