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