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