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 = '0.0', $dry_run = false) {
47 // Loading an extension in same mode, but not test/update, twice is not
48 // good, so is the extension $ext_name already loaded in mode $ext_mode?
49 if ((isset($GLOBALS['loaded_extension'][$ext_name][$ext_mode])) && (!in_array($ext_mode, array('update', 'test')))) {
50 // If this happens twice, we need the bug report from you, except for updates/tests
51 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));
54 // Make sure this situation can only happen once
55 $GLOBALS['loaded_extension'][$ext_name][$ext_mode] = true;
58 setExtensionMode($ext_mode);
60 // Set current extension name
61 setCurrentExtensionName($ext_name);
63 // By default all extensions are in productive phase
64 enableExtensionProductive();
66 if (!empty($ext_ver)) {
67 // Set current extension version
68 setCurrentExtensionVersion($ext_ver);
70 // Set it to 0.0 by default
71 setCurrentExtensionVersion('0.0');
73 // Is the extension installed?
74 if ((isExtensionInstalled($ext_name)) && ($ext_mode != 'register')) {
75 // Get extension's version
76 setCurrentExtensionVersion(getExtensionVersion($ext_name));
79 // In all but test-mode we need these messages to debug! Please report all (together, e.g.)
80 if (($ext_mode != 'test') && (getCurrentExtensionVersion() == '0.0')) {
81 // Abort here, this must now always be set!
82 debug_report_bug(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0. ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', dry_run=' . intval($dry_run));
87 enableExtensionDryRun($dry_run);
90 initIncludePool('extension');
92 // Init EXT_UPDATE_DEPENDS if not yet done
93 if (!isExtensionUpdateDependenciesInitialized()) {
95 initExtensionUpdateDependencies();
98 // Init current extension name list
101 // Is the extension already loaded?
102 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Loading extension ' . $ext_name . ', mode=' . getExtensionMode() . ', ver=' . getCurrentExtensionVersion());
103 if ((isset($GLOBALS['ext_loaded']['ext'][$ext_name])) && (getExtensionMode() == 'init')) {
105 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
111 // Is the extension file NOT there?
112 if (!isExtensionNameValid($ext_name)) {
114 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable.", $ext_name));
120 // Load extension's own language file if not in test mode
121 if ((getExtensionMode() != 'test') && (ifExtensionHasLanguageFile($ext_name))) {
123 loadLanguageFile($ext_name);
127 if (isExtensionFunctionFileReadable($ext_name)) {
129 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]))) {
130 // Construct IFN for functions file
131 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
134 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
136 // Download functions file
137 loadIncludeOnce($funcsInclude);
139 } elseif ((!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) && (isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme') && (getExtensionMode() == 'test')) {
140 // No functions file is not so good...
141 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no own functions file or we cannot read from it. mode=%s",
147 // Extensions are not deprecated by default
148 setExtensionDeprecated('N');
150 // Extensions are not always active by default
151 setExtensionAlwaysActive('N');
153 // Extension update notes
154 // @TODO Do we still need this? setExtensionUpdateNotes('');
156 // Include the extension file
157 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension loaded.');
158 loadExtensionInclude();
160 // Is this extension deprecated?
161 if (isExtensionDeprecated()) {
162 // Deactivate the extension
163 doDeactivateExtension($ext_name);
169 // Mark it as loaded in normal mode
170 if (getExtensionMode() == '') {
172 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
179 // Registeres an extension and possible update depencies
180 function registerExtension ($ext_name, $taskId, $dry_run = false, $logout = true) {
181 // Set current extension name
182 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',task_id=' . $taskId . ',dry_run=' . intval($dry_run) . ',logout=' . intval($logout) . ' - ENTERED!');
183 setCurrentExtensionName($ext_name);
186 enableExtensionDryRun($dry_run);
188 // By default all extensions are in productive phase
189 enableExtensionProductive();
191 // This shall never do a non-admin user or if the extension is active (already installed)
192 if ((!isAdmin()) || (isExtensionInstalled($ext_name))) {
193 // Abort here with 'false'
197 // When this extension is already in registration/update phase, all is fine
198 if ((isExtensionRegistrationRunning($ext_name)) || (isExtensionUpdateRunning($ext_name))) {
199 // Then abort here with 'true' becaus it is fine
200 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ALREADY!');
204 // This registration is running
205 addExtensionRunningRegistration($ext_name);
207 // Init EXT_UPDATE_DEPENDS if not yet done
208 if (!isExtensionUpdateDependenciesInitialized()) {
210 initExtensionUpdateDependencies();
213 // Is the task id zero? Then we need to auto-fix it here
214 if ($taskId == '0') {
215 // Try to find the task
216 $taskId = determineExtensionTaskId(getCurrentExtensionName());
218 // Still zero and not in dry-run?
219 if (($taskId == '0') && (!isExtensionDryRun())) {
220 // Now try to create a new task
221 $taskId = createNewExtensionTask(getCurrentExtensionName());
224 if ($taskId == '0') {
225 // Then request a bug report
226 debug_report_bug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId(%s)",
228 getCurrentExtensionName()
234 // Init queries and notes
236 initExtensionNotes();
241 initIncludePool('extension');
243 // By default we have no failures
244 setExtensionReportsFailure(false);
246 // Does this extension exists?
247 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
248 if (loadExtension(getCurrentExtensionName(), 'register', '0.0', isExtensionDryRun())) {
249 // Set current extension name again
250 setCurrentExtensionName($ext_name);
252 // And run possible updates
253 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
254 $history = getExtensionVersionHistory();
255 foreach ($history as $ext_ver) {
256 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ext_ver);
257 // Load extension in update mode
258 loadExtension(getCurrentExtensionName(), 'update', $ext_ver, isExtensionDryRun());
260 // Add update notes to our output
261 addExtensionNotes($ext_ver);
264 // Does this extension depends on an outstanding update of another update?
265 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
267 $ext_update = getExtensionUpdateDependenciesIterator();
269 // Increment here to avoid endless loop
270 incrementExtensionUpdateIterator();
272 // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
273 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
274 if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', isExtensionDryRun()))) {
275 // Set current extension name again
276 setCurrentExtensionName($ext_name);
278 // If versions mismatch update extension first
280 if (isExtensionInstalled($ext_update)) {
281 // Get version only if installed
282 $ext_ver = getExtensionVersion($ext_update);
285 // Extension version set? If empty the extension is not registered
286 if (empty($ext_ver)) {
287 // Extension not registered so far so first load task's id...
288 $task = determineExtensionTaskId($ext_update);
292 // Try to register the extension
293 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ':ext_update=' . $ext_update . ',taskId=' . $task);
294 $test = registerExtension($ext_update, $task, isExtensionDryRun(), false);
296 // Reset extension name
297 setCurrentExtensionName($ext_name);
298 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
300 } elseif ($ext_ver != getCurrentExtensionVersion()) {
301 // Ok, update this extension now
302 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
303 $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
304 $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
305 updateExtension($ext_update, $ext_ver, isExtensionDryRun());
306 setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
307 setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
308 unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
309 unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
310 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
315 // Nothing to register / update before...
319 // Required file for update does not exists!
321 // But this is fine for the first time...
324 // Restore the current extension name
325 setCurrentExtensionName($ext_name);
328 // Is there no update?
329 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
330 // Then test is passed!
334 // Switch back to register mode
335 setExtensionMode('register');
337 // Remains true if extension registration reports no failures
338 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
339 $test = (($test === true) && (getExtensionReportsFailure() === false));
340 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
342 // Does everthing before wents ok?
343 if ($test === true) {
344 // "Dry-run-mode" activated?
345 if ((isExtensionDryRun() === false) && (!isExtensionOnRemovalList())) {
346 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
347 // Init SQLs and transfer ext->generic
349 setSqlsArray(getExtensionSqls());
351 // Run installation pre-installation filters
352 runFilterChain('pre_extension_installed', array('dry_run' => isExtensionDryRun(), 'enable_codes' => false));
354 // Register extension
355 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
356 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
357 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
359 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`,`ext_has_css`) VALUES ('%s','%s','%s','%s')",
361 getCurrentExtensionName(),
362 getExtensionAlwaysActive(),
363 getCurrentExtensionVersion(),
365 ), __FUNCTION__, __LINE__);
367 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
369 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
371 getCurrentExtensionName(),
372 getExtensionAlwaysActive(),
373 getCurrentExtensionVersion()
374 ), __FUNCTION__, __LINE__);
377 // Use the insert id as extension id and cache it for early usage
378 $GLOBALS['cache_array']['extension']['ext_id'][getCurrentExtensionName()] = SQL_INSERTID();
379 $GLOBALS['cache_array']['extension']['ext_name'][SQL_INSERTID()] = getCurrentExtensionName();
381 // Mark it as installed
382 $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
384 // Remove cache file(s) if extension is active
385 runFilterChain('post_extension_installed', array(
386 'pool' => 'extension',
387 'ext_name' => getCurrentExtensionName(),
391 // Re-init queries and notes
392 initExtensionSqls(true);
393 initExtensionNotes(true);
395 // In normal mode return a true on success
397 } elseif (isExtensionDryRun() === true) {
398 // In "dry-run" mode do always return a true
401 // Extension has been removed for updates, so all is fine!
405 // No, an error occurs while registering extension :-(
406 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
409 } elseif (($taskId > 0) && (getCurrentExtensionName() != '')) {
410 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
411 // Remove task from system when id and extension's name is valid
412 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
413 array(bigintval($taskId)), __FUNCTION__, __LINE__);
416 // Is this the sql_patches?
417 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . '/' . getExtensionMode());
418 if ((getCurrentExtensionName() == 'sql_patches') && ((getExtensionMode() == 'register') || (getExtensionMode() == 'remove')) && (!isExtensionDryRun()) && ($test)) {
419 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ': LOAD!');
420 if ($logout === true) {
421 // Then redirect to logout
422 redirectToUrl('modules.php?module=admin&logout=1&' . getExtensionMode() . '=sql_patches');
424 // Add temporary filter
425 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
426 $GLOBALS['ext_load_mode'] = getExtensionMode();
430 // Return status code
431 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',test=' . intval($test) . ' - EXIT!');
435 // Run SQL queries for given extension id
436 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
437 function doExtensionSqls ($ext_id, $load_mode) {
438 // This shall never do a non-admin user!
439 if (!isAdmin()) return false;
441 // Get extension's name
442 $ext_name = getExtensionName($ext_id);
444 // Set current SQL name
445 setCurrentExtensionName($ext_name);
447 // Init EXT_UPDATE_DEPENDS
448 if (!isExtensionUpdateDependenciesInitialized()) {
450 initExtensionUpdateDependencies();
454 initExtensionSqls(true);
456 // By default no SQL has been executed
459 // Load extension in detected mode
460 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName() . ',load_mode=' . $load_mode);
461 loadExtension(getCurrentExtensionName(), $load_mode, '0.0', false);
465 setSqlsArray(getExtensionSqls());
468 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls());
470 // Do we have entries?
472 // Run SQL commands...
473 runFilterChain('run_sqls');
476 // Run any filters depending on the action here
477 runFilterChain('extension_' . $load_mode);
479 // Remove cache file(s) if extension is active
480 if (((isExtensionActive('cache')) && ((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
482 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
485 // Is this the sql_patches?
486 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $ext_id . ',currName=' . getCurrentExtensionName() . ',loadMode=' . $load_mode);
487 if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
488 // Then redirect to logout
489 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ': LOAD!');
490 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
494 // Check wether the given extension is installed
495 function isExtensionInstalled ($ext_name) {
496 // We don't like empty extension names here
497 if (empty($ext_name)) {
498 // Please fix them all
499 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty.');
502 // By default non is installed
503 $isInstalled = false;
505 // Check if there is a cache entry
506 if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
507 // Use cache built from below queries
508 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
509 } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
514 incrementStatsEntry('cache_hits');
515 } elseif ((isInstallationPhase())) {
516 // Extensions are all inactive/not installed during installation
519 $ext_id = getExtensionId($ext_name);
522 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension ' . $ext_name . ' has ext_id=' . $ext_id);
524 // Do we have a record?
525 $isInstalled = ($ext_id > 0);
527 // Is it installed, then cache the entry
528 if ($isInstalled === true) {
529 // Dummy call (get is okay here)
530 getExtensionId($ext_name, true);
533 // Remember the status
534 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
541 // Check if given extension is active
542 function isExtensionActive ($ext_name) {
543 if (isInstallationPhase()) {
544 // Extensions are all inactive during installation
546 } elseif (empty($ext_name)) {
547 // Empty extension names must befixed
548 debug_report_bug(__FUNCTION__, __LINE__, 'Empty extension name provided.');
549 } elseif (!isExtensionInstalled($ext_name)) {
550 // Not installed extensions are always inactive
554 // Not active is the default
555 $data['ext_active'] = 'N';
558 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
560 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE! ext_name=' . $ext_name);
561 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
564 incrementStatsEntry('cache_hits');
565 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
566 // @TODO Extension is loaded, what next?
567 debug_report_bug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
568 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
569 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'DB! ext_name=' . $ext_name);
570 // Load from database
571 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
572 array($ext_name), __FUNCTION__, __LINE__);
575 if (SQL_NUMROWS($result) == 1) {
577 $data = SQL_FETCHARRAY($result);
581 SQL_FREERESULT($result);
584 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . '[DB]: ' . $data['ext_active']);
585 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
587 // Extension not active!
588 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ': Not active!');
589 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
593 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',active=' . $data['ext_active']);
595 // Is this extension activated? (For admins we always have active extensions...)
596 return ($data['ext_active'] == 'Y');
599 // Get version from extensions
600 function getExtensionVersion ($ext_name, $force = false) {
601 // By default no extension is found
602 $data['ext_version'] = 'false';
604 // Empty extension name should be fixed!
605 if (empty($ext_name)) {
606 // Please report this bug!
607 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
610 // Extensions are all inactive during installation
611 if (isInstallationPhase()) return '';
612 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
614 // Is the cache written?
615 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
616 // Load data from cache
617 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
618 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
621 incrementStatsEntry('cache_hits');
622 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (!isHtmlOutputMode())) {
623 // Load from database
624 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
625 array($ext_name), __FUNCTION__, __LINE__);
626 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
628 // Is the extension there?
629 if (SQL_NUMROWS($result) == 1) {
631 $data = SQL_FETCHARRAY($result);
632 } elseif (isDebugModeEnabled()) {
633 // Not found, please report all
634 debug_report_bug(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
638 SQL_FREERESULT($result);
641 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
644 // Extension version should not be invalid
645 if (($data['ext_version'] == 'false') && ($force === false)) {
646 // Please report this trouble
647 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
651 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_version=' . $data['ext_version']);
652 return $data['ext_version'];
655 // Updates a given extension with current extension version to latest version
656 function updateExtension ($ext_name, $ext_ver, $dry_run = false) {
657 // Only admins are allowed to update extensions
658 if ((!isAdmin()) || (empty($ext_name))) return false;
660 // Set current SQL name
661 setCurrentExtensionName($ext_name);
665 initExtensionNotes();
666 initIncludePool('extension');
668 // Load extension in test mode
669 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
671 // Save version history
672 $history = getExtensionVersionHistory();
674 // Remove old SQLs array to prevent possible bugs
677 // Check if version is updated
678 //* DEBUG: */ debugOutput(getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)));
679 if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
680 // Search for starting point
681 $start = array_search($ext_ver, $history);
683 // And load SQL queries in order of version history
684 for ($idx = ($start + 1); $idx < count($history); $idx++) {
685 // Set extension version
686 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
689 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
692 $depencies = getExtensionUpdateDependencies();
695 if (count($depencies) > 0) {
696 // Apply all extension depencies
697 foreach ($depencies as $ext_depend) {
698 // Did we already update/register this?
699 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
701 setCurrentExtensionName($ext_depend);
703 // Mark it as already updated before we update it
704 $GLOBALS['ext_updated'][$ext_depend] = true;
706 // Is the extension there?
707 if (isExtensionInstalled($ext_depend)) {
708 // Update another extension first!
709 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun());
711 // Register new extension
712 $test = registerExtension($ext_depend, 0, isExtensionDryRun(), false);
717 // Set extension version here
718 setCurrentExtensionVersion($ext_ver);
721 setCurrentExtensionName($ext_name);
725 addExtensionNotes($history[$idx]);
728 // In real-mode execute any existing includes
729 if (isExtensionDryRun() === false) {
730 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
731 runFilterChain('load_includes', 'extension');
732 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
733 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
738 setSqlsArray(getExtensionSqls());
741 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun(), 'enable_codes' => false));
743 if (isExtensionDryRun() === false) {
744 // Run filters on success extension update
745 runFilterChain('extension_update', getCurrentExtensionName());
750 // Output verbose SQL table for extension
751 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
754 // Then fix it to default
755 $title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}';
761 // Do we have queries?
762 if (isVerboseSqlEnabled()) {
763 // Do we have entries?
764 if (countExtensionSqls() > 0) {
768 foreach (getExtensionSqls() as $sqls) {
769 // New array format is recursive
770 foreach ($sqls as $sql) {
774 // Output command if set
776 // Prepare output for template
779 'sql' => str_replace('{', '{', str_replace('}', '}', encodeEntities($sql)))
783 $OUT .= loadTemplate('admin_extension_sql_row', true, $content);
791 // Prepare content for template
799 // Load main template
800 $OUT = loadTemplate('admin_extension_sql_table', true, $content);
802 // No addional SQL commands to run
803 $OUT = loadTemplate('admin_settings_saved', true, '{--ADMIN_NO_ADDITIONAL_SQLS--}');
811 // Get extension name from id
812 function getExtensionName ($ext_id) {
813 // Init extension name
814 $data['ext_name'] = '';
817 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
819 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
822 incrementStatsEntry('cache_hits');
823 } elseif (!isExtensionActive('cache')) {
824 // Load from database
825 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
826 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
828 // Is the entry there?
829 if (SQL_NUMROWS($result) == 1) {
830 // Get the extension's name from database
831 $data = SQL_FETCHARRAY($result);
835 SQL_FREERESULT($result);
838 // Did we find some extension?
839 if (empty($data['ext_name'])) {
840 // We should fix these all!
841 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
844 // Return the extension name
845 return $data['ext_name'];
848 // Get extension id from name
849 function getExtensionId ($ext_name) {
851 $data['ext_id'] = '0';
854 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
856 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
859 incrementStatsEntry('cache_hits');
861 // Load from database
862 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
863 array($ext_name), __FUNCTION__, __LINE__);
865 // Is the entry there?
866 if (SQL_NUMROWS($result) == 1) {
867 // Get the extension's id from database
868 $data = SQL_FETCHARRAY($result);
872 SQL_FREERESULT($result);
875 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
879 return $data['ext_id'];
882 // Determines wether the given extension name is valid
883 function isExtensionNameValid ($ext_name) {
885 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
886 // Generate include file name
887 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
889 // Is there a file in inc/extensions/ ?
890 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
894 return $GLOBALS['ext_name_valid'][$ext_name];
897 // Determines wether the given extension id is valid
898 function isExtensionIdValid ($ext_id) {
899 // Default is nothing valid
902 // Check in cache then in database
903 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
908 incrementStatsEntry('cache_hits');
911 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
918 // Activate given extension
919 function doActivateExtension ($ext_name) {
920 // Is the extension installed?
921 if (!isExtensionInstalled($ext_name)) {
922 // Non-installed extensions cannot be activated
923 debug_report_bug(__FUNCTION__, __LINE__, 'Tried to activate non-installed extension ' . $ext_name);
926 // Activate the extension
927 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
928 array($ext_name), __FUNCTION__, __LINE__);
930 // Then run all queries
931 doExtensionSqls(getExtensionId($ext_name), 'activate');
934 // Deactivate given extension
935 function doDeactivateExtension ($ext_name) {
936 // Is the extension installed?
937 if (!isExtensionInstalled($ext_name)) {
938 // Non-installed extensions cannot be activated
939 debug_report_bug(__FUNCTION__, __LINE__, 'Tried to deactivate non-installed extension ' . $ext_name);
942 // Activate the extension
943 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
944 array($ext_name), __FUNCTION__, __LINE__);
946 // Then run all queries
947 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
949 // Create new task (we ignore the task id here)
950 createExtensionDeactivationTask($ext_name);
953 sendAdminNotification(
954 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
955 'admin_extension_deactivated',
956 array('ext_name' => $ext_name)
960 // Checks wether the extension is older than given
961 function isExtensionOlder ($ext_name, $ext_ver) {
962 // Get current extension version
963 $currVersion = getExtensionVersion($ext_name);
965 // Remove all dots from both versions
966 $currVersion = str_replace('.', '', $currVersion);
967 $ext_ver = str_replace('.', '', $ext_ver);
969 // Now compare both and return the result
970 return ($currVersion < $ext_ver);
973 // Creates a new task for updated extension
974 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
975 // Create subject line
976 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
979 $taskId = determineTaskIdBySubject($subject);
981 // Is the extension there?
982 if (isExtensionInstalled($ext_name)) {
983 // Check if task is not there
984 if ($taskId == '0') {
985 // Create extension update-task
986 $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
989 // Extension not there! :-(
990 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
997 // Creates a new task for newly installed extension
998 function createNewExtensionTask ($ext_name) {
999 // Generate subject line
1000 $subject = sprintf("[%s:]", $ext_name);
1003 $taskId = determineTaskIdBySubject($subject);
1005 // Not installed and do we have created a task for the admin?
1006 if (($taskId == '0') && (!isExtensionInstalled($ext_name))) {
1007 // Set default message if ext-foo is missing
1008 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext_name);
1011 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
1017 // Load text for task if found
1018 if (isFileReadable($tpl)) {
1019 // Load extension's own text template (HTML!)
1020 $message = loadTemplate('ext_' . $ext_name, true);
1022 // Write this in debug.log as well
1023 logDebugMessage(__FUNCTION__, __LINE__, $message);
1026 // Task not created so it's a brand-new extension which we need to register and create a task for!
1027 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), false);
1034 // Creates a task for automatically deactivated (deprecated) extension
1035 function createExtensionDeactivationTask ($ext_name) {
1036 // Create subject line
1037 $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1040 $taskId = determineTaskIdBySubject($subject);
1042 // Not installed and do we have created a task for the admin?
1043 if (($taskId == '0') && (isExtensionInstalled($ext_name))) {
1044 // Task not created so add it
1045 $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext_name)), 'EXTENSION_DEACTIVATION');
1052 // Determines the task id for given extension
1053 function determineExtensionTaskId ($ext_name) {
1054 // Default is not found
1055 $data['task_id'] = '0';
1057 // Search for extension task's id
1058 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1059 array($ext_name), __FUNCTION__, __LINE__);
1062 if (SQL_NUMROWS($result) == 1) {
1063 // Task found so load task's id and register extension...
1064 $data = SQL_FETCHARRAY($result);
1068 SQL_FREERESULT($result);
1071 return $data['task_id'];
1074 // Determines the task id for given subject
1075 function determineTaskIdBySubject ($subject) {
1076 // Default is not found
1077 $data['task_id'] = '0';
1079 // Search for task id
1080 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1081 array($subject), __FUNCTION__, __LINE__);
1084 if (SQL_NUMROWS($result) == 1) {
1085 // Task found so load task's id and register extension...
1086 $data = SQL_FETCHARRAY($result);
1090 SQL_FREERESULT($result);
1093 return $data['task_id'];
1096 // Add updates notes for given version
1097 function addExtensionNotes ($ext_ver) {
1098 // Init notes/content
1102 // Is do we have verbose output enabled?
1103 if ((!isExtensionActive('sql_patches')) || (isVerboseSqlEnabled())) {
1104 // Update notes found?
1105 if (isExtensionUpdateNoteSet($ext_ver)) {
1106 // Update notes found
1109 'notes' => getExtensionUpdateNotes($ext_ver)
1113 setExtensionUpdateNotes('', $ext_ver);
1114 } elseif (in_array($ext_ver, array('0.0', '0.0.0'))) {
1118 'notes' => '{--INITIAL_RELEASE--}'
1121 // No update notes found!
1124 'notes' => '{--NO_UPDATE_NOTES--}'
1129 $out = loadTemplate('admin_extension_notes', true, $content);
1133 appendExtensionNotes($out);
1136 // Getter for CSS files array
1137 function getExtensionCssFiles () {
1138 // By default no additional CSS files are found
1139 $cssFiles = array();
1141 // Is the array there?
1142 if (isset($GLOBALS['css_files'])) {
1144 $cssFiles = $GLOBALS['css_files'];
1151 // Init CSS files array
1152 function initExtensionCssFiles () {
1154 $GLOBALS['css_files'] = array();
1158 function addExtensionCssFile ($file) {
1159 // Is the array there?
1160 if (!isset($GLOBALS['css_files'])) {
1161 // Then auto-init them
1162 initExtensionCssFiles();
1166 $GLOBALS['css_files'][] = $file;
1169 // Setter for EXT_ALWAYS_ACTIVE flag
1170 function setExtensionAlwaysActive ($active) {
1171 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1174 // Getter for EXT_ALWAYS_ACTIVE flag
1175 function getExtensionAlwaysActive () {
1176 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1179 // Checks wether the current extension is always active
1180 function isExtensionAlwaysActive () {
1181 return (getExtensionAlwaysActive() == 'Y');
1184 // Setter for EXT_VERSION flag
1185 function setThisExtensionVersion ($ext_version) {
1186 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1189 // Getter for EXT_VERSION flag
1190 function getThisExtensionVersion () {
1191 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1194 // Setter for EXT_DEPRECATED flag
1195 function setExtensionDeprecated ($deprecated) {
1196 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1199 // Getter for EXT_DEPRECATED flag
1200 function isExtensionDeprecated () {
1201 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1204 // Setter for EXT_UPDATE_DEPENDS flag
1205 function addExtensionUpdateDependency ($updateDepends) {
1206 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1207 // Is the update depency empty? (NEED TO BE FIXED!)
1208 if (empty($updateDepends)) {
1209 // Please report this bug!
1210 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1213 // Is it not yet added?
1214 /* Only for debugging!
1215 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1216 // Double-adding isn't fine, too
1217 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1221 // Add it to the list of extension update depencies map
1222 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1224 // Remember it in the list of running updates
1225 $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1226 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1229 // Getter for running updates
1230 function getExtensionUpdatesRunning () {
1231 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1234 // Checks wether the given extension registration is in progress
1235 function isExtensionRegistrationRunning ($ext_name) {
1237 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1240 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1244 // Init EXT_UPDATE_DEPENDS flag
1245 function initExtensionUpdateDependencies () {
1246 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1248 // Init update depency map automatically if not found
1249 if (isExtensionUpdateDependenciesInitialized()) {
1250 // We need these bug reports as well...
1251 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1254 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1256 // Init running updates array
1257 initExtensionRuningUpdates();
1259 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1262 // Adds an extension as "registration in progress"
1263 function addExtensionRunningRegistration ($ext_name) {
1265 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1266 if (isExtensionRegistrationRunning($ext_name)) {
1267 // This is really bad and should not be quietly ignored
1268 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1272 $GLOBALS['ext_register_running'][] = $ext_name;
1273 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1276 // Checks wether EXT_UPDATE_DEPENDS is initialized
1277 function isExtensionUpdateDependenciesInitialized () {
1278 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1279 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1282 // Checks wether an update is already running for given extension
1283 function isExtensionUpdateRunning ($ext_name) {
1284 // Current and given extensions means whole array
1285 if ($ext_name == getCurrentExtensionName()) {
1286 // Default is not found
1289 // Walk through whole array
1290 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1292 if (($ext1 == $ext_name) || ($isRunning === true)) {
1294 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1301 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1306 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1309 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1313 // Initializes the list of running updates
1314 function initExtensionRuningUpdates () {
1315 // Auto-init ext_running_updates
1316 if (!isset($GLOBALS['ext_running_updates'])) {
1317 $GLOBALS['ext_running_updates'] = array();
1318 $GLOBALS['ext_register_running'] = array();
1322 // Getter for EXT_UPDATE_DEPENDS flag
1323 function getExtensionUpdateDependencies () {
1324 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1325 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1328 // Getter for next iterator depency
1329 function getExtensionUpdateDependenciesIterator () {
1330 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1331 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1334 // Counter for extension update depencies
1335 function countExtensionUpdateDependencies () {
1336 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1337 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1340 // Removes given extension from update denpency list
1341 function removeExtensionUpdateDependency ($ext_name) {
1343 $key = array_search($ext_name, getExtensionUpdateDependencies());
1346 if ($key !== false) {
1348 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1350 // And sort the array
1351 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1355 // Init iterator for update depencies
1356 function initExtensionUpdateIterator () {
1357 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1358 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1361 // Getter for depency iterator
1362 function getExtensionUpdateIterator () {
1363 // Auto-init iterator
1364 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1367 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1368 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1371 // Increments the update iterator
1372 function incrementExtensionUpdateIterator () {
1373 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1374 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1377 // Setter for EXT_REPORTS_FAILURE flag
1378 function setExtensionReportsFailure ($reportsFailure) {
1379 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1382 // Getter for EXT_REPORTS_FAILURE flag
1383 function getExtensionReportsFailure () {
1384 return $GLOBALS['ext_reports_failure'];
1387 // Setter for EXT_VER_HISTORY flag
1388 function setExtensionVersionHistory ($versionHistory) {
1389 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1392 // Getter for EXT_VER_HISTORY array
1393 function getExtensionVersionHistory () {
1394 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1397 // Setter for EXT_UPDATE_NOTES
1398 function setExtensionUpdateNotes ($updateNotes, $ext_ver = '') {
1399 // . '/' . getCurrentExtensionVersion()
1400 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ',getExtensionMode()=' . getExtensionMode() . ',ext_ver=' . $ext_ver . ',updateNotes()=' . strlen($updateNotes));
1401 if (empty($ext_ver)) {
1402 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1404 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1408 // Getter for EXT_UPDATE_NOTES
1409 function getExtensionUpdateNotes ($ext_ver) {
1410 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1413 // Checks if ext_update_notes is set
1414 function isExtensionUpdateNoteSet ($ext_ver) {
1415 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
1418 // Init extension notice
1419 function initExtensionNotes ($force = false) {
1420 // Is it already initialized?
1421 if (($force === false) && (isset($GLOBALS['ext_notes'][getCurrentExtensionName()]))) {
1422 // This is mostly not wanted, so please report it
1423 debug_report_bug(__FUNCTION__, __LINE__, 'ext_notes already set for extension ' . getCurrentExtensionName());
1426 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1427 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1430 // Append extension notice
1431 function appendExtensionNotes ($notes) {
1432 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ', notes()=' . strlen($notes));
1433 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1436 // Getter for extension notes
1437 function getExtensionNotes () {
1438 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1441 // Setter for current extension name
1442 function setCurrentExtensionName ($ext_name) {
1443 $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1446 // Getter for current extension name
1447 function getCurrentExtensionName () {
1448 if (!isset($GLOBALS['curr_extension_name'])) {
1450 debug_report_bug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1454 return $GLOBALS['curr_extension_name'];
1457 // Init SQLs array for current extension
1458 function initExtensionSqls ($force = false) {
1459 // Auto-init the array or if forced
1460 if (($force === true) || (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()]))) {
1462 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1464 // Initialize the generic array
1469 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1470 function addExtensionSql ($sql) {
1472 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',ext_version=' . getCurrentExtensionVersion() . ',sql=' . $sql);
1473 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1476 // Getter for SQLs array for current extension
1477 function getExtensionSqls () {
1478 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1479 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1480 // Not found, should not happen
1481 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1482 getCurrentExtensionName()
1487 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1490 // Count SQLs for current extension
1491 function countExtensionSqls () {
1492 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1493 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1494 // Not found, should not happen
1495 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1496 getCurrentExtensionName()
1501 return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1504 // Removes SQLs for current extension
1505 function unsetExtensionSqls () {
1506 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1509 // Auto-initializes the removal list
1510 function initExtensionRemovalList () {
1511 // Is the remove list there?
1512 if (!isset($GLOBALS['ext_update_remove'])) {
1514 $GLOBALS['ext_update_remove'] = array();
1518 // Checks wether the current extension is on the removal list
1519 function isExtensionOnRemovalList () {
1520 // Init removal list
1521 initExtensionRemovalList();
1524 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1527 // Adds the current extension to the removal list
1528 function addCurrentExtensionToRemovalList () {
1530 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1533 // Getter for removal list
1534 function getExtensionRemovalList () {
1535 // Return the removal list
1536 return $GLOBALS['ext_update_remove'];
1539 // Redirects if the provided extension is not installed
1540 function redirectOnUninstalledExtension ($ext_name) {
1541 // So is the extension there?
1542 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1543 // Redirect to index
1544 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1548 // Filter for initialization of all extensions by loading them in 'init' mode
1549 function FILTER_INIT_EXTENSIONS () {
1550 // Do we have some entries?
1551 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY!');
1552 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1553 // Load all found extensions if found
1554 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
1555 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1557 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - START');
1558 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1559 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - END');
1561 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1563 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1566 // Setter for extension mode
1567 function setExtensionMode ($ext_mode) {
1568 $GLOBALS['ext_mode'] = (string) $ext_mode;
1571 // Getter for extension mode
1572 function getExtensionMode () {
1573 return $GLOBALS['ext_mode'];
1576 // Setter for dry-run
1577 function enableExtensionDryRun ($dry_run = true) {
1578 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()='.getCurrentExtensionName().',dry='.intval($dry_run));
1579 $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1582 // Getter for dry-run
1583 function isExtensionDryRun () {
1584 return $GLOBALS['ext_dry_run'];
1587 // Setter for current extension version
1588 function setCurrentExtensionVersion ($ext_ver) {
1589 // ext_ver should never be empty in other modes than 'test'
1590 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1591 // Please report all these messages
1592 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1596 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1597 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1600 // Getter for current extension version
1601 function getCurrentExtensionVersion () {
1602 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1603 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1606 // Remove the extension from cache array
1607 function removeExtensionFromArray () {
1608 // "Cache" this name
1609 $ext_name = getCurrentExtensionName();
1611 // Now loop through the whole cache
1612 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1613 // Is it an element?
1614 if (isset($cacheArray[$ext_name])) {
1616 unset($cacheArray[$ext_name]);
1617 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1620 $key = array_search($ext_name, $cacheArray);
1623 if ($key !== false) {
1624 // Found, so remove it
1625 unset($cacheArray[$key]);
1626 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1632 // "Getter" for 'extension has a CSS file' (with same name, of course)
1633 function getExtensionHasCss () {
1634 // Default is no CSS
1637 // Construct FQFN for check
1638 $FQFN = sprintf("%stheme/%s/css/%s.css",
1641 getCurrentExtensionName()
1645 if (isFileReadable($FQFN)) {
1646 // Readable, so it is there...
1654 // Checks wether the given extension has a language file
1655 function ifExtensionHasLanguageFile ($ext_name) {
1656 // Default is no language file
1657 $hasLanguage = false;
1659 // Do we have cache?
1660 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1662 $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1665 incrementStatsEntry('cache_hits');
1667 // Not readable is default
1670 // Is the language file readable for this extension?
1671 if (isLanguageIncludeReadable($ext_name)) {
1677 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = $readable;
1681 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1684 // Load an extension's include file
1685 function loadExtensionInclude () {
1687 if (!isExtensionIncludeReadable()) {
1689 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1692 // Generate INC name
1693 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1699 // Checks wether an extension is readable
1700 function isExtensionIncludeReadable ($ext_name = '') {
1701 // If empty, use current
1702 if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1705 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1706 // Generate INC name
1707 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1710 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1714 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1715 return $GLOBALS['ext_inc_readable'][$ext_name];
1718 // Checks if an extension's function file is readable
1719 function isExtensionFunctionFileReadable ($ext_name) {
1721 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
1722 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1723 // Just count cache hits
1724 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] .' - CACHE!');
1725 incrementStatsEntry('cache_hits');
1727 // Construct IFN for functions file
1728 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1730 // Is this include there?
1731 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1733 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=Y - FOUND!');
1734 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1737 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=N - NOT FOUND!');
1738 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1743 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name]);
1744 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1747 // Adds an admin menu to the SQL queue of the menu entry is not found
1748 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1749 // Now check if this menu is there
1750 if (!isMenuActionValid('admin', $action, $what)) {
1752 if (is_null($what)) {
1754 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1762 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1771 // Add it to the queue
1772 addExtensionSql($sql);
1773 } elseif (isDebugModeEnabled()) {
1774 // Double menus should be located and fixed!
1775 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
1779 // Adds a guest menu to the SQL queue if the menu entry is not found
1780 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1781 // Now check if this menu is there
1782 if (!isMenuActionValid('guest', $action, $what)) {
1784 if (is_null($what)) {
1786 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1795 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1805 // Add it to the queue
1806 addExtensionSql($sql);
1807 } elseif (isDebugModeEnabled()) {
1808 // Double menus should be located and fixed!
1809 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s,what=%s,title=%s,active=%s detected.", $action, $what, $title, $active));
1813 // Adds a member menu to the SQL queue if the menu entry is not found
1814 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1815 // Now check if this menu is there
1816 if (!isMenuActionValid('member', $action, $what)) {
1818 if (is_null($what)) {
1820 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1829 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1839 // Add it to the queue
1840 addExtensionSql($sql);
1841 } elseif (isDebugModeEnabled()) {
1842 // Double menus should be located and fixed!
1843 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s,what=%s,title=%s,visivle=%s,locked=%s detected.", $action, $what, $title, $visible, $locked));
1847 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1848 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1849 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1850 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1852 if (is_null($what)) {
1854 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1862 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1871 // Add it to the queue
1872 addExtensionSql($sql);
1873 } elseif (isDebugModeEnabled()) {
1874 // Double menus should be located and fixed!
1875 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s,what=%s,title=%s,active=%s detected.", $action, $what, $title, $active));
1879 // Enables/disables productive mode for current extension (used only while
1881 function enableExtensionProductive ($isProductive = true) {
1882 // Log debug message
1883 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%d", getCurrentExtensionName(), intval($isProductive)));
1886 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1889 // Checks wether the extension is in productive phase. If not set, development
1890 // phase (=false) is assumed.
1891 function isExtensionProductive ($ext_name = '') {
1892 // Is the extension name empty? Then use current
1893 if (empty($ext_name)) {
1894 // Get current extension name
1895 $ext_name = getCurrentExtensionName();
1898 // Do we have cache?
1899 if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
1900 // Load extension only if not yet loaded
1901 if (!isset($GLOBALS['ext_productive'][$ext_name])) {
1902 // Load extension in test mode
1903 loadExtension($ext_name, 'test');
1907 $GLOBALS[__FUNCTION__][$ext_name] = ((isset($GLOBALS['ext_productive'][$ext_name])) && ($GLOBALS['ext_productive'][$ext_name] === true));
1910 // Log debug message
1911 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%s", $ext_name, intval($GLOBALS[__FUNCTION__][$ext_name])));
1914 return $GLOBALS[__FUNCTION__][$ext_name];