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