Index: app/trunk/application/login.php =================================================================== diff -u -r38 -r55 --- app/trunk/application/login.php (.../login.php) (revision 38) +++ app/trunk/application/login.php (.../login.php) (revision 55) @@ -33,50 +33,61 @@ } function lemans_build_base_url(){ - //return (!empty($_SERVER['HTTPS'])) ? "https://" . $_SERVER['SERVER_NAME'] : "http://" . $_SERVER['SERVER_NAME']; return (!empty($_SERVER['HTTPS'])) ? "https://" . $_SERVER['CLSTCAMEL'] : "http://" . $_SERVER['CLSTCAMEL']; } - //Function borrowed from http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/ + function lemans_do_post_request($url, $data, $optional_headers = null) { $baseUrl = lemans_build_base_url(); - $params = array( - 'http' => array( - 'method' => 'POST', - 'content' => $data - )); + $requestString = lemans_generate_payload_string($data); + $curl = curl_init(); - if ($optional_headers !== null) { - $params['http']['header'] = $optional_headers; - } + curl_setopt($curl, CURLOPT_HEADER, 1); + curl_setopt($curl,CURLOPT_URL, $baseUrl . $url); + curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded")); + curl_setopt($curl,CURLOPT_POST, count($data)); + curl_setopt($curl,CURLOPT_POSTFIELDS, $requestString); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_VERBOSE, 1); + $response = curl_exec($curl); + $header_array = get_headers_from_curl_response($response); - $ctx = stream_context_create($params); - $fp = @fopen($baseUrl . $url, 'rb', false, $ctx); + //close connection + curl_close($curl); - if (!$fp) { - //Error Logging In, Return False - //throw new Exception("Problem with " . $url, $php_errormsg); + if (!$response || $response === false) { return false; } - $response = @stream_get_contents($fp); - - if ($response === false) { - //throw new Exception("Problem reading data from " . $url, $php_errormsg); - return false; - } - //Get the Token and Expire Time - list($token, $tokenExpires) = lemans_process_http_response_header($http_response_header); + list($token, $tokenExpires) = lemans_process_http_response_header_array($header_array); $_SESSION['loginToken'] = !empty($token) ? $token : ''; $_SESSION['tokenExpires'] = !empty($tokenExpires) ? $tokenExpires : ''; return $response; } + function get_headers_from_curl_response($response) + { + $headers = array(); + $header_text = substr($response, 0, strpos($response, "\r\n\r\n")); + + foreach (explode("\r\n", $header_text) as $i => $line) + if ($i === 0) + $headers['http_code'] = $line; + else + { + list ($key, $value) = explode(': ', $line); + + $headers[$key] = $value; + } + + return $headers; + } + function lemans_do_get_request($url, $data, $optional_headers = null) { $baseUrl = lemans_build_base_url(); @@ -143,6 +154,7 @@ } + //process header string function lemans_process_http_response_header($http_response_header) { //Search the Headers Array for the Token and Token Expires Values $tokenExpires = ''; @@ -163,6 +175,40 @@ return array($token, $tokenExpires); } + //process header when passed as an array + function lemans_process_http_response_header_array($http_response_header) + { //Search the Headers Array for the Token and Token Expires Values + $tokenExpires = ''; + $token = ''; + + foreach ($http_response_header as $key => $val) { + if ($key == 'loginTokenExpiry') { + $tokenExpires = empty($tokenExpires) ? trim($val) : $tokenExpires; + } elseif ($key == 'loginToken') { + $token = empty($token) ? trim($val) : $token; + } + } + + return array($token, $tokenExpires); + } + + function lemans_generate_payload_string($items = array()) + { + if (!empty($items)) { + $requestString = ''; + $cnt = 0; + foreach ($items as $key => $item) { + if (!empty($item)) { + $requestString .= ($cnt == 0) ? $key . '=' . $item : '&' . $key . '=' . rawurlencode($item); + } + $cnt++; + } + return $requestString; + } else { + return false; + } + } + function lemans_generate_query_parameter_string($items = array()) { if (!empty($items)) { @@ -190,10 +236,8 @@ 'password' => trim($_POST['password']) ); - $requestString = lemans_generate_query_parameter_string($requestItems); + $tokenRequest = lemans_do_post_request($_SERVER['SRVLOGIN'], $requestItems); - $tokenRequest = lemans_do_post_request($_SERVER['SRVLOGIN'] . $requestString, null); - if (!$tokenRequest) { return '

Login Unsuccessful. Please Try Again.

'; } else {