summaryrefslogtreecommitdiff
path: root/includes/controller/api.php
blob: 3c87e5b4eac97b406a41ea05dc623eaed0c128dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?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'

Methods without key:
--------------------
getVersion
  Description:
    Returns API version.
  Parameters:
    nothing
  Return Example:
    {"status":"success","version": "1"}

getApiKey
  Description:
    Returns API Key version.
  Parameters:
    user (string)
    pw (string)
  Return Example:
    {"status":"success","Key":"1234567890123456789012"}

Methods with Key:
-----------------
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":"23"},{"RID":"42"}]
    {"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":"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}]}

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"}

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"}

************************************************************************************************/


/**
 * General API Controller
 */
function api_controller() {
  global $user, $DataJson, $_REQUEST;

  header("Content-Type: application/json; charset=utf-8");

  // decode JSON request
  $input = file_get_contents("php://input");
  $input = json_decode($input, true);
  $_REQUEST = $input;

  // get command
  $cmd='';
  if (isset($_REQUEST['cmd']) )
    $cmd = strtolower( $_REQUEST['cmd']);

  // decode commands, without key
  switch( $cmd) {
    case 'getversion':
      getVersion();
      die( json_encode($DataJson));
      break;
    case 'getapikey':
      getApiKey();
      die( json_encode($DataJson));
      break;
  }

  // get API KEY
  if (isset($_REQUEST['key']) && preg_match("/^[0-9a-f]{32}$/", $_REQUEST['key']))
    $key = $_REQUEST['key'];
  else
  die( json_encode( array (
          'status' => 'failed',
          'error' => 'Missing parameter "key".' )));

  // check API key
  $user = User_by_api_key($key);
  if ($user === false)
  	die( json_encode( array (
          'status' => 'failed',
          'error' => 'Unable to find user' )));
  if ($user == null)
  	die( json_encode( array (
          'status' => 'failed',
          'error' => 'Key invalid.' )));

  // decode command
  switch( $cmd) {
    case 'getroom':
      getRoom();
      break;
    case 'getangeltype':
      getAngelType();
      break;
    case 'getuser':
      getUser();
      break;
    case 'getshift':
      getShift();
      break;
    case 'getmessage':
      getMessage();
      break;
    case 'sendmessage':
      sendMessage();
      break;
    default:
      $DataJson = array (
          'status' => 'failed',
          'error' => 'Unknown Command "'. $cmd. '"' );
  }

  // check
  if( $DataJson === false) {
    $DataJson = array (
      'status' => 'failed',
      'error' => 'DataJson === false' );
  } elseif( $DataJson == null) {
    $DataJson = array (
      'status' => 'failed',
      'error' => 'DataJson == null' );
  }

  echo json_encode($DataJson);
  die();
}

/**
 * Get Version of API
 */
function getVersion(){
  global $DataJson;

  $DataJson = array(
    'status' => 'success',
    'Version' => 1);
}


/**
 * Get API Key
 */
function getApiKey(){
  global $DataJson, $_REQUEST;

  if (!isset($_REQUEST['user']) ) {
    $DataJson = array (
      'status' => 'failed',
      'error' => 'Missing parameter "user".' );
  }
  elseif (!isset($_REQUEST['pw']) ) {
    $DataJson = array (
      'status' => 'failed',
      'error' => 'Missing parameter "pw".' );
  } else {
    $Erg = sql_select( "SELECT `UID`, `Passwort`, `api_key` FROM `User` WHERE `Nick`='" . sql_escape($_REQUEST['user']) . "'");

    if (count($Erg) == 1) {
      $Erg = $Erg[0];
      if (verify_password( $_REQUEST['pw'], $Erg["Passwort"], $Erg["UID"])) {
        $key = $Erg["api_key"];
        $DataJson = array(
          'status' => 'success',
          'Key' => $key);
      } else {
        $DataJson = array (
          'status' => 'failed',
          'error' => 'PW wrong' );
      }
    } else {
      $DataJson = array (
        'status' => 'failed',
        'error' => 'User not found.' );
    }
  }

  sleep(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();
  }
}

/**
 * Send Message
 */
function sendMessage(){
  global $DataJson, $_REQUEST;

  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 {
    if( mMessage_Send( $_REQUEST['uid'], $_REQUEST['text']) === true) {
    	$DataJson = array( 'status' => 'success');
    } else {
      $DataJson = array(
        'status' => 'failed',
        'error' => 'Transmitting was terminated with an Error.');
    }
  }
}

?>