<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

if ($this->getRequest()->isPost()) {

	$settings = Engine_Api::_()->getApi('settings', 'core');
	$coreApi  = Engine_Api::_()->core();

	//Domain Name
	if (engine_in_array($_SERVER['SERVER_NAME'], ['localhost', '127.0.0.1'])) {
		$domainName = $_POST['domain_name'];
	} else {
		$domainName = str_replace(array('http://', 'https://', 'www.'), array('', '', ''), $_SERVER['HTTP_HOST']);
	}

	//License Key
	$licenseKey = $values['gdpr_licensekey'];

	//Verify License Key
	$valid = Engine_Api::_()->getApi('verify', 'core')->verifyKey($licenseKey, $domainName);

	if ($valid['status'] == 'Active') {
		if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('gdpr.pluginactivated')) {

			$db = Zend_Db_Table_Abstract::getDefaultAdapter();

			$db->query('INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES
			("gdpr_admin_main_cookir", "gdpr", "Cookies Settings", "", \'{"route":"admin_default","module":"gdpr","controller":"settings","action":"cookie"}\', "gdpr_admin_main", "", 2),
			("gdpr_admin_main_request", "gdpr", "DPO Requests", "", \'{"route":"admin_default","module":"gdpr","controller":"settings","action":"dpo"}\', "gdpr_admin_main", "", 3),
			("gdpr_admin_main_privacyse", "gdpr", "Privacy Settings", "", \'{"route":"admin_default","module":"gdpr","controller":"settings","action":"privacy"}\', "gdpr_admin_main", "", 4),
			("gdpr_admin_main_accessrequest", "gdpr", "Subject Access Requests", "", \'{"route":"admin_default","module":"gdpr","controller":"settings","action":"subject-access"}\', "gdpr_admin_main", "", 5),
			("gdpr_admin_main_unsubscribe", "gdpr", "Unsubscribe", "", \'{"route":"admin_default","module":"gdpr","controller":"settings","action":"unsubscribe"}\', "gdpr_admin_main", "", 6),
			("gdpr_admin_main_forgotme", "gdpr", "Forget Me", "", \'{"route":"admin_default","module":"gdpr","controller":"settings","action":"forgot-me"}\', "gdpr_admin_main", "", 7),
			("gdpr_admin_main_audit", "gdpr", "Audit Log", "", \'{"route":"admin_default","module":"gdpr","controller":"settings","action":"audit-log"}\', "gdpr_admin_main", "", 8);');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_gdpr_contents` (
				`content_id` int(11) unsigned NOT NULL auto_increment,
				`user_id` varchar(128) NOT NULL,
				`name` VARCHAR(255) NOT NULL,
				`email` VARCHAR(255) NOT NULL,
				`message` TEXT NOT NULL,
				`type` VARCHAR(36) NOT NULL,
				`note` TEXT NULL,
				`replied` TINYINT(1) NOT NULL DEFAULT "0",
				`creation_date` datetime NOT NULL,
				`modified_date` datetime NOT NULL,
				PRIMARY KEY (`content_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_gdpr_services` (
				`service_id` int(11) unsigned NOT NULL auto_increment,
				`name` VARCHAR(255) NOT NULL,
				`url` VARCHAR(255) NOT NULL,
				`description` TEXT NOT NULL,
				`enabled` tinyint(1) NOT NULL DEFAULT "0",
				`creation_date` datetime NOT NULL,
				`modified_date` datetime NOT NULL,
				PRIMARY KEY (`service_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;');
			
			$db->query('CREATE TABLE IF NOT EXISTS `engine4_gdpr_audits` (
				`audit_id` int(11) unsigned NOT NULL auto_increment,
				`email` VARCHAR(255) NOT NULL,
				`description` TEXT(255) NOT NULL,
				`creation_date` datetime NOT NULL,
				`modified_date` datetime NOT NULL,
				PRIMARY KEY (`audit_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;');

			$db->query('INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES ("gdpr_footer_consent", "gdpr", "Privacy Center", "", \'{"route":"gdpr_view","module":"gdpr"}\', "core_footer", "", "3");');

			$db->query('INSERT IGNORE INTO `engine4_core_mailtemplates` (`type`, `module`, `vars`) VALUES
			("gdpr_admin_reply", "gdpr", "[host],[email],[recipient_title],[recipient_link],[recipient_photo],[host],[object_link],[subject],[body]"),
			("gdpr_consent_user", "gdpr", "[host],[email],[recipient_title],[recipient_link],[recipient_photo],[host],[object_link],[subject],[body]");');

			$db->query('INSERT IGNORE INTO `engine4_gdpr_services` ( `name`, `url`, `description`, `enabled`, `creation_date`, `modified_date`) VALUES
			("Facebook", "https://www.facebook.com/", "We currently use Facebook to login on our website.", 1, NOW(), NOW()),
			("Youtube", "https://www.youtube.com/", "We currently allow you to create videos from Youtube and view them on our site.", 1, NOW(), NOW()),
			("X", "https://www.x.com", "We currently use X to login on our website.", 1, NOW(), NOW());');

			$settings->setSetting('gdpr.licensekey', $licenseKey);
			$settings->setSetting('core.license.domain', $domainName);
			$settings->setSetting('gdpr.pluginactivated', 1);
		}

		// Update cached status
		$cachedState = isset($valid['status']) ? $valid['status'] : 'Inactive';
		$settings->setSetting('gdpr.license.cachedstatus', $cachedState);
		$settings->setSetting('gdpr.license.lastcheck', time());

		$settings->setSetting('gdpr.license.keyauth', @base64_encode($licenseKey));
		$settings->setSetting('core.license.domainauth', @base64_encode($domainName));
		$error = 1;
	} else {
		$error = $this->view->translate('Please enter correct License key for this product.');
		$error = Zend_Registry::get('Zend_Translate')->_($error);
		$form->getDecorator('errors')->setOption('escape', false);
		$form->addError($error);
		$error = 0;
		$settings->setSetting('gdpr.licensekey', $_POST['gdpr_licensekey']);
		return;
		$this->_helper->redirector->gotoRoute(array());
	}
}