<?php 
/*--------------------------------------------------\
| Kvcodes    	|   Saaiaran    | login file        |
|---------------------------------------------------|
| For use with:                                    	|
| FrontAccounting 									|
| http://www.kvcodes.com/  	            			|
| by kvvaradha                            			|
|---------------------------------------------------|
| Note:                                         	|
| Changes can be made to this CSS that will be  	|
| reflected throughout FrontAccounting.             |
|                                                   |
\--------------------------------------------------*/
$page_security = 'SA_BACKUP';
$path_to_root = "../.."; 
global $saai_options, $fonts_list, $saai_version;
include_once($path_to_root."/includes/ui.inc");
include_once($path_to_root."/includes/session.inc");
include_once($path_to_root."/themes/Saaisaran/kvcodes.inc");
include_once($path_to_root."/themes/Saaisaran/version.php");
include_once($path_to_root . "/admin/db/company_db.inc");

if(!function_exists('kv_update_user_theme')){
	function kv_update_user_theme($id, $theme){
		$sql = "UPDATE ".TB_PREF."users SET theme=". db_escape($theme)." WHERE id=".db_escape($id);
		return db_query($sql, "could not update user display prefs for $id");
	}	
}

page(_($help_context = "Saai Theme Options"));

if(isset($_GET['updated'])){
	display_notification("Your Custom Settings Updated Successfully!");
}
if(isset($_POST['submit_options'])){
	$dir =  $path_to_root."/themes/Saaisaran/images";
	if($_FILES['logo']["size"] >0){
		$tmpname = $_FILES['logo']['tmp_name'];		
		$ext = end((explode(".", $_FILES['logo']['name'])));
		$filesize = $_FILES['logo']['size'];
		$filetype = $_FILES['logo']['type'];
		if (file_exists($dir."/".kv_get_option('logo')))
			unlink($dir."/".kv_get_option('logo'));
			
		move_uploaded_file($tmpname, $dir."/kv_logo.".$ext);
		kv_update_option('logo', 'kv_logo.'.$ext);
	}
	
	if($_FILES['favicon']["size"] >0){
		$tmpname = $_FILES['favicon']['tmp_name'];		
		$extn = end((explode(".", $_FILES['favicon']['name'])));
		$filesize = $_FILES['favicon']['size'];
		$filetype = $_FILES['favicon']['type'];
		if (file_exists($dir."/".kv_get_option('favicon')))
			unlink($dir."/".kv_get_option('favicon'));
			
		move_uploaded_file($tmpname, $dir."/kv_favicon.".$extn);
		kv_update_option('favicon', 'kv_favicon.'.$extn);
	}

	if(!isset($_POST['hide_version'])){
		$_POST['hide_version'] = 0;
	}
	if(!isset($_POST['hide_help_link'])){
		$_POST['hide_help_link'] = 0;
	}if(!isset($_POST['hide_back_button'])){
		$_POST['hide_back_button'] = 0;
	}
	if(!isset($_POST['hide_dashboard'])){
		$_POST['hide_dashboard'] = 0;
	}if(!isset($_POST['use_software_logo'])){
		$_POST['use_software_logo'] = 0;
	}		
	kv_update_option('hide_version', $_POST['hide_version']);
	kv_update_option('font', $_POST['font']);
	kv_update_option('login', $_POST['login']);
	kv_update_option('hide_help_link', $_POST['hide_help_link']);
	kv_update_option('hide_back_button', $_POST['hide_back_button']);
	kv_update_option('hide_dashboard', $_POST['hide_dashboard']);
	kv_update_option('custom_color', $_POST['custom_color']);
	kv_update_option('use_software_logo', $_POST['use_software_logo']);
	kv_update_option('enable_master_login', check_value('enable_master_login'));

	if(check_value('enable_master_login') == 1 ) {
		if($_SESSION['wa_current_user']->company == 0 && isset($_SESSION['wa_current_user']->user)) {
                $sql = "CREATE TABLE IF NOT EXISTS `0_master_login` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `user_id` varchar(60) NOT NULL,
			  `role` int(11) NOT NULL,
			  `companies` text NOT NULL,
			  PRIMARY KEY(`id`)
			) ENGINE=InnoDB DEFAULT CHARSET=latin1;"; 
			            db_query($sql, 'master Table has not Created!');

            $sql1 = db_query("SELECT COUNT(*) FROM 0_master_login WHERE user_id = ".db_escape($_SESSION['wa_current_user']->loginname), "Can't get master_login");
            if(db_num_rows($sql1) == 0){
                 $sql2 = "INSERT INTO 0_master_login (user_id, role, companies) VALUES (".db_escape($_SESSION['wa_current_user']->loginname).", ".db_escape($_SESSION['wa_current_user']->access).", ".db_escape(base64_encode(serialize([$_SESSION['wa_current_user']->company]))).")";
                  db_query($sql2, "Cant insert default master");
          	}   
        }           
   
	}


	if(check_value('reset_dashboard')){
		db_query("DELETE FROM saai_theme_option WHERE option_name='active_graph'", "Can't delete active graph");
		$ar = array( 1 => 'classbalancechart', 2 => 'sales', 3 => 'customers', 4 => 'suppliers', 5 => 'expenses', 6 => 'taxes', 7 => 'payments', 8 => 'salesinvoices', 9 => 'overduesales', 10 => 'bankbalance', 11 => 'overduesalesrecurrent', 12 => 'avgsalesdaily', 13 => 'overduepurchase', 14 => 'top10items', 15 => 'classbalancetable');
		$res = base64_encode(serialize($ar));

		kv_update_option('active_graph', $res);
	}
	if(check_value('set_default_theme')){
		if(!defined('DEFAULT_THEME')){
			$fp = fopen($path_to_root.'/config.php', 'a');//opens file in append mode  
			fwrite($fp, PHP_EOL."define('DEFAULT_THEME','Saaisaran');".PHP_EOL);  
			fclose($fp);  

			$session_inc = file_get_contents($path_to_root.'/includes/session.inc');
			$pt_root = '$path_to_root';
			$login = "if(defined('DEFAULT_THEME') && file_exists($pt_root.'/themes/'.DEFAULT_THEME.'/login.php'))
					include($pt_root . '/themes/'.DEFAULT_THEME.'/login.php');
				else
					include($pt_root . '/access/login.php');";

			$session_inc_1 = str_replace('include($path_to_root . "/access/login.php");', $login, $session_inc); 

			$pwd_reset = "if(defined('DEFAULT_THEME') && file_exists($pt_root.'/themes/'.DEFAULT_THEME.'/password_reset.php'))
					include($pt_root . '/themes/'.DEFAULT_THEME.'/password_reset.php');
				else
					include($pt_root . '/access/password_reset.php');";

			$session_inc_2 = str_replace('include($path_to_root . "/access/password_reset.php");', $pwd_reset, $session_inc_1); 

			file_put_contents($path_to_root.'/includes/session.inc', $session_inc_2);
		} else {
			$config_php = file_get_contents($path_to_root.'/config.php');  
			// using str_replace() function 
			$resStr = str_replace("define('DEFAULT_THEME','".DEFAULT_THEME."');", "define('DEFAULT_THEME','Saaisaran');", $config_php); 

			file_put_contents($path_to_root.'/config.php', $resStr);
		}

		$sql ="ALTER TABLE `".TB_PREF."users` CHANGE `theme` `theme` VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Saaisaran';";
		db_query($sql, "Can't set saai as default theme");
	}
	if(check_value('set_exist_user')){
		$sql ="UPDATE `".TB_PREF."users` SET `theme`='Saaisaran'  WHERE 1";
		db_query($sql, "Can't set saai as default theme");
	}
	
	if(strlen(trim($_POST['powered_name'])) > 0 ){
		kv_update_option('powered_name', $_POST['powered_name']);
	}

	if(strlen(trim($_POST['powered_url'])) > 0 ){
		kv_update_option('powered_url', $_POST['powered_url']);
	}
	if(strlen(trim($_POST['theme'])) > 0 ){
		kv_update_option('theme', $_POST['theme']);
	}
	if(strlen(trim($_POST['color_scheme'])) > 0 ){
		kv_update_option('color_scheme', $_POST['color_scheme']);
	}
	$_POST['theme'] = clean_file_name($_POST['theme']);
	$chg_theme = $_POST['theme'];
	if ($chg_theme){
		kv_update_user_theme($_SESSION["wa_current_user"]->user, $_POST['theme']);
		$_SESSION["wa_current_user"]->prefs->theme = $_POST['theme'];	
	}
	unset($_FILES);
	unset($_POST);
	if ($chg_theme)
		meta_forward($path_to_root.'/themes/'.$chg_theme.'/theme-options.php', 'updated=yes');	
	else
		meta_forward($_SERVER['PHP_SELF'].'?updated=yes');	
}


$_POST['hide_version'] = isset($saai_options['hide_version']) ? $saai_options['hide_version'] : 0 ; 

$_POST['hide_dashboard'] = isset($saai_options['hide_dashboard']) ? $saai_options['hide_dashboard'] : 0 ; 

$_POST['hide_help_link'] = isset($saai_options['hide_help_link']) ? $saai_options['hide_help_link'] : 0 ; 

$_POST['hide_back_button'] = isset($saai_options['hide_back_button']) ? $saai_options['hide_back_button'] : 0 ; 

$_POST['powered_name'] = isset($saai_options['powered_name']) ? $saai_options['powered_name'] : 'FrontAccounting' ; 

$_POST['powered_url'] = isset($saai_options['powered_url']) ? $saai_options['powered_url'] : 'frontaccounting.com' ; 

$_POST['font'] = isset($saai_options['font']) ? $saai_options['font'] : 'Ubuntu' ; 

$_POST['login'] = isset($saai_options['login']) ? $saai_options['login'] : '' ; 

$_POST['custom_color'] = isset($saai_options['custom_color']) ? $saai_options['custom_color'] : '#4e4e4e' ; 

$_POST['color_scheme'] = isset($saai_options['color_scheme']) ? $saai_options['color_scheme'] : 'default' ; 

$_POST['enable_master_login'] = isset($saai_options['enable_master_login']) ? $saai_options['enable_master_login'] : 0 ; 

$_POST['use_software_logo']= (isset($saai_options['use_software_logo']) ? $saai_options['use_software_logo'] : 0 );

	start_form(true); 
	div_start('ThemeOptions');
		start_outer_table(TABLESTYLE, "width='60%'");
			table_section(1);
			table_section_title(_("General Options"));

				kv_image_row(_("Upload Logo") . ":", 'logo', 'logo');
				kv_image_row(_("Favicon Icon") . ":", 'favicon', 'favicon');				
				text_row(_("Powered Name*:"), 'powered_name', null, 28, 80);	
				text_row(_("Powered By*:"), 'powered_url', null, 28, 80);					
				themes_list_row(_("Theme:"), "theme", user_theme());
				Saai_color_schemes(_("Color Schemes:"), "color_scheme", null);
				echo '<tr><td class="label"> '._("Custom Color").'</td><td> <input name="custom_color" value="'.$_POST['custom_color'].'" size="28" data-jscolor="" > </td> </tr>';
				
				//if(list_updated('color_scheme') && get_post('color_scheme') == 'custom'){
					//$Ajax->activate('ThemeOptions');
					
				//}
				fonts_list_row(_("Select a Font:"), "font", null);
				login_page_list_row(_("Select a Login Page:"), "login", null);

				table_section(2);
				table_section_title(_("Selective Features"));
				check_row(_("Hide Version Details").':', 'hide_version', null);
				check_row(_("Hide Top Help Link").':', 'hide_help_link', null);
				check_row(_("Hide Dashboard").':', 'hide_dashboard', null);
				check_row(_("Hide Bottom Back Button").':', 'hide_back_button', null);
				check_row(_("Reset Dashboard Widgets").':', 'reset_dashboard', null);
				check_row(_("Set Saai as Default Theme").':', 'set_default_theme', null);
				check_row(_("Set Saai to existing Users").':', 'set_exist_user', null);
				check_row(_("Display Software logo").':', 'use_software_logo', null);
				check_row(_("Enable Master Login").':', 'enable_master_login', null);
				echo '<tr><td>'._("Theme Version").'</td><td><a target="_blank" href="http://www.kvcodes.com/clients/products/client/item_details/saai-theme-single-license" >'.$saai_version.'</a></td> </tr>';
			//table_section_title(_("General Options"));
				//text_row(_("Powered Name*:"), 'powered_name', null, 28, 80);	
				//text_row(_("Powered By*:"), 'powered_url', null, 28, 80);
			//$tzlist = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
			//print_r($tzlist); 
			//$tzlis =  generate_timezone_list();
			//$options = array('select_submit'=> false,'disabled' => null);
			//echo '<tr><td> Time Zone </td><td>'. array_selector('time_zone', null, $tzlis, $options).'</td> </tr>';
			//print_r($tzlis);
		end_outer_table();
		div_end();
		br();
		submit_center('submit_options', _("Update Options"), _('Theme data'));
	end_form();
br(2); ?>

<script src="<?php echo $path_to_root.'/themes/Saaisaran/js/jscolor.js'; ?>" type="text/javascript" > </script>
<script type="text/javascript" >

	</script> <?php 
end_page(); 


function fonts_list_row($label, $name, $selected_id=null) {
	global $fonts_list;
	$fonts_list = array_keys($fonts_list);
	$fonts = [];
	foreach($fonts_list as $font)
		$fonts[$font] = $font;
	echo "<tr>";
	if ($label != null)
		echo "<td class='label'>$label</td>\n";
	echo "<td>";

	echo array_selector($name, $selected_id, $fonts, 
		array( 'select_submit'=> false ) );
	echo "</td></tr>\n";
}
function login_page_list_row($label, $name, $selected_id=null) {
	global $path_to_root;	
	$login_page_list = [ 'login.php' => 'Default'];
	 foreach(glob($path_to_root.'/themes/Saaisaran/login/*', GLOB_NOSORT) as $login)   
    {  
       $login_page_list[pathinfo($login, PATHINFO_FILENAME).'.php'] = ucfirst(str_replace('-', ' ', pathinfo($login, PATHINFO_FILENAME)));
    } 	
		
	echo "<tr>";
	if ($label != null)
		echo "<td class='label'>$label</td>\n";
	echo "<td>";

	echo array_selector($name, $selected_id, $login_page_list, 
		array( 'select_submit'=> false ) );
	echo "</td></tr>\n";
}
?>
