<?php
 // include("debuglib.php")
include_once('../../bootloader.php');

?>

<?php
header("Content-Type: text/plain");

// http://www.framework.zend.com/manual/1.9/en/zend.rest.client.html
// http://www.framework.zend.com/manual/2.0/en/modules/zendservice.twitter.html
// const VERSION = '1.9.7';
// http://www.quadrillian.com/#view-project/161

set_time_limit(600); // increase timeout
define('ATMAIL_MIN_PHP_VERSION', '5.1.6');

// Read in current Atmail version
$atmailVersion = trim(@file_get_contents('../.VERSION'));

// Setup lib paths
// define('APP_ROOT', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
$path = dirname(dirname(__FILE__));
//set_include_path('.' . PATH_SEPARATOR . "$path/library/" . PATH_SEPARATOR . "$path/sso/phpseclib/");

session_start();

require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();

$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);

// Report all errors except E_NOTICE
error_reporting(E_ALL ^ E_NOTICE);

// echo "<hr />";
// echo "cookies:<br />";
// print_r($_COOKIE);
// echo "<hr />";
// print_r($_COOKIE[atmailuser]);
// echo "<hr />";
// print_r($_COOKIE[PHPSESSID]);
// echo "<hr />";

setcookie('chtrsessionid', 'chtrsessionid001',  time()+60*60*24*30, '/');
setcookie('chtrpkey', 'chtrpkey001',  time()+60*60*24*30, '/');


// $loginserviceURL =      "http://atmail.local";
$loginserviceURL =      "http://atmail.rest";
$loginserviceEndpoint = "/index.php/api/inbox/accounts";

// $loginserviceEndpoint = "/loginmiddle/rest/api/login/v1/login/sensitive_details";

Zend_Registry::get('log')->debug( __METHOD__ . ' #' . __LINE__ . ': $loginserviceURL = |' . $loginserviceURL . '|');
Zend_Registry::get('log')->debug( __METHOD__ . ' #' . __LINE__ . ': $loginserviceEndpoint = |' . $loginserviceEndpoint . '|');

Zend_Registry::get('log')->debug(__METHOD__ . ' #' . __LINE__ . "Starting SSO....");

$base_url = $loginserviceURL;
$endpoint = $loginserviceEndpoint;

$loginService = new Zend_Rest_Client($base_url);
$loginService->getHttpClient()->setHeaders(array('Accept' => 'application/json'));

$httpClient = $loginService->getHttpClient();

$chtrsessionid = $_COOKIE['chtrsessionid'];
$chtrpkey = $_COOKIE['chtrpkey'];

$httpClient->setHeaders('Cookie', array(
    "XDEBUG_SESSION=PHPSTORM",
    "chtrsessionid=$chtrsessionid",
    "chtrpkey=$chtrpkey"
));

$data = array();
$data['appId'] = 'atmail';

Zend_Registry::get('log')->debug(__METHOD__ . ' #' . __LINE__ . "chtrsessionid: $chtrsessionid");
Zend_Registry::get('log')->debug(__METHOD__ . ' #' . __LINE__ . "chtrpkey: $chtrpkey");

$response = $loginService->restGet($endpoint, $data);
$responseBody  = $response->getBody();

Zend_Registry::get('log')->debug(__METHOD__ . ' #' . __LINE__ . "Response: $response");
Zend_Registry::get('log')->debug(__METHOD__ . ' #' . __LINE__ . "##### Body: $responseBody");

$responseObject = json_decode($responseBody);

Zend_Registry::get('log')->debug(__METHOD__ . ' #' . __LINE__ . "##### responseBody: $responseBody");

Zend_Registry::get('log')->debug(__METHOD__ . ' #' . __LINE__ . "##### responseBodyObj: " . print_r($responseObject, true));


print($responseBody);

print("\n\n=========================================================\n\n");

print_r($responseObject);



