Client.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Gogs\Lib\Curl {
  3. /**
  4. * A trait used for every class referencing the api-url and token.
  5. *
  6. * @author Joachim M. Giaever (joachim[]giaever.org)
  7. * @package curl
  8. * @version 0.1
  9. */
  10. trait Client {
  11. protected $url;
  12. protected $token;
  13. protected $user_agent = "Gogs PHP Api Curl\\Client/0.1 (compatible; LINUX)";
  14. protected $timeout = 30;
  15. protected $max_redirects = 4;
  16. /**
  17. * array_2_params takes an array and converts it into a
  18. * query string (e.g param=val&param2=val2).
  19. *
  20. * @param array $params parameters to pass
  21. * @return string
  22. */
  23. private function array_2_params(array $params) {
  24. return join("&", array_map(function($k, $v) {
  25. return sprintf("%s=%s", $k, rawurlencode(is_bool($v) ? ($v ? "true" : "false") : $v ));
  26. }, array_keys($params), $params));
  27. }
  28. /**
  29. * array_2_json takes an array and converts it into a
  30. * json-string (e.g {'name': 'This'}) which is typically
  31. * used in a request body.
  32. *
  33. * @param array $params paramters to pass
  34. * @return string
  35. */
  36. private function array_2_json(array $params) {
  37. return count($params) == 0 ? null : json_encode($params);
  38. }
  39. /**
  40. * Initializes a curl request of different kinds, depending
  41. * on the specified method. This can be
  42. *
  43. * DELETE, PATCH, POST or GET. An unidentified value will
  44. * become a GET-request.
  45. *
  46. * @param string $method either DELETE, PATCH, POST, GET
  47. * @param string &$req variable to store request body in
  48. * @param string $scope scope within the API (e.g /user/repos)
  49. * @param array $params parameters to pass
  50. * @param bool $ret return transfer
  51. * @return int the status code
  52. */
  53. protected function method(string $method, string &$req, string $scope, array $params, bool $ret) {
  54. $c = curl_init();
  55. if (!$c) {
  56. return false;
  57. }
  58. $headers = array(
  59. sprintf("Authorization: token %s", $this->token),
  60. );
  61. $url = sprintf("%s%s", $this->url, $scope);
  62. echo sprintf("%s: %s, params: %s\n", $method, $url, $this->array_2_json($params));
  63. if (in_array($method, array("DELETE", "PATCH", "POST"))) {
  64. $json = $this->array_2_json($params);
  65. curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
  66. curl_setopt($c, CURLOPT_POSTFIELDS, $json);
  67. array_unshift($headers, "Content-Type: application/json");
  68. array_push($headers, "Content-Length: " . strlen($json));
  69. } else {
  70. $url .= "?" . $this->array_2_params($params);
  71. }
  72. curl_setopt($c, CURLOPT_USERAGENT, $this->user_agent);
  73. curl_setopt($c, CURLOPT_URL, $url);
  74. curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
  75. curl_setopt($c, CURLOPT_RETURNTRANSFER, $ret);
  76. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  77. curl_setopt($c, CURLOPT_MAXREDIRS, $this->max_redirects);
  78. curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
  79. $req = curl_exec($c);
  80. $status_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  81. curl_close($c);
  82. return $status_code;
  83. }
  84. /**
  85. * Checks if the user is authorized for the scope. Shouldn't
  86. * be used frequently. One test for one scope should be enough,
  87. * but if you know for sure thats you're programming with the
  88. * use of an authorized user you should leave this and just
  89. * handle the NotAuthorizedExeption whenever thrown.
  90. *
  91. * @param $scope the scope, a relative uri.
  92. * @throws Not AuthorizedException if server responde with a 401
  93. * @return bool
  94. */
  95. protected function authorized(string $scope = "") {
  96. $ret = "";
  97. if ($this->method("GET", $ret, $scope, array(), false) == 401) {
  98. throw new NotAuthorizedException("Not authorized", 401);
  99. }
  100. return true;
  101. }
  102. /**
  103. * Post method.
  104. *
  105. * @param string $scope the scope, a relative uri.
  106. * @param array $params the parameters to post.
  107. * @throws NotAuthorizedException on 401, 403
  108. * @throws HTTPUnexpectedResponse when not 200,201,401,403
  109. * @return string the request content.
  110. */
  111. private function post(string $scope = "", array $params = array()) {
  112. $req = "";
  113. $code = $this->method("POST", $req, $scope, $params, true);
  114. switch ($code) {
  115. case 200:
  116. case 201:
  117. return $req;
  118. case 401:
  119. case 403:
  120. throw new Exception\NotAuthorizedException($req, $code);
  121. default:
  122. throw new Exception\HTTPUnexpectedResponse($req, $code);
  123. }
  124. }
  125. /**
  126. * Delete method.
  127. *
  128. * @param string $scope the scope, a relative uri.
  129. * @throws NotAuthorizedException on 401, 403
  130. * @throws HTTPUnexpectedResponse when not 200,204,401,403
  131. * @return string the request content.
  132. */
  133. private function delete(string $scope = "") {
  134. $req = "";
  135. $code = $this->method("DELETE", $req, $scope, array(), true);
  136. switch ($code) {
  137. case 200:
  138. case 204:
  139. return $req;
  140. case 401:
  141. case 403:
  142. throw new Exception\NotAuthorizedException($req, $code);
  143. default:
  144. throw new Exception\HTTPUnexpectedResponse($req, $code);
  145. }
  146. }
  147. /**
  148. * GET method.
  149. *
  150. * @param string $scope the scope, a relative uri.
  151. * @param array $params the parameters to post.
  152. * @throws NotAuthorizedException on 401, 403
  153. * @throws HTTPUnexpectedResponse when not 200,401,403
  154. * @return string the request content.
  155. */
  156. private function get($scope = "", $params = array()) {
  157. $req = "";
  158. $code = $this->method("GET", $req, $scope, $params, true);
  159. switch ($code) {
  160. case 200:
  161. return $req;
  162. case 401:
  163. case 403:
  164. throw new Exception\NotAuthorizedException($req, $code);
  165. default:
  166. throw new Exception\HTTPUnexpectedResponse($req, $code);
  167. }
  168. }
  169. }
  170. }
  171. ?>