<?php

class ControllerSaleBookingAuth extends Controller {
    private $error = array ();
    private $file = '/admin/booking_get_sms.html';
    private $file_set = '/admin/booking_set_sms.html';

    public function index() {
   unset($this->session->data['refer']);
            unset($this->session->data['hotel_id']);
            unset($this->session->data['ses']);
            unset($this->session->data['account_id']);
            unset($this->session->data['dest']);
            unset($this->session->data['enter_security_pin']);
        $user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; (R1 1.5))";
        $user_cookie_file = DIR_CACHE.'cookie_ext.txt';
        if(file_exists($_SERVER['DOCUMENT_ROOT'].$this->file)) {
            $file = file_get_contents($_SERVER['DOCUMENT_ROOT'].$this->file);

            $data['check'] = false;
            $phone_id_sms = '';
            $dest = '';
            $dom = new DOMDocument();
            libxml_use_internal_errors(true);
            $dom->loadHTML($file);
            libxml_use_internal_errors(false);
            $xdom = new DOMXPath($dom);
            $ress = [];
            $page_links = $xdom->query('//form[@id="select_phone_number"]/@action');
            $form_action = '';
            if ($page_links->length) {
                $el = $page_links->item(0);

                $form_action = 'https://admin.booking.com/hotel/hoteladmin/new_location/'.$el->nodeValue;
                $this->session->data['refer'] = $form_action;
                preg_match('/hotel_id=([\w]*).*&ses=([\w]*)/su',$form_action,$ress);


            }

            $page_links = $xdom->query('//select[@name="phone_id_sms"]/option/@value');
            if ($page_links->length) {
                $el = $page_links->item(0);

                $phone_id_sms = $el->nodeValue;

            }

            $page_links = $xdom->query('//input[@name="dest"]/@value');
            if ($page_links->length) {
                $el = $page_links->item(0);
 
                $dest = $el->nodeValue;

            }
            if (!$ch = curl_init()) {
                echo curl_error($ch);
                exit;
            }
            $refer = 'https://admin.booking.com/hotel/hoteladmin/new_location/?dest='.$dest.'&ses='.$ress[2].'&hotel_id='.$ress[1];

            $post_fields = "phone_id_sms=".$phone_id_sms."&message_type=sms&dest=".$dest.'&csrf_token=empty-token&phone_id_call='.$phone_id_sms.'&phone_id='.$phone_id_sms.'&check_pin_auth=&ask_pin=';
            if (!empty($form_action)) {
                if (!$ch = curl_init()) {
                    echo curl_error($ch);
                    exit;
                }

                curl_setopt($ch,CURLOPT_URL,$form_action);
                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,$refer);
                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);

                if ($content) { 
				
                    file_put_contents('booking_set_sms.html',$content);
                }


                $file_set = file_get_contents($_SERVER['DOCUMENT_ROOT'].$this->file_set);

                libxml_use_internal_errors(true);
                $dom->loadHTML($file_set);
                libxml_clear_errors();
                $xdom = new DOMXPath($dom);
                $page_links = $xdom->query('//form[@id="enter_security_pin"]/@action');
                if ($page_links->length) {
                    $el = $page_links->item(0);
                    $this->session->data['enter_security_pin'] = $el->nodeValue;
                }

                $page_links = $xdom->query('//input[@name="hotel_id"]/@value');
                if ($page_links->length) {
                    $el = $page_links->item(0);
                    $this->session->data['hotel_id'] = $el->nodeValue;
                }

                $page_links = $xdom->query('//input[@name="ses"]/@value');
                if ($page_links->length) {
                    $el = $page_links->item(0);
                    $this->session->data['ses'] = $el->nodeValue;
                }

                $page_links = $xdom->query('//input[@name="account_id"]/@value');
                if ($page_links->length) {
                    $el = $page_links->item(0);
                    $this->session->data['account_id'] = $el->nodeValue;
                }

                $page_links = $xdom->query('//input[@name="dest"]/@value');
                if ($page_links->length) {
                    $el = $page_links->item(0);
                    $this->session->data['dest'] = $el->nodeValue;
                }
                if (!empty($this->session->data['enter_security_pin'])) {
                    $data['check'] = true;
                }
            }
            $data['heading_title'] = "Букинг Авторизация";
            $data['breadcrumbs'] = array ();

            $data['breadcrumbs'][] = array (
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/dashboard','token='.$this->session->data['token'],true)
            );

            $data['breadcrumbs'][] = array (
                'text' => "Букинг Авторизация",
                'href' => $this->url->link('sale/booking_auth','token='.$this->session->data['token'],true)
            );


            $data['check_file'] = true;
            $data['token'] = $this->session->data['token'];

            $data['header'] = $this->load->controller('common/header');
            $data['column_left'] = $this->load->controller('common/column_left');
            $data['footer'] = $this->load->controller('common/footer');
            $this->response->setOutput($this->load->view('sale/booking_auth',$data));
        } else {      $data['token'] = $this->session->data['token'];
            $data['heading_title'] = "Букинг Авторизация";
            $data['breadcrumbs'] = array ();

            $data['breadcrumbs'][] = array (
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/dashboard','token='.$this->session->data['token'],true)
            );

            $data['breadcrumbs'][] = array (
                'text' => "Букинг Авторизация",
                'href' => $this->url->link('sale/booking_auth','token='.$this->session->data['token'],true)
            );
            $data['token'] = $this->session->data['token'];
            $data['check_file'] = false;
            $data['header'] = $this->load->controller('common/header');
            $data['column_left'] = $this->load->controller('common/column_left');
            $data['footer'] = $this->load->controller('common/footer');
            $this->response->setOutput($this->load->view('sale/booking_auth',$data));
        }
    }

    public function sendCode() {
        if (isset($this->request->post['code']) and !empty($this->request->post['code'])) {
            $code = $this->request->post['code'];
            $user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; (R1 1.5))";
            $user_cookie_file = DIR_CACHE.'cookie_ext.txt';
            $refer = $this->session->data['refer'];
			$post_fields  = 'ask_pin='.$code;
            $post_fields .= '&hotel_id='.$this->session->data['hotel_id'];
            $post_fields .= '&ses='.$this->session->data['ses'];
            $post_fields .= '&account_id='.$this->session->data['account_id'];
            $post_fields .= '&dest='.$this->session->data['dest'];
            $post_fields .= '&check_pin_auth=';
            $post_fields .= '&csrf_token:empty-token';
            if (!$ch = curl_init()) {
                echo curl_error($ch);
                exit;
            }
			file_put_contents('bookingparams.txt',$post_fields);
            curl_setopt($ch,CURLOPT_URL,'https://admin.booking.com/hotel/hoteladmin/new_location/confirm.html');
            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,$refer);
            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);
			file_put_contents('booking_send_sms.html',$content);
            curl_close($ch);
            unlink($_SERVER['DOCUMENT_ROOT'].$this->file);
            unlink($_SERVER['DOCUMENT_ROOT'].$this->file_set);

            unset($this->session->data['refer']);
            unset($this->session->data['hotel_id']);
            unset($this->session->data['ses']);
            unset($this->session->data['account_id']);
            unset($this->session->data['dest']);
            unset($this->session->data['enter_security_pin']);

          
        }
    }

}
