<?php
class ControllerToolEmail extends Controller {
	public function index() {
		
		/*
		 * 
CREATE TABLE `brech`.`oc_product_import` 
		  ( 
			`ref_id` INT(11) NOT NULL ,
			`product_id` INT(11) NOT NULL , 
			`referens_id` INT(11) NOT NULL , 
			`room_id` INT(11) NOT NULL , 
			`customer` VARCHAR(80) NOT NULL , 
			`date_in` DATE NOT NULL , 
			`date_out` DATE NOT NULL , 
			`guests` INT NOT NULL , 
			`rooms` INT NOT NULL , 
			`price` INT NOT NULL ,
		    `status` VARCHAR(20) NOT NULL ,
			`processed` INT NOT NULL ,
		    `resseller` VARCHAR(20) NOT NULL 
		 ) ENGINE = MyISAM;		 
ALTER TABLE `oc_product_import` ADD INDEX(`product_id`);	
ALTER TABLE `oc_product_import` ADD PRIMARY KEY(`ref_id`);
ALTER TABLE `oc_product_import` CHANGE `ref_id` `ref_id` INT(11) NOT NULL AUTO_INCREMENT;
		 * 		 * 
		 *  	 
		 *  
		 *
		 *  */
		
		
//		 */5 * * * * wget -O /dev/null "http://brech.home/index.php?route=tool/email"
		$log = new Log('sync.log');
		$message = " =========== Read Email START! ============";
		$log->write($message);

		# goggle with pop3 or imap
		#$authhost="{pop.gmail.com:995/pop3/ssl/novalidate-cert}";
		$authhost="{imap.gmail.com:993/imap/ssl/novalidate-cert}";
		# $user="username@gmail.com";

		$user="brech.eko@gmail.com";
		$pass="3n3H40HXXz";

		$this->load->model('tool/sinc');

		if ($mbox=imap_open( $authhost, $user, $pass )) {
			$message = 'Connected';
			$log->write($message);
			
			$mails = imap_search($mbox, 'FROM "Booking.com" SINCE "'.date("Y-m-d", strtotime('-7 days')).'"');
			$results = array();

			if ($mails) {
				foreach ($mails as $mail) {
					$header = imap_headerinfo($mbox, $mail);
					$pattern_b = '/Booking.com - (\w+) booking([\w\s!-]+) \((\d+), (\w+), ([\w ]+)/';
					$pattern_h = '/Акт Заселения №(\w+)([\w\s!-]+)/';
					$result = array();
					
					if (preg_match($pattern_b, $header->subject, $result)) {
						if (!$this->model_tool_sinc->getReferens($result[1], $result[3])) {
							$message = $result[1].' '.$result[3].' '.$result[5];
							$log->write($message);
							$tmp = imap_fetchbody($mbox, $mail, 2.1);
							$tmp = base64_decode($tmp);
							$pattern = '/.*&amp;_e=([\d]*)&amp;_s=(.*)"/';
							$code = array();
							preg_match($pattern, $tmp, $code);
							$results['booking'][] = [
								'status' => $result[1],
								'referens_id' => $result[3],
								'date_add' => strftime('%F',strtotime($result[5])),
							];
						}
					} 
				}
			}

			$mails = imap_search($mbox, 'FROM "hotels24.ua" SINCE "'.date("Y-m-d").'"');
			if ($mails) {
				foreach ($mails as $mail) {
					$header = imap_headerinfo($mbox, $mail);
					$header = imap_utf8($header->subject);
					$pattern_h = '/Акт Заселения No(\w+)([\w\s!-]+)/is';
					$result = array();
					if (preg_match($pattern_h, $header, $result)) {
						$message = $result[0];
						$log->write($message);
						$results['hotels24'][] = [ 'referens_id' => $result[1] ];
					}
				}
			}
			
			var_dump($results);
			if (!empty($results['booking'])) { 
				$this->model_tool_sinc->addReferensBooking($results['booking']);
			}
			if (!empty($results['hotels24'])) { 
				$this->model_tool_sinc->addReferensHotels24($results['hotels24']);
			}
			imap_close($mbox);
		} else {
			$message = 'FAIL!';
			$log->write($message);
		}

		
		$message = " =========== Read Email END! ==============";
		$log->write($message);
		
	}

}