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