Reverted of changes in 1704, see ticket #160
[mailer.git] / inc / extensions-functions.php
index a9d1ed29103109e68f87d31369e617fbcb5816bc..a33aabce0f13c33ae093a8315cf57f4f0c3310e7 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 10/25/2009 *
- * ===============                              Last change: 10/25/2009 *
+ * Mailer v0.2.1-FINAL                                Start: 10/25/2009 *
+ * ===================                          Last change: 10/25/2009 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : extensions-functions.php                         *
@@ -18,6 +18,7 @@
  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -226,7 +227,7 @@ function registerExtension ($ext_name, $task_id, $dry_run = false, $logout = tru
                        incrementExtensionUpdateIterator();
 
                        // Check for required file
-                       if (loadExtension($ext_update, 'register', '', getExtensionDryRun())) {
+                       if ((!isExtensionInstalled($ext_update)) && (loadExtension($ext_update, 'register', '', getExtensionDryRun()))) {
                                // Set current extension name again
                                setCurrentExtensionName($ext_name);
 
@@ -254,12 +255,15 @@ function registerExtension ($ext_name, $task_id, $dry_run = false, $logout = tru
                                        } // END - if
                                } elseif ($ext_ver != getCurrentExtensionVersion()) {
                                        // Ok, update this extension now
-                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().",ext_update=".$ext_update.",ext_ver=".$ext_ver."/".getCurrentExtensionVersion()."");
-                                       $GLOBALS['ext_backup'][$ext_update][$ext_ver] = getCurrentExtensionName();
+                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName='.getCurrentExtensionName().',currVer='.getCurrentExtensionVersion().'');
+                                       $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
+                                       $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
                                        updateExtension($ext_update, $ext_ver, getExtensionDryRun());
-                                       setCurrentExtensionName($GLOBALS['ext_backup'][$ext_update][$ext_ver]);
-                                       unset($GLOBALS['ext_backup'][$ext_update][$ext_ver]);
-                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName()."");
+                                       setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
+                                       setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
+                                       unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
+                                       unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
+                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName='.getCurrentExtensionName().',currVer='.getCurrentExtensionVersion().'');
 
                                        // All okay!
                                        $test = true;
@@ -475,19 +479,19 @@ function isExtensionActive ($ext_name) {
        if ((isInstallationPhase()) || (empty($ext_name))) return false;
 
        // Not active is the default
-       $active = 'N';
+       $data['ext_active'] = 'N';
 
        // Check cache
        if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
                // Load from cache
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
-               $active = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
+               $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
 
                // Count cache hits
                incrementStatsEntry('cache_hits');
        } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
                // @TODO Extension is loaded, what next?
-               app_die(__FUNCTION__, __LINE__, "LOADED:$ext_name");
+               app_die(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
        } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
                // Load from database
@@ -497,15 +501,15 @@ function isExtensionActive ($ext_name) {
                // Entry found?
                if (SQL_NUMROWS($result) == 1) {
                        // Load entry
-                       list($active) = SQL_FETCHROW($result);
+                       $data = SQL_FETCHARRAY($result);
                } // END - if
 
                // Free result
                SQL_FREERESULT($result);
 
                // Write cache array
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name."[DB]: {$active}");
-               $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $active;
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name."[DB]: ".$data['ext_active']."");
+               $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
        } else {
                // Extension not active!
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.": Not active!");
@@ -513,16 +517,16 @@ function isExtensionActive ($ext_name) {
        }
 
        // Debug message
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name={$ext_name},active={$active}");
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.",active=".$data['ext_active']."");
 
        // Is this extension activated? (For admins we always have active extensions...)
-       return ($active == 'Y');
+       return ($data['ext_active'] == 'Y');
 }
 
 // Get version from extensions
-function getExtensionVersion ($ext_name) {
+function getExtensionVersion ($ext_name, $force = false) {
        // By default no extension is found
-       $ext_ver = 'invalid';
+       $data['ext_version'] = 'false';
 
        // Empty extension name should be fixed!
        if (empty($ext_name)) {
@@ -532,13 +536,13 @@ function getExtensionVersion ($ext_name) {
 
        // Extensions are all inactive during installation
        if (isInstallationPhase()) return '';
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ext_name=".$ext_name."");
 
        // Is the cache written?
        if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
                // Load data from cache
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": CACHE!");
-               $ext_ver = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
+               $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
 
                // Count cache hits
                incrementStatsEntry('cache_hits');
@@ -546,33 +550,33 @@ function getExtensionVersion ($ext_name) {
                // 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__);
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": DB - ".SQL_NUMROWS($result)."");
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
 
                // Is the extension there?
                if (SQL_NUMROWS($result) == 1) {
                        // Load entry
-                       list($ext_ver) = SQL_FETCHROW($result);
+                       $data = SQL_FETCHARRAY($result);
                } elseif (isDebugModeEnabled()) {
-                       // Not found!
-                       logDebugMessage(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
+                       // Not found, please report all
+                       debug_report_bug(sprintf(": Cannot find extension %s in database!", $ext_name));
                }
 
                // Free result
                SQL_FREERESULT($result);
 
                // Set cache
-               $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $ext_ver;
+               $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
        }
 
        // Extension version should not be invalid
-       if ($ext_ver == 'invalid') {
+       if (($data['ext_version'] == 'false') && ($force === false)) {
                // Please report this trouble
                debug_report_bug(sprintf("Extension <strong>%s</strong> has empty version!", $ext_name));
        } // END - if
 
        // Return result
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
-       return $ext_ver;
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ret={$data['ext_version']}");
+       return $data['ext_version'];
 }
 
 // Updates a given extension with current extension version to latest version
@@ -679,7 +683,7 @@ function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false
        $OUT = '';
 
        // Do we have queries?
-       if ((getExtensionVersion('sql_patches') >= '0.0.7') && (getConfig('verbose_sql') == 'Y')) {
+       if ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
                // Init switch color
                $SW = 2;
 
@@ -718,7 +722,7 @@ function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false
 
                // Load main template
                $OUT = loadTemplate('admin_ext_sql_table', true, $content);
-       } elseif ((getExtensionVersion('sql_patches') >= '0.0.7') && (getConfig('verbose_sql') == 'Y')) {
+       } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
                // No addional SQL commands to run
                $OUT = loadTemplate('admin_settings_saved', true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
        } // END - if
@@ -730,12 +734,12 @@ function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false
 // Get extension name from id
 function getExtensionName ($ext_id) {
        // Init extension name
-       $ret = '';
+       $data['ext_name'] = '';
 
        // Is cache there?
        if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
                // Load from cache
-               $ret = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
+               $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
 
                // Count cache hits
                incrementStatsEntry('cache_hits');
@@ -747,7 +751,7 @@ function getExtensionName ($ext_id) {
                // Is the entry there?
                if (SQL_NUMROWS($result) == 1) {
                        // Get the extension's name from database
-                       list($ret) = SQL_FETCHROW($result);
+                       $data = SQL_FETCHARRAY($result);
                } // END - if
 
                // Free result
@@ -755,47 +759,47 @@ function getExtensionName ($ext_id) {
        }
 
        // Did we find some extension?
-       if (empty($ret)) {
+       if (empty($data['ext_name'])) {
                // We should fix these all!
                debug_report_bug(__FUNCTION__ . ': ext_name is empty. ext_id=' . $ext_id);
        } // END - if
 
        // Return the extension name
-       return $ret;
+       return $data['ext_name'];
 }
 
 // Get extension id from name
 function getExtensionId ($ext_name) {
        // Init id number
-       $ret = '0';
+       $data['ext_id'] = '0';
 
        // Do we have cache?
        if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
                // Load from cache
-               $ret = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
+               $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
 
                // Count cache hits
                incrementStatsEntry('cache_hits');
        } else {
                // Load from database
-               $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
+               $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
                        array($ext_name), __FUNCTION__, __LINE__);
 
                // Is the entry there?
                if (SQL_NUMROWS($result) == 1) {
                        // Get the extension's id from database
-                       list($ret) = SQL_FETCHROW($result);
+                       $data = SQL_FETCHARRAY($result);
                } // END - if
 
                // Free result
                SQL_FREERESULT($result);
 
                // Cache it
-               $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $ret;
+               $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
        }
 
        // Return value
-       return $ret;
+       return $data['ext_id'];
 }
 
 // Determines wether the given extension name is valid
@@ -863,7 +867,7 @@ function doDeactivateExtension($ext_name) {
 
                // Notify the admin
                sendAdminNotification(
-               getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
+                       getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
                        'admin_ext_deactivated',
                        array('ext_name' => $ext_name)
                );
@@ -906,7 +910,7 @@ function createNewExtensionTask ($adminId, $subject, $ext) {
        // Not installed and do we have created a task for the admin?
        if ((determineTaskIdBySubject($subject) == '0') && (!isExtensionInstalled($ext))) {
                // Set default message if ext-foo is missing
-               $message = sprintf(getMessage('ADMIN_EXT_TEXT_FILE_MISSING'), $ext);
+               $message = getMaskedMessage('ADMIN_EXT_TEXT_FILE_MISSING', $ext);
 
                // Template file
                $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
@@ -946,7 +950,7 @@ function ifModuleHasMenu ($mod, $forceDb = false) {
        // All is false by default
        $ret = false;
 
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "mod={$mod},cache=".getExtensionVersion('cache'));
+       // Extension installed and newer than or has version 0.1.2?
        if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
                // Cache version is okay, so let's check the cache!
                if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
@@ -957,9 +961,9 @@ function ifModuleHasMenu ($mod, $forceDb = false) {
                        // Check cache and count hit
                        $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
                        incrementStatsEntry('cache_hits');
-               } elseif ($mod == 'admin') {
-                       // Admin module has always a menu!
-                       $ret = true;
+               } else {
+                       // Admin/guest/member/sponsor modules have always a menu!
+                       $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
                }
        } elseif ((isExtensionInstalled('sql_patches')) && (getExtensionVersion('sql_patches') >= '0.3.6') && ((!isExtensionActive('cache')) || ($forceDb === true))) {
                // Check database for entry
@@ -969,69 +973,71 @@ function ifModuleHasMenu ($mod, $forceDb = false) {
                // Entry found?
                if (SQL_NUMROWS($result) == 1) {
                        // Load "has_menu" column
-                       list($has_menu) = SQL_FETCHROW($result);
+                       $data = SQL_FETCHARRAY($result);
 
                        // Fake cache... ;-)
-                       $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $has_menu;
+                       $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
 
                        // Does it have a menu?
-                       $ret = ($has_menu == 'Y');
+                       $ret = ($data['has_menu'] == 'Y');
                } // END  - if
 
                // Free memory
                SQL_FREERESULT($result);
        } elseif (!isExtensionInstalled('sql_patches')) {
-               // No sql_patches installed, so maybe in admin area or no admin registered?
-               $ret = (((isAdmin()) || (!isAdminRegistered())) && ($mod == 'admin')); // Then there is a menu!
+               // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
+               $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
+       } else {
+               // Unsupported state!
+               logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
        }
 
        // Return status
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().intval($ret));
        return $ret;
 }
 
 // Determines the task id for given extension
 function determineExtensionTaskId ($ext_name) {
        // Default is not found
-       $task_id = '0';
+       $data['task_id'] = '0';
 
        // Search for extension task's id
-       $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
+       $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
                array($ext_name), __FUNCTION__, __LINE__);
 
        // Entry found?
        if (SQL_NUMROWS($result) == 1) {
                // Task found so load task's id and register extension...
-               list($task_id) = SQL_FETCHROW($result);
+               $data = SQL_FETCHARRAY($result);
        } // END - if
 
        // Free result
        SQL_FREERESULT($result);
 
        // Return it
-       return $task_id;
+       return $data['task_id'];
 }
 
 // Determines the task id for given subject
 function determineTaskIdBySubject ($subject) {
        // Default is not found
-       $task_id = '0';
+       $data['task_id'] = '0';
 
        // Search for task id
-       $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
+       $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
                array($subject), __FUNCTION__, __LINE__);
 
        // Entry found?
        if (SQL_NUMROWS($result) == 1) {
                // Task found so load task's id and register extension...
-               list($task_id) = SQL_FETCHROW($result);
+               $data = SQL_FETCHARRAY($result);
        } // END - if
 
        // Free result
        SQL_FREERESULT($result);
 
        // Return it
-       return $task_id;
+       return $data['task_id'];
 }
 
 // Add updates notes for given version
@@ -1141,7 +1147,7 @@ function addExtensionUpdateDependency ($updateDepends) {
        // Is the update depency empty? (NEED TO BE FIXED!)
        if (empty($updateDepends)) {
                // Please report this bug!
-               debug_report_bug("updateDepends is left empty!");
+               debug_report_bug('updateDepends is left empty!');
        } // END - if
 
        // Is it not yet added?
@@ -1445,13 +1451,13 @@ function getCurrentExtensionVersion () {
        return $GLOBALS['ext_current_version'];
 }
 
-// Remove the extension from global cache array
+// Remove the extension from cache array
 function removeExtensionFromArray () {
        // "Cache" this name
        $ext_name = getCurrentExtensionName();
 
        // Now loop through the whole cache
-       foreach ($GLOBALS['cache_array']['extension'] as $cacheName=>$cacheArray) {
+       foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
                // Is it an element?
                if (isset($cacheArray[$ext_name])) {
                        // Array element
@@ -1553,7 +1559,7 @@ function isExtensionIncludeReadable ($ext_name = '') {
        } // END - if
 
        // Return result
-       //* DEBUG: */ print __FUNCTION__.': '.$ext_name.'='.intval($GLOBALS['ext_inc_readable'][$ext_name]).'<br />';
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
        return $GLOBALS['ext_inc_readable'][$ext_name];
 }