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 if (!empty($ext_ver)) {
62 // Set current extension version
63 setCurrentExtensionVersion($ext_ver);
66 setCurrentExtensionVersion('0.0');
68 // In all but test-mode we need these messages to debug! Please report all (together, e.g.)
69 if ($ext_mode != 'test') {
70 // Log empty versions not in test-mode, but maybe it is fine...
71 logDebugMessage(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0. ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', dry_run=' . intval($dry_run));
76 enableExtensionDryRun($dry_run);
79 initIncludePool('extension');
81 // Init EXT_UPDATE_DEPENDS if not yet done
82 if (!isExtensionUpdateDependenciesInitialized()) {
84 initExtensionUpdateDependencies();
87 // Init current extension name list
90 // Is the extension already loaded?
91 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Loading extension ' . $ext_name . ', mode=' . getExtensionMode() . ', ver=' . getCurrentExtensionVersion());
92 if ((isset($GLOBALS['ext_loaded']['ext'][$ext_name])) && (getExtensionMode() == 'init')) {
94 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
100 // Is the extension file NOT there?
101 if (!isExtensionNameValid($ext_name)) {
103 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable.", $ext_name));
109 // Load extension's own language file if not in test mode
110 if ((getExtensionMode() != 'test') && (ifExtensionHasLanguageFile($ext_name))) {
112 loadLanguageFile($ext_name);
116 if (isExtensionFunctionFileReadable($ext_name)) {
118 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]))) {
119 // Construct IFN for functions file
120 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
123 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
125 // Download functions file
126 loadIncludeOnce($funcsInclude);
128 } 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')) {
129 // No functions file is not so good...
130 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no own functions file or we cannot read from it. mode=%s",
136 // Extensions are not deprecated by default
137 setExtensionDeprecated('N');
139 // Extensions are not always active by default
140 setExtensionAlwaysActive('N');
142 // Extension update notes
143 // @TODO Do we still need this? setExtensionUpdateNotes('');
145 // Include the extension file
146 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension loaded.');
147 loadExtensionInclude();
149 // Is this extension deprecated?
150 if (isExtensionDeprecated()) {
151 // Deactivate the extension
152 doDeactivateExtension($ext_name);
158 // Mark it as loaded in normal mode
159 if (getExtensionMode() == '') {
161 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
168 // Registeres an extension and possible update depencies
169 function registerExtension ($ext_name, $taskId, $dry_run = false, $logout = true) {
170 // Set current extension name
171 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',task_id=' . $taskId . ',dry_run=' . intval($dry_run) . ',logout=' . intval($logout) . ' - ENTERED!');
172 setCurrentExtensionName($ext_name);
175 enableExtensionDryRun($dry_run);
177 // By default all extensions are in productive phase
178 enableExtensionProductive();
180 // This shall never do a non-admin user or if the extension is active (already installed)
181 if ((!isAdmin()) || (isExtensionInstalled($ext_name))) {
182 // Abort here with 'false'
186 // When this extension is already in registration/update phase, all is fine
187 if ((isExtensionRegistrationRunning($ext_name)) || (isExtensionUpdateRunning($ext_name))) {
188 // Then abort here with 'true' becaus it is fine
189 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ALREADY!');
193 // This registration is running
194 addExtensionRunningRegistration($ext_name);
196 // Init EXT_UPDATE_DEPENDS if not yet done
197 if (!isExtensionUpdateDependenciesInitialized()) {
199 initExtensionUpdateDependencies();
202 // Is the task id zero? Then we need to auto-fix it here
203 if ($taskId == '0') {
204 // Try to find the task
205 $taskId = determineExtensionTaskId(getCurrentExtensionName());
207 // Still zero and not in dry-run?
208 if (($taskId == '0') && (!isExtensionDryRun())) {
209 // Now try to create a new task
210 $taskId = createNewExtensionTask(getCurrentExtensionName());
213 if ($taskId == '0') {
214 // Then request a bug report
215 debug_report_bug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId(%s)",
217 getCurrentExtensionName()
223 // Init queries and notes
225 initExtensionNotes();
230 initIncludePool('extension');
232 // By default we have no failures
233 setExtensionReportsFailure(false);
235 // Does this extension exists?
236 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
237 if (loadExtension(getCurrentExtensionName(), 'register', '', isExtensionDryRun())) {
238 // Set current extension name again
239 setCurrentExtensionName($ext_name);
241 // And run possible updates
242 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
243 $history = getExtensionVersionHistory();
244 foreach ($history as $ext_ver) {
245 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ext_ver);
246 // Load extension in update mode
247 loadExtension(getCurrentExtensionName(), 'update', $ext_ver, isExtensionDryRun());
249 // Add update notes to our output
250 addExtensionNotes($ext_ver);
253 // Does this extension depends on an outstanding update of another update?
254 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
256 $ext_update = getExtensionUpdateDependenciesIterator();
258 // Increment here to avoid endless loop
259 incrementExtensionUpdateIterator();
261 // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
262 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
263 if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', isExtensionDryRun()))) {
264 // Set current extension name again
265 setCurrentExtensionName($ext_name);
267 // If versions mismatch update extension first
269 if (isExtensionInstalled($ext_update)) {
270 // Get version only if installed
271 $ext_ver = getExtensionVersion($ext_update);
274 // Extension version set? If empty the extension is not registered
275 if (empty($ext_ver)) {
276 // Extension not registered so far so first load task's id...
277 $task = determineExtensionTaskId($ext_update);
281 // Try to register the extension
282 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ':ext_update=' . $ext_update . ',taskId=' . $task);
283 $test = registerExtension($ext_update, $task, isExtensionDryRun(), false);
285 // Reset extension name
286 setCurrentExtensionName($ext_name);
287 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
289 } elseif ($ext_ver != getCurrentExtensionVersion()) {
290 // Ok, update this extension now
291 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
292 $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
293 $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
294 updateExtension($ext_update, $ext_ver, isExtensionDryRun());
295 setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
296 setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
297 unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
298 unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
299 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
304 // Nothing to register / update before...
308 // Required file for update does not exists!
310 // But this is fine for the first time...
313 // Restore the current extension name
314 setCurrentExtensionName($ext_name);
317 // Is there no update?
318 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
319 // Then test is passed!
323 // Switch back to register mode
324 setExtensionMode('register');
326 // Remains true if extension registration reports no failures
327 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
328 $test = (($test === true) && (getExtensionReportsFailure() === false));
329 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
331 // Does everthing before wents ok?
332 if ($test === true) {
333 // "Dry-run-mode" activated?
334 if ((isExtensionDryRun() === false) && (!isExtensionOnRemovalList())) {
335 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
336 // Init SQLs and transfer ext->generic
338 setSqlsArray(getExtensionSqls());
340 // Run installation pre-installation filters
341 runFilterChain('pre_extension_installed', array('dry_run' => isExtensionDryRun()));
343 // Register extension
344 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
345 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
346 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
348 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`,`ext_has_css`) VALUES ('%s','%s','%s','%s')",
350 getCurrentExtensionName(),
351 getExtensionAlwaysActive(),
352 getCurrentExtensionVersion(),
354 ), __FUNCTION__, __LINE__);
356 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
358 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
360 getCurrentExtensionName(),
361 getExtensionAlwaysActive(),
362 getCurrentExtensionVersion()
363 ), __FUNCTION__, __LINE__);
366 // Use the insert id as extension id and cache it for early usage
367 $GLOBALS['cache_array']['extension']['ext_id'][getCurrentExtensionName()] = SQL_INSERTID();
369 // Remove cache file(s) if extension is active
370 runFilterChain('post_extension_installed', array(
371 'pool' => 'extension',
372 'ext_name' => getCurrentExtensionName(),
376 // Re-init queries and notes
377 initExtensionSqls(true);
378 initExtensionNotes(true);
380 // Mark it as installed
381 $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
383 // In normal mode return a true on success
385 } elseif (isExtensionDryRun() === true) {
386 // In "dry-run" mode do always return a true
389 // Extension has been removed for updates, so all is fine!
393 // No, an error occurs while registering extension :-(
394 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
397 } elseif (($taskId > 0) && (getCurrentExtensionName() != '')) {
398 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
399 // Remove task from system when id and extension's name is valid
400 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
401 array(bigintval($taskId)), __FUNCTION__, __LINE__);
404 // Is this the sql_patches?
405 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . '/' . getExtensionMode());
406 if ((getCurrentExtensionName() == 'sql_patches') && ((getExtensionMode() == 'register') || (getExtensionMode() == 'remove')) && (!isExtensionDryRun()) && ($test)) {
407 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ': LOAD!');
408 if ($logout === true) {
409 // Then redirect to logout
410 redirectToUrl('modules.php?module=admin&logout=1&' . getExtensionMode() . '=sql_patches');
412 // Add temporary filter
413 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
414 $GLOBALS['ext_load_mode'] = getExtensionMode();
418 // Return status code
419 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',test=' . intval($test) . ' - EXIT!');
423 // Run SQL queries for given extension id
424 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
425 function doExtensionSqls ($ext_id, $load_mode) {
426 // This shall never do a non-admin user!
427 if (!isAdmin()) return false;
429 // Get extension's name
430 $ext_name = getExtensionName($ext_id);
432 // Set current SQL name
433 setCurrentExtensionName($ext_name);
435 // Init EXT_UPDATE_DEPENDS
436 if (!isExtensionUpdateDependenciesInitialized()) {
438 initExtensionUpdateDependencies();
444 // By default no SQL has been executed
447 // Load extension in detected mode
448 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName());
449 loadExtension(getCurrentExtensionName(), $load_mode, '', false);
453 setSqlsArray(getExtensionSqls());
455 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls());
457 // Run SQL commands...
458 runFilterChain('run_sqls');
461 // Run any filters depending on the action here
462 runFilterChain('extension_' . $load_mode);
464 // Remove cache file(s) if extension is active
465 if (((isExtensionActive('cache')) && ((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
467 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
470 // Is this the sql_patches?
471 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $ext_id . ',currName=' . getCurrentExtensionName() . ',loadMode=' . $load_mode);
472 if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
473 // Then redirect to logout
474 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ': LOAD!');
475 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
479 // Check wether the given extension is installed
480 function isExtensionInstalled ($ext_name) {
481 // We don't like empty extension names here
482 if (empty($ext_name)) {
483 // Please fix them all
484 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty.');
487 // By default non is installed
488 $isInstalled = false;
490 // Check if there is a cache entry
491 if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
492 // Use cache built from below queries
493 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
494 } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
499 incrementStatsEntry('cache_hits');
500 } elseif ((isInstallationPhase())) {
501 // Extensions are all inactive/not installed during installation
504 $ext_id = getExtensionId($ext_name);
506 // Do we have a record?
507 $isInstalled = ($ext_id > 0);
509 // Is it installed, then cache the entry
510 if ($isInstalled === true) {
511 // Dummy call (get is okay here)
512 getExtensionId($ext_name, true);
515 // Remember the status
516 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
523 // Check if given extension is active
524 function isExtensionActive ($ext_name) {
525 if (isInstallationPhase()) {
526 // Extensions are all inactive during installation
528 } elseif (empty($ext_name)) {
529 // Empty extension names must befixed
530 debug_report_bug(__FUNCTION__, __LINE__, 'Empty extension name provided.');
531 } elseif (!isExtensionInstalled($ext_name)) {
532 // Not installed extensions are always inactive
536 // Not active is the default
537 $data['ext_active'] = 'N';
540 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
542 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE! ext_name=' . $ext_name);
543 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
546 incrementStatsEntry('cache_hits');
547 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
548 // @TODO Extension is loaded, what next?
549 debug_report_bug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
550 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
551 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'DB! ext_name=' . $ext_name);
552 // Load from database
553 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
554 array($ext_name), __FUNCTION__, __LINE__);
557 if (SQL_NUMROWS($result) == 1) {
559 $data = SQL_FETCHARRAY($result);
563 SQL_FREERESULT($result);
566 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . '[DB]: ' . $data['ext_active']);
567 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
569 // Extension not active!
570 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ': Not active!');
571 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
575 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',active=' . $data['ext_active']);
577 // Is this extension activated? (For admins we always have active extensions...)
578 return ($data['ext_active'] == 'Y');
581 // Get version from extensions
582 function getExtensionVersion ($ext_name, $force = false) {
583 // By default no extension is found
584 $data['ext_version'] = 'false';
586 // Empty extension name should be fixed!
587 if (empty($ext_name)) {
588 // Please report this bug!
589 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
592 // Extensions are all inactive during installation
593 if (isInstallationPhase()) return '';
594 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
596 // Is the cache written?
597 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
598 // Load data from cache
599 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
600 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
603 incrementStatsEntry('cache_hits');
604 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (getScriptOutputMode() != 0)) {
605 // Load from database
606 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
607 array($ext_name), __FUNCTION__, __LINE__);
608 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
610 // Is the extension there?
611 if (SQL_NUMROWS($result) == 1) {
613 $data = SQL_FETCHARRAY($result);
614 } elseif (isDebugModeEnabled()) {
615 // Not found, please report all
616 debug_report_bug(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
620 SQL_FREERESULT($result);
623 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
626 // Extension version should not be invalid
627 if (($data['ext_version'] == 'false') && ($force === false)) {
628 // Please report this trouble
629 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
633 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_version=' . $data['ext_version']);
634 return $data['ext_version'];
637 // Updates a given extension with current extension version to latest version
638 function updateExtension ($ext_name, $ext_ver, $dry_run = false) {
639 // Only admins are allowed to update extensions
640 if ((!isAdmin()) || (empty($ext_name))) return false;
642 // Set current SQL name
643 setCurrentExtensionName($ext_name);
647 initExtensionNotes();
648 initIncludePool('extension');
650 // Load extension in test mode
651 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
653 // Save version history
654 $history = getExtensionVersionHistory();
656 // Remove old SQLs array to prevent possible bugs
659 // Check if version is updated
660 //* DEBUG: */ debugOutput(getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)));
661 if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
662 // Search for starting point
663 $start = array_search($ext_ver, $history);
665 // And load SQL queries in order of version history
666 for ($idx = ($start + 1); $idx < count($history); $idx++) {
667 // Set extension version
668 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
671 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
674 $depencies = getExtensionUpdateDependencies();
677 if (count($depencies) > 0) {
678 // Apply all extension depencies
679 foreach ($depencies as $ext_depend) {
680 // Did we already update/register this?
681 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
683 setCurrentExtensionName($ext_depend);
685 // Mark it as already updated before we update it
686 $GLOBALS['ext_updated'][$ext_depend] = true;
688 // Is the extension there?
689 if (isExtensionInstalled($ext_depend)) {
690 // Update another extension first!
691 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun());
693 // Register new extension
694 $test = registerExtension($ext_depend, 0, isExtensionDryRun(), false);
699 // Set extension version here
700 setCurrentExtensionVersion($ext_ver);
703 setCurrentExtensionName($ext_name);
707 addExtensionNotes($history[$idx]);
710 // In real-mode execute any existing includes
711 if (isExtensionDryRun() === false) {
712 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
713 runFilterChain('load_includes', 'extension');
714 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
715 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
720 setSqlsArray(getExtensionSqls());
723 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun()));
725 if (isExtensionDryRun() === false) {
726 // Run filters on success extension update
727 runFilterChain('extension_update', getCurrentExtensionName());
732 // Output verbose SQL table for extension
733 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
736 // Then fix it to default
737 $title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}';
743 // Do we have queries?
744 if (isVerboseSqlEnabled()) {
745 // Do we have entries?
746 if (countExtensionSqls() > 0) {
750 foreach (getExtensionSqls() as $sqls) {
751 // New array format is recursive
752 foreach ($sqls as $sql) {
756 // Output command if set
758 // Prepare output for template
765 $OUT .= loadTemplate('admin_extension_sql_row', true, $content);
773 // Prepare content for template
781 // Load main template
782 $OUT = loadTemplate('admin_extension_sql_table', true, $content);
784 // No addional SQL commands to run
785 $OUT = loadTemplate('admin_settings_saved', true, '{--ADMIN_NO_ADDITIONAL_SQLS--}');
793 // Get extension name from id
794 function getExtensionName ($ext_id) {
795 // Init extension name
796 $data['ext_name'] = '';
799 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
801 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
804 incrementStatsEntry('cache_hits');
805 } elseif (!isExtensionActive('cache')) {
806 // Load from database
807 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
808 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
810 // Is the entry there?
811 if (SQL_NUMROWS($result) == 1) {
812 // Get the extension's name from database
813 $data = SQL_FETCHARRAY($result);
817 SQL_FREERESULT($result);
820 // Did we find some extension?
821 if (empty($data['ext_name'])) {
822 // We should fix these all!
823 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
826 // Return the extension name
827 return $data['ext_name'];
830 // Get extension id from name
831 function getExtensionId ($ext_name) {
833 $data['ext_id'] = '0';
836 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
838 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
841 incrementStatsEntry('cache_hits');
843 // Load from database
844 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
845 array($ext_name), __FUNCTION__, __LINE__);
847 // Is the entry there?
848 if (SQL_NUMROWS($result) == 1) {
849 // Get the extension's id from database
850 $data = SQL_FETCHARRAY($result);
854 SQL_FREERESULT($result);
857 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
861 return $data['ext_id'];
864 // Determines wether the given extension name is valid
865 function isExtensionNameValid ($ext_name) {
867 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
868 // Generate include file name
869 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
871 // Is there a file in inc/extensions/ ?
872 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
876 return $GLOBALS['ext_name_valid'][$ext_name];
879 // Determines wether the given extension id is valid
880 function isExtensionIdValid ($ext_id) {
881 // Default is nothing valid
884 // Check in cache then in database
885 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
890 incrementStatsEntry('cache_hits');
893 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
900 // Activate given extension
901 function doActivateExtension ($ext_name) {
902 // Activate the extension
903 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
904 array($ext_name), __FUNCTION__, __LINE__);
906 // Extension has been activated?
907 if (SQL_AFFECTEDROWS() == 1) {
908 // Then run all queries
909 doExtensionSqls(getExtensionId($ext_name), 'activate');
913 // Deactivate given extension
914 function doDeactivateExtension($ext_name) {
915 // Activate the extension
916 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
917 array($ext_name), __FUNCTION__, __LINE__);
919 // Extension has been activated?
920 if (SQL_AFFECTEDROWS() == 1) {
921 // Then run all queries
922 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
924 // Create new task (we ignore the task id here)
925 createExtensionDeactivationTask($ext_name);
928 sendAdminNotification(
929 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
930 'admin_extension_deactivated',
931 array('ext_name' => $ext_name)
936 // Checks wether the extension is older than given
937 function isExtensionOlder ($ext_name, $ext_ver) {
938 // Get current extension version
939 $currVersion = getExtensionVersion($ext_name);
941 // Remove all dots from both versions
942 $currVersion = str_replace('.', '', $currVersion);
943 $ext_ver = str_replace('.', '', $ext_ver);
945 // Now compare both and return the result
946 return ($currVersion < $ext_ver);
949 // Creates a new task for updated extension
950 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
951 // Create subject line
952 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
955 $taskId = determineTaskIdBySubject($subject);
957 // Is the extension there?
958 if (isExtensionInstalled($ext_name)) {
959 // Check if task is not there
960 if ($taskId == '0') {
961 // Create extension update-task
962 $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
965 // Extension not there! :-(
966 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
973 // Creates a new task for newly installed extension
974 function createNewExtensionTask ($ext_name) {
975 // Generate subject line
976 $subject = sprintf("[%s:]", $ext_name);
979 $taskId = determineTaskIdBySubject($subject);
981 // Not installed and do we have created a task for the admin?
982 if (($taskId == '0') && (!isExtensionInstalled($ext_name))) {
983 // Set default message if ext-foo is missing
984 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext_name);
987 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
993 // Load text for task if found
994 if (isFileReadable($tpl)) {
995 // Load extension's own text template (HTML!)
996 $message = loadTemplate('ext_' . $ext_name, true);
998 // Write this in debug.log as well
999 logDebugMessage(__FUNCTION__, __LINE__, $message);
1002 // Task not created so it's a brand-new extension which we need to register and create a task for!
1003 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), false);
1010 // Creates a task for automatically deactivated (deprecated) extension
1011 function createExtensionDeactivationTask ($ext_name) {
1012 // Create subject line
1013 $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1016 $taskId = determineTaskIdBySubject($subject);
1018 // Not installed and do we have created a task for the admin?
1019 if (($taskId == '0') && (isExtensionInstalled($ext_name))) {
1020 // Task not created so add it
1021 $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext_name)), 'EXTENSION_DEACTIVATION');
1028 // Determines the task id for given extension
1029 function determineExtensionTaskId ($ext_name) {
1030 // Default is not found
1031 $data['task_id'] = '0';
1033 // Search for extension task's id
1034 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1035 array($ext_name), __FUNCTION__, __LINE__);
1038 if (SQL_NUMROWS($result) == 1) {
1039 // Task found so load task's id and register extension...
1040 $data = SQL_FETCHARRAY($result);
1044 SQL_FREERESULT($result);
1047 return $data['task_id'];
1050 // Determines the task id for given subject
1051 function determineTaskIdBySubject ($subject) {
1052 // Default is not found
1053 $data['task_id'] = '0';
1055 // Search for task id
1056 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1057 array($subject), __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 // Add updates notes for given version
1073 function addExtensionNotes ($ext_ver) {
1074 // Init notes/content
1078 // Is do we have verbose output enabled?
1079 if ((!isExtensionActive('sql_patches')) || (isVerboseSqlEnabled())) {
1080 // Update notes found?
1081 if (isExtensionUpdateNoteSet($ext_ver)) {
1082 // Update notes found
1085 'notes' => getExtensionUpdateNotes($ext_ver)
1089 setExtensionUpdateNotes('', $ext_ver);
1090 } elseif (in_array($ext_ver, array('0.0', '0.0.0'))) {
1094 'notes' => '{--INITIAL_RELEASE--}'
1097 // No update notes found!
1100 'notes' => '{--NO_UPDATE_NOTES--}'
1105 $out = loadTemplate('admin_extension_notes', true, $content);
1109 appendExtensionNotes($out);
1112 // Getter for CSS files array
1113 function getExtensionCssFiles () {
1114 // By default no additional CSS files are found
1115 $cssFiles = array();
1117 // Is the array there?
1118 if (isset($GLOBALS['css_files'])) {
1120 $cssFiles = $GLOBALS['css_files'];
1127 // Init CSS files array
1128 function initExtensionCssFiles () {
1130 $GLOBALS['css_files'] = array();
1134 function addExtensionCssFile ($file) {
1135 // Is the array there?
1136 if (!isset($GLOBALS['css_files'])) {
1137 // Then auto-init them
1138 initExtensionCssFiles();
1142 $GLOBALS['css_files'][] = $file;
1145 // Setter for EXT_ALWAYS_ACTIVE flag
1146 function setExtensionAlwaysActive ($active) {
1147 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1150 // Getter for EXT_ALWAYS_ACTIVE flag
1151 function getExtensionAlwaysActive () {
1152 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1155 // Checks wether the current extension is always active
1156 function isExtensionAlwaysActive () {
1157 return (getExtensionAlwaysActive() == 'Y');
1160 // Setter for EXT_VERSION flag
1161 function setThisExtensionVersion ($ext_version) {
1162 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1165 // Getter for EXT_VERSION flag
1166 function getThisExtensionVersion () {
1167 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1170 // Setter for EXT_DEPRECATED flag
1171 function setExtensionDeprecated ($deprecated) {
1172 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1175 // Getter for EXT_DEPRECATED flag
1176 function isExtensionDeprecated () {
1177 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1180 // Setter for EXT_UPDATE_DEPENDS flag
1181 function addExtensionUpdateDependency ($updateDepends) {
1182 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1183 // Is the update depency empty? (NEED TO BE FIXED!)
1184 if (empty($updateDepends)) {
1185 // Please report this bug!
1186 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1189 // Is it not yet added?
1190 /* Only for debugging!
1191 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1192 // Double-adding isn't fine, too
1193 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1197 // Add it to the list of extension update depencies map
1198 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1200 // Remember it in the list of running updates
1201 $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1202 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1205 // Getter for running updates
1206 function getExtensionUpdatesRunning () {
1207 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1210 // Checks wether the given extension registration is in progress
1211 function isExtensionRegistrationRunning ($ext_name) {
1213 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1216 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1220 // Init EXT_UPDATE_DEPENDS flag
1221 function initExtensionUpdateDependencies () {
1222 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1224 // Init update depency map automatically if not found
1225 if (isExtensionUpdateDependenciesInitialized()) {
1226 // We need these bug reports as well...
1227 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1230 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1232 // Init running updates array
1233 initExtensionRuningUpdates();
1235 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1238 // Adds an extension as "registration in progress"
1239 function addExtensionRunningRegistration ($ext_name) {
1241 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1242 if (isExtensionRegistrationRunning($ext_name)) {
1243 // This is really bad and should not be quietly ignored
1244 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1248 $GLOBALS['ext_register_running'][] = $ext_name;
1249 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1252 // Checks wether EXT_UPDATE_DEPENDS is initialized
1253 function isExtensionUpdateDependenciesInitialized () {
1254 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1255 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1258 // Checks wether an update is already running for given extension
1259 function isExtensionUpdateRunning ($ext_name) {
1260 // Current and given extensions means whole array
1261 if ($ext_name == getCurrentExtensionName()) {
1262 // Default is not found
1265 // Walk through whole array
1266 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1268 if (($ext1 == $ext_name) || ($isRunning === true)) {
1270 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1277 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1282 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1285 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1289 // Initializes the list of running updates
1290 function initExtensionRuningUpdates () {
1291 // Auto-init ext_running_updates
1292 if (!isset($GLOBALS['ext_running_updates'])) {
1293 $GLOBALS['ext_running_updates'] = array();
1294 $GLOBALS['ext_register_running'] = array();
1298 // Getter for EXT_UPDATE_DEPENDS flag
1299 function getExtensionUpdateDependencies () {
1300 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1301 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1304 // Getter for next iterator depency
1305 function getExtensionUpdateDependenciesIterator () {
1306 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1307 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1310 // Counter for extension update depencies
1311 function countExtensionUpdateDependencies () {
1312 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1313 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1316 // Removes given extension from update denpency list
1317 function removeExtensionUpdateDependency ($ext_name) {
1319 $key = array_search($ext_name, getExtensionUpdateDependencies());
1322 if ($key !== false) {
1324 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1326 // And sort the array
1327 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1331 // Init iterator for update depencies
1332 function initExtensionUpdateIterator () {
1333 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1334 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1337 // Getter for depency iterator
1338 function getExtensionUpdateIterator () {
1339 // Auto-init iterator
1340 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1343 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1344 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1347 // Increments the update iterator
1348 function incrementExtensionUpdateIterator () {
1349 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1350 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1353 // Setter for EXT_REPORTS_FAILURE flag
1354 function setExtensionReportsFailure ($reportsFailure) {
1355 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1358 // Getter for EXT_REPORTS_FAILURE flag
1359 function getExtensionReportsFailure () {
1360 return $GLOBALS['ext_reports_failure'];
1363 // Setter for EXT_VER_HISTORY flag
1364 function setExtensionVersionHistory ($versionHistory) {
1365 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1368 // Getter for EXT_VER_HISTORY array
1369 function getExtensionVersionHistory () {
1370 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1373 // Setter for EXT_UPDATE_NOTES
1374 function setExtensionUpdateNotes ($updateNotes, $ext_ver = '') {
1375 // . '/' . getCurrentExtensionVersion()
1376 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ',getExtensionMode()=' . getExtensionMode() . ',ext_ver=' . $ext_ver . ',updateNotes()=' . strlen($updateNotes));
1377 if (empty($ext_ver)) {
1378 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1380 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1384 // Getter for EXT_UPDATE_NOTES
1385 function getExtensionUpdateNotes ($ext_ver) {
1386 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1389 // Checks if ext_update_notes is set
1390 function isExtensionUpdateNoteSet ($ext_ver) {
1391 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
1394 // Init extension notice
1395 function initExtensionNotes ($force = false) {
1396 // Is it already initialized?
1397 if (($force === false) && (isset($GLOBALS['ext_notes'][getCurrentExtensionName()]))) {
1398 // This is mostly not wanted, so please report it
1399 debug_report_bug(__FUNCTION__, __LINE__, 'ext_notes already set for extension ' . getCurrentExtensionName());
1402 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1403 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1406 // Append extension notice
1407 function appendExtensionNotes ($notes) {
1408 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ', notes()=' . strlen($notes));
1409 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1412 // Getter for extension notes
1413 function getExtensionNotes () {
1414 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1417 // Setter for current extension name
1418 function setCurrentExtensionName ($ext_name) {
1419 $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1422 // Getter for current extension name
1423 function getCurrentExtensionName () {
1424 if (!isset($GLOBALS['curr_extension_name'])) {
1426 debug_report_bug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1430 return $GLOBALS['curr_extension_name'];
1433 // Init SQLs array for current extension
1434 function initExtensionSqls ($force = false) {
1435 // Auto-init the array or if forced
1436 if (($force === true) || (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()]))) {
1438 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1440 // Initialize the generic array
1445 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1446 function addExtensionSql ($sql) {
1448 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1451 // Getter for SQLs array for current extension
1452 function getExtensionSqls () {
1453 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1454 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1455 // Not found, should not happen
1456 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1457 getCurrentExtensionName()
1462 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1465 // Count SQLs for current extension
1466 function countExtensionSqls () {
1467 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1468 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1469 // Not found, should not happen
1470 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1471 getCurrentExtensionName()
1476 return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1479 // Removes SQLs for current extension
1480 function unsetExtensionSqls () {
1481 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1484 // Auto-initializes the removal list
1485 function initExtensionRemovalList () {
1486 // Is the remove list there?
1487 if (!isset($GLOBALS['ext_update_remove'])) {
1489 $GLOBALS['ext_update_remove'] = array();
1493 // Checks wether the current extension is on the removal list
1494 function isExtensionOnRemovalList () {
1495 // Init removal list
1496 initExtensionRemovalList();
1499 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1502 // Adds the current extension to the removal list
1503 function addCurrentExtensionToRemovalList () {
1505 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1508 // Getter for removal list
1509 function getExtensionRemovalList () {
1510 // Return the removal list
1511 return $GLOBALS['ext_update_remove'];
1514 // Redirects if the provided extension is not installed
1515 function redirectOnUninstalledExtension ($ext_name) {
1516 // So is the extension there?
1517 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1518 // Redirect to index
1519 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1523 // Filter for initialization of all extensions by loading them in 'init' mode
1524 function FILTER_INIT_EXTENSIONS () {
1525 // Do we have some entries?
1526 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY!');
1527 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1528 // Load all found extensions if found
1529 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
1530 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1532 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - START');
1533 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1534 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - END');
1536 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1538 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1541 // Setter for extension mode
1542 function setExtensionMode ($ext_mode) {
1543 $GLOBALS['ext_mode'] = (string) $ext_mode;
1546 // Getter for extension mode
1547 function getExtensionMode () {
1548 return $GLOBALS['ext_mode'];
1551 // Setter for dry-run
1552 function enableExtensionDryRun ($dry_run = true) {
1553 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()='.getCurrentExtensionName().',dry='.intval($dry_run));
1554 $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1557 // Getter for dry-run
1558 function isExtensionDryRun () {
1559 return $GLOBALS['ext_dry_run'];
1562 // Setter for current extension version
1563 function setCurrentExtensionVersion ($ext_ver) {
1564 // ext_ver should never be empty in other modes than 'test'
1565 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1566 // Please report all these messages
1567 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1571 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1572 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1575 // Getter for current extension version
1576 function getCurrentExtensionVersion () {
1577 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1578 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1581 // Remove the extension from cache array
1582 function removeExtensionFromArray () {
1583 // "Cache" this name
1584 $ext_name = getCurrentExtensionName();
1586 // Now loop through the whole cache
1587 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1588 // Is it an element?
1589 if (isset($cacheArray[$ext_name])) {
1591 unset($cacheArray[$ext_name]);
1592 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1595 $key = array_search($ext_name, $cacheArray);
1598 if ($key !== false) {
1599 // Found, so remove it
1600 unset($cacheArray[$key]);
1601 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1607 // "Getter" for 'extension has a CSS file' (with same name, of course)
1608 function getExtensionHasCss () {
1609 // Default is no CSS
1612 // Construct FQFN for check
1613 $FQFN = sprintf("%stheme/%s/css/%s.css",
1616 getCurrentExtensionName()
1620 if (isFileReadable($FQFN)) {
1621 // Readable, so it is there...
1629 // Checks wether the given extension has a language file
1630 function ifExtensionHasLanguageFile ($ext_name) {
1631 // Default is no language file
1632 $hasLanguage = false;
1634 // Do we have cache?
1635 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1637 $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1640 incrementStatsEntry('cache_hits');
1642 // Not readable is default
1645 // Is the language file readable for this extension?
1646 if (isLanguageIncludeReadable($ext_name)) {
1652 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = 'Y';
1656 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1659 // Load an extension's include file
1660 function loadExtensionInclude () {
1662 if (!isExtensionIncludeReadable()) {
1664 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1667 // Generate INC name
1668 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1674 // Checks wether an extension is readable
1675 function isExtensionIncludeReadable ($ext_name = '') {
1676 // If empty, use current
1677 if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1680 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1681 // Generate INC name
1682 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1685 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1689 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1690 return $GLOBALS['ext_inc_readable'][$ext_name];
1693 // Checks if an extension's function file is readable
1694 function isExtensionFunctionFileReadable ($ext_name) {
1696 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
1697 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1698 // Just count cache hits
1699 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] .' - CACHE!');
1700 incrementStatsEntry('cache_hits');
1702 // Construct IFN for functions file
1703 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1705 // Is this include there?
1706 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1708 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=Y - FOUND!');
1709 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1712 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=N - NOT FOUND!');
1713 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1718 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name]);
1719 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1722 // Adds an admin menu to the SQL queue of the menu entry is not found
1723 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1724 // Now check if this menu is there
1725 if (!isMenuActionValid('admin', $action, $what)) {
1727 if (is_null($what)) {
1729 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1737 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1746 // Add it to the queue
1747 addExtensionSql($sql);
1748 } elseif (isDebugModeEnabled()) {
1749 // Double menus should be located and fixed!
1750 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s, what=%s detected.", $action, $what));
1754 // Adds a guest menu to the SQL queue if the menu entry is not found
1755 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1756 // Now check if this menu is there
1757 if (!isMenuActionValid('guest', $action, $what)) {
1759 if (is_null($what)) {
1761 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1770 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1780 // Add it to the queue
1781 addExtensionSql($sql);
1782 } elseif (isDebugModeEnabled()) {
1783 // Double menus should be located and fixed!
1784 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s, what=%s detected.", $action, $what));
1788 // Adds a member menu to the SQL queue if the menu entry is not found
1789 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1790 // Now check if this menu is there
1791 if (!isMenuActionValid('member', $action, $what)) {
1793 if (is_null($what)) {
1795 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1804 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1814 // Add it to the queue
1815 addExtensionSql($sql);
1816 } elseif (isDebugModeEnabled()) {
1817 // Double menus should be located and fixed!
1818 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s, what=%s detected.", $action, $what));
1822 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1823 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1824 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1825 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1827 if (is_null($what)) {
1829 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1837 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1846 // Add it to the queue
1847 addExtensionSql($sql);
1848 } elseif (isDebugModeEnabled()) {
1849 // Double menus should be located and fixed!
1850 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s, what=%s detected.", $action, $what));
1854 // Enables/disables productive mode for current extension (used only while
1856 function enableExtensionProductive ($isProductive = true) {
1857 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1860 // Checks wether the extension is in productive phase. If not set, development
1861 // phase (=false) is assumed.
1862 function isExtensionProductive () {
1863 return ((isset($GLOBALS['ext_productive'][getCurrentExtensionName()])) && ($GLOBALS['ext_productive'][getCurrentExtensionName()] === true));