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