Some dublicated braces removed, some fixed (broken: update depencies got registered...
[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                                 // Remove all SQL commands
270                                 UNSET_SQLS();
271
272                                 // In normal mode return a true on success
273                                 $ret = true;
274                         } elseif ($dry_run) {
275                                 // Init SQLs and transfer ext->generic
276                                 INIT_SQLS();
277                                 SET_SQLS(GET_EXT_SQLS());
278
279                                 // Rewrite SQL command to keep { and } inside for dry-run
280                                 foreach (GET_SQLS() as $key => $sql) {
281                                         $sql = str_replace('{', "&#123;", str_replace('}', "&#125;", $sql));
282                                         SET_SQL_KEY($key, $sql);
283                                 } // END - foreach
284
285                                 // In  "dry-run" mode return array with all SQL commands
286                                 $ret = GET_SQLS();
287
288                                 // Remove all SQL commands
289                                 UNSET_SQLS();
290                         } else {
291                                 // Extension has been removed for updates, so all is fine!
292                                 $ret = true;
293                         }
294                 } else {
295                         // No, an error occurs while registering extension :-(
296                         //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME()."<br />\n";
297                         $ret = false;
298                 }
299         } elseif (($task_id > 0) && (EXT_GET_CURR_NAME() != "")) {
300                 // Remove task from system when id and extension's name is valid
301                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE id=%s AND `status`='NEW' LIMIT 1",
302                         array(bigintval($task_id)), __FILE__, __LINE__);
303         }
304
305         // Is this the sql_patches?
306         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":{EXT_GET_CURR_NAME()}/{$EXT_LOAD_MODE}");
307         if ((EXT_GET_CURR_NAME() == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
308                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
309                 if ($logout === true) {
310                         // Then redirect to logout
311                         LOAD_URL("modules.php?module=admin&amp;logout=1&amp;".$EXT_LOAD_MODE."=sql_patches");
312                 } else {
313                         // Add temporary filter
314                         REGISTER_FILTER('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
315                         $GLOBALS['ext_load_mode'] = $EXT_LOAD_MODE;
316                 }
317         } // END - if
318
319         // Return status code
320         return $ret;
321 }
322
323 // Run SQL queries for given extension id
324 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
325 function EXTENSION_RUN_SQLS ($ext_id, $load_mode) {
326         // This shall never do a non-admin user!
327         if (!IS_ADMIN()) return false;
328
329         // Get extension's name
330         $ext_name = GET_EXT_NAME($ext_id);
331
332         // If it is not set then maybe there is no extension for that ID number
333         if ($ext_name == "") return false;
334
335         // Set current SQL name
336         EXT_SET_CURR_NAME($ext_name);
337
338         // Init EXT_UPDATE_DEPENDS
339         EXT_INIT_UPDATE_DEPENDS();
340
341         // Init array
342         INIT_EXT_SQLS();
343
344         // By default no SQL has been executed
345         $sqlRan = false;
346
347         // Load extension in detected mode
348         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]={EXT_GET_CURR_NAME()}");
349         LOAD_EXTENSION(EXT_GET_CURR_NAME(), $load_mode, "", false);
350
351         // Init these SQLs
352         INIT_SQLS();
353         SET_SQLS(GET_EXT_SQLS());
354
355         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".COUNT_SQLS()."");
356         if ((IS_SQLS_VALID() && (COUNT_SQLS() > 0))) {
357                 // Run SQL commands...
358                 runFilterChain('run_sqls');
359
360                 // Removal mode?
361                 if ($load_mode == "remove") {
362                         // Delete this extension (remember to remove it from your server *before* you click on welcome!
363                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
364                                 array(EXT_GET_CURR_NAME()), __FILE__, __LINE__);
365                 } // END - if
366         } // END - if
367
368         // Remove cache file(s) if extension is active
369         if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == "activate") || ($load_mode == "deactivate"))) {
370                 // Run filters
371                 runFilterChain('post_extension_run_sql', EXT_GET_CURR_NAME());
372         } // END - if
373
374         // Is this the sql_patches?
375         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": {$ext_id}/{EXT_GET_CURR_NAME()}/{$load_mode}");
376         if ((EXT_GET_CURR_NAME() == "sql_patches") && (($load_mode == "register") || ($load_mode == "remove"))) {
377                 // Then redirect to logout
378                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
379                 LOAD_URL("modules.php?module=admin&amp;logout=1&amp;".$load_mode."=sql_patches");
380         } // END - if
381 }
382
383 // Check if given extension is active
384 function EXT_IS_ACTIVE ($ext_name) {
385         // Extensions are all inactive during installation
386         if ((!isInstalled()) || (isInstalling()) || (empty($ext_name))) return false;
387
388         // Not active is the default
389         $active = "N";
390
391         // Check cache
392         if (isset($GLOBALS['cache_array']['extensions']['ext_active'][$ext_name])) {
393                 // Load from cache
394                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
395                 $active = $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name];
396
397                 // Count cache hits
398                 incrementConfigEntry('cache_hits');
399         } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
400                 // @TODO Extension is loaded, what next?
401                 mxchange_die("LOADED:$ext_name");
402         } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
403                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
404                 // Load from database
405                 $result = SQL_QUERY_ESC("SELECT ext_active FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
406                         array($ext_name), __FILE__, __LINE__);
407
408                 // Entry found?
409                 if (SQL_NUMROWS($result) == 1) {
410                         // Load entry
411                         list($active) = SQL_FETCHROW($result);
412                 } // END - if
413
414                 // Free result
415                 SQL_FREERESULT($result);
416
417                 // Write cache array
418                 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
419                 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = $active;
420         } else {
421                 // Extension not active!
422                 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
423                 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = "N";
424         }
425
426         // Debug message
427         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
428
429         // Is this extension activated? (For admins we always have active extensions...)
430         return ($active == "Y");
431 }
432 // Get version from extensions
433 function GET_EXT_VERSION ($ext_name) {
434         // By default no extension is found
435         $ext_ver = false;
436
437         // Extensions are all inactive during installation
438         if ((!isInstalled()) || (isInstalling())) return "";
439         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
440
441         // Is the cache written?
442         if (isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name])) {
443                 // Load data from cache
444                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
445                 $ext_ver = $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name];
446
447                 // Count cache hits
448                 incrementConfigEntry('cache_hits');
449         } elseif (!isCacheInstanceValid()) {
450                 // Load from database
451                 $result = SQL_QUERY_ESC("SELECT ext_version FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
452                         array($ext_name), __FILE__, __LINE__);
453                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": DB - ".SQL_NUMROWS($result)."");
454
455                 // Is the extension there?
456                 if (SQL_NUMROWS($result) == 1) {
457                         // Load entry
458                         list($ext_ver) = SQL_FETCHROW($result);
459                 } // END - if
460
461                 // Free result
462                 SQL_FREERESULT($result);
463
464                 // Set cache
465                 $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name] = $ext_ver;
466         }
467
468         // Return result
469         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
470         return $ext_ver;
471 }
472
473 // Updates a given extension with current extension version to latest version
474 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
475         // Only admins are allowed to update extensions
476         if ((!IS_ADMIN()) || (empty($ext_name))) return false;
477
478         // Set current SQL name
479         EXT_SET_CURR_NAME($ext_name);
480
481         // Init arrays
482         INIT_EXT_SQLS();
483         EXT_INIT_NOTES();
484         INIT_INC_POOL();
485
486         // Load extension in test mode
487         LOAD_EXTENSION($ext_name, "test", $ext_ver, $dry_run);
488
489         // Save version history
490         $history = EXT_GET_VER_HISTORY();
491
492         // Remove old SQLs array to prevent possible bugs
493         INIT_EXT_SQLS();
494
495         // Check if version is updated
496         if (((EXT_GET_VERSION() != $ext_ver) || ($dry_run)) && (is_array($history))) {
497                 // Search for starting point
498                 $start = array_search($ext_ver, $history);
499
500                 // And load SQL queries in order of version history
501                 for ($idx = ($start + 1); $idx < count($history); $idx++) {
502                         // Set extension version
503                         $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()] = $history[$idx];
504
505                         // Load again...
506                         LOAD_EXTENSION(EXT_GET_CURR_NAME(), "update", $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], $dry_run);
507
508                         if (EXT_GET_UPDATE_DEPENDS() != "") {
509                                 // Is the extension there?
510                                 if (GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()) != "") {
511                                         // Update another extension first!
512                                         $test = EXTENSION_UPDATE(EXT_GET_UPDATE_DEPENDS(), GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()), $dry_run);
513                                 } else {
514                                         // Register new extension
515                                         $test = REGISTER_EXTENSION(EXT_GET_UPDATE_DEPENDS(), 0, $dry_run, false);
516                                 }
517                         } // END - if
518
519                         // Add notes
520                         ADD_EXTENSION_NOTES($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
521                 } // END - for
522
523                 // In real-mode execute any existing includes
524                 if (!$dry_run) {
525                         $GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()] = GET_INC_POOL();
526                         runFilterChain('load_includes');
527                         SET_INC_POOL($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
528                         unset($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
529                 } // END - if
530
531                 // Init these SQLs
532                 INIT_SQLS();
533                 SET_SQLS(GET_EXT_SQLS());
534
535                 // Run SQLs
536                 runFilterChain('run_sqls', array('dry_run' => $dry_run));
537
538                 if (!$dry_run) {
539                         // Create task
540                         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())));
541
542                         // Update extension's version
543                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
544                                 array($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], EXT_GET_CURR_NAME()), __FILE__, __LINE__);
545
546                         // Remove arrays
547                         UNSET_SQLS();
548                         unset($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
549
550                         // Run filters on success extension update
551                         runFilterChain('extension_update', EXT_GET_CURR_NAME());
552                 } // END - if
553         } // END - if
554 }
555
556 // Output verbose SQL table for extension
557 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = "", $dashed = "", $switch = false, $width = "100%") {
558         // Empty title?
559         if (empty($title)) {
560                 // Then fix it to default
561                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
562         } // END - if
563
564         // Are there some queries in $queries?
565         if (count($queries) > 0) {
566                 // Then use them instead!
567                 SET_SQLS($queries);
568         } // END - if
569
570         // Init variables
571         $SW = 2; $i = 1;
572         $OUT = "";
573
574         // Do we have queries?
575         if ((IS_SQLS_VALID()) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
576                 foreach (GET_SQLS() as $idx => $sql) {
577                         // Trim out spaces
578                         $sql = trim($sql);
579
580                         // Output command if set
581                         if (!empty($sql)) {
582                                 // Prepare output for template
583                                 $content = array(
584                                         'sw'  => $SW,
585                                         'i'   => $i,
586                                         'sql' => $sql
587                                 );
588
589                                 // Load row template
590                                 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
591
592                                 // Switch color and count up
593                                 $SW = 3 - $SW;
594                                 $i++;
595                         } // END - if
596                 } // END - foreach
597
598                 // Prepare content for template
599                 $content = array(
600                         'width'  => $width,
601                         'dashed' => $dashed,
602                         'title'  => $title,
603                         'out'    => $OUT
604                 );
605
606                 // Load main template
607                 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
608         } elseif ((GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
609                 // No addional SQL commands to run
610                 $OUT = LOAD_TEMPLATE("admin_settings_saved", true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
611         } // END - if
612
613         // Return output
614         return $OUT;
615 }
616
617 // Get extension name from id
618 function GET_EXT_NAME ($ext_id) {
619         // Init extension name
620         $ret = "";
621
622         // Is cache there?
623         if (isset($GLOBALS['cache_array']['extensions']['ext_name'][$ext_id])) {
624                 // Load from cache
625                 $ret = $GLOBALS['cache_array']['extensions']['ext_name'][$ext_id];
626
627                 // Count cache hits
628                 incrementConfigEntry('cache_hits');
629         } elseif (!EXT_IS_ACTIVE("cache")) {
630                 // Load from database
631                 $result = SQL_QUERY_ESC("SELECT ext_name FROM `{!_MYSQL_PREFIX!}_extensions` WHERE id=%s LIMIT 1",
632                         array(bigintval($ext_id)), __FILE__, __LINE__);
633                 list($ret) = SQL_FETCHROW($result);
634                 SQL_FREERESULT($result);
635         }
636         return $ret;
637 }
638
639 // Get extension id from name
640 function GET_EXT_ID ($ext_name) {
641         // Init ID number
642         $ret = 0;
643         if (isset($GLOBALS['cache_array']['extensions']['ext_id'][$ext_name])) {
644                 // Load from cache
645                 $ret = $GLOBALS['cache_array']['extensions']['ext_id'][$ext_name];
646
647                 // Count cache hits
648                 incrementConfigEntry('cache_hits');
649         } elseif (!EXT_IS_ACTIVE("cache")) {
650                 // Load from database
651                 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
652                         array($ext_name), __FILE__, __LINE__);
653                 list($ret) = SQL_FETCHROW($result);
654                 SQL_FREERESULT($result);
655         }
656
657         // Return value
658         return $ret;
659 }
660
661 // Activate given extension
662 function ACTIVATE_EXTENSION ($ext_name) {
663         // Activate the extension
664         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
665                 array($ext_name), __FILE__, __LINE__);
666
667         // Extension has been activated?
668         if (SQL_AFFECTEDROWS() == 1) {
669                 // Then run all queries
670                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
671         } // END - if
672 }
673
674 // Deactivate given extension
675 function DEACTIVATE_EXTENSION($ext_name) {
676         // Activate the extension
677         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='N' WHERE ext_name='%s' LIMIT 1",
678                 array($ext_name), __FILE__, __LINE__);
679
680         // Extension has been activated?
681         if (SQL_AFFECTEDROWS() == 1) {
682                 // Then run all queries
683                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "deactivate");
684
685                 // Create new task
686                 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
687
688                 // Notify the admin
689                 SEND_ADMIN_NOTIFICATION(
690                         getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
691                         "admin_ext_deactivated",
692                         array('ext_name' => $ext_name)
693                 );
694         } // END - if
695 }
696
697 // Checks wether the extension is older than given
698 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
699         // Get current extension version
700         $currVersion = GET_EXT_VERSION($ext_name);
701
702         // Remove all dots from both versions
703         $currVersion = str_replace(".", "", $currVersion);
704         $ext_ver = str_replace(".", "", $ext_ver);
705
706         // Now compare both and return the result
707         return ($currVersion < $ext_ver);
708 }
709
710 // Creates a new task for updated extension
711 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
712         // Create subject line
713         $subject = "[UPDATE-".$ext_name."-".$ext_ver.":] {--ADMIN_UPDATE_EXT_SUBJ--}";
714
715         // Is the extension there?
716         if (GET_EXT_VERSION($ext_name) != "") {
717                 // Check if task is not there
718                 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
719                         // Task not created so it's a brand-new extension which we need to register and create a task for!
720                         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())",
721                                 array($admin_id, $subject, $notes), __FILE__, __LINE__);
722                 } // END - if
723         } // END - if
724 }
725
726 // Creates a new task for newly installed extension
727 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
728         // Not installed and do we have created a task for the admin?
729         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == "")) {
730                 // Template file
731                 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
732                         constant('PATH'),
733                         GET_LANGUAGE(),
734                         $ext
735                 );
736
737                 // Load text for task
738                 if (FILE_READABLE($tpl)) {
739                         // Load extension's own text template (HTML!)
740                         $msg = LOAD_TEMPLATE("ext_".$ext, true);
741                 } else {
742                         // Load default message
743                         $msg = LOAD_TEMPLATE("admin_new_ext", "", 0);
744                 }
745
746                 // Task not created so it's a brand-new extension which we need to register and create a task for!
747                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
748 VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
749                         array(
750                                 $admin_id,
751                                 $subject,
752                                 SQL_ESCAPE($msg),
753                         ),  __FILE__, __LINE__, true, false
754                 );
755         } // END - if
756 }
757
758 // Creates a task for automatically deactivated (deprecated) extension
759 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
760         // Create subject line
761         $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
762
763         // Not installed and do we have created a task for the admin?
764         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != "")) {
765                 // Task not created so add it
766                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
767 VALUES (0,0,'NEW','EXTENSION_DEACTIVATION','%s','%s',UNIX_TIMESTAMP())",
768                         array(
769                                 $subject,
770                                 SQL_ESCAPE(LOAD_TEMPLATE("task_ext_deactivated", true, $ext)),
771                         ),  __FILE__, __LINE__, true, false
772                 );
773         } // END - if
774 }
775
776 // Checks if the module has a menu
777 function MODULE_HAS_MENU ($mod, $forceDb = false) {
778         // All is false by default
779         $ret = false;
780
781         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mod={$mod},cache=".GET_EXT_VERSION("cache")."<br />\n";
782         if (GET_EXT_VERSION("cache") >= "0.1.2") {
783                 // Cache version is okay, so let's check the cache!
784                 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
785                         // Check module cache and count hit
786                         $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == "Y");
787                         incrementConfigEntry('cache_hits');
788                 } elseif (isset($GLOBALS['cache_array']['extensions']['ext_menu'][$mod])) {
789                         // Check cache and count hit
790                         $ret = ($GLOBALS['cache_array']['extensions']['ext_menu'][$mod] == "Y");
791                         incrementConfigEntry('cache_hits');
792                 } elseif ((IS_ADMIN()) && ($mod == "admin")) {
793                         // Admin module has always a menu!
794                         $ret = true;
795                 }
796         } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
797                 // Check database for entry
798                 $result = SQL_QUERY_ESC("SELECT has_menu FROM `{!_MYSQL_PREFIX!}_mod_reg` WHERE module='%s' LIMIT 1",
799                         array($mod), __FILE__, __LINE__);
800
801                 // Entry found?
802                 if (SQL_NUMROWS($result) == 1) {
803                         // Load "has_menu" column
804                         list($has_menu) = SQL_FETCHROW($result);
805
806                         // Fake cache... ;-)
807                         $GLOBALS['cache_array']['extensions']['ext_menu'][$mod] = $has_menu;
808
809                         // Does it have a menu?
810                         $ret = ($has_menu == "Y");
811                 } // END  - if
812
813                 // Free memory
814                 SQL_FREERESULT($result);
815         } elseif (GET_EXT_VERSION("sql_patches") == "") {
816                 // No sql_patches installed, so maybe in admin area or no admin registered?
817                 $ret = (((IS_ADMIN()) || (!isAdminRegistered())) && ($mod == "admin")); // Then there is a menu!
818         }
819
820         // Return status
821         //* DEBUG: */ var_dump($ret);
822         return $ret;
823 }
824
825 // Determines the task id for given extension
826 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
827         // Default is not found
828         $task_id = 0;
829
830         // Search for extension task's id
831         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE task_type='EXTENSION' AND subject='[%s:]' LIMIT 1",
832                 array($ext_name), __FILE__, __LINE__);
833
834         // Entry found?
835         if (SQL_NUMROWS($result) == 1) {
836                 // Task found so load task's ID and register extension...
837                 list($task_id) = SQL_FETCHROW($result);
838         } // END - if
839
840         // Free result
841         SQL_FREERESULT($result);
842
843         // Return it
844         return $task_id;
845 }
846
847 // Determines the task id for given subject
848 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
849         // Default is not found
850         $task_id = 0;
851
852         // Search for task id
853         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject LIKE '%s%%' LIMIT 1",
854                 array($subject), __FILE__, __LINE__);
855
856         // Entry found?
857         if (SQL_NUMROWS($result) == 1) {
858                 // Task found so load task's ID and register extension...
859                 list($task_id) = SQL_FETCHROW($result);
860         } // END - if
861
862         // Free result
863         SQL_FREERESULT($result);
864
865         // Return it
866         return $task_id;
867 }
868
869 // Add updates notes for given version
870 function ADD_EXTENSION_NOTES ($ver) {
871         // Init notes/content
872         $out = ""; $content = array();
873
874         // Is do we have verbose output enabled?
875         if ((getConfig('verbose_sql') == "Y") || (!EXT_IS_ACTIVE("sql_patches"))) {
876
877                 // Update notes found?
878                 if (EXT_GET_UPDATE_NOTES() != "") {
879                         // Update notes found
880                         $content = array(
881                                 'ver'   => $ver,
882                                 'notes' => EXT_GET_UPDATE_NOTES()
883                         );
884
885                         // Reset them
886                         EXT_SET_UPDATE_NOTES("");
887                 } elseif (($ver == "0.0") || ($ver == "0.0.0")) {
888                         // Initial release
889                         $content = array(
890                                 'ver'   => $ver,
891                                 'notes' => getMessage('INITIAL_RELEASE')
892                         );
893                 } else {
894                         // No update notes found!
895                         $content = array(
896                                 'ver'   => $ver,
897                                 'notes' => getMessage('NO_UPDATE_NOTES')
898                         );
899                 }
900
901                 // Load template
902                 $out = LOAD_TEMPLATE("admin_ext_notes", true, $content);
903         } // END - if
904
905         // Add the notes
906         EXT_APPEND_NOTES($out);
907 }
908
909 // Getter for CSS files array
910 function EXT_GET_CSS_FILES () {
911         // By default no additional CSS files are found
912         $cssFiles = array();
913
914         // Is the array there?
915         if (isset($GLOBALS['css_files'])) {
916                 // Then use it
917                 $cssFiles = $GLOBALS['css_files'];
918         } // END - if
919
920         // Return array
921         return $cssFiles;
922 }
923
924 // Init CSS files array
925 function EXT_INIT_CSS_FILES () {
926         // Simply init it
927         $GLOBALS['css_files'] = array();
928 }
929
930 // Add new entry
931 function EXT_ADD_CSS_FILE ($file) {
932         // Is the array there?
933         if (!isset($GLOBALS['css_files'])) {
934                 // Then auto-init them
935                 EXT_INIT_CSS_FILES();
936         } // END - if
937
938         // Add the entry
939         $GLOBALS['css_files'][] = $file;
940 }
941
942 // Setter for EXT_ALWAYS_ACTIVE flag
943 function EXT_SET_ALWAYS_ACTIVE ($active) {
944         $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()] = (string) $active;
945 }
946
947 // Getter for EXT_ALWAYS_ACTIVE flag
948 function EXT_GET_ALWAYS_ACTIVE () {
949         return $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()];
950 }
951
952 // Setter for EXT_VERSION flag
953 function EXT_SET_VERSION ($version) {
954         $GLOBALS['ext_version'][EXT_GET_CURR_NAME()] = (float) $version;
955 }
956
957 // Getter for EXT_VERSION flag
958 function EXT_GET_VERSION () {
959         return $GLOBALS['ext_version'][EXT_GET_CURR_NAME()];
960 }
961
962 // Setter for EXT_DEPRECATED flag
963 function EXT_SET_DEPRECATED ($deprecated) {
964         $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()] = (string) $deprecated;
965 }
966
967 // Getter for EXT_DEPRECATED flag
968 function EXT_GET_DEPRECATED () {
969         return $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()];
970 }
971
972 // Setter for EXT_UPDATE_DEPENDS flag
973 function EXT_ADD_UPDATE_DEPENDS ($updateDepends) {
974         // Is the update depency empty? (NEED TO BE FIXED!)
975         if (empty($updateDepends)) {
976                 // Please report this bug!
977                 debug_report_bug("updateDepends is left empty!");
978         } // END - if
979
980         // Is it not yet added?
981         if (!in_array($updateDepends, $GLOBALS['ext_running_updates'])) {
982                 //* DEBUG */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$updateDepends."<br />\n";
983                 // Add it to the list of extension update depencies map
984                 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][] = (string) $updateDepends;
985
986                 // Remember it in the list of running updates
987                 $GLOBALS['ext_running_updates'][] = $updateDepends;
988         } // END - if
989 }
990
991 // Init EXT_UPDATE_DEPENDS flag
992 function EXT_INIT_UPDATE_DEPENDS () {
993         //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
994
995         // Init update depency map
996         $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()] = array();
997
998         // Init running updates array
999         EXT_INIT_RUNNING_UPDATES();
1000 }
1001
1002 // Initializes the list of running updates
1003 function EXT_INIT_RUNNING_UPDATES () {
1004         // Auto-init ext_running_updates
1005         if (!isset($GLOBALS['ext_running_updates'])) {
1006                 $GLOBALS['ext_running_updates'] = array();
1007         } // END - if
1008 }
1009
1010 // Getter for EXT_UPDATE_DEPENDS flag
1011 function EXT_GET_UPDATE_DEPENDS () {
1012         //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1013         return $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()];
1014 }
1015
1016 // Getter for next iterator depency
1017 function EXT_GET_ITERATOR_UPDATE_DEPENDS () {
1018         //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1019         return ($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][EXT_GET_UPDATE_ITERATOR()]);
1020 }
1021
1022 // Counter for extension update depencies
1023 function EXT_COUNT_UPDATE_DEPENDS () {
1024         //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1025         return count($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1026 }
1027
1028 // Removes given extension from update denpency list
1029 function EXT_REMOVE_UPDATE_DEPENDS ($ext_name) {
1030         // Look it up
1031         $key = array_search($ext_name, EXT_GET_UPDATE_DEPENDS());
1032
1033         // Is it valid?
1034         if ($key !== false) {
1035                 // Then remove it
1036                 unset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][$key]);
1037
1038                 // And sort the array
1039                 ksort($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1040         } // END - if
1041 }
1042
1043 // Init iterator for update depencies
1044 function EXT_INIT_UPDATE_ITERATOR () {
1045         //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1046         $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()] = 0;
1047 }
1048
1049 // Getter for depency iterator
1050 function EXT_GET_UPDATE_ITERATOR () {
1051         // Auto-init iterator
1052         if (!isset($GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()])) EXT_INIT_UPDATE_ITERATOR();
1053
1054         // Return it
1055         //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]."<br />\n";
1056         return $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()];
1057 }
1058
1059 // Increments the update iterator
1060 function EXT_INCREMENT_UPDATE_INTERATOR () {
1061         //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1062         $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]++;
1063 }
1064
1065 // Setter for EXT_REPORTS_FAILURE flag
1066 function EXT_SET_REPORTS_FAILURE ($reportsFailure) {
1067         $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1068 }
1069
1070 // Getter for EXT_REPORTS_FAILURE flag
1071 function EXT_GET_REPORTS_FAILURE () {
1072         return $GLOBALS['ext_reports_failure'];
1073 }
1074
1075 // Setter for EXT_VER_HISTORY flag
1076 function EXT_SET_VER_HISTORY ($verHistory) {
1077         $GLOBALS['ext_ver_history'] = (array) $verHistory;
1078 }
1079
1080 // Getter for EXT_VER_HISTORY array
1081 function EXT_GET_VER_HISTORY () {
1082         return $GLOBALS['ext_ver_history'];
1083 }
1084
1085 // Setter for EXT_UPDATE_NOTES flag
1086 function EXT_SET_UPDATE_NOTES ($updateNotes) {
1087         $GLOBALS['ext_update_notes'] = (string) $updateNotes;
1088 }
1089
1090 // Getter for EXT_UPDATE_NOTES flag
1091 function EXT_GET_UPDATE_NOTES () {
1092         return $GLOBALS['ext_update_notes'];
1093 }
1094
1095 // Init extension notice
1096 function EXT_INIT_NOTES () {
1097         $GLOBALS['ext_notes'] = "";
1098 }
1099
1100 // Append extension notice
1101 function EXT_APPEND_NOTES ($notes) {
1102         $GLOBALS['ext_notes'] .= (string) $notes;
1103 }
1104
1105 // Getter for extension notes
1106 function EXT_GET_NOTES () {
1107         return $GLOBALS['ext_notes'];
1108 }
1109
1110 // Setter for current extension name
1111 function EXT_SET_CURR_NAME ($ext_name) {
1112         $GLOBALS['curr_ext_name'] = (string) $ext_name;
1113 }
1114
1115 // Getter for current extension name
1116 function EXT_GET_CURR_NAME () {
1117         if (isset($GLOBALS['curr_ext_name'])) {
1118                 return $GLOBALS['curr_ext_name'];
1119         } // END - if
1120
1121         // Not set!
1122         debug_report_bug(__FUNCTION__.": curr_ext_name not initialized. Please execute INIT_EXT_SQLS() before calling this function.");
1123 }
1124
1125 // Init SQLs array for current extension
1126 function INIT_EXT_SQLS () {
1127         // Auto-init the array now...
1128         if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1129                 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()] = array();
1130         } // END - if
1131 }
1132
1133 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1134 function ADD_EXT_SQL ($sql) {
1135         $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()][] = $sql;
1136 }
1137
1138 // Getter for SQLs array for current extension
1139 function GET_EXT_SQLS () {
1140         // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1141         if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1142                 // Not found, should not happen
1143                 debug_report_bug(sprintf("ext_sqls is empty, current extension: %s",
1144                         EXT_GET_CURR_NAME()
1145                 ));
1146         } // END - if
1147
1148         // Return the array
1149         return $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()];
1150 }
1151
1152 // Removes SQLs for current extension
1153 function UNSET_EXT_SQLS () {
1154         unset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()]);
1155 }
1156
1157 // Auto-initializes the removal list
1158 function EXT_INIT_REMOVAL_LIST () {
1159         // Is the remove list there?
1160         if (!isset($GLOBALS['ext_update_remove'])) {
1161                 // Then create it
1162                 $GLOBALS['ext_update_remove'] = array();
1163         } // END - if
1164 }
1165
1166 // Checks wether the current extension is on the removal list
1167 function EXT_IS_ON_REMOVAL_LIST () {
1168         // Init removal list
1169         EXT_INIT_REMOVAL_LIST();
1170
1171         // Is it there?
1172         return (in_array(EXT_GET_CURR_NAME(), $GLOBALS['ext_update_remove']));
1173 }
1174
1175 // Adds the current extension to the removal list
1176 function EXT_ADD_CURRENT_TO_REMOVAL_LIST () {
1177         // Simply add it
1178         $GLOBALS['ext_update_remove'][] = EXT_GET_CURR_NAME();
1179 }
1180
1181 // Getter for removal list
1182 function EXT_GET_REMOVAL_LIST () {
1183         // Return the removal list
1184         return $GLOBALS['ext_update_remove'];
1185 }
1186
1187 //
1188 ?>