<?php

/**
 * Contao Open Source CMS
 * Copyright (C) 2005-2011 Leo Feyer
 *
 * Formerly known as TYPOlight Open Source CMS.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 * @copyright  eSales Media 2012 
 * @author     Benjamin Roth <www.esales-media.de> 
 * @package    legalAgeCheck 
 * @license    GNU/LGPL 
 * @filesource
 */

namespace legalAgeCheck;

use Contao\InsertTags;
use Contao\PageModel;
use Contao\LayoutModel;
use Contao\PageRegular;
use Contao\System;

/**
 * Class LegalAgeCheck 
 *
 * @copyright  eSales Media 2012 
 * @author     Benjamin Roth <www.esales-media.de> 
 * @package    Controller
 */
class LegalAgeCheck extends \Frontend
{

	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'agecheck_dialog';


	/**
	 * Perform age check
	 */
	public function performAgeCheck(PageModel $objPage, LayoutModel $objLayout, PageRegular $objPageRegular)
	{
		// Skip age check if passed before
		//if ($this->Session->get('legalAgeCheck_passed'))
		if ($this->Input->cookie('legalAgeCheck_passed') || $objPage->es_ext_agecheck_ignorePage)
		{
			// $this->Session->set('legalAgeCheck_passed', null);
			return;
		}

		// Get root page
		$objRootPage = \PageModel::findByPk($objPage->rootId);
		
		// Cancel verification if age check is disabled					
		if (is_null($objRootPage) || !$objRootPage->es_ext_agecheck || $objRootPage->es_ext_agecheck_exitPage == $objPage->id)
			return;
		
		// Get exit page
		$objExitPage = \PageModel::findWithDetails($objRootPage->es_ext_agecheck_exitPage);
		if (is_null($objExitPage) || !$objExitPage->published)
			return;
		
		// Load dialog template
		$objTemplate = new \FrontendTemplate($this->strTemplate);
		
		// Load modal box template
		//$objModalBoxTemplate = new FrontendTemplate('agecheck_dialog_template');
		
		// Set template vars
		$objTemplate->title = $objRootPage->es_ext_agecheck_title;
		$objTemplate->text = str_replace(array("\r", "\n"),array("\\r","\\n"),$objRootPage->es_ext_agecheck_text);
		$objTemplate->btn_over_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['over_18'];
		$objTemplate->btn_under_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['under_18'];
		$objTemplate->exitPageURL = $this->generateFrontendUrl($objExitPage->row(), null, null, true);
		$objTemplate->commitURL = 'system/modules/legalAgeCheck/ajax/Ajax.php?do=legalage_authentication&legalage_commit='.$this->createToken();
		//$objTemplate->modalBoxTemplate = addcslashes($objModalBoxTemplate->parse(), "\\'\"&\n\r");
		
		// Add dialog code to page
		$GLOBALS['TL_BODY'][] = InsertTags::replaceInsertTags($objTemplate->parse());


	}

	public static function ajaxUnlockPage()
	{
		if (!\Input::get('legalage_commit'))
			return false;
			
		/*if ($this->Input->get('legalage_commit') == $this->Session->get('legalAgeCheck_token'))
		{
			$this->Session->set('legalAgeCheck_passed', true);
			$this->Session->set('legalAgeCheck_token', null);
		}*/

		if (\Input::get('legalage_commit') == $_SESSION['legalAgeCheck_token'])
		{
			//preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', \Environment::get('host'), $regs);
      preg_match('/(?P<subdomain>[a-z0-9][a-z0-9\-]{1,63}|)\.(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', \Environment::get('host'), $regs);
			
			setcookie('legalAgeCheck_passed', true, 0, '/',$regs['subdomain'].'.'.$regs['domain']);
			unset($_SESSION['legalAgeCheck_token']);

			return true;
		}

		return false;
	}
	
	protected function createToken()
	{
		if (!$_SESSION['legalAgeCheck_token'])
		{
			$length = 32;
			$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			$random_string = "";
			for ($p = 0; $p < $length; $p++) {
				$random_string .= $characters[mt_rand(0, strlen($characters))];
			}
			$_SESSION['legalAgeCheck_token'] = $random_string;
		}
		return $_SESSION['legalAgeCheck_token'];
	}
}