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