| 
									
										
										
										
											2018-12-14 21:16:31 -07:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * 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/. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountHubApi { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function get(string $action, array $data = null, bool $throwex = false) { | 
					
						
							| 
									
										
										
										
											2018-12-20 23:45:45 -07:00
										 |  |  |         global $SETTINGS; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 21:16:31 -07:00
										 |  |  |         $content = [ | 
					
						
							|  |  |  |             "action" => $action, | 
					
						
							| 
									
										
										
										
											2018-12-20 23:45:45 -07:00
										 |  |  |             "key" => $SETTINGS['accounthub']['key'] | 
					
						
							| 
									
										
										
										
											2018-12-14 21:16:31 -07:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         if (!is_null($data)) { | 
					
						
							|  |  |  |             $content = array_merge($content, $data); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $options = [ | 
					
						
							|  |  |  |             'http' => [ | 
					
						
							|  |  |  |                 'method' => 'POST', | 
					
						
							|  |  |  |                 'content' => json_encode($content), | 
					
						
							|  |  |  |                 'header' => "Content-Type: application/json\r\n" . | 
					
						
							|  |  |  |                 "Accept: application/json\r\n", | 
					
						
							|  |  |  |                 "ignore_errors" => true | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $context = stream_context_create($options); | 
					
						
							| 
									
										
										
										
											2018-12-20 23:45:45 -07:00
										 |  |  |         $result = file_get_contents($SETTINGS['accounthub']['api'], false, $context); | 
					
						
							| 
									
										
										
										
											2018-12-14 21:16:31 -07:00
										 |  |  |         $response = json_decode($result, true); | 
					
						
							|  |  |  |         if ($result === false || !AccountHubApi::checkHttpRespCode($http_response_header) || json_last_error() != JSON_ERROR_NONE) { | 
					
						
							|  |  |  |             if ($throwex) { | 
					
						
							|  |  |  |                 throw new Exception($result); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 sendError($result); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $response; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static function checkHttpRespCode(array $headers): bool { | 
					
						
							|  |  |  |         foreach ($headers as $header) { | 
					
						
							|  |  |  |             if (preg_match("/HTTP\/[0-9]\.[0-9] [0-9]{3}.*/", $header)) { | 
					
						
							|  |  |  |                 $respcode = explode(" ", $header)[1] * 1; | 
					
						
							|  |  |  |                 if ($respcode >= 200 && $respcode < 300) { | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |