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