56f624b44b5f551eb52335629ffcd4bea16c1744
[mailer.git] / inc / extensions-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/25/2009 *
4  * ===================                          Last change: 10/25/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : extensions-functions.php                         *
8  * -------------------------------------------------------------------- *
9  * Short description : Extension management                             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Erweiterungen-Management                         *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Load the extension and maybe found language and function files.
44 function loadExtension ($ext_name, $ext_mode, $ext_ver = '0.0', $dry_run = false) {
45         // Loading an extension in same mode, but not test/update, twice is not
46         // good, so is the extension $ext_name already loaded in mode $ext_mode?
47         if ((isset($GLOBALS['loaded_extension'][$ext_name][$ext_mode])) && (!in_array($ext_mode, array('update', 'test')))) {
48                 // If this happens twice, we need the bug report from you, except for updates/tests
49                 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: ext_name=' . $ext_name . ', ext_mode='. $ext_mode . ',ext_sqls=' . print_r(getExtensionSqls(), true) . ', ext_register_running=' . print_r($GLOBALS['ext_register_running'], true) . ', ext_running_updates=' . print_r($GLOBALS['ext_running_updates'], true));
50         } // END - if
51
52         // Make sure this situation can only happen once
53         $GLOBALS['loaded_extension'][$ext_name][$ext_mode] = true;
54
55         // Set extension mode
56         setExtensionMode($ext_mode);
57
58         // Set current extension name
59         setCurrentExtensionName($ext_name);
60
61         // By default all extensions are in productive phase
62         enableExtensionProductive();
63
64         if (!empty($ext_ver)) {
65                 // Set current extension version
66                 setCurrentExtensionVersion($ext_ver);
67         } else {
68                 // Set it to 0.0 by default
69                 setCurrentExtensionVersion('0.0');
70
71                 // Is the extension installed?
72                 if ((isExtensionInstalled($ext_name)) && ($ext_mode != 'register')) {
73                         // Get extension's version
74                         setCurrentExtensionVersion(getExtensionVersion($ext_name));
75                 } // END - if
76
77                 // In all but test-mode we need these messages to debug! Please report all (together, e.g.)
78                 if (($ext_mode != 'test') && (getCurrentExtensionVersion() == '0.0')) {
79                         // Abort here, this must now always be set!
80                         debug_report_bug(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0. ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', dry_run=' . intval($dry_run));
81                 } // END - if
82         }
83
84         // Set dry-run
85         enableExtensionDryRun($dry_run);
86
87         // Init array
88         initIncludePool('extension');
89
90         // Init EXT_UPDATE_DEPENDS if not yet done
91         if (!isExtensionUpdateDependenciesInitialized()) {
92                 // Init here...
93                 initExtensionUpdateDependencies();
94         } // END - if
95
96         // Init current extension name list
97         initExtensionSqls();
98
99         // Is the extension already loaded?
100         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Loading extension ' . $ext_name . ', mode=' . getExtensionMode() . ', ver=' . getCurrentExtensionVersion());
101         if ((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', '0.0', 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                                 // Init SQLs and transfer ext->generic
345                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
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                                         // New way, with CSS
356                                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
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                                         // Old way, no CSS
366                                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
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, '0.0', 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_HASZEROAFFECTED())) || ($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))) {
657                 return false;
658         } // END - if
659
660         // Set current SQL name
661         setCurrentExtensionName($ext_name);
662
663         // Init arrays
664         initExtensionSqls();
665         initExtensionNotes();
666         initIncludePool('extension');
667
668         // Load extension in test mode
669         loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
670
671         // Save version history
672         $history = getExtensionVersionHistory();
673
674         // Remove old SQLs array to prevent possible bugs
675         initExtensionSqls();
676
677         // Check if version is updated
678         //* DEBUG: */ debugOutput(getCurrentExtensionName() . '/' . $ext_name . ':' . getThisExtensionVersion() . '/' . $ext_ver . '/' . intval(is_array($history)));
679         if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
680                 // Search for starting point
681                 $start = array_search($ext_ver, $history);
682
683                 // And load SQL queries in order of version history
684                 for ($idx = ($start + 1); $idx < count($history); $idx++) {
685                         // Set extension version
686                         $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
687
688                         // Load again...
689                         loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
690
691                         // Get all depencies
692                         $depencies = getExtensionUpdateDependencies();
693
694                         // Nothing to apply?
695                         if (count($depencies) > 0) {
696                                 // Apply all extension depencies
697                                 foreach ($depencies as $ext_depend) {
698                                         // Did we already update/register this?
699                                         if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
700                                                 // Set it as current
701                                                 setCurrentExtensionName($ext_depend);
702
703                                                 // Mark it as already updated before we update it
704                                                 $GLOBALS['ext_updated'][$ext_depend] = true;
705
706                                                 // Is the extension there?
707                                                 if (isExtensionInstalled($ext_depend)) {
708                                                         // Update another extension first!
709                                                         $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun());
710                                                 } else {
711                                                         // Register new extension
712                                                         $test = registerExtension($ext_depend, 0, isExtensionDryRun(), false);
713                                                 }
714                                         } // END - if
715                                 } // END - foreach
716
717                                 // Set extension version here
718                                 setCurrentExtensionVersion($ext_ver);
719
720                                 // Set name back
721                                 setCurrentExtensionName($ext_name);
722                         } // END - if
723
724                         // Add notes
725                         addExtensionNotes($history[$idx]);
726                 } // END - for
727
728                 // In real-mode execute any existing includes
729                 if (isExtensionDryRun() === false) {
730                         $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
731                         runFilterChain('load_includes', 'extension');
732                         setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
733                         unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
734                 } // END - if
735
736                 // Init these SQLs
737                 initSqls();
738                 setSqlsArray(getExtensionSqls());
739
740                 // Run SQLs
741                 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun(), 'enable_codes' => false));
742
743                 if (isExtensionDryRun() === false) {
744                         // Run filters on success extension update
745                         runFilterChain('extension_update', getCurrentExtensionName());
746                 } // END - if
747         } // END - if
748 }
749
750 // Output verbose SQL table for extension
751 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
752         // Empty title?
753         if (empty($title)) {
754                 // Then fix it to default
755                 $title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}';
756         } // END - if
757
758         // Init variables
759         $OUT = '';
760
761         // Do we have queries?
762         if (isVerboseSqlEnabled()) {
763                 // Do we have entries?
764                 if (countExtensionSqls() > 0) {
765                         // Init counter
766                         $idx = 0;
767                         // Get all SQLs
768                         foreach (getExtensionSqls() as $sqls) {
769                                 // New array format is recursive
770                                 foreach ($sqls as $sql) {
771                                         // Trim out spaces
772                                         $sql = trim($sql);
773
774                                         // Output command if set
775                                         if (!empty($sql)) {
776                                                 // Prepare output for template
777                                                 $content = array(
778                                                         'i'   => ($idx + 1),
779                                                         'sql' => str_replace('{', '&#123;', str_replace('}', '&#125;', encodeEntities($sql)))
780                                                 );
781
782                                                 // Load row template
783                                                 $OUT .= loadTemplate('admin_extension_sql_row', true, $content);
784
785                                                 // Count up
786                                                 $idx++;
787                                         } // END - if
788                                 } // END - foreach
789                         } // END - foreach
790
791                         // Prepare content for template
792                         $content = array(
793                                 'width'  => $width,
794                                 'dashed' => $dashed,
795                                 'title'  => $title,
796                                 'rows'   => $OUT
797                         );
798
799                         // Load main template
800                         $OUT = loadTemplate('admin_extension_sql_table', true, $content);
801                 } else {
802                         // No addional SQL commands to run
803                         $OUT = displayMessage('{--ADMIN_NO_ADDITIONAL_SQLS--}', true);
804                 }
805         } // END - if
806
807         // Return output
808         return $OUT;
809 }
810
811 // Get extension name from id
812 function getExtensionName ($ext_id) {
813         // Init extension name
814         $data['ext_name'] = '';
815
816         // Is cache there?
817         if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
818                 // Load from cache
819                 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
820
821                 // Count cache hits
822                 incrementStatsEntry('cache_hits');
823         } elseif (!isExtensionActive('cache')) {
824                 // Load from database
825                 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
826                         array(bigintval($ext_id)), __FUNCTION__, __LINE__);
827
828                 // Is the entry there?
829                 if (SQL_NUMROWS($result) == 1) {
830                         // Get the extension's name from database
831                         $data = SQL_FETCHARRAY($result);
832                 } // END - if
833
834                 // Free result
835                 SQL_FREERESULT($result);
836         }
837
838         // Did we find some extension?
839         if (empty($data['ext_name'])) {
840                 // We should fix these all!
841                 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
842         } // END - if
843
844         // Return the extension name
845         return $data['ext_name'];
846 }
847
848 // Get extension id from name
849 function getExtensionId ($ext_name) {
850         // Init id number
851         $data['ext_id'] = '0';
852
853         // Do we have cache?
854         if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
855                 // Load from cache
856                 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
857
858                 // Count cache hits
859                 incrementStatsEntry('cache_hits');
860         } else {
861                 // Load from database
862                 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
863                         array($ext_name), __FUNCTION__, __LINE__);
864
865                 // Is the entry there?
866                 if (SQL_NUMROWS($result) == 1) {
867                         // Get the extension's id from database
868                         $data = SQL_FETCHARRAY($result);
869                 } // END - if
870
871                 // Free result
872                 SQL_FREERESULT($result);
873
874                 // Cache it
875                 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
876         }
877
878         // Return value
879         return $data['ext_id'];
880 }
881
882 // Determines wether the given extension name is valid
883 function isExtensionNameValid ($ext_name) {
884         // Do we have cache?
885         if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
886                 // Generate include file name
887                 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
888
889                 // Is there a file in inc/extensions/ ?
890                 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
891         } // END - if
892
893         // Return result
894         return $GLOBALS['ext_name_valid'][$ext_name];
895 }
896
897 // Determines wether the given extension id is valid
898 function isExtensionIdValid ($ext_id) {
899         // Default is nothing valid
900         $isValid = false;
901
902         // Check in cache then in database
903         if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
904                 // Valid!
905                 $isValid = true;
906
907                 // Count cache hits
908                 incrementStatsEntry('cache_hits');
909         } else {
910                 // Query database
911                 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
912         }
913
914         // Return result
915         return $isValid;
916 }
917
918 // Activate given extension
919 function doActivateExtension ($ext_name) {
920         // Is the extension installed?
921         if (!isExtensionInstalled($ext_name)) {
922                 // Non-installed extensions cannot be activated
923                 debug_report_bug(__FUNCTION__, __LINE__, 'Tried to activate non-installed extension ' . $ext_name);
924         } // END - if
925
926         // Activate the extension
927         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
928                 array($ext_name), __FUNCTION__, __LINE__);
929
930         // Then run all queries
931         doExtensionSqls(getExtensionId($ext_name), 'activate');
932 }
933
934 // Deactivate given extension
935 function doDeactivateExtension ($ext_name) {
936         // Is the extension installed?
937         if (!isExtensionInstalled($ext_name)) {
938                 // Non-installed extensions cannot be activated
939                 debug_report_bug(__FUNCTION__, __LINE__, 'Tried to deactivate non-installed extension ' . $ext_name);
940         } // END - if
941
942         // Activate the extension
943         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
944                 array($ext_name), __FUNCTION__, __LINE__);
945
946         // Then run all queries
947         doExtensionSqls(getExtensionId($ext_name), 'deactivate');
948
949         // Create new task (we ignore the task id here)
950         createExtensionDeactivationTask($ext_name);
951
952         // Notify the admin
953         sendAdminNotification(
954                 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
955                 'admin_extension_deactivated',
956                 array('ext_name' => $ext_name)
957         );
958 }
959
960 // Checks wether the extension is older than given
961 function isExtensionOlder ($ext_name, $ext_ver) {
962         // Get current extension version
963         $currVersion = getExtensionVersion($ext_name);
964
965         // Remove all dots from both versions
966         $currVersion = str_replace('.', '', $currVersion);
967         $ext_ver = str_replace('.', '', $ext_ver);
968
969         // Now compare both and return the result
970         return ($currVersion < $ext_ver);
971 }
972
973 // Creates a new task for updated extension
974 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
975         // Create subject line
976         $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
977
978         // Get task id
979         $taskId = determineTaskIdBySubject($subject);
980
981         // Is the extension there?
982         if (isExtensionInstalled($ext_name)) {
983                 // Check if task is not there
984                 if ($taskId == '0') {
985                         // Create extension update-task
986                         $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
987                 } // END - if
988         } else {
989                 // Extension not there! :-(
990                 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
991         }
992
993         // Return task id
994         return $taskId;
995 }
996
997 // Creates a new task for newly installed extension
998 function createNewExtensionTask ($ext_name) {
999         // Generate subject line
1000         $subject = sprintf("[%s:]", $ext_name);
1001
1002         // Get task id
1003         $taskId = determineTaskIdBySubject($subject);
1004
1005         // Not installed and do we have created a task for the admin?
1006         if (($taskId == '0') && (!isExtensionInstalled($ext_name))) {
1007                 // Set default message if ext-foo is missing
1008                 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext_name);
1009
1010                 // Template file
1011                 $FQFN = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
1012                         getPath(),
1013                         getLanguage(),
1014                         $ext_name
1015                 );
1016
1017                 // Load text for task if found
1018                 if (isFileReadable($FQFN)) {
1019                         // Load extension's description template (but do not compile the code)
1020                         $message = loadTemplate('ext_' . $ext_name, true, array(), false);
1021                 } else {
1022                         // Write this in debug.log as well
1023                         logDebugMessage(__FUNCTION__, __LINE__, $message);
1024                 }
1025
1026                 // Task not created so it's a brand-new extension which we need to register and create a task for!
1027                 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), false);
1028         } // END - if
1029
1030         // Return task id
1031         return $taskId;
1032 }
1033
1034 // Creates a task for automatically deactivated (deprecated) extension
1035 function createExtensionDeactivationTask ($ext_name) {
1036         // Create subject line
1037         $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1038
1039         // Get task id
1040         $taskId = determineTaskIdBySubject($subject);
1041
1042         // Not installed and do we have created a task for the admin?
1043         if (($taskId == '0') && (isExtensionInstalled($ext_name))) {
1044                 // Task not created so add it
1045                 $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext_name)), 'EXTENSION_DEACTIVATION');
1046         } // END - if
1047
1048         // Return task id
1049         return $taskId;
1050 }
1051
1052 // Determines the task id for given extension
1053 function determineExtensionTaskId ($ext_name) {
1054         // Default is not found
1055         $data['task_id'] = '0';
1056
1057         // Search for extension task's id
1058         $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1059                 array($ext_name), __FUNCTION__, __LINE__);
1060
1061         // Entry found?
1062         if (SQL_NUMROWS($result) == 1) {
1063                 // Task found so load task's id and register extension...
1064                 $data = SQL_FETCHARRAY($result);
1065         } // END - if
1066
1067         // Free result
1068         SQL_FREERESULT($result);
1069
1070         // Return it
1071         return $data['task_id'];
1072 }
1073
1074 // Determines the task id for given subject
1075 function determineTaskIdBySubject ($subject) {
1076         // Default is not found
1077         $data['task_id'] = '0';
1078
1079         // Search for task id
1080         $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1081                 array($subject), __FUNCTION__, __LINE__);
1082
1083         // Entry found?
1084         if (SQL_NUMROWS($result) == 1) {
1085                 // Task found so load task's id and register extension...
1086                 $data = SQL_FETCHARRAY($result);
1087         } // END - if
1088
1089         // Free result
1090         SQL_FREERESULT($result);
1091
1092         // Return it
1093         return $data['task_id'];
1094 }
1095
1096 // Add updates notes for given version
1097 function addExtensionNotes ($ext_ver) {
1098         // Init notes/content
1099         $out = '';
1100         $content = array();
1101
1102         // Is do we have verbose output enabled?
1103         if ((!isExtensionActive('sql_patches')) || (isVerboseSqlEnabled())) {
1104                 // Update notes found?
1105                 if (isExtensionUpdateNoteSet($ext_ver)) {
1106                         // Update notes found
1107                         $content = array(
1108                                 'ver'   => $ext_ver,
1109                                 'notes' => getExtensionUpdateNotes($ext_ver)
1110                         );
1111
1112                         // Reset them
1113                         setExtensionUpdateNotes('', $ext_ver);
1114                 } elseif (in_array($ext_ver, array('0.0', '0.0.0'))) {
1115                         // Initial release
1116                         $content = array(
1117                                 'ver'   => $ext_ver,
1118                                 'notes' => '{--INITIAL_RELEASE--}'
1119                         );
1120                 } else {
1121                         // No update notes found
1122                         $content = array(
1123                                 'ver'   => $ext_ver,
1124                                 'notes' => '{--NO_UPDATE_NOTES--}'
1125                         );
1126                 }
1127
1128                 // Load template
1129                 $out = loadTemplate('admin_extension_notes', true, $content);
1130         } // END - if
1131
1132         // Add the notes
1133         appendExtensionNotes($out);
1134 }
1135
1136 // Getter for CSS files array
1137 function getExtensionCssFiles () {
1138         // By default no additional CSS files are found
1139         $cssFiles = array();
1140
1141         // Is the array there?
1142         if (isset($GLOBALS['css_files'])) {
1143                 // Then use it
1144                 $cssFiles = $GLOBALS['css_files'];
1145         } // END - if
1146
1147         // Return array
1148         return $cssFiles;
1149 }
1150
1151 // Init CSS files array
1152 function initExtensionCssFiles () {
1153         // Simply init it
1154         $GLOBALS['css_files'] = array();
1155 }
1156
1157 // Add new entry
1158 function addExtensionCssFile ($file) {
1159         // Is the array there?
1160         if (!isset($GLOBALS['css_files'])) {
1161                 // Then auto-init them
1162                 initExtensionCssFiles();
1163         } // END - if
1164
1165         // Add the entry
1166         $GLOBALS['css_files'][] = $file;
1167 }
1168
1169 // Setter for EXT_ALWAYS_ACTIVE flag
1170 function setExtensionAlwaysActive ($active) {
1171         $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1172 }
1173
1174 // Getter for EXT_ALWAYS_ACTIVE flag
1175 function getExtensionAlwaysActive () {
1176         return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1177 }
1178
1179 // Checks wether the current extension is always active
1180 function isExtensionAlwaysActive () {
1181         return (getExtensionAlwaysActive() == 'Y');
1182 }
1183
1184 // Setter for EXT_VERSION flag
1185 function setThisExtensionVersion ($ext_version) {
1186         $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1187 }
1188
1189 // Getter for EXT_VERSION flag
1190 function getThisExtensionVersion () {
1191         return $GLOBALS['ext_version'][getCurrentExtensionName()];
1192 }
1193
1194 // Setter for EXT_DEPRECATED flag
1195 function setExtensionDeprecated ($deprecated) {
1196         $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1197 }
1198
1199 // Getter for EXT_DEPRECATED flag
1200 function isExtensionDeprecated () {
1201         return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1202 }
1203
1204 // Setter for EXT_UPDATE_DEPENDS flag
1205 function addExtensionDependency ($updateDepends) {
1206         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1207         // Is the update depency empty? (NEED TO BE FIXED!)
1208         if (empty($updateDepends)) {
1209                 // Please report this bug!
1210                 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1211         } // END - if
1212
1213         // Is it not yet added?
1214         /* Only for debugging!
1215         if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1216                 // Double-adding isn't fine, too
1217                 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1218         } // END - if
1219         */
1220
1221         // Add it to the list of extension update depencies map
1222         $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1223
1224         // Remember it in the list of running updates
1225         $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1226         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1227 }
1228
1229 // Getter for running updates
1230 function getExtensionUpdatesRunning () {
1231         return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1232 }
1233
1234 // Checks wether the given extension registration is in progress
1235 function isExtensionRegistrationRunning ($ext_name) {
1236         // Simply check it
1237         $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1238
1239         // Return it
1240         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1241         return $isRunning;
1242 }
1243
1244 // Init EXT_UPDATE_DEPENDS flag
1245 function initExtensionUpdateDependencies () {
1246         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1247
1248         // Init update depency map automatically if not found
1249         if (isExtensionUpdateDependenciesInitialized()) {
1250                 // We need these bug reports as well...
1251                 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1252         } // END - if
1253
1254         $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1255
1256         // Init running updates array
1257         initExtensionRuningUpdates();
1258
1259         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1260 }
1261
1262 // Adds an extension as "registration in progress"
1263 function addExtensionRunningRegistration ($ext_name) {
1264         // Is it running?
1265         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1266         if (isExtensionRegistrationRunning($ext_name)) {
1267                 // This is really bad and should not be quietly ignored
1268                 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1269         } // END - if
1270
1271         // Then add it!
1272         $GLOBALS['ext_register_running'][] = $ext_name;
1273         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1274 }
1275
1276 // Checks wether EXT_UPDATE_DEPENDS is initialized
1277 function isExtensionUpdateDependenciesInitialized () {
1278         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1279         return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1280 }
1281
1282 // Checks wether an update is already running for given extension
1283 function isExtensionUpdateRunning ($ext_name) {
1284         // Current and given extensions means whole array
1285         if ($ext_name == getCurrentExtensionName()) {
1286                 // Default is not found
1287                 $isRunning = false;
1288
1289                 // Walk through whole array
1290                 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1291                         // Is it found?
1292                         if (($ext1 == $ext_name) || ($isRunning === true)) {
1293                                 // found
1294                                 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1295                                 $isRunning = true;
1296                                 break;
1297                         } // END - if
1298                 } // END - foreach
1299
1300                 // Return result
1301                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1302                 return $isRunning;
1303         } // END - if
1304
1305         // Simply check it
1306         $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1307
1308         // Return it
1309         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1310         return $isRunning;
1311 }
1312
1313 // Initializes the list of running updates
1314 function initExtensionRuningUpdates () {
1315         // Auto-init ext_running_updates
1316         if (!isset($GLOBALS['ext_running_updates'])) {
1317                 $GLOBALS['ext_running_updates'] = array();
1318                 $GLOBALS['ext_register_running'] = array();
1319         } // END - if
1320 }
1321
1322 // Getter for EXT_UPDATE_DEPENDS flag
1323 function getExtensionUpdateDependencies () {
1324         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1325         return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1326 }
1327
1328 // Getter for next iterator depency
1329 function getExtensionUpdateDependenciesIterator () {
1330         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1331         return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1332 }
1333
1334 // Counter for extension update depencies
1335 function countExtensionUpdateDependencies () {
1336         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1337         return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1338 }
1339
1340 // Removes given extension from update denpency list
1341 function removeExtensionDependency ($ext_name) {
1342         // Look it up
1343         $key = array_search($ext_name, getExtensionUpdateDependencies());
1344
1345         // Is it valid?
1346         if ($key !== false) {
1347                 // Then remove it
1348                 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1349
1350                 // And sort the array
1351                 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1352         } // END - if
1353 }
1354
1355 // Init iterator for update depencies
1356 function initExtensionUpdateIterator () {
1357         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1358         $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1359 }
1360
1361 // Getter for depency iterator
1362 function getExtensionUpdateIterator () {
1363         // Auto-init iterator
1364         if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1365
1366         // Return it
1367         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1368         return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1369 }
1370
1371 // Increments the update iterator
1372 function incrementExtensionUpdateIterator () {
1373         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1374         $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1375 }
1376
1377 // Setter for EXT_REPORTS_FAILURE flag
1378 function setExtensionReportsFailure ($reportsFailure) {
1379         $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1380 }
1381
1382 // Getter for EXT_REPORTS_FAILURE flag
1383 function getExtensionReportsFailure () {
1384         return $GLOBALS['ext_reports_failure'];
1385 }
1386
1387 // Setter for EXT_VER_HISTORY flag
1388 function setExtensionVersionHistory ($versionHistory) {
1389         $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1390 }
1391
1392 // Getter for EXT_VER_HISTORY array
1393 function getExtensionVersionHistory () {
1394         return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1395 }
1396
1397 // Setter for EXT_UPDATE_NOTES
1398 function setExtensionUpdateNotes ($updateNotes, $ext_ver = '') {
1399         // . '/' . getCurrentExtensionVersion()
1400         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ',getExtensionMode()=' . getExtensionMode() . ',ext_ver=' . $ext_ver . ',updateNotes()=' . strlen($updateNotes));
1401         if (empty($ext_ver)) {
1402                 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1403         } else {
1404                 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1405         }
1406 }
1407
1408 // Getter for EXT_UPDATE_NOTES
1409 function getExtensionUpdateNotes ($ext_ver) {
1410         return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1411 }
1412
1413 // Checks if ext_update_notes is set
1414 function isExtensionUpdateNoteSet ($ext_ver) {
1415         return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
1416 }
1417
1418 // Init extension notice
1419 function initExtensionNotes ($force = false) {
1420         // Is it already initialized?
1421         if (($force === false) && (isset($GLOBALS['ext_notes'][getCurrentExtensionName()]))) {
1422                 // This is mostly not wanted, so please report it
1423                 debug_report_bug(__FUNCTION__, __LINE__, 'ext_notes already set for extension ' . getCurrentExtensionName());
1424         } // END - if
1425
1426         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1427         $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1428 }
1429
1430 // Append extension notice
1431 function appendExtensionNotes ($notes) {
1432         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ', notes()=' . strlen($notes));
1433         $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1434 }
1435
1436 // Getter for extension notes
1437 function getExtensionNotes () {
1438         return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1439 }
1440
1441 // Setter for current extension name
1442 function setCurrentExtensionName ($ext_name) {
1443         $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1444 }
1445
1446 // Getter for current extension name
1447 function getCurrentExtensionName () {
1448         if (!isset($GLOBALS['curr_extension_name'])) {
1449                 // Not set!
1450                 debug_report_bug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1451         } // END - if
1452
1453         // Return it
1454         return $GLOBALS['curr_extension_name'];
1455 }
1456
1457 // Init SQLs array for current extension
1458 function initExtensionSqls ($force = false) {
1459         // Auto-init the array or if forced
1460         if (($force === true) || (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()]))) {
1461                 // Set the array
1462                 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1463
1464                 // Initialize the generic array
1465                 initSqls();
1466         } // END - if
1467 }
1468
1469 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1470 function addExtensionSql ($sql) {
1471         // Add it
1472         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',ext_version=' . getCurrentExtensionVersion() . ',sql=' . $sql);
1473         $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1474 }
1475
1476 // Getter for SQLs array for current extension
1477 function getExtensionSqls () {
1478         // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1479         if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1480                 // Not found, should not happen
1481                 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1482                         getCurrentExtensionName()
1483                 ));
1484         } // END - if
1485
1486         // Return the array
1487         return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1488 }
1489
1490 // Count SQLs for current extension
1491 function countExtensionSqls () {
1492         // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1493         if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1494                 // Not found, should not happen
1495                 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1496                         getCurrentExtensionName()
1497                 ));
1498         } // END - if
1499
1500         // Count them all
1501         return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1502 }
1503
1504 // Removes SQLs for current extension
1505 function unsetExtensionSqls () {
1506         unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1507 }
1508
1509 // Auto-initializes the removal list
1510 function initExtensionRemovalList () {
1511         // Is the remove list there?
1512         if (!isset($GLOBALS['ext_update_remove'])) {
1513                 // Then create it
1514                 $GLOBALS['ext_update_remove'] = array();
1515         } // END - if
1516 }
1517
1518 // Checks wether the current extension is on the removal list
1519 function isExtensionOnRemovalList () {
1520         // Init removal list
1521         initExtensionRemovalList();
1522
1523         // Is it there?
1524         return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1525 }
1526
1527 // Adds the current extension to the removal list
1528 function addCurrentExtensionToRemovalList () {
1529         // Simply add it
1530         $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1531 }
1532
1533 // Getter for removal list
1534 function getExtensionRemovalList () {
1535         // Return the removal list
1536         return $GLOBALS['ext_update_remove'];
1537 }
1538
1539 // Redirects if the provided extension is not installed
1540 function redirectOnUninstalledExtension ($ext_name) {
1541         // So is the extension there?
1542         if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1543                 // Redirect to index
1544                 redirectToUrl('modules.php?module=index&amp;code=' . getCode('EXTENSION_PROBLEM') . '&amp;ext=' . $ext_name);
1545         } // END - if
1546 }
1547
1548 // Filter for initialization of all extensions by loading them in 'init' mode
1549 function FILTER_INIT_EXTENSIONS () {
1550         // Do we have some entries?
1551         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY!');
1552         if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1553                 // Load all found extensions if found
1554                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
1555                 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1556                         // Load it
1557                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - START');
1558                         loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1559                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - END');
1560                 } // END - foreach
1561                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1562         } // END - if
1563         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1564 }
1565
1566 // Setter for extension mode
1567 function setExtensionMode ($ext_mode) {
1568         $GLOBALS['ext_mode'] = (string) $ext_mode;
1569 }
1570
1571 // Getter for extension mode
1572 function getExtensionMode () {
1573         return $GLOBALS['ext_mode'];
1574 }
1575
1576 // Setter for dry-run
1577 function enableExtensionDryRun ($dry_run = true) {
1578         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()='.getCurrentExtensionName().',dry='.intval($dry_run));
1579         $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1580 }
1581
1582 // Getter for dry-run
1583 function isExtensionDryRun () {
1584         return $GLOBALS['ext_dry_run'];
1585 }
1586
1587 // Setter for current extension version
1588 function setCurrentExtensionVersion ($ext_ver) {
1589         // ext_ver should never be empty in other modes than 'test'
1590         if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1591                 // Please report all these messages
1592                 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1593         } // END - if
1594
1595         // Add version
1596         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1597         $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1598 }
1599
1600 // Getter for current extension version
1601 function getCurrentExtensionVersion () {
1602         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1603         return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1604 }
1605
1606 // Remove the extension from cache array
1607 function removeExtensionFromArray () {
1608         // "Cache" this name
1609         $ext_name = getCurrentExtensionName();
1610
1611         // Now loop through the whole cache
1612         foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1613                 // Is it an element?
1614                 if (isset($cacheArray[$ext_name])) {
1615                         // Array element
1616                         unset($cacheArray[$ext_name]);
1617                         $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1618                 } else {
1619                         // Maybe in array?
1620                         $key = array_search($ext_name, $cacheArray);
1621
1622                         // Is it there?
1623                         if ($key !== false) {
1624                                 // Found, so remove it
1625                                 unset($cacheArray[$key]);
1626                                 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1627                         } // END - if
1628                 }
1629         } // END - foreach
1630 }
1631
1632 // "Getter" for 'extension has a CSS file' (with same name, of course)
1633 function getExtensionHasCss () {
1634         // Default is no CSS
1635         $hasCss = 'N';
1636
1637         // Construct FQFN for check
1638         $FQFN = sprintf("%stheme/%s/css/%s.css",
1639                 getPath(),
1640                 getCurrentTheme(),
1641                 getCurrentExtensionName()
1642         );
1643
1644         // Is it there?
1645         if (isFileReadable($FQFN)) {
1646                 // Readable, so it is there...
1647                 $hasCss = 'Y';
1648         } // END - if
1649
1650         // Return it
1651         return $hasCss;
1652 }
1653
1654 // Checks wether the given extension has a language file
1655 function ifExtensionHasLanguageFile ($ext_name) {
1656         // Do we have cache?
1657         if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1658                 // Count cache hits
1659                 incrementStatsEntry('cache_hits');
1660         } else {
1661                 // Not readable is default
1662                 $readable = 'N';
1663
1664                 // Is the language file readable for this extension?
1665                 if (isLanguageIncludeReadable($ext_name)) {
1666                         // Readable
1667                         $readable = 'Y';
1668                 } // END - if
1669
1670                 // Put it in cache
1671                 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = $readable;
1672         }
1673
1674         // Return result
1675         return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1676 }
1677
1678 // Load an extension's include file
1679 function loadExtensionInclude () {
1680         // Is it readable?
1681         if (!isExtensionIncludeReadable()) {
1682                 // Not readable
1683                 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1684         } // END - if
1685
1686         // Generate INC name
1687         $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1688
1689         // Load it
1690         loadInclude($INC);
1691 }
1692
1693 // Checks wether an extension is readable
1694 function isExtensionIncludeReadable ($ext_name = '') {
1695         // If empty, use current
1696         if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1697
1698         // Array found?
1699         if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1700                 // Generate INC name
1701                 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1702
1703                 // Is it readable?
1704                 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1705         } // END - if
1706
1707         // Return result
1708         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1709         return $GLOBALS['ext_inc_readable'][$ext_name];
1710 }
1711
1712 // Checks if an extension's function file is readable
1713 function isExtensionFunctionFileReadable ($ext_name) {
1714         // Is cache there?
1715         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
1716         if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1717                 // Just count cache hits
1718                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] .' - CACHE!');
1719                 incrementStatsEntry('cache_hits');
1720         } else {
1721                 // Construct IFN for functions file
1722                 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1723
1724                 // Is this include there?
1725                 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1726                         // Cache it!
1727                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=Y - FOUND!');
1728                         $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1729                 } else {
1730                         // Cache it!
1731                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=N - NOT FOUND!');
1732                         $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1733                 }
1734         }
1735
1736         // Return result
1737         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name]);
1738         return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1739 }
1740
1741 // Adds a DROP TABLE statement if the requested tabled is there
1742 function addDropTableSql ($table) {
1743         // Is the table there?
1744         if (isSqlTableCreated($table)) {
1745                 // Then add it, non-existing tables can be ignored because it will
1746                 // happen with every newly installed extension.
1747                 addExtensionSql('DROP TABLE `{?_MYSQL_PREFIX?}_' . $table . '`');
1748         } // END - if
1749 }
1750
1751 // Adds an admin menu to the SQL queue of the menu entry is not found
1752 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1753         // Now check if this menu is there
1754         if (!isMenuActionValid('admin', $action, $what)) {
1755                 // Is what null?
1756                 if (is_null($what)) {
1757                         // Add main menu
1758                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1759                                 $action,
1760                                 $title,
1761                                 $descr,
1762                                 bigintval($sort)
1763                         );
1764                 } else {
1765                         // Add sub menu
1766                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1767                                 $action,
1768                                 $what,
1769                                 $title,
1770                                 $descr,
1771                                 bigintval($sort)
1772                         );
1773                 }
1774
1775                 // Add it to the queue
1776                 addExtensionSql($sql);
1777         } elseif (isDebugModeEnabled()) {
1778                 // Double menus should be located and fixed!
1779                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
1780         }
1781 }
1782
1783 // Adds a guest menu to the SQL queue if the menu entry is not found
1784 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1785         // Now check if this menu is there
1786         if (!isMenuActionValid('guest', $action, $what)) {
1787                 // Is what null?
1788                 if (is_null($what)) {
1789                         // Add main menu
1790                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1791                                 $action,
1792                                 $title,
1793                                 $visible,
1794                                 $locked,
1795                                 bigintval($sort)
1796                         );
1797                 } else {
1798                         // Add sub menu
1799                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1800                                 $action,
1801                                 $what,
1802                                 $title,
1803                                 $visible,
1804                                 $locked,
1805                                 bigintval($sort)
1806                         );
1807                 }
1808
1809                 // Add it to the queue
1810                 addExtensionSql($sql);
1811         } elseif (isDebugModeEnabled()) {
1812                 // Double menus should be located and fixed!
1813                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s,what=%s,title=%s,locked=%s,visible=%s detected.", $action, $what, $title, $locked, $visible));
1814         }
1815 }
1816
1817 // Adds a member menu to the SQL queue if the menu entry is not found
1818 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1819         // Now check if this menu is there
1820         if (!isMenuActionValid('member', $action, $what)) {
1821                 // Is what null?
1822                 if (is_null($what)) {
1823                         // Add main menu
1824                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1825                                 $action,
1826                                 $title,
1827                                 $visible,
1828                                 $locked,
1829                                 bigintval($sort)
1830                         );
1831                 } else {
1832                         // Add sub menu
1833                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1834                                 $action,
1835                                 $what,
1836                                 $title,
1837                                 $visible,
1838                                 $locked,
1839                                 bigintval($sort)
1840                         );
1841                 }
1842
1843                 // Add it to the queue
1844                 addExtensionSql($sql);
1845         } elseif (isDebugModeEnabled()) {
1846                 // Double menus should be located and fixed!
1847                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s,what=%s,title=%s,visivle=%s,locked=%s detected.", $action, $what, $title, $visible, $locked));
1848         }
1849 }
1850
1851 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1852 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1853         // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1854         if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1855                 // Is what null?
1856                 if (is_null($what)) {
1857                         // Add main menu
1858                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1859                                 $action,
1860                                 $title,
1861                                 $active,
1862                                 bigintval($sort)
1863                         );
1864                 } else {
1865                         // Add sub menu
1866                         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1867                                 $action,
1868                                 $what,
1869                                 $title,
1870                                 $active,
1871                                 bigintval($sort)
1872                         );
1873                 }
1874
1875                 // Add it to the queue
1876                 addExtensionSql($sql);
1877         } elseif (isDebugModeEnabled()) {
1878                 // Double menus should be located and fixed!
1879                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s,what=%s,title=%s,active=%s detected.", $action, $what, $title, $active));
1880         }
1881 }
1882
1883 // Enables/disables productive mode for current extension (used only while
1884 // registration).
1885 function enableExtensionProductive ($isProductive = true) {
1886         // Log debug message
1887         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%d", getCurrentExtensionName(), intval($isProductive)));
1888
1889         // Set it
1890         $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1891 }
1892
1893 // Checks wether the extension is in productive phase. If not set, development
1894 // phase (=false) is assumed.
1895 function isExtensionProductive ($ext_name = '') {
1896         // Is the extension name empty? Then use current
1897         if (empty($ext_name)) {
1898                 // Get current extension name
1899                 $ext_name = getCurrentExtensionName();
1900         } // END - if
1901
1902         // Do we have cache?
1903         if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
1904                 // Load extension only if not yet loaded
1905                 if (!isset($GLOBALS['ext_productive'][$ext_name])) {
1906                         // Load extension in test mode
1907                         loadExtension($ext_name, 'test');
1908                 } // END - if
1909
1910                 // Determine it
1911                 $GLOBALS[__FUNCTION__][$ext_name] = ((isset($GLOBALS['ext_productive'][$ext_name])) && ($GLOBALS['ext_productive'][$ext_name] === true));
1912         } // END - if
1913
1914         // Log debug message
1915         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%s", $ext_name, intval($GLOBALS[__FUNCTION__][$ext_name])));
1916
1917         // Return result
1918         return $GLOBALS[__FUNCTION__][$ext_name];
1919 }
1920
1921 // [EOF]
1922 ?>