| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Simple JSON API to allow other apps to access accounts in this system. | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * 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) { | 
					
						
							|  |  |  |     header("HTTP/1.1 403 Unauthorized"); | 
					
						
							| 
									
										
										
										
											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-05-01 14:29:22 -06:00
										 |  |  |             insertAuthLog(12, null, "Username: " . $VARS['username'] . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |             exit(json_encode(["status" => "OK", "msg" => lang("login successful", false)])); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-05-06 23:19:22 -06:00
										 |  |  |             insertAuthLog(13, $uid, "Username: " . $VARS['username'] . ", Key: " . getCensoredKey()); | 
					
						
							|  |  |  |             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)])); | 
					
						
							|  |  |  |                     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-05-01 14:38:36 -06:00
										 |  |  |                 $data = $database->select("accounts", ["uid", "username", "realname (name)", "email", "phone" => ["phone1 (1)", "phone2 (2)"]], ["username" => $VARS['username']])[0]; | 
					
						
							|  |  |  |                 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']])) { | 
					
						
							|  |  |  |                 $data = $database->select("accounts", ["uid", "username", "realname (name)", "email", "phone" => ["phone1 (1)", "phone2 (2)"]], ["uid" => $VARS['uid']])[0]; | 
					
						
							|  |  |  |                 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-05-01 14:38:36 -06:00
										 |  |  |             header("HTTP/1.1 400 Bad Request"); | 
					
						
							|  |  |  |             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-05-01 14:29:22 -06:00
										 |  |  |             insertAuthLog(7, null, "Username: " . $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": | 
					
						
							|  |  |  |         // 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-05-01 14:29:22 -06:00
										 |  |  |             $uid = $database->select('accounts', 'uid', ['username' => $VARS['username']])[0]; | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |             switch (get_account_status($VARS['username'])) { | 
					
						
							|  |  |  |                 case "LOCKED_OR_DISABLED": | 
					
						
							| 
									
										
										
										
											2017-05-01 14:29:22 -06:00
										 |  |  |                     insertAuthLog(5, $uid, "Username: " . $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-05-01 14:29:22 -06:00
										 |  |  |                     insertAuthLog(5, $uid, "Username: " . $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-05-01 14:29:22 -06:00
										 |  |  |                     insertAuthLog(5, $uid, "Username: " . $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-05-01 14:29:22 -06:00
										 |  |  |                     insertAuthLog(4, $uid, "Username: " . $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-05-01 14:29:22 -06:00
										 |  |  |                     insertAuthLog(4, $uid, "Username: " . $VARS['username'] . ", Key: " . getCensoredKey()); | 
					
						
							| 
									
										
										
										
											2017-04-29 02:35:49 -06:00
										 |  |  |                     exit(json_encode(["status" => "OK", "alert" => true])); | 
					
						
							|  |  |  |                 default: | 
					
						
							| 
									
										
										
										
											2017-05-01 14:29:22 -06:00
										 |  |  |                     insertAuthLog(5, $uid, "Username: " . $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-05-01 14:29:22 -06:00
										 |  |  |             insertAuthLog(5, null, "Username: " . $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": | 
					
						
							| 
									
										
										
										
											2017-05-02 19:18:59 -06:00
										 |  |  |         if ($VARS['uid'] === 1) { | 
					
						
							|  |  |  |             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-05-02 19:18:59 -06:00
										 |  |  |                     $managerid = $database->select('accounts', 'uid', ['username' => $VARS['manager']]); | 
					
						
							|  |  |  |                     $employeeid = $database->select('accounts', 'uid', ['username' => $VARS['employee']]); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false), "user" => $VARS['employee']])); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false), "user" => $VARS['manager']])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         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']) { | 
					
						
							|  |  |  |             if ($database->has("accounts", ['username' => $VARS['username']])) { | 
					
						
							|  |  |  |                 $managerid = $database->select('accounts', 'uid', ['username' => $VARS['username']]); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             header("HTTP/1.1 400 Bad Request"); | 
					
						
							|  |  |  |             die("\"400 Bad Request\""); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $managed = $database->select('managers', 'employeeid', ['managerid' => $managerid]); | 
					
						
							|  |  |  |         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']) { | 
					
						
							|  |  |  |             if ($database->has("accounts", ['username' => $VARS['username']])) { | 
					
						
							|  |  |  |                 $empid = $database->select('accounts', 'uid', ['username' => $VARS['username']]); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 exit(json_encode(["status" => "ERROR", "msg" => lang("user does not exist", false)])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             header("HTTP/1.1 400 Bad Request"); | 
					
						
							|  |  |  |             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; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         header("HTTP/1.1 400 Bad Request"); | 
					
						
							|  |  |  |         die("\"400 Bad Request\""); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  |     /* } catch (Exception $e) { | 
					
						
							|  |  |  |       header("HTTP/1.1 500 Internal Server Error"); | 
					
						
							|  |  |  |       die("\"500 Internal Server Error\""); | 
					
						
							|  |  |  |       } */     |