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