/platform/libraries/joomla/client/github.php

https://github.com/ianmacl/pulltester · PHP · 188 lines · 124 code · 33 blank · 31 comment · 23 complexity · 6342c659658037f2bed62d8629699a7f MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Client
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.environment.uri');
  11. jimport('joomla.client.http');
  12. JLoader::register('JHttpResponse', JPATH_PLATFORM.'/joomla/client/http.php');
  13. jimport('joomla.client.github.githubpulls');
  14. jimport('joomla.client.github.githubgists');
  15. jimport('joomla.client.github.githubissues');
  16. jimport('joomla.client.github.githubrefs');
  17. jimport('joomla.client.githubobject');
  18. /**
  19. * HTTP client class.
  20. *
  21. * @package Joomla.Platform
  22. * @subpackage Client
  23. * @since 11.1
  24. */
  25. class JGithub
  26. {
  27. const AUTHENTICATION_NONE = 0;
  28. const AUTHENTICATION_BASIC = 1;
  29. const AUTHENTICATION_OAUTH = 2;
  30. /**
  31. * Authentication Method
  32. *
  33. * Possible values are 0 - no authentication, 1 - basic authentication, 2 - OAuth
  34. *
  35. * @var string
  36. * @since 11.1
  37. */
  38. protected $authentication_method = 0;
  39. protected $gists = null;
  40. protected $issues = null;
  41. protected $pulls = null;
  42. protected $refs = null;
  43. protected $credentials = array();
  44. /**
  45. * Constructor.
  46. *
  47. * @param array $options Array of configuration options for the client.
  48. *
  49. * @return void
  50. *
  51. * @since 11.1
  52. */
  53. public function __construct($options = array())
  54. {
  55. if (isset($options['username']) && isset($options['password'])) {
  56. $this->credentials['username'] = $options['username'];
  57. $this->credentials['password'] = $options['password'];
  58. $this->authentication_method = JGithub::AUTHENTICATION_BASIC;
  59. } elseif (isset($options['token'])) {
  60. $this->credentials['token'] = $options['token'];
  61. $this->authentication_method = JGithub::AUTHENTICATION_OAUTH;
  62. } else {
  63. $this->authentication_method = JGithub::AUTHENTICATION_NONE;
  64. }
  65. $this->http = curl_init();
  66. }
  67. public function __get($name)
  68. {
  69. if ($name == 'gists') {
  70. if ($this->gists == null) {
  71. $this->gists = new JGithubGists($this);
  72. }
  73. return $this->gists;
  74. }
  75. if ($name == 'issues') {
  76. if ($this->issues == null) {
  77. $this->issues = new JGithubIssues($this);
  78. }
  79. return $this->issues;
  80. }
  81. if ($name == 'pulls') {
  82. if ($this->pulls == null) {
  83. $this->pulls = new JGithubPulls($this);
  84. }
  85. return $this->pulls;
  86. }
  87. if ($name == 'refs') {
  88. if ($this->refs == null)
  89. {
  90. $this->refs = new JGithubRefs($this);
  91. }
  92. return $this->refs;
  93. }
  94. }
  95. public function sendRequest($url, $method = 'get', $data = array(), $options = array())
  96. {
  97. $this->http = curl_init();
  98. $curl_options = array(
  99. CURLOPT_URL => 'https://api.github.com'.$url,
  100. CURLOPT_RETURNTRANSFER => true,
  101. CURLOPT_HEADER => false,
  102. CURLOPT_FOLLOWLOCATION => false,
  103. CURLOPT_USERAGENT => 'JGithub',
  104. CURLOPT_CONNECTTIMEOUT => 120,
  105. CURLOPT_TIMEOUT => 120,
  106. CURLINFO_HEADER_OUT => true,
  107. CURLOPT_HTTPHEADER => array('Content-type: application/json')
  108. );
  109. switch ($this->authentication_method)
  110. {
  111. case JGithub::AUTHENTICATION_BASIC:
  112. $curl_options[CURLOPT_USERPWD] = $this->credentials['username'].':'.$this->credentials['password'];
  113. break;
  114. case JGithub::AUTHENTICATION_OAUTH:
  115. if (strpos($url, '?') === false) {
  116. $url .= '?access_token='.$this->credentials['token'];
  117. } else {
  118. $url .= '&access_token='.$this->credentials['token'];
  119. }
  120. break;
  121. }
  122. switch ($method) {
  123. case 'post':
  124. $curl_options[CURLOPT_POST] = 1;
  125. $curl_options[CURLOPT_POSTFIELDS] = json_encode($data);
  126. break;
  127. case 'put':
  128. $curl_options[CURLOPT_POST] = 1;
  129. $curl_options[CURLOPT_POSTFIELDS] = '';
  130. $curl_options[CURLOPT_CUSTOMREQUEST] = 'PUT';
  131. $curl_options[CURLOPT_HTTPGET] = false;
  132. break;
  133. case 'patch':
  134. $curl_options[CURLOPT_POSTFIELDS] = json_encode($data);
  135. case 'delete':
  136. $curl_options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
  137. $curl_options[CURLOPT_POST] = false;
  138. $curl_options[CURLOPT_HTTPGET] = false;
  139. break;
  140. case 'get':
  141. $curl_options[CURLOPT_POSTFIELDS] = null;
  142. $curl_options[CURLOPT_POST] = false;
  143. $curl_options[CURLOPT_HTTPGET] = true;
  144. break;
  145. }
  146. curl_setopt_array($this->http, $curl_options);
  147. $response = new JHttpResponse;
  148. $response->body = json_decode(curl_exec($this->http));
  149. $request_data = curl_getinfo($this->http);
  150. $response->headers = $request_data['request_header'];
  151. $response->code = $request_data['http_code'];
  152. curl_close($this->http);
  153. return $response;
  154. }
  155. }