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