Some functions rewritten to hungarian notation, handling of array rewritten
[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                 OUTPUT_HTML("<P align=\"center\">");
107                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_EXT_CHANGED);
108                 OUTPUT_HTML("</P>");
109         } else {
110                 // Edit selected entries
111                 $SW = "2"; $OUT = "";
112                 foreach ($_POST['sel'] as $ext_id => $sel) {
113                         // Edit this extension?
114                         if (($sel == "Y") || ($sel == "N")) {
115                                 // Load required data
116                                 if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
117                                         $result = SQL_QUERY_ESC("SELECT ext_name, ext_has_css, ext_active FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
118                                          array(bigintval($ext_id)), __FILE__, __LINE__);
119                                         list($name, $css, $active) = SQL_FETCHROW($result);
120                                         SQL_FREERESULT($result);
121                                 } else {
122                                         $result = SQL_QUERY_ESC("SELECT ext_name, ext_active FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
123                                          array(bigintval($ext_id)), __FILE__, __LINE__);
124                                         list($name, $active) = SQL_FETCHROW($result);
125                                         SQL_FREERESULT($result);
126                                         $css = "X";
127                                 }
128
129                                 // Output row
130                                 $CSS = "---";
131                                 if (GET_EXT_VERSION("sql_patches") >= "0.0.6") $CSS = ADD_SELECTION("yn", $css, "css", $ext_id);
132
133                                 // Prepare data for the row template
134                                 $content = array(
135                                         'sw'     => $SW,
136                                         'id'     => $ext_id,
137                                         'name'   => $name,
138                                         'active' => ADD_SELECTION("yn", $active, "active", $ext_id),
139                                         'css'    => $CSS,
140                                 );
141
142                                 // Load row template and switch color
143                                 $OUT .= LOAD_TEMPLATE("admin_extensions_edit_row", true, $content);
144                                 $SW = 3 - $SW;
145                         }
146                 } // END - foreach
147                 define('__EXTENSIONS_ROWS', $OUT);
148
149                 // Load template
150                 LOAD_TEMPLATE("admin_extensions_edit");
151                 $do = "edit";
152         }
153 } elseif ((isset($_POST['delete'])) && ($SEL > 0) && (!IS_DEMO())) {
154         // List extensions and when verbose is enabled SQL statements which will be executed
155         $SW = 2; $OUT = "";
156         foreach ($_POST['sel'] as $ext_id => $sel) {
157                 // Init variables
158                 $VERBOSE_OUT = ""; $SQLs = array();
159
160                 // Secure id number
161                 $ext_id = bigintval($ext_id);
162
163                 // Get extension name
164                 $ext_name = GET_EXT_NAME($ext_id);
165
166                 if (getConfig('verbose_sql') == "Y") {
167                         // Load SQL commands in remove mode
168                         if (LOAD_EXTENSION($ext_name, "remove", "", true, $SQLs)) {
169                                 // Generate extra table with loaded SQL commands
170                                 $VERBOSE_OUT = EXTENSION_VERBOSE_TABLE($SQLs);
171                         } // END - if
172                 } // END - if
173
174                 // Prepare data for the row template
175                 $content = array(
176                         'sw'       => $SW,
177                         'id'       => $ext_id,
178                         'ext_name' => $ext_name,
179                         'ext_ver'  => GET_EXT_VERSION($ext_name),
180                         'verbose'  => $VERBOSE_OUT
181                 );
182
183                 // Load row template and switch color
184                 $OUT .= LOAD_TEMPLATE("admin_extensions_delete_row", true, $content);
185                 $SW = 3 - $SW;
186         } // END - foreach
187         define('__EXTENSIONS_ROWS', $OUT);
188
189         // Load template
190         LOAD_TEMPLATE("admin_extensions_delete");
191         $do = "delete";
192 } elseif ((isset($_POST['remove'])) && ($SEL > 0) && (!IS_DEMO())) {
193         // Remove extensions from DB (you have to delete all files manually!)
194         $cache_update = 0;
195         foreach ($_POST['sel'] as $ext_id => $active) {
196                 // Secure ID number
197                 $ext_id = bigintval($ext_id);
198
199                 // Is this extension selected?
200                 if ($active == 1) {
201                         // Run any assigned removal filters
202                         RUN_FILTER('extension_remove', GET_EXT_NAME($ext_id));
203
204                         // Run embeded SQL commands
205                         EXTENSION_RUN_SQLS($ext_id, "remove");
206                 } // END - if
207         } // END - foreach
208 } elseif (!empty($_GET['do']) && (!IS_DEMO())) {
209         // Other things to do
210         $do = SQL_ESCAPE(strip_tags($_GET['do']));
211 } elseif (!empty($_GET['do'])) {
212         // Demo mode active!
213         $do = "demo";
214 }
215
216 // Shall we display active/inactive extensions?
217 $where = "";
218 if (!empty($_GET['active'])) {
219         $where = sprintf("WHERE ext_active = '%s'", SQL_ESCAPE(strip_tags($_GET['active'])));
220 } // END - if
221
222 // Case selection
223 switch ($do) {
224 case "overview": // List all registered extensions
225         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
226                 // Load extension data with CSS informations
227                 $result = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_active, ext_has_css AS ext_css, ext_version
228 FROM "._MYSQL_PREFIX."_extensions
229 ".$where."
230 ORDER BY ext_name", __FILE__, __LINE__);
231         } else {
232                 // Load extension data without CSS informations
233                 $result = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_active, id, ext_version
234 FROM "._MYSQL_PREFIX."_extensions
235 ".$where."
236 ORDER BY ext_name", __FILE__, __LINE__);
237         }
238
239         // Are there some entries?
240         if (SQL_NUMROWS($result) > 0) {
241                 // Extensions are registered
242                 $SW = 2; $OUT = "";
243                 while ($content = SQL_FETCHARRAY($result)) {
244                         $CSS = "---";
245                         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") $CSS = 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'     => $CSS,
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, ADMIN_NO_EXTENSION_REGISTERED."</FONT>");
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                         global $EXT_DEPRECATED;
301                         LOAD_EXTENSION($ext_name, "test");
302
303                         // Is the extension deprecated?
304                         if ($EXT_DEPRECATED == "N") {
305                                 // ... so we can finally register and load it in registration mode
306                                 $status = EXTENSION_REGISTER($ext_name, $ext_id);
307                                 if ($status == true) {
308                                         // Extension was found and successfully registered
309                                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_EXTENSION_REGISTERED);
310
311                                         // Do we need to update cache file?
312                                         if ((EXT_IS_ACTIVE("cache")) && ($cacheMode != "no")) {
313                                                 // Remove cache file (will be auto-created again!)
314                                                 if ($cacheInstance->loadCacheFile("config")) $cacheInstance->destroyCacheFile();
315                                                 if ($cacheInstance->loadCacheFile("extensions", true)) $cacheInstance->destroyCacheFile();
316                                                 if ($cacheInstance->loadCacheFile("mod_reg")) $cacheInstance->destroyCacheFile();
317                                         } // END - if
318                                 } elseif (GET_EXT_VERSION($ext_name) != "") {
319                                         // Notify the admin that we have a problem here...
320                                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ALREADY);
321                                 } else {
322                                         // Notify the admin that we have a problem here...
323                                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_404);
324                                 }
325                         } else {
326                                 // Motify the admin that we have a problem here...
327                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_DEPRECATED);
328                         }
329                 } else {
330                         // Extension was not found in task management
331                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ID_404);
332                 }
333         } elseif ($task_found == "0") {
334                 // No longer assigned or old task
335                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ASSIGED);
336         } else {
337                 // ID is invalid
338                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_INVALID);
339         }
340         break;
341
342 case "demo":
343         LOAD_TEMPLATE("admin_settings_saved", false, SETTINGS_NOT_SAVED);
344         break;
345 }
346 //
347 ?>