| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 13:27:09 -07:00
										 |  |  | /* This Source Code Form is subject to the terms of the Mozilla Public | 
					
						
							|  |  |  |  * License, v. 2.0. If a copy of the MPL was not distributed with this | 
					
						
							|  |  |  |  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Simple JSON API to allow other apps to access accounts in this system. | 
					
						
							| 
									
										
										
										
											2018-01-03 21:53:42 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |  * Requests can be sent via either GET or POST requests.  POST is recommended | 
					
						
							|  |  |  |  * as it has a lower chance of being logged on the server, exposing unencrypted | 
					
						
							|  |  |  |  * user passwords. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | require __DIR__ . '/required.php'; | 
					
						
							|  |  |  | require_once __DIR__ . '/lib/login.php'; | 
					
						
							|  |  |  | header("Content-Type: application/json"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //try {
 | 
					
						
							|  |  |  | $key = $VARS['key']; | 
					
						
							|  |  |  | if ($database->has('apikeys', ['key' => $key]) !== TRUE) { | 
					
						
							| 
									
										
										
										
											2017-06-16 18:40:12 -06:00
										 |  |  |     engageRateLimit(); | 
					
						
							|  |  |  |     http_response_code(403); | 
					
						
							| 
									
										
										
										
											2017-05-01 14:29:22 -06:00
										 |  |  |     insertAuthLog(14, null, "Key: " . $key); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |     die("\"403 Unauthorized\""); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-01 14:29:22 -06:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Get the API key with most of the characters replaced with *s. | 
					
						
							|  |  |  |  * @global string $key | 
					
						
							|  |  |  |  * @return string | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function getCensoredKey() { | 
					
						
							|  |  |  |     global $key; | 
					
						
							|  |  |  |     $resp = $key; | 
					
						
							|  |  |  |     if (strlen($key) > 5) { | 
					
						
							|  |  |  |         for ($i = 2; $i < strlen($key) - 2; $i++) { | 
					
						
							|  |  |  |             $resp[$i] = "*"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return $resp; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  | switch ($VARS['action']) { | 
					
						
							|  |  |  |     case "ping": | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK"])); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "auth": | 
					
						
							| 
									
										
										
										
											2017-05-06 23:19:22 -06:00
										 |  |  |         $errmsg = ""; | 
					
						
							|  |  |  |         if (authenticate_user($VARS['username'], $VARS['password'], $errmsg)) { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |             insertAuthLog(12, null, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |             exit(json_encode(["status" => "OK", "msg" => lang("login successful", false)])); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |             insertAuthLog(13, $uid, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-05-06 23:19:22 -06:00
										 |  |  |             if (!is_empty($errmsg)) { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang2("ldap error", ['error' => $errmsg], false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (user_exists($VARS['username'])) { | 
					
						
							|  |  |  |                 switch (get_account_status($VARS['username'])) { | 
					
						
							|  |  |  |                     case "LOCKED_OR_DISABLED": | 
					
						
							|  |  |  |                         exit(json_encode(["status" => "ERROR", "msg" => lang("account locked", false)])); | 
					
						
							|  |  |  |                     case "TERMINATED": | 
					
						
							|  |  |  |                         exit(json_encode(["status" => "ERROR", "msg" => lang("account terminated", false)])); | 
					
						
							|  |  |  |                     case "CHANGE_PASSWORD": | 
					
						
							|  |  |  |                         exit(json_encode(["status" => "ERROR", "msg" => lang("password expired", false)])); | 
					
						
							| 
									
										
										
										
											2017-05-14 12:42:02 -06:00
										 |  |  |                     case "NORMAL": | 
					
						
							|  |  |  |                         break; | 
					
						
							| 
									
										
										
										
											2017-05-06 23:19:22 -06:00
										 |  |  |                     default: | 
					
						
							|  |  |  |                         exit(json_encode(["status" => "ERROR", "msg" => lang("account state error", false)])); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |             exit(json_encode(["status" => "ERROR", "msg" => lang("login incorrect", false)])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "userinfo": | 
					
						
							| 
									
										
										
										
											2017-05-01 14:38:36 -06:00
										 |  |  |         if (!is_empty($VARS['username'])) { | 
					
						
							| 
									
										
										
										
											2017-05-06 23:19:22 -06:00
										 |  |  |             if (user_exists_local($VARS['username'])) { | 
					
						
							| 
									
										
										
										
											2017-12-30 11:30:48 -07:00
										 |  |  |                 $data = $database->select("accounts", ["uid", "username", "realname (name)", "email", "phone" => ["phone1 (1)", "phone2 (2)"], 'pin'], ["username" => strtolower($VARS['username'])])[0]; | 
					
						
							|  |  |  |                 $data['pin'] = (is_null($data['pin']) || $data['pin'] == "" ? false : true); | 
					
						
							| 
									
										
										
										
											2017-05-01 14:38:36 -06:00
										 |  |  |                 exit(json_encode(["status" => "OK", "data" => $data])); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("login incorrect", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else if (!is_empty($VARS['uid'])) { | 
					
						
							|  |  |  |             if ($database->has('accounts', ['uid' => $VARS['uid']])) { | 
					
						
							| 
									
										
										
										
											2017-12-30 11:30:48 -07:00
										 |  |  |                 $data = $database->select("accounts", ["uid", "username", "realname (name)", "email", "phone" => ["phone1 (1)", "phone2 (2)"], 'pin'], ["uid" => $VARS['uid']])[0]; | 
					
						
							|  |  |  |                 $data['pin'] = (is_null($data['pin']) || $data['pin'] == "" ? false : true); | 
					
						
							| 
									
										
										
										
											2017-05-01 14:38:36 -06:00
										 |  |  |                 exit(json_encode(["status" => "OK", "data" => $data])); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("login incorrect", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-06-16 18:40:12 -06:00
										 |  |  |             http_response_code(400); | 
					
						
							| 
									
										
										
										
											2017-05-01 14:38:36 -06:00
										 |  |  |             die("\"400 Bad Request\""); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "userexists": | 
					
						
							| 
									
										
										
										
											2017-05-05 17:13:55 -06:00
										 |  |  |         if (!is_empty($VARS['uid'])) { | 
					
						
							|  |  |  |             if ($database->has('accounts', ['uid' => $VARS['uid']])) { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "OK", "exists" => true])); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "OK", "exists" => false])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-05-06 23:19:22 -06:00
										 |  |  |         if (user_exists_local($VARS['username'])) { | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |             exit(json_encode(["status" => "OK", "exists" => true])); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK", "exists" => false])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "hastotp": | 
					
						
							|  |  |  |         if (userHasTOTP($VARS['username'])) { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK", "otp" => true])); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK", "otp" => false])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "verifytotp": | 
					
						
							|  |  |  |         if (verifyTOTP($VARS['username'], $VARS['code'])) { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK", "valid" => true])); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |             insertAuthLog(7, null, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |             exit(json_encode(["status" => "ERROR", "msg" => lang("2fa incorrect", false), "valid" => false])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "acctstatus": | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "account" => get_account_status($VARS['username'])])); | 
					
						
							|  |  |  |     case "login": | 
					
						
							| 
									
										
										
										
											2017-06-16 18:40:12 -06:00
										 |  |  |         engageRateLimit(); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |         // simulate a login, checking account status and alerts
 | 
					
						
							| 
									
										
										
										
											2017-05-06 23:19:22 -06:00
										 |  |  |         $errmsg = ""; | 
					
						
							|  |  |  |         if (authenticate_user($VARS['username'], $VARS['password'], $errmsg)) { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |             $uid = $database->select('accounts', 'uid', ['username' => strtolower($VARS['username'])])[0]; | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |             switch (get_account_status($VARS['username'])) { | 
					
						
							|  |  |  |                 case "LOCKED_OR_DISABLED": | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |                     insertAuthLog(5, $uid, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |                     exit(json_encode(["status" => "ERROR", "msg" => lang("account locked", false)])); | 
					
						
							|  |  |  |                 case "TERMINATED": | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |                     insertAuthLog(5, $uid, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |                     exit(json_encode(["status" => "ERROR", "msg" => lang("account terminated", false)])); | 
					
						
							|  |  |  |                 case "CHANGE_PASSWORD": | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |                     insertAuthLog(5, $uid, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |                     exit(json_encode(["status" => "ERROR", "msg" => lang("password expired", false)])); | 
					
						
							|  |  |  |                 case "NORMAL": | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |                     insertAuthLog(4, $uid, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |                     exit(json_encode(["status" => "OK"])); | 
					
						
							|  |  |  |                 case "ALERT_ON_ACCESS": | 
					
						
							|  |  |  |                     sendLoginAlertEmail($VARS['username']); | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |                     insertAuthLog(4, $uid, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |                     exit(json_encode(["status" => "OK", "alert" => true])); | 
					
						
							|  |  |  |                 default: | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |                     insertAuthLog(5, $uid, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |                     exit(json_encode(["status" => "ERROR", "msg" => lang("account state error", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |             insertAuthLog(5, null, "Username: " . strtolower($VARS['username']) . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-05-06 23:19:22 -06:00
										 |  |  |             if (!is_empty($errmsg)) { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang2("ldap error", ['error' => $errmsg], false)])); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |             exit(json_encode(["status" => "ERROR", "msg" => lang("login incorrect", false)])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "ismanagerof": | 
					
						
							| 
									
										
										
										
											2018-01-03 21:53:42 -07:00
										 |  |  |         if ($VARS['uid'] == "1") { | 
					
						
							| 
									
										
										
										
											2017-05-02 19:18:59 -06:00
										 |  |  |             if ($database->has("accounts", ['uid' => $VARS['manager']])) { | 
					
						
							|  |  |  |                 if ($database->has("accounts", ['uid' => $VARS['employee']])) { | 
					
						
							|  |  |  |                     $managerid = $VARS['manager']; | 
					
						
							|  |  |  |                     $employeeid = $VARS['employee']; | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2017-05-02 19:18:59 -06:00
										 |  |  |                     exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false), "user" => $VARS['employee']])); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2017-05-02 19:18:59 -06:00
										 |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false), "user" => $VARS['manager']])); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-05-06 23:19:22 -06:00
										 |  |  |             if (user_exists_local($VARS['manager'])) { | 
					
						
							|  |  |  |                 if (user_exists_local($VARS['employee'])) { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |                     $managerid = $database->select('accounts', 'uid', ['username' => strtolower($VARS['manager'])]); | 
					
						
							|  |  |  |                     $employeeid = $database->select('accounts', 'uid', ['username' => strtolower($VARS['employee'])]); | 
					
						
							| 
									
										
										
										
											2017-05-02 19:18:59 -06:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |                     exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false), "user" => strtolower($VARS['employee'])])); | 
					
						
							| 
									
										
										
										
											2017-05-02 19:18:59 -06:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false), "user" => strtolower($VARS['manager'])])); | 
					
						
							| 
									
										
										
										
											2017-05-02 19:18:59 -06:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($database->has('managers', ['AND' => ['managerid' => $managerid, 'employeeid' => $employeeid]])) { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK", "managerof" => true])); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK", "managerof" => false])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-05-03 13:32:11 -06:00
										 |  |  |     case "getmanaged": | 
					
						
							|  |  |  |         if ($VARS['uid']) { | 
					
						
							|  |  |  |             if ($database->has("accounts", ['uid' => $VARS['uid']])) { | 
					
						
							|  |  |  |                 $managerid = $VARS['uid']; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else if ($VARS['username']) { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |             if ($database->has("accounts", ['username' => strtolower($VARS['username'])])) { | 
					
						
							|  |  |  |                 $managerid = $database->select('accounts', 'uid', ['username' => strtolower($VARS['username'])]); | 
					
						
							| 
									
										
										
										
											2017-05-03 13:32:11 -06:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-06-16 18:40:12 -06:00
										 |  |  |             http_response_code(400); | 
					
						
							| 
									
										
										
										
											2017-05-03 13:32:11 -06:00
										 |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-10-10 12:36:18 -06:00
										 |  |  |         if ($VARS['get'] == "username") { | 
					
						
							|  |  |  |             $managed = $database->select('managers', ['[>]accounts' => ['employeeid' => 'uid']], 'username', ['managerid' => $managerid]); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $managed = $database->select('managers', 'employeeid', ['managerid' => $managerid]); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-05-03 13:32:11 -06:00
										 |  |  |         exit(json_encode(["status" => "OK", "employees" => $managed])); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "getmanagers": | 
					
						
							|  |  |  |         if ($VARS['uid']) { | 
					
						
							|  |  |  |             if ($database->has("accounts", ['uid' => $VARS['uid']])) { | 
					
						
							|  |  |  |                 $empid = $VARS['uid']; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else if ($VARS['username']) { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |             if ($database->has("accounts", ['username' => strtolower($VARS['username'])])) { | 
					
						
							|  |  |  |                 $empid = $database->select('accounts', 'uid', ['username' => strtolower($VARS['username'])]); | 
					
						
							| 
									
										
										
										
											2017-05-03 13:32:11 -06:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-06-16 18:40:12 -06:00
										 |  |  |             http_response_code(400); | 
					
						
							| 
									
										
										
										
											2017-05-03 13:32:11 -06:00
										 |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $managers = $database->select('managers', 'managerid', ['employeeid' => $empid]); | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "managers" => $managers])); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-05-02 19:18:59 -06:00
										 |  |  |     case "usersearch": | 
					
						
							|  |  |  |         if (is_empty($VARS['search']) || strlen($VARS['search']) < 3) { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK", "result" => []])); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-05-05 17:13:55 -06:00
										 |  |  |         $data = $database->select('accounts', ['uid', 'username', 'realname (name)'], ["OR" => ['username[~]' => $VARS['search'], 'realname[~]' => $VARS['search']], "LIMIT" => 10]); | 
					
						
							| 
									
										
										
										
											2017-05-02 19:18:59 -06:00
										 |  |  |         exit(json_encode(["status" => "OK", "result" => $data])); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-05-25 00:43:33 -06:00
										 |  |  |     case "permission": | 
					
						
							|  |  |  |         if (is_empty($VARS['code'])) { | 
					
						
							| 
									
										
										
										
											2017-06-16 18:40:12 -06:00
										 |  |  |             http_response_code(400); | 
					
						
							| 
									
										
										
										
											2017-05-25 00:43:33 -06:00
										 |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $perm = $VARS['code']; | 
					
						
							|  |  |  |         if ($VARS['uid']) { | 
					
						
							|  |  |  |             if ($database->has("accounts", ['uid' => $VARS['uid']])) { | 
					
						
							|  |  |  |                 $user = $database->select('accounts', ['username'], ['uid' => $VARS['uid']])[0]['username']; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else if ($VARS['username']) { | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |             if ($database->has("accounts", ['username' => strtolower($VARS['username'])])) { | 
					
						
							| 
									
										
										
										
											2017-05-25 00:43:33 -06:00
										 |  |  |                 $user = $VARS['username']; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-06-16 18:40:12 -06:00
										 |  |  |             http_response_code(400); | 
					
						
							| 
									
										
										
										
											2017-05-25 00:43:33 -06:00
										 |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $hasperm = account_has_permission($user, $perm); | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "has_permission" => $hasperm])); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-06-16 22:25:26 -06:00
										 |  |  |     case "mobileenabled": | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "mobile" => MOBILE_ENABLED])); | 
					
						
							|  |  |  |     case "mobilevalid": | 
					
						
							|  |  |  |         if (is_empty($VARS['username']) || is_empty($VARS['code'])) { | 
					
						
							|  |  |  |             http_response_code(400); | 
					
						
							|  |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-07-03 03:00:23 -06:00
										 |  |  |         $code = strtoupper($VARS['code']); | 
					
						
							|  |  |  |         $user_key_valid = $database->has('mobile_codes', ['[>]accounts' => ['uid' => 'uid']], ["AND" => ['mobile_codes.code' => $code, 'accounts.username' => strtolower($VARS['username'])]]); | 
					
						
							| 
									
										
										
										
											2017-06-16 22:25:26 -06:00
										 |  |  |         exit(json_encode(["status" => "OK", "valid" => $user_key_valid])); | 
					
						
							| 
									
										
										
										
											2017-06-23 15:48:45 -06:00
										 |  |  |     case "alertemail": | 
					
						
							|  |  |  |         engageRateLimit(); | 
					
						
							|  |  |  |         if (is_empty($VARS['username']) || !user_exists($VARS['username'])) { | 
					
						
							|  |  |  |             http_response_code(400); | 
					
						
							|  |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $appname = "???"; | 
					
						
							|  |  |  |         if (!is_empty($VARS['appname'])) { | 
					
						
							|  |  |  |             $appname = $VARS['appname']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $result = sendLoginAlertEmail($VARS['username'], $appname); | 
					
						
							|  |  |  |         if ($result === TRUE) { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK"])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         exit(json_encode(["status" => "ERROR", "msg" => $result])); | 
					
						
							| 
									
										
										
										
											2017-12-18 01:44:53 -07:00
										 |  |  |     case "codelogin": | 
					
						
							|  |  |  |         $database->delete("onetimekeys", ["expires[<]" => date("Y-m-d H:i:s")]); // cleanup
 | 
					
						
							|  |  |  |         if ($database->has("onetimekeys", ["key" => $VARS['code'], "expires[>]" => date("Y-m-d H:i:s")])) { | 
					
						
							|  |  |  |             $user = $database->get("onetimekeys", ["[>]accounts" => ["uid" => "uid"]], ["username", "realname", "accounts.uid"], ["key" => $VARS['code']]); | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK", "user" => $user])); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "ERROR", "msg" => lang("no such code or code expired", false)])); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-20 17:36:25 -07:00
										 |  |  |     case "listapps": | 
					
						
							|  |  |  |         $apps = EXTERNAL_APPS; | 
					
						
							|  |  |  |         // Format paths as absolute URLs
 | 
					
						
							|  |  |  |         foreach ($apps as $k => $v) { | 
					
						
							|  |  |  |             if (strpos($apps[$k]['url'], "http") === FALSE) { | 
					
						
							|  |  |  |                 $apps[$k]['url'] = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . ($_SERVER['SERVER_PORT'] != 80 || $_SERVER['SERVER_PORT'] != 443 ? ":" . $_SERVER['SERVER_PORT'] : "") . $apps[$k]['url']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "apps" => $apps])); | 
					
						
							| 
									
										
										
										
											2017-12-21 01:21:18 -07:00
										 |  |  |     case "getusersbygroup": | 
					
						
							|  |  |  |         if ($VARS['gid']) { | 
					
						
							|  |  |  |             if ($database->has("groups", ['groupid' => $VARS['gid']])) { | 
					
						
							|  |  |  |                 $groupid = $VARS['gid']; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("group does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             http_response_code(400); | 
					
						
							|  |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($VARS['get'] == "username") { | 
					
						
							| 
									
										
										
										
											2018-01-19 14:19:33 -07:00
										 |  |  |             $users = $database->select('assigned_groups', ['[>]accounts' => ['uid' => 'uid']], 'username', ['groupid' => $groupid, "ORDER" => "username"]); | 
					
						
							| 
									
										
										
										
											2017-12-28 16:12:15 -07:00
										 |  |  |         } else if ($VARS['get'] == "detail") { | 
					
						
							| 
									
										
										
										
											2018-01-19 14:19:33 -07:00
										 |  |  |             $users = $database->select('assigned_groups', ['[>]accounts' => ['uid' => 'uid']], ['username', 'realname (name)', 'accounts.uid', 'pin'], ['groupid' => $groupid, "ORDER" => "realname"]); | 
					
						
							| 
									
										
										
										
											2017-12-30 11:30:48 -07:00
										 |  |  |             for ($i = 0; $i < count($users); $i++) { | 
					
						
							|  |  |  |                 if (is_null($users[$i]['pin']) || $users[$i]['pin'] == "") { | 
					
						
							|  |  |  |                     $users[$i]['pin'] = false; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $users[$i]['pin'] = true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-12-21 01:21:18 -07:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $users = $database->select('assigned_groups', 'uid', ['groupid' => $groupid]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "users" => $users])); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "getgroupsbyuser": | 
					
						
							|  |  |  |         if ($VARS['uid']) { | 
					
						
							|  |  |  |             if ($database->has("accounts", ['uid' => $VARS['uid']])) { | 
					
						
							|  |  |  |                 $empid = $VARS['uid']; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else if ($VARS['username']) { | 
					
						
							|  |  |  |             if ($database->has("accounts", ['username' => strtolower($VARS['username'])])) { | 
					
						
							|  |  |  |                 $empid = $database->select('accounts', 'uid', ['username' => strtolower($VARS['username'])]); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             http_response_code(400); | 
					
						
							|  |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $groups = $database->select('assigned_groups', ["[>]groups" => ["groupid" => "groupid"]], ['groups.groupid (id)', 'groups.groupname (name)'], ['uid' => $empid]); | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "groups" => $groups])); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "getgroups": | 
					
						
							|  |  |  |         $groups = $database->select('groups', ['groupid (id)', 'groupname (name)']); | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "groups" => $groups])); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case "groupsearch": | 
					
						
							|  |  |  |         if (is_empty($VARS['search']) || strlen($VARS['search']) < 2) { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "OK", "result" => []])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $data = $database->select('groups', ['groupid (id)', 'groupname (name)'], ['groupname[~]' => $VARS['search'], "LIMIT" => 10]); | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "result" => $data])); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-12-30 11:30:48 -07:00
										 |  |  |     case "checkpin": | 
					
						
							|  |  |  |         $pin = ""; | 
					
						
							|  |  |  |         if (is_empty($VARS['pin'])) { | 
					
						
							|  |  |  |             http_response_code(400); | 
					
						
							|  |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!is_empty($VARS['username'])) { | 
					
						
							|  |  |  |             if (user_exists_local($VARS['username'])) { | 
					
						
							|  |  |  |                 $pin = $database->get("accounts", "pin", ["username" => strtolower($VARS['username'])]); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("login incorrect", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else if (!is_empty($VARS['uid'])) { | 
					
						
							|  |  |  |             if ($database->has('accounts', ['uid' => $VARS['uid']])) { | 
					
						
							|  |  |  |                 $pin = $database->get("accounts", "pin", ["uid" => strtolower($VARS['uid'])]); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("login incorrect", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             http_response_code(400); | 
					
						
							|  |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (is_null($pin) || $pin == "") { | 
					
						
							|  |  |  |             exit(json_encode(["status" => "ERROR", "pinvalid" => false, "nopinset" => true])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         exit(json_encode(["status" => "OK", "pinvalid" => ($pin == $VARS['pin'])])); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2017-06-16 18:40:12 -06:00
										 |  |  |         http_response_code(404); | 
					
						
							| 
									
										
										
										
											2017-06-16 22:25:26 -06:00
										 |  |  |         die(json_encode("404 Not Found: the requested action is not available.")); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  | } | 
					
						
							|  |  |  |     /* } catch (Exception $e) { | 
					
						
							|  |  |  |       header("HTTP/1.1 500 Internal Server Error"); | 
					
						
							|  |  |  |       die("\"500 Internal Server Error\""); | 
					
						
							| 
									
										
										
										
											2018-01-03 21:53:42 -07:00
										 |  |  |       } */ |