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