2012-12-26 14:02:27 +01:00
|
|
|
<?php
|
2013-10-13 00:52:44 +02:00
|
|
|
|
2012-12-26 14:02:27 +01:00
|
|
|
/**
|
|
|
|
* Returns user by id.
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2012-12-26 14:02:27 +01:00
|
|
|
* @param $id UID
|
|
|
|
*/
|
|
|
|
function User($id) {
|
|
|
|
$user_source = sql_select("SELECT * FROM `User` WHERE `UID`=" . sql_escape($id) . " LIMIT 1");
|
2013-10-13 00:52:44 +02:00
|
|
|
if ($user_source === false)
|
2013-09-18 01:38:36 +02:00
|
|
|
return false;
|
2013-10-13 00:52:44 +02:00
|
|
|
if (count($user_source) > 0)
|
2012-12-26 14:02:27 +01:00
|
|
|
return $user_source[0];
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-09-10 14:27:31 +02:00
|
|
|
/**
|
|
|
|
* Returns User by api_key.
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
|
|
|
* @param string $api_key
|
|
|
|
* User api key
|
2013-09-10 14:27:31 +02:00
|
|
|
* @return Matching user, null or false on error
|
|
|
|
*/
|
|
|
|
function User_by_api_key($api_key) {
|
|
|
|
$user = sql_select("SELECT * FROM `User` WHERE `api_key`='" . sql_escape($api_key) . "' LIMIT 1");
|
2013-10-13 00:52:44 +02:00
|
|
|
if ($user === false)
|
2013-09-10 14:27:31 +02:00
|
|
|
return false;
|
|
|
|
if (count($user) == 0)
|
|
|
|
return null;
|
|
|
|
return $user[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new api key for given user.
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2013-09-10 14:27:31 +02:00
|
|
|
* @param User $user
|
|
|
|
*/
|
2013-09-10 14:45:41 +02:00
|
|
|
function User_reset_api_key(&$user) {
|
2013-09-10 14:27:31 +02:00
|
|
|
$user['api_key'] = md5($user['Nick'] . time() . rand());
|
2013-09-18 01:38:36 +02:00
|
|
|
$result = sql_query("UPDATE `User` SET `api_key`='" . sql_escape($user['api_key']) . "' WHERE `UID`='" . sql_escape($user['UID']) . "' LIMIT 1");
|
2013-10-13 00:52:44 +02:00
|
|
|
if ($result === false)
|
2013-09-18 01:38:36 +02:00
|
|
|
return false;
|
2013-09-10 14:27:31 +02:00
|
|
|
engelsystem_log("API key resetted.");
|
|
|
|
}
|
|
|
|
|
2012-12-26 14:02:27 +01:00
|
|
|
?>
|