<?php
	profile :: is_user(true);
	$title = $l = 'Настройки';
	$lBack = profile :: getLink($user);
	$thisLink = "/".$thisPage;
	$act = (isset($_GET['act']) ? intval($_GET['act']) : 1);
	$act = (in_array($act, [1,2]) ? $act : 1);
$prv = DB :: $pdo -> queryFetch("SELECT * FROM `privatMail` WHERE `id_user` = '$user[id]' LIMIT 1");

	// Смена пароля.
	if($act == 1) {
		if(isset($_POST['cfms-password'])) {
			if(@$_POST['access'] == ACCESS) {
				$password_n = (isset($_POST['new_password']) ? system :: text($_POST['new_password']) : NULL );
				$password_r = (isset($_POST['r_new_password']) ? system :: text($_POST['r_new_password']) : NULL );
				$password = (isset($_POST['password']) ? system :: text($_POST['password']) : NULL );
				if($password == $user['password'] || System::bhash($password) == $user['password']) {
					if(system::utf_strlen($password_n) >= 6){
						if(system::utf_strlen($password_n) <= 32){
							if(preg_match('|^[a-z0-9\-]+$|i', $password_n)){
								if($password_r == $password_n){
									$sid = system :: genHash($password_n, $user['id']);
									$access = rand(100000000,200000000);
									DB :: $pdo -> query("UPDATE `users` SET `sid`=?,`password`=?,`access`=?, `password_update` = ? WHERE `id`=? ",array($sid,System::bhash($password_n),$access, 1, $user['id']));
									setcookie('xsid', $sid, time()+60*60*24*365, "/");
									system :: redirect("Пароль успешно изменен!", $thisLink."?act=1");
								} else { $serror = "Введенные пароли не совпадают."; }
							} else { $serror = "Недопустимые символы в новом пароле."; }
						} else { $serror = "Пароль слишком длинный, максимальная длина - 16 символов."; }
					} else { $serror = "Пароль слишком короткий, минимальная длина - 6 символов."; }
				} else { $serror = "Текущий пароль введен неверно!"; }
			} else { $serror = "Вероятно вам подкинули эту ссылку."; }
		}

		if(isset($_POST['cfms-timezone'])) {
		    $timezone = empty($_POST['timezone']) ? 0 : (int)$_POST['timezone'];
		    DB :: $pdo -> query("UPDATE `users` SET `timezone`=? WHERE `id`=? ",array($timezone, $user['id']));
		    system :: redirect("Изменения сохранены.", $thisLink."?act=1");
		}
	} 

	require (CORE."head.php");

	system :: show($serror);
	
	?>
	<div class="app-block">
	<div class="list tab">
        <a class="<?=( $act == 1  ? "tab_active" : '' )?>" href="<?=$thisLink?>?act=1">Основное</a>
  <a class="<?=( $act == 2  ? "tab_active" : '' )?>" href="<?=$thisLink?>?act=2">Приватность</a>
   
       </div>

<?php if($act == 1) { ?>

	<div>
		<a href="#" class="app-toggler app-toggler-white" data-id="settings--password" data-class="app-toggler-open" onclick="App.toggle(this,event);return false;"><i class="fa fa-key fa-0px" style="margin: 0 4px -2px 0;"></i> Изменить пароль<span class="app-toggler-icon app-icons"></span></a>
		<div id="settings--password" style="display: none;">
	<div class="list">
	<div class="app-block-w">
	<form method="post" action="" name="settings--password">
		<div style="margin-bottom: 3px;">
			<label class="_gray _dbl" for="password">Введите ваш текущий пароль:</label>
			<input type="password" size="20" maxlength="32" class="main-input _w-100" name="password" value="" id="password">
		</div>
		<div style="margin-bottom: 3px;">
			<label class="_gray _dbl" for="new_password">Введите новый пароль:</label>
			<input type="password" size="20" maxlength="32" class="main-input _w-100" name="new_password" value="" id="new_password">
		</div>		
		<div style="margin-bottom: 3px;">
			<label class="_gray _dbl" for="r_new_password">Повторите новый пароль:</label>
			<input type="password" size="20" maxlength="32" class="main-input _w-100" name="r_new_password" value="" id="r_new_password">
		</div>
		<input type="hidden" name="access" value="<?=ACCESS?>">
		<input type="submit" value="Сохранить" name="cfms-password" class="main-btn _w-100">
	</form>
	</div>
	</div>
	</div>
<a href="/language" class="app-toggler app-toggler-white" ><i class="fa fa-language fa-0px" style="margin: 0 4px -2px 0;"></i> Изменить язык</a>
    <a href="#" class="app-toggler app-toggler-white" data-id="settings--time" data-class="app-toggler-open" onclick="App.toggle(this,event);return false;"><i class="fa fa-clock-o fa-0px" style="margin: 0 4px -2px 0;"></i> Время<span class="app-toggler-icon app-icons"></span></a>
<div id="settings--time" style="display: none;">
	<div class="list">
	<div class="app-block-w">
	<form method="post" action="" name="settings--time">
		<div style="margin-bottom: 3px;">
			<label class="_gray _dbl">Часовой пояс:
			    <select name="timezone"  class="_dbl _w-100" >
                <?php
                $arrTimezone = ['-54000' => '(UTC-12.00)', '-50400' => '(UTC-11.00)', '-46800' => '(UTC-10.00)', '-43200' => '(UTC-9.00)', '-39600' => '(UTC-8.00)', '-36000' => '(UTC-7.00)', '-32400' => '(UTC-6.00)', '-28800' => '(UTC-5.00)', '-27000' => '(UTC-4.30)', '-25200' => '(UTC-4.00)', '-23400' => '(UTC-3.30)', '-21600' => '(UTC-3.00)', '-19800' => '(UTC-2.30)', '-18000' => '(UTC-2.00)', '-14400' => '(UTC-1.00)', '-10800' => '(UTC)', '-7200' => '(UTC+1.00)', '-3600' => '(UTC+2.00)', '-1800' => '(UTC+2.30)', '0' => '(UTC+3.00)', '1800' => '(UTC+3.30)', '3600' => '(UTC+4.00)', '5400' => '(UTC+4.30)', '7200' => '(UTC+5.00)', '9000' => '(UTC+5.30)', '9900' => '(UTC+5.45)', '10800' => '(UTC+6.00)', '12600' => '(UTC+6.30)', '14400' => '(UTC+7.00)', '18000' => '(UTC+8.00)', '19800' => '(UTC+8.30)', '21600' => '(UTC+9.00)', '23400' => '(UTC+9.30)', '25200' => '(UTC+10.00)', '28800' => '(UTC+11.00)', '32400' => '(UTC+12.00)', '36000' => '(UTC+13.00)'];
                foreach(array_keys($arrTimezone) as $key => $zone) { ?>
                <option value="<?= $zone ?>"<?= ($user['timezone'] == $zone?' selected="selected"':'') ?>>сейчас <?= date("G:i", $_SERVER['REQUEST_TIME'] + $zone) ?> <?= $arrTimezone[$zone] ?></option>
                <?php } ?>
                </select>
            </label>
		</div>
		<input type="submit" value="Сохранить" name="cfms-timezone" class="main-btn _w-100">
	</form>
	</div>
	</div>
	</div>
	</div>
    
		
	<?php
	}else if($act == 2) {
	?><div class='list'>
	<span class='_gray'>Писать в почту могут: <?=($prv['privat_mail']==1?"<a href='/mail/?act=settings'><b>Все</b></a>":"<a href='/mail/?act=settings'><b>Друзья</b></a></span>")?></div>
	<?php }




	require (CORE."foot.php");