diff --git a/Src/config/config.php b/Src/config/config.php index bc77948..078848a 100644 --- a/Src/config/config.php +++ b/Src/config/config.php @@ -41,5 +41,6 @@ function loadConfig() require_once "lib/functions.php"; require_once "lib/database.php"; +require_once "lib/request.php"; require_once "lib/appveyor.php"; require_once "lib/github.php"; diff --git a/Src/lib/appveyor.php b/Src/lib/appveyor.php index 21342e0..0e830d5 100644 --- a/Src/lib/appveyor.php +++ b/Src/lib/appveyor.php @@ -5,54 +5,7 @@ function requestAppVeyor($url, $data = null) global $appVeyorKey; $baseUrl = "https://ci.appveyor.com/api/"; + $url = $baseUrl . $url; - $headers = array(); - $headers[] = "User-Agent: " . USER_AGENT; - $headers[] = "Content-type: application/json"; - $headers[] = "Authorization: Bearer " . $appVeyorKey; - - $fields = array( - CURLOPT_URL => $baseUrl . $url, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HEADER => true, - CURLOPT_ENCODING => "", - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_SSL_VERIFYHOST => false, - CURLOPT_HTTPHEADER => $headers - ); - - if ($data !== null) { - $fields[CURLOPT_POST] = true; - $fields[CURLOPT_POSTFIELDS] = json_encode($data); - } - - $curl = curl_init(); - - curl_setopt_array($curl, $fields); - - $response = curl_exec($curl); - - if ($response === false) { - echo htmlspecialchars($url); - echo "\r\n"; - die(curl_error($curl)); - } - - if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) { - echo htmlspecialchars($url); - echo "\r\n"; - die($response); - } - - $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); - $header = substr($response, 0, $headerSize); - $headers = extractHeaders($header); - $body = substr($response, $headerSize); - curl_close($curl); - - return array("headers" => $headers, "body" => $body); + return doRequest($url, $appVeyorKey, $data); } diff --git a/Src/lib/github.php b/Src/lib/github.php index 8064e89..a76c485 100644 --- a/Src/lib/github.php +++ b/Src/lib/github.php @@ -9,51 +9,9 @@ function requestGitHub($gitHubToken, $url, $data = null) { $baseUrl = "https://api.github.com/"; + $url = $baseUrl . $url; - $headers = array(); - $headers[] = "User-Agent: " . USER_AGENT; - $headers[] = "Content-type: application/json"; - $headers[] = "Authorization: Bearer " . $gitHubToken; - - $fields = array( - CURLOPT_URL => $baseUrl . $url, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HEADER => true, - CURLOPT_ENCODING => "", - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_SSL_VERIFYHOST => false, - CURLOPT_HTTPHEADER => $headers - ); - - if ($data !== null) { - $fields[CURLOPT_POST] = true; - $fields[CURLOPT_POSTFIELDS] = json_encode($data); - } - - $curl = curl_init(); - - curl_setopt_array($curl, $fields); - - $response = curl_exec($curl); - - if ($response === false) { - echo htmlspecialchars($url); - echo "\r\n"; - die(curl_error($curl)); - } - - $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); - $header = substr($response, 0, $headerSize); - $headers = extractHeaders($header); - $body = substr($response, $headerSize); - $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); - curl_close($curl); - - return array("status" => $http_code, "headers" => $headers, "body" => $body); + return doRequest($url, $gitHubToken, $data); } function generateAppToken() diff --git a/Src/lib/request.php b/Src/lib/request.php new file mode 100644 index 0000000..e8853c4 --- /dev/null +++ b/Src/lib/request.php @@ -0,0 +1,50 @@ + $url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HEADER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_HTTPHEADER => $headers + ); + + if ($data !== null) { + $fields[CURLOPT_POST] = true; + $fields[CURLOPT_POSTFIELDS] = json_encode($data); + } + + $curl = curl_init(); + + curl_setopt_array($curl, $fields); + + $response = curl_exec($curl); + + if ($response === false) { + echo htmlspecialchars($url); + echo "\r\n"; + die(curl_error($curl)); + } + + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + $header = substr($response, 0, $headerSize); + $headers = extractHeaders($header); + $body = substr($response, $headerSize); + $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + + return array("status" => $http_code, "headers" => $headers, "body" => $body); +}