Some global variables rewritten, a lot more language constants rewritten to getMessage()
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
42         require($INC);
43 }
44
45 // Load the extension and maybe found language and function files.
46 function LOAD_EXTENSION ($ext_name, $EXT_LOAD_MODE = "", $EXT_VER = "", $dry_run = false) {
47         // Init array
48         INIT_INC_POOL();
49
50         // Set current extension name
51         EXT_SET_CURR_NAME($ext_name);
52
53         // Init EXT_UPDATE_DEPENDS
54         EXT_INIT_UPDATE_DEPENDS();
55
56         // Init current extension name list
57         INIT_EXT_SQLS();
58
59         // Is the extension already loaded?
60         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Loading extension {$ext_name}, mode={$EXT_LOAD_MODE}, ver={$EXT_VER}.");
61         if ((isset($GLOBALS['ext_loaded']['ext'][$ext_name])) && (empty($EXT_LOAD_MODE))) {
62                 // Debug message
63                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
64                 return false;
65         } // END - if
66
67         // Construct include filename and FQFN for extension file
68         $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
69         $FQFN = constant('PATH') . $INC;
70
71         // Is the extension file NOT there?
72         if (!INCLUDE_READABLE($INC)) {
73                 // Debug message
74                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s not found.", $ext_name));
75
76                 // Abort here
77                 return false;
78         } // END - if
79
80         // Construct FQFN for language file
81         $langInclude = sprintf("inc/language/%s_%s.php", $ext_name, GET_LANGUAGE());
82
83         // Is this include there?
84         if ((FILE_READABLE($langInclude)) && (!isset($GLOBALS['ext_loaded']['lang'][$ext_name]))) {
85                 // Then load it
86                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Language loaded.");
87                 $GLOBALS['ext_loaded']['lang'][$ext_name] = true;
88                 LOAD_INC_ONCE($langInclude);
89         } // END - if
90
91         // Construct FQFN for functions file
92         $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
93
94         // Is this include there?
95         if ((FILE_READABLE($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name]))) {
96                 // Then load it
97                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Functions loaded.");
98                 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
99                 LOAD_INC_ONCE($funcsInclude);
100         } // END - if
101
102         // Extensions are not deprecated by default
103         EXT_SET_DEPRECATED("N");
104
105         // Extensions are not always active by default
106         EXT_SET_ALWAYS_ACTIVE("N");
107
108         // Extension update notes
109         EXT_SET_UPDATE_NOTES("");
110
111         // Include the extension file
112         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Extension loaded.");
113         require($FQFN);
114
115         // Is this extension deprecated?
116         if (EXT_GET_DEPRECATED() == "Y") {
117                 // Deactivate the extension
118                 DEACTIVATE_EXTENSION($ext_name);
119
120                 // Abort here
121                 return false;
122         } // END - if
123
124         // Mark it as loaded in normal mode
125         if (empty($EXT_LOAD_MODE)) {
126                 // Mark it now...
127                 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
128         } // END - if
129
130         // All fine!
131         return true;
132 }
133
134 // Registeres an extension and possible update depencies
135 function REGISTER_EXTENSION ($ext_name, $task_id, $dry_run = false, $logout = true) {
136         // This shall never do a non-admin user!
137         if (!IS_ADMIN()) return false;
138
139         // Is this extension already installed?
140         if (EXT_IS_ACTIVE($ext_name)) return false;
141
142         // Set current extension name
143         EXT_SET_CURR_NAME($ext_name);
144
145         // Init EXT_UPDATE_DEPENDS
146         EXT_INIT_UPDATE_DEPENDS();
147
148         // Is the task id zero? Then we need to auto-fix it here
149         if ($task_id == 0) {
150                 // Try to find the task
151                 $task_id = DETERMINE_EXTENSION_TASK_ID(EXT_GET_CURR_NAME());
152
153                 // Still zero and not in dry-run?
154                 if (($task_id == 0) && (!$dry_run)) {
155                         // Then request a bug report
156                         debug_report_bug(sprintf("%s: task_id is still zero after DETERMINE_EXTENSION_TASK_ID(%s)",
157                                 __FUNCTION__,
158                                 EXT_GET_CURR_NAME()
159                         ));
160                 } // END - if
161         } // END - if
162
163         // Init queries and notes
164         INIT_EXT_SQLS();
165         EXT_INIT_NOTES();
166
167         // Init variables
168         $ret = false;
169         $test = false;
170         INIT_INC_POOL();
171
172         // By default we have no failures
173         EXT_SET_REPORTS_FAILURE(false);
174
175         // Does this extension exists?
176         //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME()."<br />\n";
177         if (LOAD_EXTENSION(EXT_GET_CURR_NAME(), "register", "", $dry_run)) {
178                 // And run possible updates
179                 //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME()."<br />\n";
180                 $history = EXT_GET_VER_HISTORY();
181                 foreach ($history as $ver) {
182                         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name={EXT_GET_CURR_NAME()}, ext_ver={$ver}");
183                         // Load extension in update mode
184                         LOAD_EXTENSION(EXT_GET_CURR_NAME(), "update", $ver, $dry_run);
185
186                         // Add update notes to our output
187                         ADD_EXTENSION_NOTES($ver);
188                 } // END - foreach
189
190                 // Does this extension depends on an outstanding update of another update?
191                 for ($dmy = EXT_GET_UPDATE_ITERATOR(); EXT_GET_UPDATE_ITERATOR() < EXT_COUNT_UPDATE_DEPENDS();) {
192                         // Get next update
193                         $ext_update = EXT_GET_ITERATOR_UPDATE_DEPENDS();
194
195                         // Increment here to avoid endless loop
196                         EXT_INCREMENT_UPDATE_INTERATOR();
197
198                         // Check for required file
199                         if (LOAD_EXTENSION($ext_update, "register", "", $dry_run)) {
200                                 // If versions mismatch update extension first
201                                 $ext_ver = GET_EXT_VERSION($ext_update);
202
203                                 // Extension version set? If empty the extension is not registered
204                                 if (empty($ext_ver)) {
205                                         // Extension not registered so far so first load task's ID...
206                                         $task = DETERMINE_EXTENSION_TASK_ID($ext_update);
207
208                                         // Entry found?
209                                         if ($task > 0) {
210                                                 // Try to register the extension
211                                                 //* DEBUG: */ print __LINE__.sprintf(":ext_update=%s,task=%s<br />\n", $ext_update, $task);
212                                                 $test = REGISTER_EXTENSION($ext_update, $task, $dry_run, false);
213                                                 //* DEBUG: */ var_dump($test);
214                                         } // END - if
215                                 } elseif ($ext_ver != EXT_GET_VERSION()) {
216                                         // Ok, update this extension now
217                                         EXTENSION_UPDATE($ext_update, $ext_ver, $dry_run);
218
219                                         // All okay!
220                                         $test = true;
221                                 } else {
222                                         // Nothing to register / update before...
223                                         $test = true;
224                                 }
225                         } else {
226                                 // Required file for update does not exists!
227                                 $test = true;
228                                 // But this is fine for the first time...
229                         }
230
231                         // Restore the current extension name
232                         EXT_SET_CURR_NAME($ext_name);
233                 } // END - for
234
235                 // Is there no update?
236                 if (EXT_COUNT_UPDATE_DEPENDS(EXT_GET_CURR_NAME()) == 0) {
237                         // Then test is passed!
238                         $test = true;
239                 } // END - if
240
241                 // Switch back to register mode
242                 $EXT_LOAD_MODE = "register";
243
244                 // Remains true if extension registration reports no failures
245                 //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME();
246                 //* DEBUG: */ var_dump($test);
247                 $test = (($test === true) && (EXT_GET_REPORTS_FAILURE() === false));
248                 //* DEBUG: */ var_dump($test);
249
250                 // Does everthing before wents ok?
251                 if ($test === true) {
252                         // "Dry-run-mode" activated?
253                         if ((!$dry_run) && (!EXT_IS_ON_REMOVAL_LIST())) {
254                                 //* DEBUG: */ print __LINE__.": ext_name=".EXT_GET_CURR_NAME()."<br />\n";
255                                 // Init SQLs and transfer ext->generic
256                                 INIT_SQLS();
257                                 SET_SQLS(GET_EXT_SQLS());
258
259                                 // Run installation pre-installation filters
260                                 runFilterChain('pre_extension_installed', array('dry_run' => $dry_run));
261
262                                 // Register extension
263                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_extensions` (ext_name, ext_active, ext_version) VALUES ('%s','%s','%s')",
264                                         array(EXT_GET_CURR_NAME(), EXT_GET_ALWAYS_ACTIVE(), EXT_GET_VERSION()), __FILE__, __LINE__);
265
266                                 // Remove cache file(s) if extension is active
267                                 runFilterChain('post_extension_installed', array('ext_name' => EXT_GET_CURR_NAME(), 'task_id' => $task_id));
268
269                                 // In normal mode return a true on success
270                                 $ret = true;
271                         } elseif ($dry_run) {
272                                 // Rewrite SQL command to keep { and } inside for dry-run
273                                 foreach (GET_SQLS() as $key => $sql) {
274                                         $sql = str_replace('{', "&#123;", str_replace('}', "&#125;", $sql));
275                                         SET_SQL_KEY($key, $sql);
276                                 } // END - foreach
277
278                                 // In  "dry-run" mode return array with all SQL commands
279                                 $ret = GET_SQLS();
280
281                                 // Remove all SQL commands
282                                 UNSET_SQLS();
283                         } else {
284                                 // Extension has been removed for updates, so all is fine!
285                                 $ret = true;
286                         }
287                 } else {
288                         // No, an error occurs while registering extension :-(
289                         //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME()."<br />\n";
290                         $ret = false;
291                 }
292         } elseif (($task_id > 0) && (EXT_GET_CURR_NAME() != "")) {
293                 // Remove task from system when id and extension's name is valid
294                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE id=%s AND `status`='NEW' LIMIT 1",
295                         array(bigintval($task_id)), __FILE__, __LINE__);
296         }
297
298         // Is this the sql_patches?
299         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":{EXT_GET_CURR_NAME()}/{$EXT_LOAD_MODE}");
300         if ((EXT_GET_CURR_NAME() == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
301                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
302                 if ($logout === true) {
303                         // Then redirect to logout
304                         LOAD_URL("modules.php?module=admin&amp;logout=1&amp;".$EXT_LOAD_MODE."=sql_patches");
305                 } else {
306                         // Add temporary filter
307                         REGISTER_FILTER('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
308                         $GLOBALS['ext_load_mode'] = $EXT_LOAD_MODE;
309                 }
310         } // END - if
311
312         // Return status code
313         return $ret;
314 }
315
316 // Run SQL queries for given extension id
317 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
318 function EXTENSION_RUN_SQLS ($ext_id, $load_mode) {
319         // This shall never do a non-admin user!
320         if (!IS_ADMIN()) return false;
321
322         // Get extension's name
323         $ext_name = GET_EXT_NAME($ext_id);
324
325         // If it is not set then maybe there is no extension for that ID number
326         if ($ext_name == "") return false;
327
328         // Set current SQL name
329         EXT_SET_CURR_NAME($ext_name);
330
331         // Init array
332         INIT_EXT_SQLS();
333
334         // By default no SQL has been executed
335         $sqlRan = false;
336
337         // Load extension in detected mode
338         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]={EXT_GET_CURR_NAME()}");
339         LOAD_EXTENSION(EXT_GET_CURR_NAME(), $load_mode, "", false);
340
341         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".COUNT_SQLS()."");
342         if ((IS_SQLS_VALID() && (COUNT_SQLS() > 0))) {
343                 // Run SQL commands...
344                 runFilterChain('run_sqls');
345
346                 // Removal mode?
347                 if ($load_mode == "remove") {
348                         // Delete this extension (remember to remove it from your server *before* you click on welcome!
349                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
350                                 array(EXT_GET_CURR_NAME()), __FILE__, __LINE__);
351                 } // END - if
352         } // END - if
353
354         // Remove cache file(s) if extension is active
355         if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == "activate") || ($load_mode == "deactivate"))) {
356                 // Run filters
357                 runFilterChain('post_extension_run_sql', EXT_GET_CURR_NAME());
358         } // END - if
359
360         // Is this the sql_patches?
361         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": {$ext_id}/{EXT_GET_CURR_NAME()}/{$load_mode}");
362         if ((EXT_GET_CURR_NAME() == "sql_patches") && (($load_mode == "register") || ($load_mode == "remove"))) {
363                 // Then redirect to logout
364                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
365                 LOAD_URL("modules.php?module=admin&amp;logout=1&amp;".$load_mode."=sql_patches");
366         } // END - if
367 }
368
369 // Check if given extension is active
370 function EXT_IS_ACTIVE ($ext_name) {
371         // Extensions are all inactive during installation
372         if ((!isInstalled()) || (isInstalling()) || (empty($ext_name))) return false;
373
374         // Not active is the default
375         $active = "N";
376
377         // Check cache
378         if (isset($GLOBALS['cache_array']['extensions']['ext_active'][$ext_name])) {
379                 // Load from cache
380                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
381                 $active = $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name];
382
383                 // Count cache hits
384                 incrementConfigEntry('cache_hits');
385         } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
386                 // @TODO Extension is loaded, what next?
387                 mxchange_die("LOADED:$ext_name");
388         } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
389                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
390                 // Load from database
391                 $result = SQL_QUERY_ESC("SELECT ext_active FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
392                         array($ext_name), __FILE__, __LINE__);
393
394                 // Entry found?
395                 if (SQL_NUMROWS($result) == 1) {
396                         // Load entry
397                         list($active) = SQL_FETCHROW($result);
398                 } // END - if
399
400                 // Free result
401                 SQL_FREERESULT($result);
402
403                 // Write cache array
404                 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
405                 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = $active;
406         } else {
407                 // Extension not active!
408                 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
409                 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = "N";
410         }
411
412         // Debug message
413         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
414
415         // Is this extension activated? (For admins we always have active extensions...)
416         return ($active == "Y");
417 }
418 // Get version from extensions
419 function GET_EXT_VERSION ($ext_name) {
420         // By default no extension is found
421         $ext_ver = false;
422
423         // Extensions are all inactive during installation
424         if ((!isInstalled()) || (isInstalling())) return "";
425         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
426
427         // Is the cache written?
428         if (isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name])) {
429                 // Load data from cache
430                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
431                 $ext_ver = $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name];
432
433                 // Count cache hits
434                 incrementConfigEntry('cache_hits');
435         } elseif (!isCacheInstanceValid()) {
436                 // Load from database
437                 $result = SQL_QUERY_ESC("SELECT ext_version FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
438                         array($ext_name), __FILE__, __LINE__);
439                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": DB - ".SQL_NUMROWS($result)."");
440
441                 // Is the extension there?
442                 if (SQL_NUMROWS($result) == 1) {
443                         // Load entry
444                         list($ext_ver) = SQL_FETCHROW($result);
445                 } // END - if
446
447                 // Free result
448                 SQL_FREERESULT($result);
449
450                 // Set cache
451                 $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name] = $ext_ver;
452         }
453
454         // Return result
455         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
456         return $ext_ver;
457 }
458
459 // Updates a given extension with current extension version to latest version
460 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
461         // Only admins are allowed to update extensions
462         if ((!IS_ADMIN()) || (empty($ext_name))) return false;
463
464         // Set current SQL name
465         EXT_SET_CURR_NAME($ext_name);
466
467         // Init arrays
468         INIT_EXT_SQLS();
469         EXT_INIT_NOTES();
470         INIT_INC_POOL();
471
472         // Load extension in test mode
473         LOAD_EXTENSION($ext_name, "test", $ext_ver, $dry_run);
474
475         // Save version history
476         $history = EXT_GET_VER_HISTORY();
477
478         // Remove old SQLs array to prevent possible bugs
479         INIT_EXT_SQLS();
480
481         // Check if version is updated
482         if (((EXT_GET_VERSION() != $ext_ver) || ($dry_run)) && (is_array($history))) {
483                 // Search for starting point
484                 $start = array_search($ext_ver, $history);
485
486                 // And load SQL queries in order of version history
487                 for ($idx = ($start + 1); $idx < count($history); $idx++) {
488                         // Set extension version
489                         $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()] = $history[$idx];
490
491                         // Load again...
492                         LOAD_EXTENSION(EXT_GET_CURR_NAME(), "update", $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], $dry_run);
493
494                         if (EXT_GET_UPDATE_DEPENDS() != "") {
495                                 // Backup current SQL queries
496                                 $GLOBALS['cache_array']['update_sqls'][EXT_GET_CURR_NAME()] = GET_SQLS();
497
498                                 // Is the extension there?
499                                 if (GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()) != "") {
500                                         // Update another extension first!
501                                         $test = EXTENSION_UPDATE(EXT_GET_UPDATE_DEPENDS(), GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()), $dry_run);
502                                 } else {
503                                         // Register new extension
504                                         $test = REGISTER_EXTENSION(EXT_GET_UPDATE_DEPENDS(), 0, $dry_run, false);
505                                 }
506
507                                 // Restore previous SQL queries
508                                 SET_SQLS($GLOBALS['cache_array']['update_sqls'][EXT_GET_CURR_NAME()]);
509                                 unset($GLOBALS['cache_array']['update_sqls'][EXT_GET_CURR_NAME()]);
510                         } // END - if
511
512                         // Add notes
513                         ADD_EXTENSION_NOTES($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
514                 } // END - for
515
516                 // In real-mode execute any existing includes
517                 if (!$dry_run) {
518                         $GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()] = GET_INC_POOL();
519                         runFilterChain('load_includes');
520                         SET_INC_POOL($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
521                         unset($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
522                 } // END - if
523
524                 // Run SQLs
525                 runFilterChain('run_sqls', array('dry_run' => $dry_run));
526
527                 if (!$dry_run) {
528                         // Create task
529                         CREATE_EXTENSION_UPDATE_TASK(GET_CURRENT_ADMIN_ID(), EXT_GET_CURR_NAME(), $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], SQL_ESCAPE(EXT_GET_NOTES(EXT_GET_NOTES())));
530
531                         // Update extension's version
532                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
533                                 array($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], EXT_GET_CURR_NAME()), __FILE__, __LINE__);
534
535                         // Remove arrays
536                         UNSET_SQLS();
537                         unset($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
538
539                         // Run filters on success extension update
540                         runFilterChain('extension_update', EXT_GET_CURR_NAME());
541                 } // END - if
542         } // END - if
543 }
544
545 // Output verbose SQL table for extension
546 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = "", $dashed = "", $switch = false, $width = "100%") {
547         // Empty title?
548         if (empty($title)) {
549                 // Then fix it to default
550                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
551         } // END - if
552
553         // Are there some queries in $queries?
554         if (count($queries) > 0) {
555                 // Then use them instead!
556                 SET_SQLS($queries);
557         } // END - if
558
559         // Init variables
560         $SW = 2; $i = 1;
561         $OUT = "";
562
563         // Do we have queries?
564         if ((IS_SQLS_VALID()) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
565                 foreach (GET_SQLS() as $idx => $sql) {
566                         // Trim out spaces
567                         $sql = trim($sql);
568
569                         // Output command if set
570                         if (!empty($sql)) {
571                                 // Prepare output for template
572                                 $content = array(
573                                         'sw'  => $SW,
574                                         'i'   => $i,
575                                         'sql' => $sql
576                                 );
577
578                                 // Load row template
579                                 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
580
581                                 // Switch color and count up
582                                 $SW = 3 - $SW;
583                                 $i++;
584                         } // END - if
585                 } // END - foreach
586
587                 // Prepare content for template
588                 $content = array(
589                         'width'  => $width,
590                         'dashed' => $dashed,
591                         'title'  => $title,
592                         'out'    => $OUT
593                 );
594
595                 // Load main template
596                 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
597         } elseif ((GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
598                 // No addional SQL commands to run
599                 $OUT = LOAD_TEMPLATE("admin_settings_saved", true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
600         } // END - if
601
602         // Return output
603         return $OUT;
604 }
605
606 // Get extension name from id
607 function GET_EXT_NAME ($ext_id) {
608         // Init extension name
609         $ret = "";
610
611         // Is cache there?
612         if (isset($GLOBALS['cache_array']['extensions']['ext_name'][$ext_id])) {
613                 // Load from cache
614                 $ret = $GLOBALS['cache_array']['extensions']['ext_name'][$ext_id];
615
616                 // Count cache hits
617                 incrementConfigEntry('cache_hits');
618         } elseif (!EXT_IS_ACTIVE("cache")) {
619                 // Load from database
620                 $result = SQL_QUERY_ESC("SELECT ext_name FROM `{!_MYSQL_PREFIX!}_extensions` WHERE id=%s LIMIT 1",
621                         array(bigintval($ext_id)), __FILE__, __LINE__);
622                 list($ret) = SQL_FETCHROW($result);
623                 SQL_FREERESULT($result);
624         }
625         return $ret;
626 }
627
628 // Get extension id from name
629 function GET_EXT_ID ($ext_name) {
630         // Init ID number
631         $ret = 0;
632         if (isset($GLOBALS['cache_array']['extensions']['ext_id'][$ext_name])) {
633                 // Load from cache
634                 $ret = $GLOBALS['cache_array']['extensions']['ext_id'][$ext_name];
635
636                 // Count cache hits
637                 incrementConfigEntry('cache_hits');
638         } elseif (!EXT_IS_ACTIVE("cache")) {
639                 // Load from database
640                 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
641                         array($ext_name), __FILE__, __LINE__);
642                 list($ret) = SQL_FETCHROW($result);
643                 SQL_FREERESULT($result);
644         }
645
646         // Return value
647         return $ret;
648 }
649
650 // Activate given extension
651 function ACTIVATE_EXTENSION ($ext_name) {
652         // Activate the extension
653         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
654                 array($ext_name), __FILE__, __LINE__);
655
656         // Extension has been activated?
657         if (SQL_AFFECTEDROWS() == 1) {
658                 // Then run all queries
659                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
660         } // END - if
661 }
662
663 // Deactivate given extension
664 function DEACTIVATE_EXTENSION($ext_name) {
665         // Activate the extension
666         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='N' WHERE ext_name='%s' LIMIT 1",
667                 array($ext_name), __FILE__, __LINE__);
668
669         // Extension has been activated?
670         if (SQL_AFFECTEDROWS() == 1) {
671                 // Then run all queries
672                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "deactivate");
673
674                 // Create new task
675                 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
676
677                 // Notify the admin
678                 SEND_ADMIN_NOTIFICATION(
679                         getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
680                         "admin_ext_deactivated",
681                         array('ext_name' => $ext_name)
682                 );
683         } // END - if
684 }
685
686 // Checks wether the extension is older than given
687 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
688         // Get current extension version
689         $currVersion = GET_EXT_VERSION($ext_name);
690
691         // Remove all dots from both versions
692         $currVersion = str_replace(".", "", $currVersion);
693         $ext_ver = str_replace(".", "", $ext_ver);
694
695         // Now compare both and return the result
696         return ($currVersion < $ext_ver);
697 }
698
699 // Creates a new task for updated extension
700 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
701         // Create subject line
702         $subject = "[UPDATE-".$ext_name."-".$ext_ver.":] {--ADMIN_UPDATE_EXT_SUBJ--}";
703
704         // Is the extension there?
705         if (GET_EXT_VERSION($ext_name) != "") {
706                 // Check if task is not there
707                 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
708                         // Task not created so it's a brand-new extension which we need to register and create a task for!
709                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created) VALUES ('%s','0','NEW','EXTENSION_UPDATE','%s','%s', UNIX_TIMESTAMP())",
710                                 array($admin_id, $subject, $notes), __FILE__, __LINE__);
711                 } // END - if
712         } // END - if
713 }
714
715 // Creates a new task for newly installed extension
716 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
717         // Not installed and do we have created a task for the admin?
718         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == "")) {
719                 // Template file
720                 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
721                         constant('PATH'),
722                         GET_LANGUAGE(),
723                         $ext
724                 );
725
726                 // Load text for task
727                 if (FILE_READABLE($tpl)) {
728                         // Load extension's own text template (HTML!)
729                         $msg = LOAD_TEMPLATE("ext_".$ext, true);
730                 } else {
731                         // Load default message
732                         $msg = LOAD_TEMPLATE("admin_new_ext", "", 0);
733                 }
734
735                 // Task not created so it's a brand-new extension which we need to register and create a task for!
736                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
737 VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
738                         array(
739                                 $admin_id,
740                                 $subject,
741                                 SQL_ESCAPE($msg),
742                         ),  __FILE__, __LINE__, true, false
743                 );
744         } // END - if
745 }
746
747 // Creates a task for automatically deactivated (deprecated) extension
748 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
749         // Create subject line
750         $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
751
752         // Not installed and do we have created a task for the admin?
753         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != "")) {
754                 // Task not created so add it
755                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
756 VALUES (0,0,'NEW','EXTENSION_DEACTIVATION','%s','%s',UNIX_TIMESTAMP())",
757                         array(
758                                 $subject,
759                                 SQL_ESCAPE(LOAD_TEMPLATE("task_ext_deactivated", true, $ext)),
760                         ),  __FILE__, __LINE__, true, false
761                 );
762         } // END - if
763 }
764
765 // Checks if the module has a menu
766 function MODULE_HAS_MENU ($mod, $forceDb = false) {
767         // All is false by default
768         $ret = false;
769
770         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mod={$mod},cache=".GET_EXT_VERSION("cache")."<br />\n";
771         if (GET_EXT_VERSION("cache") >= "0.1.2") {
772                 // Cache version is okay, so let's check the cache!
773                 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
774                         // Check module cache and count hit
775                         $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == "Y");
776                         incrementConfigEntry('cache_hits');
777                 } elseif (isset($GLOBALS['cache_array']['extensions']['ext_menu'][$mod])) {
778                         // Check cache and count hit
779                         $ret = ($GLOBALS['cache_array']['extensions']['ext_menu'][$mod] == "Y");
780                         incrementConfigEntry('cache_hits');
781                 } elseif ((IS_ADMIN()) && ($mod == "admin")) {
782                         // Admin module has always a menu!
783                         $ret = true;
784                 }
785         } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
786                 // Check database for entry
787                 $result = SQL_QUERY_ESC("SELECT has_menu FROM `{!_MYSQL_PREFIX!}_mod_reg` WHERE module='%s' LIMIT 1",
788                         array($mod), __FILE__, __LINE__);
789
790                 // Entry found?
791                 if (SQL_NUMROWS($result) == 1) {
792                         // Load "has_menu" column
793                         list($has_menu) = SQL_FETCHROW($result);
794
795                         // Fake cache... ;-)
796                         $GLOBALS['cache_array']['extensions']['ext_menu'][$mod] = $has_menu;
797
798                         // Does it have a menu?
799                         $ret = ($has_menu == "Y");
800                 } // END  - if
801
802                 // Free memory
803                 SQL_FREERESULT($result);
804         } elseif (GET_EXT_VERSION("sql_patches") == "") {
805                 // No sql_patches installed, so maybe in admin area or no admin registered?
806                 $ret = (((IS_ADMIN()) || (!isAdminRegistered())) && ($mod == "admin")); // Then there is a menu!
807         }
808
809         // Return status
810         //* DEBUG: */ var_dump($ret);
811         return $ret;
812 }
813
814 // Determines the task id for given extension
815 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
816         // Default is not found
817         $task_id = 0;
818
819         // Search for extension task's id
820         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE task_type='EXTENSION' AND subject='[%s:]' LIMIT 1",
821                 array($ext_name), __FILE__, __LINE__);
822
823         // Entry found?
824         if (SQL_NUMROWS($result) == 1) {
825                 // Task found so load task's ID and register extension...
826                 list($task_id) = SQL_FETCHROW($result);
827         } // END - if
828
829         // Free result
830         SQL_FREERESULT($result);
831
832         // Return it
833         return $task_id;
834 }
835
836 // Determines the task id for given subject
837 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
838         // Default is not found
839         $task_id = 0;
840
841         // Search for task id
842         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject LIKE '%s%%' LIMIT 1",
843                 array($subject), __FILE__, __LINE__);
844
845         // Entry found?
846         if (SQL_NUMROWS($result) == 1) {
847                 // Task found so load task's ID and register extension...
848                 list($task_id) = SQL_FETCHROW($result);
849         } // END - if
850
851         // Free result
852         SQL_FREERESULT($result);
853
854         // Return it
855         return $task_id;
856 }
857
858 // Add updates notes for given version
859 function ADD_EXTENSION_NOTES ($ver) {
860         // Init notes/content
861         $out = ""; $content = array();
862
863         // Is do we have verbose output enabled?
864         if ((getConfig('verbose_sql') == "Y") || (!EXT_IS_ACTIVE("sql_patches"))) {
865
866                 // Update notes found?
867                 if (EXT_GET_UPDATE_NOTES() != "") {
868                         // Update notes found
869                         $content = array(
870                                 'ver'   => $ver,
871                                 'notes' => EXT_GET_UPDATE_NOTES()
872                         );
873
874                         // Reset them
875                         EXT_SET_UPDATE_NOTES("");
876                 } elseif (($ver == "0.0") || ($ver == "0.0.0")) {
877                         // Initial release
878                         $content = array(
879                                 'ver'   => $ver,
880                                 'notes' => getMessage('INITIAL_RELEASE')
881                         );
882                 } else {
883                         // No update notes found!
884                         $content = array(
885                                 'ver'   => $ver,
886                                 'notes' => getMessage('NO_UPDATE_NOTES')
887                         );
888                 }
889
890                 // Load template
891                 $out = LOAD_TEMPLATE("admin_ext_notes", true, $content);
892         } // END - if
893
894         // Add the notes
895         EXT_APPEND_NOTES($out);
896 }
897
898 // Getter for CSS files array
899 function EXT_GET_CSS_FILES () {
900         // By default no additional CSS files are found
901         $cssFiles = array();
902
903         // Is the array there?
904         if (isset($GLOBALS['css_files'])) {
905                 // Then use it
906                 $cssFiles = $GLOBALS['css_files'];
907         } // END - if
908
909         // Return array
910         return $cssFiles;
911 }
912
913 // Init CSS files array
914 function EXT_INIT_CSS_FILES () {
915         // Simply init it
916         $GLOBALS['css_files'] = array();
917 }
918
919 // Add new entry
920 function EXT_ADD_CSS_FILE ($file) {
921         // Is the array there?
922         if (!isset($GLOBALS['css_files'])) {
923                 // Then auto-init them
924                 EXT_INIT_CSS_FILES();
925         } // END - if
926
927         // Add the entry
928         $GLOBALS['css_files'][] = $file;
929 }
930
931 // Setter for EXT_ALWAYS_ACTIVE flag
932 function EXT_SET_ALWAYS_ACTIVE ($active) {
933         $GLOBALS['ext_always_active'] = (string) $active;
934 }
935
936 // Getter for EXT_ALWAYS_ACTIVE flag
937 function EXT_GET_ALWAYS_ACTIVE () {
938         return $GLOBALS['ext_always_active'];
939 }
940
941 // Setter for EXT_VERSION flag
942 function EXT_SET_VERSION ($version) {
943         $GLOBALS['ext_version'] = (float) $version;
944 }
945
946 // Getter for EXT_VERSION flag
947 function EXT_GET_VERSION () {
948         return $GLOBALS['ext_version'];
949 }
950
951 // Setter for EXT_DEPRECATED flag
952 function EXT_SET_DEPRECATED ($deprecated) {
953         $GLOBALS['ext_deprecated'] = (string) $deprecated;
954 }
955
956 // Getter for EXT_DEPRECATED flag
957 function EXT_GET_DEPRECATED () {
958         return $GLOBALS['ext_deprecated'];
959 }
960
961 // Setter for EXT_UPDATE_DEPENDS flag
962 function EXT_ADD_UPDATE_DEPENDS ($updateDepends) {
963         // Is the update depency empty? (NEED TO BE FIXED!)
964         if (empty($updateDepends)) {
965                 // Please report this bug!
966                 debug_report_bug("updateDepends is left empty!");
967         } // END - if
968
969         // Is it not yet added?
970         if (!in_array($updateDepends, $GLOBALS['ext_running_updates'])) {
971                 //* DEBUG */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$updateDepends."<br />\n";
972                 // Add it to the list of extension update depencies map
973                 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][] = (string) $updateDepends;
974
975                 // Remember it in the list of running updates
976                 $GLOBALS['ext_running_updates'][] = $updateDepends;
977         } // END - if
978 }
979
980 // Init EXT_UPDATE_DEPENDS flag
981 function EXT_INIT_UPDATE_DEPENDS () {
982         //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
983
984         // Init update depency map
985         $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()] = array();
986
987         // Init running updates array
988         EXT_INIT_RUNNING_UPDATES();
989 }
990
991 // Initializes the list of running updates
992 function EXT_INIT_RUNNING_UPDATES () {
993         // Auto-init ext_running_updates
994         if (!isset($GLOBALS['ext_running_updates'])) {
995                 $GLOBALS['ext_running_updates'] = array();
996         } // END - if
997 }
998
999 // Getter for EXT_UPDATE_DEPENDS flag
1000 function EXT_GET_UPDATE_DEPENDS () {
1001         print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1002         return $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()];
1003 }
1004
1005 // Getter for next iterator depency
1006 function EXT_GET_ITERATOR_UPDATE_DEPENDS () {
1007         print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1008         return ($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][EXT_GET_UPDATE_ITERATOR()]);
1009 }
1010
1011 // Counter for extension update depencies
1012 function EXT_COUNT_UPDATE_DEPENDS () {
1013         print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1014         return count($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1015 }
1016
1017 // Removes given extension from update denpency list
1018 function EXT_REMOVE_UPDATE_DEPENDS ($ext_name) {
1019         // Look it up
1020         $key = array_search($ext_name, EXT_GET_UPDATE_DEPENDS());
1021
1022         // Is it valid?
1023         if ($key !== false) {
1024                 // Then remove it
1025                 unset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][$key]);
1026
1027                 // And sort the array
1028                 ksort($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1029         } // END - if
1030 }
1031
1032 // Init iterator for update depencies
1033 function EXT_INIT_UPDATE_ITERATOR () {
1034         print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1035         $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()] = 0;
1036 }
1037
1038 // Getter for depency iterator
1039 function EXT_GET_UPDATE_ITERATOR () {
1040         // Auto-init iterator
1041         if (!isset($GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()])) EXT_INIT_UPDATE_ITERATOR();
1042
1043         // Return it
1044         print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]."<br />\n";
1045         return $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()];
1046 }
1047
1048 // Increments the update iterator
1049 function EXT_INCREMENT_UPDATE_INTERATOR () {
1050         print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1051         $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]++;
1052 }
1053
1054 // Setter for EXT_REPORTS_FAILURE flag
1055 function EXT_SET_REPORTS_FAILURE ($reportsFailure) {
1056         $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1057 }
1058
1059 // Getter for EXT_REPORTS_FAILURE flag
1060 function EXT_GET_REPORTS_FAILURE () {
1061         return $GLOBALS['ext_reports_failure'];
1062 }
1063
1064 // Setter for EXT_VER_HISTORY flag
1065 function EXT_SET_VER_HISTORY ($verHistory) {
1066         $GLOBALS['ext_ver_history'] = (array) $verHistory;
1067 }
1068
1069 // Getter for EXT_VER_HISTORY array
1070 function EXT_GET_VER_HISTORY () {
1071         return $GLOBALS['ext_ver_history'];
1072 }
1073
1074 // Setter for EXT_UPDATE_NOTES flag
1075 function EXT_SET_UPDATE_NOTES ($updateNotes) {
1076         $GLOBALS['ext_update_notes'] = (string) $updateNotes;
1077 }
1078
1079 // Getter for EXT_UPDATE_NOTES flag
1080 function EXT_GET_UPDATE_NOTES () {
1081         return $GLOBALS['ext_update_notes'];
1082 }
1083
1084 // Init extension notice
1085 function EXT_INIT_NOTES () {
1086         $GLOBALS['ext_notes'] = "";
1087 }
1088
1089 // Append extension notice
1090 function EXT_APPEND_NOTES ($notes) {
1091         $GLOBALS['ext_notes'] .= (string) $notes;
1092 }
1093
1094 // Getter for extension notes
1095 function EXT_GET_NOTES () {
1096         return $GLOBALS['ext_notes'];
1097 }
1098
1099 // Setter for current extension name
1100 function EXT_SET_CURR_NAME ($ext_name) {
1101         $GLOBALS['curr_ext_name'] = (string) $ext_name;
1102 }
1103
1104 // Getter for current extension name
1105 function EXT_GET_CURR_NAME () {
1106         if (isset($GLOBALS['curr_ext_name'])) {
1107                 return $GLOBALS['curr_ext_name'];
1108         } // END - if
1109
1110         // Not set!
1111         debug_report_bug(__FUNCTION__.": curr_ext_name not initialized. Please execute INIT_EXT_SQLS() before calling this function.");
1112 }
1113
1114 // Init SQLs array for current extension
1115 function INIT_EXT_SQLS () {
1116         $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()] = array();
1117 }
1118
1119 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1120 function ADD_EXT_SQL ($sql) {
1121         $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()][] = $sql;
1122 }
1123
1124 // Getter for SQLs array for current extension
1125 function GET_EXT_SQLS () {
1126         // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1127         if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1128                 // Not found, should not happen
1129                 debug_report_bug(sprintf("ext_sqls is empty, current extension: %s",
1130                         EXT_GET_CURR_NAME()
1131                 ));
1132         } // END - if
1133
1134         // Return the array
1135         return $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()];
1136 }
1137
1138 // Removes SQLs for current extension
1139 function UNSET_EXT_SQLS () {
1140         unset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()]);
1141 }
1142
1143 // Auto-initializes the removal list
1144 function EXT_INIT_REMOVAL_LIST () {
1145         // Is the remove list there?
1146         if (!isset($GLOBALS['ext_update_remove'])) {
1147                 // Then create it
1148                 $GLOBALS['ext_update_remove'] = array();
1149         } // END - if
1150 }
1151
1152 // Checks wether the current extension is on the removal list
1153 function EXT_IS_ON_REMOVAL_LIST () {
1154         // Init removal list
1155         EXT_INIT_REMOVAL_LIST();
1156
1157         // Is it there?
1158         return (in_array(EXT_GET_CURR_NAME(), $GLOBALS['ext_update_remove']));
1159 }
1160
1161 // Adds the current extension to the removal list
1162 function EXT_ADD_CURRENT_TO_REMOVAL_LIST () {
1163         // Simply add it
1164         $GLOBALS['ext_update_remove'][] = EXT_GET_CURR_NAME();
1165 }
1166
1167 // Getter for removal list
1168 function EXT_GET_REMOVAL_LIST () {
1169         // Return the removal list
1170         return $GLOBALS['ext_update_remove'];
1171 }
1172
1173 //
1174 ?>