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