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