Fixes for missing parameters for debug_report_bug(), was introduced in some revisions ago
[mailer.git] / inc / extensions-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/25/2009 *
4  * ===================                          Last change: 10/25/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : extensions-functions.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 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Load the extension and maybe found language and function files.
46 function loadExtension ($ext_name, $ext_mode = 'init', $ext_ver = '', $dry_run = false) {
47         // If this happens twice, we need the bug report from you, except for updates/tests
48         if ((isset($GLOBALS['loaded_extension'][$ext_name][$ext_mode])) && (!in_array($ext_mode, array('update', 'test')))) {
49                 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: ext_name=' . $ext_name . ', ext_mode='. $ext_mode . ',ext_sqls=' . print_r(getExtensionSqls(), true) . ', ext_register_running=' . print_r($GLOBALS['ext_register_running'], true) . ', ext_running_updates=' . print_r($GLOBALS['ext_running_updates'], true));
50         } // END - if
51
52         // Make sure this situation can only happen once
53         $GLOBALS['loaded_extension'][$ext_name][$ext_mode] = true;
54
55         // Set extension mode
56         setExtensionMode($ext_mode);
57
58         // Set current extension name
59         setCurrentExtensionName($ext_name);
60
61         if (!empty($ext_ver)) {
62                 // Set current extension version
63                 setCurrentExtensionVersion($ext_ver);
64         } else {
65                 // Set it to 0.0
66                 setCurrentExtensionVersion('0.0');
67
68                 // In all but test-mode we need these messages to debug! Please report all (together, e.g.)
69                 if ($ext_mode != 'test') {
70                         // Log empty versions not in test-mode, but maybe it is fine...
71                         logDebugMessage(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0. ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', dry_run=' . intval($dry_run));
72                 } // END - if
73         }
74
75         // Set dry-run
76         enableExtensionDryRun($dry_run);
77
78         // Init array
79         initIncludePool('extension');
80
81         // Init EXT_UPDATE_DEPENDS if not yet done
82         if (!isExtensionUpdateDependenciesInitialized()) {
83                 // Init here...
84                 initExtensionUpdateDependencies();
85         } // END - if
86
87         // Init current extension name list
88         initExtensionSqls();
89
90         // Is the extension already loaded?
91         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Loading extension ' . $ext_name . ', mode=' . getExtensionMode() . ', ver=' . getCurrentExtensionVersion());
92         if ((isset($GLOBALS['ext_loaded']['ext'][$ext_name])) && (getExtensionMode() == 'init')) {
93                 // Debug message
94                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
95
96                 // Abort here
97                 return false;
98         } // END - if
99
100         // Is the extension file NOT there?
101         if (!isExtensionNameValid($ext_name)) {
102                 // Debug message
103                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable.", $ext_name));
104
105                 // Abort here
106                 return false;
107         } // END - if
108
109         // Load extension's own language file if not in test mode
110         if ((getExtensionMode() != 'test') && (ifExtensionHasLanguageFile($ext_name))) {
111                 // Load it
112                 loadLanguageFile($ext_name);
113         } // END - if
114
115         // Do we have cache?
116         if (isExtensionFunctionFileReadable($ext_name)) {
117                 // Not yet loaded?
118                 if ((($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y') || (!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name]))) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name]))) {
119                         // Construct FQFN for functions file
120                         $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
121
122                         // Mark it as loaded
123                         $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
124
125                         // Download functions file
126                         loadIncludeOnce($funcsInclude);
127                 } // END - if
128         } elseif ((!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) && (isDebugModeEnabled()) && (getOutputMode() == '0') && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme') && (getExtensionMode() == 'test')) {
129                 // No functions file is not so good...
130                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no own functions file or we cannot read from it. mode=%s",
131                         $ext_name,
132                         getExtensionMode()
133                 ));
134         }
135
136         // Extensions are not deprecated by default
137         setExtensionDeprecated('N');
138
139         // Extensions are not always active by default
140         setExtensionAlwaysActive('N');
141
142         // Extension update notes
143         // @TODO Do we still need this? setExtensionUpdateNotes('');
144
145         // Include the extension file
146         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension loaded.');
147         loadExtensionInclude();
148
149         // Is this extension deprecated?
150         if (isExtensionDeprecated()) {
151                 // Deactivate the extension
152                 doDeactivateExtension($ext_name);
153
154                 // Abort here
155                 return false;
156         } // END - if
157
158         // Mark it as loaded in normal mode
159         if (getExtensionMode() == '') {
160                 // Mark it now...
161                 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
162         } // END - if
163
164         // All fine!
165         return true;
166 }
167
168 // Registeres an extension and possible update depencies
169 function registerExtension ($ext_name, $task_id, $dry_run = false, $logout = true) {
170         // Set current extension name
171         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',task_id=' . $task_id . ',dry_run=' . intval($dry_run) . ',logout=' . intval($logout) . ' - ENTERED!');
172         setCurrentExtensionName($ext_name);
173
174         // Enable dry-run
175         enableExtensionDryRun($dry_run);
176
177         // By default all extensions are in productive phase
178         enableExtensionProductive();
179
180         // This shall never do a non-admin user or if the extension is active (already installed)
181         if ((!isAdmin()) || (isExtensionInstalled($ext_name))) {
182                 // Abort here with 'false'
183                 return false;
184         } // END - if
185
186         // When this extension is already in registration/update phase, all is fine
187         if ((isExtensionRegistrationRunning($ext_name)) || (isExtensionUpdateRunning($ext_name))) {
188                 // Then abort here with 'true' becaus it is fine
189                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ALREADY!');
190                 return true;
191         } // END - if
192
193         // This registration is running
194         addExtensionRunningRegistration($ext_name);
195
196         // Init EXT_UPDATE_DEPENDS if not yet done
197         if (!isExtensionUpdateDependenciesInitialized()) {
198                 // Init here...
199                 initExtensionUpdateDependencies();
200         } // END - if
201
202         // Is the task id zero? Then we need to auto-fix it here
203         if ($task_id == '0') {
204                 // Try to find the task
205                 $task_id = determineExtensionTaskId(getCurrentExtensionName());
206
207                 // Still zero and not in dry-run?
208                 if (($task_id == '0') && (!getExtensionDryRun())) {
209                         // Then request a bug report
210                         debug_report_bug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId(%s)",
211                                 __FUNCTION__,
212                                 getCurrentExtensionName()
213                         ));
214                 } // END - if
215         } // END - if
216
217         // Init queries and notes
218         initExtensionSqls();
219         initExtensionNotes();
220
221         // Init variables
222         $ret = false;
223         $test = false;
224         initIncludePool('extension');
225
226         // By default we have no failures
227         setExtensionReportsFailure(false);
228
229         // Does this extension exists?
230         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
231         if (loadExtension(getCurrentExtensionName(), 'register', '', getExtensionDryRun())) {
232                 // Set current extension name again
233                 setCurrentExtensionName($ext_name);
234
235                 // And run possible updates
236                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
237                 $history = getExtensionVersionHistory();
238                 foreach ($history as $ver) {
239                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ver);
240                         // Load extension in update mode
241                         loadExtension(getCurrentExtensionName(), 'update', $ver, getExtensionDryRun());
242
243                         // Add update notes to our output
244                         addExtensionNotes($ver);
245                 } // END - foreach
246
247                 // Does this extension depends on an outstanding update of another update?
248                 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
249                         // Get next update
250                         $ext_update = getExtensionUpdateDependenciesIterator();
251
252                         // Increment here to avoid endless loop
253                         incrementExtensionUpdateIterator();
254
255                         // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
256                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
257                         if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', getExtensionDryRun()))) {
258                                 // Set current extension name again
259                                 setCurrentExtensionName($ext_name);
260
261                                 // If versions mismatch update extension first
262                                 $ext_ver = '';
263                                 if (isExtensionInstalled($ext_update)) {
264                                         // Get version only if installed
265                                         $ext_ver = getExtensionVersion($ext_update);
266                                 } // END - if
267
268                                 // Extension version set? If empty the extension is not registered
269                                 if (empty($ext_ver)) {
270                                         // Extension not registered so far so first load task's id...
271                                         $task = determineExtensionTaskId($ext_update);
272
273                                         // Entry found?
274                                         if ($task > 0) {
275                                                 // Try to register the extension
276                                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ':ext_update=' . $ext_update . ',taskId=' . $task);
277                                                 $test = registerExtension($ext_update, $task, getExtensionDryRun(), false);
278
279                                                 // Reset extension name
280                                                 setCurrentExtensionName($ext_name);
281                                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
282                                                 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>');
283                                         } // END - if
284                                 } elseif ($ext_ver != getCurrentExtensionVersion()) {
285                                         // Ok, update this extension now
286                                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
287                                         $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
288                                         $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
289                                         updateExtension($ext_update, $ext_ver, getExtensionDryRun());
290                                         setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
291                                         setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
292                                         unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
293                                         unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
294                                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
295
296                                         // All okay!
297                                         $test = true;
298                                 } else {
299                                         // Nothing to register / update before...
300                                         $test = true;
301                                 }
302                         } else {
303                                 // Required file for update does not exists!
304                                 $test = true;
305                                 // But this is fine for the first time...
306                         }
307
308                         // Restore the current extension name
309                         setCurrentExtensionName($ext_name);
310                 } // END - for
311
312                 // Is there no update?
313                 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
314                         // Then test is passed!
315                         $test = true;
316                 } // END - if
317
318                 // Switch back to register mode
319                 setExtensionMode('register');
320
321                 // Remains true if extension registration reports no failures
322                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
323                 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>');
324                 $test = (($test === true) && (getExtensionReportsFailure() === false));
325                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
326                 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>');
327
328                 // Does everthing before wents ok?
329                 if ($test === true) {
330                         // "Dry-run-mode" activated?
331                         if ((getExtensionDryRun() === false) && (!isExtensionOnRemovalList())) {
332                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
333                                 // Init SQLs and transfer ext->generic
334                                 initSqls();
335                                 setSqlsArray(getExtensionSqls());
336
337                                 // Run installation pre-installation filters
338                                 runFilterChain('pre_extension_installed', array('dry_run' => getExtensionDryRun()));
339
340                                 // Register extension
341                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
342                                 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
343                                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
344                                         // New way, with CSS
345                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`,`ext_has_css`) VALUES ('%s','%s','%s','%s')",
346                                                 array(
347                                                         getCurrentExtensionName(),
348                                                         getExtensionAlwaysActive(),
349                                                         getCurrentExtensionVersion(),
350                                                         getExtensionHasCss()
351                                                 ), __FUNCTION__, __LINE__);
352                                 } else {
353                                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
354                                         // Old way, no CSS
355                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
356                                                 array(
357                                                         getCurrentExtensionName(),
358                                                         getExtensionAlwaysActive(),
359                                                         getCurrentExtensionVersion()
360                                                 ), __FUNCTION__, __LINE__);
361                                 }
362
363                                 // Remove cache file(s) if extension is active
364                                 runFilterChain('post_extension_installed', array(
365                                         'pool'     => 'extension',
366                                         'ext_name' => getCurrentExtensionName(),
367                                         'task_id'  => $task_id
368                                 ));
369
370                                 // Re-init queries and notes
371                                 initExtensionSqls(true);
372                                 initExtensionNotes();
373
374                                 // Mark it as installed
375                                 $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
376
377                                 // In normal mode return a true on success
378                                 $ret = true;
379                         } elseif (getExtensionDryRun() === true) {
380                                 // In  "dry-run" mode return array with all SQL commands
381                                 $ret = getExtensionSqls();
382
383                                 // Re-init queries and notes
384                                 initExtensionSqls(true);
385                                 initExtensionNotes();
386                         } else {
387                                 // Extension has been removed for updates, so all is fine!
388                                 $ret = true;
389                         }
390                 } else {
391                         // No, an error occurs while registering extension :-(
392                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
393                         $ret = false;
394                 }
395         } elseif (($task_id > 0) && (getCurrentExtensionName() != '')) {
396                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
397                 // Remove task from system when id and extension's name is valid
398                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
399                         array(bigintval($task_id)), __FUNCTION__, __LINE__);
400         }
401
402         // Is this the sql_patches?
403         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . '/' . getExtensionMode());
404         if ((getCurrentExtensionName() == 'sql_patches') && ((getExtensionMode() == 'register') || (getExtensionMode() == 'remove')) && (!getExtensionDryRun()) && ($test)) {
405                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ': LOAD!');
406                 if ($logout === true) {
407                         // Then redirect to logout
408                         redirectToUrl('modules.php?module=admin&amp;logout=1&amp;' . getExtensionMode() . '=sql_patches');
409                 } else {
410                         // Add temporary filter
411                         registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
412                         $GLOBALS['ext_load_mode'] = getExtensionMode();
413                 }
414         } // END - if
415
416         // Return status code
417         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ' - EXIT!');
418         //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>'.print_r($ret, true).'</pre>');
419         return $ret;
420 }
421
422 // Run SQL queries for given extension id
423 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
424 function doExtensionSqls ($ext_id, $load_mode) {
425         // This shall never do a non-admin user!
426         if (!isAdmin()) return false;
427
428         // Get extension's name
429         $ext_name = getExtensionName($ext_id);
430
431         // Set current SQL name
432         setCurrentExtensionName($ext_name);
433
434         // Init EXT_UPDATE_DEPENDS
435         if (!isExtensionUpdateDependenciesInitialized()) {
436                 // Init here...
437                 initExtensionUpdateDependencies();
438         } // END - if
439
440         // Init array
441         initExtensionSqls();
442
443         // By default no SQL has been executed
444         $sqlRan = false;
445
446         // Load extension in detected mode
447         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]=".getCurrentExtensionName()."");
448         loadExtension(getCurrentExtensionName(), $load_mode, '', false);
449
450         // Init these SQLs
451         initSqls();
452         setSqlsArray(getExtensionSqls());
453
454         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ":SQLs::count=".countSqls()."");
455         if (isSqlsValid()) {
456                 // Run SQL commands...
457                 runFilterChain('run_sqls');
458         } // END - if
459
460         // Run any filters depending on the action here
461         runFilterChain('extension_' . $load_mode);
462
463         // Remove cache file(s) if extension is active
464         if (((isExtensionActive('cache')) && ((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
465                 // Run filters
466                 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
467         } // END - if
468
469         // Is this the sql_patches?
470         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": id=".$ext_id.",currName=".getCurrentExtensionName().",loadMode=".$load_mode);
471         if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
472                 // Then redirect to logout
473                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": LOAD!");
474                 redirectToUrl('modules.php?module=admin&amp;logout=1&amp;' . $load_mode . '=sql_patches');
475         } // END - if
476 }
477
478 // Check wether the given extension is installed
479 function isExtensionInstalled ($ext_name) {
480         // We don't like empty extension names here
481         if (empty($ext_name)) {
482                 // Please fix them all
483                 debug_report_bug(__FUNCTION__.': ext_name is empty.');
484         } // END - if
485
486         // By default non is installed
487         $isInstalled = false;
488
489         // Check if there is a cache entry
490         if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
491                 // Use cache built from below queries
492                 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
493         } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
494                 // Found!
495                 $isInstalled = true;
496
497                 // Count cache hits
498                 incrementStatsEntry('cache_hits');
499         } elseif ((isInstallationPhase())) {
500                 // Extensions are all inactive/not installed during installation
501         } else {
502                 // Look in database
503                 $ext_id = getExtensionId($ext_name);
504
505                 // Do we have a record?
506                 $isInstalled = ($ext_id > 0);
507
508                 // Is it installed, then cache the entry
509                 if ($isInstalled === true) {
510                         // Dummy call (get is okay here)
511                         getExtensionId($ext_name, true);
512                 } // END - if
513
514                 // Remember the status
515                 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
516         }
517
518         // Return status
519         return $isInstalled;
520 }
521
522 // Check if given extension is active
523 function isExtensionActive ($ext_name) {
524         // Extensions are all inactive during installation
525         if ((isInstallationPhase()) || (empty($ext_name))) return false;
526
527         // Not active is the default
528         $data['ext_active'] = 'N';
529
530         // Check cache
531         if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
532                 // Load from cache
533                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
534                 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
535
536                 // Count cache hits
537                 incrementStatsEntry('cache_hits');
538         } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
539                 // @TODO Extension is loaded, what next?
540                 debug_report_bug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
541         } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
542                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
543                 // Load from database
544                 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
545                         array($ext_name), __FUNCTION__, __LINE__);
546
547                 // Entry found?
548                 if (SQL_NUMROWS($result) == 1) {
549                         // Load entry
550                         $data = SQL_FETCHARRAY($result);
551                 } // END - if
552
553                 // Free result
554                 SQL_FREERESULT($result);
555
556                 // Write cache array
557                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name."[DB]: ".$data['ext_active']."");
558                 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
559         } else {
560                 // Extension not active!
561                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.": Not active!");
562                 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
563         }
564
565         // Debug message
566         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.",active=".$data['ext_active']."");
567
568         // Is this extension activated? (For admins we always have active extensions...)
569         return ($data['ext_active'] == 'Y');
570 }
571
572 // Get version from extensions
573 function getExtensionVersion ($ext_name, $force = false) {
574         // By default no extension is found
575         $data['ext_version'] = 'false';
576
577         // Empty extension name should be fixed!
578         if (empty($ext_name)) {
579                 // Please report this bug!
580                 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
581         } // END - if
582
583         // Extensions are all inactive during installation
584         if (isInstallationPhase()) return '';
585         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ext_name=".$ext_name."");
586
587         // Is the cache written?
588         if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
589                 // Load data from cache
590                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
591                 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
592
593                 // Count cache hits
594                 incrementStatsEntry('cache_hits');
595         } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (getOutputMode() != 0)) {
596                 // Load from database
597                 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
598                         array($ext_name), __FUNCTION__, __LINE__);
599                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
600
601                 // Is the extension there?
602                 if (SQL_NUMROWS($result) == 1) {
603                         // Load entry
604                         $data = SQL_FETCHARRAY($result);
605                 } elseif (isDebugModeEnabled()) {
606                         // Not found, please report all
607                         debug_report_bug(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
608                 }
609
610                 // Free result
611                 SQL_FREERESULT($result);
612
613                 // Set cache
614                 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
615         }
616
617         // Extension version should not be invalid
618         if (($data['ext_version'] == 'false') && ($force === false)) {
619                 // Please report this trouble
620                 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
621         } // END - if
622
623         // Return result
624         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ret={$data['ext_version']}");
625         return $data['ext_version'];
626 }
627
628 // Updates a given extension with current extension version to latest version
629 function updateExtension ($ext_name, $ext_ver, $dry_run = false) {
630         // Only admins are allowed to update extensions
631         if ((!isAdmin()) || (empty($ext_name))) return false;
632
633         // Set current SQL name
634         setCurrentExtensionName($ext_name);
635
636         // Init arrays
637         initExtensionSqls();
638         initExtensionNotes();
639         initIncludePool('extension');
640
641         // Load extension in test mode
642         loadExtension($ext_name, 'test', $ext_ver, getExtensionDryRun());
643
644         // Save version history
645         $history = getExtensionVersionHistory();
646
647         // Remove old SQLs array to prevent possible bugs
648         initExtensionSqls();
649
650         // Check if version is updated
651         //* DEBUG: */ debugOutput(getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)));
652         if (((getThisExtensionVersion() != $ext_ver) || (getExtensionDryRun())) && (is_array($history))) {
653                 // Search for starting point
654                 $start = array_search($ext_ver, $history);
655
656                 // And load SQL queries in order of version history
657                 for ($idx = ($start + 1); $idx < count($history); $idx++) {
658                         // Set extension version
659                         $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
660
661                         // Load again...
662                         loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], getExtensionDryRun());
663
664                         // Get all depencies
665                         $depencies = getExtensionUpdateDependencies();
666
667                         // Nothing to apply?
668                         if (count($depencies) > 0) {
669                                 // Apply all extension depencies
670                                 foreach ($depencies as $ext_depend) {
671                                         // Did we already update/register this?
672                                         if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
673                                                 // Set it as current
674                                                 setCurrentExtensionName($ext_depend);
675
676                                                 // Mark it as already updated before we update it
677                                                 $GLOBALS['ext_updated'][$ext_depend] = true;
678
679                                                 // Is the extension there?
680                                                 if (isExtensionInstalled($ext_depend)) {
681                                                         // Update another extension first!
682                                                         $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), getExtensionDryRun());
683                                                 } else {
684                                                         // Register new extension
685                                                         $test = registerExtension($ext_depend, 0, getExtensionDryRun(), false);
686                                                 }
687                                         } // END - if
688                                 } // END - foreach
689
690                                 // Set extension version here
691                                 setCurrentExtensionVersion($ext_ver);
692
693                                 // Set name back
694                                 setCurrentExtensionName($ext_name);
695                         } // END - if
696
697                         // Add notes
698                         addExtensionNotes($history[$idx]);
699                 } // END - for
700
701                 // In real-mode execute any existing includes
702                 if (getExtensionDryRun() === false) {
703                         $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
704                         runFilterChain('load_includes', 'extension');
705                         setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
706                         unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
707                 } // END - if
708
709                 // Init these SQLs
710                 initSqls();
711                 setSqlsArray(getExtensionSqls());
712
713                 // Run SQLs
714                 runFilterChain('run_sqls', array('dry_run' => getExtensionDryRun()));
715
716                 if (getExtensionDryRun() === false) {
717                         // Run filters on success extension update
718                         runFilterChain('extension_update', getCurrentExtensionName());
719                 } // END - if
720         } // END - if
721 }
722
723 // Output verbose SQL table for extension
724 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
725         // Empty title?
726         if (empty($title)) {
727                 // Then fix it to default
728                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
729         } // END - if
730
731         // Init variables
732         $OUT = '';
733
734         // Do we have queries?
735         if ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
736                 // Init switch color
737                 $SW = 2;
738
739                 // Get all SQLs
740                 foreach (getExtensionSqls() as $sqls) {
741                         // New array format is recursive
742                         foreach ($sqls as $idx => $sql) {
743                                 // Trim out spaces
744                                 $sql = trim($sql);
745
746                                 // Output command if set
747                                 if (!empty($sql)) {
748                                         // Prepare output for template
749                                         $content = array(
750                                                 'sw'  => $SW,
751                                                 'i'   => ($idx+1),
752                                                 'sql' => $sql
753                                         );
754
755                                         // Load row template
756                                         $OUT .= loadTemplate('admin_ext_sql_row', true, $content);
757
758                                         // Switch color
759                                         $SW = 3 - $SW;
760                                 } // END - if
761                         } // END - foreach
762                 } // END - foreach
763
764                 // Prepare content for template
765                 $content = array(
766                         'width'  => $width,
767                         'dashed' => $dashed,
768                         'title'  => $title,
769                         'rows'   => $OUT
770                 );
771
772                 // Load main template
773                 $OUT = loadTemplate('admin_ext_sql_table', true, $content);
774         } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
775                 // No addional SQL commands to run
776                 $OUT = loadTemplate('admin_settings_saved', true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
777         } // END - if
778
779         // Return output
780         return $OUT;
781 }
782
783 // Get extension name from id
784 function getExtensionName ($ext_id) {
785         // Init extension name
786         $data['ext_name'] = '';
787
788         // Is cache there?
789         if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
790                 // Load from cache
791                 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
792
793                 // Count cache hits
794                 incrementStatsEntry('cache_hits');
795         } elseif (!isExtensionActive('cache')) {
796                 // Load from database
797                 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
798                         array(bigintval($ext_id)), __FUNCTION__, __LINE__);
799
800                 // Is the entry there?
801                 if (SQL_NUMROWS($result) == 1) {
802                         // Get the extension's name from database
803                         $data = SQL_FETCHARRAY($result);
804                 } // END - if
805
806                 // Free result
807                 SQL_FREERESULT($result);
808         }
809
810         // Did we find some extension?
811         if (empty($data['ext_name'])) {
812                 // We should fix these all!
813                 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
814         } // END - if
815
816         // Return the extension name
817         return $data['ext_name'];
818 }
819
820 // Get extension id from name
821 function getExtensionId ($ext_name) {
822         // Init id number
823         $data['ext_id'] = '0';
824
825         // Do we have cache?
826         if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
827                 // Load from cache
828                 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
829
830                 // Count cache hits
831                 incrementStatsEntry('cache_hits');
832         } else {
833                 // Load from database
834                 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
835                         array($ext_name), __FUNCTION__, __LINE__);
836
837                 // Is the entry there?
838                 if (SQL_NUMROWS($result) == 1) {
839                         // Get the extension's id from database
840                         $data = SQL_FETCHARRAY($result);
841                 } // END - if
842
843                 // Free result
844                 SQL_FREERESULT($result);
845
846                 // Cache it
847                 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
848         }
849
850         // Return value
851         return $data['ext_id'];
852 }
853
854 // Determines wether the given extension name is valid
855 function isExtensionNameValid ($ext_name) {
856         // Do we have cache?
857         if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
858                 // Generate include file name
859                 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
860
861                 // Is there a file in inc/extensions/ ?
862                 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
863         } // END - if
864
865         // Return result
866         return $GLOBALS['ext_name_valid'][$ext_name];
867 }
868
869 // Determines wether the given extension id is valid
870 function isExtensionIdValid ($ext_id) {
871         // Default is nothing valid
872         $isValid = false;
873
874         // Check in cache then in database
875         if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
876                 // Valid!
877                 $isValid = true;
878
879                 // Count cache hits
880                 incrementStatsEntry('cache_hits');
881         } else {
882                 // Query database
883                 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
884         }
885
886         // Return result
887         return $isValid;
888 }
889
890 // Activate given extension
891 function doActivateExtension ($ext_name) {
892         // Activate the extension
893         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
894                 array($ext_name), __FUNCTION__, __LINE__);
895
896         // Extension has been activated?
897         if (SQL_AFFECTEDROWS() == 1) {
898                 // Then run all queries
899                 doExtensionSqls(getExtensionId($ext_name), 'activate');
900         } // END - if
901 }
902
903 // Deactivate given extension
904 function doDeactivateExtension($ext_name) {
905         // Activate the extension
906         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
907                 array($ext_name), __FUNCTION__, __LINE__);
908
909         // Extension has been activated?
910         if (SQL_AFFECTEDROWS() == 1) {
911                 // Then run all queries
912                 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
913
914                 // Create new task
915                 createExtensionDeactivationTask($ext_name);
916
917                 // Notify the admin
918                 sendAdminNotification(
919                         getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
920                         'admin_ext_deactivated',
921                         array('ext_name' => $ext_name)
922                 );
923         } // END - if
924 }
925
926 // Checks wether the extension is older than given
927 function isExtensionOlder ($ext_name, $ext_ver) {
928         // Get current extension version
929         $currVersion = getExtensionVersion($ext_name);
930
931         // Remove all dots from both versions
932         $currVersion = str_replace('.', '', $currVersion);
933         $ext_ver = str_replace('.', '', $ext_ver);
934
935         // Now compare both and return the result
936         return ($currVersion < $ext_ver);
937 }
938
939 // Creates a new task for updated extension
940 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
941         // Create subject line
942         $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJ--}';
943
944         // Is the extension there?
945         if (isExtensionInstalled($ext_name)) {
946                 // Check if task is not there
947                 if (determineTaskIdBySubject($subject) == '0') {
948                         // Create extension update-task
949                         createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
950                 } // END - if
951         } else {
952                 // Extension not there! :-(
953                 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
954         }
955 }
956
957 // Creates a new task for newly installed extension
958 function createNewExtensionTask ($adminId, $subject, $ext) {
959         // Not installed and do we have created a task for the admin?
960         if ((determineTaskIdBySubject($subject) == '0') && (!isExtensionInstalled($ext))) {
961                 // Set default message if ext-foo is missing
962                 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext);
963
964                 // Template file
965                 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
966                         getConfig('PATH'),
967                         getLanguage(),
968                         $ext
969                 );
970
971                 // Load text for task if found
972                 if (isFileReadable($tpl)) {
973                         // Load extension's own text template (HTML!)
974                         $message = loadTemplate('ext_' . $ext, true);
975                 } else {
976                         // Write this in debug.log as well
977                         logDebugMessage(__FUNCTION__, __LINE__, $message);
978                 }
979
980                 // Task not created so it's a brand-new extension which we need to register and create a task for!
981                 createNewTask($subject, $message, 'EXTENSION', 0, $adminId, false);
982         } // END - if
983 }
984
985 // Creates a task for automatically deactivated (deprecated) extension
986 function createExtensionDeactivationTask ($ext) {
987         // Create subject line
988         $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
989
990         // Not installed and do we have created a task for the admin?
991         if ((determineTaskIdBySubject($subject) == '0') && (isExtensionInstalled($ext))) {
992                 // Task not created so add it
993                 createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext)), 'EXTENSION_DEACTIVATION');
994         } // END - if
995 }
996
997 // Checks if the module has a menu
998 function ifModuleHasMenu ($mod, $forceDb = false) {
999         // All is false by default
1000         $ret = false;
1001
1002         // Extension installed and newer than or has version 0.1.2?
1003         if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
1004                 // Cache version is okay, so let's check the cache!
1005                 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
1006                         // Check module cache and count hit
1007                         $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
1008                         incrementStatsEntry('cache_hits');
1009                 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$mod])) {
1010                         // Check cache and count hit
1011                         $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
1012                         incrementStatsEntry('cache_hits');
1013                 } else {
1014                         // Admin/guest/member/sponsor modules have always a menu!
1015                         $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
1016                 }
1017         } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
1018                 // Check database for entry
1019                 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
1020                         array($mod), __FUNCTION__, __LINE__);
1021
1022                 // Entry found?
1023                 if (SQL_NUMROWS($result) == 1) {
1024                         // Load "has_menu" column
1025                         $data = SQL_FETCHARRAY($result);
1026
1027                         // Fake cache... ;-)
1028                         $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
1029
1030                         // Does it have a menu?
1031                         $ret = ($data['has_menu'] == 'Y');
1032                 } // END  - if
1033
1034                 // Free memory
1035                 SQL_FREERESULT($result);
1036         } elseif (!isExtensionInstalled('sql_patches')) {
1037                 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
1038                 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
1039         } else {
1040                 // Unsupported state!
1041                 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
1042         }
1043
1044         // Return status
1045         return $ret;
1046 }
1047
1048 // Determines the task id for given extension
1049 function determineExtensionTaskId ($ext_name) {
1050         // Default is not found
1051         $data['task_id'] = '0';
1052
1053         // Search for extension task's id
1054         $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1055                 array($ext_name), __FUNCTION__, __LINE__);
1056
1057         // Entry found?
1058         if (SQL_NUMROWS($result) == 1) {
1059                 // Task found so load task's id and register extension...
1060                 $data = SQL_FETCHARRAY($result);
1061         } // END - if
1062
1063         // Free result
1064         SQL_FREERESULT($result);
1065
1066         // Return it
1067         return $data['task_id'];
1068 }
1069
1070 // Determines the task id for given subject
1071 function determineTaskIdBySubject ($subject) {
1072         // Default is not found
1073         $data['task_id'] = '0';
1074
1075         // Search for task id
1076         $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1077                 array($subject), __FUNCTION__, __LINE__);
1078
1079         // Entry found?
1080         if (SQL_NUMROWS($result) == 1) {
1081                 // Task found so load task's id and register extension...
1082                 $data = SQL_FETCHARRAY($result);
1083         } // END - if
1084
1085         // Free result
1086         SQL_FREERESULT($result);
1087
1088         // Return it
1089         return $data['task_id'];
1090 }
1091
1092 // Add updates notes for given version
1093 function addExtensionNotes ($ver) {
1094         // Init notes/content
1095         $out = ''; $content = array();
1096
1097         // Is do we have verbose output enabled?
1098         if ((!isExtensionActive('sql_patches')) || (getConfig('verbose_sql') == 'Y')) {
1099                 // Update notes found?
1100                 if (isExtensionUpdateNoteSet($ver)) {
1101                         // Update notes found
1102                         $content = array(
1103                                 'ver'   => $ver,
1104                                 'notes' => getExtensionUpdateNotes($ver)
1105                         );
1106
1107                         // Reset them
1108                         setExtensionUpdateNotes('', $ver);
1109                 } elseif (($ver == '0.0') || ($ver == '0.0.0')) {
1110                         // Initial release
1111                         $content = array(
1112                                 'ver'   => $ver,
1113                                 'notes' => getMessage('INITIAL_RELEASE')
1114                         );
1115                 } else {
1116                         // No update notes found!
1117                         $content = array(
1118                                 'ver'   => $ver,
1119                                 'notes' => getMessage('NO_UPDATE_NOTES')
1120                         );
1121                 }
1122
1123                 // Load template
1124                 $out = loadTemplate('admin_EXTENSION_notes', true, $content);
1125         } // END - if
1126
1127         // Add the notes
1128         appendExtensionNotes($out);
1129 }
1130
1131 // Getter for CSS files array
1132 function getExtensionCssFiles () {
1133         // By default no additional CSS files are found
1134         $cssFiles = array();
1135
1136         // Is the array there?
1137         if (isset($GLOBALS['css_files'])) {
1138                 // Then use it
1139                 $cssFiles = $GLOBALS['css_files'];
1140         } // END - if
1141
1142         // Return array
1143         return $cssFiles;
1144 }
1145
1146 // Init CSS files array
1147 function initExtensionCssFiles () {
1148         // Simply init it
1149         $GLOBALS['css_files'] = array();
1150 }
1151
1152 // Add new entry
1153 function addExtensionCssFile ($file) {
1154         // Is the array there?
1155         if (!isset($GLOBALS['css_files'])) {
1156                 // Then auto-init them
1157                 initExtensionCssFiles();
1158         } // END - if
1159
1160         // Add the entry
1161         $GLOBALS['css_files'][] = $file;
1162 }
1163
1164 // Setter for EXT_ALWAYS_ACTIVE flag
1165 function setExtensionAlwaysActive ($active) {
1166         $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1167 }
1168
1169 // Getter for EXT_ALWAYS_ACTIVE flag
1170 function getExtensionAlwaysActive () {
1171         return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1172 }
1173
1174 // Checks wether the current extension is always active
1175 function isExtensionAlwaysActive () {
1176         return (getExtensionAlwaysActive() == 'Y');
1177 }
1178
1179 // Setter for EXT_VERSION flag
1180 function setThisExtensionVersion ($version) {
1181         $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $version;
1182 }
1183
1184 // Getter for EXT_VERSION flag
1185 function getThisExtensionVersion () {
1186         return $GLOBALS['ext_version'][getCurrentExtensionName()];
1187 }
1188
1189 // Setter for EXT_DEPRECATED flag
1190 function setExtensionDeprecated ($deprecated) {
1191         $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1192 }
1193
1194 // Getter for EXT_DEPRECATED flag
1195 function isExtensionDeprecated () {
1196         return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1197 }
1198
1199 // Setter for EXT_UPDATE_DEPENDS flag
1200 function addExtensionUpdateDependency ($updateDepends) {
1201         //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1202         // Is the update depency empty? (NEED TO BE FIXED!)
1203         if (empty($updateDepends)) {
1204                 // Please report this bug!
1205                 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1206         } // END - if
1207
1208         // Is it not yet added?
1209         /* DEBUG:
1210         if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1211                 // Double-adding isn't fine, too
1212                 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1213         } // END - if
1214         */
1215
1216         // Add it to the list of extension update depencies map
1217         $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1218
1219         // Remember it in the list of running updates
1220         $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1221         //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1222 }
1223
1224 // Getter for running updates
1225 function getExtensionUpdatesRunning () {
1226         return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1227 }
1228
1229 // Checks wether the given extension registration is in progress
1230 function isExtensionRegistrationRunning ($ext_name) {
1231         // Simply check it
1232         $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1233
1234         // Return it
1235         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1236         return $isRunning;
1237 }
1238
1239 // Init EXT_UPDATE_DEPENDS flag
1240 function initExtensionUpdateDependencies () {
1241         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1242
1243         // Init update depency map automatically if not found
1244         if (isExtensionUpdateDependenciesInitialized()) {
1245                 // We need these bug reports as well...
1246                 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1247         } // END - if
1248
1249         $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1250
1251         // Init running updates array
1252         initExtensionRuningUpdates();
1253
1254         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1255 }
1256
1257 // Adds an extension as "registration in progress"
1258 function addExtensionRunningRegistration ($ext_name) {
1259         // Is it running?
1260         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1261         if (isExtensionRegistrationRunning($ext_name)) {
1262                 // This is really bad and should not be quietly ignored
1263                 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1264         } // END - if
1265
1266         // Then add it!
1267         $GLOBALS['ext_register_running'][] = $ext_name;
1268         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1269 }
1270
1271 // Checks wether EXT_UPDATE_DEPENDS is initialized
1272 function isExtensionUpdateDependenciesInitialized () {
1273         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1274         return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1275 }
1276
1277 // Checks wether an update is already running for given extension
1278 function isExtensionUpdateRunning ($ext_name) {
1279         // Current and given extensions means whole array
1280         if ($ext_name == getCurrentExtensionName()) {
1281                 // Default is not found
1282                 $isRunning = false;
1283
1284                 // Walk through whole array
1285                 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1286                         // Is it found?
1287                         if (($ext1 == $ext_name) || ($isRunning === true)) {
1288                                 // Found!
1289                                 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1290                                 $isRunning = true;
1291                                 break;
1292                         } // END - if
1293                 } // END - foreach
1294
1295                 // Return result
1296                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1297                 return $isRunning;
1298         } // END - if
1299
1300         // Simply check it
1301         $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1302
1303         // Return it
1304         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1305         return $isRunning;
1306 }
1307
1308 // Initializes the list of running updates
1309 function initExtensionRuningUpdates () {
1310         // Auto-init ext_running_updates
1311         if (!isset($GLOBALS['ext_running_updates'])) {
1312                 $GLOBALS['ext_running_updates'] = array();
1313                 $GLOBALS['ext_register_running'] = array();
1314         } // END - if
1315 }
1316
1317 // Getter for EXT_UPDATE_DEPENDS flag
1318 function getExtensionUpdateDependencies () {
1319         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1320         return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1321 }
1322
1323 // Getter for next iterator depency
1324 function getExtensionUpdateDependenciesIterator () {
1325         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1326         return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1327 }
1328
1329 // Counter for extension update depencies
1330 function countExtensionUpdateDependencies () {
1331         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1332         return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1333 }
1334
1335 // Removes given extension from update denpency list
1336 function removeExtensionUpdateDependency ($ext_name) {
1337         // Look it up
1338         $key = array_search($ext_name, getExtensionUpdateDependencies());
1339
1340         // Is it valid?
1341         if ($key !== false) {
1342                 // Then remove it
1343                 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1344
1345                 // And sort the array
1346                 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1347         } // END - if
1348 }
1349
1350 // Init iterator for update depencies
1351 function initExtensionUpdateIterator () {
1352         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1353         $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1354 }
1355
1356 // Getter for depency iterator
1357 function getExtensionUpdateIterator () {
1358         // Auto-init iterator
1359         if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1360
1361         // Return it
1362         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1363         return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1364 }
1365
1366 // Increments the update iterator
1367 function incrementExtensionUpdateIterator () {
1368         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1369         $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1370 }
1371
1372 // Setter for EXT_REPORTS_FAILURE flag
1373 function setExtensionReportsFailure ($reportsFailure) {
1374         $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1375 }
1376
1377 // Getter for EXT_REPORTS_FAILURE flag
1378 function getExtensionReportsFailure () {
1379         return $GLOBALS['ext_reports_failure'];
1380 }
1381
1382 // Setter for EXT_VER_HISTORY flag
1383 function setExtensionVersionHistory ($verHistory) {
1384         $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $verHistory;
1385 }
1386
1387 // Getter for EXT_VER_HISTORY array
1388 function getExtensionVersionHistory () {
1389         return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1390 }
1391
1392 // Setter for EXT_UPDATE_NOTES
1393 function setExtensionUpdateNotes ($updateNotes, $ver='') {
1394         // . '/' . getCurrentExtensionVersion()
1395         //* DEBUG: */ debug_report_bug(__FUNCTION__.':' . getCurrentExtensionName() . '/' . getExtensionMode() . '/' . $ver . '=' . $updateNotes);
1396         if (empty($ver)) {
1397                 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1398         } else {
1399                 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver] = (string) $updateNotes;
1400         }
1401 }
1402
1403 // Getter for EXT_UPDATE_NOTES
1404 function getExtensionUpdateNotes ($ver) {
1405         return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver];
1406 }
1407
1408 // Checks if ext_update_notes is set
1409 function isExtensionUpdateNoteSet ($ver) {
1410         return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver]);
1411 }
1412
1413 // Init extension notice
1414 function initExtensionNotes () {
1415         $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1416 }
1417
1418 // Append extension notice
1419 function appendExtensionNotes ($notes) {
1420         $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1421 }
1422
1423 // Getter for extension notes
1424 function getExtensionNotes () {
1425         return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1426 }
1427
1428 // Setter for current extension name
1429 function setCurrentExtensionName ($ext_name) {
1430         $GLOBALS['curr_EXTENSION_name'] = (string) trim($ext_name);
1431 }
1432
1433 // Getter for current extension name
1434 function getCurrentExtensionName () {
1435         if (isset($GLOBALS['curr_EXTENSION_name'])) {
1436                 return $GLOBALS['curr_EXTENSION_name'];
1437         } // END - if
1438
1439         // Not set!
1440         debug_report_bug(__FUNCTION__.": curr_EXTENSION_name not initialized. Please execute initExtensionSqls() before calling this function.");
1441 }
1442
1443 // Init SQLs array for current extension
1444 function initExtensionSqls ($force = false) {
1445         // Auto-init the array or if forced
1446         if ((!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) || ($force === true)) {
1447                 // Set the array
1448                 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1449
1450                 // Initialize the generic array
1451                 initSqls();
1452         } // END - if
1453 }
1454
1455 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1456 function addExtensionSql ($sql) {
1457         // Add it
1458         $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1459 }
1460
1461 // Getter for SQLs array for current extension
1462 function getExtensionSqls () {
1463         // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1464         if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1465                 // Not found, should not happen
1466                 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1467                         getCurrentExtensionName()
1468                 ));
1469         } // END - if
1470
1471         // Return the array
1472         return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1473 }
1474
1475 // Removes SQLs for current extension
1476 function unsetExtensionSqls () {
1477         unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1478 }
1479
1480 // Auto-initializes the removal list
1481 function initExtensionRemovalList () {
1482         // Is the remove list there?
1483         if (!isset($GLOBALS['ext_update_remove'])) {
1484                 // Then create it
1485                 $GLOBALS['ext_update_remove'] = array();
1486         } // END - if
1487 }
1488
1489 // Checks wether the current extension is on the removal list
1490 function isExtensionOnRemovalList () {
1491         // Init removal list
1492         initExtensionRemovalList();
1493
1494         // Is it there?
1495         return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1496 }
1497
1498 // Adds the current extension to the removal list
1499 function addCurrentExtensionToRemovalList () {
1500         // Simply add it
1501         $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1502 }
1503
1504 // Getter for removal list
1505 function getExtensionRemovalList () {
1506         // Return the removal list
1507         return $GLOBALS['ext_update_remove'];
1508 }
1509
1510 // Redirects if the provided extension is not installed
1511 function redirectOnUninstalledExtension ($ext_name) {
1512         // So is the extension there?
1513         if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1514                 // Redirect to index
1515                 redirectToUrl('modules.php?module=index&amp;code=' . getCode('EXTENSION_PROBLEM') . '&amp;ext=' . $ext_name);
1516         } // END - if
1517 }
1518
1519 // Filter for initialization of all extensions by loading them in 'init' mode
1520 function FILTER_INIT_EXTENSIONS () {
1521         // Do we have some entries?
1522         //* DEBUG */ print __FUNCTION__.': ENTRY!<br />';
1523         if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1524                 // Load all found extensions if found
1525                 //* DEBUG */ print __FUNCTION__.': CACHE - START!<br />';
1526                 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1527                         // Load it
1528                         //* DEBUG */ print __FUNCTION__.': '.$ext_name.' - START<br />';
1529                         loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1530                         //* DEBUG */ print __FUNCTION__.': '.$ext_name.' - END<br />';
1531                 } // END - foreach
1532                 //* DEBUG */ print __FUNCTION__.': CACHE - END!<br />';
1533         } // END - if
1534         //* DEBUG */ print __FUNCTION__.': EXIT!<br />';
1535 }
1536
1537 // Setter for extension mode
1538 function setExtensionMode ($ext_mode) {
1539         $GLOBALS['ext_mode'] = (string) $ext_mode;
1540 }
1541
1542 // Getter for extension mode
1543 function getExtensionMode () {
1544         return $GLOBALS['ext_mode'];
1545 }
1546
1547 // Setter for dry-run
1548 function enableExtensionDryRun ($dry_run = true) {
1549         //* DEBUG: */ debugOutput(__FUNCTION__.': '.getCurrentExtensionName().'='.intval($dry_run));
1550         $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1551 }
1552
1553 // Getter for dry-run
1554 function getExtensionDryRun () {
1555         return $GLOBALS['ext_dry_run'];
1556 }
1557
1558 // Setter for current extension version
1559 function setCurrentExtensionVersion ($ext_ver) {
1560         // ext_ver should never be empty in other modes than 'test'
1561         if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1562                 // Please report all these messages
1563                 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1564         } // END - if
1565
1566         // Add version
1567         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1568         $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1569 }
1570
1571 // Getter for current extension version
1572 function getCurrentExtensionVersion () {
1573         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1574         return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1575 }
1576
1577 // Remove the extension from cache array
1578 function removeExtensionFromArray () {
1579         // "Cache" this name
1580         $ext_name = getCurrentExtensionName();
1581
1582         // Now loop through the whole cache
1583         foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1584                 // Is it an element?
1585                 if (isset($cacheArray[$ext_name])) {
1586                         // Array element
1587                         unset($cacheArray[$ext_name]);
1588                         $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1589                 } else {
1590                         // Maybe in array?
1591                         $key = array_search($ext_name, $cacheArray);
1592
1593                         // Is it there?
1594                         if ($key !== false) {
1595                                 // Found, so remove it
1596                                 unset($cacheArray[$key]);
1597                                 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1598                         } // END - if
1599                 }
1600         } // END - foreach
1601 }
1602
1603 // "Getter" for 'extension has a CSS file' (with same name, of course)
1604 function getExtensionHasCss () {
1605         // Default is no CSS
1606         $hasCss = 'N';
1607
1608         // Construct FQFN for check
1609         $FQFN = sprintf("%stheme/%s/css/%s.css",
1610                 getConfig('PATH'),
1611                 getCurrentTheme(),
1612                 getCurrentExtensionName()
1613         );
1614
1615         // Is it there?
1616         if (isFileReadable($FQFN)) {
1617                 // Readable, so it is there...
1618                 $hasCss = 'Y';
1619         } // END - if
1620
1621         // Return it
1622         return $hasCss;
1623 }
1624
1625 // Checks wether the given extension has a language file
1626 function ifExtensionHasLanguageFile ($ext_name) {
1627         // Default is no language file
1628         $hasLanguage = false;
1629
1630         // Do we have cache?
1631         if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1632                 // Then use it
1633                 $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1634
1635                 // Count cache hits
1636                 incrementStatsEntry('cache_hits');
1637         } else {
1638                 // Not readable is default
1639                 $readable = 'N';
1640
1641                 // Is the language file readable for this extension?
1642                 if (isLanguageIncludeReadable($ext_name)) {
1643                         // Readable
1644                         $readable = 'Y';
1645                 } // END - if
1646
1647                 // Put it in cache
1648                 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = 'Y';
1649         }
1650
1651         // Return result
1652         return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1653 }
1654
1655 // Load an extension's include file
1656 function loadExtensionInclude () {
1657         // Is it readable?
1658         if (!isExtensionIncludeReadable()) {
1659                 // Not readable
1660                 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1661         } // END - if
1662
1663         // Generate INC name
1664         $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1665
1666         // Load it
1667         loadInclude($INC);
1668 }
1669
1670 // Checks wether an extension is readable
1671 function isExtensionIncludeReadable ($ext_name = '') {
1672         // If empty, use current
1673         if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1674
1675         // Array found?
1676         if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1677                 // Generate INC name
1678                 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1679
1680                 // Is it readable?
1681                 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1682         } // END - if
1683
1684         // Return result
1685         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1686         return $GLOBALS['ext_inc_readable'][$ext_name];
1687 }
1688
1689 // Checks if an extension's function file is readable
1690 function isExtensionFunctionFileReadable ($ext_name) {
1691         if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1692                 // Just count cache hits
1693                 incrementStatsEntry('cache_hits');
1694         } else {
1695                 // Construct FQFN for functions file
1696                 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1697
1698                 // Is this include there?
1699                 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1700                         // Cache it!
1701                         $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1702                 } else {
1703                         // Cache it!
1704                         $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1705                 }
1706         }
1707
1708         // Return result
1709         return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1710 }
1711
1712 // Adds an admin menu to the SQL queue of the menu entry is not found
1713 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1714         // Now check if this menu is there
1715         if (!isMenuActionValid('admin', $action, $what)) {
1716                 // Is what null?
1717                 if (is_null($what)) {
1718                         // Add main menu
1719                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1720                                 $action,
1721                                 $title,
1722                                 $descr,
1723                                 bigintval($sort)
1724                         );
1725                 } else {
1726                         // Add sub menu
1727                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1728                                 $action,
1729                                 $what,
1730                                 $title,
1731                                 $descr,
1732                                 bigintval($sort)
1733                         );
1734                 }
1735
1736                 // Add it to the queue
1737                 addExtensionSql($sql);
1738         } elseif (isDebugModeEnabled()) {
1739                 // Double menus should be located and fixed!
1740                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s, what=%s detected.", $action, $what));
1741         }
1742 }
1743
1744 // Adds a guest menu to the SQL queue if the menu entry is not found
1745 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1746         // Now check if this menu is there
1747         if (!isMenuActionValid('guest', $action, $what)) {
1748                 // Is what null?
1749                 if (is_null($what)) {
1750                         // Add main menu
1751                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1752                                 $action,
1753                                 $title,
1754                                 $visible,
1755                                 $locked,
1756                                 bigintval($sort)
1757                         );
1758                 } else {
1759                         // Add sub menu
1760                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1761                                 $action,
1762                                 $what,
1763                                 $title,
1764                                 $visible,
1765                                 $locked,
1766                                 bigintval($sort)
1767                         );
1768                 }
1769
1770                 // Add it to the queue
1771                 addExtensionSql($sql);
1772         } elseif (isDebugModeEnabled()) {
1773                 // Double menus should be located and fixed!
1774                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s, what=%s detected.", $action, $what));
1775         }
1776 }
1777
1778 // Adds a member menu to the SQL queue if the menu entry is not found
1779 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1780         // Now check if this menu is there
1781         if (!isMenuActionValid('member', $action, $what)) {
1782                 // Is what null?
1783                 if (is_null($what)) {
1784                         // Add main menu
1785                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1786                                 $action,
1787                                 $title,
1788                                 $visible,
1789                                 $locked,
1790                                 bigintval($sort)
1791                         );
1792                 } else {
1793                         // Add sub menu
1794                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1795                                 $action,
1796                                 $what,
1797                                 $title,
1798                                 $visible,
1799                                 $locked,
1800                                 bigintval($sort)
1801                         );
1802                 }
1803
1804                 // Add it to the queue
1805                 addExtensionSql($sql);
1806         } elseif (isDebugModeEnabled()) {
1807                 // Double menus should be located and fixed!
1808                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s, what=%s detected.", $action, $what));
1809         }
1810 }
1811
1812 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1813 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1814         // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1815         if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1816                 // Is what null?
1817                 if (is_null($what)) {
1818                         // Add main menu
1819                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1820                                 $action,
1821                                 $title,
1822                                 $active,
1823                                 bigintval($sort)
1824                         );
1825                 } else {
1826                         // Add sub menu
1827                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1828                                 $action,
1829                                 $what,
1830                                 $title,
1831                                 $active,
1832                                 bigintval($sort)
1833                         );
1834                 }
1835
1836                 // Add it to the queue
1837                 addExtensionSql($sql);
1838         } elseif (isDebugModeEnabled()) {
1839                 // Double menus should be located and fixed!
1840                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s, what=%s detected.", $action, $what));
1841         }
1842 }
1843
1844 // Enables/disables productive mode for current extension (used only while
1845 // registration).
1846 function enableExtensionProductive ($isProductive = true) {
1847         $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1848 }
1849
1850 // Checks wether the extension is in productive phase. If not set, development
1851 // phase (=false) is assumed.
1852 function isExtensionProductive () {
1853         return ((isset($GLOBALS['ext_productive'][getCurrentExtensionName()])) && ($GLOBALS['ext_productive'][getCurrentExtensionName()] === true));
1854 }
1855
1856 // [EOF]
1857 ?>