2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 10/25/2009 *
4 * =================== Last change: 10/25/2009 *
6 * -------------------------------------------------------------------- *
7 * File : extensions-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Extension management *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Erweiterungen-Management *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
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));
52 // Make sure this situation can only happen once
53 $GLOBALS['loaded_extension'][$ext_name][$ext_mode] = true;
56 setExtensionMode($ext_mode);
58 // Set current extension name
59 setCurrentExtensionName($ext_name);
61 // By default all extensions are in productive phase
62 enableExtensionProductive();
64 if (!empty($ext_ver)) {
65 // Set current extension version
66 setCurrentExtensionVersion($ext_ver);
68 // Set it to 0.0 by default
69 setCurrentExtensionVersion('0.0');
71 // Is the extension installed?
72 if ((isExtensionInstalled($ext_name)) && ($ext_mode != 'register')) {
73 // Get extension's version
74 setCurrentExtensionVersion(getExtensionVersion($ext_name));
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));
85 enableExtensionDryRun($dry_run);
88 initIncludePool('extension');
90 // Init EXT_UPDATE_DEPENDS if not yet done
91 if (!isExtensionUpdateDependenciesInitialized()) {
93 initExtensionUpdateDependencies();
96 // Init current extension name list
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')) {
103 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
109 // Is the extension file NOT there?
110 if (!isExtensionNameValid($ext_name)) {
112 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable.", $ext_name));
118 // Load extension's own language file if not in test mode
119 if ((getExtensionMode() != 'test') && (ifExtensionHasLanguageFile($ext_name))) {
121 loadLanguageFile($ext_name);
125 if (isExtensionFunctionFileReadable($ext_name)) {
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);
132 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
134 // Download functions file
135 loadIncludeOnce($funcsInclude);
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",
145 // Extensions are not deprecated by default
146 setExtensionDeprecated('N');
148 // Extensions are not always active by default
149 setExtensionAlwaysActive('N');
151 // Extension update notes
152 // @TODO Do we still need this? setExtensionUpdateNotes('');
154 // Include the extension file
155 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension loaded.');
156 loadExtensionInclude();
158 // Is this extension deprecated?
159 if (isExtensionDeprecated()) {
160 // Deactivate the extension
161 doDeactivateExtension($ext_name);
167 // Mark it as loaded in normal mode
168 if (getExtensionMode() == '') {
170 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
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);
184 enableExtensionDryRun($dry_run);
186 // By default all extensions are in productive phase
187 enableExtensionProductive();
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'
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!');
202 // This registration is running
203 addExtensionRunningRegistration($ext_name);
205 // Init EXT_UPDATE_DEPENDS if not yet done
206 if (!isExtensionUpdateDependenciesInitialized()) {
208 initExtensionUpdateDependencies();
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());
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());
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)",
226 getCurrentExtensionName()
232 // Init queries and notes
234 initExtensionNotes();
239 initIncludePool('extension');
241 // By default we have no failures
242 setExtensionReportsFailure(false);
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);
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());
258 // Add update notes to our output
259 addExtensionNotes($ext_ver);
262 // Does this extension depends on an outstanding update of another update?
263 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
265 $ext_update = getExtensionUpdateDependenciesIterator();
267 // Increment here to avoid endless loop
268 incrementExtensionUpdateIterator();
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);
276 // If versions mismatch update extension first
278 if (isExtensionInstalled($ext_update)) {
279 // Get version only if installed
280 $ext_ver = getExtensionVersion($ext_update);
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);
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);
294 // Reset extension name
295 setCurrentExtensionName($ext_name);
296 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
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());
313 // Nothing to register / update before...
317 // Required file for update does not exists!
319 // But this is fine for the first time...
322 // Restore the current extension name
323 setCurrentExtensionName($ext_name);
326 // Is there no update?
327 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
328 // Then test is passed!
332 // Switch back to register mode
333 setExtensionMode('register');
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));
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());
347 setSqlsArray(getExtensionSqls());
349 // Run installation pre-installation filters
350 runFilterChain('pre_extension_installed', array('dry_run' => isExtensionDryRun(), 'enable_codes' => false));
352 // Register extension
353 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
354 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
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')",
359 getCurrentExtensionName(),
360 getExtensionAlwaysActive(),
361 getCurrentExtensionVersion(),
363 ), __FUNCTION__, __LINE__);
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')",
369 getCurrentExtensionName(),
370 getExtensionAlwaysActive(),
371 getCurrentExtensionVersion()
372 ), __FUNCTION__, __LINE__);
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();
379 // Mark it as installed
380 $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
382 // Remove cache file(s) if extension is active
383 runFilterChain('post_extension_installed', array(
384 'pool' => 'extension',
385 'ext_name' => getCurrentExtensionName(),
389 // Re-init queries and notes
390 initExtensionSqls(true);
391 initExtensionNotes(true);
393 // In normal mode return a true on success
395 } elseif (isExtensionDryRun() === true) {
396 // In "dry-run" mode do always return a true
399 // Extension has been removed for updates, so all is fine!
403 // No, an error occurs while registering extension :-(
404 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
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__);
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&logout=1&' . getExtensionMode() . '=sql_patches');
422 // Add temporary filter
423 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
424 $GLOBALS['ext_load_mode'] = getExtensionMode();
428 // Return status code
429 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',test=' . intval($test) . ' - EXIT!');
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;
439 // Get extension's name
440 $ext_name = getExtensionName($ext_id);
442 // Set current SQL name
443 setCurrentExtensionName($ext_name);
445 // Init EXT_UPDATE_DEPENDS
446 if (!isExtensionUpdateDependenciesInitialized()) {
448 initExtensionUpdateDependencies();
452 initExtensionSqls(true);
454 // By default no SQL has been executed
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);
463 setSqlsArray(getExtensionSqls());
466 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls());
468 // Do we have entries?
470 // Run SQL commands...
471 runFilterChain('run_sqls');
474 // Run any filters depending on the action here
475 runFilterChain('extension_' . $load_mode);
477 // Remove cache file(s) if extension is active
478 if (((isExtensionActive('cache')) && ((!SQL_HASZEROAFFECTED())) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
480 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
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&logout=1&' . $load_mode . '=sql_patches');
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.');
500 // By default non is installed
501 $isInstalled = false;
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])) {
512 incrementStatsEntry('cache_hits');
513 } elseif ((isInstallationPhase())) {
514 // Extensions are all inactive/not installed during installation
517 $ext_id = getExtensionId($ext_name);
520 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension ' . $ext_name . ' has ext_id=' . $ext_id);
522 // Do we have a record?
523 $isInstalled = ($ext_id > 0);
525 // Is it installed, then cache the entry
526 if ($isInstalled === true) {
527 // Dummy call (get is okay here)
528 getExtensionId($ext_name, true);
531 // Remember the status
532 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
539 // Check if given extension is active
540 function isExtensionActive ($ext_name) {
541 if (isInstallationPhase()) {
542 // Extensions are all inactive during installation
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
552 // Not active is the default
553 $data['ext_active'] = 'N';
556 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
558 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE! ext_name=' . $ext_name);
559 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
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__);
573 if (SQL_NUMROWS($result) == 1) {
575 $data = SQL_FETCHARRAY($result);
579 SQL_FREERESULT($result);
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'];
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';
591 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',active=' . $data['ext_active']);
593 // Is this extension activated? (For admins we always have active extensions...)
594 return ($data['ext_active'] == 'Y');
597 // Get version from extensions
598 function getExtensionVersion ($ext_name, $force = false) {
599 // By default no extension is found
600 $data['ext_version'] = 'false';
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.');
608 // Extensions are all inactive during installation
609 if (isInstallationPhase()) return '';
610 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
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];
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).'');
626 // Is the extension there?
627 if (SQL_NUMROWS($result) == 1) {
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));
636 SQL_FREERESULT($result);
639 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
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));
649 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_version=' . $data['ext_version']);
650 return $data['ext_version'];
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))) {
660 // Set current SQL name
661 setCurrentExtensionName($ext_name);
665 initExtensionNotes();
666 initIncludePool('extension');
668 // Load extension in test mode
669 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
671 // Save version history
672 $history = getExtensionVersionHistory();
674 // Remove old SQLs array to prevent possible bugs
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);
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];
689 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
692 $depencies = getExtensionUpdateDependencies();
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])) {
701 setCurrentExtensionName($ext_depend);
703 // Mark it as already updated before we update it
704 $GLOBALS['ext_updated'][$ext_depend] = true;
706 // Is the extension there?
707 if (isExtensionInstalled($ext_depend)) {
708 // Update another extension first!
709 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun());
711 // Register new extension
712 $test = registerExtension($ext_depend, 0, isExtensionDryRun(), false);
717 // Set extension version here
718 setCurrentExtensionVersion($ext_ver);
721 setCurrentExtensionName($ext_name);
725 addExtensionNotes($history[$idx]);
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()]);
738 setSqlsArray(getExtensionSqls());
741 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun(), 'enable_codes' => false));
743 if (isExtensionDryRun() === false) {
744 // Run filters on success extension update
745 runFilterChain('extension_update', getCurrentExtensionName());
750 // Output verbose SQL table for extension
751 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
754 // Then fix it to default
755 $title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}';
761 // Do we have queries?
762 if (isVerboseSqlEnabled()) {
763 // Do we have entries?
764 if (countExtensionSqls() > 0) {
768 foreach (getExtensionSqls() as $sqls) {
769 // New array format is recursive
770 foreach ($sqls as $sql) {
774 // Output command if set
776 // Prepare output for template
779 'sql' => str_replace('{', '{', str_replace('}', '}', encodeEntities($sql)))
783 $OUT .= loadTemplate('admin_extension_sql_row', true, $content);
791 // Prepare content for template
799 // Load main template
800 $OUT = loadTemplate('admin_extension_sql_table', true, $content);
802 // No addional SQL commands to run
803 $OUT = loadTemplate('admin_settings_saved', true, '{--ADMIN_NO_ADDITIONAL_SQLS--}');
811 // Get extension name from id
812 function getExtensionName ($ext_id) {
813 // Init extension name
814 $data['ext_name'] = '';
817 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
819 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
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__);
828 // Is the entry there?
829 if (SQL_NUMROWS($result) == 1) {
830 // Get the extension's name from database
831 $data = SQL_FETCHARRAY($result);
835 SQL_FREERESULT($result);
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);
844 // Return the extension name
845 return $data['ext_name'];
848 // Get extension id from name
849 function getExtensionId ($ext_name) {
851 $data['ext_id'] = '0';
854 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
856 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
859 incrementStatsEntry('cache_hits');
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__);
865 // Is the entry there?
866 if (SQL_NUMROWS($result) == 1) {
867 // Get the extension's id from database
868 $data = SQL_FETCHARRAY($result);
872 SQL_FREERESULT($result);
875 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
879 return $data['ext_id'];
882 // Determines wether the given extension name is valid
883 function isExtensionNameValid ($ext_name) {
885 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
886 // Generate include file name
887 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
889 // Is there a file in inc/extensions/ ?
890 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
894 return $GLOBALS['ext_name_valid'][$ext_name];
897 // Determines wether the given extension id is valid
898 function isExtensionIdValid ($ext_id) {
899 // Default is nothing valid
902 // Check in cache then in database
903 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
908 incrementStatsEntry('cache_hits');
911 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
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);
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__);
930 // Then run all queries
931 doExtensionSqls(getExtensionId($ext_name), 'activate');
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);
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__);
946 // Then run all queries
947 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
949 // Create new task (we ignore the task id here)
950 createExtensionDeactivationTask($ext_name);
953 sendAdminNotification(
954 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
955 'admin_extension_deactivated',
956 array('ext_name' => $ext_name)
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);
965 // Remove all dots from both versions
966 $currVersion = str_replace('.', '', $currVersion);
967 $ext_ver = str_replace('.', '', $ext_ver);
969 // Now compare both and return the result
970 return ($currVersion < $ext_ver);
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--}';
979 $taskId = determineTaskIdBySubject($subject);
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);
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));
997 // Creates a new task for newly installed extension
998 function createNewExtensionTask ($ext_name) {
999 // Generate subject line
1000 $subject = sprintf("[%s:]", $ext_name);
1003 $taskId = determineTaskIdBySubject($subject);
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);
1011 $FQFN = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
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);
1022 // Write this in debug.log as well
1023 logDebugMessage(__FUNCTION__, __LINE__, $message);
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);
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--}');
1040 $taskId = determineTaskIdBySubject($subject);
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');
1052 // Determines the task id for given extension
1053 function determineExtensionTaskId ($ext_name) {
1054 // Default is not found
1055 $data['task_id'] = '0';
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__);
1062 if (SQL_NUMROWS($result) == 1) {
1063 // Task found so load task's id and register extension...
1064 $data = SQL_FETCHARRAY($result);
1068 SQL_FREERESULT($result);
1071 return $data['task_id'];
1074 // Determines the task id for given subject
1075 function determineTaskIdBySubject ($subject) {
1076 // Default is not found
1077 $data['task_id'] = '0';
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__);
1084 if (SQL_NUMROWS($result) == 1) {
1085 // Task found so load task's id and register extension...
1086 $data = SQL_FETCHARRAY($result);
1090 SQL_FREERESULT($result);
1093 return $data['task_id'];
1096 // Add updates notes for given version
1097 function addExtensionNotes ($ext_ver) {
1098 // Init notes/content
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
1109 'notes' => getExtensionUpdateNotes($ext_ver)
1113 setExtensionUpdateNotes('', $ext_ver);
1114 } elseif (in_array($ext_ver, array('0.0', '0.0.0'))) {
1118 'notes' => '{--INITIAL_RELEASE--}'
1121 // No update notes found
1124 'notes' => '{--NO_UPDATE_NOTES--}'
1129 $out = loadTemplate('admin_extension_notes', true, $content);
1133 appendExtensionNotes($out);
1136 // Getter for CSS files array
1137 function getExtensionCssFiles () {
1138 // By default no additional CSS files are found
1139 $cssFiles = array();
1141 // Is the array there?
1142 if (isset($GLOBALS['css_files'])) {
1144 $cssFiles = $GLOBALS['css_files'];
1151 // Init CSS files array
1152 function initExtensionCssFiles () {
1154 $GLOBALS['css_files'] = array();
1158 function addExtensionCssFile ($file) {
1159 // Is the array there?
1160 if (!isset($GLOBALS['css_files'])) {
1161 // Then auto-init them
1162 initExtensionCssFiles();
1166 $GLOBALS['css_files'][] = $file;
1169 // Setter for EXT_ALWAYS_ACTIVE flag
1170 function setExtensionAlwaysActive ($active) {
1171 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1174 // Getter for EXT_ALWAYS_ACTIVE flag
1175 function getExtensionAlwaysActive () {
1176 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1179 // Checks wether the current extension is always active
1180 function isExtensionAlwaysActive () {
1181 return (getExtensionAlwaysActive() == 'Y');
1184 // Setter for EXT_VERSION flag
1185 function setThisExtensionVersion ($ext_version) {
1186 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1189 // Getter for EXT_VERSION flag
1190 function getThisExtensionVersion () {
1191 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1194 // Setter for EXT_DEPRECATED flag
1195 function setExtensionDeprecated ($deprecated) {
1196 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1199 // Getter for EXT_DEPRECATED flag
1200 function isExtensionDeprecated () {
1201 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
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());
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());
1221 // Add it to the list of extension update depencies map
1222 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
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!');
1229 // Getter for running updates
1230 function getExtensionUpdatesRunning () {
1231 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1234 // Checks wether the given extension registration is in progress
1235 function isExtensionRegistrationRunning ($ext_name) {
1237 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1240 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1244 // Init EXT_UPDATE_DEPENDS flag
1245 function initExtensionUpdateDependencies () {
1246 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
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());
1254 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1256 // Init running updates array
1257 initExtensionRuningUpdates();
1259 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1262 // Adds an extension as "registration in progress"
1263 function addExtensionRunningRegistration ($ext_name) {
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);
1272 $GLOBALS['ext_register_running'][] = $ext_name;
1273 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
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()]));
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
1289 // Walk through whole array
1290 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1292 if (($ext1 == $ext_name) || ($isRunning === true)) {
1294 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1301 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1306 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1309 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
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();
1322 // Getter for EXT_UPDATE_DEPENDS flag
1323 function getExtensionUpdateDependencies () {
1324 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1325 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1328 // Getter for next iterator depency
1329 function getExtensionUpdateDependenciesIterator () {
1330 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1331 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1334 // Counter for extension update depencies
1335 function countExtensionUpdateDependencies () {
1336 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1337 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1340 // Removes given extension from update denpency list
1341 function removeExtensionDependency ($ext_name) {
1343 $key = array_search($ext_name, getExtensionUpdateDependencies());
1346 if ($key !== false) {
1348 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1350 // And sort the array
1351 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1355 // Init iterator for update depencies
1356 function initExtensionUpdateIterator () {
1357 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1358 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1361 // Getter for depency iterator
1362 function getExtensionUpdateIterator () {
1363 // Auto-init iterator
1364 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1367 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1368 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1371 // Increments the update iterator
1372 function incrementExtensionUpdateIterator () {
1373 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1374 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1377 // Setter for EXT_REPORTS_FAILURE flag
1378 function setExtensionReportsFailure ($reportsFailure) {
1379 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1382 // Getter for EXT_REPORTS_FAILURE flag
1383 function getExtensionReportsFailure () {
1384 return $GLOBALS['ext_reports_failure'];
1387 // Setter for EXT_VER_HISTORY flag
1388 function setExtensionVersionHistory ($versionHistory) {
1389 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1392 // Getter for EXT_VER_HISTORY array
1393 function getExtensionVersionHistory () {
1394 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
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;
1404 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1408 // Getter for EXT_UPDATE_NOTES
1409 function getExtensionUpdateNotes ($ext_ver) {
1410 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1413 // Checks if ext_update_notes is set
1414 function isExtensionUpdateNoteSet ($ext_ver) {
1415 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
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());
1426 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1427 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
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);
1436 // Getter for extension notes
1437 function getExtensionNotes () {
1438 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1441 // Setter for current extension name
1442 function setCurrentExtensionName ($ext_name) {
1443 $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1446 // Getter for current extension name
1447 function getCurrentExtensionName () {
1448 if (!isset($GLOBALS['curr_extension_name'])) {
1450 debug_report_bug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1454 return $GLOBALS['curr_extension_name'];
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()]))) {
1462 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1464 // Initialize the generic array
1469 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1470 function addExtensionSql ($sql) {
1472 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',ext_version=' . getCurrentExtensionVersion() . ',sql=' . $sql);
1473 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
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()
1487 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
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()
1501 return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1504 // Removes SQLs for current extension
1505 function unsetExtensionSqls () {
1506 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1509 // Auto-initializes the removal list
1510 function initExtensionRemovalList () {
1511 // Is the remove list there?
1512 if (!isset($GLOBALS['ext_update_remove'])) {
1514 $GLOBALS['ext_update_remove'] = array();
1518 // Checks wether the current extension is on the removal list
1519 function isExtensionOnRemovalList () {
1520 // Init removal list
1521 initExtensionRemovalList();
1524 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1527 // Adds the current extension to the removal list
1528 function addCurrentExtensionToRemovalList () {
1530 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1533 // Getter for removal list
1534 function getExtensionRemovalList () {
1535 // Return the removal list
1536 return $GLOBALS['ext_update_remove'];
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&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
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) {
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');
1561 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1563 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1566 // Setter for extension mode
1567 function setExtensionMode ($ext_mode) {
1568 $GLOBALS['ext_mode'] = (string) $ext_mode;
1571 // Getter for extension mode
1572 function getExtensionMode () {
1573 return $GLOBALS['ext_mode'];
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;
1582 // Getter for dry-run
1583 function isExtensionDryRun () {
1584 return $GLOBALS['ext_dry_run'];
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());
1596 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1597 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
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()];
1606 // Remove the extension from cache array
1607 function removeExtensionFromArray () {
1608 // "Cache" this name
1609 $ext_name = getCurrentExtensionName();
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])) {
1616 unset($cacheArray[$ext_name]);
1617 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1620 $key = array_search($ext_name, $cacheArray);
1623 if ($key !== false) {
1624 // Found, so remove it
1625 unset($cacheArray[$key]);
1626 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1632 // "Getter" for 'extension has a CSS file' (with same name, of course)
1633 function getExtensionHasCss () {
1634 // Default is no CSS
1637 // Construct FQFN for check
1638 $FQFN = sprintf("%stheme/%s/css/%s.css",
1641 getCurrentExtensionName()
1645 if (isFileReadable($FQFN)) {
1646 // Readable, so it is there...
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])) {
1659 incrementStatsEntry('cache_hits');
1661 // Not readable is default
1664 // Is the language file readable for this extension?
1665 if (isLanguageIncludeReadable($ext_name)) {
1671 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = $readable;
1675 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1678 // Load an extension's include file
1679 function loadExtensionInclude () {
1681 if (!isExtensionIncludeReadable()) {
1683 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1686 // Generate INC name
1687 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
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();
1699 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1700 // Generate INC name
1701 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1704 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
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];
1712 // Checks if an extension's function file is readable
1713 function isExtensionFunctionFileReadable ($ext_name) {
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');
1721 // Construct IFN for functions file
1722 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1724 // Is this include there?
1725 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1727 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=Y - FOUND!');
1728 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1731 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=N - NOT FOUND!');
1732 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
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');
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 . '`');
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)) {
1756 if (is_null($what)) {
1758 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1766 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
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));
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)) {
1788 if (is_null($what)) {
1790 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1799 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
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));
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)) {
1822 if (is_null($what)) {
1824 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1833 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
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));
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))) {
1856 if (is_null($what)) {
1858 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1866 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
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));
1883 // Enables/disables productive mode for current extension (used only while
1885 function enableExtensionProductive ($isProductive = true) {
1886 // Log debug message
1887 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%d", getCurrentExtensionName(), intval($isProductive)));
1890 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
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();
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');
1911 $GLOBALS[__FUNCTION__][$ext_name] = ((isset($GLOBALS['ext_productive'][$ext_name])) && ($GLOBALS['ext_productive'][$ext_name] === true));
1914 // Log debug message
1915 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%s", $ext_name, intval($GLOBALS[__FUNCTION__][$ext_name])));
1918 return $GLOBALS[__FUNCTION__][$ext_name];