<?php

require './OutsparkUserModel.inc.php';

if (empty($_SERVER['REQUEST_METHOD'])) {
	$user = $argv[1];
	if (empty($user)) {
		die("Missing username");
	}
	$remote_ip = '127.0.0.1';
} else {
	if (empty($_GET['user'])) {
		die("Missing username");
	} else {
		$user = $_GET['user'];
	}
	$remote_ip = $_SERVER['REMOTE_ADDR'];
}

$model = new OutsparkUserModel();
$userinfo = $model->getUserInfo($user);

$time = time();

$token = $userinfo['name'] . '|' . $time . '|' . $remote_ip;

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = 'Very super secret key';
$enc_token = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $token, MCRYPT_MODE_CBC, $iv);

echo '<pre>';
var_dump(strlen($token));
var_dump(strlen($enc_token));
$url_token = str_replace(array('+','/','='),array('-','_','.'),base64_encode($enc_token));
var_dump($url_token);

$dec_token = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $enc_token, MCRYPT_MODE_CBC, $iv);
print_r(explode('|', rtrim($dec_token, "\0")));

echo '</pre>';

?>
