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

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

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

			$db = Zend_Db_Table_Abstract::getDefaultAdapter();

			$db->query('INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES
			("twitterclone_admin_main_footer", "twitterclone", "Footer Settings", "", \'{"route":"admin_default","module":"twitterclone","controller":"settings", "action":"footer"}\', "twitterclone_admin_main", "", 2),
			("twitterclone_admin_main_styling", "twitterclone", "Color Schemes", "", \'{"route":"admin_default","module":"twitterclone","controller":"settings", "action":"styling"}\', "twitterclone_admin_main", "", 3),
			("twitterclone_admin_main_managefonts", "twitterclone", "Manage Fonts", "", \'{"route":"admin_default","module":"twitterclone","controller":"settings", "action":"manage-fonts"}\', "twitterclone_admin_main", "", 4),
			("twitterclone_admin_main_customcss", "twitterclone", "Custom CSS", "", \'{"route":"admin_default","module":"twitterclone","controller":"custom-theme", "action":"index"}\', "twitterclone_admin_main", "", 5),
			("twitterclone_admin_main_dashborad", "twitterclone", "Manage Sidebar Dashboard", "", \'{"route":"admin_default","module":"twitterclone","controller":"manage", "action":"dashboard-links"}\', "twitterclone_admin_main", "", 6);');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_twitterclone_dashboardlinks` (
				`dashboardlink_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
				`name` varchar(255) NOT NULL,
				`url` varchar(255) NOT NULL,
				`enabled` tinyint(1) NOT NULL DEFAULT "1",
				`sublink` INT(1) NOT NULL DEFAULT "0",
				`photo` VARCHAR(255) NULL DEFAULT NULL,
				`icon_type` TINYINT(1) NOT NULL DEFAULT "0",
				`font_icon` VARCHAR(64) NOT NULL,
				`fonticoncolor` VARCHAR(16) NOT NULL,
				`order` INT(11) NOT NULL,
				`type` VARCHAR(32) NULL,
				PRIMARY KEY (`dashboardlink_id`)
			) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci AUTO_INCREMENT=1;');
			
			$db->query('INSERT IGNORE INTO `engine4_twitterclone_dashboardlinks` (`dashboardlink_id`, `name`, `url`, `enabled`, `sublink`, `photo`, `icon_type`, `font_icon`, `fonticoncolor`, `order`, `type`) VALUES
			(1, "Explore", "", 1, 0, NULL, 0, "", "", 0, "vertical"),
			(2, "Home", "", 1, 1, NULL, 1, "fi fi-rr-home", "#E6204F", 6, NULL),
			(3, "Members", "members", 1, 1, NULL, 1, "fi fi-rr-users-alt", "#E64919", 5, NULL),
			(4, "Blogs", "blogs", 1, 1, NULL, 1, "fi fi-rr-blog-text", "#DE1486", 4, NULL),
			(5, "Albums", "albums", 1, 1, NULL, 1, "fi fi-rr-picture", "#51CF8B", 3, NULL),
			(6, "Videos", "videos", 1, 1, NULL, 1, "fi fi-rr-play-alt", "#413482", 2, NULL);');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_twitterclone_customthemes` (
				`customtheme_id` int(11) NOT NULL AUTO_INCREMENT,
				`name` varchar(128) NOT NULL,
				`value` varchar(32) NOT NULL,
				`column_key` varchar(128) NOT NULL,
				`theme_id` int(11) NOT NULL,
				`default` TINYINT(1) NOT NULL DEFAULT "0" ,
				PRIMARY KEY (`customtheme_id`),
				UNIQUE KEY `UNIQUEKEY` (`column_key`,`theme_id`,`default`)
			) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;');

			$db->query('INSERT IGNORE INTO `engine4_twitterclone_customthemes` (`name`, `value`, `column_key`, `theme_id`, `default`) VALUES
			("Theme - 1", "1", "theme_color", 1, 0),
			("Theme - 1", "1", "custom_theme_color", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_header_background_color", 1, 0),
			("Theme - 1", "#0F1419", "twitterclone_header_logo_color", 1, 0),
			("Theme - 1", "#F7F9FA", "twitterclone_minimenu_search_background_color", 1, 0),
			("Theme - 1", "#536471", "twitterclone_minimenu_search_font_color", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_mainmenu_background_color", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_topbar_menu_link_background_color", 1, 0),
			("Theme - 1", "#f7f9fa", "twitterclone_topbar_menu_link_background_color_hover", 1, 0),
			("Theme - 1", "#0F1419", "twitterclone_topbar_menu_link_color", 1, 0),
			("Theme - 1", "#0F1419", "twitterclone_topbar_menu_link_hover_color", 1, 0),
			("Theme - 1", "#536471", "twitterclone_topbar_menu_footer_color", 1, 0),
			("Theme - 1", "#EBEEF0", "twitterclone_topbar_menu_border", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_minimenu_background_color", 1, 0),
			("Theme - 1", "#0F1419", "twitterclone_minimenu_hover_background_color", 1, 0),
			("Theme - 1", "#0F1419", "twitterclone_mininmenu_links_color", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_mininmenu_links_hover_color", 1, 0),
			("Theme - 1", "#0F1419", "twitterclone_topbar_minimenu_border_color", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_footer_background_color", 1, 0),
			("Theme - 1", "#536471", "twitterclone_footer_font_color", 1, 0),
			("Theme - 1", "#536471", "twitterclone_footer_links_color", 1, 0),
			("Theme - 1", "#EBEEF0", "twitterclone_footer_border_color", 1, 0),
			("Theme - 1", "#1D9BF0", "twitterclone_theme_color", 1, 0),
			("Theme - 1", "#F7F9FA", "twitterclone_body_background_color", 1, 0),
			("Theme - 1", "#0F1419", "twitterclone_font_color", 1, 0),
			("Theme - 1", "#536471", "twitterclone_font_color_light", 1, 0),
			("Theme - 1", "#1D9BF0", "twitterclone_links_color", 1, 0),
			("Theme - 1", "#1383cf", "twitterclone_links_hover_color", 1, 0),
			("Theme - 1", "#000000", "twitterclone_headline_color", 1, 0),
			("Theme - 1", "#EBEEF0", "twitterclone_border_color", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_box_background_color", 1, 0),
			("Theme - 1", "#f7f7f7", "twitterclone_box_background_color_alt", 1, 0),
			("Theme - 1", "#677681", "twitterclone_form_label_color", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_input_background_color", 1, 0),
			("Theme - 1", "#536471", "twitterclone_input_font_color", 1, 0),
			("Theme - 1", "#D9D9D9", "twitterclone_input_border_color", 1, 0),
			("Theme - 1", "#1D9BF0", "twitterclone_button_background_color", 1, 0),
			("Theme - 1", "#1383cf", "twitterclone_button_background_color_hover", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_button_font_color", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_button_font_color_hover", 1, 0),
			("Theme - 1", "#1D9BF0", "twitterclone_button_border_color", 1, 0),
			("Theme - 1", "#1383cf", "twitterclone_button_border_color_hover", 1, 0),
			("Theme - 1", "#f2f2f2", "twitterclone_secondary_button_background_color", 1, 0),
			("Theme - 1", "#e5e5e5", "twitterclone_secondary_button_background_color_hover", 1, 0),
			("Theme - 1", "#212529", "twitterclone_secondary_button_font_color", 1, 0),
			("Theme - 1", "#212529", "twitterclone_secondary_button_font_color_hover", 1, 0),
			("Theme - 1", "#E4E6EB", "twitterclone_secondary_button_border_color", 1, 0),
			("Theme - 1", "#d8dadf", "twitterclone_secondary_button_border_color_hover", 1, 0),
			("Theme - 1", "#FFFFFF", "twitterclone_comments_background_color", 1, 0),
			("Theme - 2", "2", "theme_color", 2, 0),
			("Theme - 2", "2", "custom_theme_color", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_header_background_color", 2, 0),
			("Theme - 2", "#0F1419", "twitterclone_header_logo_color", 2, 0),
			("Theme - 2", "#F7F9FA", "twitterclone_minimenu_search_background_color", 2, 0),
			("Theme - 2", "#536471", "twitterclone_minimenu_search_font_color", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_mainmenu_background_color", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_topbar_menu_link_background_color", 2, 0),
			("Theme - 2", "#f7f9fa", "twitterclone_topbar_menu_link_background_color_hover", 2, 0),
			("Theme - 2", "#0F1419", "twitterclone_topbar_menu_link_color", 2, 0),
			("Theme - 2", "#0F1419", "twitterclone_topbar_menu_link_hover_color", 2, 0),
			("Theme - 2", "#536471", "twitterclone_topbar_menu_footer_color", 2, 0),
			("Theme - 2", "#EBEEF0", "twitterclone_topbar_menu_border", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_minimenu_background_color", 2, 0),
			("Theme - 2", "#0F1419", "twitterclone_minimenu_hover_background_color", 2, 0),
			("Theme - 2", "#0F1419", "twitterclone_mininmenu_links_color", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_mininmenu_links_hover_color", 2, 0),
			("Theme - 2", "#0F1419", "twitterclone_topbar_minimenu_border_color", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_footer_background_color", 2, 0),
			("Theme - 2", "#536471", "twitterclone_footer_font_color", 2, 0),
			("Theme - 2", "#536471", "twitterclone_footer_links_color", 2, 0),
			("Theme - 2", "#EBEEF0", "twitterclone_footer_border_color", 2, 0),
			("Theme - 2", "#000000", "twitterclone_theme_color", 2, 0),
			("Theme - 2", "#F7F9FA", "twitterclone_body_background_color", 2, 0),
			("Theme - 2", "#0F1419", "twitterclone_font_color", 2, 0),
			("Theme - 2", "#536471", "twitterclone_font_color_light", 2, 0),
			("Theme - 2", "#ff7a00", "twitterclone_links_color", 2, 0),
			("Theme - 2", "#d56600", "twitterclone_links_hover_color", 2, 0),
			("Theme - 2", "#000000", "twitterclone_headline_color", 2, 0),
			("Theme - 2", "#EBEEF0", "twitterclone_border_color", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_box_background_color", 2, 0),
			("Theme - 2", "#f7f7f7", "twitterclone_box_background_color_alt", 2, 0),
			("Theme - 2", "#677681", "twitterclone_form_label_color", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_input_background_color", 2, 0),
			("Theme - 2", "#536471", "twitterclone_input_font_color", 2, 0),
			("Theme - 2", "#D9D9D9", "twitterclone_input_border_color", 2, 0),
			("Theme - 2", "#ff7a00", "twitterclone_button_background_color", 2, 0),
			("Theme - 2", "#d56600", "twitterclone_button_background_color_hover", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_button_font_color", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_button_font_color_hover", 2, 0),
			("Theme - 2", "#ff7a00", "twitterclone_button_border_color", 2, 0),
			("Theme - 2", "#d56600", "twitterclone_button_border_color_hover", 2, 0),
			("Theme - 2", "#f2f2f2", "twitterclone_secondary_button_background_color", 2, 0),
			("Theme - 2", "#e5e5e5", "twitterclone_secondary_button_background_color_hover", 2, 0),
			("Theme - 2", "#212529", "twitterclone_secondary_button_font_color", 2, 0),
			("Theme - 2", "#212529", "twitterclone_secondary_button_font_color_hover", 2, 0),
			("Theme - 2", "#E4E6EB", "twitterclone_secondary_button_border_color", 2, 0),
			("Theme - 2", "#d8dadf", "twitterclone_secondary_button_border_color_hover", 2, 0),
			("Theme - 2", "#FFFFFF", "twitterclone_comments_background_color", 2, 0),
			("Theme - 3", "3", "theme_color", 3, 0),
			("Theme - 3", "3", "custom_theme_color", 3, 0),
			("Theme - 3", "#000000", "twitterclone_header_background_color", 3, 0),
			("Theme - 3", "#FFFFFF", "twitterclone_header_logo_color", 3, 0),
			("Theme - 3", "#272727", "twitterclone_minimenu_search_background_color", 3, 0),
			("Theme - 3", "#E6E6E6", "twitterclone_minimenu_search_font_color", 3, 0),
			("Theme - 3", "#000000", "twitterclone_mainmenu_background_color", 3, 0),
			("Theme - 3", "#000000", "twitterclone_topbar_menu_link_background_color", 3, 0),
			("Theme - 3", "#181818", "twitterclone_topbar_menu_link_background_color_hover", 3, 0),
			("Theme - 3", "#E8E8E8", "twitterclone_topbar_menu_link_color", 3, 0),
			("Theme - 3", "#E8E8E8", "twitterclone_topbar_menu_link_hover_color", 3, 0),
			("Theme - 3", "#E0E0E0", "twitterclone_topbar_menu_footer_color", 3, 0),
			("Theme - 3", "#2F3336", "twitterclone_topbar_menu_border", 3, 0),
			("Theme - 3", "#000000", "twitterclone_minimenu_background_color", 3, 0),
			("Theme - 3", "#363636", "twitterclone_minimenu_hover_background_color", 3, 0),
			("Theme - 3", "#E8E8E8", "twitterclone_mininmenu_links_color", 3, 0),
			("Theme - 3", "#FFFFFF", "twitterclone_mininmenu_links_hover_color", 3, 0),
			("Theme - 3", "#E8E8E8", "twitterclone_topbar_minimenu_border_color", 3, 0),
			("Theme - 3", "#000000", "twitterclone_footer_background_color", 3, 0),
			("Theme - 3", "#E8E8E8", "twitterclone_footer_font_color", 3, 0),
			("Theme - 3", "#E8E8E8", "twitterclone_footer_links_color", 3, 0),
			("Theme - 3", "#2F3336", "twitterclone_footer_border_color", 3, 0),
			("Theme - 3", "#1D9BF0", "twitterclone_theme_color", 3, 0),
			("Theme - 3", "#151515", "twitterclone_body_background_color", 3, 0),
			("Theme - 3", "#E8E8E8", "twitterclone_font_color", 3, 0),
			("Theme - 3", "#E0E0E0", "twitterclone_font_color_light", 3, 0),
			("Theme - 3", "#1D9BF0", "twitterclone_links_color", 3, 0),
			("Theme - 3", "#1584cf", "twitterclone_links_hover_color", 3, 0),
			("Theme - 3", "#E8E8E8", "twitterclone_headline_color", 3, 0),
			("Theme - 3", "#2F3336", "twitterclone_border_color", 3, 0),
			("Theme - 3", "#222222", "twitterclone_box_background_color", 3, 0),
			("Theme - 3", "#181818", "twitterclone_box_background_color_alt", 3, 0),
			("Theme - 3", "#E8E8E8", "twitterclone_form_label_color", 3, 0),
			("Theme - 3", "#222222", "twitterclone_input_background_color", 3, 0),
			("Theme - 3", "#E6E6E6", "twitterclone_input_font_color", 3, 0),
			("Theme - 3", "#535353", "twitterclone_input_border_color", 3, 0),
			("Theme - 3", "#1D9BF0", "twitterclone_button_background_color", 3, 0),
			("Theme - 3", "#1584cf", "twitterclone_button_background_color_hover", 3, 0),
			("Theme - 3", "#FFFFFF", "twitterclone_button_font_color", 3, 0),
			("Theme - 3", "#FFFFFF", "twitterclone_button_font_color_hover", 3, 0),
			("Theme - 3", "#1D9BF0", "twitterclone_button_border_color", 3, 0),
			("Theme - 3", "#1584cf", "twitterclone_button_border_color_hover", 3, 0),
			("Theme - 3", "#343536", "twitterclone_secondary_button_background_color", 3, 0),
			("Theme - 3", "#4c4d4e", "twitterclone_secondary_button_background_color_hover", 3, 0),
			("Theme - 3", "#e4e6eb", "twitterclone_secondary_button_font_color", 3, 0),
			("Theme - 3", "#dbdbdb", "twitterclone_secondary_button_font_color_hover", 3, 0),
			("Theme - 3", "#5c5c5c", "twitterclone_secondary_button_border_color", 3, 0),
			("Theme - 3", "#FDFDFD", "twitterclone_secondary_button_border_color_hover", 3, 0),
			("Theme - 3", "#222222", "twitterclone_comments_background_color", 3, 0);');

			$db->query('UPDATE `engine4_core_menuitems` SET `params` = \'{"icon":"far fa-bell"}\' WHERE `engine4_core_menuitems`.`name` = "core_mini_update";');
			$db->query('UPDATE `engine4_core_menuitems` SET `params` = \'{"icon":"far fa-user"}\' WHERE `engine4_core_menuitems`.`name` = "core_mini_profile";');
			$db->query('UPDATE `engine4_core_menuitems` SET `params` = \'{"icon":"fa-sign-in-alt"}\' WHERE `engine4_core_menuitems`.`name` = "core_mini_auth";');
			$db->query('UPDATE `engine4_core_menuitems` SET `params` = \'{"icon":"fa-user-plus"}\' WHERE `engine4_core_menuitems`.`name` = "core_mini_signup";');
			$db->query('UPDATE `engine4_core_menuitems` SET `params` = \'{"icon":"far fa-envelope"}\' WHERE `engine4_core_menuitems`.`name` = "core_mini_messages";');

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

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

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