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