<?php
    $_fullname = isset($_POST['fullname']) ? trim(strval($_POST['fullname'])) : '';
    $_year = isset($_POST['year']) ? intval($_POST['year']) : 0;
    $_month = isset($_POST['month']) ? intval($_POST['month']) : 0;
    $_day = isset($_POST['day']) ? intval($_POST['day']) : 0;
    $_gender = isset($_POST['gender']) ? intval($_POST['gender']) : '0';
    $_country = isset($_POST['country']) ? trim(strval($_POST['country'])) : '';
    $_region = isset($_POST['region']) ? trim(strval($_POST['region'])) : '';
    $_city = isset($_POST['city']) ? trim(strval($_POST['city'])) : '';
    $_website = isset($_POST['website']) ? trim(strval($_POST['website'])) : '';
    $_aim = isset($_POST['aim']) ? trim(strval($_POST['aim'])) : '';
    $_icq = isset($_POST['icq']) ? trim(strval($_POST['icq'])) : '';
    $_msn = isset($_POST['msn']) ? trim(strval($_POST['msn'])) : '';
    $_yahoo = isset($_POST['yahoo']) ? trim(strval($_POST['yahoo'])) : '';
    $_gtalk = isset($_POST['gtalk']) ? trim(strval($_POST['gtalk'])) : '';
    $_privacy = isset($_POST['privacy']) ? intval($_POST['privacy']) : 0;
    $_notification = isset($_POST['notification']) ? intval($_POST['notification']) : 0;
    
    if ($_gender != 0 && $_gender != 1 && $_gender != 2)
    {
        $_gender = 0;
    }

    $birthday = '0000-00-00';
    if (checkdate($_month, $_day, $_year))
    {
        $birthday = substr('000' . $_year, -4) . '-' . substr('0' . $_month, -2) . '-' . substr('0' . $_day, -2);
    }
    
    $longitude = $latitude = 0;
    $city_id = $region_id = $country_id = 0;
    $city_name = $region_name = $country_name = '';
        
    $cities  = mysqli_query_logged("SELECT city_id, region_id FROM geo_cities WHERE city_id = " . sq($_city));        
    if ($city = mysqli_fetch_array($cities))
    {
        $city_id = $city['city_id'];
        $region_id = $city['region_id'];
    }
        
    $regions = mysqli_query_logged("SELECT country_id, region_id FROM geo_regions WHERE region_id = '" . ($region_id ? $region_id : $_region) . "'");
    if ($region = mysqli_fetch_array($regions))
    {
        $country_id = $region['country_id'];
        $region_id = $region['region_id'];
    }
    
    $countries = mysqli_query_logged("SELECT country_id FROM geo_countries WHERE country_id = '" . ($country_id ? $country_id : $_country) . "'");
    if ($country = mysqli_fetch_array($countries))
    {
        $country_id = $region['country_id'];
    }        
        
    if ($city_id)
    {
        $city = mysqli_fetch_array(mysqli_query_logged("SELECT city_name, latitude, longitude FROM geo_cities WHERE city_id = '" . $city_id . "'"));
        $city_name = $city['city_name'];
        $longitude = $city['longitude'];
        $latitude = $city['latitude'];
    }

    if ($region_id)
    {
        $region = mysqli_fetch_array(mysqli_query_logged("SELECT region_name FROM geo_regions WHERE region_id = '" . $region_id . "'"));
        $region_name = $region['region_name'];
    }
        
    if ($country_id)
    {
        $country = mysqli_fetch_array(mysqli_query_logged("SELECT country_name FROM geo_countries WHERE country_id = '" . $country_id . "'"));
        $country_name = $country['country_name'];
    }        
    
    mysqli_query_logged("REPLACE INTO members_extras SET user_id = '" . $GLOBALS['auth']['id'] . "', fullname = " . sq($_fullname) . ", birthday = '" . $birthday . "', gender = " . sq($_gender) . ", country = '" . addslashes($country_name) . "', region = '" . addslashes($region_name) . "', city = '" . addslashes($city_name) . "', latitude = '" . $latitude . "', longitude = '" . $longitude . "', website = " . sq($_website) . ", aim = " . sq($_aim) . ", icq = " . sq($_icq) . ", msn = " . sq($_msn) . ", yahoo = " . sq($_yahoo) . ", gtalk = " . sq($_gtalk) . ", privacy = " . sq($_privacy) . ", notification = " . sq($_notification));
    header('Location: /?s=userinfo');
?>