1439e97a8c18e1e9bc0be0828af2670fdb0c3629
[mailer.git] / inc / extensions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 03/25/2004 *
4  * ===============                              Last change: 09/29/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : extensions.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')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Load the extension and maybe found language and function files.
41 function LOAD_EXTENSION ($ext_name, $EXT_LOAD_MODE = "", $EXT_VER = "", $dry_run = false, &$SQLs = array()) {
42         global $EXT_LOADED, $_CONFIG, $CSS, $cacheMode, $EXT_VER_HISTORY;
43         global $INC_POOL, $EXT_UPDATE_DEPENDS, $EXT_DEPRECATED, $UPDATE_NOTES;
44         global $EXT_VERSION, $EXT_ALWAYS_ACTIVE;
45
46         // Init array
47         $INC_POOL = array();
48
49         // Is the extension already loaded?
50         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Loading extension {$ext_name}, mode={$EXT_LOAD_MODE}, ver={$EXT_VER}.");
51         if ((isset($EXT_LOADED['ext'][$ext_name])) && (empty($EXT_LOAD_MODE))) {
52                 // Debug message
53                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
54                 return false;
55         } // END - if
56
57         // Construct FQFN for extension file
58         $extInclude = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
59
60         // Is the extension file NOT there?
61         if (!FILE_READABLE($extInclude)) {
62                 // Debug message
63                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s not found.", $ext_name));
64
65                 // Abort here
66                 return false;
67         } // END - if
68
69         // Construct FQFN for language file
70         $langInclude = sprintf("%sinc/language/%s_%s.php", PATH, $ext_name, GET_LANGUAGE());
71
72         // Is this include there?
73         if ((FILE_READABLE($langInclude)) && (!isset($EXT_LOADED['lang'][$ext_name]))) {
74                 // Then load it
75                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Language loaded.");
76                 $EXT_LOADED['lang'][$ext_name] = true;
77                 require($langInclude);
78         } // END - if
79
80         // Construct FQFN for functions file
81         $funcsInclude = sprintf("%sinc/libs/%s_functions.php", PATH, $ext_name);
82
83         // Is this include there?
84         if ((FILE_READABLE($funcsInclude)) && (!isset($EXT_LOADED['funcs'][$ext_name]))) {
85                 // Then load it
86                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Functions loaded.");
87                 $EXT_LOADED['funcs'][$ext_name] = true;
88                 require($funcsInclude);
89         } // END - if
90
91         // Extensions are not deprecated by default
92         $EXT_DEPRECATED = "N";
93
94         // Extensions are not always active by default
95         $EXT_ALWAYS_ACTIVE = "N";
96
97         // By default an extension update does not depend on other extensions
98         $EXT_UPDATE_DEPENDS = "";
99
100         // Extension update notes
101         $UPDATE_NOTES = "";
102
103         // Include the extension file
104         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Extension loaded.");
105         require($extInclude);
106
107         // Is this extension deprecated?
108         if ($EXT_DEPRECATED == "Y") {
109                 // Deactivate the extension
110                 DEACTIVATE_EXTENSION($ext_name);
111
112                 // Abort here
113                 return false;
114         } // END - if
115
116         // Mark it as loaded in normal mode
117         if (empty($EXT_LOAD_MODE)) {
118                 // Mark it now...
119                 $EXT_LOADED['ext'][$ext_name] = true;
120         } // END - if
121
122         // All fine!
123         return true;
124 }
125
126 // Registeres an extension and possible update depencies
127 function EXTENSION_REGISTER ($ext_name, $task_id, $dry_run = false, $logout = true) {
128         global $UPDATE_NOTES, $INC_POOL, $cacheInstance;
129         global $EXT_VER_HISTORY, $NOTES, $EXT_ALWAYS_ACTIVE, $EXT_VERSION;
130         global $EXT_UPDATE_DEPENDS;
131
132         // This shall never do a non-admin user!
133         if (!IS_ADMIN()) return false;
134
135         // Is this extension already installed?
136         if (EXT_IS_ACTIVE($ext_name)) return false;
137
138         // Init variables
139         $ret = false; $SQLs = array();
140         $NOTES = "";
141         $INC_POOL = array();
142
143         // By default we have no failtures
144         $EXT_REPORTS_FAILURE = false;
145
146         // Does this extension exists?
147         if (LOAD_EXTENSION($ext_name, "register", "", $dry_run, $SQLs)) {
148                 // And run possible updates
149                 $history = $EXT_VER_HISTORY;
150                 foreach ($history as $ver) {
151                         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name={$ext_name}, ext_ver={$ver}");
152                         // Load extension in update mode
153                         LOAD_EXTENSION($ext_name, "update", $ver, $dry_run, $SQLs);
154
155                         // Do we have an update?
156                         if (((GET_EXT_VERSION("sql_patches") != "") && (getConfig('verbose_sql') == "Y")) || (!EXT_IS_ACTIVE("sql_patches"))) {
157                                 if (!empty($UPDATE_NOTES)) {
158                                         // Update notes found
159                                         $NOTES .= ("<div class=\"update_notes\"><strong>v".$ver.":</strong><br />".$UPDATE_NOTES."</div>");
160                                         $UPDATE_NOTES = "";
161                                 } elseif (($ver == "0.0") || ($ver == "0.0.0")) {
162                                         // Initial release
163                                         $NOTES .= ("<div class=\"update_notes\"><strong>v".$ver.":</strong><br />".INITIAL_RELEASE."</div>");
164                                 } else {
165                                         // No update notes found!
166                                         $NOTES .= ("<div class=\"update_notes\"><strong>v".$ver.":</strong><br /><I>".NO_UPDATE_NOTES."</I></div>");
167                                 }
168                         } // END - if
169                 } // END - foreach
170
171                 // Does this extension depends on an outstanding update of another update?
172                 if (!empty($EXT_UPDATE_DEPENDS)) {
173                         // Backup SQL commands and clear current
174                         $SQLs2 = $SQLs;
175                         $SQLs  = array();
176                         $test  = false;
177                         $ext_update = $EXT_UPDATE_DEPENDS;
178
179                         // Check for required file
180                         if (LOAD_EXTENSION($ext_update, "register", "", $dry_run, $SQLs)) {
181                                 // If versions mismatch update extension first
182                                 $ext_ver = GET_EXT_VERSION($ext_update);
183
184                                 // Extension version set? If empty the extension is not registered
185                                 if (empty($ext_ver)) {
186                                         // Extension not registered so far so first load task's ID...
187                                         $task = DETERMINE_EXTENSION_TASK_ID($ext_update);
188
189                                         // Entry found?
190                                         if ($task > 0) {
191                                                 // Try to register the extension
192                                                 $test = EXTENSION_REGISTER($ext_update, $task, $dry_run, false);
193                                         } // END - if
194                                 } elseif ($ext_ver != $EXT_VERSION) {
195                                         // Ok, update this extension now
196                                         EXTENSION_UPDATE($ext_update, $ext_ver, $dry_run);
197
198                                         // All okay!
199                                         $test = true;
200                                 } else {
201                                         // Nothing to register / update before...
202                                         $test = true;
203                                 }
204                         } else {
205                                 // Required file for update does not exists!
206                                 $test = true;
207                                 // But this is fine for the first time...
208                         }
209
210                         // Finally restore previous SQLs
211                         $SQLs = $SQLs2; unset($SQLs2);
212                 } else {
213                         // Does not depend on an other extension
214                         $test = true;
215                 }
216
217                 // Switch back to register mode
218                 $EXT_LOAD_MODE = "register";
219
220                 // Remains true if extension registration reports no failtures
221                 $test = ($test && !$EXT_REPORTS_FAILURE);
222
223                 // Does everthing before wents ok?
224                 if ($test) {
225                         // "Dry-run-mode" activated?
226                         if (!$dry_run) {
227                                 // Run installation pre-installation filters
228                                 RUN_FILTER('pre_extension_installed', array('dry_run' => $dry_run, 'sqls' => $SQLs));
229
230                                 // Register extension
231                                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_active, ext_version) VALUES ('%s','%s','%s')",
232                                         array($ext_name, $EXT_ALWAYS_ACTIVE, $EXT_VERSION), __FILE__, __LINE__);
233
234                                 // Remove cache file(s) if extension is active
235                                 RUN_FILTER('post_extension_installed', array('ext_name' => $ext_name, 'task_id' => $task_id, 'inc_pool' => $INC_POOL));
236
237                                 // In normal mode return a true on success
238                                 $ret = true;
239                         } else {
240                                 // Rewrite SQL command to keep { and } inside
241                                 foreach ($SQLs as $key => $sql) {
242                                         $sql = str_replace('{', "&#123;", str_replace('}', "&#125;", $sql));
243                                         $SQLs[$key] = $sql;
244                                 } // END - foreach
245
246                                 // In  "dry-run" mode return array with all SQL commands
247                                 $ret = $SQLs;
248
249                                 // Remove all SQL commands
250                                 unset($SQLs);
251                         }
252                 } else {
253                         // No, an error occurs while registering extension :-(
254                         $ret = false;
255                 }
256         } elseif (($task_id > 0) && (!empty($ext_name))) {
257                 // Remove task from system when id and extension's name is valid
258                 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND status='NEW' LIMIT 1",
259                         array(bigintval($task_id)), __FILE__, __LINE__);
260         }
261
262         // Is this the sql_patches?
263         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":{$ext_name}/{$EXT_LOAD_MODE}");
264         if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
265                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
266                 if ($logout) {
267                         // Then redirect to logout
268                         LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
269                 } else {
270                         // Add temporary filter
271                         REGISTER_FILTER('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
272                         $GLOBALS['ext_load_mode'] = $EXT_LOAD_MODE;
273                 }
274         } // END - if
275
276         // Return status code
277         return $ret;
278 }
279
280 // Run SQL queries for given extension id
281 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
282 function EXTENSION_RUN_SQLS ($ext_id, $load_mode) {
283         global $cacheInstance;
284
285         // This shall never do a non-admin user!
286         if (!IS_ADMIN()) return false;
287
288         // Init array
289         $SQLs = array();
290
291         // By default no SQL has been executed
292         $sqlRan = false;
293
294         // Get extension's name
295         $ext_name = GET_EXT_NAME($ext_id);
296         if (empty($ext_name)) return false;
297
298         // Load extension in detected mode
299         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]={$ext_name}");
300         LOAD_EXTENSION($ext_name, $load_mode, "", false, $SQLs);
301
302         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".count($SQLs)."");
303         if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
304                 // Run SQL commands...
305                 RUN_FILTER('run_sqls', array('dry_run' => false, 'sqls' => $SQLs));
306
307                 // Removal mode?
308                 if ($load_mode == "remove") {
309                         // Delete this extension (remember to remove it from your server *before* you click on welcome!
310                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
311                                 array($ext_name), __FILE__, __LINE__);
312                 } // END - if
313         } // END - if
314
315         // Remove cache file(s) if extension is active
316         if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == "activate") || ($load_mode == "deactivate"))) {
317                 // Run filters
318                 RUN_FILTER('post_extension_run_sql', $ext_name);
319         } // END - if
320
321         // Is this the sql_patches?
322         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": {$ext_id}/{$ext_name}/{$load_mode}");
323         if (($ext_name == "sql_patches") && (($load_mode == "register") || ($load_mode == "remove"))) {
324                 // Then redirect to logout
325                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
326                 LOAD_URL("modules.php?module=admin&logout=1&".$load_mode."=sql_patches");
327         } // END - if
328 }
329
330 // Check if given extension is active
331 function EXT_IS_ACTIVE ($ext_name) {
332         global $cacheArray, $_CONFIG;
333
334         // Extensions are all inactive during installation
335         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
336
337         // Not active is the default
338         $active = "N";
339
340         // Check cache
341         if (isset($cacheArray['extensions']['ext_active'][$ext_name])) {
342                 // Load from cache
343                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
344                 $active = $cacheArray['extensions']['ext_active'][$ext_name];
345
346                 // Count cache hits
347                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
348         } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
349                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
350                 // Load from database
351                 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
352                         array($ext_name), __FILE__, __LINE__);
353
354                 // Entry found?
355                 if (SQL_NUMROWS($result) == 1) {
356                         // Load entry
357                         list($active) = SQL_FETCHROW($result);
358                 } // END - if
359
360                 // Free result
361                 SQL_FREERESULT($result);
362
363                 // Write cache array
364                 //* DEBUG: */ echo $ext_name."[DB]: {$active}");
365                 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
366         } else {
367                 // Extension not active!
368                 //* DEBUG: */ echo $ext_name.": Not active!");
369                 $cacheArray['extensions']['ext_active'][$ext_name] = "N";
370         }
371
372         // Debug message
373         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
374
375         // Is this extension activated? (For admins we always have active extensions...)
376         return ($active == "Y");
377 }
378 // Get version from extensions
379 function GET_EXT_VERSION ($ext_name) {
380         global $cacheArray, $_CONFIG, $cacheInstance;
381         $ext_ver = false;
382
383         // Extensions are all inactive during installation
384         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
385         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
386
387         // Is the cache written?
388         if (isset($cacheArray['extensions']['ext_version'][$ext_name])) {
389                 // Load data from cache
390                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
391                 $ext_ver = $cacheArray['extensions']['ext_version'][$ext_name];
392
393                 // Count cache hits
394                 if (getConfig('cache_hits') > 0) $_CONFIG['cache_hits']++; else $_CONFIG['cache_hits'] = 1;
395         } elseif (!is_object($cacheInstance)) {
396                 // Load from database
397                 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
398                         array($ext_name), __FILE__, __LINE__);
399
400                 // Is the extension there?
401                 if (SQL_NUMROWS($result) == 1) {
402                         // Load entry
403                         list($ext_ver) = SQL_FETCHROW($result);
404                 } // END - if
405
406                 // Free result
407                 SQL_FREERESULT($result);
408
409                 // Set cache
410                 $cacheArray['extensions']['ext_version'][$ext_name] = $ext_ver;
411         }
412
413         // Return result
414         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
415         return $ext_ver;
416 }
417
418 // Updates a given extension with current extension version to latest version
419 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
420         // This shall never do a non-admin user!
421         global $cacheInstance, $UPDATE_NOTES, $NOTES, $EXT_VER_HISTORY;
422         global $EXT_UPDATE_DEPENDS, $EXT_VERSION, $INC_POOL, $cacheArray, $SQLs;
423
424         // Init arrays
425         $SQLs = array(); $INC_POOL = array();
426
427         // Only admins are allowed to update extensions
428         if ((!IS_ADMIN()) || (empty($ext_name))) return false;
429
430         // Load extension in test mode
431         LOAD_EXTENSION($ext_name, "test", $ext_ver, $dry_run, $SQLs);
432
433         // Save version history
434         $history = $EXT_VER_HISTORY;
435
436         // Remove old SQLs array to prevent possible bugs
437         $SQLs = array();
438
439         // Check if version is updated
440         if ((($EXT_VERSION != $ext_ver) || ($dry_run)) && (is_array($history))) {
441                 // Search for starting point
442                 $start = array_search($ext_ver, $history);
443                 $NOTES = "";
444
445                 // And load SQL queries in order of version history
446                 for ($idx = ($start + 1); $idx < sizeof($history); $idx++) {
447                         // Set extension version
448                         $cacheArray['update_ver'][$ext_name] = $history[$idx];
449
450                         // Load again...
451                         LOAD_EXTENSION($ext_name, "update", $cacheArray['update_ver'][$ext_name], $dry_run, $SQLs);
452
453                         if (!empty($EXT_UPDATE_DEPENDS)) {
454                                 // Backup current SQL queries
455                                 $cacheArray['update_sqls'][$ext_name] = $SQLs;
456
457                                 // Is the extension there?
458                                 if (GET_EXT_VERSION($EXT_UPDATE_DEPENDS) != "") {
459                                         // Update another extension first!
460                                         $test = EXTENSION_UPDATE($EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
461                                 } else {
462                                         // Register new extension
463                                         $test = EXTENSION_REGISTER($EXT_UPDATE_DEPENDS, 0, $dry_run, false);
464                                 }
465
466                                 // Restore previous SQL queries
467                                 $SQLs = $cacheArray['update_sqls'][$ext_name];
468                                 unset($cacheArray['update_sqls'][$ext_name]);
469                         } // END - if
470
471                         // Add notes
472                         if (getConfig('verbose_sql') == "Y") {
473                                 if (!empty($UPDATE_NOTES)) {
474                                         // Update notes found
475                                         $NOTES .= ("<div class=\"update_notes\"><strong>v".$cacheArray['update_ver'][$ext_name].":</strong><br />".$UPDATE_NOTES."</div>");
476                                         $UPDATE_NOTES = "";
477                                 } elseif ($cacheArray['update_ver'][$ext_name] == "0.0") {
478                                         // Initial release
479                                         $NOTES .= ("<div class=\"update_notes\"><strong>v".$cacheArray['update_ver'][$ext_name].":</strong><br />".INITIAL_RELEASE."</div>");
480                                 } else {
481                                         $NOTES .= ("<div class=\"update_notes\"><strong>v".$cacheArray['update_ver'][$ext_name].":</strong><br /><I>".NO_UPDATE_NOTES."</I></div>");
482                                 }
483                         } // END - if
484                 } // END - for
485
486                 // In real-mode execute any existing includes
487                 if (!$dry_run) {
488                         $cacheArray['inc_pool'][$ext_name] = $INC_POOL;
489                         RUN_FILTER('load_includes', $INC_POOL);
490                         $INC_POOL = $cacheArray['inc_pool'][$ext_name];
491                         unset($cacheArray['inc_pool'][$ext_name]);
492                 } // END - if
493
494                 // Run SQLs
495                 RUN_FILTER('run_sqls', array('dry_run' => $dry_run, 'sqls' => $SQLs));
496
497                 if (!$dry_run) {
498                         // Create task
499                         CREATE_EXTENSION_UPDATE_TASK(GET_CURRENT_ADMIN_ID(), $ext_name, $cacheArray['update_ver'][$ext_name], addslashes($NOTES));
500
501                         // Update extension's version
502                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
503                                 array($cacheArray['update_ver'][$ext_name], $ext_name), __FILE__, __LINE__);
504
505                         // Remove arrays
506                         unset($SQLs);
507                         unset($cacheArray['update_ver'][$ext_name]);
508
509                         // Run filters on success extension update
510                         RUN_FILTER('extension_update', $ext_name);
511                 } else {
512                         // In "dry-run" mode return array with SQL commands
513                         return $SQLs;
514                 }
515         } // END - if
516 }
517
518 // Output verbose SQL table for extension
519 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed = "", $switch = false, $width = "100%") {
520         global $SQLs;
521
522         // Are there some queries in $queries?
523         if (count($queries) > 0) {
524                 // Then use them instead!
525                 $SQLs = $queries;
526         } // END - if
527
528         // Init variables
529         $S = false; $SW = 2; $i = 1;
530         $OUT = "";
531
532         // Do we have queries?
533         if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
534                 $OUT  = "<div align=\"center\">
535 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"".$width."\" align=\"center\"".$dashed.">
536 <tr>
537   <td colspan=\"2\" align=\"center\" class=\"admin_title bottom2\" height=\"24\">
538     <strong>".$title.":</strong>
539   </td>
540 </tr>\n";
541                 foreach ($SQLs as $idx => $sql) {
542                         $sql = trim($sql);
543                         if (!empty($sql)) {
544                                 $S = true;
545                                 $OUT .= "<tr>
546   <td class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 2px\" width=\"30\">".$i.".</td>
547   <td class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 5px; padding-right: 5px\">
548     ".$sql."
549   </td>
550 </tr>\n";
551                                 if ($switch) $SW = 3 - $SW;
552                                 $i++;
553                         }
554                 }
555         }
556
557         if ((!$S) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
558                 // No addional SQL commands to run
559                 $OUT .= "<tr>
560   <td colspan=\"2\" align=\"center\" class=\"switch_sw2 bottom2\" height=\"24\">
561     <font class=\"admin_note\">".ADMIN_NO_ADDIONAL_SQLS."</font>
562   </td>
563 </tr>\n";
564         }
565
566         if (!empty($OUT)) {
567                 // Add missing close-table tag
568                 $OUT .= "</table>
569 </div>\n";
570         }
571
572         // Return output
573         return $OUT;
574 }
575
576 // Get extension name from id
577 function GET_EXT_NAME ($ext_id) {
578         global $cacheArray, $_CONFIG;
579
580         // Init extension name
581         $ret = "";
582
583         // Is cache there?
584         if (isset($cacheArray['extensions']['ext_name'][$ext_id])) {
585                 // Load from cache
586                 $ret = $cacheArray['extensions']['ext_name'][$ext_id];
587
588                 // Count cache hits
589                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
590         } elseif (!EXT_IS_ACTIVE("cache")) {
591                 // Load from database
592                 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
593                         array(bigintval($ext_id)), __FILE__, __LINE__);
594                 list($ret) = SQL_FETCHROW($result);
595                 SQL_FREERESULT($result);
596         }
597         return $ret;
598 }
599
600 // Get extension id from name
601 function GET_EXT_ID ($ext_name) {
602         global $cacheArray, $_CONFIG;
603
604         // Init ID number
605         $ret = 0;
606         if (isset($cacheArray['extensions']['ext_id'][$ext_name])) {
607                 // Load from cache
608                 $ret = $cacheArray['extensions']['ext_id'][$ext_name];
609
610                 // Count cache hits
611                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
612         } elseif (!EXT_IS_ACTIVE("cache")) {
613                 // Load from database
614                 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
615                         array($ext_name), __FILE__, __LINE__);
616                 list($ret) = SQL_FETCHROW($result);
617                 SQL_FREERESULT($result);
618         }
619
620         // Return value
621         return $ret;
622 }
623
624 // Activate given extension
625 function ACTIVATE_EXTENSION ($ext_name) {
626         // Activate the extension
627         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
628                 array($ext_name), __FILE__, __LINE__);
629
630         // Extension has been activated?
631         if (SQL_AFFECTEDROWS() == 1) {
632                 // Then run all queries
633                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
634         } // END - if
635 }
636
637 // Deactivate given extension
638 function DEACTIVATE_EXTENSION($ext_name) {
639         // Activate the extension
640         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='N' WHERE ext_name='%s' LIMIT 1",
641                 array($ext_name), __FILE__, __LINE__);
642
643         // Extension has been activated?
644         if (SQL_AFFECTEDROWS() == 1) {
645                 // Then run all queries
646                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "deactivate");
647
648                 // Create new task
649                 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
650
651                 // Notify the admin
652                 SEND_ADMIN_NOTIFICATION(ADMIN_SUBJECT_EXTENSION_DEACTIVATED, "admin_ext_deactivated", array('ext_name' => $ext_name));
653         } // END - if
654 }
655
656 // Checks wether the extension is older than given
657 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
658         // Get current extension version
659         $currVersion = GET_EXT_VERSION($ext_name);
660
661         // Remove all dots from both versions
662         $currVersion = str_replace(".", "", $currVersion);
663         $ext_ver = str_replace(".", "", $ext_ver);
664
665         // Now compare both and return the result
666         return ($currVersion < $ext_ver);
667 }
668
669 // Creates a new task for updated extension
670 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
671         // Create subject line
672         $subject = "[UPDATE-".$ext_name."-".$ext_ver.":] ".ADMIN_UPDATE_EXT_SUBJ;
673
674         // Is the extension there?
675         if (GET_EXT_VERSION($ext_name) != "") {
676                 // Check if task is not there
677                 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
678                         // Task not created so it's a brand-new extension which we need to register and create a task for!
679                         SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created) VALUES ('%s','0','NEW','EXTENSION_UPDATE','%s','%s', UNIX_TIMESTAMP())",
680                                 array($admin_id, $subject, $notes), __FILE__, __LINE__);
681                 } // END - if
682         } // END - if
683 }
684
685 // Creates a new task for newly installed extension
686 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
687         // Not installed and do we have created a task for the admin?
688         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == "")) {
689                 // Template file
690                 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
691                         PATH,
692                         GET_LANGUAGE(),
693                         $ext
694                 );
695
696                 // Load text for task
697                 if (FILE_READABLE($tpl)) {
698                         // Load extension's own text template (HTML!)
699                         $msg = LOAD_TEMPLATE("ext_".$ext, true);
700                 } else {
701                         // Load default message
702                         $msg = LOAD_TEMPLATE("admin_new_ext", "", 0);
703                 }
704
705                 // Task not created so it's a brand-new extension which we need to register and create a task for!
706                 $result_insert = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created)
707 VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
708                         array(
709                                 $admin_id,
710                                 $subject,
711                                 addslashes($msg),
712                         ),  __FILE__, __LINE__, true, false
713                 );
714         } // END - if
715 }
716
717 // Creates a task for automatically deactivated (deprecated) extension
718 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
719         // Create subject line
720         $subject = sprintf("[%s:] %s", $ext, TASK_SUBJ_EXTENSION_DEACTIVATED);
721
722         // Not installed and do we have created a task for the admin?
723         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != "")) {
724                 // Task not created so add it
725                 $result_insert = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created)
726 VALUES (0,0,'NEW','EXTENSION_DEACTIVATION','%s','%s',UNIX_TIMESTAMP())",
727                         array(
728                                 $subject,
729                                 addslashes(LOAD_TEMPLATE("task_ext_deactivated", true, $ext)),
730                         ),  __FILE__, __LINE__, true, false
731                 );
732         } // END - if
733
734         // Free memory
735         SQL_FREERESULT($result);
736 }
737
738 // Checks if the module has a menu
739 function MODULE_HAS_MENU ($mod, $forceDb = false) {
740         global $cacheArray, $_CONFIG;
741
742         // All is false by default
743         $ret = false;
744         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mod={$mod},cache=".GET_EXT_VERSION("cache")."<br />\n";
745         if (GET_EXT_VERSION("cache") >= "0.1.2") {
746                 // Cache version is okay, so let's check the cache!
747                 if (isset($cacheArray['modules']['has_menu'][$mod])) {
748                         // Check module cache and count hit
749                         $ret = ($cacheArray['modules']['has_menu'][$mod] == "Y");
750                         if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
751                 } elseif (isset($cacheArray['extensions']['ext_menu'][$mod])) {
752                         // Check cache and count hit
753                         $ret = ($cacheArray['extensions']['ext_menu'][$mod] == "Y");
754                         if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
755                 } elseif ((IS_ADMIN()) && ($mod == "admin")) {
756                         // Admin module has always a menu!
757                         $ret = true;
758                 }
759         } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
760                 // Check database for entry
761                 $result = SQL_QUERY_ESC("SELECT has_menu FROM "._MYSQL_PREFIX."_mod_reg WHERE module='%s' LIMIT 1",
762                         array($mod), __FILE__, __LINE__);
763
764                 // Entry found?
765                 if (SQL_NUMROWS($result) == 1) {
766                         // Load "has_menu" column
767                         list($has_menu) = SQL_FETCHROW($result);
768
769                         // Fake cache... ;-)
770                         $cacheArray['extensions']['ext_menu'][$mod] = $has_menu;
771
772                         // Does it have a menu?
773                         $ret = ($has_menu == "Y");
774                 } // END  - if
775
776                 // Free memory
777                 SQL_FREERESULT($result);
778         } elseif (GET_EXT_VERSION("sql_patches") == "") {
779                 // No sql_patches installed, so maybe in admin area?
780                 $ret = ((IS_ADMIN()) && ($mod == "admin")); // Then there is a menu!
781         }
782
783         // Return status
784         //* DEBUG: */ var_dump($ret);
785         return $ret;
786 }
787
788 // Determines the task id for given extension
789 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
790         // Default is not found
791         $task_id = 0;
792
793         // Search for extension task's id
794         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE task_type='EXTENSION' AND subject='[%s:]' LIMIT 1",
795                 array($ext_name), __FILE__, __LINE__);
796
797         // Entry found?
798         if (SQL_NUMROWS($result) == 1) {
799                 // Task found so load task's ID and register extension...
800                 list($task_id) = SQL_FETCHROW($result);
801         } // END - if
802
803         // Free result
804         SQL_FREERESULT($result);
805
806         // Return it
807         return $task_id;
808 }
809
810 // Determines the task id for given subject
811 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
812         // Default is not found
813         $task_id = 0;
814
815         // Search for task id
816         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE subject LIKE '%s%%' LIMIT 1",
817                 array($subject), __FILE__, __LINE__);
818
819         // Entry found?
820         if (SQL_NUMROWS($result) == 1) {
821                 // Task found so load task's ID and register extension...
822                 list($task_id) = SQL_FETCHROW($result);
823         } // END - if
824
825         // Free result
826         SQL_FREERESULT($result);
827
828         // Return it
829         return $task_id;
830 }
831
832 //
833 ?>