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 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2009 by Roland Haeder *
21 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Load the extension and maybe found language and function files.
46 function loadExtension ($ext_name, $ext_mode, $ext_ver = '', $dry_run = false) {
47 // If this happens twice, we need the bug report from you, except for updates/tests
48 if ((isset($GLOBALS['loaded_extension'][$ext_name][$ext_mode])) && (!in_array($ext_mode, array('update', 'test')))) {
49 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: ext_name=' . $ext_name . ', ext_mode='. $ext_mode . ',ext_sqls=' . print_r(getExtensionSqls(), true) . ', ext_register_running=' . print_r($GLOBALS['ext_register_running'], true) . ', ext_running_updates=' . print_r($GLOBALS['ext_running_updates'], true));
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);
69 setCurrentExtensionVersion('0.0');
71 // In all but test-mode we need these messages to debug! Please report all (together, e.g.)
72 if ($ext_mode != 'test') {
73 // Log empty versions not in test-mode, but maybe it is fine...
74 logDebugMessage(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0. ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', dry_run=' . intval($dry_run));
79 enableExtensionDryRun($dry_run);
82 initIncludePool('extension');
84 // Init EXT_UPDATE_DEPENDS if not yet done
85 if (!isExtensionUpdateDependenciesInitialized()) {
87 initExtensionUpdateDependencies();
90 // Init current extension name list
93 // Is the extension already loaded?
94 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Loading extension ' . $ext_name . ', mode=' . getExtensionMode() . ', ver=' . getCurrentExtensionVersion());
95 if ((isset($GLOBALS['ext_loaded']['ext'][$ext_name])) && (getExtensionMode() == 'init')) {
97 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
103 // Is the extension file NOT there?
104 if (!isExtensionNameValid($ext_name)) {
106 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable.", $ext_name));
112 // Load extension's own language file if not in test mode
113 if ((getExtensionMode() != 'test') && (ifExtensionHasLanguageFile($ext_name))) {
115 loadLanguageFile($ext_name);
119 if (isExtensionFunctionFileReadable($ext_name)) {
121 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]))) {
122 // Construct IFN for functions file
123 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
126 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
128 // Download functions file
129 loadIncludeOnce($funcsInclude);
131 } elseif ((!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) && (isDebugModeEnabled()) && (getScriptOutputMode() == '0') && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme') && (getExtensionMode() == 'test')) {
132 // No functions file is not so good...
133 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no own functions file or we cannot read from it. mode=%s",
139 // Extensions are not deprecated by default
140 setExtensionDeprecated('N');
142 // Extensions are not always active by default
143 setExtensionAlwaysActive('N');
145 // Extension update notes
146 // @TODO Do we still need this? setExtensionUpdateNotes('');
148 // Include the extension file
149 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension loaded.');
150 loadExtensionInclude();
152 // Is this extension deprecated?
153 if (isExtensionDeprecated()) {
154 // Deactivate the extension
155 doDeactivateExtension($ext_name);
161 // Mark it as loaded in normal mode
162 if (getExtensionMode() == '') {
164 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
171 // Registeres an extension and possible update depencies
172 function registerExtension ($ext_name, $taskId, $dry_run = false, $logout = true) {
173 // Set current extension name
174 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',task_id=' . $taskId . ',dry_run=' . intval($dry_run) . ',logout=' . intval($logout) . ' - ENTERED!');
175 setCurrentExtensionName($ext_name);
178 enableExtensionDryRun($dry_run);
180 // By default all extensions are in productive phase
181 enableExtensionProductive();
183 // This shall never do a non-admin user or if the extension is active (already installed)
184 if ((!isAdmin()) || (isExtensionInstalled($ext_name))) {
185 // Abort here with 'false'
189 // When this extension is already in registration/update phase, all is fine
190 if ((isExtensionRegistrationRunning($ext_name)) || (isExtensionUpdateRunning($ext_name))) {
191 // Then abort here with 'true' becaus it is fine
192 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ALREADY!');
196 // This registration is running
197 addExtensionRunningRegistration($ext_name);
199 // Init EXT_UPDATE_DEPENDS if not yet done
200 if (!isExtensionUpdateDependenciesInitialized()) {
202 initExtensionUpdateDependencies();
205 // Is the task id zero? Then we need to auto-fix it here
206 if ($taskId == '0') {
207 // Try to find the task
208 $taskId = determineExtensionTaskId(getCurrentExtensionName());
210 // Still zero and not in dry-run?
211 if (($taskId == '0') && (!isExtensionDryRun())) {
212 // Now try to create a new task
213 $taskId = createNewExtensionTask(getCurrentExtensionName());
216 if ($taskId == '0') {
217 // Then request a bug report
218 debug_report_bug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId(%s)",
220 getCurrentExtensionName()
226 // Init queries and notes
228 initExtensionNotes();
233 initIncludePool('extension');
235 // By default we have no failures
236 setExtensionReportsFailure(false);
238 // Does this extension exists?
239 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
240 if (loadExtension(getCurrentExtensionName(), 'register', '', isExtensionDryRun())) {
241 // Set current extension name again
242 setCurrentExtensionName($ext_name);
244 // And run possible updates
245 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
246 $history = getExtensionVersionHistory();
247 foreach ($history as $ext_ver) {
248 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ext_ver);
249 // Load extension in update mode
250 loadExtension(getCurrentExtensionName(), 'update', $ext_ver, isExtensionDryRun());
252 // Add update notes to our output
253 addExtensionNotes($ext_ver);
256 // Does this extension depends on an outstanding update of another update?
257 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
259 $ext_update = getExtensionUpdateDependenciesIterator();
261 // Increment here to avoid endless loop
262 incrementExtensionUpdateIterator();
264 // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
265 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
266 if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', isExtensionDryRun()))) {
267 // Set current extension name again
268 setCurrentExtensionName($ext_name);
270 // If versions mismatch update extension first
272 if (isExtensionInstalled($ext_update)) {
273 // Get version only if installed
274 $ext_ver = getExtensionVersion($ext_update);
277 // Extension version set? If empty the extension is not registered
278 if (empty($ext_ver)) {
279 // Extension not registered so far so first load task's id...
280 $task = determineExtensionTaskId($ext_update);
284 // Try to register the extension
285 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ':ext_update=' . $ext_update . ',taskId=' . $task);
286 $test = registerExtension($ext_update, $task, isExtensionDryRun(), false);
288 // Reset extension name
289 setCurrentExtensionName($ext_name);
290 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
292 } elseif ($ext_ver != getCurrentExtensionVersion()) {
293 // Ok, update this extension now
294 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
295 $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
296 $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
297 updateExtension($ext_update, $ext_ver, isExtensionDryRun());
298 setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
299 setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
300 unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
301 unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
302 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
307 // Nothing to register / update before...
311 // Required file for update does not exists!
313 // But this is fine for the first time...
316 // Restore the current extension name
317 setCurrentExtensionName($ext_name);
320 // Is there no update?
321 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
322 // Then test is passed!
326 // Switch back to register mode
327 setExtensionMode('register');
329 // Remains true if extension registration reports no failures
330 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
331 $test = (($test === true) && (getExtensionReportsFailure() === false));
332 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
334 // Does everthing before wents ok?
335 if ($test === true) {
336 // "Dry-run-mode" activated?
337 if ((isExtensionDryRun() === false) && (!isExtensionOnRemovalList())) {
338 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
339 // Init SQLs and transfer ext->generic
341 setSqlsArray(getExtensionSqls());
343 // Run installation pre-installation filters
344 runFilterChain('pre_extension_installed', array('dry_run' => isExtensionDryRun(), 'enable_codes' => false));
346 // Register extension
347 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
348 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
349 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
351 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`,`ext_has_css`) VALUES ('%s','%s','%s','%s')",
353 getCurrentExtensionName(),
354 getExtensionAlwaysActive(),
355 getCurrentExtensionVersion(),
357 ), __FUNCTION__, __LINE__);
359 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
361 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
363 getCurrentExtensionName(),
364 getExtensionAlwaysActive(),
365 getCurrentExtensionVersion()
366 ), __FUNCTION__, __LINE__);
369 // Use the insert id as extension id and cache it for early usage
370 $GLOBALS['cache_array']['extension']['ext_id'][getCurrentExtensionName()] = SQL_INSERTID();
372 // Remove cache file(s) if extension is active
373 runFilterChain('post_extension_installed', array(
374 'pool' => 'extension',
375 'ext_name' => getCurrentExtensionName(),
379 // Re-init queries and notes
380 initExtensionSqls(true);
381 initExtensionNotes(true);
383 // Mark it as installed
384 $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
386 // In normal mode return a true on success
388 } elseif (isExtensionDryRun() === true) {
389 // In "dry-run" mode do always return a true
392 // Extension has been removed for updates, so all is fine!
396 // No, an error occurs while registering extension :-(
397 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
400 } elseif (($taskId > 0) && (getCurrentExtensionName() != '')) {
401 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
402 // Remove task from system when id and extension's name is valid
403 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
404 array(bigintval($taskId)), __FUNCTION__, __LINE__);
407 // Is this the sql_patches?
408 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . '/' . getExtensionMode());
409 if ((getCurrentExtensionName() == 'sql_patches') && ((getExtensionMode() == 'register') || (getExtensionMode() == 'remove')) && (!isExtensionDryRun()) && ($test)) {
410 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ': LOAD!');
411 if ($logout === true) {
412 // Then redirect to logout
413 redirectToUrl('modules.php?module=admin&logout=1&' . getExtensionMode() . '=sql_patches');
415 // Add temporary filter
416 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
417 $GLOBALS['ext_load_mode'] = getExtensionMode();
421 // Return status code
422 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',test=' . intval($test) . ' - EXIT!');
426 // Run SQL queries for given extension id
427 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
428 function doExtensionSqls ($ext_id, $load_mode) {
429 // This shall never do a non-admin user!
430 if (!isAdmin()) return false;
432 // Get extension's name
433 $ext_name = getExtensionName($ext_id);
435 // Set current SQL name
436 setCurrentExtensionName($ext_name);
438 // Init EXT_UPDATE_DEPENDS
439 if (!isExtensionUpdateDependenciesInitialized()) {
441 initExtensionUpdateDependencies();
447 // By default no SQL has been executed
450 // Load extension in detected mode
451 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName());
452 loadExtension(getCurrentExtensionName(), $load_mode, '', false);
456 setSqlsArray(getExtensionSqls());
458 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls());
460 // Run SQL commands...
461 runFilterChain('run_sqls');
464 // Run any filters depending on the action here
465 runFilterChain('extension_' . $load_mode);
467 // Remove cache file(s) if extension is active
468 if (((isExtensionActive('cache')) && ((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
470 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
473 // Is this the sql_patches?
474 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $ext_id . ',currName=' . getCurrentExtensionName() . ',loadMode=' . $load_mode);
475 if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
476 // Then redirect to logout
477 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ': LOAD!');
478 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
482 // Check wether the given extension is installed
483 function isExtensionInstalled ($ext_name) {
484 // We don't like empty extension names here
485 if (empty($ext_name)) {
486 // Please fix them all
487 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty.');
490 // By default non is installed
491 $isInstalled = false;
493 // Check if there is a cache entry
494 if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
495 // Use cache built from below queries
496 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
497 } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
502 incrementStatsEntry('cache_hits');
503 } elseif ((isInstallationPhase())) {
504 // Extensions are all inactive/not installed during installation
507 $ext_id = getExtensionId($ext_name);
509 // Do we have a record?
510 $isInstalled = ($ext_id > 0);
512 // Is it installed, then cache the entry
513 if ($isInstalled === true) {
514 // Dummy call (get is okay here)
515 getExtensionId($ext_name, true);
518 // Remember the status
519 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
526 // Check if given extension is active
527 function isExtensionActive ($ext_name) {
528 if (isInstallationPhase()) {
529 // Extensions are all inactive during installation
531 } elseif (empty($ext_name)) {
532 // Empty extension names must befixed
533 debug_report_bug(__FUNCTION__, __LINE__, 'Empty extension name provided.');
534 } elseif (!isExtensionInstalled($ext_name)) {
535 // Not installed extensions are always inactive
539 // Not active is the default
540 $data['ext_active'] = 'N';
543 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
545 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE! ext_name=' . $ext_name);
546 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
549 incrementStatsEntry('cache_hits');
550 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
551 // @TODO Extension is loaded, what next?
552 debug_report_bug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
553 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
554 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'DB! ext_name=' . $ext_name);
555 // Load from database
556 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
557 array($ext_name), __FUNCTION__, __LINE__);
560 if (SQL_NUMROWS($result) == 1) {
562 $data = SQL_FETCHARRAY($result);
566 SQL_FREERESULT($result);
569 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . '[DB]: ' . $data['ext_active']);
570 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
572 // Extension not active!
573 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ': Not active!');
574 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
578 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',active=' . $data['ext_active']);
580 // Is this extension activated? (For admins we always have active extensions...)
581 return ($data['ext_active'] == 'Y');
584 // Get version from extensions
585 function getExtensionVersion ($ext_name, $force = false) {
586 // By default no extension is found
587 $data['ext_version'] = 'false';
589 // Empty extension name should be fixed!
590 if (empty($ext_name)) {
591 // Please report this bug!
592 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
595 // Extensions are all inactive during installation
596 if (isInstallationPhase()) return '';
597 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
599 // Is the cache written?
600 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
601 // Load data from cache
602 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
603 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
606 incrementStatsEntry('cache_hits');
607 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (getScriptOutputMode() != 0)) {
608 // Load from database
609 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
610 array($ext_name), __FUNCTION__, __LINE__);
611 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
613 // Is the extension there?
614 if (SQL_NUMROWS($result) == 1) {
616 $data = SQL_FETCHARRAY($result);
617 } elseif (isDebugModeEnabled()) {
618 // Not found, please report all
619 debug_report_bug(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
623 SQL_FREERESULT($result);
626 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
629 // Extension version should not be invalid
630 if (($data['ext_version'] == 'false') && ($force === false)) {
631 // Please report this trouble
632 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
636 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_version=' . $data['ext_version']);
637 return $data['ext_version'];
640 // Updates a given extension with current extension version to latest version
641 function updateExtension ($ext_name, $ext_ver, $dry_run = false) {
642 // Only admins are allowed to update extensions
643 if ((!isAdmin()) || (empty($ext_name))) return false;
645 // Set current SQL name
646 setCurrentExtensionName($ext_name);
650 initExtensionNotes();
651 initIncludePool('extension');
653 // Load extension in test mode
654 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
656 // Save version history
657 $history = getExtensionVersionHistory();
659 // Remove old SQLs array to prevent possible bugs
662 // Check if version is updated
663 //* DEBUG: */ debugOutput(getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)));
664 if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
665 // Search for starting point
666 $start = array_search($ext_ver, $history);
668 // And load SQL queries in order of version history
669 for ($idx = ($start + 1); $idx < count($history); $idx++) {
670 // Set extension version
671 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
674 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
677 $depencies = getExtensionUpdateDependencies();
680 if (count($depencies) > 0) {
681 // Apply all extension depencies
682 foreach ($depencies as $ext_depend) {
683 // Did we already update/register this?
684 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
686 setCurrentExtensionName($ext_depend);
688 // Mark it as already updated before we update it
689 $GLOBALS['ext_updated'][$ext_depend] = true;
691 // Is the extension there?
692 if (isExtensionInstalled($ext_depend)) {
693 // Update another extension first!
694 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun());
696 // Register new extension
697 $test = registerExtension($ext_depend, 0, isExtensionDryRun(), false);
702 // Set extension version here
703 setCurrentExtensionVersion($ext_ver);
706 setCurrentExtensionName($ext_name);
710 addExtensionNotes($history[$idx]);
713 // In real-mode execute any existing includes
714 if (isExtensionDryRun() === false) {
715 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
716 runFilterChain('load_includes', 'extension');
717 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
718 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
723 setSqlsArray(getExtensionSqls());
726 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun(), 'enable_codes' => false));
728 if (isExtensionDryRun() === false) {
729 // Run filters on success extension update
730 runFilterChain('extension_update', getCurrentExtensionName());
735 // Output verbose SQL table for extension
736 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
739 // Then fix it to default
740 $title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}';
746 // Do we have queries?
747 if (isVerboseSqlEnabled()) {
748 // Do we have entries?
749 if (countExtensionSqls() > 0) {
753 foreach (getExtensionSqls() as $sqls) {
754 // New array format is recursive
755 foreach ($sqls as $sql) {
759 // Output command if set
761 // Prepare output for template
764 'sql' => str_replace('{', '{', str_replace('}', '}', encodeEntities($sql)))
768 $OUT .= loadTemplate('admin_extension_sql_row', true, $content);
776 // Prepare content for template
784 // Load main template
785 $OUT = loadTemplate('admin_extension_sql_table', true, $content);
787 // No addional SQL commands to run
788 $OUT = loadTemplate('admin_settings_saved', true, '{--ADMIN_NO_ADDITIONAL_SQLS--}');
796 // Get extension name from id
797 function getExtensionName ($ext_id) {
798 // Init extension name
799 $data['ext_name'] = '';
802 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
804 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
807 incrementStatsEntry('cache_hits');
808 } elseif (!isExtensionActive('cache')) {
809 // Load from database
810 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
811 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
813 // Is the entry there?
814 if (SQL_NUMROWS($result) == 1) {
815 // Get the extension's name from database
816 $data = SQL_FETCHARRAY($result);
820 SQL_FREERESULT($result);
823 // Did we find some extension?
824 if (empty($data['ext_name'])) {
825 // We should fix these all!
826 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
829 // Return the extension name
830 return $data['ext_name'];
833 // Get extension id from name
834 function getExtensionId ($ext_name) {
836 $data['ext_id'] = '0';
839 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
841 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
844 incrementStatsEntry('cache_hits');
846 // Load from database
847 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
848 array($ext_name), __FUNCTION__, __LINE__);
850 // Is the entry there?
851 if (SQL_NUMROWS($result) == 1) {
852 // Get the extension's id from database
853 $data = SQL_FETCHARRAY($result);
857 SQL_FREERESULT($result);
860 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
864 return $data['ext_id'];
867 // Determines wether the given extension name is valid
868 function isExtensionNameValid ($ext_name) {
870 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
871 // Generate include file name
872 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
874 // Is there a file in inc/extensions/ ?
875 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
879 return $GLOBALS['ext_name_valid'][$ext_name];
882 // Determines wether the given extension id is valid
883 function isExtensionIdValid ($ext_id) {
884 // Default is nothing valid
887 // Check in cache then in database
888 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
893 incrementStatsEntry('cache_hits');
896 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
903 // Activate given extension
904 function doActivateExtension ($ext_name) {
905 // Activate the extension
906 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
907 array($ext_name), __FUNCTION__, __LINE__);
909 // Extension has been activated?
910 if (SQL_AFFECTEDROWS() == 1) {
911 // Then run all queries
912 doExtensionSqls(getExtensionId($ext_name), 'activate');
916 // Deactivate given extension
917 function doDeactivateExtension($ext_name) {
918 // Activate the extension
919 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
920 array($ext_name), __FUNCTION__, __LINE__);
922 // Extension has been activated?
923 if (SQL_AFFECTEDROWS() == 1) {
924 // Then run all queries
925 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
927 // Create new task (we ignore the task id here)
928 createExtensionDeactivationTask($ext_name);
931 sendAdminNotification(
932 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
933 'admin_extension_deactivated',
934 array('ext_name' => $ext_name)
939 // Checks wether the extension is older than given
940 function isExtensionOlder ($ext_name, $ext_ver) {
941 // Get current extension version
942 $currVersion = getExtensionVersion($ext_name);
944 // Remove all dots from both versions
945 $currVersion = str_replace('.', '', $currVersion);
946 $ext_ver = str_replace('.', '', $ext_ver);
948 // Now compare both and return the result
949 return ($currVersion < $ext_ver);
952 // Creates a new task for updated extension
953 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
954 // Create subject line
955 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
958 $taskId = determineTaskIdBySubject($subject);
960 // Is the extension there?
961 if (isExtensionInstalled($ext_name)) {
962 // Check if task is not there
963 if ($taskId == '0') {
964 // Create extension update-task
965 $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
968 // Extension not there! :-(
969 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
976 // Creates a new task for newly installed extension
977 function createNewExtensionTask ($ext_name) {
978 // Generate subject line
979 $subject = sprintf("[%s:]", $ext_name);
982 $taskId = determineTaskIdBySubject($subject);
984 // Not installed and do we have created a task for the admin?
985 if (($taskId == '0') && (!isExtensionInstalled($ext_name))) {
986 // Set default message if ext-foo is missing
987 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext_name);
990 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
996 // Load text for task if found
997 if (isFileReadable($tpl)) {
998 // Load extension's own text template (HTML!)
999 $message = loadTemplate('ext_' . $ext_name, true);
1001 // Write this in debug.log as well
1002 logDebugMessage(__FUNCTION__, __LINE__, $message);
1005 // Task not created so it's a brand-new extension which we need to register and create a task for!
1006 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), false);
1013 // Creates a task for automatically deactivated (deprecated) extension
1014 function createExtensionDeactivationTask ($ext_name) {
1015 // Create subject line
1016 $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1019 $taskId = determineTaskIdBySubject($subject);
1021 // Not installed and do we have created a task for the admin?
1022 if (($taskId == '0') && (isExtensionInstalled($ext_name))) {
1023 // Task not created so add it
1024 $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext_name)), 'EXTENSION_DEACTIVATION');
1031 // Determines the task id for given extension
1032 function determineExtensionTaskId ($ext_name) {
1033 // Default is not found
1034 $data['task_id'] = '0';
1036 // Search for extension task's id
1037 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1038 array($ext_name), __FUNCTION__, __LINE__);
1041 if (SQL_NUMROWS($result) == 1) {
1042 // Task found so load task's id and register extension...
1043 $data = SQL_FETCHARRAY($result);
1047 SQL_FREERESULT($result);
1050 return $data['task_id'];
1053 // Determines the task id for given subject
1054 function determineTaskIdBySubject ($subject) {
1055 // Default is not found
1056 $data['task_id'] = '0';
1058 // Search for task id
1059 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1060 array($subject), __FUNCTION__, __LINE__);
1063 if (SQL_NUMROWS($result) == 1) {
1064 // Task found so load task's id and register extension...
1065 $data = SQL_FETCHARRAY($result);
1069 SQL_FREERESULT($result);
1072 return $data['task_id'];
1075 // Add updates notes for given version
1076 function addExtensionNotes ($ext_ver) {
1077 // Init notes/content
1081 // Is do we have verbose output enabled?
1082 if ((!isExtensionActive('sql_patches')) || (isVerboseSqlEnabled())) {
1083 // Update notes found?
1084 if (isExtensionUpdateNoteSet($ext_ver)) {
1085 // Update notes found
1088 'notes' => getExtensionUpdateNotes($ext_ver)
1092 setExtensionUpdateNotes('', $ext_ver);
1093 } elseif (in_array($ext_ver, array('0.0', '0.0.0'))) {
1097 'notes' => '{--INITIAL_RELEASE--}'
1100 // No update notes found!
1103 'notes' => '{--NO_UPDATE_NOTES--}'
1108 $out = loadTemplate('admin_extension_notes', true, $content);
1112 appendExtensionNotes($out);
1115 // Getter for CSS files array
1116 function getExtensionCssFiles () {
1117 // By default no additional CSS files are found
1118 $cssFiles = array();
1120 // Is the array there?
1121 if (isset($GLOBALS['css_files'])) {
1123 $cssFiles = $GLOBALS['css_files'];
1130 // Init CSS files array
1131 function initExtensionCssFiles () {
1133 $GLOBALS['css_files'] = array();
1137 function addExtensionCssFile ($file) {
1138 // Is the array there?
1139 if (!isset($GLOBALS['css_files'])) {
1140 // Then auto-init them
1141 initExtensionCssFiles();
1145 $GLOBALS['css_files'][] = $file;
1148 // Setter for EXT_ALWAYS_ACTIVE flag
1149 function setExtensionAlwaysActive ($active) {
1150 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1153 // Getter for EXT_ALWAYS_ACTIVE flag
1154 function getExtensionAlwaysActive () {
1155 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1158 // Checks wether the current extension is always active
1159 function isExtensionAlwaysActive () {
1160 return (getExtensionAlwaysActive() == 'Y');
1163 // Setter for EXT_VERSION flag
1164 function setThisExtensionVersion ($ext_version) {
1165 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1168 // Getter for EXT_VERSION flag
1169 function getThisExtensionVersion () {
1170 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1173 // Setter for EXT_DEPRECATED flag
1174 function setExtensionDeprecated ($deprecated) {
1175 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1178 // Getter for EXT_DEPRECATED flag
1179 function isExtensionDeprecated () {
1180 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1183 // Setter for EXT_UPDATE_DEPENDS flag
1184 function addExtensionUpdateDependency ($updateDepends) {
1185 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1186 // Is the update depency empty? (NEED TO BE FIXED!)
1187 if (empty($updateDepends)) {
1188 // Please report this bug!
1189 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1192 // Is it not yet added?
1193 /* Only for debugging!
1194 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1195 // Double-adding isn't fine, too
1196 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1200 // Add it to the list of extension update depencies map
1201 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1203 // Remember it in the list of running updates
1204 $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1205 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1208 // Getter for running updates
1209 function getExtensionUpdatesRunning () {
1210 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1213 // Checks wether the given extension registration is in progress
1214 function isExtensionRegistrationRunning ($ext_name) {
1216 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1219 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1223 // Init EXT_UPDATE_DEPENDS flag
1224 function initExtensionUpdateDependencies () {
1225 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1227 // Init update depency map automatically if not found
1228 if (isExtensionUpdateDependenciesInitialized()) {
1229 // We need these bug reports as well...
1230 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1233 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1235 // Init running updates array
1236 initExtensionRuningUpdates();
1238 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1241 // Adds an extension as "registration in progress"
1242 function addExtensionRunningRegistration ($ext_name) {
1244 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1245 if (isExtensionRegistrationRunning($ext_name)) {
1246 // This is really bad and should not be quietly ignored
1247 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1251 $GLOBALS['ext_register_running'][] = $ext_name;
1252 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1255 // Checks wether EXT_UPDATE_DEPENDS is initialized
1256 function isExtensionUpdateDependenciesInitialized () {
1257 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1258 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1261 // Checks wether an update is already running for given extension
1262 function isExtensionUpdateRunning ($ext_name) {
1263 // Current and given extensions means whole array
1264 if ($ext_name == getCurrentExtensionName()) {
1265 // Default is not found
1268 // Walk through whole array
1269 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1271 if (($ext1 == $ext_name) || ($isRunning === true)) {
1273 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1280 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1285 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1288 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1292 // Initializes the list of running updates
1293 function initExtensionRuningUpdates () {
1294 // Auto-init ext_running_updates
1295 if (!isset($GLOBALS['ext_running_updates'])) {
1296 $GLOBALS['ext_running_updates'] = array();
1297 $GLOBALS['ext_register_running'] = array();
1301 // Getter for EXT_UPDATE_DEPENDS flag
1302 function getExtensionUpdateDependencies () {
1303 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1304 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1307 // Getter for next iterator depency
1308 function getExtensionUpdateDependenciesIterator () {
1309 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1310 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1313 // Counter for extension update depencies
1314 function countExtensionUpdateDependencies () {
1315 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1316 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1319 // Removes given extension from update denpency list
1320 function removeExtensionUpdateDependency ($ext_name) {
1322 $key = array_search($ext_name, getExtensionUpdateDependencies());
1325 if ($key !== false) {
1327 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1329 // And sort the array
1330 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1334 // Init iterator for update depencies
1335 function initExtensionUpdateIterator () {
1336 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1337 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1340 // Getter for depency iterator
1341 function getExtensionUpdateIterator () {
1342 // Auto-init iterator
1343 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1346 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1347 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1350 // Increments the update iterator
1351 function incrementExtensionUpdateIterator () {
1352 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1353 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1356 // Setter for EXT_REPORTS_FAILURE flag
1357 function setExtensionReportsFailure ($reportsFailure) {
1358 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1361 // Getter for EXT_REPORTS_FAILURE flag
1362 function getExtensionReportsFailure () {
1363 return $GLOBALS['ext_reports_failure'];
1366 // Setter for EXT_VER_HISTORY flag
1367 function setExtensionVersionHistory ($versionHistory) {
1368 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1371 // Getter for EXT_VER_HISTORY array
1372 function getExtensionVersionHistory () {
1373 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1376 // Setter for EXT_UPDATE_NOTES
1377 function setExtensionUpdateNotes ($updateNotes, $ext_ver = '') {
1378 // . '/' . getCurrentExtensionVersion()
1379 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ',getExtensionMode()=' . getExtensionMode() . ',ext_ver=' . $ext_ver . ',updateNotes()=' . strlen($updateNotes));
1380 if (empty($ext_ver)) {
1381 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1383 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1387 // Getter for EXT_UPDATE_NOTES
1388 function getExtensionUpdateNotes ($ext_ver) {
1389 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1392 // Checks if ext_update_notes is set
1393 function isExtensionUpdateNoteSet ($ext_ver) {
1394 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
1397 // Init extension notice
1398 function initExtensionNotes ($force = false) {
1399 // Is it already initialized?
1400 if (($force === false) && (isset($GLOBALS['ext_notes'][getCurrentExtensionName()]))) {
1401 // This is mostly not wanted, so please report it
1402 debug_report_bug(__FUNCTION__, __LINE__, 'ext_notes already set for extension ' . getCurrentExtensionName());
1405 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1406 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1409 // Append extension notice
1410 function appendExtensionNotes ($notes) {
1411 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ', notes()=' . strlen($notes));
1412 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1415 // Getter for extension notes
1416 function getExtensionNotes () {
1417 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1420 // Setter for current extension name
1421 function setCurrentExtensionName ($ext_name) {
1422 $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1425 // Getter for current extension name
1426 function getCurrentExtensionName () {
1427 if (!isset($GLOBALS['curr_extension_name'])) {
1429 debug_report_bug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1433 return $GLOBALS['curr_extension_name'];
1436 // Init SQLs array for current extension
1437 function initExtensionSqls ($force = false) {
1438 // Auto-init the array or if forced
1439 if (($force === true) || (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()]))) {
1441 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1443 // Initialize the generic array
1448 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1449 function addExtensionSql ($sql) {
1451 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1454 // Getter for SQLs array for current extension
1455 function getExtensionSqls () {
1456 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1457 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1458 // Not found, should not happen
1459 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1460 getCurrentExtensionName()
1465 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1468 // Count SQLs for current extension
1469 function countExtensionSqls () {
1470 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1471 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1472 // Not found, should not happen
1473 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1474 getCurrentExtensionName()
1479 return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1482 // Removes SQLs for current extension
1483 function unsetExtensionSqls () {
1484 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1487 // Auto-initializes the removal list
1488 function initExtensionRemovalList () {
1489 // Is the remove list there?
1490 if (!isset($GLOBALS['ext_update_remove'])) {
1492 $GLOBALS['ext_update_remove'] = array();
1496 // Checks wether the current extension is on the removal list
1497 function isExtensionOnRemovalList () {
1498 // Init removal list
1499 initExtensionRemovalList();
1502 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1505 // Adds the current extension to the removal list
1506 function addCurrentExtensionToRemovalList () {
1508 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1511 // Getter for removal list
1512 function getExtensionRemovalList () {
1513 // Return the removal list
1514 return $GLOBALS['ext_update_remove'];
1517 // Redirects if the provided extension is not installed
1518 function redirectOnUninstalledExtension ($ext_name) {
1519 // So is the extension there?
1520 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1521 // Redirect to index
1522 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1526 // Filter for initialization of all extensions by loading them in 'init' mode
1527 function FILTER_INIT_EXTENSIONS () {
1528 // Do we have some entries?
1529 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY!');
1530 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1531 // Load all found extensions if found
1532 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
1533 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1535 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - START');
1536 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1537 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - END');
1539 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1541 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1544 // Setter for extension mode
1545 function setExtensionMode ($ext_mode) {
1546 $GLOBALS['ext_mode'] = (string) $ext_mode;
1549 // Getter for extension mode
1550 function getExtensionMode () {
1551 return $GLOBALS['ext_mode'];
1554 // Setter for dry-run
1555 function enableExtensionDryRun ($dry_run = true) {
1556 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()='.getCurrentExtensionName().',dry='.intval($dry_run));
1557 $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1560 // Getter for dry-run
1561 function isExtensionDryRun () {
1562 return $GLOBALS['ext_dry_run'];
1565 // Setter for current extension version
1566 function setCurrentExtensionVersion ($ext_ver) {
1567 // ext_ver should never be empty in other modes than 'test'
1568 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1569 // Please report all these messages
1570 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1574 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1575 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1578 // Getter for current extension version
1579 function getCurrentExtensionVersion () {
1580 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1581 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1584 // Remove the extension from cache array
1585 function removeExtensionFromArray () {
1586 // "Cache" this name
1587 $ext_name = getCurrentExtensionName();
1589 // Now loop through the whole cache
1590 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1591 // Is it an element?
1592 if (isset($cacheArray[$ext_name])) {
1594 unset($cacheArray[$ext_name]);
1595 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1598 $key = array_search($ext_name, $cacheArray);
1601 if ($key !== false) {
1602 // Found, so remove it
1603 unset($cacheArray[$key]);
1604 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1610 // "Getter" for 'extension has a CSS file' (with same name, of course)
1611 function getExtensionHasCss () {
1612 // Default is no CSS
1615 // Construct FQFN for check
1616 $FQFN = sprintf("%stheme/%s/css/%s.css",
1619 getCurrentExtensionName()
1623 if (isFileReadable($FQFN)) {
1624 // Readable, so it is there...
1632 // Checks wether the given extension has a language file
1633 function ifExtensionHasLanguageFile ($ext_name) {
1634 // Default is no language file
1635 $hasLanguage = false;
1637 // Do we have cache?
1638 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1640 $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1643 incrementStatsEntry('cache_hits');
1645 // Not readable is default
1648 // Is the language file readable for this extension?
1649 if (isLanguageIncludeReadable($ext_name)) {
1655 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = $readable;
1659 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1662 // Load an extension's include file
1663 function loadExtensionInclude () {
1665 if (!isExtensionIncludeReadable()) {
1667 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1670 // Generate INC name
1671 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1677 // Checks wether an extension is readable
1678 function isExtensionIncludeReadable ($ext_name = '') {
1679 // If empty, use current
1680 if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1683 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1684 // Generate INC name
1685 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1688 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1692 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1693 return $GLOBALS['ext_inc_readable'][$ext_name];
1696 // Checks if an extension's function file is readable
1697 function isExtensionFunctionFileReadable ($ext_name) {
1699 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
1700 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1701 // Just count cache hits
1702 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] .' - CACHE!');
1703 incrementStatsEntry('cache_hits');
1705 // Construct IFN for functions file
1706 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1708 // Is this include there?
1709 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1711 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=Y - FOUND!');
1712 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1715 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=N - NOT FOUND!');
1716 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1721 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name]);
1722 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1725 // Adds an admin menu to the SQL queue of the menu entry is not found
1726 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1727 // Now check if this menu is there
1728 if (!isMenuActionValid('admin', $action, $what)) {
1730 if (is_null($what)) {
1732 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1740 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1749 // Add it to the queue
1750 addExtensionSql($sql);
1751 } elseif (isDebugModeEnabled()) {
1752 // Double menus should be located and fixed!
1753 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s, what=%s detected.", $action, $what));
1757 // Adds a guest menu to the SQL queue if the menu entry is not found
1758 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1759 // Now check if this menu is there
1760 if (!isMenuActionValid('guest', $action, $what)) {
1762 if (is_null($what)) {
1764 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1773 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1783 // Add it to the queue
1784 addExtensionSql($sql);
1785 } elseif (isDebugModeEnabled()) {
1786 // Double menus should be located and fixed!
1787 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s, what=%s detected.", $action, $what));
1791 // Adds a member menu to the SQL queue if the menu entry is not found
1792 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1793 // Now check if this menu is there
1794 if (!isMenuActionValid('member', $action, $what)) {
1796 if (is_null($what)) {
1798 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1807 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1817 // Add it to the queue
1818 addExtensionSql($sql);
1819 } elseif (isDebugModeEnabled()) {
1820 // Double menus should be located and fixed!
1821 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s, what=%s detected.", $action, $what));
1825 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1826 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1827 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1828 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1830 if (is_null($what)) {
1832 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1840 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1849 // Add it to the queue
1850 addExtensionSql($sql);
1851 } elseif (isDebugModeEnabled()) {
1852 // Double menus should be located and fixed!
1853 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s, what=%s detected.", $action, $what));
1857 // Enables/disables productive mode for current extension (used only while
1859 function enableExtensionProductive ($isProductive = true) {
1860 // Log debug message
1861 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%d", getCurrentExtensionName(), intval($isProductive)));
1864 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1867 // Checks wether the extension is in productive phase. If not set, development
1868 // phase (=false) is assumed.
1869 function isExtensionProductive ($ext_name = '') {
1870 // Is the extension name empty? Then use current
1871 if (empty($ext_name)) {
1872 // Get current extension name
1873 $ext_name = getCurrentExtensionName();
1876 // Do we have cache?
1877 if (!isset($GLOBALS['ext_is_productive'][$ext_name])) {
1878 // Load extension only if not yet loaded
1879 if (!isset($GLOBALS['ext_productive'][$ext_name])) {
1880 // Load extension in test mode
1881 loadExtension($ext_name, 'test');
1885 $GLOBALS['ext_is_productive'][$ext_name] = ((isset($GLOBALS['ext_productive'][$ext_name])) && ($GLOBALS['ext_productive'][$ext_name] === true));
1888 // Log debug message
1889 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%s", $ext_name, intval($GLOBALS['ext_is_productive'][$ext_name])));
1892 return $GLOBALS['ext_is_productive'][$ext_name];