Refback will be payed now (user cannot setup currently)
[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
190                                         // Remove array
191                                         unset($INC_POOL);
192                                 } // END - if
193
194                                 // Register extension
195                                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_lang_file, ext_active, ext_version) VALUES ('%s','%s','%s','%s')",
196                                  array($ext_name, $EXT_LANG_PREFIX, $EXT_ALWAYS_ACTIVE, $EXT_VERSION), __FILE__, __LINE__);
197
198                                 // Update task management
199                                 ADMIN_SOLVE_TASK($id);
200
201                                 // @TODO This causes the whole (!) menu cache being purged
202                                 CACHE_PURGE_ADMIN_MENU();
203
204                                 // In normal mode return a true on success
205                                 $ret = true;
206
207                                 // Remove SQLs
208                                 unset($SQLs);
209                         } else {
210                                 // Rewrite SQL command to keep { and } inside
211                                 foreach ($SQLs as $key => $sql) {
212                                         $sql = str_replace('{', "&#123;", str_replace('}', "&#125;", $sql));
213                                         $SQLs[$key] = $sql;
214                                 } // END - foreach
215
216                                 // In  "dry-run" mode return array with all SQL commands
217                                 $ret = $SQLs;
218
219                                 // Remove all SQL commands
220                                 unset($SQLs);
221                         }
222                 } else {
223                         // No, an error occurs while registering extension :-(
224                         $ret = false;
225                 }
226         } elseif (($id > 0) && (!empty($ext_name))) {
227                 // Remove task from system when id and extension's name is valid
228                 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND status='NEW' LIMIT 1",
229                  array(bigintval($id)), __FILE__, __LINE__);
230         }
231
232         // Is this the sql_patches?
233         //* DEBUG: */ echo __LINE__.":{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
234         if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
235                 // Then redirect to logout
236                 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
237                 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
238         } // END - if
239
240         // Return status code
241         return $ret;
242 }
243 //
244 function EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE) {
245         global $cacheInstance, $_CONFIG;
246         $SQLs = array();
247
248         // By default no SQL has been executed
249         $sqlRan = false;
250
251         // This shall never do a non-admin user!
252         if (!IS_ADMIN()) return false;
253
254         // Get extension's name
255         $ext_name = GET_EXT_NAME($id);
256         if (empty($ext_name)) return false;
257
258         // Load extension in detected mode
259         //* DEBUG: */ echo __FUNCTION__.":ext_name[{$id}]={$ext_name}<br />\n";
260         $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
261         if (FILE_READABLE($file)) {
262                 // Load the include
263                 require($file);
264         } // END - if
265
266         //* DEBUG: */ echo __FUNCTION__.":SQLs::count=".count($SQLs)."<br />\n";
267         if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
268                 // Run SQL commands...
269                 foreach ($SQLs as $sql) {
270                         // Trim spaces away which we don't need
271                         $sql = trim($sql);
272
273                         // Is there still an SQL query?
274                         if (!empty($sql)) {
275                                 // Do we have an "ALTER TABLE" command?
276                                 //* DEBUG: */ echo __FUNCTION__.":SQL={$SQL}<br />\n";
277                                 if (substr(strtolower($sql), 0, 11) == "alter table") {
278                                         // Analyse the alteration command
279                                         SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
280                                 } else {
281                                         // Run regular SQL command
282                                         $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
283                                 }
284
285                                 // An SQL has been executed
286                                 $sqlRan = true;
287                         } // END - if
288                 } // END - foreach
289
290                 // Removal mode?
291                 if ($EXT_LOAD_MODE == "remove") {
292                         // Delete this extension (remember to remove it from your server *before* you click on welcome!
293                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
294                          array($id), __FILE__, __LINE__);
295                 } // END - if
296
297                 //* DEBUG: */ echo __FUNCTION__.":mode={$EXT_LOAD_MODE}<br />\n";
298
299                 // Remove cache file(s) if extension is active
300                 if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1) && ($EXT_LOAD_MODE == "remove")) || ($sqlRan === true))) {
301                         //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
302                         // Remove cache files
303                         if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
304                         if ($cacheInstance->cache_file("mod_reg", true))    $cacheInstance->cache_destroy();
305                         if ($cacheInstance->cache_file("config", true))     $cacheInstance->cache_destroy();
306
307                         // @TODO This causes the whole (!) menu cache being purged
308                         CACHE_PURGE_ADMIN_MENU();
309                 } // END - if
310
311                 // Is this the sql_patches?
312                 //* DEBUG: */ echo __LINE__.": {$id}/{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
313                 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove"))) {
314                         // Then redirect to logout
315                         //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
316                         LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
317                 } // END - if
318         } // END - if
319 }
320 // Check if given extension is active
321 function EXT_IS_ACTIVE ($ext_name) {
322         global $cacheArray, $_CONFIG;
323
324         // Extensions are all inactive during installation
325         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
326
327         // Not active is the default
328         $active = "N";
329
330         // Check cache
331         if (!empty($cacheArray['extensions']['ext_active'][$ext_name])) {
332                 // Load from cache
333                 //* DEBUG: */ echo "CACHE! ext_name={$ext_name}<br />\n";
334                 $active = $cacheArray['extensions']['ext_active'][$ext_name];
335
336                 // Count cache hits
337                 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++;
338         } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
339                 //* DEBUG: */ echo "DB! ext_name={$ext_name}<br />\n";
340                 // Load from database
341                 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
342                  array($ext_name), __FILE__, __LINE__);
343                 if (SQL_NUMROWS($result) == 0) {
344                         // Extension was not found!
345                         return false;
346                 }
347                 list($active) = SQL_FETCHROW($result);
348                 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
349                 SQL_FREERESULT($result);
350
351                 // Write cache array
352                 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
353         } else {
354                 // Extension not active!
355                 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
356                 $cacheArray['extensions']['ext_active'][$ext_name] = "N";
357         }
358
359         // Create FQFN for extension file
360         $inc = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
361
362         // Debug message
363         //DEBUG_LOG(__FUNCTION__.": ext_name={$ext_name},active={$active}");
364
365         // Is this extension activated? (For admins we always have active extensions...)
366         return ($active == "Y");
367 }
368 // Get version from extensions
369 function GET_EXT_VERSION ($ext_name) {
370         global $cacheArray, $_CONFIG, $cacheInstance;
371         $ret = false;
372
373         // Extensions are all inactive during installation
374         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
375         //* DEBUG: */ echo __FUNCTION__.": ext_name={$ext_name}<br />\n";
376
377         // Is the cache written?
378         if (!empty($cacheArray['extensions']['ext_version'][$ext_name])) {
379                 // Load data from cache
380                 //* DEBUG: */ echo __FUNCTION__.": CACHE!<br />\n";
381                 $ret = $cacheArray['extensions']['ext_version'][$ext_name];
382
383                 // Count cache hits
384                 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++; else $_CONFIG['cache_hits'] = 1;
385         } elseif (!is_object($cacheInstance)) {
386                 // Load from database
387                 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
388                  array($ext_name), __FILE__, __LINE__);
389                 list($ret) = SQL_FETCHROW($result);
390                 SQL_FREERESULT($result);
391
392                 // Set cache
393                 $cacheArray['extensions']['ext_version'][$ext_name] = $ret;
394         }
395
396         // Return result
397         //* DEBUG: */ echo __FUNCTION__.": ret={$ret}<br />\n";
398         return $ret;
399 }
400 //
401 function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
402 {
403         // This shall never do a non-admin user!
404         global $cacheInstance, $_CONFIG, $NOTES;
405
406         // Init arrays
407         $SQLs = array(); $INC_POOL = array();
408
409         // Only admins are allowed to update extensions
410         if ((!IS_ADMIN()) || (empty($ext))) return false;
411
412         // Load extension in update mode
413         $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = ""; $NOTES = "";
414
415         // Load extension file
416         include(sprintf("%sinc/extensions/%s", PATH, $file));
417
418         if (!empty($EXT_UPDATE_DEPENDS)) {
419                 // Update another extension first!
420                 $test = EXTENSION_UPDATE(("ext-".$EXT_UPDATE_DEPENDS.".php"), $EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
421         }
422
423         // Check if version is updated
424         if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY)))
425         {
426                 // Search for starting point
427                 $start = array_search($EXT_VER, $EXT_VER_HISTORY);
428                 $NOTES = "";
429
430                 // And load SQL queries in order of version history
431                 for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++)
432                 {
433                         // Remove old SQLs array to prevent possible bugs
434                         if (!$dry_run) { unset($SQLs); $SQLs = array(); }
435
436                         // Set version
437                         $EXT_VER = $EXT_VER_HISTORY[$idx];
438
439                         // Include again...
440                         include(PATH."inc/extensions/".$file);
441
442                         // Add notes
443                         if ($_CONFIG['verbose_sql'] == "Y")
444                         {
445                                 $EXT_VER = $EXT_VER_HISTORY[$idx];
446                                 if (!empty($UPDATE_NOTES))
447                                 {
448                                         // Update notes found
449                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
450                                         $UPDATE_NOTES = "";
451                                 }
452                                  elseif ($EXT_VER == "0.0")
453                                 {
454                                         // Initial release
455                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
456                                 }
457                                  else
458                                 {
459                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
460                                 }
461                         }
462
463                         // In real-mode execute any existing includes
464                         if ((!$dry_run) && (count($INC_POOL) > 0)) {
465                                 // Include all files
466                                 foreach ($INC_POOL as $fqfn) {
467                                         require_once($fqfn);
468                                 } // END - foreach
469                         } // END - if
470
471                         // Run SQLs
472                         if ((is_array($SQLs)) && (!$dry_run)) {
473                                 // Run SQL commands
474                                 foreach ($SQLs as $sql)
475                                 {
476                                         $sql = trim($sql);
477                                         if (!empty($sql))
478                                         {
479                                                 // Do we have an "ALTER TABLE" command?
480                                                 if (substr(strtolower($sql), 0, 11) == "alter table") {
481                                                         // Analyse the alteration command
482                                                         SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
483                                                 } else {
484                                                         // Run regular SQL command
485                                                         $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
486                                                 }
487                                         }
488                                 }
489                         } elseif (GET_EXT_VERSION("sql_patches") == "") {
490                                 // Remove SQLs if extension is not installed
491                                 $SQLs = array();
492                         }
493                 }
494
495                 if (!$dry_run)
496                 {
497                         // In normal mode insert task and update extension's version...
498                         $ext_subj = "[UPDATE-".$ext."-".$EXT_VERSION.":] ".ADMIN_UPDATE_EXT_SUBJ;
499
500                         // Create task
501                         CREATE_EXTENSION_UPDATE_TASK(GET_ADMIN_ID(get_session('admin_login')), $ext_subj, addslashes($NOTES));
502
503                         // Update extension's version
504                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
505                          array($EXT_VERSION, $ext), __FILE__, __LINE__);
506
507                         // Update cache
508                         if (EXT_IS_ACTIVE("cache")) {
509                                 if ($cacheInstance->cache_file("extensions", true) == true) $cacheInstance->cache_destroy();
510                                 if ($cacheInstance->cache_file("config", true) == true)     $cacheInstance->cache_destroy();
511                                 if ($cacheInstance->cache_file("mod_reg", true) == true)    $cacheInstance->cache_destroy();
512                         } // END - if
513
514                         // Remove array
515                         unset($SQLs);
516                 } else {
517                         // In "dry-run" mode return array with SQL commands
518                         return $SQLs;
519                 }
520         }
521 }
522 //
523 function EXTENSION_VERBOSE_TABLE($SQLs, $title = ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed = "", $switch = false, $WIDTH = "480") {
524         global $_CONFIG;
525
526         $S = false; $SW = 2; $i = 1;
527         $OUT = "";
528         if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
529                 $OUT  = "<DIV align=\"center\">
530 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"".$WIDTH."\" align=\"center\"".$dashed.">
531 <TR>
532   <TD colspan=\"2\" align=\"center\" class=\"admin_title bottom2\" height=\"24\">
533     <STRONG>".$title.":</STRONG>
534   </TD>
535 </TR>\n";
536                 foreach ($SQLs as $idx => $sql) {
537                         $sql = trim($sql);
538                         if (!empty($sql)) {
539                                 $S = true;
540                                 $OUT .= "<TR>
541   <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 2px\" width=\"30\">".$i.".</TD>
542   <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 5px; padding-right: 5px\">
543     ".$sql."
544   </TD>
545 </TR>\n";
546                                 if ($switch) $SW = 3 - $SW;
547                                 $i++;
548                         }
549                 }
550         }
551
552         if ((!$S) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
553                 // No addional SQL commands to run
554                 $OUT .= "<TR>
555   <TD colspan=\"2\" align=\"center\" class=\"switch_sw2 bottom2\" height=\"24\">
556     <FONT class=\"admin_note\">".ADMIN_NO_ADDIONAL_SQLS."</FONT>
557   </TD>
558 </TR>\n";
559         }
560
561         if (!empty($OUT)) {
562                 // Add missing close-table tag
563                 $OUT .= "</TABLE>
564 </DIV>\n";
565         }
566
567         // Return output
568         return $OUT;
569 }
570 // Get extension name from id
571 function GET_EXT_NAME ($id) {
572         $ret = "";
573         global $cacheArray, $_CONFIG;
574         if (!empty($cacheArray['extensions']['ext_name'][$id])) {
575                 // Load from cache
576                 $ret = $cacheArray['extensions']['ext_name'][$id];
577
578                 // Count cache hits
579                 $_CONFIG['cache_hits']++;
580         } elseif (!EXT_IS_ACTIVE("cache")) {
581                 // Load from database
582                 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
583                  array(bigintval($id)), __FILE__, __LINE__);
584                 list($ret) = SQL_FETCHROW($result);
585                 SQL_FREERESULT($result);
586         }
587         return $ret;
588 }
589 // Get extension id from name
590 function GET_EXT_ID($name) {
591         $ret = 0;
592         global $cacheArray, $_CONFIG;
593         if (isset($cacheArray['extensions']['ext_id'][$name])) {
594                 // Load from cache
595                 $ret = $cacheArray['extensions']['ext_id'][$name];
596
597                 // Count cache hits
598                 $_CONFIG['cache_hits']++;
599         } elseif (!EXT_IS_ACTIVE("cache")) {
600                 // Load from database
601                 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
602                  array($name), __FILE__, __LINE__);
603                 list($ret) = SQL_FETCHROW($result);
604                 SQL_FREERESULT($result);
605         }
606
607         // Return value
608         return $ret;
609 }
610 // Activate given extension
611 function ACTIVATE_EXTENSION($ext_name) {
612         // Activate the extension
613         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
614                 array($ext_name), __FILE__, __LINE__);
615
616         // Extension has been activated?
617         if (SQL_AFFECTEDROWS() == 1) {
618                 // Then run all queries
619                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
620         } // END - if
621 }
622 // Checks wether the extension is older than given
623 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
624         // Get current extension version
625         $currVersion = GET_EXT_VERSION($ext_name);
626
627         // Remove all dots from both versions
628         $currVersion = str_replace(".", "", $currVersion);
629         $ext_ver = str_replace(".", "", $ext_ver);
630
631         // Now compare both and return the result
632         return ($currVersion < $ext_ver);
633 }
634
635 //
636 ?>