<?php

/*
	Googlyzer
	Contact: Bill Edgar (bill.edgar@oaktondata.com)
	http://www.oaktondata.com/googlyzer
	
	Copyright (c) 2011-2012, Oakton Data LLC
	All rights reserved.

	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
	    
	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in the
	      documentation and/or other materials provided with the distribution.
	    * Neither the name of the Oakton Data LLC nor the
	      names of its contributors may be used to endorse or promote products
	      derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL Oakton Data LLC BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	
	This is the googlyzer_GoogleAuth PHP class file.
	
 */

if ( !class_exists( 'googlyzer_GoogleAuth' ) ) {
	
	/** googlyzer_GoogleAuth class
	 *
	 * PHP class for handling authentication POST request from Googlyzer general settings page.
	 * @author wmsedgar
	 *
	 */
	class googlyzer_GoogleAuth {
		/*
		 * public variables
		 */
		public $AUTH_TOKEN = 			null;
		public $CAPTCHA_URL =			null;
		public $CAPTCHA_TOKEN = 		null;
		public $EMAIL = 				null;
		public $PASSWORD = 				null;
		public $STATUS = 				null;
		public $STORE = 				null;
		/*
		 * private variables
		 */
		private $captcha_response = 	null;
		private $captcha_token = 		null;
		
		// old style constructor method for backward PHP compatibility
		public function googlyzer_GoogleAuth() {
			$this->__construct();
		}
		
		// public constructor method
		public function __construct() {
			// verify that authentication form has been submitted
			if ( isset( $_POST['GoogleAuthButton'] ) ) {
				// check to see if we're logging in
				if ( $_POST['GoogleAuthButton'] == 'Login' ) {
					// grab login credentials if they exist in $_POST
					if ( array_key_exists( 'ga_email', $_POST ) && array_key_exists( 'ga_password', $_POST ) ) {
						$this->EMAIL = $_POST['ga_email'];
						$this->PASSWORD = $_POST['ga_password'];
						if ( array_key_exists( 'ga_auth_store_enable', $_POST ) ) {
							$this->STORE = $_POST['ga_auth_store_enable'];
						} else {
							$this->STORE = 'disabled';
						}
						if ( array_key_exists( 'captcha_response', $_POST ) && !empty( $_POST['captcha_response'] ) ) {
							$this->captcha_response = $_POST['captcha_response'];
							$this->captcha_token = $_POST['captcha_token'];
						}
						// check to make sure login credentials are not empty strings
						if ( !empty( $this->EMAIL ) && !empty( $this->PASSWORD ) ) {
							// if credentials are supplied, call ClientLogin class
							if ( class_exists( 'googlyzer_ClientLogin' ) ) {
								// instantiate clientLogin feed
								$gaClient = new googlyzer_ClientLogin(
									$feedType =					'login',
									$url =						'https://www.google.com/accounts/ClientLogin',
									$method = 					'POST',
									$feedSpecificSettings = array( 
										'email' =>				$this->EMAIL,
										'passwd' =>				$this->PASSWORD,
										'captcha_response' =>	$this->captcha_response,
										'GA_CAPTCHA_TOKEN' => 	$this->captcha_token
										)
									);
								// start login and process response
								try {
									// initiate login process
									$gaClient->login();
									$this->STATUS = 'Authenticated.';
									$this->AUTH_TOKEN = $gaClient->GA_AUTH_TOKEN;
									// check authorization status and process appropriately
									switch ( $gaClient->GA_AUTH_STATUS ) {
										case 'authorized':
											$this->STATUS = "Authenticated.";
											break;
										case 'curl_error':
											$this->STATUS = "Curl Error: " . $gaClient->getCurlError();
											break;
										case 'captcha_challenge':
											$this->STATUS = "Google Error: " . $gaClient->getGoogleError();
											$this->CAPTCHA_URL = $gaClient->GA_CAPTCHA_URL;
											$this->CAPTCHA_TOKEN = $gaClient->GA_CAPTCHA_TOKEN;
											break;
										case 'google_error':
											$this->STATUS = "Google Error: " . $gaClient->getGoogleError();
											break;
									}
								} catch ( googlyzer_Exception $e ) {
									echo $e->getError();
								}
							} else {
								throw new googlyzer_Exception( 'googlyzer_ClientLogin class not loaded.' );
							}
						} else {
							// alert user to missing login credentials
							$this->STATUS = "Login credentials missing.";
						}
					} else {
						// login credentials were not submitted, or were blank
						$this->STATUS = "Login credentials missing.";
					}
				// otherwise, we're logging out
				} else {
					// set status for logout
					$this->STATUS = 'Logout';
				}
			} else {
				// otherwise, authenticate form not submitted, set status to 'not authenticated'
				$this->STATUS = "Not authenticated.";
			}
		} // end __construct method
		
	} // end class googlyzer_GoogleAuth
	
}

?>