<?php
define('_IN_JOHNCMS', 1);
$rootpath = '../';
$headmod = 'login';
require('../incfiles/core.php');
if($user_id) header('Location: /?err');
$lng_reg = core::load_lng('registration');
$textl = $lng['registration'];
require('../incfiles/head.php');


$s = file_get_contents('http://ulogin.ru/token.php?token=' . $_POST['token'] . '&host=' . $_SERVER['HTTP_HOST']); 
$user = json_decode($s, true); 

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `users` WHERE `type_reg` = '" . $user['network'] . "' AND `identity` = '" . $user['identity'] . "'"),0) == 0)
{

require('classsimpleimage.php');
// func
function downloadFile($str, $file)  
    {  
        ob_end_clean();  
        ob_start();  
        echo $str;  
        header('Content-Description: File Transfer');  
        header('Content-Type: application/octet-stream');  
        header('Content-Disposition: attachment; filename=' . $file);  
        header('Content-Transfer-Encoding: binary');  
        header('Expires: 0');  
        header('Cache-Control: must-revalidate');  
        header('Pragma: public');  
        header('Content-Length: ' . ob_get_length());  
        flush();  
        return true;  
    }
function passgen($y) {
$p = '';
$arr = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
$x = 0;
while ($x < $y) {
$p .= $arr[array_rand($arr)]; $x++; } return $p; }
//func end



$preg = $set['mod_reg'] > 1 ? 1 : 0;
$reg_sex = $_POST['sex'] == 2 ? 'm' : 'zh';
$password = passgen(8);
$pass = md5(md5($password));
$reg_name = $_POST['imname'];
$live = $_POST['live'];
$reg_nick2 = $user['nickname'];
$reg_nick = $_POST['nick'];
$abaut = $_POST['abaut'];
$email = $_POST['email'];
$dmy = $_POST['dmy'];
$photo = $_POST['photo'];
$identity = $_POST['identity'];
$type_reg = $_POST['type_reg'];
if (isset($_POST['submit']) && $_SESSION['token'] == $_POST['token_reg']) {

// Определяем день рождение
if (!empty($dmy)) {
$ardr = explode('.', $dmy);
if (sizeof($ardr)==3) {
list($dayb, $monthb, $yearofbirth) = explode('.', $dmy);
} elseif(sizeof($ardr)==3) {
list($dayb, $monthb) = explode('.', $dmy);
} else {
list($dayb, $monthb, $yearofbirth) = explode('.', date('d.m.Y'));    
}
}

$sql_r = mysql_query("INSERT INTO `users` SET `name` = '" . mysql_real_escape_string($reg_nick) . "', `name_lat` = '" . mysql_real_escape_string(functions::rus_lat(mb_strtolower($reg_nick))) . "', `password` = '" . mysql_real_escape_string($pass) . "', `imname` = '" . mysql_real_escape_string($reg_name) . "', `about` = '" . mysql_real_escape_string($abaut) . "', `sex` = '$reg_sex', `live` = '" . mysql_real_escape_string($live) . "', `mail` = '" . mysql_real_escape_string($email) . "', `rights` = '0', `ip` = '" . core::$ip . "', `ip_via_proxy` = '" . core::$ip_via_proxy . "', `browser` = '" . mysql_real_escape_string($agn) . "', `datereg` = '" . time() . "', `lastdate` = '" . time() . "', `sestime` = '" . time() . "', `preg` = '$preg', `dayb` = '" . intval($dayb) . "', `monthb` = '" . intval($monthb) . "', `yearofbirth` = '" . intval($yearofbirth) . "',`identity` = '".mysql_real_escape_string($identity)."',
`type_reg` = '".mysql_real_escape_string($type_reg)."'");
if ($sql_r) {
$usid = mysql_insert_id();

//Реферальская система
		if($ref_ok == true) {
			mysql_query("INSERT INTO `referals` SET	`user_id`='" . $usid . "', `ref`='" . $_SESSION['referals'] . "', `ip`='" . core::$ip . "', `ua`='" . mysql_real_escape_string($agn) . "';");
			$shop = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'reg' LIMIT 1"));

			mysql_query("UPDATE `users` SET	`balls`=`balls`+".$shop['val']." WHERE `id`='" . $_SESSION['referals'] . "';");
			}
			
if ($photo) {
//  Копируем аватарку
$image = new SimpleImage();
$image->load($photo);
$image->resize(80, 80);
$image->save('../files/users/photo/'.$usid.'_small.jpg');

$image = new SimpleImage();
$image->load($photo);
$image->resizeToWidth(320);
$image->save('../files/users/photo/'.$usid.'.jpg');

$image = new SimpleImage();
$image->load($photo);
$image->resize(64, 64);
$image->save('../files/users/avatar/'.$usid.'.png');
}
echo '<div class="menu"><p><h3>' . $lng_reg['you_registered'] . '</h3>' . $lng_reg['your_id'] .
': <b>' . $usid . '</b><br/>' . $lng_reg['your_login'] . ': <b>' . $reg_nick .
'</b><br/>' . $lng_reg['your_password'] . ': <b>' . $password . '</b></p>' .
'<br/><a href="../login.php">' . $lng_reg['enter'] . '</a></div>';
require('../incfiles/end.php'); exit;
}
} 
if(count($user) > '1') {
$token = mt_rand(1000, 100000);
$_SESSION['token'] = $token;
echo '<div class="phdr">регистрация</div>
<form action="" method="post">
<div class="list2">
Выберете себе никнейм<br />
<input type="hidden" name="sex" value="' . $user['sex'] . '" />
<input type="hidden" name="imname" value="' . htmlspecialchars($user['first_name'] . ' ' . $user['last_name']) . '" />
<input type="hidden" name="live" value="' . htmlspecialchars($user['country'].', '.$user['city']) . '" />
<input type="hidden" name="dmy" value="' . htmlspecialchars($user['bdate']) . '" />
<input type="hidden" name="photo" value="' . htmlspecialchars($user['photo_big']) . '" />
<input type="hidden" name="abaut" value="' . htmlspecialchars($user['identity']) . '" />
<input type="hidden" name="email" value="' . htmlspecialchars($user['email']) . '" />
<input type="hidden" name="identity" value="' . htmlspecialchars($user['identity']) . '" />
<input type="hidden" name="type_reg" value="' . htmlspecialchars($user['network']) . '" />
<input type="hidden" name="token_reg" value="' . $token . '" />
<p><input type="text" name="nick" maxlength="15" value="' . htmlspecialchars($reg_nick2) . '" /> <input type="submit" name="submit" value="' . $lng_reg['registration'] . '"/></p>
<div class="sub">'.(!mysql_num_rows(mysql_query("select * from `users` where name='" . $reg_nick2 . "';")) && strlen($reg_nick2) > '5' ? '<b style="color:green">Ник свободный, вы можете его регистрировать =)</b>' : '<b style="color:red">Ник слишком короткий, или уже существует. Пожалуйста выберите другой никнейм =)</b>').'</div>
<div class="sub">Никто не увидит введённые вами данные на сайте.Продолжая, вы соглашаетесь с <a href="/pages/rules.php" target="_blank">Правилами</a> сайта.</div>
</div>
</form>'; } else echo 'Error!';

}else{
echo'К данной социальной сети уже привязана учетная запись';
require('../incfiles/end.php');
exit();
}


require('../incfiles/end.php');
?>
