<?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['browserpush_licensekey'];

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

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

			$db = Zend_Db_Table_Abstract::getDefaultAdapter();

			$db->query('INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES
			("browserpush_admin_main_fbsettings", "browserpush", "API Settings", "", \'{"route":"admin_default","module":"browserpush","controller":"settings","action":"fb-settings"}\', "browserpush_admin_main", "", 2),
			("browserpush_admin_main_welcome", "browserpush", "Welcome Notification", "", \'{"route":"admin_default","module":"browserpush","controller":"settings","action":"welcome"}\', "browserpush_admin_main", "", 3),
			("browserpush_admin_main_sendnoti", "browserpush", "Send Notifications", "", \'{"route":"admin_default","module":"browserpush","controller":"settings","action":"notification"}\', "browserpush_admin_main", "", 4),
			("browserpush_admin_main_scheduled", "browserpush", "Schedule Notifications", "", \'{"route":"admin_default","module":"browserpush","controller":"scheduled","action":"index"}\', "browserpush_admin_main", "", 5),
			("browserpush_admin_main_sentscheduled", "browserpush", "Sent Notifications", "", \'{"route":"admin_default","module":"browserpush","controller":"scheduled","action":"sent"}\', "browserpush_admin_main", "", 6),
			("browserpush_admin_main_managesubscriber", "browserpush", "Manage Subscribers", "", \'{"route":"admin_default","module":"browserpush","controller":"settings","action":"subscriber"}\', "browserpush_admin_main", "", 7),
			("browserpush_admin_main_reports", "browserpush", "Report & Statistics", "", \'{"route":"admin_default","module":"browserpush","controller":"settings","action":"reports"}\', "browserpush_admin_main", "", 8);');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_browserpush_notifications` (
				`notification_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
				`access_token` VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT "",
				`user_id` INT(11) UNSIGNED NOT NULL,
				`scheduled_id` INT(11) UNSIGNED NOT NULL,
				`param` TINYINT(1) DEFAULT "1",
				`creation_date` DATETIME DEFAULT NULL,
				PRIMARY KEY (`notification_id`),
				KEY `user_id` (`user_id`),
				KEY `scheduled_id` (`scheduled_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_browserpush_tokens` (
				`token_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
				`access_token` VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT "",
				`user_id` INT(11) UNSIGNED NOT NULL,
				`browser` VARCHAR(100) DEFAULT NULL,
				`test_user` tinyint(1) DEFAULT "0",
				`ip_address` VARCHAR(45) DEFAULT NULL,
				`creation_date` DATETIME DEFAULT NULL,
				`user_agent` VARCHAR(32) NULL,
				PRIMARY KEY (`token_id`),
				KEY `browser` (`browser`),
				KEY `user_agent` (`user_agent`),
				UNIQUE KEY `access_token` (`access_token`,`user_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_browserpush_scheduleds` (
				`scheduled_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
				`title` VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT "",
				`description` TEXT DEFAULT NULL,
				`file_id` INT(11) DEFAULT NULL,
				`criteria` VARCHAR(244) DEFAULT NULL,
				`param` TEXT DEFAULT "",
				`sent` TINYINT(1) DEFAULT "0",
				`link` VARCHAR(255) DEFAULT NULL,
				`scheduled_time` VARCHAR(255) NOT NULL,
				`creation_date` DATETIME DEFAULT NULL,
				PRIMARY KEY (`scheduled_id`),
				KEY `criteria` (`criteria`),
				KEY `sent` (`sent`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;');

			$db->query('INSERT IGNORE INTO `engine4_core_tasks` (`title`, `module`, `plugin`, `timeout`, `processes`, `semaphore`, `started_last`, `started_count`, `completed_last`, `completed_count`, `failure_last`, `failure_count`, `success_last`, `success_count`) VALUES ("Scheduled Browser Notification", "browserpush", "Browserpush_Plugin_Task_Jobs", 20, 1, 0,"", "", "", "", 0, 0, "", "");');

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

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

		$settings->setSetting('browserpush.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('browserpush.licensekey', $_POST['browserpush_licensekey']);
		return;
		$this->_helper->redirector->gotoRoute(array());
	}
}