Renamed function so it might be more understandable
[mailer.git] / inc / extensions-functions.php
index e755418accd30e5ac705a8406b972a48675ba7a2..fa35d12e2b4a9f40bc7299f99f502092a161ef7c 100644 (file)
@@ -14,8 +14,6 @@
  * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
  * $Author::                                                          $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
@@ -43,9 +41,11 @@ if (!defined('__SECURITY')) {
 } // END - if
 
 // Load the extension and maybe found language and function files.
-function loadExtension ($ext_name, $ext_mode, $ext_ver = '', $dry_run = false) {
-       // If this happens twice, we need the bug report from you, except for updates/tests
+function loadExtension ($ext_name, $ext_mode, $ext_ver = '0.0', $dry_run = false) {
+       // Loading an extension in same mode, but not test/update, twice is not
+       // good, so is the extension $ext_name already loaded in mode $ext_mode?
        if ((isset($GLOBALS['loaded_extension'][$ext_name][$ext_mode])) && (!in_array($ext_mode, array('update', 'test')))) {
+               // If this happens twice, we need the bug report from you, except for updates/tests
                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));
        } // END - if
 
@@ -58,17 +58,26 @@ function loadExtension ($ext_name, $ext_mode, $ext_ver = '', $dry_run = false) {
        // Set current extension name
        setCurrentExtensionName($ext_name);
 
+       // By default all extensions are in productive phase
+       enableExtensionProductive();
+
        if (!empty($ext_ver)) {
                // Set current extension version
                setCurrentExtensionVersion($ext_ver);
        } else {
-               // Set it to 0.0
+               // Set it to 0.0 by default
                setCurrentExtensionVersion('0.0');
 
+               // Is the extension installed?
+               if ((isExtensionInstalled($ext_name)) && ($ext_mode != 'register')) {
+                       // Get extension's version
+                       setCurrentExtensionVersion(getExtensionVersion($ext_name));
+               } // END - if
+
                // In all but test-mode we need these messages to debug! Please report all (together, e.g.)
-               if ($ext_mode != 'test') {
-                       // Log empty versions not in test-mode, but maybe it is fine...
-                       logDebugMessage(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0. ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', dry_run=' . intval($dry_run));
+               if (($ext_mode != 'test') && (getCurrentExtensionVersion() == '0.0')) {
+                       // Abort here, this must now always be set!
+                       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));
                } // END - if
        }
 
@@ -125,7 +134,7 @@ function loadExtension ($ext_name, $ext_mode, $ext_ver = '', $dry_run = false) {
                        // Download functions file
                        loadIncludeOnce($funcsInclude);
                } // END - if
-       } elseif ((!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) && (isDebugModeEnabled()) && (getScriptOutputMode() == '0') && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme') && (getExtensionMode() == 'test')) {
+       } elseif ((!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) && (isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme') && (getExtensionMode() == 'test')) {
                // No functions file is not so good...
                logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no own functions file or we cannot read from it. mode=%s",
                        $ext_name,
@@ -234,7 +243,7 @@ function registerExtension ($ext_name, $taskId, $dry_run = false, $logout = true
 
        // Does this extension exists?
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
-       if (loadExtension(getCurrentExtensionName(), 'register', '', isExtensionDryRun())) {
+       if (loadExtension(getCurrentExtensionName(), 'register', '0.0', isExtensionDryRun())) {
                // Set current extension name again
                setCurrentExtensionName($ext_name);
 
@@ -365,6 +374,10 @@ function registerExtension ($ext_name, $taskId, $dry_run = false, $logout = true
 
                                // Use the insert id as extension id and cache it for early usage
                                $GLOBALS['cache_array']['extension']['ext_id'][getCurrentExtensionName()] = SQL_INSERTID();
+                               $GLOBALS['cache_array']['extension']['ext_name'][SQL_INSERTID()] = getCurrentExtensionName();
+
+                               // Mark it as installed
+                               $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
 
                                // Remove cache file(s) if extension is active
                                runFilterChain('post_extension_installed', array(
@@ -377,9 +390,6 @@ function registerExtension ($ext_name, $taskId, $dry_run = false, $logout = true
                                initExtensionSqls(true);
                                initExtensionNotes(true);
 
-                               // Mark it as installed
-                               $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
-
                                // In normal mode return a true on success
                                $ret = true;
                        } elseif (isExtensionDryRun() === true) {
@@ -439,20 +449,23 @@ function doExtensionSqls ($ext_id, $load_mode) {
        } // END - if
 
        // Init array
-       initExtensionSqls();
+       initExtensionSqls(true);
 
        // By default no SQL has been executed
        $sqlRan = false;
 
        // Load extension in detected mode
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName());
-       loadExtension(getCurrentExtensionName(), $load_mode, '', false);
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName() . ',load_mode=' . $load_mode);
+       loadExtension(getCurrentExtensionName(), $load_mode, '0.0', false);
 
        // Init these SQLs
        initSqls();
        setSqlsArray(getExtensionSqls());
 
+       // Debug message
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls());
+
+       // Do we have entries?
        if (isSqlsValid()) {
                // Run SQL commands...
                runFilterChain('run_sqls');
@@ -462,7 +475,7 @@ function doExtensionSqls ($ext_id, $load_mode) {
        runFilterChain('extension_' . $load_mode);
 
        // Remove cache file(s) if extension is active
-       if (((isExtensionActive('cache')) && ((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
+       if (((isExtensionActive('cache')) && ((!SQL_HASZEROAFFECTED())) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
                // Run filters
                runFilterChain('post_extension_run_sql', getCurrentExtensionName());
        } // END - if
@@ -503,6 +516,9 @@ function isExtensionInstalled ($ext_name) {
                // Look in database
                $ext_id = getExtensionId($ext_name);
 
+               // Log debug message
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension ' . $ext_name . ' has ext_id=' . $ext_id);
+
                // Do we have a record?
                $isInstalled = ($ext_id > 0);
 
@@ -601,7 +617,7 @@ function getExtensionVersion ($ext_name, $force = false) {
 
                // Count cache hits
                incrementStatsEntry('cache_hits');
-       } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (getScriptOutputMode() != 0)) {
+       } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (!isHtmlOutputMode())) {
                // Load from database
                $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
                        array($ext_name), __FUNCTION__, __LINE__);
@@ -899,38 +915,44 @@ function isExtensionIdValid ($ext_id) {
 
 // Activate given extension
 function doActivateExtension ($ext_name) {
+       // Is the extension installed?
+       if (!isExtensionInstalled($ext_name)) {
+               // Non-installed extensions cannot be activated
+               debug_report_bug(__FUNCTION__, __LINE__, 'Tried to activate non-installed extension ' . $ext_name);
+       } // END - if
+
        // Activate the extension
        SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
                array($ext_name), __FUNCTION__, __LINE__);
 
-       // Extension has been activated?
-       if (SQL_AFFECTEDROWS() == 1) {
-               // Then run all queries
-               doExtensionSqls(getExtensionId($ext_name), 'activate');
-       } // END - if
+       // Then run all queries
+       doExtensionSqls(getExtensionId($ext_name), 'activate');
 }
 
 // Deactivate given extension
-function doDeactivateExtension($ext_name) {
+function doDeactivateExtension ($ext_name) {
+       // Is the extension installed?
+       if (!isExtensionInstalled($ext_name)) {
+               // Non-installed extensions cannot be activated
+               debug_report_bug(__FUNCTION__, __LINE__, 'Tried to deactivate non-installed extension ' . $ext_name);
+       } // END - if
+
        // Activate the extension
        SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
                array($ext_name), __FUNCTION__, __LINE__);
 
-       // Extension has been activated?
-       if (SQL_AFFECTEDROWS() == 1) {
-               // Then run all queries
-               doExtensionSqls(getExtensionId($ext_name), 'deactivate');
+       // Then run all queries
+       doExtensionSqls(getExtensionId($ext_name), 'deactivate');
 
-               // Create new task (we ignore the task id here)
-               createExtensionDeactivationTask($ext_name);
+       // Create new task (we ignore the task id here)
+       createExtensionDeactivationTask($ext_name);
 
-               // Notify the admin
-               sendAdminNotification(
-                       '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
-                       'admin_extension_deactivated',
-                       array('ext_name' => $ext_name)
-               );
-       } // END - if
+       // Notify the admin
+       sendAdminNotification(
+               '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
+               'admin_extension_deactivated',
+               array('ext_name' => $ext_name)
+       );
 }
 
 // Checks wether the extension is older than given
@@ -1178,8 +1200,8 @@ function isExtensionDeprecated () {
 }
 
 // Setter for EXT_UPDATE_DEPENDS flag
-function addExtensionUpdateDependency ($updateDepends) {
-       //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
+function addExtensionDependency ($updateDepends) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
        // Is the update depency empty? (NEED TO BE FIXED!)
        if (empty($updateDepends)) {
                // Please report this bug!
@@ -1199,7 +1221,7 @@ function addExtensionUpdateDependency ($updateDepends) {
 
        // Remember it in the list of running updates
        $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
-       //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
 }
 
 // Getter for running updates
@@ -1314,7 +1336,7 @@ function countExtensionUpdateDependencies () {
 }
 
 // Removes given extension from update denpency list
-function removeExtensionUpdateDependency ($ext_name) {
+function removeExtensionDependency ($ext_name) {
        // Look it up
        $key = array_search($ext_name, getExtensionUpdateDependencies());
 
@@ -1445,6 +1467,7 @@ function initExtensionSqls ($force = false) {
 // Adds SQLs to the SQLs array but "assigns" it with current extension name
 function addExtensionSql ($sql) {
        // Add it
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',ext_version=' . getCurrentExtensionVersion() . ',sql=' . $sql);
        $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
 }
 
@@ -1523,19 +1546,19 @@ function redirectOnUninstalledExtension ($ext_name) {
 // Filter for initialization of all extensions by loading them in 'init' mode
 function FILTER_INIT_EXTENSIONS () {
        // Do we have some entries?
-       //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY!');
        if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
                // Load all found extensions if found
-               //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
                foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
                        // Load it
-                       //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - START');
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - START');
                        loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
-                       //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - END');
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - END');
                } // END - foreach
-               //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
        } // END - if
-       //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
 }
 
 // Setter for extension mode
@@ -1628,14 +1651,8 @@ function getExtensionHasCss () {
 
 // Checks wether the given extension has a language file
 function ifExtensionHasLanguageFile ($ext_name) {
-       // Default is no language file
-       $hasLanguage = false;
-
        // Do we have cache?
        if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
-               // Then use it
-               $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
-
                // Count cache hits
                incrementStatsEntry('cache_hits');
        } else {
@@ -1649,7 +1666,7 @@ function ifExtensionHasLanguageFile ($ext_name) {
                } // END - if
 
                // Put it in cache
-               $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = 'Y';
+               $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = $readable;
        }
 
        // Return result
@@ -1747,7 +1764,7 @@ function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
                addExtensionSql($sql);
        } elseif (isDebugModeEnabled()) {
                // Double menus should be located and fixed!
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s, what=%s detected.", $action, $what));
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
        }
 }
 
@@ -1781,7 +1798,7 @@ function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
                addExtensionSql($sql);
        } elseif (isDebugModeEnabled()) {
                // Double menus should be located and fixed!
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s, what=%s detected.", $action, $what));
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s,what=%s,title=%s,locked=%s,visible=%s detected.", $action, $what, $title, $locked, $visible));
        }
 }
 
@@ -1815,7 +1832,7 @@ function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
                addExtensionSql($sql);
        } elseif (isDebugModeEnabled()) {
                // Double menus should be located and fixed!
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s, what=%s detected.", $action, $what));
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s,what=%s,title=%s,visivle=%s,locked=%s detected.", $action, $what, $title, $visible, $locked));
        }
 }
 
@@ -1847,20 +1864,46 @@ function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
                addExtensionSql($sql);
        } elseif (isDebugModeEnabled()) {
                // Double menus should be located and fixed!
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s, what=%s detected.", $action, $what));
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s,what=%s,title=%s,active=%s detected.", $action, $what, $title, $active));
        }
 }
 
 // Enables/disables productive mode for current extension (used only while
 // registration).
 function enableExtensionProductive ($isProductive = true) {
+       // Log debug message
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%d", getCurrentExtensionName(), intval($isProductive)));
+
+       // Set it
        $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
 }
 
 // Checks wether the extension is in productive phase. If not set, development
 // phase (=false) is assumed.
-function isExtensionProductive () {
-       return ((isset($GLOBALS['ext_productive'][getCurrentExtensionName()])) && ($GLOBALS['ext_productive'][getCurrentExtensionName()] === true));
+function isExtensionProductive ($ext_name = '') {
+       // Is the extension name empty? Then use current
+       if (empty($ext_name)) {
+               // Get current extension name
+               $ext_name = getCurrentExtensionName();
+       } // END - if
+
+       // Do we have cache?
+       if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
+               // Load extension only if not yet loaded
+               if (!isset($GLOBALS['ext_productive'][$ext_name])) {
+                       // Load extension in test mode
+                       loadExtension($ext_name, 'test');
+               } // END - if
+
+               // Determine it
+               $GLOBALS[__FUNCTION__][$ext_name] = ((isset($GLOBALS['ext_productive'][$ext_name])) && ($GLOBALS['ext_productive'][$ext_name] === true));
+       } // END - if
+
+       // Log debug message
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ext_name=%s,isProductive=%s", $ext_name, intval($GLOBALS[__FUNCTION__][$ext_name])));
+
+       // Return result
+       return $GLOBALS[__FUNCTION__][$ext_name];
 }
 
 // [EOF]