Ejemplo n.º 1
0
 public static function respond()
 {
     ini_set("display_errors", 0);
     set_exception_handler(["Fight\\APIWrapper", "error_handler"]);
     register_shutdown_function(["Fight\\APIWrapper", "fatal_handler"]);
     $url = $_SERVER["REQUEST_URI"];
     if (strrpos($url, "?")) {
         $url = substr($url, 0, strrpos($url, "?"));
     }
     $path = explode("/", $url);
     $path = $path[count($path) - 1];
     $params = json_decode(file_get_contents("php://input"), TRUE) ?: [];
     $params = array_merge($_POST, $params);
     if ($params["text"] === null && $path !== "login") {
         throw new Exception("Text missing", 400);
     }
     $text = $params["text"];
     if ($path === "fight") {
         $command = explode(" ", $text)[0];
         if (Main::isMethod($command)) {
             $path = $command;
             $text = substr($text, strlen($command) + 1);
         }
     }
     if ($path === "login") {
         if ($params["email"] === null) {
             throw new Exception("Email is missing", 400);
         }
         $result = Main::login($params);
     } else {
         if ($params["user_id"] === null) {
             throw new Exception("You are not logged in!", 401);
         }
         if ($params["team_id"] === null) {
             throw new Exception("Team ID missing", 400);
         }
         if ($params["channel_id"] === null) {
             throw new Exception("Channel ID missing", 400);
         }
         $result = Main::main($path, ["text" => $text, "user_id" => $params["user_id"], "user_name" => $params["user_name"], "team_id" => $params["team_id"], "channel_id" => $params["channel_id"]]);
     }
     $status_header = 'HTTP/1.1 ' . $result["status"] . ' ' . getStatusCodeMessage($result["status"]);
     header($status_header);
     header('Content-Type: application/json');
     $attachments = [];
     foreach ($result["data"] as $update) {
         $attachment = $update->toAttachment($result["user"]);
         if ($attachment) {
             $attachments[] = $attachment;
         }
     }
     echo json_encode($attachments);
     self::$complete = true;
 }