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

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

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

			$db = Zend_Db_Table_Abstract::getDefaultAdapter();

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

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_fbclone_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_fbclone_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, "Albums", "albums", 1, 1, NULL, 1, "fa fa-image", "#E6204F", 6, NULL),
			(3, "Blogs", "blogs", 1, 1, NULL, 1, "fa fa-pencil-alt", "#E64919", 5, NULL),
			(4, "Groups", "groups", 1, 1, NULL, 1, "fa fa-users", "#DE1486", 4, NULL),
			(5, "Events", "events", 1, 1, NULL, 1, "fa fa-calendar", "#51CF8B", 3, NULL),
			(6, "Music", "music", 1, 1, NULL, 1, "fa fa-music", "#413482", 2, NULL),
			(7, "Polls", "polls", 1, 1, NULL, 1, "fa fa-chart-bar", "#FF0008", 1, NULL),
			(8, "Videos", "videos", 1, 1, NULL, 1, "fa fa-video", "#08B5C2", 0, NULL);');

			$db->query('CREATE TABLE IF NOT EXISTS `engine4_fbclone_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_fbclone_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", "fbclone_header_background_color", 1, 0),
			("Theme - 1", "#636569", "fbclone_mainmenu_links_color", 1, 0),
			("Theme - 1", "#1D7CF7", "fbclone_mainmenu_links_hover_color", 1, 0),
			("Theme - 1", "#ececec", "fbclone_minimenu_background_color", 1, 0),
			("Theme - 1", "#f2f7ff", "fbclone_minimenu_hover_background_color", 1, 0),
			("Theme - 1", "#000000", "fbclone_mininmenu_links_color", 1, 0),
			("Theme - 1", "#1D7CF7", "fbclone_mininmenu_links_hover_color", 1, 0),
			("Theme - 1", "#F6F6F6", "fbclone_minimenu_search_background_color", 1, 0),
			("Theme - 1", "#5F727F", "fbclone_minimenu_search_font_color", 1, 0),
			("Theme - 1", "#FDFDFD", "fbclone_footer_background_color", 1, 0),
			("Theme - 1", "#737373", "fbclone_footer_font_color", 1, 0),
			("Theme - 1", "#737373", "fbclone_footer_links_color", 1, 0),
			("Theme - 1", "#1D7CF7", "fbclone_theme_color", 1, 0),
			("Theme - 1", "#F0F2F5", "fbclone_body_background_color", 1, 0),
			("Theme - 1", "#000000", "fbclone_font_color", 1, 0),
			("Theme - 1", "#65676B", "fbclone_font_color_light", 1, 0),
			("Theme - 1", "#0d6efd", "fbclone_links_color", 1, 0),
			("Theme - 1", "#085fdf", "fbclone_links_hover_color", 1, 0),
			("Theme - 1", "#000000", "fbclone_headline_color", 1, 0),
			("Theme - 1", "#E1E2E3", "fbclone_border_color", 1, 0),
			("Theme - 1", "#FFFFFF", "fbclone_box_background_color", 1, 0),
			("Theme - 1", "#F5F5F5", "fbclone_box_background_color_alt", 1, 0),
			("Theme - 1", "#455B6B", "fbclone_form_label_color", 1, 0),
			("Theme - 1", "#FFFFFF", "fbclone_input_background_color", 1, 0),
			("Theme - 1", "#5F727F", "fbclone_input_font_color", 1, 0),
			("Theme - 1", "#D7D8DA", "fbclone_input_border_color", 1, 0),
			("Theme - 1", "#1D7CF7", "fbclone_button_background_color", 1, 0),
			("Theme - 1", "#2783FB", "fbclone_button_background_color_hover", 1, 0),
			("Theme - 1", "#FFFFFF", "fbclone_button_font_color", 1, 0),
			("Theme - 1", "#FFFFFF", "fbclone_button_font_color_hover", 1, 0),
			("Theme - 1", "#1D7CF7", "fbclone_button_border_color", 1, 0),
			("Theme - 1", "#1D7CF7", "fbclone_button_border_color_hover", 1, 0),
			("Theme - 1", "#f2f2f2", "fbclone_secondary_button_background_color", 1, 0),
			("Theme - 1", "#e5e5e5", "fbclone_secondary_button_background_color_hover", 1, 0),
			("Theme - 1", "#212529", "fbclone_secondary_button_font_color", 1, 0),
			("Theme - 1", "#212529", "fbclone_secondary_button_font_color_hover", 1, 0),
			("Theme - 1", "#E4E6EB", "fbclone_secondary_button_border_color", 1, 0),
			("Theme - 1", "#d8dadf", "fbclone_secondary_button_border_color_hover", 1, 0),
			("Theme - 1", "#f9f9fb", "fbclone_comments_background_color", 1, 0),
			("Theme - 1", "#E5E5E5", "fbclone_dashboard_list_background_color_hover", 1, 0),
			("Theme - 1", "#dbdbdb", "fbclone_dashboard_list_border_color", 1, 0),
			("Theme - 1", "#040609", "fbclone_dashboard_font_color", 1, 0),
			("Theme - 1", "#040609", "fbclone_dashboard_link_color", 1, 0),
			("Theme - 1", "#46BD2E", "fbclone_lp_signup_button_color", 1, 0),
			("Theme - 1", "#46BD2E", "fbclone_lp_signup_button_border_color", 1, 0),
			("Theme - 1", "#FFFFFF", "fbclone_lp_signup_button_font_color", 1, 0),
			("Theme - 1", "#46BD2E", "fbclone_lp_signup_button_hover_color", 1, 0),
			("Theme - 1", "#FFFFFF", "fbclone_lp_signup_button_hover_font_color", 1, 0),
			("Theme - 2", "2", "theme_color", 2, 0),
			("Theme - 2", "#FFFFFF", "fbclone_header_background_color", 2, 0),
			("Theme - 2", "#636569", "fbclone_mainmenu_links_color", 2, 0),
			("Theme - 2", "#46BD2E", "fbclone_mainmenu_links_hover_color", 2, 0),
			("Theme - 2", "#ececec", "fbclone_minimenu_background_color", 2, 0),
			("Theme - 2", "#ececec", "fbclone_minimenu_hover_background_color", 2, 0),
			("Theme - 2", "#000000", "fbclone_mininmenu_links_color", 2, 0),
			("Theme - 2", "#46BD2E", "fbclone_mininmenu_links_hover_color", 2, 0),
			("Theme - 2", "#F6F6F6", "fbclone_minimenu_search_background_color", 2, 0),
			("Theme - 2", "#5F727F", "fbclone_minimenu_search_font_color", 2, 0),
			("Theme - 2", "#FDFDFD", "fbclone_footer_background_color", 2, 0),
			("Theme - 2", "#737373", "fbclone_footer_font_color", 2, 0),
			("Theme - 2", "#737373", "fbclone_footer_links_color", 2, 0),
			("Theme - 2", "#46BD2E", "fbclone_theme_color", 2, 0),
			("Theme - 2", "#EFEDE9", "fbclone_body_background_color", 2, 0),
			("Theme - 2", "#1E293B", "fbclone_font_color", 2, 0),
			("Theme - 2", "#64748B", "fbclone_font_color_light", 2, 0),
			("Theme - 2", "#0d6efd", "fbclone_links_color", 2, 0),
			("Theme - 2", "#085fdf", "fbclone_links_hover_color", 2, 0),
			("Theme - 2", "#000000", "fbclone_headline_color", 2, 0),
			("Theme - 2", "#E1E2E3", "fbclone_border_color", 2, 0),
			("Theme - 2", "#FFFFFF", "fbclone_box_background_color", 2, 0),
			("Theme - 2", "#F5F5F5", "fbclone_box_background_color_alt", 2, 0),
			("Theme - 2", "#455B6B", "fbclone_form_label_color", 2, 0),
			("Theme - 2", "#FFFFFF", "fbclone_input_background_color", 2, 0),
			("Theme - 2", "#5F727F", "fbclone_input_font_color", 2, 0),
			("Theme - 2", "#D7D8DA", "fbclone_input_border_color", 2, 0),
			("Theme - 2", "#46BD2E", "fbclone_button_background_color", 2, 0),
			("Theme - 2", "#46BD2E", "fbclone_button_background_color_hover", 2, 0),
			("Theme - 2", "#FFFFFF", "fbclone_button_font_color", 1, 0),
			("Theme - 2", "#FFFFFF", "fbclone_button_font_color_hover", 1, 0),
			("Theme - 2", "#46BD2E", "fbclone_button_border_color", 1, 0),
			("Theme - 2", "#46BD2E", "fbclone_button_border_color_hover", 1, 0),
			("Theme - 2", "#f2f2f2", "fbclone_secondary_button_background_color", 1, 0),
			("Theme - 2", "#e5e5e5", "fbclone_secondary_button_background_color_hover", 1, 0),
			("Theme - 2", "#212529", "fbclone_secondary_button_font_color", 1, 0),
			("Theme - 2", "#212529", "fbclone_secondary_button_font_color_hover", 1, 0),
			("Theme - 2", "#E4E6EB", "fbclone_secondary_button_border_color", 1, 0),
			("Theme - 2", "#d8dadf", "fbclone_secondary_button_border_color_hover", 1, 0),
			("Theme - 2", "#f9f9fb", "fbclone_comments_background_color", 2, 0),
			("Theme - 2", "#E5E5E5", "fbclone_dashboard_list_background_color_hover", 2, 0),
			("Theme - 2", "#dbdbdb", "fbclone_dashboard_list_border_color", 2, 0),
			("Theme - 2", "#040609", "fbclone_dashboard_font_color", 2, 0),
			("Theme - 2", "#040609", "fbclone_dashboard_link_color", 2, 0),
			("Theme - 2", "#1D7CF7", "fbclone_lp_signup_button_color", 2, 0),
			("Theme - 2", "#1D7CF7", "fbclone_lp_signup_button_border_color", 2, 0),
			("Theme - 2", "#FFFFFF", "fbclone_lp_signup_button_font_color", 2, 0),
			("Theme - 2", "#1D7CF7", "fbclone_lp_signup_button_hover_color", 2, 0),
			("Theme - 2", "#FFFFFF", "fbclone_lp_signup_button_hover_font_color", 2, 0),
			("Theme - 3", "3", "theme_color", 3, 0),
			("Theme - 3", "3", "custom_theme_color", 3, 0),
			("Theme - 3", "#2E2F30", "fbclone_header_background_color", 3, 0),
			("Theme - 3", "#9EB8BE", "fbclone_mainmenu_links_color", 3, 0),
			("Theme - 3", "#1D7CF7", "fbclone_mainmenu_links_hover_color", 3, 0),
			("Theme - 3", "#474747", "fbclone_minimenu_background_color", 3, 0),
			("Theme - 3", "#686868", "fbclone_minimenu_hover_background_color", 3, 0),
			("Theme - 3", "#FFFFFF", "fbclone_mininmenu_links_color", 3, 0),
			("Theme - 3", "#1D7CF7", "fbclone_mininmenu_links_hover_color", 3, 0),
			("Theme - 3", "#4F4F4F", "fbclone_minimenu_search_background_color", 3, 0),
			("Theme - 3", "#FFFFFF", "fbclone_minimenu_search_font_color", 3, 0),
			("Theme - 3", "#363738", "fbclone_footer_background_color", 3, 0),
			("Theme - 3", "#CBCDD2", "fbclone_footer_font_color", 3, 0),
			("Theme - 3", "#CBCDD2", "fbclone_footer_links_color", 3, 0),
			("Theme - 3", "#1D7CF7", "fbclone_theme_color", 3, 0),
			("Theme - 3", "#252627", "fbclone_body_background_color", 3, 0),
			("Theme - 3", "#CBCDD2", "fbclone_font_color", 3, 0),
			("Theme - 3", "#CBCDD2", "fbclone_font_color_light", 3, 0),
			("Theme - 3", "#0d6efd", "fbclone_links_color", 3, 0),
			("Theme - 3", "#085fdf", "fbclone_links_hover_color", 3, 0),
			("Theme - 3", "#B0B3B7", "fbclone_headline_color", 3, 0),
			("Theme - 3", "#464646", "fbclone_border_color", 3, 0),
			("Theme - 3", "#2E2F30", "fbclone_box_background_color", 3, 0),
			("Theme - 3", "#252627", "fbclone_box_background_color_alt", 3, 0),
			("Theme - 3", "#B0B3B7", "fbclone_form_label_color", 3, 0),
			("Theme - 3", "#2E2F30", "fbclone_input_background_color", 3, 0),
			("Theme - 3", "#CBCDD2", "fbclone_input_font_color", 3, 0),
			("Theme - 3", "#575757", "fbclone_input_border_color", 3, 0),
			("Theme - 3", "#1D7CF7", "fbclone_button_background_color", 3, 0),
			("Theme - 3", "#208ED3", "fbclone_button_background_color_hover", 3, 0),
			("Theme - 3", "#FFFFFF", "fbclone_button_font_color", 3, 0),
			("Theme - 3", "#FFFFFF", "fbclone_button_font_color_hover", 3, 0),
			("Theme - 3", "#208ED3", "fbclone_button_border_color", 3, 0),
			("Theme - 3", "#208ED3", "fbclone_button_border_color_hover", 3, 0),
			("Theme - 3", "#343536", "fbclone_secondary_button_background_color", 3, 0),
			("Theme - 3", "#4c4d4e", "fbclone_secondary_button_background_color_hover", 3, 0),
			("Theme - 3", "#e4e6eb", "fbclone_secondary_button_font_color", 3, 0),
			("Theme - 3", "#dbdbdb", "fbclone_secondary_button_font_color_hover", 3, 0),
			("Theme - 3", "#5c5c5c", "fbclone_secondary_button_border_color", 3, 0),
			("Theme - 3", "#FDFDFD", "fbclone_secondary_button_border_color_hover", 3, 0),
			("Theme - 3", "#4F4F4F", "fbclone_comments_background_color", 3, 0),
			("Theme - 3", "#000000", "fbclone_dashboard_list_background_color_hover", 3, 0),
			("Theme - 3", "#696969", "fbclone_dashboard_list_border_color", 3, 0),
			("Theme - 3", "#FFFFFF", "fbclone_dashboard_font_color", 3, 0),
			("Theme - 3", "#cbcdd2", "fbclone_dashboard_link_color", 3, 0),
			("Theme - 3", "#46BD2E", "fbclone_lp_signup_button_color", 3, 0),
			("Theme - 3", "#46BD2E", "fbclone_lp_signup_button_border_color", 3, 0),
			("Theme - 3", "#FFFFFF", "fbclone_lp_signup_button_font_color", 3, 0),
			("Theme - 3", "#46BD2E", "fbclone_lp_signup_button_hover_color", 3, 0),
			("Theme - 3", "#FFFFFF", "fbclone_lp_signup_button_hover_font_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` = "" 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('fbclone.licensekey', $licenseKey);
			$settings->setSetting('core.license.domain', $domainName);
			$settings->setSetting('fbclone.pluginactivated', 1);
		}

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

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