curl.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php namespace Lib;
  2. class HTTPUnexpectedResponse extends \Exception {
  3. static $ecode = array(
  4. 100 => 'Continue',
  5. 101 => 'Switching Protocols',
  6. 102 => 'Processing', // WebDAV; RFC 2518
  7. 200 => 'OK',
  8. 201 => 'Created',
  9. 202 => 'Accepted',
  10. 203 => 'Non-Authoritative Information', // since HTTP/1.1
  11. 204 => 'No Content',
  12. 205 => 'Reset Content',
  13. 206 => 'Partial Content',
  14. 207 => 'Multi-Status', // WebDAV; RFC 4918
  15. 208 => 'Already Reported', // WebDAV; RFC 5842
  16. 226 => 'IM Used', // RFC 3229
  17. 300 => 'Multiple Choices',
  18. 301 => 'Moved Permanently',
  19. 302 => 'Found',
  20. 303 => 'See Other', // since HTTP/1.1
  21. 304 => 'Not Modified',
  22. 305 => 'Use Proxy', // since HTTP/1.1
  23. 306 => 'Switch Proxy',
  24. 307 => 'Temporary Redirect', // since HTTP/1.1
  25. 308 => 'Permanent Redirect', // approved as experimental RFC
  26. 400 => 'Bad Request',
  27. 401 => 'Unauthorized',
  28. 402 => 'Payment Required',
  29. 403 => 'Forbidden',
  30. 404 => 'Not Found',
  31. 405 => 'Method Not Allowed',
  32. 406 => 'Not Acceptable',
  33. 407 => 'Proxy Authentication Required',
  34. 408 => 'Request Timeout',
  35. 409 => 'Conflict',
  36. 410 => 'Gone',
  37. 411 => 'Length Required',
  38. 412 => 'Precondition Failed',
  39. 413 => 'Request Entity Too Large',
  40. 414 => 'Request-URI Too Long',
  41. 415 => 'Unsupported Media Type',
  42. 416 => 'Requested Range Not Satisfiable',
  43. 417 => 'Expectation Failed',
  44. 418 => 'I\'m a teapot', // RFC 2324
  45. 419 => 'Authentication Timeout', // not in RFC 2616
  46. 420 => 'Enhance Your Calm', // Twitter
  47. 420 => 'Method Failure', // Spring Framework
  48. 422 => 'Unprocessable Entity', // WebDAV; RFC 4918
  49. 423 => 'Locked', // WebDAV; RFC 4918
  50. 424 => 'Failed Dependency', // WebDAV; RFC 4918
  51. 424 => 'Method Failure', // WebDAV)
  52. 425 => 'Unordered Collection', // Internet draft
  53. 426 => 'Upgrade Required', // RFC 2817
  54. 428 => 'Precondition Required', // RFC 6585
  55. 429 => 'Too Many Requests', // RFC 6585
  56. 431 => 'Request Header Fields Too Large', // RFC 6585
  57. 444 => 'No Response', // Nginx
  58. 449 => 'Retry With', // Microsoft
  59. 450 => 'Blocked by Windows Parental Controls', // Microsoft
  60. 451 => 'Redirect', // Microsoft
  61. 451 => 'Unavailable For Legal Reasons', // Internet draft
  62. 494 => 'Request Header Too Large', // Nginx
  63. 495 => 'Cert Error', // Nginx
  64. 496 => 'No Cert', // Nginx
  65. 497 => 'HTTP to HTTPS', // Nginx
  66. 499 => 'Client Closed Request', // Nginx
  67. 500 => 'Internal Server Error',
  68. 501 => 'Not Implemented',
  69. 502 => 'Bad Gateway',
  70. 503 => 'Service Unavailable',
  71. 504 => 'Gateway Timeout',
  72. 505 => 'HTTP Version Not Supported',
  73. 506 => 'Variant Also Negotiates', // RFC 2295
  74. 507 => 'Insufficient Storage', // WebDAV; RFC 4918
  75. 508 => 'Loop Detected', // WebDAV; RFC 5842
  76. 509 => 'Bandwidth Limit Exceeded', // Apache bw/limited extension
  77. 510 => 'Not Extended', // RFC 2774
  78. 511 => 'Network Authentication Required', // RFC 6585
  79. 598 => 'Network read timeout error', // Unknown
  80. 599 => 'Network connect timeout error', // Unknown
  81. );
  82. }
  83. class NotAutorizedException extends \Exception{}
  84. trait Curl {
  85. protected $url;
  86. protected $token;
  87. protected $user_agent = "Gogs PHP Api Client/0.0 (compatible; LINUX)";
  88. protected $timeout = 30;
  89. protected $max_redirects = 4;
  90. protected function method(&$req, $scope, $params, $post, $ret) {
  91. echo sprintf(
  92. "URL: %s{%s} Token: %s\n",
  93. $this->url,
  94. $scope,
  95. $this->token
  96. );
  97. $c = curl_init();
  98. if (!$c) {
  99. return false;
  100. }
  101. if ($post) {
  102. curl_setopt($c, CURLOPT_POST, 1);
  103. curl_setopt($c, CURLOPT_POSTFIELDS, $params);
  104. }
  105. curl_setopt($c, CURLOPT_USERAGENT, $this->user_agent);
  106. curl_setopt($c, CURLOPT_URL, sprintf("%s%s", $this->url, $scope));
  107. curl_setopt($c, CURLOPT_HTTPHEADER, array(
  108. sprintf("Authorization: token %s", $this->token)
  109. ));
  110. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  111. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  112. curl_setopt($c, CURLOPT_MAXREDIRS, $this->max_redirects);
  113. curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
  114. $req = curl_exec($c);
  115. $status_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  116. curl_close($c);
  117. return $status_code;
  118. }
  119. protected function authorized($scope) {
  120. if ($this->test($scope, 401)) {
  121. throw new NotAutorizedException("Not authorized", 401);
  122. }
  123. return true;
  124. }
  125. protected function test($scope, $code = 200) {
  126. $req = "";
  127. return $this->method($ret, $scope, array(), false, false) == $code;
  128. }
  129. private function post($scope, $params) {
  130. $req = "";
  131. $code = $this->method($req, $scope, $params, true, true);
  132. switch ($code) {
  133. case 200:
  134. return $req;
  135. default:
  136. throw new HTTPUnexpectedResponse(HTTPUnexpectedResponse::$ecode[$code], $code);
  137. }
  138. }
  139. private function get($scope, $params) {
  140. $req = "";
  141. $code = $this->method($req, $scope, $params, false, true);
  142. switch ($code) {
  143. case 200:
  144. return $req;
  145. default:
  146. throw new HTTPUnexpectedResponse(HTTPUnexpectedResponse::$ecode[$code], $code);
  147. }
  148. }
  149. }