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