engelsystem/includes/controller/api.php

356 lines
8.5 KiB
PHP
Raw Normal View History

2013-12-29 15:08:33 +01:00
<?php
/************************************************************************************************
* API Documentation
************************************************************************************************
General:
--------
All API calls output JSON-encoded data. Client parameters should be passed encoded using JSON in HTTP POST data.
Every API Request must be contained the Api Key (using JSON parameter 'key') and the Command (using JSON parameter 'cmd').
Testing API calls (using curl):
-------------------------------
$ curl -d '{"cmd":"getVersion"}' '<Address>/?p=api'
$ curl -d '{"cmd":"getApiKey","user":"admin","pw":"admin"}' '<Address>/?p=api'
$ curl -d '{"key":"<key>","cmd":"getRoom"}' '<Address>/?p=api'
$ curl -d '{"key":"<key>","cmd":"sendmessage","uid":"23","text":"test message"}' '<Address>/?p=api'
2013-12-29 15:08:33 +01:00
2014-01-05 19:33:52 +01:00
Methods without key:
--------------------
2013-12-29 15:08:33 +01:00
getVersion
Description:
Returns API version.
2013-12-29 15:08:33 +01:00
Parameters:
nothing
Return Example:
2014-01-05 19:33:52 +01:00
{"status":"success","version": "1"}
getApiKey
Description:
Returns API Key version.
2014-01-05 19:33:52 +01:00
Parameters:
user (string)
pw (string)
Return Example:
{"status":"success","Key":"1234567890123456789012"}
2013-12-29 15:08:33 +01:00
2014-01-05 19:33:52 +01:00
Methods with Key:
-----------------
2013-12-29 15:08:33 +01:00
getRoom
Description:
Returns a list of all Rooms (no id set) or details of a single Room (requested id)
2013-12-29 15:08:33 +01:00
Parameters:
id (integer) - Room ID
2013-12-29 15:08:33 +01:00
Return Example:
[{"RID":"1"},{"RID":"23"},{"RID":"42"}]
2013-12-29 15:08:33 +01:00
{"RID":"1","Name":"Room Name","Man":null,"FromPentabarf":"","show":"Y","Number":"0"}
getAngelType
Description:
Returns a list of all Angel Types (no id set) or details of a single Angel Type (requested id)
2013-12-29 15:08:33 +01:00
Parameters:
id (integer) - Type ID
2013-12-29 15:08:33 +01:00
Return Example:
[{"id":"8"},{"id":"9"}]
{"id":"9","name":"Angeltypes 2","restricted":"0"}
getUser
Description:
Returns a list of all Users (no id set) or details of a single User (requested id)
2013-12-29 15:08:33 +01:00
Parameters:
id (integer) - User ID
2013-12-29 15:08:33 +01:00
Return Example:
[{"UID":"1"},{"UID":"23"},{"UID":"42"}]
{"UID":"1","Nick":"admin","Name":"Gates","Vorname":"Bill","Telefon":"","DECT":"","Handy":"","email":"","ICQ":"","jabber":"","Avatar":"115"}
getShift
Description:
Returns a list of all Shifte (no id set, filter is optional) or details of a single Shift (requested id)
2013-12-29 15:08:33 +01:00
Parameters:
id (integer) - Shift ID
2013-12-29 15:08:33 +01:00
filterRoom (Array of integer) - Array of Room IDs (optional, for list request)
filterTask (Array of integer) - Array if Task (optional, for list request)
filterOccupancy (integer) - Occupancy state: (optional, for list request)
1 occupied
2 free
3 occupied and free
Return Example:
[{"SID":"1"},{"SID":"2"},{"SID":"3"}]
2014-01-05 19:33:52 +01:00
{"SID":"10","start":"1388264400","end":"1388271600","RID":"1","name":"Shift 1","URL":null,"PSID":null,\
"ShiftEntry":[{"TID":"8","UID":"4","freeloaded":"0"}],
"NeedAngels":[{"TID":"8","count":"1","restricted":"0","taken":1},{"TID":"9","count":"2","restricted":"0","taken":0}]}
2013-12-29 15:08:33 +01:00
getMessage
Description:
Returns a list of all Messages (no id set) or details of a single Message (requested id)
2013-12-29 15:08:33 +01:00
Parameters:
id (integer) - Message ID
2013-12-29 15:08:33 +01:00
Return Example:
[{"id":"1"},{"id":"2"},{"id":"3"}]
{"id":"3","Datum":"1388247583","SUID":"23","RUID":"42","isRead":"N","Text":"message text"}
sendMessage
Description:
send a Message to an other angel
Parameters:
uid (integer) - User ID of the reciever
text (string) - Message Text
Return Example:
{"status":"success"}
2013-12-29 15:08:33 +01:00
************************************************************************************************/
/**
* General API Controller
*/
function api_controller() {
2014-01-07 15:50:16 +01:00
global $user, $DataJson;
2014-01-05 19:33:52 +01:00
header("Content-Type: application/json; charset=utf-8");
2014-01-07 15:50:16 +01:00
2013-12-29 15:08:33 +01:00
// decode JSON request
$input = file_get_contents("php://input");
$input = json_decode($input, true);
$_REQUEST = $input;
2014-01-07 15:50:16 +01:00
2014-01-05 19:33:52 +01:00
// get command
2014-01-07 15:50:16 +01:00
$cmd = '';
if (isset($_REQUEST['cmd']))
$cmd = strtolower($_REQUEST['cmd']);
// decode commands, without key
switch ($cmd) {
2014-01-05 19:33:52 +01:00
case 'getversion':
getVersion();
2014-01-07 15:50:16 +01:00
die(json_encode($DataJson));
2014-01-05 19:33:52 +01:00
break;
case 'getapikey':
getApiKey();
2014-01-07 15:50:16 +01:00
die(json_encode($DataJson));
2014-01-05 19:33:52 +01:00
break;
}
2014-01-07 15:50:16 +01:00
2013-12-29 15:08:33 +01:00
// get API KEY
if (isset($_REQUEST['key']) && preg_match("/^[0-9a-f]{32}$/", $_REQUEST['key']))
$key = $_REQUEST['key'];
else
2014-01-07 15:50:16 +01:00
die(json_encode(array(
'status' => 'failed',
'error' => 'Missing parameter "key".'
)));
// check API key
2013-12-29 15:08:33 +01:00
$user = User_by_api_key($key);
if ($user === false)
2014-01-07 15:50:16 +01:00
die(json_encode(array(
'status' => 'failed',
'error' => 'Unable to find user'
)));
2013-12-29 15:08:33 +01:00
if ($user == null)
2014-01-07 15:50:16 +01:00
die(json_encode(array(
'status' => 'failed',
'error' => 'Key invalid.'
)));
// decode command
switch ($cmd) {
2013-12-29 15:08:33 +01:00
case 'getroom':
getRoom();
break;
case 'getangeltype':
getAngelType();
break;
case 'getuser':
2014-01-07 15:50:16 +01:00
// TODO Dataleak! Only coordinators are allowed to see so much user informations.
//getUser();
2013-12-29 15:08:33 +01:00
break;
case 'getshift':
getShift();
break;
case 'getmessage':
2014-01-07 15:50:16 +01:00
// TODO Dataleak!
//getMessage();
2013-12-29 15:08:33 +01:00
break;
case 'sendmessage':
sendMessage();
break;
2013-12-29 15:08:33 +01:00
default:
2014-01-07 15:50:16 +01:00
$DataJson = array(
2014-01-05 19:33:52 +01:00
'status' => 'failed',
2014-01-07 15:50:16 +01:00
'error' => 'Unknown Command "' . $cmd . '"'
);
2014-01-05 19:33:52 +01:00
}
2014-01-07 15:50:16 +01:00
// check
2014-01-07 15:50:16 +01:00
if ($DataJson === false) {
$DataJson = array(
'status' => 'failed',
'error' => 'DataJson === false'
);
} elseif ($DataJson == null) {
$DataJson = array(
'status' => 'failed',
'error' => 'DataJson == null'
);
2013-12-29 15:08:33 +01:00
}
2014-01-07 15:50:16 +01:00
2013-12-29 15:08:33 +01:00
echo json_encode($DataJson);
die();
}
/**
* Get Version of API
*/
2014-01-07 15:50:16 +01:00
function getVersion() {
2013-12-29 15:08:33 +01:00
global $DataJson;
2014-01-07 15:50:16 +01:00
$DataJson = array(
2014-01-07 15:50:16 +01:00
'status' => 'success',
'Version' => 1
);
2013-12-29 15:08:33 +01:00
}
2014-01-05 19:33:52 +01:00
/**
* Get API Key
*/
2014-01-07 15:50:16 +01:00
function getApiKey() {
global $DataJson;
if (! isset($_REQUEST['user'])) {
$DataJson = array(
'status' => 'failed',
'error' => 'Missing parameter "user".'
);
} elseif (! isset($_REQUEST['pw'])) {
$DataJson = array(
'status' => 'failed',
'error' => 'Missing parameter "pw".'
);
2014-01-05 19:33:52 +01:00
} else {
2014-01-07 15:50:16 +01:00
$Erg = sql_select("SELECT `UID`, `Passwort`, `api_key` FROM `User` WHERE `Nick`='" . sql_escape($_REQUEST['user']) . "'");
2014-01-05 19:33:52 +01:00
if (count($Erg) == 1) {
$Erg = $Erg[0];
2014-01-07 15:50:16 +01:00
if (verify_password($_REQUEST['pw'], $Erg["Passwort"], $Erg["UID"])) {
2014-01-05 19:33:52 +01:00
$key = $Erg["api_key"];
$DataJson = array(
2014-01-07 15:50:16 +01:00
'status' => 'success',
'Key' => $key
);
2014-01-05 19:33:52 +01:00
} else {
2014-01-07 15:50:16 +01:00
$DataJson = array(
'status' => 'failed',
'error' => 'PW wrong'
);
2014-01-05 19:33:52 +01:00
}
} else {
2014-01-07 15:50:16 +01:00
$DataJson = array(
'status' => 'failed',
'error' => 'User not found.'
);
2014-01-05 19:33:52 +01:00
}
}
2014-01-07 15:50:16 +01:00
2014-01-05 19:33:52 +01:00
sleep(1);
}
2013-12-29 15:08:33 +01:00
/**
* Get Room
2013-12-29 15:08:33 +01:00
*/
2014-01-07 15:50:16 +01:00
function getRoom() {
global $DataJson;
if (isset($_REQUEST['id'])) {
$DataJson = Room($_REQUEST['id']);
2013-12-29 15:08:33 +01:00
} else {
2014-01-07 15:50:16 +01:00
$DataJson = Room_ids();
2013-12-29 15:08:33 +01:00
}
}
/**
* Get AngelType
*/
2014-01-07 15:50:16 +01:00
function getAngelType() {
global $DataJson;
if (isset($_REQUEST['id'])) {
$DataJson = AngelType($_REQUEST['id']);
2013-12-29 15:08:33 +01:00
} else {
2014-01-07 15:50:16 +01:00
$DataJson = AngelType_ids();
2013-12-29 15:08:33 +01:00
}
}
/**
* Get User
*/
2014-01-07 15:50:16 +01:00
function getUser() {
global $DataJson;
if (isset($_REQUEST['id'])) {
$DataJson = mUser_Limit($_REQUEST['id']);
2013-12-29 15:08:33 +01:00
} else {
2014-01-07 15:50:16 +01:00
$DataJson = User_ids();
2013-12-29 15:08:33 +01:00
}
}
/**
* Get Shift
*/
2014-01-07 15:50:16 +01:00
function getShift() {
global $DataJson;
if (isset($_REQUEST['id'])) {
$DataJson = Shift($_REQUEST['id']);
2013-12-29 15:08:33 +01:00
} else {
2014-01-07 15:50:16 +01:00
$DataJson = Shifts_filtered();
2013-12-29 15:08:33 +01:00
}
}
/**
2014-01-07 15:50:16 +01:00
* @TODO: Why are ALL messages of ALL users returned? Data leak. It is not checked if this is my message!
2013-12-29 15:08:33 +01:00
* Get Message
*/
2014-01-07 15:50:16 +01:00
function getMessage() {
global $DataJson;
if (isset($_REQUEST['id'])) {
$DataJson = Message($_REQUEST['id']);
2013-12-29 15:08:33 +01:00
} else {
2014-01-07 15:50:16 +01:00
$DataJson = Message_ids();
2013-12-29 15:08:33 +01:00
}
}
/**
* Send Message
*/
2014-01-07 15:50:16 +01:00
function sendMessage() {
global $DataJson;
if (! isset($_REQUEST['uid'])) {
$DataJson = array(
'status' => 'failed',
'error' => 'Missing parameter "uid".'
);
} elseif (! isset($_REQUEST['text'])) {
$DataJson = array(
'status' => 'failed',
'error' => 'Missing parameter "text".'
);
} else {
2014-01-07 15:50:16 +01:00
if (Message_send($_REQUEST['uid'], $_REQUEST['text']) === true) {
$DataJson = array(
'status' => 'success'
);
} else {
$DataJson = array(
2014-01-07 15:50:16 +01:00
'status' => 'failed',
'error' => 'Transmitting was terminated with an Error.'
);
}
}
}
?>