<?php
/**
 * Description of booking
 *
 * @author Igor Zabuga (igor.zabuga@gmail.com)
 */
class ModelExtensionModuleBooking extends Model {
	public function login() {
		$this->load->model('setting/setting');
		$login             = $this->config->get('booking_login');
		$pass              = $this->config->get('booking_pass');
		$login_url         = ''; 
		$set_url           = '';
		$ref_url           = '';
		$login_var_name    = 'loginname'; 
		$password_var_name = 'password'; 
		$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; (R1 1.5))"; 
		$phone_id = '';
		$user_cookie_file  = DIR_CACHE . 'cookie_ext.txt'; 		
		$request = [];
		if (!$ch = curl_init()) 
		{ 
			echo curl_error($ch); 
			exit; 
		}   
		$post_fields = "$login_var_name=$login&$password_var_name=$pass"; 

		curl_setopt($ch, CURLOPT_URL, $login_url);    
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_VERBOSE, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
		curl_setopt($ch, CURLOPT_REFERER, $login_url); 
		curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie_file); 
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $user_cookie_file);  
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 

		$content = curl_exec($ch); 
		curl_close($ch); 

		$pattern = '/data-ses="(.[0-9a-zA-Z]*)"/is';
		preg_match($pattern, $content, $res);

		if (isset($res[1])){
			$params = '&ses='.$res[1].'&hotel_id='.$login;

			if (!$ch = curl_init()) 
			{ 
				echo curl_error($ch); 
				exit; 
			}   

			$set_url = 'ht?hotel_id='.$login.'&ses='.$res[1];
			$post_array = [
				"dest=%2Fhotel%2Fhoteladmin%2Fextranet_ng%2Fmanage%2Fnew_and_updated_reservations.html%3Fhotel_id%3D%26lang%3Dru",
				"phone_id=",
				"message_type=sms",
				"phone_id_call=",
				"phone_id_sms=",
				"check_pin_auth=",
				"ask_pin="
			];

			$post_fields = implode('&', $post_array);

			curl_setopt($ch, CURLOPT_URL, $set_url);    
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
			curl_setopt($ch, CURLOPT_VERBOSE, 1); 
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
			curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
			curl_setopt($ch, CURLOPT_REFERER, $ref_url.''.$params); 
			curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie_file); 
			curl_setopt($ch, CURLOPT_COOKIEJAR,  $user_cookie_file);  
			curl_setopt($ch, CURLOPT_POST, 1); 
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 

			$content = curl_exec($ch); 
			echo $content;
			$request[] = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
			curl_close($ch); 

		}
		
		return $res[1];
	}
	
	public function auth($ses, $auth) {
		$this->load->model('setting/setting');
		$login             = $this->config->get('booking_login');
		$pass              = $this->config->get('booking_pass');
		$login_url         = ''; 
		$set_url           = '';
		$ref_url           = '';
		$login_var_name    = 'loginname'; 
		$password_var_name = 'password'; 
		$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; (R1 1.5))"; 
		$phone_id = '';
		$user_cookie_file  = DIR_CACHE . 'cookie_ext.txt'; 		
		$params = '&ses='.$ses.'&hotel_id='.$login;

		if (!$ch = curl_init()) 
		{ 
			echo curl_error($ch); 
			exit; 
		}   

		$set_url = '';
		$post_array = [
			"dest=%2Fhotel%2Fhoteladmin%2Fextranet_ng%2Fmanage%2Fnew_and_updated_reservations.html%3Fhotel_id%3D%26lang%3Dru",
			"ask_pin=".$auth,
			"account_id=",
			"check_pin_auth=",
			"hotel_id=".$login,
			"ses=".$ses
		];

		$post_fields = implode('&', $post_array);

		curl_setopt($ch, CURLOPT_URL, $set_url);    
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_VERBOSE, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
		curl_setopt($ch, CURLOPT_REFERER, $ref_url.''.$params); 
		curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie_file); 
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $user_cookie_file);  
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 

		$content = curl_exec($ch); 
		echo $content;
		$request[] = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
		curl_close($ch); 
		
	}
}