Client.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 (in_array(($code = $this->method("GET", $ret, $scope, array(), false)),
  98. array(400, 401, 402, 403)
  99. )) {
  100. throw new NotAuthorizedException("Not authorized", 401);
  101. }
  102. return true;
  103. }
  104. /**
  105. * Post method.
  106. *
  107. * @param string $scope the scope, a relative uri.
  108. * @param array $params the parameters to post.
  109. * @throws NotAuthorizedException on 401, 403
  110. * @throws HTTPUnexpectedResponse when not 200,201,401,403
  111. * @return string the request content.
  112. */
  113. private function post(string $scope = "", array $params = array()) {
  114. $req = "";
  115. $code = $this->method("POST", $req, $scope, $params, true);
  116. switch ($code) {
  117. case 200:
  118. case 201:
  119. return $req;
  120. case 400:
  121. case 401:
  122. case 403:
  123. throw new Exception\NotAuthorizedException($req, $code);
  124. default:
  125. throw new Exception\HTTPUnexpectedResponse($req, $code);
  126. }
  127. }
  128. /**
  129. * Delete method.
  130. *
  131. * @param string $scope the scope, a relative uri.
  132. * @throws NotAuthorizedException on 401, 403
  133. * @throws HTTPUnexpectedResponse when not 200,204,401,403
  134. * @return string the request content.
  135. */
  136. private function delete(string $scope = "") {
  137. $req = "";
  138. $code = $this->method("DELETE", $req, $scope, array(), true);
  139. switch ($code) {
  140. case 200:
  141. case 204:
  142. return $req;
  143. case 401:
  144. case 403:
  145. throw new Exception\NotAuthorizedException($req, $code);
  146. default:
  147. throw new Exception\HTTPUnexpectedResponse($req, $code);
  148. }
  149. }
  150. /**
  151. * GET method.
  152. *
  153. * @param string $scope the scope, a relative uri.
  154. * @param array $params the parameters to post.
  155. * @throws NotAuthorizedException on 401, 403
  156. * @throws HTTPUnexpectedResponse when not 200,401,403
  157. * @return string the request content.
  158. */
  159. private function get($scope = "", $params = array()) {
  160. $req = "";
  161. $code = $this->method("GET", $req, $scope, $params, true);
  162. switch ($code) {
  163. case 200:
  164. return $req;
  165. case 401:
  166. case 403:
  167. throw new Exception\NotAuthorizedException($req, $code);
  168. default:
  169. throw new Exception\HTTPUnexpectedResponse($req, $code);
  170. }
  171. }
  172. }
  173. }
  174. ?>