List active/inactive extensions
[mailer.git] / 0.2.1 / inc / modules / admin / what-extensions.php
1 <?php\r
2 /************************************************************************\r
3  * MXChange v0.2.1                                    Start: 03/22/2004 *\r
4  * ================                             Last change: 12/13/2004 *\r
5  *                                                                      *\r
6  * -------------------------------------------------------------------- *\r
7  * File              : what-extentions.php                              *\r
8  * -------------------------------------------------------------------- *\r
9  * Short description : Extension management                             *\r
10  * -------------------------------------------------------------------- *\r
11  * Kurzbeschreibung  : Erweiterungen-Management                         *\r
12  * -------------------------------------------------------------------- *\r
13  *                                                                      *\r
14  * -------------------------------------------------------------------- *\r
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *\r
16  * For more information visit: http://www.mxchange.org                  *\r
17  *                                                                      *\r
18  * This program is free software; you can redistribute it and/or modify *\r
19  * it under the terms of the GNU General Public License as published by *\r
20  * the Free Software Foundation; either version 2 of the License, or    *\r
21  * (at your option) any later version.                                  *\r
22  *                                                                      *\r
23  * This program is distributed in the hope that it will be useful,      *\r
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *\r
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *\r
26  * GNU General Public License for more details.                         *\r
27  *                                                                      *\r
28  * You should have received a copy of the GNU General Public License    *\r
29  * along with this program; if not, write to the Free Software          *\r
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *\r
31  * MA  02110-1301  USA                                                  *\r
32  ************************************************************************/\r
33 \r
34 // Some security stuff...\r
35 if ((ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) || (!IS_ADMIN())) {\r
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";\r
37         require($INC);\r
38 }
39 \r
40 // Add description as navigation point\r
41 ADD_DESCR("admin", basename(__FILE__));\r
42 \r
43 global $CACHE, $KEEP_ACTIVE, $CACHE_FILE;\r
44 \r
45 // Normally we want the overview of all registered extensions\r
46 $do = "overview";\r
47 $SEL = "0";\r
48 if (!empty($_POST['sel'])) $SEL = SELECTION_COUNT($_POST['sel']);\r
49 \r
50 if (!empty($_GET['reg_ext'])) {\r
51         // We are about to register a new extension\r
52         $do = "register"; $id = $_GET['reg_ext'];\r
53         // The ID comes from task management and it is - of course - *not* the extension's name!\r
54 } elseif ((isset($_POST['change'])) && ($SEL > 0) && (!IS_DEMO())) {\r
55         // De-/activate extensions\r
56         foreach ($_POST['sel'] as $id=>$active) {\r
57                 // Shall we keep the extension always active?\r
58                 if ((isset($KEEP_ACTIVE[GET_EXT_NAME($id)])) && ($KEEP_ACTIVE[GET_EXT_NAME($id)] == "Y") && ($active == "N")) {\r
59                         // Keep this extension active!\r
60                 } else {\r
61                         // De/activate extension\r
62                         $ACT = "N"; $EXT_LOAD_MODE = "deactivate";\r
63                         if ($active == "N") { $ACT = "Y"; $EXT_LOAD_MODE = "activate"; }\r
64                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='".$ACT."' WHERE id=%d AND ext_active='%s' LIMIT 1",\r
65                          array(bigintval($id), $active), __FILE__, __LINE__);\r
66 \r
67                         // Run embeded SQL commands\r
68                         EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE);\r
69                 }\r
70         }\r
71 } elseif (((isset($_POST['edit'])) || (isset($_POST['modify']))) && ($SEL > 0) && (!IS_DEMO())) {\r
72         // Change settings like CSS file load\r
73         if (isset($_POST['modify'])) {\r
74                 // Change entries\r
75                 $CACHE_UPDATE = "0";\r
76                 foreach ($_POST['sel'] as $id=>$sel) {\r
77                         // Secure ID\r
78                         $id = bigintval($id);\r
79 \r
80                         // Change this extension?\r
81                         if ($sel == 1) {\r
82                                 // Update extension's record\r
83                                 $active = $_POST['active'][$id];\r
84                                 if (GET_EXT_VERSION("sql_patches") >= "0.0.6")  {\r
85                                         // Update also CSS column when extensions sql_patches is newer or exact v0.0.6\r
86                                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_has_css='%s', ext_active='%s' WHERE id=%d LIMIT 1",\r
87                                          array($_POST['css'][$id], $active, $id), __FILE__, __LINE__);\r
88                                 } else {\r
89                                         // When extension is older than v0.0.6 there is no column for the CSS information\r
90                                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='%s' WHERE id=%d LIMIT 1",\r
91                                          array($active, $id), __FILE__, __LINE__);\r
92                                 }
93 \r
94                                 // Run SQLs on activation / deactivation\r
95                                 switch ($active) {\r
96                                         case "Y": $EXT_LOAD_MODE = "activate";   break;\r
97                                         case "N": $EXT_LOAD_MODE = "deactivate"; break;\r
98                                 }\r
99 \r
100                                 // Run embeded SQL commands\r
101                                 EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE);\r
102                         }\r
103                 }\r
104 \r
105                 // Extensions changed\r
106                 OUTPUT_HTML ("<P align=\"center\">");\r
107                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_EXT_CHANGED);\r
108                 OUTPUT_HTML ("</P>");\r
109         } else {\r
110                 // Edit selected entries\r
111                 $SW = "2"; $OUT = "";\r
112                 foreach ($_POST['sel'] as $id=>$sel) {\r
113                         // Edit this extension?\r
114                         if (($sel == "Y") || ($sel == "N")) {\r
115                                 // Load required data\r
116                                 if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {\r
117                                         $result = SQL_QUERY_ESC("SELECT ext_name, ext_has_css, ext_active FROM "._MYSQL_PREFIX."_extensions WHERE id=%d LIMIT 1",\r
118                                          array(bigintval($id)), __FILE__, __LINE__);\r
119                                         list($name, $css, $active) = SQL_FETCHROW($result);\r
120                                         SQL_FREERESULT($result);\r
121                                 } else {\r
122                                         $result = SQL_QUERY_ESC("SELECT ext_name, ext_active FROM "._MYSQL_PREFIX."_extensions WHERE id=%d LIMIT 1",\r
123                                          array(bigintval($id)), __FILE__, __LINE__);\r
124                                         list($name, $active) = SQL_FETCHROW($result);\r
125                                         SQL_FREERESULT($result);\r
126                                         $css = "X";\r
127                                 }\r
128 \r
129                                 // Output row\r
130                                 $CSS = "---";\r
131                                 if (GET_EXT_VERSION("sql_patches") >= "0.0.6") $CSS = ADD_SELECTION("yn", $css, "css", $id);\r
132 \r
133                                 // Prepare data for the row template\r
134                                 $content = array(\r
135                                         'sw'     => $SW,\r
136                                         'id'     => $id,\r
137                                         'name'   => $name,\r
138                                         'active' => ADD_SELECTION("yn", $active, "active", $id),\r
139                                         'css'    => $CSS,\r
140                                 );\r
141 \r
142                                 // Load row template and switch color\r
143                                 $OUT .= LOAD_TEMPLATE("admin_extensions_edit_row", true, $content);\r
144                                 $SW = 3 - $SW;\r
145                         }\r
146                 }\r
147                 define('__EXTENSIONS_ROWS', $OUT);\r
148 \r
149                 // Load template\r
150                 LOAD_TEMPLATE("admin_extensions_edit");\r
151                 $do = "edit";\r
152         }\r
153 } elseif ((isset($_POST['delete'])) && ($SEL > 0) && (!IS_DEMO())) {\r
154         // List extensions and when verbose is enabled SQL statements which will be executed\r
155         $SW = 2; $OUT = "";\r
156         foreach ($_POST['sel'] as $id=>$sel) {\r
157                 $VERBOSE_OUT = ""; $SQLs = array();\r
158                 // Load extension's data\r
159                 $result = SQL_QUERY_ESC("SELECT ext_name, ext_version FROM "._MYSQL_PREFIX."_extensions WHERE id=%d LIMIT 1",\r
160                  array(bigintval($id)), __FILE__, __LINE__);\r
161                 list($ext_name, $ext_ver) = SQL_FETCHROW($result);
162
163                 // Free the result\r
164                 SQL_FREERESULT($result);\r
165 \r
166                 if ($CONFIG['verbose_sql']) {\r
167                         // Load SQL commands in remove mode\r
168                         $EXT_LOAD_MODE = "remove";\r
169                         $file = sprintf(PATH."inc/extensions/ext-%s.php", $ext_name);\r
170                         include($file);\r
171 \r
172                         // Generate extra table with loaded SQL commands\r
173                         $VERBOSE_OUT = EXTENSION_VERBOSE_TABLE($SQLs);\r
174                 }\r
175 \r
176                 // Prepare data for the row template\r
177                 $content = array(\r
178                         'sw'       => $SW,\r
179                         'id'       => $id,\r
180                         'ext_name' => $ext_name,\r
181                         'ext_ver'  => $ext_ver,\r
182                         'verbose'  => $VERBOSE_OUT\r
183                 );\r
184 \r
185                 // Load row template and switch color\r
186                 $OUT .= LOAD_TEMPLATE("admin_extensions_delete_row", true, $content);\r
187                 $SW = 3 - $SW;\r
188         }\r
189         define('__EXTENSIONS_ROWS', $OUT);\r
190 \r
191         // Load template\r
192         LOAD_TEMPLATE("admin_extensions_delete");\r
193         $do = "delete";\r
194 } elseif ((isset($_POST['remove'])) && ($SEL > 0) && (!IS_DEMO())) {\r
195         // Remove extensions from DB (you have to delete all files manually!)\r
196         $CACHE_UPDATE = "0";\r
197         foreach ($_POST['sel'] as $id=>$active) {
198                 // Secure ID number
199                 $id = bigintval($id);
200
201                 // Is this extension selected?\r
202                 if ($active == 1) {\r
203                         // Run embeded SQL commands\r
204                         EXTENSION_RUN_SQLS($id, "remove");\r
205 \r
206                         // Delete this extension (remember to remove it from your server *before* you click on welcome!\r
207                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE id=%d LIMIT 1",\r
208                          array(bigintval($id)), __FILE__, __LINE__);\r
209                 }\r
210         }\r
211 } elseif (!empty($_GET['do']) && (!IS_DEMO())) {\r
212         // Other things to do\r
213         $do = SQL_ESCAPE(strip_tags($_GET['do']));\r
214 } elseif (!empty($_GET['do'])) {\r
215         // Demo mode active!\r
216         $do = "demo";\r
217 }\r
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\r
226 switch ($do) {\r
227 case "overview": // List all registered extensions\r
228         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {\r
229                 // Load extension data with CSS informations\r
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__);\r
234         } else {\r
235                 // Load extension data without CSS informations\r
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__);\r
240         }
241
242         // Are there some entries?\r
243         if (SQL_NUMROWS($result) > 0) {\r
244                 // Extensions are registered\r
245                 $SW = 2; $OUT = "";\r
246                 while (list($id, $name, $lang, $active, $css, $ver) = SQL_FETCHROW($result)) {\r
247                         $CSS = "---";\r
248                         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") $CSS = TRANSLATE_YESNO($css);\r
249 \r
250                         // Prepare data for the row template\r
251                         $content = array(\r
252                                 'sw'      => $SW,\r
253                                 'id'      => $id,\r
254                                 'name'    => $name,\r
255                                 'active'  => TRANSLATE_YESNO($active),\r
256                                 'act_val' => $active,\r
257                                 'lang'    => $lang,\r
258                                 'css'     => $CSS,\r
259                                 'ver'     => $ver,\r
260                         );\r
261 \r
262                         // Load row template and switch color\r
263                         $OUT .= LOAD_TEMPLATE("admin_extensions_row", true, $content);\r
264                         $SW = 3 - $SW;\r
265                 }\r
266 \r
267                 // Free memory\r
268                 SQL_FREERESULT($result);\r
269 \r
270                 // Remember rows in constant for the template\r
271                 define('__EXT_ROWS', $OUT);\r
272 \r
273                 // Load template\r
274                 LOAD_TEMPLATE("admin_extensions");\r
275         } else {\r
276                 // No extensions are registered\r
277                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_NO_EXTENSION_REGISTERED."</FONT>");\r
278         }\r
279         // Link for checking for new or updated extensions\r
280         OUTPUT_HTML ("<BR><A href=\"".URL."/modules.php?module=admin&amp;what=extensions&amp;do=search\">".ADMIN_SEARCH_NEW_EXTENSIONS."</A>");\r
281         break;\r
282 \r
283 case "register": // Register new extension\r
284         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE assigned_admin='%s' AND task_type='EXTENSION' LIMIT 1",\r
285          array(bigintval(GET_ADMIN_ID($_COOKIE['admin_login']))), __FILE__, __LINE__);\r
286         $task_found = SQL_NUMROWS($result);
287
288         // Free result\r
289         SQL_FREERESULT($result);\r
290
291         // Is the ID number valid and the task was found?
292         if (($id > 0) && ($task_found == 1)) {\r
293                 // ID is valid so begin with registration, we first want to it's real name from task management (subject column)\r
294                 $result = SQL_QUERY_ESC("SELECT subject FROM "._MYSQL_PREFIX."_task_system WHERE id=%d LIMIT 1",\r
295                  array(bigintval($id)), __FILE__, __LINE__);\r
296                 list($subj) = SQL_FETCHROW($result);\r
297                 SQL_FREERESULT($result);\r
298 \r
299                 // Disable cache update by default\r
300                 $CACHE_UPDATE = "0";\r
301                 if (!empty($subj)) {\r
302                         // Extract extension's name from subject...\r
303                         $ext_name = trim(substr($subj, 1, strpos($subj, ":") - 1));\r
304 \r
305                         // ... so we can finally register and load it in registration mode\r
306                         $status = EXTENSION_REGISTER($ext_name, $id);\r
307                         if ($status) {\r
308                                 // Extension was found and successfully registered\r
309                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_EXTENSION_REGISTERED);\r
310 \r
311                                 // Do we need to update cache file?\r
312                                 if ((EXT_IS_ACTIVE("cache")) && ($CACHE_FILE != "no")) {\r
313                                         // Remove cache file (will be auto-created again!)\r
314                                         if ($CACHE->cache_file("extensions", true)) $CACHE->cache_destroy();\r
315                                 }\r
316                         } else {\r
317                                 // Motify the admin that we have a problem here...\r
318                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_404);\r
319                         }\r
320                 } else {\r
321                         // Extension was not found in task management\r
322                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ID_404);\r
323                 }\r
324         } elseif ($task_found == "0") {\r
325                 // No longer assigned or old task\r
326                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_ASSIGED);\r
327         } else {\r
328                 // ID is invalid\r
329                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_REG_FAILED_INVALID);\r
330         }\r
331         break;\r
332 \r
333 case "search": // Search for new extensions on our server\r
334         // Get response from our server\r
335         $response = MXCHANGE_OPEN("extensions.php");\r
336 \r
337         // Are extensions found?\r
338         if (($response[sizeof($response) - 1] == "[EOF]") && ($response[0] != "[EOF]"))\r
339         {\r
340                 // Ok, mark found and create the array\r
341                 $EXT_SEARCH = array(\r
342                         'fname'  => array(), // File names\r
343                         'fsize'  => array(), // File size\r
344                         'fctime' => array(), // File creation timestamp\r
345                         'infos'  => array(), // File informations (maybe loaded!)\r
346                         'ver'    => array(), // Version number\r
347                         'cver'   => array(), // Current version number\r
348                 );\r
349 \r
350                 // Get count of extensions for validation\r
351                 $count = trim($response[sizeof($response) - 2]);\r
352                 foreach ($response as $id=>$value)\r
353                 {\r
354                         $value = str_replace("\n", "", $value); $ver = "";\r
355                         // Leave loop when data is invalid or EOF?\r
356                         if ((substr($value, 0, 4) == "ext-") && (substr($value, -4) == ".zip"))\r
357                         {\r
358                                 $name = substr($value, 4, -4);\r
359                                 $file = sprintf(PATH."inc/extensions/ext-%s.php", $name);\r
360                                 $ver = trim(substr($response[$id + 3], 4));\r
361 \r
362                                 // Load current extension's version\r
363                                 $cver = GET_EXT_VERSION($name);\r
364                                 if (empty($cver)) $cver = "-.-";\r
365 \r
366                                 // Is the extension already installed or not?\r
367                                 if (!file_exists($file) || ($ver != $cver))\r
368                                 {\r
369                                         // No, it isn't. So let's add this one!\r
370                                         $EXT_SEARCH['fname'][]  = $name;\r
371                                         $EXT_SEARCH['fsize'][]  = $response[$id + 1];\r
372                                         $EXT_SEARCH['fctime'][] = $response[$id + 2];\r
373                                         $EXT_SEARCH['ver'][]    = $ver;\r
374                                         $EXT_SEARCH['cver'][]   = $cver;\r
375 \r
376                                         $LANG_DUMMY = explode("[nl]", $response[$id + 4]);\r
377                                         $LANG = array();\r
378                                         $INFO = ADMIN_EXT_NO_INFO_FOUND;\r
379 \r
380                                         // Trim every data line\r
381                                         foreach ($LANG_DUMMY as $k=>$v)\r
382                                         {\r
383                                                 $v = trim($v);\r
384                                                 if (substr($v, 3) == "") $v = "---";\r
385                                                 $LANG_DUMMY[$k] = $v;\r
386                                                 if ($v == "xx:xx") break;\r
387                                                 $LANG[] = $v;\r
388                                         }\r
389 \r
390                                         // If language is found stop searching on matching line\r
391                                         foreach($LANG as $search)\r
392                                         {\r
393                                                 if (substr($search, 0, 3) == (GET_LANGUAGE().":")) { $INFO = substr($search, 3); break; }\r
394                                         }\r
395 \r
396                                         // Add informations to array\r
397                                         $EXT_SEARCH['infos'][] = $INFO;\r
398                                 }\r
399                         }\r
400                 }\r
401 \r
402                 // Ok, extensions are on our server but maybe you have already installed them?\r
403                 if (sizeof($EXT_SEARCH['fname']) > 0)\r
404                 {\r
405                         // Sort array (I missed ver and cver here)\r
406                         array_pk_sort($EXT_SEARCH, array("cver", "fname"), 0, 1);\r
407 \r
408                         // Extensions where found which are not downloaded and installed\r
409                         $SW = 2; $OUT = ""; $TSIZE = 0;\r
410                         foreach ($EXT_SEARCH['fname'] as $id=>$name)\r
411                         {\r
412                                 // Generate download link\r
413                                 $LINK = SERVER_URL."/extensions/ext-".$name.".zip";\r
414 \r
415                                 // Prepare data for the row template\r
416                                 $content = array(\r
417                                         'sw'      => $SW,\r
418                                         'id'      => ($id + 1),\r
419                                         'name'    => $name,\r
420                                         'new_ver' => $EXT_SEARCH['ver'][$id],\r
421                                         'old_ver' => $EXT_SEARCH['cver'][$id],\r
422                                         'link'    => $LINK,\r
423                                         'last'    => MAKE_DATETIME($EXT_SEARCH['fctime'][$id], "2"),\r
424                                         'size'    => TRANSLATE_COMMA(round($EXT_SEARCH['fsize'][$id] / 1.024) / 1000),\r
425                                         'info'    => stripslashes($EXT_SEARCH['infos'][$id]),\r
426                                 );\r
427 \r
428                                 // Load row template add current size to total size and switch color\r
429                                 $OUT .= LOAD_TEMPLATE("admin_list_extensions_row", true, $content);\r
430                                 $TSIZE += $EXT_SEARCH['fsize'][$id];\r
431                                 $SW = 3 - $SW;\r
432                         }\r
433                         define('__EXTENSIONS_ROWS', $OUT);\r
434                         define('__TKBYTES_VALUE', TRANSLATE_COMMA(round($TSIZE / 1.024) / 1000));\r
435                         define('__TEXT_VALUE', sizeof($EXT_SEARCH['fname']));\r
436 \r
437                         // Load template\r
438                         LOAD_TEMPLATE("admin_list_extensions");\r
439                 }\r
440                  else\r
441                 {\r
442                         // All extensions are downloaded and installed\r
443                         LOAD_TEMPLATE("admin_extensions_installed", false, $count);\r
444                 }\r
445         }\r
446          else\r
447         {\r
448                 // No extensions where found\r
449                 LOAD_TEMPLATE("admin_extensions_404");\r
450         }\r
451         break;\r
452 \r
453 case "demo":\r
454         LOAD_TEMPLATE("admin_settings_saved", false, SETTINGS_NOT_SAVED);\r
455         break;\r
456 }\r
457 //\r
458 ?>\r