Several fixes and and improvements:
[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 //
41 function EXTENSION_REGISTER ($ext_name, $id, $dry_run=false)
42 {
43         global $NOTES, $_CONFIG, $INC_POOL, $cacheInstance;
44
45         // We want to register an extension and registration status is by default "failed" (= false)
46         $EXT_LOAD_MODE = "register"; $ret = false; $SQLs = array();
47         $INC_POOL = array();
48
49         // This shall never do a non-admin user!
50         if (!IS_ADMIN()) return false;
51
52         // By default we have no failtures
53         $EXT_REPORTS_FAILURE = true;
54
55         // Is this extension already installed?
56         if (EXT_IS_ACTIVE($ext_name)) return false;
57
58         // Generate file name
59         $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
60
61         // Does this extension exists?
62         if (FILE_READABLE($file)) {
63                 // Extension was found so we can load it in registration mode
64                 $EXT_ALWAYS_ACTIVE = "N";
65                 require($file);
66
67                 // And run possible updates
68                 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = "";
69                 foreach ($EXT_VER_HISTORY as $EXT_VER) {
70                         // Load extension in update mode
71                         require($file);
72
73                         // Do we have an update?
74                         if (((GET_EXT_VERSION("sql_patches") != "") && ($_CONFIG['verbose_sql'] == "Y")) || (!EXT_IS_ACTIVE("sql_patches"))) {
75                                 if (!empty($UPDATE_NOTES)) {
76                                         // Update notes found
77                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
78                                         $UPDATE_NOTES = "";
79                                 } elseif (($EXT_VER == "0.0") || ($EXT_VER == "0.0.0")) {
80                                         // Initial release
81                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
82                                 } else {
83                                         // No update notes found!
84                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
85                                 }
86                         } // END - if
87                 } // END - foreach
88
89                 // Does this extension depends on an outstanding update of another update?
90                 if (!empty($EXT_UPDATE_DEPENDS)) {
91                         // Backup SQL commands and clear current
92                         $SQLs2 = $SQLs;
93                         $SQLs  = array();
94                         $test  = false;
95
96                         // Load required extension also in update mode
97                         $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $EXT_UPDATE_DEPENDS);
98
99                         // Check for required file
100                         if (FILE_READABLE($file)) {
101                                 // Bacup version number
102                                 $VER_BACKUP = $EXT_VERSION;
103
104                                 // Backup language as well
105                                 $LANG_BCK = $EXT_LANG_PREFIX;
106
107                                 // Save the Parrent $EXT_ALWAYS_ACTIVE for later!
108                                 $EXT_ALWAYS_ACTIVE_PARRENT = $EXT_ALWAYS_ACTIVE;
109
110                                 // Set EXT_ALWAYS_ACTIVE for update
111                                 $EXT_ALWAYS_ACTIVE = "N";
112
113                                 // File exists so let's load it
114                                 require($file);
115
116                                 // If versions mismatch update extension first
117                                 $ext_ver = GET_EXT_VERSION($EXT_UPDATE_DEPENDS);
118
119                                 // Extension version set?
120                                 if (empty($ext_ver)) {
121                                         // Extension not registered so far so first load task's ID...
122                                         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE task_type='EXTENSION' AND subject LIKE '[%s:]%%' LIMIT 1",
123                                          array($EXT_UPDATE_DEPENDS), __FILE__, __LINE__);
124
125                                         // Entry found?
126                                         if (SQL_NUMROWS($result) == 1) {
127                                                 // Task found so load task's ID and register extension...
128                                                 list($task) = SQL_FETCHROW($result);
129
130                                                 // Try to register the extension
131                                                 $test = EXTENSION_REGISTER($EXT_UPDATE_DEPENDS, $task, $dry_run);
132                                         } // END - if
133
134                                         // Free result
135                                         SQL_FREERESULT($result);
136                                 } elseif ($ext_ver != $EXT_VERSION) {
137                                         // Ok, update this extension now
138                                         EXTENSION_UPDATE(basename($file), $EXT_UPDATE_DEPENDS, $ext_ver, $dry_run);
139
140                                         // All okay!
141                                         $test = true;
142                                 } else {
143                                         // Nothing to register / update before...
144                                         $test = true;
145                                 }
146
147                                 // Restore version number
148                                 $EXT_VERSION = $VER_BACKUP;
149
150                                 // Restore language back
151                                 $EXT_LANG_PREFIX = $LANG_BCK;
152
153                                 // Restore $EXT_ALWAYS_ACTIVE with the value from parrent
154                                 $EXT_ALWAYS_ACTIVE = $EXT_ALWAYS_ACTIVE_PARRENT;
155                         } else {
156                                 // Required file for update does not exists!
157                                 $test = true;
158                                 // But this is fine for the first time...
159                         }
160
161                         // Finally restore previous SQLs
162                         $SQLs = $SQLs2; unset($SQLs2);
163                 } else {
164                         // Does not depend on an other extension
165                         $test = true;
166                 }
167
168                 // Switch back to register mode
169                 $EXT_LOAD_MODE = "register";
170
171                 // Remains true if extension registration reports no failtures
172                 $test = ($test && !$EXT_REPORTS_FAILTURE);
173
174                 // Does everthing before wents ok?
175                 if ($test) {
176                         // "Dry-run-mode" activated?
177                         if (!$dry_run) {
178                                 // Run all SQLs
179                                 foreach ($SQLs as $sql) {
180                                         // Trim spaces away which we don't need
181                                         $sql = trim($sql);
182
183                                         // Is there still an SQL query?
184                                         if (!empty($sql)) {
185                                                 // Do we have an "ALTER TABLE" command?
186                                                 if (substr(strtolower($sql), 0, 11) == "alter table") {
187                                                         // Analyse the alteration command
188                                                         SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
189                                                 } else {
190                                                         // Run regular SQL command
191                                                         $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
192                                                 }
193                                         } // END - if
194                                 } // END - foreach
195
196                                 // Remove cache file(s) if extension is active
197                                 if ((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) {
198                                         //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
199                                         // Remove cache files
200                                         if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
201                                         if ($cacheInstance->cache_file("mod_reg", true))    $cacheInstance->cache_destroy();
202                                         if ($cacheInstance->cache_file("config", true))     $cacheInstance->cache_destroy();
203                                 } // END - if
204
205                                 // Check for added include files
206                                 if (count($INC_POOL > 0)) {
207                                         // Loads every include file
208                                         foreach ($INC_POOL as $inc) {
209                                                 require_once($inc);
210                                         } // END - foreach
211
212                                         // Remove array
213                                         unset($INC_POOL);
214                                 } // END - if
215
216                                 // Register extension
217                                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_lang_file, ext_active, ext_version) VALUES ('%s','%s','%s','%s')",
218                                  array($ext_name, $EXT_LANG_PREFIX, $EXT_ALWAYS_ACTIVE, $EXT_VERSION), __FILE__, __LINE__);
219
220                                 // Update task management
221                                 ADMIN_SOLVE_TASK($id);
222
223                                 // @TODO This causes the whole (!) menu cache being purged
224                                 CACHE_PURGE_ADMIN_MENU();
225
226                                 // In normal mode return a true on success
227                                 $ret = true;
228
229                                 // Remove SQLs
230                                 unset($SQLs);
231                         } else {
232                                 // Rewrite SQL command to keep { and } inside
233                                 foreach ($SQLs as $key => $sql) {
234                                         $sql = str_replace('{', "&#123;", str_replace('}', "&#125;", $sql));
235                                         $SQLs[$key] = $sql;
236                                 } // END - foreach
237
238                                 // In  "dry-run" mode return array with all SQL commands
239                                 $ret = $SQLs;
240
241                                 // Remove all SQL commands
242                                 unset($SQLs);
243                         }
244                 } else {
245                         // No, an error occurs while registering extension :-(
246                         $ret = false;
247                 }
248         } elseif (($id > 0) && (!empty($ext_name))) {
249                 // Remove task from system when id and extension's name is valid
250                 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND status='NEW' LIMIT 1",
251                  array(bigintval($id)), __FILE__, __LINE__);
252         }
253
254         // Is this the sql_patches?
255         //* DEBUG: */ echo __LINE__.":{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
256         if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
257                 // Then redirect to logout
258                 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
259                 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
260         } // END - if
261
262         // Return status code
263         return $ret;
264 }
265 //
266 function EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE) {
267         global $cacheInstance, $_CONFIG;
268         $SQLs = array();
269
270         // Extensions are never active by default
271         $EXT_ALWAYS_ACTIVE = "N";
272
273         // By default no SQL has been executed
274         $sqlRan = false;
275
276         // This shall never do a non-admin user!
277         if (!IS_ADMIN()) return false;
278
279         // Get extension's name
280         $ext_name = GET_EXT_NAME($id);
281         if (empty($ext_name)) return false;
282
283         // Load extension in detected mode
284         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):ext_name[{$id}]={$ext_name}<br />\n";
285         $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
286         if (FILE_READABLE($file)) {
287                 // Load the include
288                 require($file);
289         } // END - if
290
291         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):SQLs::count=".count($SQLs)."<br />\n";
292         if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
293                 // Run SQL commands...
294                 foreach ($SQLs as $sql) {
295                         // Trim spaces away which we don't need
296                         $sql = trim($sql);
297
298                         // Is there still an SQL query?
299                         if (!empty($sql)) {
300                                 // Do we have an "ALTER TABLE" command?
301                                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):SQL={$SQL}<br />\n";
302                                 if (substr(strtolower($sql), 0, 11) == "alter table") {
303                                         // Analyse the alteration command
304                                         SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
305                                 } else {
306                                         // Run regular SQL command
307                                         $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
308                                 }
309
310                                 // An SQL has been executed
311                                 $sqlRan = true;
312                         } // END - if
313                 } // END - foreach
314
315                 // Removal mode?
316                 if ($EXT_LOAD_MODE == "remove") {
317                         // Delete this extension (remember to remove it from your server *before* you click on welcome!
318                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
319                          array($id), __FILE__, __LINE__);
320                 } // END - if
321
322                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mode={$EXT_LOAD_MODE}<br />\n";
323
324                 // Is this the sql_patches?
325                 //* DEBUG: */ echo __LINE__.": {$id}/{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
326                 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove"))) {
327                         // Then redirect to logout
328                         //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
329                         LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
330                 } // END - if
331         } // END - if
332
333         // Remove cache file(s) if extension is active
334         if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($EXT_LOAD_MODE == "activate") || ($EXT_LOAD_MODE == "deactivate"))) {
335                 //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
336                 // Remove cache files
337                 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
338                 if ($cacheInstance->cache_file("mod_reg", true))    $cacheInstance->cache_destroy();
339                 if ($cacheInstance->cache_file("config", true))     $cacheInstance->cache_destroy();
340
341                 // @TODO This causes the whole (!) menu cache being purged
342                 CACHE_PURGE_ADMIN_MENU();
343         } // END - if
344 }
345 // Check if given extension is active
346 function EXT_IS_ACTIVE ($ext_name) {
347         global $cacheArray, $_CONFIG;
348
349         // Extensions are all inactive during installation
350         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
351
352         // Not active is the default
353         $active = "N";
354
355         // Check cache
356         if (!empty($cacheArray['extensions']['ext_active'][$ext_name])) {
357                 // Load from cache
358                 //* DEBUG: */ echo "CACHE! ext_name={$ext_name}<br />\n";
359                 $active = $cacheArray['extensions']['ext_active'][$ext_name];
360
361                 // Count cache hits
362                 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++;
363         } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
364                 //* DEBUG: */ echo "DB! ext_name={$ext_name}<br />\n";
365                 // Load from database
366                 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
367                  array($ext_name), __FILE__, __LINE__);
368                 if (SQL_NUMROWS($result) == 0) {
369                         // Extension was not found!
370                         return false;
371                 }
372
373                 // Load entry
374                 list($active) = SQL_FETCHROW($result);
375
376                 // Free result
377                 SQL_FREERESULT($result);
378
379
380                 // Write cache array
381                 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
382                 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
383         } else {
384                 // Extension not active!
385                 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
386                 $cacheArray['extensions']['ext_active'][$ext_name] = "N";
387         }
388
389         // Debug message
390         //DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
391
392         // Is this extension activated? (For admins we always have active extensions...)
393         return ($active == "Y");
394 }
395 // Get version from extensions
396 function GET_EXT_VERSION ($ext_name) {
397         global $cacheArray, $_CONFIG, $cacheInstance;
398         $ret = false;
399
400         // Extensions are all inactive during installation
401         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
402         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ext_name={$ext_name}<br />\n";
403
404         // Is the cache written?
405         if (!empty($cacheArray['extensions']['ext_version'][$ext_name])) {
406                 // Load data from cache
407                 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): CACHE!<br />\n";
408                 $ret = $cacheArray['extensions']['ext_version'][$ext_name];
409
410                 // Count cache hits
411                 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++; else $_CONFIG['cache_hits'] = 1;
412         } elseif (!is_object($cacheInstance)) {
413                 // Load from database
414                 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
415                  array($ext_name), __FILE__, __LINE__);
416                 list($ret) = SQL_FETCHROW($result);
417                 SQL_FREERESULT($result);
418
419                 // Set cache
420                 $cacheArray['extensions']['ext_version'][$ext_name] = $ret;
421         }
422
423         // Return result
424         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ret={$ret}<br />\n";
425         return $ret;
426 }
427 //
428 function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
429 {
430         // This shall never do a non-admin user!
431         global $cacheInstance, $_CONFIG, $NOTES;
432
433         // Init arrays
434         $SQLs = array(); $INC_POOL = array();
435
436         // Only admins are allowed to update extensions
437         if ((!IS_ADMIN()) || (empty($ext))) return false;
438
439         // Load extension in update mode
440         $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = ""; $NOTES = "";
441
442         // Load extension file
443         include(sprintf("%sinc/extensions/%s", PATH, $file));
444
445         if (!empty($EXT_UPDATE_DEPENDS)) {
446                 // Update another extension first!
447                 $test = EXTENSION_UPDATE(("ext-".$EXT_UPDATE_DEPENDS.".php"), $EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
448         }
449
450         // Check if version is updated
451         if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY)))
452         {
453                 // Search for starting point
454                 $start = array_search($EXT_VER, $EXT_VER_HISTORY);
455                 $NOTES = "";
456
457                 // And load SQL queries in order of version history
458                 for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++)
459                 {
460                         // Remove old SQLs array to prevent possible bugs
461                         if (!$dry_run) { unset($SQLs); $SQLs = array(); }
462
463                         // Set version
464                         $EXT_VER = $EXT_VER_HISTORY[$idx];
465
466                         // Include again...
467                         include(PATH."inc/extensions/".$file);
468
469                         // Add notes
470                         if ($_CONFIG['verbose_sql'] == "Y")
471                         {
472                                 $EXT_VER = $EXT_VER_HISTORY[$idx];
473                                 if (!empty($UPDATE_NOTES))
474                                 {
475                                         // Update notes found
476                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
477                                         $UPDATE_NOTES = "";
478                                 }
479                                  elseif ($EXT_VER == "0.0")
480                                 {
481                                         // Initial release
482                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
483                                 }
484                                  else
485                                 {
486                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
487                                 }
488                         }
489
490                         // In real-mode execute any existing includes
491                         if ((!$dry_run) && (count($INC_POOL) > 0)) {
492                                 // Include all files
493                                 foreach ($INC_POOL as $fqfn) {
494                                         require_once($fqfn);
495                                 } // END - foreach
496                         } // END - if
497
498                         // Run SQLs
499                         if ((is_array($SQLs)) && (!$dry_run)) {
500                                 // Run SQL commands
501                                 foreach ($SQLs as $sql)
502                                 {
503                                         $sql = trim($sql);
504                                         if (!empty($sql))
505                                         {
506                                                 // Do we have an "ALTER TABLE" command?
507                                                 if (substr(strtolower($sql), 0, 11) == "alter table") {
508                                                         // Analyse the alteration command
509                                                         SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
510                                                 } else {
511                                                         // Run regular SQL command
512                                                         $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
513                                                 }
514                                         }
515                                 }
516                         } elseif (GET_EXT_VERSION("sql_patches") == "") {
517                                 // Remove SQLs if extension is not installed
518                                 $SQLs = array();
519                         }
520                 }
521
522                 if (!$dry_run)
523                 {
524                         // In normal mode insert task and update extension's version...
525                         $ext_subj = "[UPDATE-".$ext."-".$EXT_VERSION.":] ".ADMIN_UPDATE_EXT_SUBJ;
526
527                         // Create task
528                         CREATE_EXTENSION_UPDATE_TASK(GET_ADMIN_ID(get_session('admin_login')), $ext_subj, addslashes($NOTES));
529
530                         // Update extension's version
531                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
532                          array($EXT_VERSION, $ext), __FILE__, __LINE__);
533
534                         // Update cache
535                         if (EXT_IS_ACTIVE("cache")) {
536                                 if ($cacheInstance->cache_file("extensions", true) == true) $cacheInstance->cache_destroy();
537                                 if ($cacheInstance->cache_file("config", true) == true)     $cacheInstance->cache_destroy();
538                                 if ($cacheInstance->cache_file("mod_reg", true) == true)    $cacheInstance->cache_destroy();
539                         } // END - if
540
541                         // Remove array
542                         unset($SQLs);
543                 } else {
544                         // In "dry-run" mode return array with SQL commands
545                         return $SQLs;
546                 }
547         }
548 }
549 //
550 function EXTENSION_VERBOSE_TABLE($SQLs, $title = ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed = "", $switch = false, $WIDTH = "480") {
551         global $_CONFIG;
552
553         $S = false; $SW = 2; $i = 1;
554         $OUT = "";
555         if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
556                 $OUT  = "<DIV align=\"center\">
557 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"".$WIDTH."\" align=\"center\"".$dashed.">
558 <TR>
559   <TD colspan=\"2\" align=\"center\" class=\"admin_title bottom2\" height=\"24\">
560     <STRONG>".$title.":</STRONG>
561   </TD>
562 </TR>\n";
563                 foreach ($SQLs as $idx => $sql) {
564                         $sql = trim($sql);
565                         if (!empty($sql)) {
566                                 $S = true;
567                                 $OUT .= "<TR>
568   <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 2px\" width=\"30\">".$i.".</TD>
569   <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 5px; padding-right: 5px\">
570     ".$sql."
571   </TD>
572 </TR>\n";
573                                 if ($switch) $SW = 3 - $SW;
574                                 $i++;
575                         }
576                 }
577         }
578
579         if ((!$S) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
580                 // No addional SQL commands to run
581                 $OUT .= "<TR>
582   <TD colspan=\"2\" align=\"center\" class=\"switch_sw2 bottom2\" height=\"24\">
583     <FONT class=\"admin_note\">".ADMIN_NO_ADDIONAL_SQLS."</FONT>
584   </TD>
585 </TR>\n";
586         }
587
588         if (!empty($OUT)) {
589                 // Add missing close-table tag
590                 $OUT .= "</TABLE>
591 </DIV>\n";
592         }
593
594         // Return output
595         return $OUT;
596 }
597 // Get extension name from id
598 function GET_EXT_NAME ($id) {
599         $ret = "";
600         global $cacheArray, $_CONFIG;
601         if (!empty($cacheArray['extensions']['ext_name'][$id])) {
602                 // Load from cache
603                 $ret = $cacheArray['extensions']['ext_name'][$id];
604
605                 // Count cache hits
606                 if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
607         } elseif (!EXT_IS_ACTIVE("cache")) {
608                 // Load from database
609                 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
610                  array(bigintval($id)), __FILE__, __LINE__);
611                 list($ret) = SQL_FETCHROW($result);
612                 SQL_FREERESULT($result);
613         }
614         return $ret;
615 }
616 // Get extension id from name
617 function GET_EXT_ID($name) {
618         $ret = 0;
619         global $cacheArray, $_CONFIG;
620         if (isset($cacheArray['extensions']['ext_id'][$name])) {
621                 // Load from cache
622                 $ret = $cacheArray['extensions']['ext_id'][$name];
623
624                 // Count cache hits
625                 if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
626         } elseif (!EXT_IS_ACTIVE("cache")) {
627                 // Load from database
628                 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
629                  array($name), __FILE__, __LINE__);
630                 list($ret) = SQL_FETCHROW($result);
631                 SQL_FREERESULT($result);
632         }
633
634         // Return value
635         return $ret;
636 }
637 // Activate given extension
638 function ACTIVATE_EXTENSION($ext_name) {
639         // Activate the extension
640         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='Y' 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), "activate");
647         } // END - if
648 }
649 // Checks wether the extension is older than given
650 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
651         // Get current extension version
652         $currVersion = GET_EXT_VERSION($ext_name);
653
654         // Remove all dots from both versions
655         $currVersion = str_replace(".", "", $currVersion);
656         $ext_ver = str_replace(".", "", $ext_ver);
657
658         // Now compare both and return the result
659         return ($currVersion < $ext_ver);
660 }
661
662 //
663 ?>