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 = 'init', $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()) && (getOutputMode() == '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__.': 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 // Extensions are all inactive during installation
526 if ((isInstallationPhase()) || (empty($ext_name))) return false;
528 // Not active is the default
529 $data['ext_active'] = 'N';
532 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
534 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE! ext_name=' . $ext_name);
535 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
538 incrementStatsEntry('cache_hits');
539 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
540 // @TODO Extension is loaded, what next?
541 debug_report_bug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
542 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
543 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'DB! ext_name=' . $ext_name);
544 // Load from database
545 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
546 array($ext_name), __FUNCTION__, __LINE__);
549 if (SQL_NUMROWS($result) == 1) {
551 $data = SQL_FETCHARRAY($result);
555 SQL_FREERESULT($result);
558 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . '[DB]: ' . $data['ext_active']);
559 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
561 // Extension not active!
562 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ': Not active!');
563 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
567 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',active=' . $data['ext_active']);
569 // Is this extension activated? (For admins we always have active extensions...)
570 return ($data['ext_active'] == 'Y');
573 // Get version from extensions
574 function getExtensionVersion ($ext_name, $force = false) {
575 // By default no extension is found
576 $data['ext_version'] = 'false';
578 // Empty extension name should be fixed!
579 if (empty($ext_name)) {
580 // Please report this bug!
581 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
584 // Extensions are all inactive during installation
585 if (isInstallationPhase()) return '';
586 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
588 // Is the cache written?
589 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
590 // Load data from cache
591 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
592 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
595 incrementStatsEntry('cache_hits');
596 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (getOutputMode() != 0)) {
597 // Load from database
598 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
599 array($ext_name), __FUNCTION__, __LINE__);
600 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
602 // Is the extension there?
603 if (SQL_NUMROWS($result) == 1) {
605 $data = SQL_FETCHARRAY($result);
606 } elseif (isDebugModeEnabled()) {
607 // Not found, please report all
608 debug_report_bug(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
612 SQL_FREERESULT($result);
615 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
618 // Extension version should not be invalid
619 if (($data['ext_version'] == 'false') && ($force === false)) {
620 // Please report this trouble
621 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
625 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_version=' . $data['ext_version']);
626 return $data['ext_version'];
629 // Updates a given extension with current extension version to latest version
630 function updateExtension ($ext_name, $ext_ver, $dry_run = false) {
631 // Only admins are allowed to update extensions
632 if ((!isAdmin()) || (empty($ext_name))) return false;
634 // Set current SQL name
635 setCurrentExtensionName($ext_name);
639 initExtensionNotes();
640 initIncludePool('extension');
642 // Load extension in test mode
643 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
645 // Save version history
646 $history = getExtensionVersionHistory();
648 // Remove old SQLs array to prevent possible bugs
651 // Check if version is updated
652 //* DEBUG: */ debugOutput(getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)));
653 if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
654 // Search for starting point
655 $start = array_search($ext_ver, $history);
657 // And load SQL queries in order of version history
658 for ($idx = ($start + 1); $idx < count($history); $idx++) {
659 // Set extension version
660 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
663 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
666 $depencies = getExtensionUpdateDependencies();
669 if (count($depencies) > 0) {
670 // Apply all extension depencies
671 foreach ($depencies as $ext_depend) {
672 // Did we already update/register this?
673 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
675 setCurrentExtensionName($ext_depend);
677 // Mark it as already updated before we update it
678 $GLOBALS['ext_updated'][$ext_depend] = true;
680 // Is the extension there?
681 if (isExtensionInstalled($ext_depend)) {
682 // Update another extension first!
683 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun());
685 // Register new extension
686 $test = registerExtension($ext_depend, 0, isExtensionDryRun(), false);
691 // Set extension version here
692 setCurrentExtensionVersion($ext_ver);
695 setCurrentExtensionName($ext_name);
699 addExtensionNotes($history[$idx]);
702 // In real-mode execute any existing includes
703 if (isExtensionDryRun() === false) {
704 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
705 runFilterChain('load_includes', 'extension');
706 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
707 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
712 setSqlsArray(getExtensionSqls());
715 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun()));
717 if (isExtensionDryRun() === false) {
718 // Run filters on success extension update
719 runFilterChain('extension_update', getCurrentExtensionName());
724 // Output verbose SQL table for extension
725 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
728 // Then fix it to default
729 $title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}';
735 // Do we have queries?
736 if ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
737 // Do we have entries?
738 if (countExtensionSqls() > 0) {
742 foreach (getExtensionSqls() as $sqls) {
743 // New array format is recursive
744 foreach ($sqls as $sql) {
748 // Output command if set
750 // Prepare output for template
757 $OUT .= loadTemplate('admin_extension_sql_row', true, $content);
765 // Prepare content for template
773 // Load main template
774 $OUT = loadTemplate('admin_extension_sql_table', true, $content);
776 // No addional SQL commands to run
777 $OUT = loadTemplate('admin_settings_saved', true, '{--ADMIN_NO_ADDITIONAL_SQLS--}');
785 // Get extension name from id
786 function getExtensionName ($ext_id) {
787 // Init extension name
788 $data['ext_name'] = '';
791 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
793 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
796 incrementStatsEntry('cache_hits');
797 } elseif (!isExtensionActive('cache')) {
798 // Load from database
799 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
800 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
802 // Is the entry there?
803 if (SQL_NUMROWS($result) == 1) {
804 // Get the extension's name from database
805 $data = SQL_FETCHARRAY($result);
809 SQL_FREERESULT($result);
812 // Did we find some extension?
813 if (empty($data['ext_name'])) {
814 // We should fix these all!
815 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
818 // Return the extension name
819 return $data['ext_name'];
822 // Get extension id from name
823 function getExtensionId ($ext_name) {
825 $data['ext_id'] = '0';
828 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
830 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
833 incrementStatsEntry('cache_hits');
835 // Load from database
836 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
837 array($ext_name), __FUNCTION__, __LINE__);
839 // Is the entry there?
840 if (SQL_NUMROWS($result) == 1) {
841 // Get the extension's id from database
842 $data = SQL_FETCHARRAY($result);
846 SQL_FREERESULT($result);
849 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
853 return $data['ext_id'];
856 // Determines wether the given extension name is valid
857 function isExtensionNameValid ($ext_name) {
859 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
860 // Generate include file name
861 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
863 // Is there a file in inc/extensions/ ?
864 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
868 return $GLOBALS['ext_name_valid'][$ext_name];
871 // Determines wether the given extension id is valid
872 function isExtensionIdValid ($ext_id) {
873 // Default is nothing valid
876 // Check in cache then in database
877 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
882 incrementStatsEntry('cache_hits');
885 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
892 // Activate given extension
893 function doActivateExtension ($ext_name) {
894 // Activate the extension
895 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
896 array($ext_name), __FUNCTION__, __LINE__);
898 // Extension has been activated?
899 if (SQL_AFFECTEDROWS() == 1) {
900 // Then run all queries
901 doExtensionSqls(getExtensionId($ext_name), 'activate');
905 // Deactivate given extension
906 function doDeactivateExtension($ext_name) {
907 // Activate the extension
908 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
909 array($ext_name), __FUNCTION__, __LINE__);
911 // Extension has been activated?
912 if (SQL_AFFECTEDROWS() == 1) {
913 // Then run all queries
914 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
916 // Create new task (we ignore the task id here)
917 createExtensionDeactivationTask($ext_name);
920 sendAdminNotification(
921 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
922 'admin_extension_deactivated',
923 array('ext_name' => $ext_name)
928 // Checks wether the extension is older than given
929 function isExtensionOlder ($ext_name, $ext_ver) {
930 // Get current extension version
931 $currVersion = getExtensionVersion($ext_name);
933 // Remove all dots from both versions
934 $currVersion = str_replace('.', '', $currVersion);
935 $ext_ver = str_replace('.', '', $ext_ver);
937 // Now compare both and return the result
938 return ($currVersion < $ext_ver);
941 // Creates a new task for updated extension
942 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
943 // Create subject line
944 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
947 $taskId = determineTaskIdBySubject($subject);
949 // Is the extension there?
950 if (isExtensionInstalled($ext_name)) {
951 // Check if task is not there
952 if ($taskId == '0') {
953 // Create extension update-task
954 $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
957 // Extension not there! :-(
958 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
965 // Creates a new task for newly installed extension
966 function createNewExtensionTask ($ext_name) {
967 // Generate subject line
968 $subject = sprintf("[%s:]", $ext_name);
971 $taskId = determineTaskIdBySubject($subject);
973 // Not installed and do we have created a task for the admin?
974 if (($taskId == '0') && (!isExtensionInstalled($ext_name))) {
975 // Set default message if ext-foo is missing
976 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext_name);
979 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
985 // Load text for task if found
986 if (isFileReadable($tpl)) {
987 // Load extension's own text template (HTML!)
988 $message = loadTemplate('ext_' . $ext_name, true);
990 // Write this in debug.log as well
991 logDebugMessage(__FUNCTION__, __LINE__, $message);
994 // Task not created so it's a brand-new extension which we need to register and create a task for!
995 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), false);
1002 // Creates a task for automatically deactivated (deprecated) extension
1003 function createExtensionDeactivationTask ($ext_name) {
1004 // Create subject line
1005 $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1008 $taskId = determineTaskIdBySubject($subject);
1010 // Not installed and do we have created a task for the admin?
1011 if (($taskId == '0') && (isExtensionInstalled($ext_name))) {
1012 // Task not created so add it
1013 $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext_name)), 'EXTENSION_DEACTIVATION');
1020 // Checks if the module has a menu
1021 function ifModuleHasMenu ($mod, $forceDb = false) {
1022 // All is false by default
1025 // Extension installed and newer than or has version 0.1.2?
1026 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
1027 // Cache version is okay, so let's check the cache!
1028 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
1029 // Check module cache and count hit
1030 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
1031 incrementStatsEntry('cache_hits');
1032 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$mod])) {
1033 // Check cache and count hit
1034 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
1035 incrementStatsEntry('cache_hits');
1037 // Admin/guest/member/sponsor modules have always a menu!
1038 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
1040 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
1041 // Check database for entry
1042 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
1043 array($mod), __FUNCTION__, __LINE__);
1046 if (SQL_NUMROWS($result) == 1) {
1047 // Load "has_menu" column
1048 $data = SQL_FETCHARRAY($result);
1050 // Fake cache... ;-)
1051 $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
1053 // Does it have a menu?
1054 $ret = ($data['has_menu'] == 'Y');
1058 SQL_FREERESULT($result);
1059 } elseif (!isExtensionInstalled('sql_patches')) {
1060 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
1061 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
1063 // Unsupported state!
1064 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
1071 // Determines the task id for given extension
1072 function determineExtensionTaskId ($ext_name) {
1073 // Default is not found
1074 $data['task_id'] = '0';
1076 // Search for extension task's id
1077 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1078 array($ext_name), __FUNCTION__, __LINE__);
1081 if (SQL_NUMROWS($result) == 1) {
1082 // Task found so load task's id and register extension...
1083 $data = SQL_FETCHARRAY($result);
1087 SQL_FREERESULT($result);
1090 return $data['task_id'];
1093 // Determines the task id for given subject
1094 function determineTaskIdBySubject ($subject) {
1095 // Default is not found
1096 $data['task_id'] = '0';
1098 // Search for task id
1099 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1100 array($subject), __FUNCTION__, __LINE__);
1103 if (SQL_NUMROWS($result) == 1) {
1104 // Task found so load task's id and register extension...
1105 $data = SQL_FETCHARRAY($result);
1109 SQL_FREERESULT($result);
1112 return $data['task_id'];
1115 // Add updates notes for given version
1116 function addExtensionNotes ($ext_ver) {
1117 // Init notes/content
1121 // Is do we have verbose output enabled?
1122 if ((!isExtensionActive('sql_patches')) || (getConfig('verbose_sql') == 'Y')) {
1123 // Update notes found?
1124 if (isExtensionUpdateNoteSet($ext_ver)) {
1125 // Update notes found
1128 'notes' => getExtensionUpdateNotes($ext_ver)
1132 setExtensionUpdateNotes('', $ext_ver);
1133 } elseif (in_array($ext_ver, array('0.0', '0.0.0'))) {
1137 'notes' => '{--INITIAL_RELEASE--}'
1140 // No update notes found!
1143 'notes' => '{--NO_UPDATE_NOTES--}'
1148 $out = loadTemplate('admin_extension_notes', true, $content);
1152 appendExtensionNotes($out);
1155 // Getter for CSS files array
1156 function getExtensionCssFiles () {
1157 // By default no additional CSS files are found
1158 $cssFiles = array();
1160 // Is the array there?
1161 if (isset($GLOBALS['css_files'])) {
1163 $cssFiles = $GLOBALS['css_files'];
1170 // Init CSS files array
1171 function initExtensionCssFiles () {
1173 $GLOBALS['css_files'] = array();
1177 function addExtensionCssFile ($file) {
1178 // Is the array there?
1179 if (!isset($GLOBALS['css_files'])) {
1180 // Then auto-init them
1181 initExtensionCssFiles();
1185 $GLOBALS['css_files'][] = $file;
1188 // Setter for EXT_ALWAYS_ACTIVE flag
1189 function setExtensionAlwaysActive ($active) {
1190 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1193 // Getter for EXT_ALWAYS_ACTIVE flag
1194 function getExtensionAlwaysActive () {
1195 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1198 // Checks wether the current extension is always active
1199 function isExtensionAlwaysActive () {
1200 return (getExtensionAlwaysActive() == 'Y');
1203 // Setter for EXT_VERSION flag
1204 function setThisExtensionVersion ($ext_version) {
1205 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1208 // Getter for EXT_VERSION flag
1209 function getThisExtensionVersion () {
1210 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1213 // Setter for EXT_DEPRECATED flag
1214 function setExtensionDeprecated ($deprecated) {
1215 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1218 // Getter for EXT_DEPRECATED flag
1219 function isExtensionDeprecated () {
1220 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1223 // Setter for EXT_UPDATE_DEPENDS flag
1224 function addExtensionUpdateDependency ($updateDepends) {
1225 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1226 // Is the update depency empty? (NEED TO BE FIXED!)
1227 if (empty($updateDepends)) {
1228 // Please report this bug!
1229 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1232 // Is it not yet added?
1233 /* Only for debugging!
1234 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1235 // Double-adding isn't fine, too
1236 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1240 // Add it to the list of extension update depencies map
1241 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1243 // Remember it in the list of running updates
1244 $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1245 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1248 // Getter for running updates
1249 function getExtensionUpdatesRunning () {
1250 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1253 // Checks wether the given extension registration is in progress
1254 function isExtensionRegistrationRunning ($ext_name) {
1256 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1259 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1263 // Init EXT_UPDATE_DEPENDS flag
1264 function initExtensionUpdateDependencies () {
1265 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1267 // Init update depency map automatically if not found
1268 if (isExtensionUpdateDependenciesInitialized()) {
1269 // We need these bug reports as well...
1270 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1273 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1275 // Init running updates array
1276 initExtensionRuningUpdates();
1278 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1281 // Adds an extension as "registration in progress"
1282 function addExtensionRunningRegistration ($ext_name) {
1284 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1285 if (isExtensionRegistrationRunning($ext_name)) {
1286 // This is really bad and should not be quietly ignored
1287 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1291 $GLOBALS['ext_register_running'][] = $ext_name;
1292 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1295 // Checks wether EXT_UPDATE_DEPENDS is initialized
1296 function isExtensionUpdateDependenciesInitialized () {
1297 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1298 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1301 // Checks wether an update is already running for given extension
1302 function isExtensionUpdateRunning ($ext_name) {
1303 // Current and given extensions means whole array
1304 if ($ext_name == getCurrentExtensionName()) {
1305 // Default is not found
1308 // Walk through whole array
1309 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1311 if (($ext1 == $ext_name) || ($isRunning === true)) {
1313 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1320 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1325 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1328 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1332 // Initializes the list of running updates
1333 function initExtensionRuningUpdates () {
1334 // Auto-init ext_running_updates
1335 if (!isset($GLOBALS['ext_running_updates'])) {
1336 $GLOBALS['ext_running_updates'] = array();
1337 $GLOBALS['ext_register_running'] = array();
1341 // Getter for EXT_UPDATE_DEPENDS flag
1342 function getExtensionUpdateDependencies () {
1343 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1344 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1347 // Getter for next iterator depency
1348 function getExtensionUpdateDependenciesIterator () {
1349 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1350 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1353 // Counter for extension update depencies
1354 function countExtensionUpdateDependencies () {
1355 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1356 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1359 // Removes given extension from update denpency list
1360 function removeExtensionUpdateDependency ($ext_name) {
1362 $key = array_search($ext_name, getExtensionUpdateDependencies());
1365 if ($key !== false) {
1367 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1369 // And sort the array
1370 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1374 // Init iterator for update depencies
1375 function initExtensionUpdateIterator () {
1376 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1377 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1380 // Getter for depency iterator
1381 function getExtensionUpdateIterator () {
1382 // Auto-init iterator
1383 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1386 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1387 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1390 // Increments the update iterator
1391 function incrementExtensionUpdateIterator () {
1392 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1393 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1396 // Setter for EXT_REPORTS_FAILURE flag
1397 function setExtensionReportsFailure ($reportsFailure) {
1398 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1401 // Getter for EXT_REPORTS_FAILURE flag
1402 function getExtensionReportsFailure () {
1403 return $GLOBALS['ext_reports_failure'];
1406 // Setter for EXT_VER_HISTORY flag
1407 function setExtensionVersionHistory ($versionHistory) {
1408 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1411 // Getter for EXT_VER_HISTORY array
1412 function getExtensionVersionHistory () {
1413 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1416 // Setter for EXT_UPDATE_NOTES
1417 function setExtensionUpdateNotes ($updateNotes, $ext_ver = '') {
1418 // . '/' . getCurrentExtensionVersion()
1419 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ',getExtensionMode()=' . getExtensionMode() . ',ext_ver=' . $ext_ver . ',updateNotes()=' . strlen($updateNotes));
1420 if (empty($ext_ver)) {
1421 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1423 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1427 // Getter for EXT_UPDATE_NOTES
1428 function getExtensionUpdateNotes ($ext_ver) {
1429 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1432 // Checks if ext_update_notes is set
1433 function isExtensionUpdateNoteSet ($ext_ver) {
1434 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
1437 // Init extension notice
1438 function initExtensionNotes ($force = false) {
1439 // Is it already initialized?
1440 if (($force === false) && (isset($GLOBALS['ext_notes'][getCurrentExtensionName()]))) {
1441 // This is mostly not wanted, so please report it
1442 debug_report_bug(__FUNCTION__, __LINE__, 'ext_notes already set for extension ' . getCurrentExtensionName());
1445 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1446 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1449 // Append extension notice
1450 function appendExtensionNotes ($notes) {
1451 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ', notes()=' . strlen($notes));
1452 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1455 // Getter for extension notes
1456 function getExtensionNotes () {
1457 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1460 // Setter for current extension name
1461 function setCurrentExtensionName ($ext_name) {
1462 $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1465 // Getter for current extension name
1466 function getCurrentExtensionName () {
1467 if (!isset($GLOBALS['curr_extension_name'])) {
1469 debug_report_bug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1473 return $GLOBALS['curr_extension_name'];
1476 // Init SQLs array for current extension
1477 function initExtensionSqls ($force = false) {
1478 // Auto-init the array or if forced
1479 if (($force === true) || (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()]))) {
1481 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1483 // Initialize the generic array
1488 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1489 function addExtensionSql ($sql) {
1491 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1494 // Getter for SQLs array for current extension
1495 function getExtensionSqls () {
1496 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1497 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1498 // Not found, should not happen
1499 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1500 getCurrentExtensionName()
1505 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1508 // Count SQLs for current extension
1509 function countExtensionSqls () {
1510 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1511 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1512 // Not found, should not happen
1513 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1514 getCurrentExtensionName()
1519 return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1522 // Removes SQLs for current extension
1523 function unsetExtensionSqls () {
1524 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1527 // Auto-initializes the removal list
1528 function initExtensionRemovalList () {
1529 // Is the remove list there?
1530 if (!isset($GLOBALS['ext_update_remove'])) {
1532 $GLOBALS['ext_update_remove'] = array();
1536 // Checks wether the current extension is on the removal list
1537 function isExtensionOnRemovalList () {
1538 // Init removal list
1539 initExtensionRemovalList();
1542 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1545 // Adds the current extension to the removal list
1546 function addCurrentExtensionToRemovalList () {
1548 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1551 // Getter for removal list
1552 function getExtensionRemovalList () {
1553 // Return the removal list
1554 return $GLOBALS['ext_update_remove'];
1557 // Redirects if the provided extension is not installed
1558 function redirectOnUninstalledExtension ($ext_name) {
1559 // So is the extension there?
1560 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1561 // Redirect to index
1562 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1566 // Filter for initialization of all extensions by loading them in 'init' mode
1567 function FILTER_INIT_EXTENSIONS () {
1568 // Do we have some entries?
1569 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY!');
1570 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1571 // Load all found extensions if found
1572 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
1573 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1575 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - START');
1576 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1577 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - END');
1579 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1581 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1584 // Setter for extension mode
1585 function setExtensionMode ($ext_mode) {
1586 $GLOBALS['ext_mode'] = (string) $ext_mode;
1589 // Getter for extension mode
1590 function getExtensionMode () {
1591 return $GLOBALS['ext_mode'];
1594 // Setter for dry-run
1595 function enableExtensionDryRun ($dry_run = true) {
1596 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()='.getCurrentExtensionName().',dry='.intval($dry_run));
1597 $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1600 // Getter for dry-run
1601 function isExtensionDryRun () {
1602 return $GLOBALS['ext_dry_run'];
1605 // Setter for current extension version
1606 function setCurrentExtensionVersion ($ext_ver) {
1607 // ext_ver should never be empty in other modes than 'test'
1608 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1609 // Please report all these messages
1610 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1614 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1615 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1618 // Getter for current extension version
1619 function getCurrentExtensionVersion () {
1620 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1621 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1624 // Remove the extension from cache array
1625 function removeExtensionFromArray () {
1626 // "Cache" this name
1627 $ext_name = getCurrentExtensionName();
1629 // Now loop through the whole cache
1630 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1631 // Is it an element?
1632 if (isset($cacheArray[$ext_name])) {
1634 unset($cacheArray[$ext_name]);
1635 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1638 $key = array_search($ext_name, $cacheArray);
1641 if ($key !== false) {
1642 // Found, so remove it
1643 unset($cacheArray[$key]);
1644 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1650 // "Getter" for 'extension has a CSS file' (with same name, of course)
1651 function getExtensionHasCss () {
1652 // Default is no CSS
1655 // Construct FQFN for check
1656 $FQFN = sprintf("%stheme/%s/css/%s.css",
1659 getCurrentExtensionName()
1663 if (isFileReadable($FQFN)) {
1664 // Readable, so it is there...
1672 // Checks wether the given extension has a language file
1673 function ifExtensionHasLanguageFile ($ext_name) {
1674 // Default is no language file
1675 $hasLanguage = false;
1677 // Do we have cache?
1678 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1680 $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1683 incrementStatsEntry('cache_hits');
1685 // Not readable is default
1688 // Is the language file readable for this extension?
1689 if (isLanguageIncludeReadable($ext_name)) {
1695 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = 'Y';
1699 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1702 // Load an extension's include file
1703 function loadExtensionInclude () {
1705 if (!isExtensionIncludeReadable()) {
1707 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1710 // Generate INC name
1711 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1717 // Checks wether an extension is readable
1718 function isExtensionIncludeReadable ($ext_name = '') {
1719 // If empty, use current
1720 if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1723 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1724 // Generate INC name
1725 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1728 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1732 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1733 return $GLOBALS['ext_inc_readable'][$ext_name];
1736 // Checks if an extension's function file is readable
1737 function isExtensionFunctionFileReadable ($ext_name) {
1739 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
1740 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1741 // Just count cache hits
1742 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] .' - CACHE!');
1743 incrementStatsEntry('cache_hits');
1745 // Construct IFN for functions file
1746 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1748 // Is this include there?
1749 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1751 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=Y - FOUND!');
1752 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1755 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=N - NOT FOUND!');
1756 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1761 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name]);
1762 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1765 // Adds an admin menu to the SQL queue of the menu entry is not found
1766 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1767 // Now check if this menu is there
1768 if (!isMenuActionValid('admin', $action, $what)) {
1770 if (is_null($what)) {
1772 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1780 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1789 // Add it to the queue
1790 addExtensionSql($sql);
1791 } elseif (isDebugModeEnabled()) {
1792 // Double menus should be located and fixed!
1793 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s, what=%s detected.", $action, $what));
1797 // Adds a guest menu to the SQL queue if the menu entry is not found
1798 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1799 // Now check if this menu is there
1800 if (!isMenuActionValid('guest', $action, $what)) {
1802 if (is_null($what)) {
1804 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1813 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1823 // Add it to the queue
1824 addExtensionSql($sql);
1825 } elseif (isDebugModeEnabled()) {
1826 // Double menus should be located and fixed!
1827 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s, what=%s detected.", $action, $what));
1831 // Adds a member menu to the SQL queue if the menu entry is not found
1832 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1833 // Now check if this menu is there
1834 if (!isMenuActionValid('member', $action, $what)) {
1836 if (is_null($what)) {
1838 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1847 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1857 // Add it to the queue
1858 addExtensionSql($sql);
1859 } elseif (isDebugModeEnabled()) {
1860 // Double menus should be located and fixed!
1861 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s, what=%s detected.", $action, $what));
1865 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1866 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1867 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1868 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1870 if (is_null($what)) {
1872 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1880 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1889 // Add it to the queue
1890 addExtensionSql($sql);
1891 } elseif (isDebugModeEnabled()) {
1892 // Double menus should be located and fixed!
1893 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s, what=%s detected.", $action, $what));
1897 // Enables/disables productive mode for current extension (used only while
1899 function enableExtensionProductive ($isProductive = true) {
1900 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1903 // Checks wether the extension is in productive phase. If not set, development
1904 // phase (=false) is assumed.
1905 function isExtensionProductive () {
1906 return ((isset($GLOBALS['ext_productive'][getCurrentExtensionName()])) && ($GLOBALS['ext_productive'][getCurrentExtensionName()] === true));