9c4c1c119b9290fd62e765e841822b21ea483d4b
[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"; $id = $_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 $id => $active) {
57                 // Shall we keep the extension always active?
58                 if ((isset($cacheArray['active_extensions'][GET_EXT_NAME($id)])) && ($cacheArray['active_extensions'][GET_EXT_NAME($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                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='".$ACT."' WHERE id=%s AND ext_active='%s' LIMIT 1",
65                          array(bigintval($id), $active), __FILE__, __LINE__);
66
67                         // Run embeded SQL commands
68                         EXTENSION_RUN_SQLS($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 $id => $sel) {
77                         // Secure ID
78                         $id = bigintval($id);
79
80                         // Change this extension?
81                         if ($sel == 1) {
82                                 // Update extension's record
83                                 $active = $_POST['active'][$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                                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_has_css='%s', ext_active='%s' WHERE id=%s LIMIT 1",
87                                          array($_POST['css'][$id], $active, $id), __FILE__, __LINE__);
88                                 } else {
89                                         // When extension is older than v0.0.6 there is no column for the CSS information
90                                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='%s' WHERE id=%s LIMIT 1",
91                                          array($active, $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($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 $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($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($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", $id);
132
133                                 // Prepare data for the row template
134                                 $content = array(
135                                         'sw'     => $SW,
136                                         'id'     => $id,
137                                         'name'   => $name,
138                                         'active' => ADD_SELECTION("yn", $active, "active", $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 $id => $sel) {
157                 // Init variables
158                 $VERBOSE_OUT = ""; $SQLs = array();
159
160                 // Secure id number
161                 $id = bigintval($id);
162
163                 // Get extension name
164                 $ext_name = GET_EXT_NAME($id);
165                 $ext_ver = GET_EXT_VERSION($ext_name);
166
167                 if ($_CONFIG['verbose_sql']) {
168                         // Load SQL commands in remove mode
169                         $EXT_LOAD_MODE = "remove";
170                         $EXT_ALWAYS_ACTIVE = "N";
171                         $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
172                         if (FILE_READABLE($file)) {
173                                 // Load file
174                                 include($file);
175
176                                 // Generate extra table with loaded SQL commands
177                                 $VERBOSE_OUT = EXTENSION_VERBOSE_TABLE($SQLs);
178                         }
179                 } // END - if
180
181                 // Prepare data for the row template
182                 $content = array(
183                         'sw'       => $SW,
184                         'id'       => $id,
185                         'ext_name' => $ext_name,
186                         'ext_ver'  => $ext_ver,
187                         'verbose'  => $VERBOSE_OUT
188                 );
189
190                 // Load row template and switch color
191                 $OUT .= LOAD_TEMPLATE("admin_extensions_delete_row", true, $content);
192                 $SW = 3 - $SW;
193         } // END - foreach
194         define('__EXTENSIONS_ROWS', $OUT);
195
196         // Load template
197         LOAD_TEMPLATE("admin_extensions_delete");
198         $do = "delete";
199 } elseif ((isset($_POST['remove'])) && ($SEL > 0) && (!IS_DEMO())) {
200         // Remove extensions from DB (you have to delete all files manually!)
201         $cache_update = 0;
202         foreach ($_POST['sel'] as $id => $active) {
203                 // Secure ID number
204                 $id = bigintval($id);
205
206                 // Is this extension selected?
207                 if ($active == 1) {
208                         // Run embeded SQL commands
209                         EXTENSION_RUN_SQLS($id, "remove");
210                 } // END - if
211         } // END - foreach
212 } elseif (!empty($_GET['do']) && (!IS_DEMO())) {
213         // Other things to do
214         $do = SQL_ESCAPE(strip_tags($_GET['do']));
215 } elseif (!empty($_GET['do'])) {
216         // Demo mode active!
217         $do = "demo";
218 }
219
220 // Shall we display active/inactive extensions?
221 $where = "";
222 if (!empty($_GET['active'])) {
223         $where = sprintf("WHERE ext_active = '%s'", SQL_ESCAPE(strip_tags($_GET['active'])));
224 } // END - if
225
226 // Case selection
227 switch ($do) {
228 case "overview": // List all registered extensions
229         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
230                 // Load extension data with CSS informations
231                 $result = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_active, ext_has_css, ext_version
232                 FROM "._MYSQL_PREFIX."_extensions
233                 ".$where."
234                 ORDER BY ext_name", __FILE__, __LINE__);
235         } else {
236                 // Load extension data without CSS informations
237                 $result = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_active, id, ext_version
238                 FROM "._MYSQL_PREFIX."_extensions
239                 ".$where."
240                 ORDER BY ext_name", __FILE__, __LINE__);
241         }
242
243         // Are there some entries?
244         if (SQL_NUMROWS($result) > 0) {
245                 // Extensions are registered
246                 $SW = 2; $OUT = "";
247                 while (list($id, $name, $lang, $active, $css, $ver) = SQL_FETCHROW($result)) {
248                         $CSS = "---";
249                         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") $CSS = TRANSLATE_YESNO($css);
250
251                         // Prepare data for the row template
252                         $content = array(
253                                 'sw'      => $SW,
254                                 'id'      => $id,
255                                 'name'    => $name,
256                                 'active'  => TRANSLATE_YESNO($active),
257                                 'act_val' => $active,
258                                 'lang'    => $lang,
259                                 'css'     => $CSS,
260                                 'ver'     => $ver,
261                         );
262
263                         // Load row template and switch color
264                         $OUT .= LOAD_TEMPLATE("admin_extensions_row", true, $content);
265                         $SW = 3 - $SW;
266                 }
267
268                 // Free memory
269                 SQL_FREERESULT($result);
270
271                 // Remember rows in constant for the template
272                 define('__EXT_ROWS', $OUT);
273
274                 // Load template
275                 LOAD_TEMPLATE("admin_extensions");
276         } else {
277                 // No extensions are registered
278                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_NO_EXTENSION_REGISTERED."</FONT>");
279         }
280         break;
281
282 case "register": // Register new extension
283         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE assigned_admin='%s' AND task_type='EXTENSION' LIMIT 1",
284          array(bigintval(GET_ADMIN_ID(get_session('admin_login')))), __FILE__, __LINE__);
285         $task_found = SQL_NUMROWS($result);
286
287         // Free result
288         SQL_FREERESULT($result);
289
290         // Is the ID number valid and the task was found?
291         if (($id > 0) && ($task_found == 1)) {
292                 // ID is valid so begin with registration, we first want to it's real name from task management (subject column)
293                 $result = SQL_QUERY_ESC("SELECT subject FROM "._MYSQL_PREFIX."_task_system WHERE id=%s LIMIT 1",
294                  array(bigintval($id)), __FILE__, __LINE__);
295                 list($subj) = SQL_FETCHROW($result);
296                 SQL_FREERESULT($result);
297
298                 // Disable cache update by default
299                 $cache_update = 0;
300                 if (!empty($subj)) {
301                         // Extract extension's name from subject...
302                         $ext_name = trim(substr($subj, 1, strpos($subj, ":") - 1));
303
304                         // ... so we can finally register and load it in registration mode
305                         $status = EXTENSION_REGISTER($ext_name, $id);
306                         if ($status == true) {
307                                 // Extension was found and successfully registered
308                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_EXTENSION_REGISTERED);
309
310                                 // Do we need to update cache file?
311                                 if ((EXT_IS_ACTIVE("cache")) && ($cacheMode != "no")) {
312                                         // Remove cache file (will be auto-created again!)
313                                         if ($cacheInstance->cache_file("config")) $cacheInstance->cache_destroy();
314                                         if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
315                                         if ($cacheInstance->cache_file("mod_reg")) $cacheInstance->cache_destroy();
316                                 } // END - if
317                         } elseif (GET_EXT_VERSION($ext_name) != "") {
318                                 // Motify the admin that we have a problem here...
319                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ALREADY);
320                         } else {
321                                 // Motify the admin that we have a problem here...
322                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_404);
323                         }
324                 } else {
325                         // Extension was not found in task management
326                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ID_404);
327                 }
328         } elseif ($task_found == "0") {
329                 // No longer assigned or old task
330                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ASSIGED);
331         } else {
332                 // ID is invalid
333                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_INVALID);
334         }
335         break;
336
337 case "demo":
338         LOAD_TEMPLATE("admin_settings_saved", false, SETTINGS_NOT_SAVED);
339         break;
340 }
341 //
342 ?>