| 
									
										
										
										
											2017-06-29 04:09:47 -06:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-09 19:18:19 -06: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-06-29 04:09:47 -06:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Mobile app API | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // The name of the permission needed to log in.
 | 
					
						
							|  |  |  | // Set to null if you don't need it.
 | 
					
						
							|  |  |  | $access_permission = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require __DIR__ . "/../required.php"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | header('Content-Type: application/json'); | 
					
						
							| 
									
										
										
										
											2017-07-03 03:13:29 -06:00
										 |  |  | header('Access-Control-Allow-Origin: *'); | 
					
						
							| 
									
										
										
										
											2017-06-29 04:09:47 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Allow ping check without authentication
 | 
					
						
							|  |  |  | if ($VARS['action'] == "ping") { | 
					
						
							|  |  |  |     exit(json_encode(["status" => "OK"])); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function mobile_enabled() { | 
					
						
							| 
									
										
										
										
											2018-12-14 21:16:31 -07:00
										 |  |  |     $resp = AccountHubApi::get("mobileenabled"); | 
					
						
							| 
									
										
										
										
											2017-06-29 04:09:47 -06:00
										 |  |  |     if ($resp['status'] == "OK" && $resp['mobile'] === TRUE) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function mobile_valid($username, $code) { | 
					
						
							| 
									
										
										
										
											2018-12-14 21:16:31 -07:00
										 |  |  |     try { | 
					
						
							|  |  |  |         $resp = AccountHubApi::get("mobilevalid", ["code" => $code, "username" => $username], true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($resp['status'] == "OK" && $resp['valid'] === TRUE) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } catch (Exception $ex) { | 
					
						
							| 
									
										
										
										
											2017-06-29 04:09:47 -06:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (mobile_enabled() !== TRUE) { | 
					
						
							| 
									
										
										
										
											2018-09-07 15:03:42 -06:00
										 |  |  |     exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("mobile login disabled", false)])); | 
					
						
							| 
									
										
										
										
											2017-06-29 04:09:47 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Make sure we have a username and access key
 | 
					
						
							| 
									
										
										
										
											2018-12-04 19:48:23 -07:00
										 |  |  | if (empty($VARS['username']) || empty($VARS['key'])) { | 
					
						
							| 
									
										
										
										
											2017-06-29 04:09:47 -06:00
										 |  |  |     http_response_code(401); | 
					
						
							|  |  |  |     die(json_encode(["status" => "ERROR", "msg" => "Missing username and/or access key."])); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Make sure the username and key are actually legit
 | 
					
						
							|  |  |  | if (!mobile_valid($VARS['username'], $VARS['key'])) { | 
					
						
							|  |  |  |     engageRateLimit(); | 
					
						
							|  |  |  |     http_response_code(401); | 
					
						
							|  |  |  |     die(json_encode(["status" => "ERROR", "msg" => "Invalid username and/or access key."])); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Process the action
 | 
					
						
							|  |  |  | switch ($VARS['action']) { | 
					
						
							|  |  |  |     case "start_session": | 
					
						
							|  |  |  |         // Do a web login.
 | 
					
						
							| 
									
										
										
										
											2018-09-07 15:03:42 -06:00
										 |  |  |         $user = User::byUsername($VARS['username']); | 
					
						
							|  |  |  |         if ($user->exists()) { | 
					
						
							|  |  |  |             if ($user->getStatus()->getString() == "NORMAL") { | 
					
						
							|  |  |  |                 if ($user->checkPassword($VARS['password'])) { | 
					
						
							|  |  |  |                     if (is_null($access_permission) || $user->hasPermission($access_permission)) { | 
					
						
							|  |  |  |                         Session::start($user); | 
					
						
							| 
									
										
										
										
											2017-11-13 16:14:40 -07:00
										 |  |  |                         $_SESSION['mobile'] = true; | 
					
						
							| 
									
										
										
										
											2017-06-29 04:09:47 -06:00
										 |  |  |                         exit(json_encode(["status" => "OK"])); | 
					
						
							|  |  |  |                     } else { | 
					
						
							| 
									
										
										
										
											2018-09-07 15:03:42 -06:00
										 |  |  |                         exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("no admin permission", false)])); | 
					
						
							| 
									
										
										
										
											2017-06-29 04:09:47 -06:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-07 15:03:42 -06:00
										 |  |  |         exit(json_encode(["status" => "ERROR", "msg" => $Strings->get("login incorrect", false)])); | 
					
						
							| 
									
										
										
										
											2017-06-29 04:09:47 -06:00
										 |  |  |     default: | 
					
						
							|  |  |  |         http_response_code(404); | 
					
						
							|  |  |  |         die(json_encode(["status" => "ERROR", "msg" => "The requested action is not available."])); | 
					
						
							|  |  |  | } |