<?php

/**

 * @package     Joomla.Plugin

 * @subpackage  System.SeersCookieConsent

 *

 * @copyright   Copyright (C) 2009 - 2020 NICK.SPENCER All rights reserved.

 * @license     GNU GPL v3 or later

 **/



/*This program is free software: you can redistribute it and/or modify

it under the terms of the GNU 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 General Public License for more details.



You should have received a copy of the GNU General Public License

along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/



defined('_JEXEC') or die;

use Joomla\Utilities\ArrayHelper;

use Joomla\CMS\Http\HttpFactory;

use Joomla\CMS\Factory;



/**

 * Joomla! SeersCookieConsent plugin.

 *

 * @since  1.5

 */

class PlgSystemSeersCookieConsent extends JPlugin

{

	private $cookieid;



	public function onAfterRender()

	{

		$app =jFactory::getApplication();

		if ($app->isSite() == false)

		{

			return;

		}



		$my_url = $this->params->get('my_url');

		$my_email = $this->params->get('my_email');

		

		$this->cookieid = $this->Seers_API($my_url, $my_email);

		

		$body = $app->getBody();

        $cookie_script = sprintf('<head><script id="SeersCookieConcent" data-key="%s" data-name="CookieXray" src="https://seersco.com/script/cb.js" type="text/javascript"></script>', $this->cookieid);

		$body = str_replace('<head>', $cookie_script , $body);

		$app->setBody($body);

	}

	

	function onContentPrepareForm($form, $data)

	{

		if ($this->params->get('domain_group_id') == ''){



		$uri = JUri::getInstance();

		$my_url = $uri->root();



		$user = Factory::getUser('admin');

		$my_email = $user->get('email');



		$form->setFieldAttribute('my_url', 'default' , $my_url , 'params');

		$form->setFieldAttribute('my_email', 'default' , $my_email  , 'params');





		$my_url = $this->params->get('my_url');

		$my_email = $this->params->get('my_email');

		

		$this->cookieid = $this->Seers_API($my_url, $my_email);



		$form->setFieldAttribute('domain_group_id', 'default' , $this->cookieid  , 'params');

	}



	}



	private function Seers_API ($my_url, $my_email){

		$postData = array(

			'domain'=>$my_url,

			'email'=>$my_email,

			'secret'=>'$2y$10$9ygTfodVBVM0XVCdyzEUK.0FIuLnJT0D42sIE6dIu9r/KY3XaXXyS',

			'platform'=>'joomla',

			'lang' => 'en_GB',

		);

		

		$request_headers = array(

			'Content-Type: application/json',

			'Referer: '.$my_url

		);

		$url = "https://seersco.com/api/save-domain-credentials";

		$postdata = json_encode($postData);



		$ch = curl_init($url);

		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		curl_setopt($ch, CURLOPT_POST, 1);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

		curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);

		$result = curl_exec($ch);

		curl_close($ch);

		

		$response  = json_decode($result,true);

		//$this->cookieid = '';

		if ($response){

			return( $response['key']);

		}else{

			return '';

		}

	}

}

