Client.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.1
  9. */
  10. trait Client {
  11. private static $log = array();
  12. protected $url;
  13. protected $token;
  14. protected $user_agent = "Gogs PHP Api Curl\\Client/0.1 (compatible; LINUX)";
  15. protected $timeout = 30;
  16. protected $max_redirects = 4;
  17. /**
  18. * array_2_params takes an array and converts it into a
  19. * query string (e.g param=val&param2=val2).
  20. *
  21. * @param array $params parameters to pass
  22. * @return string
  23. */
  24. private function array_2_params(array $params) {
  25. return join("&", array_map(function($k, $v) {
  26. return sprintf("%s=%s", $k, rawurlencode(is_bool($v) ? ($v ? "true" : "false") : $v ));
  27. }, array_keys($params), $params));
  28. }
  29. /**
  30. * array_2_json takes an array and converts it into a
  31. * json-string (e.g {'name': 'This'}) which is typically
  32. * used in a request body.
  33. *
  34. * @param array $params paramters to pass
  35. * @return string
  36. */
  37. private function array_2_json(array $params) {
  38. return count($params) == 0 ? null : json_encode($params);
  39. }
  40. /**
  41. * Initializes a curl request of different kinds, depending
  42. * on the specified method. This can be
  43. *
  44. * DELETE, PATCH, POST or GET. An unidentified value will
  45. * become a GET-request.
  46. *
  47. * @param string $method either DELETE, PATCH, POST, GET
  48. * @param string &$req variable to store request body in
  49. * @param string $scope scope within the API (e.g /user/repos)
  50. * @param array $params parameters to pass
  51. * @param bool $ret return transfer
  52. * @return int the status code
  53. */
  54. protected function method(string $method, string &$req, string $scope, array $params, bool $ret) {
  55. $c = curl_init();
  56. if (!$c) {
  57. return false;
  58. }
  59. $headers = array(
  60. sprintf("Authorization: token %s", $this->token),
  61. );
  62. $url = sprintf("%s%s", $this->url, $scope);
  63. array_push(
  64. self::$log,
  65. sprintf(
  66. "%s:[%s] %s, %s, %s",
  67. date("y-m-d H:i:s"),
  68. $method,
  69. $url,
  70. !empty($p = $this->array_2_json($params)) ? $p : "none",
  71. get_class($this)
  72. )
  73. );
  74. if (in_array($method, array("DELETE", "PATCH", "POST"))) {
  75. $json = $this->array_2_json($params);
  76. curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
  77. curl_setopt($c, CURLOPT_POSTFIELDS, $json);
  78. array_unshift($headers, "Content-Type: application/json");
  79. array_push($headers, "Content-Length: " . strlen($json));
  80. } else {
  81. $url .= "?" . $this->array_2_params($params);
  82. }
  83. curl_setopt($c, CURLOPT_USERAGENT, $this->user_agent);
  84. curl_setopt($c, CURLOPT_URL, $url);
  85. curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
  86. curl_setopt($c, CURLOPT_RETURNTRANSFER, $ret);
  87. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  88. curl_setopt($c, CURLOPT_MAXREDIRS, $this->max_redirects);
  89. curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
  90. $req = curl_exec($c);
  91. $status_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  92. curl_close($c);
  93. array_push(
  94. self::$log,
  95. sprintf(
  96. "%s:[%s] %s, %d, %s",
  97. date("y-m-d H:i:s"),
  98. $method,
  99. $url,
  100. $status_code,
  101. substr($req, 0, 100) . (strlen($req) > 100 ? "..." : ".")
  102. )
  103. );
  104. return $status_code;
  105. }
  106. /**
  107. * Checks if the user is authorized for the scope. Shouldn't
  108. * be used frequently. One test for one scope should be enough,
  109. * but if you know for sure thats you're programming with the
  110. * use of an authorized user you should leave this and just
  111. * handle the NotAuthorizedExeption whenever thrown.
  112. *
  113. * @param $scope the scope, a relative uri.
  114. * @throws Not AuthorizedException if server responde with a 401
  115. * @return bool
  116. */
  117. protected function authorized(string $scope = "") {
  118. $ret = "";
  119. if (in_array(($code = $this->method("GET", $ret, $scope, array(), false)),
  120. array(400, 401, 402, 403)
  121. )) {
  122. throw new NotAuthorizedException("Not authorized", 401);
  123. }
  124. return true;
  125. }
  126. /**
  127. * Post method.
  128. *
  129. * @param string $scope the scope, a relative uri.
  130. * @param array $params the parameters to post.
  131. * @throws NotAuthorizedException on 401, 403
  132. * @throws HTTPUnexpectedResponse when not 200,201,401,403
  133. * @return string the request content.
  134. */
  135. private function post(string $scope = "", array $params = array()) {
  136. $req = "";
  137. $code = $this->method("POST", $req, $scope, $params, true);
  138. switch ($code) {
  139. case 200:
  140. case 201:
  141. return $req;
  142. case 400:
  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. * Delete method.
  152. *
  153. * @param string $scope the scope, a relative uri.
  154. * @throws NotAuthorizedException on 401, 403
  155. * @throws HTTPUnexpectedResponse when not 200,204,401,403
  156. * @return string the request content.
  157. */
  158. private function delete(string $scope = "") {
  159. $req = "";
  160. $code = $this->method("DELETE", $req, $scope, array(), true);
  161. switch ($code) {
  162. case 200:
  163. case 204:
  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. * GET method.
  174. *
  175. * @param string $scope the scope, a relative uri.
  176. * @param array $params the parameters to post.
  177. * @throws NotAuthorizedException on 401, 403
  178. * @throws HTTPUnexpectedResponse when not 200,401,403
  179. * @return string the request content.
  180. */
  181. private function get($scope = "", $params = array()) {
  182. $req = "";
  183. $code = $this->method("GET", $req, $scope, $params, true);
  184. switch ($code) {
  185. case 200:
  186. return $req;
  187. case 401:
  188. case 403:
  189. throw new Exception\NotAuthorizedException($req, $code);
  190. default:
  191. throw new Exception\HTTPUnexpectedResponse($req, $code);
  192. }
  193. }
  194. /**
  195. * Returns log entries for the client.
  196. *
  197. * @return array
  198. */
  199. public static function get_log() {
  200. if (empty(self::$log))
  201. return self::$log;
  202. return array_merge(self::$log, array("\n"));
  203. }
  204. }
  205. }
  206. ?>