<?php

/*

 * Created on 25.04.2010

 *

 * profile.php

 * Author: Alex Sam <greeka@gmail.com>

 *

 */



session_start();



include 'func.php';



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



$res = new stdClass();

$res->result = 'success';

$res->session = session_id();

if ($action == 'login') {

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

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



    if (empty($login) || empty($password)) {

    	$res->result = 'error';

        $res->message = 'Username and password required';

    } else {

        $qry = 'select * from bc_user where username = "' . mysql_escape_string($login) .  '" limit 1';

        $_res = @mysql_query($qry);

        $udata = @mysql_fetch_assoc($_res);

        if (!empty($udata) && $udata['password'] == md5($password)) {

        	$res->data->user_ = $udata['user_'];

            $res->data->username = $udata['username'];

            $_SESSION['userdata'] = $udata;

        } else {

        	$res->result = 'error';

            $res->message = 'Username or password not correct';

        }

    }



} elseif ($action == 'register') {

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

    $password = isset($_POST['password']) ? trim($_POST['password']) : '';



    if (empty($login) || empty($password)) {

        $res->result = 'error';

        $res->message = 'Username and password required';

    } else {

        $qry = 'select * from bc_user where username = "' . mysql_escape_string($login) .  '" limit 1';

        $_res = @mysql_query($qry);

        $udata = @mysql_fetch_assoc($_res);

        if (empty($udata)) {

            $qry = 'insert into bc_user (username, password) values ("' . mysql_escape_string($login) . '", "' . md5($password) . '")';

            @mysql_query($qry);

        } else {

            $res->result = 'error';

            $res->message = 'Username already taken';

        }

    }

} elseif ($action == 'saveproof') {



} elseif ($action == 'login_by_token') {

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

	if (!empty($token) && isset($_SESSION[$token]) && !empty($_SESSION[$token])) {

		$res->data->user_ = $_SESSION[$token]['user_'];

        $res->data->username = $_SESSION[$token]['username'];

		$_SESSION['userdata'] = $_SESSION[$token];

	}

}



echo json_encode($res);



?>