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