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

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

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

			$db = Zend_Db_Table_Abstract::getDefaultAdapter();

			$db->query('INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES
			("stories_admin_main_manage", "stories", "Manage Stories", "", \'{"route":"admin_default","module":"stories","controller":"manage", "action":"index"}\', "stories_admin_main", "", 2),
			("stories_admin_main_bgtextstories", "stories", "Backgrounds For Text Stories", "", \'{"route":"admin_default","module":"stories","controller":"managestories","action":"index"}\', "stories_admin_main", "", 3);');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_stories_backgrounds` (
				`background_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
				`file_id` int(11) unsigned NOT NULL,
				`order` INT(11) NOT NULL,
				`enabled` TINYINT(1) NOT NULL DEFAULT "1",
				PRIMARY KEY  (`background_id`)
			) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_stories_stories` (
				`story_id` int(11) unsigned NOT NULL auto_increment,  
				`owner_id` int(11) unsigned NOT NULL,
				`type` TINYINT(1) NOT NULL default "0",
				`file_id` int(11) unsigned NOT NULL default "0",
				`title` TEXT NOT NULL,
				`view_count` int(11) unsigned NOT NULL default "0",
				`comment_count` int(11) unsigned NOT NULL default "0",
				`like_count` int(11) unsigned NOT NULL default "0",
				`creation_date` datetime NOT NULL,
				`status` TINYINT(1) NOT NULL DEFAULT "1",
				`code` VARCHAR(16) NOT NULL,
				`duration` INT(9) NOT NULL,
				`rotation` SMALLINT NOT NULL DEFAULT "0",
				`photo_id` INT(11) NOT NULL,
				`resource_type` VARCHAR(64) NOT NULL DEFAULT "user",
				`highlight` TINYINT(1) NOT NULL DEFAULT "0",
				`view_privacy` VARCHAR(255) NOT NULL,
				`background_id` INT(11) NOT NULL DEFAULT "0",
				`story_type` VARCHAR(11) NOT NULL DEFAULT "imagevideo",
				`plateform` TINYINT(1) NOT NULL DEFAULT "1",
				PRIMARY KEY (`story_id`),
				KEY `owner_id` (`owner_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_stories_mutes` (
				`mute_id` int(11) unsigned NOT NULL auto_increment,  
				`user_id` int(11) NOT NULL,
				`resource_id` int(11) NOT NULL,
				`mute` tinyint(1) NOT NULL DEFAULT "0",
				PRIMARY KEY (`mute_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_stories_userinfos` (
				`userinfo_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
				`owner_id` int(11) unsigned NOT NULL,
				`view_privacy` VARCHAR(255) NOT NULL,
				PRIMARY KEY (`userinfo_id`),
				UNIQUE KEY `owner_id` (`owner_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci AUTO_INCREMENT=1;');

			$db->query('CREATE TABLE IF NOT EXISTS  `engine4_stories_recentlyviewitems` (
				`recentlyviewed_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
				`resource_id` INT NOT NULL ,
				`owner_id` INT NOT NULL ,
				`creation_date` DATETIME NOT NULL,
				UNIQUE KEY `uniqueKey` (`resource_id`, `owner_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci AUTO_INCREMENT=1;');

			$db->query('INSERT IGNORE INTO `engine4_activity_notificationtypes` (`type`, `module`, `body`, `is_request`, `handler`) VALUES
			("stories_storycreate", "stories", \'{item:$object} create a story.\', 0, "");');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_stories_usersettings` (
				`usersetting_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
				`user_id` int(11) unsigned NOT NULL,
				`can_comment` tinyint(1) NOT NULL DEFAULT "1",
				`auto_archive` tinyint(1) NOT NULL DEFAULT "1",
				`blocked_friends` varchar(255) NOT NULL,
				UNIQUE KEY `uniqueKey` (`user_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;');

			$db->query('INSERT IGNORE INTO `engine4_core_jobtypes` (`title`, `type`, `module`, `plugin`, `enabled`, `multi`, `priority`) VALUES
			("Story Encode", "stories_encode", "stories", "Stories_Plugin_Job_Encode", 1, 2, 75);');

			// Upload Backgrounds
			$this->uploadBackgrounds();

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

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

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