From 8fa78b55ac2327f87d492cf483a7d77b79ae8db0 Mon Sep 17 00:00:00 2001 From: Philip Häusler Date: Sun, 29 Dec 2013 15:08:33 +0100 Subject: first api new files --- includes/controller/api.php | 219 +++++++++++++++++++++++++++++++++++++ includes/model/AngelType_model.php | 29 +++++ includes/model/Message_model.php | 29 +++++ includes/model/Room_model.php | 31 ++++++ 4 files changed, 308 insertions(+) create mode 100644 includes/controller/api.php create mode 100644 includes/model/AngelType_model.php create mode 100644 includes/model/Message_model.php create mode 100644 includes/model/Room_model.php (limited to 'includes') diff --git a/includes/controller/api.php b/includes/controller/api.php new file mode 100644 index 00000000..07a389b1 --- /dev/null +++ b/includes/controller/api.php @@ -0,0 +1,219 @@ +","cmd":"getVersion"}' '
/?p=api' + + +Methods: +-------- +getVersion + Description: + Returns API version. + Parameters: + nothing + Return Example: + {"version": "1"} + +getRoom + Description: + Returns a list of all Rooms (no id set) or details of a single Room (requested id) + Parameters: + id (integer) - Room ID + Return Example: + [{"RID":"1"},{"RID":"2"},{"RID":"3"},{"RID":"4"}] + {"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) + Parameters: + id (integer) - Type ID + 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) + Parameters: + id (integer) - User ID + 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) + Parameters: + id (integer) - Shift ID + 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"}] + {"SID":"1","start":"1388185200","end":"1388199600","RID":"1","name":"Shift 1","URL":null,"PSID":null} + +getMessage + Description: + Returns a list of all Messages (no id set) or details of a single Message (requested id) + Parameters: + id (integer) - Message ID + Return Example: + [{"id":"1"},{"id":"2"},{"id":"3"}] + {"id":"3","Datum":"1388247583","SUID":"23","RUID":"42","isRead":"N","Text":"message text"} + + +************************************************************************************************/ + + +/** + * General API Controller + */ +function api_controller() { + global $DataJson, $_REQUEST; + + // decode JSON request + $input = file_get_contents("php://input"); + $input = json_decode($input, true); + $_REQUEST = $input; + + // get API KEY + if (isset($_REQUEST['key']) && preg_match("/^[0-9a-f]{32}$/", $_REQUEST['key'])) + $key = $_REQUEST['key']; + else + die("Missing key."); + + // check API key + $user = User_by_api_key($key); + if ($user === false) + die("Unable to find user."); + if ($user == null) + die("Key invalid."); + + // get command + $cmd=''; + if (isset($_REQUEST['cmd']) ) + $cmd = strtolower( $_REQUEST['cmd']); + + // decode command + switch( $cmd) { + case 'echo': + $DataJson = $input; + break; + case 'getversion': + getVersion(); + break; + case 'getroom': + getRoom(); + break; + case 'getangeltype': + getAngelType(); + break; + case 'getuser': + getUser(); + break; + case 'getshift': + getShift(); + break; + case 'getmessage': + getMessage(); + break; + default: + die("Unknown Command (". $cmd. ")"); + } + + + header("Content-Type: application/json; charset=utf-8"); + echo json_encode($DataJson); + die(); +} + +/** + * Get Version of API + */ +function getVersion(){ + global $DataJson; + $DataJson['Version'] = 1; +} + +/** + * Get Room + */ +function getRoom(){ + global $DataJson, $_REQUEST; + + if (isset($_REQUEST['id']) ) { + $DataJson = mRoom( $_REQUEST['id']); + } else { + $DataJson = mRoomList(); + } +} + +/** + * Get AngelType + */ +function getAngelType(){ + global $DataJson, $_REQUEST; + + if (isset($_REQUEST['id']) ) { + $DataJson = mAngelType( $_REQUEST['id']); + } else { + $DataJson = mAngelTypeList(); + } +} + +/** + * Get User + */ +function getUser(){ + global $DataJson, $_REQUEST; + + if (isset($_REQUEST['id']) ) { + $DataJson = mUser_Limit( $_REQUEST['id']); + } else { + $DataJson = mUserList(); + } +} + +/** + * Get Shift + */ +function getShift(){ + global $DataJson, $_REQUEST; + + if (isset($_REQUEST['id']) ) { + $DataJson = mShift( $_REQUEST['id']); + } else { + $DataJson = mShiftList(); + } +} + +/** + * Get Message + */ +function getMessage(){ + global $DataJson, $_REQUEST; + + if (isset($_REQUEST['id']) ) { + $DataJson = mMessage( $_REQUEST['id']); + } else { + $DataJson = mMessageList(); + } +} + +?> diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php new file mode 100644 index 00000000..49d1c702 --- /dev/null +++ b/includes/model/AngelType_model.php @@ -0,0 +1,29 @@ + 0) + return $angelType_source; + return null; +} + +/** + * Returns angelType by id. + * + * @param $id angelType ID + */ +function mAngelType($id) { + $angelType_source = sql_select("SELECT * FROM `AngelTypes` WHERE `id`=" . sql_escape($id) . " LIMIT 1"); + if ($angelType_source === false) + return false; + if (count($angelType_source) > 0) + return $angelType_source[0]; + return null; +} + +?> \ No newline at end of file diff --git a/includes/model/Message_model.php b/includes/model/Message_model.php new file mode 100644 index 00000000..0141208b --- /dev/null +++ b/includes/model/Message_model.php @@ -0,0 +1,29 @@ + 0) + return $message_source; + return null; +} + +/** + * Returns message by id. + * + * @param $id message ID + */ +function mMessage($id) { + $message_source = sql_select("SELECT * FROM `Messages` WHERE `id`=" . sql_escape($id) . " LIMIT 1"); + if ($message_source === false) + return false; + if (count($message_source) > 0) + return $message_source[0]; + return null; +} + +?> \ No newline at end of file diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php new file mode 100644 index 00000000..fea241a6 --- /dev/null +++ b/includes/model/Room_model.php @@ -0,0 +1,31 @@ + 0) + return $room_source; + return null; +} + + +/** + * Returns room by id. + * + * @param $id RID + */ +function mRoom($id) { + $room_source = sql_select("SELECT * FROM `Room` WHERE `RID`=" . sql_escape($id) . " LIMIT 1"); + if ($room_source === false) + return false; + if (count($room_source) > 0) + return $room_source[0]; + return null; +} + + +?> -- cgit v1.2.3-54-g00ecf