Redirect fixed... *sigh* admin.php is a redirector itself.
[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 ((ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) || (!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", basename(__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=%d 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                 $cacheInstance_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=%d 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=%d 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=%d 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=%d 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                 }
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                 $VERBOSE_OUT = ""; $SQLs = array();
158                 // Load extension's data
159                 $result = SQL_QUERY_ESC("SELECT ext_name, ext_version FROM "._MYSQL_PREFIX."_extensions WHERE id=%d LIMIT 1",
160                  array(bigintval($id)), __FILE__, __LINE__);
161                 list($ext_name, $ext_ver) = SQL_FETCHROW($result);
162
163                 // Free the result
164                 SQL_FREERESULT($result);
165
166                 if ($_CONFIG['verbose_sql']) {
167                         // Load SQL commands in remove mode
168                         $EXT_LOAD_MODE = "remove";
169                         $file = sprintf(PATH."inc/extensions/ext-%s.php", $ext_name);
170                         include($file);
171
172                         // Generate extra table with loaded SQL commands
173                         $VERBOSE_OUT = EXTENSION_VERBOSE_TABLE($SQLs);
174                 }
175
176                 // Prepare data for the row template
177                 $content = array(
178                         'sw'       => $SW,
179                         'id'       => $id,
180                         'ext_name' => $ext_name,
181                         'ext_ver'  => $ext_ver,
182                         'verbose'  => $VERBOSE_OUT
183                 );
184
185                 // Load row template and switch color
186                 $OUT .= LOAD_TEMPLATE("admin_extensions_delete_row", true, $content);
187                 $SW = 3 - $SW;
188         }
189         define('__EXTENSIONS_ROWS', $OUT);
190
191         // Load template
192         LOAD_TEMPLATE("admin_extensions_delete");
193         $do = "delete";
194 } elseif ((isset($_POST['remove'])) && ($SEL > 0) && (!IS_DEMO())) {
195         // Remove extensions from DB (you have to delete all files manually!)
196         $cacheInstance_UPDATE = "0";
197         foreach ($_POST['sel'] as $id=>$active) {
198                 // Secure ID number
199                 $id = bigintval($id);
200
201                 // Is this extension selected?
202                 if ($active == 1) {
203                         // Run embeded SQL commands
204                         EXTENSION_RUN_SQLS($id, "remove");
205
206                         // Delete this extension (remember to remove it from your server *before* you click on welcome!
207                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE id=%d LIMIT 1",
208                          array(bigintval($id)), __FILE__, __LINE__);
209                 }
210         }
211 } elseif (!empty($_GET['do']) && (!IS_DEMO())) {
212         // Other things to do
213         $do = SQL_ESCAPE(strip_tags($_GET['do']));
214 } elseif (!empty($_GET['do'])) {
215         // Demo mode active!
216         $do = "demo";
217 }
218
219 // Shall we display active/inactive extensions?
220 $where = "";
221 if (!empty($_GET['active'])) {
222         $where = sprintf("WHERE ext_active = '%s'", SQL_ESCAPE(strip_tags($_GET['active'])));
223 }
224
225 // Case selection
226 switch ($do) {
227 case "overview": // List all registered extensions
228         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
229                 // Load extension data with CSS informations
230                 $result = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_active, ext_has_css, ext_version
231                 FROM "._MYSQL_PREFIX."_extensions
232                 ".$where."
233                 ORDER BY ext_name", __FILE__, __LINE__);
234         } else {
235                 // Load extension data without CSS informations
236                 $result = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_active, id, ext_version
237                 FROM "._MYSQL_PREFIX."_extensions
238                 ".$where."
239                 ORDER BY ext_name", __FILE__, __LINE__);
240         }
241
242         // Are there some entries?
243         if (SQL_NUMROWS($result) > 0) {
244                 // Extensions are registered
245                 $SW = 2; $OUT = "";
246                 while (list($id, $name, $lang, $active, $css, $ver) = SQL_FETCHROW($result)) {
247                         $CSS = "---";
248                         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") $CSS = TRANSLATE_YESNO($css);
249
250                         // Prepare data for the row template
251                         $content = array(
252                                 'sw'      => $SW,
253                                 'id'      => $id,
254                                 'name'    => $name,
255                                 'active'  => TRANSLATE_YESNO($active),
256                                 'act_val' => $active,
257                                 'lang'    => $lang,
258                                 'css'     => $CSS,
259                                 'ver'     => $ver,
260                         );
261
262                         // Load row template and switch color
263                         $OUT .= LOAD_TEMPLATE("admin_extensions_row", true, $content);
264                         $SW = 3 - $SW;
265                 }
266
267                 // Free memory
268                 SQL_FREERESULT($result);
269
270                 // Remember rows in constant for the template
271                 define('__EXT_ROWS', $OUT);
272
273                 // Load template
274                 LOAD_TEMPLATE("admin_extensions");
275         } else {
276                 // No extensions are registered
277                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_NO_EXTENSION_REGISTERED."</FONT>");
278         }
279         // Link for checking for new or updated extensions
280         OUTPUT_HTML("<br /><A href=\"".URL."/modules.php?module=admin&amp;what=extensions&amp;do=search\">".ADMIN_SEARCH_NEW_EXTENSIONS."</A>");
281         break;
282
283 case "register": // Register new extension
284         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE assigned_admin='%s' AND task_type='EXTENSION' LIMIT 1",
285          array(bigintval(GET_ADMIN_ID(get_session('admin_login')))), __FILE__, __LINE__);
286         $task_found = SQL_NUMROWS($result);
287
288         // Free result
289         SQL_FREERESULT($result);
290
291         // Is the ID number valid and the task was found?
292         if (($id > 0) && ($task_found == 1)) {
293                 // ID is valid so begin with registration, we first want to it's real name from task management (subject column)
294                 $result = SQL_QUERY_ESC("SELECT subject FROM "._MYSQL_PREFIX."_task_system WHERE id=%d LIMIT 1",
295                  array(bigintval($id)), __FILE__, __LINE__);
296                 list($subj) = SQL_FETCHROW($result);
297                 SQL_FREERESULT($result);
298
299                 // Disable cache update by default
300                 $cacheInstance_UPDATE = "0";
301                 if (!empty($subj)) {
302                         // Extract extension's name from subject...
303                         $ext_name = trim(substr($subj, 1, strpos($subj, ":") - 1));
304
305                         // ... so we can finally register and load it in registration mode
306                         $status = EXTENSION_REGISTER($ext_name, $id);
307                         if ($status == true) {
308                                 // Is this sql_patches? Then we need to auto-logout!
309                                 if ($ext_name == "sql_patches") {
310                                         // Auto-logout here
311                                         LOAD_URL("modules.php?module=admin&logout=1&sql_patches=1");
312                                 }
313
314                                 // Extension was found and successfully registered
315                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_EXTENSION_REGISTERED);
316
317                                 // Do we need to update cache file?
318                                 if ((EXT_IS_ACTIVE("cache")) && ($cacheMode != "no")) {
319                                         // Remove cache file (will be auto-created again!)
320                                         if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
321                                 }
322                         } else {
323                                 // Motify the admin that we have a problem here...
324                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_404);
325                         }
326                 } else {
327                         // Extension was not found in task management
328                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ID_404);
329                 }
330         } elseif ($task_found == "0") {
331                 // No longer assigned or old task
332                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ASSIGED);
333         } else {
334                 // ID is invalid
335                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_INVALID);
336         }
337         break;
338
339 case "search": // Search for new extensions on our server
340         // Get response from our server
341         $response = MXCHANGE_OPEN("extensions.php");
342
343         // Are extensions found?
344         if (($response[sizeof($response) - 1] == "[EOF]") && ($response[0] != "[EOF]"))
345         {
346                 // Ok, mark found and create the array
347                 $EXT_SEARCH = array(
348                         'fname'  => array(), // File names
349                         'fsize'  => array(), // File size
350                         'fctime' => array(), // File creation timestamp
351                         'infos'  => array(), // File informations (maybe loaded!)
352                         'ver'    => array(), // Version number
353                         'cver'   => array(), // Current version number
354                 );
355
356                 // Get count of extensions for validation
357                 $count = trim($response[sizeof($response) - 2]);
358                 foreach ($response as $id=>$value)
359                 {
360                         $value = str_replace("\n", "", $value); $ver = "";
361                         // Leave loop when data is invalid or EOF?
362                         if ((substr($value, 0, 4) == "ext-") && (substr($value, -4) == ".zip"))
363                         {
364                                 $name = substr($value, 4, -4);
365                                 $file = sprintf(PATH."inc/extensions/ext-%s.php", $name);
366                                 $ver = trim(substr($response[$id + 3], 4));
367
368                                 // Load current extension's version
369                                 $cver = GET_EXT_VERSION($name);
370                                 if (empty($cver)) $cver = "-.-";
371
372                                 // Is the extension already installed or not?
373                                 if (!file_exists($file) || ($ver != $cver))
374                                 {
375                                         // No, it isn't. So let's add this one!
376                                         $EXT_SEARCH['fname'][]  = $name;
377                                         $EXT_SEARCH['fsize'][]  = $response[$id + 1];
378                                         $EXT_SEARCH['fctime'][] = $response[$id + 2];
379                                         $EXT_SEARCH['ver'][]    = $ver;
380                                         $EXT_SEARCH['cver'][]   = $cver;
381
382                                         $LANG_DUMMY = explode("[nl]", $response[$id + 4]);
383                                         $LANG = array();
384                                         $INFO = ADMIN_EXT_NO_INFO_FOUND;
385
386                                         // Trim every data line
387                                         foreach ($LANG_DUMMY as $k=>$v)
388                                         {
389                                                 $v = trim($v);
390                                                 if (substr($v, 3) == "") $v = "---";
391                                                 $LANG_DUMMY[$k] = $v;
392                                                 if ($v == "xx:xx") break;
393                                                 $LANG[] = $v;
394                                         }
395
396                                         // If language is found stop searching on matching line
397                                         foreach($LANG as $search)
398                                         {
399                                                 if (substr($search, 0, 3) == (GET_LANGUAGE().":")) { $INFO = substr($search, 3); break; }
400                                         }
401
402                                         // Add informations to array
403                                         $EXT_SEARCH['infos'][] = $INFO;
404                                 }
405                         }
406                 }
407
408                 // Ok, extensions are on our server but maybe you have already installed them?
409                 if (sizeof($EXT_SEARCH['fname']) > 0)
410                 {
411                         // Sort array (I missed ver and cver here)
412                         array_pk_sort($EXT_SEARCH, array("cver", "fname"), 0, 1);
413
414                         // Extensions where found which are not downloaded and installed
415                         $SW = 2; $OUT = ""; $TSIZE = 0;
416                         foreach ($EXT_SEARCH['fname'] as $id=>$name)
417                         {
418                                 // Generate download link
419                                 $LINK = SERVER_URL."/extensions/ext-".$name.".zip";
420
421                                 // Prepare data for the row template
422                                 $content = array(
423                                         'sw'      => $SW,
424                                         'id'      => ($id + 1),
425                                         'name'    => $name,
426                                         'new_ver' => $EXT_SEARCH['ver'][$id],
427                                         'old_ver' => $EXT_SEARCH['cver'][$id],
428                                         'link'    => $LINK,
429                                         'last'    => MAKE_DATETIME($EXT_SEARCH['fctime'][$id], "2"),
430                                         'size'    => TRANSLATE_COMMA(round($EXT_SEARCH['fsize'][$id] / 1.024) / 1000),
431                                         'info'    => $EXT_SEARCH['infos'][$id],
432                                 );
433
434                                 // Load row template add current size to total size and switch color
435                                 $OUT .= LOAD_TEMPLATE("admin_list_extensions_row", true, $content);
436                                 $TSIZE += $EXT_SEARCH['fsize'][$id];
437                                 $SW = 3 - $SW;
438                         }
439                         define('__EXTENSIONS_ROWS', $OUT);
440                         define('__TKBYTES_VALUE', TRANSLATE_COMMA(round($TSIZE / 1.024) / 1000));
441                         define('__TEXT_VALUE', sizeof($EXT_SEARCH['fname']));
442
443                         // Load template
444                         LOAD_TEMPLATE("admin_list_extensions");
445                 }
446                  else
447                 {
448                         // All extensions are downloaded and installed
449                         LOAD_TEMPLATE("admin_extensions_installed", false, $count);
450                 }
451         }
452          else
453         {
454                 // No extensions where found
455                 LOAD_TEMPLATE("admin_extensions_404");
456         }
457         break;
458
459 case "demo":
460         LOAD_TEMPLATE("admin_settings_saved", false, SETTINGS_NOT_SAVED);
461         break;
462 }
463 //
464 ?>