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