public function testsugarDecode()
 {
     //execute the method and test if it returns expected values
     //key param does nothing currently.
     //blank key and data
     $expected = '';
     $actual = sugarDecode('', '');
     $this->assertSame($expected, $actual);
     //blank key and valid data
     $expected = 'Data';
     $actual = sugarDecode('', 'RGF0YQ==');
     $this->assertSame($expected, $actual);
     //valid key and data
     $expected = 'Data';
     $actual = sugarDecode('key', 'RGF0YQ==');
     $this->assertSame($expected, $actual);
 }
function check_now($send_usage_info = true, $get_request_data = false, $response_data = false, $from_install = false)
{
    global $sugar_config, $timedate;
    global $db, $license;
    $return_array = array();
    if (!$from_install && empty($license)) {
        loadLicense(true);
    }
    if (!$response_data) {
        if ($from_install) {
            $info = getBaseSystemInfo(false);
        } else {
            $info = getSystemInfo($send_usage_info);
        }
        require_once 'include/nusoap/nusoap.php';
        $GLOBALS['log']->debug('USING HTTPS TO CONNECT TO HEARTBEAT');
        $sclient = new nusoapclient('https://updates.sugarcrm.com/heartbeat/soap.php', false, false, false, false, false, 15, 15);
        $ping = $sclient->call('sugarPing', array());
        if (empty($ping) || $sclient->getError()) {
            $sclient = '';
        }
        if (empty($sclient)) {
            $GLOBALS['log']->debug('USING HTTP TO CONNECT TO HEARTBEAT');
            $sclient = new nusoapclient('http://updates.sugarcrm.com/heartbeat/soap.php', false, false, false, false, false, 15, 15);
        }
        $key = '4829482749329';
        $encoded = sugarEncode($key, serialize($info));
        if ($get_request_data) {
            $request_data = array('key' => $key, 'data' => $encoded);
            return serialize($request_data);
        }
        $encodedResult = $sclient->call('sugarHome', array('key' => $key, 'data' => $encoded));
    } else {
        $encodedResult = $response_data['data'];
        $key = $response_data['key'];
    }
    if ($response_data || !$sclient->getError()) {
        $serializedResultData = sugarDecode($key, $encodedResult);
        $resultData = unserialize($serializedResultData);
        if ($response_data && empty($resultData)) {
            $resultData = array();
            $resultData['validation'] = 'invalid validation key';
        }
    } else {
        $resultData = array();
        $resultData['versions'] = array();
    }
    if ($response_data || !$sclient->getError()) {
        if (!empty($resultData['msg'])) {
            if (!empty($resultData['msg']['admin'])) {
                $license->saveSetting('license', 'msg_admin', base64_encode($resultData['msg']['admin']));
            } else {
                $license->saveSetting('license', 'msg_admin', '');
            }
            if (!empty($resultData['msg']['all'])) {
                $license->saveSetting('license', 'msg_all', base64_encode($resultData['msg']['all']));
            } else {
                $license->saveSetting('license', 'msg_all', '');
            }
        } else {
            $license->saveSetting('license', 'msg_admin', '');
            $license->saveSetting('license', 'msg_all', '');
        }
        $license->saveSetting('license', 'last_validation', 'success');
        unset($_SESSION['COULD_NOT_CONNECT']);
    } else {
        $resultData = array();
        $resultData['versions'] = array();
        $license->saveSetting('license', 'last_connection_fail', TimeDate::getInstance()->nowDb());
        $license->saveSetting('license', 'last_validation', 'no_connection');
        if (empty($license->settings['license_last_validation_success']) && empty($license->settings['license_last_validation_fail']) && empty($license->settings['license_vk_end_date'])) {
            $license->saveSetting('license', 'vk_end_date', TimeDate::getInstance()->nowDb());
            $license->saveSetting('license', 'validation_key', base64_encode(serialize(array('verified' => false))));
        }
        $_SESSION['COULD_NOT_CONNECT'] = TimeDate::getInstance()->nowDb();
    }
    if (!empty($resultData['versions'])) {
        $license->saveSetting('license', 'latest_versions', base64_encode(serialize($resultData['versions'])));
    } else {
        $resultData['versions'] = array();
        $license->saveSetting('license', 'latest_versions', '');
    }
    include 'sugar_version.php';
    if (sizeof($resultData) == 1 && !empty($resultData['versions'][0]['version']) && $resultData['versions'][0]['version'] < $sugar_version) {
        $resultData['versions'][0]['version'] = $sugar_version;
        $resultData['versions'][0]['description'] = "You have the latest version.";
    }
    return $resultData['versions'];
}
/**
 * Authenticate license settings
 * @return boolean
 */
function authenticateDownloadKey()
{
    // Retreive license if required
    if ((!is_array($GLOBALS['license']->settings) || empty($GLOBALS['license']->settings['license_validation_key'])) && shouldCheckSugar()) {
        check_now(get_sugarbeat());
    }
    // Validation key is required
    if (!is_array($GLOBALS['license']->settings) || empty($GLOBALS['license']->settings['license_validation_key'])) {
        return false;
    }
    // We are good if a validation is already set
    if (is_array($GLOBALS['license']->settings) && is_array($GLOBALS['license']->settings['license_validation_key']) && !empty($GLOBALS['license']->settings['license_validation_key']['validation'])) {
        return true;
    }
    // Populate data from globals
    $fromGlobals = array('license_expire_date' => array('type' => 'string'), 'license_users' => array('type' => 'int'), 'license_num_lic_oc' => array('type' => 'int'), 'license_num_portal_users' => array('type' => 'int'), 'license_vk_end_date' => array('type' => 'string'), 'license_key' => array('type' => 'string'), 'license_enforce_portal_user_limit' => array('type' => 'int', 'target' => 'enforce_portal_user_limit'), 'license_enforce_user_limit' => array('type' => 'int', 'target' => 'enforce_user_limit'));
    $data = array();
    foreach ($fromGlobals as $source => $defs) {
        $target = empty($defs['target']) ? $source : $defs['target'];
        if (isset($GLOBALS['license']->settings[$source])) {
            switch ($defs['type']) {
                case 'int':
                    $data[$target] = intval($GLOBALS['license']->settings[$source]);
                    break;
                default:
                    $data[$target] = $GLOBALS['license']->settings[$source];
                    break;
            }
        }
    }
    // Decode the received validation key and compare with current settings
    $og = unserialize(sugarDecode('validation', $GLOBALS['license']->settings['license_validation_key']));
    foreach ($og as $name => $value) {
        if (!isset($data[$name]) || $data[$name] != $value) {
            return false;
        }
    }
    return true;
}