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 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
345 // Init SQLs and transfer ext->generic
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')) {
355 //* 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__);
365 //* 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))) return false;
658 // Set current SQL name
659 setCurrentExtensionName($ext_name);
663 initExtensionNotes();
664 initIncludePool('extension');
666 // Load extension in test mode
667 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
669 // Save version history
670 $history = getExtensionVersionHistory();
672 // Remove old SQLs array to prevent possible bugs
675 // Check if version is updated
676 //* DEBUG: */ debugOutput(getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)));
677 if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
678 // Search for starting point
679 $start = array_search($ext_ver, $history);
681 // And load SQL queries in order of version history
682 for ($idx = ($start + 1); $idx < count($history); $idx++) {
683 // Set extension version
684 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
687 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
690 $depencies = getExtensionUpdateDependencies();
693 if (count($depencies) > 0) {
694 // Apply all extension depencies
695 foreach ($depencies as $ext_depend) {
696 // Did we already update/register this?
697 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
699 setCurrentExtensionName($ext_depend);
701 // Mark it as already updated before we update it
702 $GLOBALS['ext_updated'][$ext_depend] = true;
704 // Is the extension there?
705 if (isExtensionInstalled($ext_depend)) {
706 // Update another extension first!
707 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun());
709 // Register new extension
710 $test = registerExtension($ext_depend, 0, isExtensionDryRun(), false);
715 // Set extension version here
716 setCurrentExtensionVersion($ext_ver);
719 setCurrentExtensionName($ext_name);
723 addExtensionNotes($history[$idx]);
726 // In real-mode execute any existing includes
727 if (isExtensionDryRun() === false) {
728 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
729 runFilterChain('load_includes', 'extension');
730 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
731 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
736 setSqlsArray(getExtensionSqls());
739 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun(), 'enable_codes' => false));
741 if (isExtensionDryRun() === false) {
742 // Run filters on success extension update
743 runFilterChain('extension_update', getCurrentExtensionName());
748 // Output verbose SQL table for extension
749 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
752 // Then fix it to default
753 $title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}';
759 // Do we have queries?
760 if (isVerboseSqlEnabled()) {
761 // Do we have entries?
762 if (countExtensionSqls() > 0) {
766 foreach (getExtensionSqls() as $sqls) {
767 // New array format is recursive
768 foreach ($sqls as $sql) {
772 // Output command if set
774 // Prepare output for template
777 'sql' => str_replace('{', '{', str_replace('}', '}', encodeEntities($sql)))
781 $OUT .= loadTemplate('admin_extension_sql_row', true, $content);
789 // Prepare content for template
797 // Load main template
798 $OUT = loadTemplate('admin_extension_sql_table', true, $content);
800 // No addional SQL commands to run
801 $OUT = loadTemplate('admin_settings_saved', true, '{--ADMIN_NO_ADDITIONAL_SQLS--}');
809 // Get extension name from id
810 function getExtensionName ($ext_id) {
811 // Init extension name
812 $data['ext_name'] = '';
815 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
817 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
820 incrementStatsEntry('cache_hits');
821 } elseif (!isExtensionActive('cache')) {
822 // Load from database
823 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
824 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
826 // Is the entry there?
827 if (SQL_NUMROWS($result) == 1) {
828 // Get the extension's name from database
829 $data = SQL_FETCHARRAY($result);
833 SQL_FREERESULT($result);
836 // Did we find some extension?
837 if (empty($data['ext_name'])) {
838 // We should fix these all!
839 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
842 // Return the extension name
843 return $data['ext_name'];
846 // Get extension id from name
847 function getExtensionId ($ext_name) {
849 $data['ext_id'] = '0';
852 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
854 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
857 incrementStatsEntry('cache_hits');
859 // Load from database
860 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
861 array($ext_name), __FUNCTION__, __LINE__);
863 // Is the entry there?
864 if (SQL_NUMROWS($result) == 1) {
865 // Get the extension's id from database
866 $data = SQL_FETCHARRAY($result);
870 SQL_FREERESULT($result);
873 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
877 return $data['ext_id'];
880 // Determines wether the given extension name is valid
881 function isExtensionNameValid ($ext_name) {
883 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
884 // Generate include file name
885 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
887 // Is there a file in inc/extensions/ ?
888 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
892 return $GLOBALS['ext_name_valid'][$ext_name];
895 // Determines wether the given extension id is valid
896 function isExtensionIdValid ($ext_id) {
897 // Default is nothing valid
900 // Check in cache then in database
901 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
906 incrementStatsEntry('cache_hits');
909 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
916 // Activate given extension
917 function doActivateExtension ($ext_name) {
918 // Is the extension installed?
919 if (!isExtensionInstalled($ext_name)) {
920 // Non-installed extensions cannot be activated
921 debug_report_bug(__FUNCTION__, __LINE__, 'Tried to activate non-installed extension ' . $ext_name);
924 // Activate the extension
925 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
926 array($ext_name), __FUNCTION__, __LINE__);
928 // Then run all queries
929 doExtensionSqls(getExtensionId($ext_name), 'activate');
932 // Deactivate given extension
933 function doDeactivateExtension ($ext_name) {
934 // Is the extension installed?
935 if (!isExtensionInstalled($ext_name)) {
936 // Non-installed extensions cannot be activated
937 debug_report_bug(__FUNCTION__, __LINE__, 'Tried to deactivate non-installed extension ' . $ext_name);
940 // Activate the extension
941 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
942 array($ext_name), __FUNCTION__, __LINE__);
944 // Then run all queries
945 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
947 // Create new task (we ignore the task id here)
948 createExtensionDeactivationTask($ext_name);
951 sendAdminNotification(
952 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
953 'admin_extension_deactivated',
954 array('ext_name' => $ext_name)
958 // Checks wether the extension is older than given
959 function isExtensionOlder ($ext_name, $ext_ver) {
960 // Get current extension version
961 $currVersion = getExtensionVersion($ext_name);
963 // Remove all dots from both versions
964 $currVersion = str_replace('.', '', $currVersion);
965 $ext_ver = str_replace('.', '', $ext_ver);
967 // Now compare both and return the result
968 return ($currVersion < $ext_ver);
971 // Creates a new task for updated extension
972 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
973 // Create subject line
974 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
977 $taskId = determineTaskIdBySubject($subject);
979 // Is the extension there?
980 if (isExtensionInstalled($ext_name)) {
981 // Check if task is not there
982 if ($taskId == '0') {
983 // Create extension update-task
984 $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
987 // Extension not there! :-(
988 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
995 // Creates a new task for newly installed extension
996 function createNewExtensionTask ($ext_name) {
997 // Generate subject line
998 $subject = sprintf("[%s:]", $ext_name);
1001 $taskId = determineTaskIdBySubject($subject);
1003 // Not installed and do we have created a task for the admin?
1004 if (($taskId == '0') && (!isExtensionInstalled($ext_name))) {
1005 // Set default message if ext-foo is missing
1006 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext_name);
1009 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
1015 // Load text for task if found
1016 if (isFileReadable($tpl)) {
1017 // Load extension's own text template (HTML!)
1018 $message = loadTemplate('ext_' . $ext_name, true);
1020 // Write this in debug.log as well
1021 logDebugMessage(__FUNCTION__, __LINE__, $message);
1024 // Task not created so it's a brand-new extension which we need to register and create a task for!
1025 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), false);
1032 // Creates a task for automatically deactivated (deprecated) extension
1033 function createExtensionDeactivationTask ($ext_name) {
1034 // Create subject line
1035 $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1038 $taskId = determineTaskIdBySubject($subject);
1040 // Not installed and do we have created a task for the admin?
1041 if (($taskId == '0') && (isExtensionInstalled($ext_name))) {
1042 // Task not created so add it
1043 $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext_name)), 'EXTENSION_DEACTIVATION');
1050 // Determines the task id for given extension
1051 function determineExtensionTaskId ($ext_name) {
1052 // Default is not found
1053 $data['task_id'] = '0';
1055 // Search for extension task's id
1056 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1057 array($ext_name), __FUNCTION__, __LINE__);
1060 if (SQL_NUMROWS($result) == 1) {
1061 // Task found so load task's id and register extension...
1062 $data = SQL_FETCHARRAY($result);
1066 SQL_FREERESULT($result);
1069 return $data['task_id'];
1072 // Determines the task id for given subject
1073 function determineTaskIdBySubject ($subject) {
1074 // Default is not found
1075 $data['task_id'] = '0';
1077 // Search for task id
1078 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1079 array($subject), __FUNCTION__, __LINE__);
1082 if (SQL_NUMROWS($result) == 1) {
1083 // Task found so load task's id and register extension...
1084 $data = SQL_FETCHARRAY($result);
1088 SQL_FREERESULT($result);
1091 return $data['task_id'];
1094 // Add updates notes for given version
1095 function addExtensionNotes ($ext_ver) {
1096 // Init notes/content
1100 // Is do we have verbose output enabled?
1101 if ((!isExtensionActive('sql_patches')) || (isVerboseSqlEnabled())) {
1102 // Update notes found?
1103 if (isExtensionUpdateNoteSet($ext_ver)) {
1104 // Update notes found
1107 'notes' => getExtensionUpdateNotes($ext_ver)
1111 setExtensionUpdateNotes('', $ext_ver);
1112 } elseif (in_array($ext_ver, array('0.0', '0.0.0'))) {
1116 'notes' => '{--INITIAL_RELEASE--}'
1119 // No update notes found
1122 'notes' => '{--NO_UPDATE_NOTES--}'
1127 $out = loadTemplate('admin_extension_notes', true, $content);
1131 appendExtensionNotes($out);
1134 // Getter for CSS files array
1135 function getExtensionCssFiles () {
1136 // By default no additional CSS files are found
1137 $cssFiles = array();
1139 // Is the array there?
1140 if (isset($GLOBALS['css_files'])) {
1142 $cssFiles = $GLOBALS['css_files'];
1149 // Init CSS files array
1150 function initExtensionCssFiles () {
1152 $GLOBALS['css_files'] = array();
1156 function addExtensionCssFile ($file) {
1157 // Is the array there?
1158 if (!isset($GLOBALS['css_files'])) {
1159 // Then auto-init them
1160 initExtensionCssFiles();
1164 $GLOBALS['css_files'][] = $file;
1167 // Setter for EXT_ALWAYS_ACTIVE flag
1168 function setExtensionAlwaysActive ($active) {
1169 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1172 // Getter for EXT_ALWAYS_ACTIVE flag
1173 function getExtensionAlwaysActive () {
1174 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1177 // Checks wether the current extension is always active
1178 function isExtensionAlwaysActive () {
1179 return (getExtensionAlwaysActive() == 'Y');
1182 // Setter for EXT_VERSION flag
1183 function setThisExtensionVersion ($ext_version) {
1184 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1187 // Getter for EXT_VERSION flag
1188 function getThisExtensionVersion () {
1189 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1192 // Setter for EXT_DEPRECATED flag
1193 function setExtensionDeprecated ($deprecated) {
1194 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1197 // Getter for EXT_DEPRECATED flag
1198 function isExtensionDeprecated () {
1199 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1202 // Setter for EXT_UPDATE_DEPENDS flag
1203 function addExtensionDependency ($updateDepends) {
1204 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1205 // Is the update depency empty? (NEED TO BE FIXED!)
1206 if (empty($updateDepends)) {
1207 // Please report this bug!
1208 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1211 // Is it not yet added?
1212 /* Only for debugging!
1213 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1214 // Double-adding isn't fine, too
1215 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1219 // Add it to the list of extension update depencies map
1220 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1222 // Remember it in the list of running updates
1223 $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1224 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1227 // Getter for running updates
1228 function getExtensionUpdatesRunning () {
1229 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1232 // Checks wether the given extension registration is in progress
1233 function isExtensionRegistrationRunning ($ext_name) {
1235 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1238 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1242 // Init EXT_UPDATE_DEPENDS flag
1243 function initExtensionUpdateDependencies () {
1244 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1246 // Init update depency map automatically if not found
1247 if (isExtensionUpdateDependenciesInitialized()) {
1248 // We need these bug reports as well...
1249 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1252 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1254 // Init running updates array
1255 initExtensionRuningUpdates();
1257 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1260 // Adds an extension as "registration in progress"
1261 function addExtensionRunningRegistration ($ext_name) {
1263 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1264 if (isExtensionRegistrationRunning($ext_name)) {
1265 // This is really bad and should not be quietly ignored
1266 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1270 $GLOBALS['ext_register_running'][] = $ext_name;
1271 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1274 // Checks wether EXT_UPDATE_DEPENDS is initialized
1275 function isExtensionUpdateDependenciesInitialized () {
1276 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1277 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1280 // Checks wether an update is already running for given extension
1281 function isExtensionUpdateRunning ($ext_name) {
1282 // Current and given extensions means whole array
1283 if ($ext_name == getCurrentExtensionName()) {
1284 // Default is not found
1287 // Walk through whole array
1288 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1290 if (($ext1 == $ext_name) || ($isRunning === true)) {
1292 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1299 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1304 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1307 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1311 // Initializes the list of running updates
1312 function initExtensionRuningUpdates () {
1313 // Auto-init ext_running_updates
1314 if (!isset($GLOBALS['ext_running_updates'])) {
1315 $GLOBALS['ext_running_updates'] = array();
1316 $GLOBALS['ext_register_running'] = array();
1320 // Getter for EXT_UPDATE_DEPENDS flag
1321 function getExtensionUpdateDependencies () {
1322 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1323 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1326 // Getter for next iterator depency
1327 function getExtensionUpdateDependenciesIterator () {
1328 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1329 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1332 // Counter for extension update depencies
1333 function countExtensionUpdateDependencies () {
1334 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1335 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1338 // Removes given extension from update denpency list
1339 function removeExtensionDependency ($ext_name) {
1341 $key = array_search($ext_name, getExtensionUpdateDependencies());
1344 if ($key !== false) {
1346 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1348 // And sort the array
1349 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1353 // Init iterator for update depencies
1354 function initExtensionUpdateIterator () {
1355 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1356 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1359 // Getter for depency iterator
1360 function getExtensionUpdateIterator () {
1361 // Auto-init iterator
1362 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1365 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1366 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1369 // Increments the update iterator
1370 function incrementExtensionUpdateIterator () {
1371 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1372 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1375 // Setter for EXT_REPORTS_FAILURE flag
1376 function setExtensionReportsFailure ($reportsFailure) {
1377 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1380 // Getter for EXT_REPORTS_FAILURE flag
1381 function getExtensionReportsFailure () {
1382 return $GLOBALS['ext_reports_failure'];
1385 // Setter for EXT_VER_HISTORY flag
1386 function setExtensionVersionHistory ($versionHistory) {
1387 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1390 // Getter for EXT_VER_HISTORY array
1391 function getExtensionVersionHistory () {
1392 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1395 // Setter for EXT_UPDATE_NOTES
1396 function setExtensionUpdateNotes ($updateNotes, $ext_ver = '') {
1397 // . '/' . getCurrentExtensionVersion()
1398 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ',getExtensionMode()=' . getExtensionMode() . ',ext_ver=' . $ext_ver . ',updateNotes()=' . strlen($updateNotes));
1399 if (empty($ext_ver)) {
1400 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1402 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1406 // Getter for EXT_UPDATE_NOTES
1407 function getExtensionUpdateNotes ($ext_ver) {
1408 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1411 // Checks if ext_update_notes is set
1412 function isExtensionUpdateNoteSet ($ext_ver) {
1413 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
1416 // Init extension notice
1417 function initExtensionNotes ($force = false) {
1418 // Is it already initialized?
1419 if (($force === false) && (isset($GLOBALS['ext_notes'][getCurrentExtensionName()]))) {
1420 // This is mostly not wanted, so please report it
1421 debug_report_bug(__FUNCTION__, __LINE__, 'ext_notes already set for extension ' . getCurrentExtensionName());
1424 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1425 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1428 // Append extension notice
1429 function appendExtensionNotes ($notes) {
1430 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ', notes()=' . strlen($notes));
1431 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1434 // Getter for extension notes
1435 function getExtensionNotes () {
1436 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1439 // Setter for current extension name
1440 function setCurrentExtensionName ($ext_name) {
1441 $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1444 // Getter for current extension name
1445 function getCurrentExtensionName () {
1446 if (!isset($GLOBALS['curr_extension_name'])) {
1448 debug_report_bug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1452 return $GLOBALS['curr_extension_name'];
1455 // Init SQLs array for current extension
1456 function initExtensionSqls ($force = false) {
1457 // Auto-init the array or if forced
1458 if (($force === true) || (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()]))) {
1460 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1462 // Initialize the generic array
1467 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1468 function addExtensionSql ($sql) {
1470 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',ext_version=' . getCurrentExtensionVersion() . ',sql=' . $sql);
1471 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1474 // Getter for SQLs array for current extension
1475 function getExtensionSqls () {
1476 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1477 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1478 // Not found, should not happen
1479 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1480 getCurrentExtensionName()
1485 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1488 // Count SQLs for current extension
1489 function countExtensionSqls () {
1490 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1491 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1492 // Not found, should not happen
1493 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1494 getCurrentExtensionName()
1499 return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1502 // Removes SQLs for current extension
1503 function unsetExtensionSqls () {
1504 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1507 // Auto-initializes the removal list
1508 function initExtensionRemovalList () {
1509 // Is the remove list there?
1510 if (!isset($GLOBALS['ext_update_remove'])) {
1512 $GLOBALS['ext_update_remove'] = array();
1516 // Checks wether the current extension is on the removal list
1517 function isExtensionOnRemovalList () {
1518 // Init removal list
1519 initExtensionRemovalList();
1522 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1525 // Adds the current extension to the removal list
1526 function addCurrentExtensionToRemovalList () {
1528 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1531 // Getter for removal list
1532 function getExtensionRemovalList () {
1533 // Return the removal list
1534 return $GLOBALS['ext_update_remove'];
1537 // Redirects if the provided extension is not installed
1538 function redirectOnUninstalledExtension ($ext_name) {
1539 // So is the extension there?
1540 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1541 // Redirect to index
1542 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1546 // Filter for initialization of all extensions by loading them in 'init' mode
1547 function FILTER_INIT_EXTENSIONS () {
1548 // Do we have some entries?
1549 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY!');
1550 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1551 // Load all found extensions if found
1552 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
1553 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1555 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - START');
1556 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1557 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - END');
1559 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1561 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1564 // Setter for extension mode
1565 function setExtensionMode ($ext_mode) {
1566 $GLOBALS['ext_mode'] = (string) $ext_mode;
1569 // Getter for extension mode
1570 function getExtensionMode () {
1571 return $GLOBALS['ext_mode'];
1574 // Setter for dry-run
1575 function enableExtensionDryRun ($dry_run = true) {
1576 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()='.getCurrentExtensionName().',dry='.intval($dry_run));
1577 $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1580 // Getter for dry-run
1581 function isExtensionDryRun () {
1582 return $GLOBALS['ext_dry_run'];
1585 // Setter for current extension version
1586 function setCurrentExtensionVersion ($ext_ver) {
1587 // ext_ver should never be empty in other modes than 'test'
1588 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1589 // Please report all these messages
1590 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1594 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1595 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1598 // Getter for current extension version
1599 function getCurrentExtensionVersion () {
1600 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1601 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1604 // Remove the extension from cache array
1605 function removeExtensionFromArray () {
1606 // "Cache" this name
1607 $ext_name = getCurrentExtensionName();
1609 // Now loop through the whole cache
1610 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1611 // Is it an element?
1612 if (isset($cacheArray[$ext_name])) {
1614 unset($cacheArray[$ext_name]);
1615 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1618 $key = array_search($ext_name, $cacheArray);
1621 if ($key !== false) {
1622 // Found, so remove it
1623 unset($cacheArray[$key]);
1624 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1630 // "Getter" for 'extension has a CSS file' (with same name, of course)
1631 function getExtensionHasCss () {
1632 // Default is no CSS
1635 // Construct FQFN for check
1636 $FQFN = sprintf("%stheme/%s/css/%s.css",
1639 getCurrentExtensionName()
1643 if (isFileReadable($FQFN)) {
1644 // Readable, so it is there...
1652 // Checks wether the given extension has a language file
1653 function ifExtensionHasLanguageFile ($ext_name) {
1654 // Do we have cache?
1655 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1657 incrementStatsEntry('cache_hits');
1659 // Not readable is default
1662 // Is the language file readable for this extension?
1663 if (isLanguageIncludeReadable($ext_name)) {
1669 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = $readable;
1673 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1676 // Load an extension's include file
1677 function loadExtensionInclude () {
1679 if (!isExtensionIncludeReadable()) {
1681 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1684 // Generate INC name
1685 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1691 // Checks wether an extension is readable
1692 function isExtensionIncludeReadable ($ext_name = '') {
1693 // If empty, use current
1694 if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1697 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1698 // Generate INC name
1699 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1702 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1706 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1707 return $GLOBALS['ext_inc_readable'][$ext_name];
1710 // Checks if an extension's function file is readable
1711 function isExtensionFunctionFileReadable ($ext_name) {
1713 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
1714 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1715 // Just count cache hits
1716 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] .' - CACHE!');
1717 incrementStatsEntry('cache_hits');
1719 // Construct IFN for functions file
1720 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1722 // Is this include there?
1723 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1725 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=Y - FOUND!');
1726 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1729 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=N - NOT FOUND!');
1730 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1735 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name]);
1736 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1739 // Adds a DROP TABLE statement if the requested tabled is there
1740 function addDropTableSql ($table) {
1741 // Is the table there?
1742 if (isSqlTableCreated($table)) {
1743 // Then add it, non-existing tables can be ignored because it will
1744 // happen with every newly installed extension.
1745 addExtensionSql('DROP TABLE `{?_MYSQL_PREFIX?}_' . $table . '`');
1749 // Adds an admin menu to the SQL queue of the menu entry is not found
1750 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1751 // Now check if this menu is there
1752 if (!isMenuActionValid('admin', $action, $what)) {
1754 if (is_null($what)) {
1756 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1764 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1773 // Add it to the queue
1774 addExtensionSql($sql);
1775 } elseif (isDebugModeEnabled()) {
1776 // Double menus should be located and fixed!
1777 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
1781 // Adds a guest menu to the SQL queue if the menu entry is not found
1782 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1783 // Now check if this menu is there
1784 if (!isMenuActionValid('guest', $action, $what)) {
1786 if (is_null($what)) {
1788 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1797 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1807 // Add it to the queue
1808 addExtensionSql($sql);
1809 } elseif (isDebugModeEnabled()) {
1810 // Double menus should be located and fixed!
1811 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s,what=%s,title=%s,locked=%s,visible=%s detected.", $action, $what, $title, $locked, $visible));
1815 // Adds a member menu to the SQL queue if the menu entry is not found
1816 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1817 // Now check if this menu is there
1818 if (!isMenuActionValid('member', $action, $what)) {
1820 if (is_null($what)) {
1822 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1831 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1841 // Add it to the queue
1842 addExtensionSql($sql);
1843 } elseif (isDebugModeEnabled()) {
1844 // Double menus should be located and fixed!
1845 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s,what=%s,title=%s,visivle=%s,locked=%s detected.", $action, $what, $title, $visible, $locked));
1849 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1850 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1851 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1852 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1854 if (is_null($what)) {
1856 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1864 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1873 // Add it to the queue
1874 addExtensionSql($sql);
1875 } elseif (isDebugModeEnabled()) {
1876 // Double menus should be located and fixed!
1877 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s,what=%s,title=%s,active=%s detected.", $action, $what, $title, $active));
1881 // Enables/disables productive mode for current extension (used only while
1883 function enableExtensionProductive ($isProductive = true) {
1884 // Log debug message
1885 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%d", getCurrentExtensionName(), intval($isProductive)));
1888 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1891 // Checks wether the extension is in productive phase. If not set, development
1892 // phase (=false) is assumed.
1893 function isExtensionProductive ($ext_name = '') {
1894 // Is the extension name empty? Then use current
1895 if (empty($ext_name)) {
1896 // Get current extension name
1897 $ext_name = getCurrentExtensionName();
1900 // Do we have cache?
1901 if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
1902 // Load extension only if not yet loaded
1903 if (!isset($GLOBALS['ext_productive'][$ext_name])) {
1904 // Load extension in test mode
1905 loadExtension($ext_name, 'test');
1909 $GLOBALS[__FUNCTION__][$ext_name] = ((isset($GLOBALS['ext_productive'][$ext_name])) && ($GLOBALS['ext_productive'][$ext_name] === true));
1912 // Log debug message
1913 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%s", $ext_name, intval($GLOBALS[__FUNCTION__][$ext_name])));
1916 return $GLOBALS[__FUNCTION__][$ext_name];