An updated theme (based on Atticus Finch) for ClassicPress
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
6.5 KiB

3 years ago
  1. <?php
  2. if ( !class_exists('Puc_v4p1_Vcs_BitBucketApi', false) ):
  3. class Puc_v4p1_Vcs_BitBucketApi extends Puc_v4p1_Vcs_Api {
  4. /**
  5. * @var Puc_v4p1_OAuthSignature
  6. */
  7. private $oauth = null;
  8. /**
  9. * @var string
  10. */
  11. private $username;
  12. /**
  13. * @var string
  14. */
  15. private $repository;
  16. public function __construct($repositoryUrl, $credentials = array()) {
  17. $path = @parse_url($repositoryUrl, PHP_URL_PATH);
  18. if ( preg_match('@^/?(?P<username>[^/]+?)/(?P<repository>[^/#?&]+?)/?$@', $path, $matches) ) {
  19. $this->username = $matches['username'];
  20. $this->repository = $matches['repository'];
  21. } else {
  22. throw new InvalidArgumentException('Invalid BitBucket repository URL: "' . $repositoryUrl . '"');
  23. }
  24. parent::__construct($repositoryUrl, $credentials);
  25. }
  26. /**
  27. * Figure out which reference (i.e tag or branch) contains the latest version.
  28. *
  29. * @param string $configBranch Start looking in this branch.
  30. * @return null|Puc_v4p1_Vcs_Reference
  31. */
  32. public function chooseReference($configBranch) {
  33. $updateSource = null;
  34. //Check if there's a "Stable tag: 1.2.3" header that points to a valid tag.
  35. $updateSource = $this->getStableTag($configBranch);
  36. //Look for version-like tags.
  37. if ( !$updateSource && ($configBranch === 'master') ) {
  38. $updateSource = $this->getLatestTag();
  39. }
  40. //If all else fails, use the specified branch itself.
  41. if ( !$updateSource ) {
  42. $updateSource = $this->getBranch($configBranch);
  43. }
  44. return $updateSource;
  45. }
  46. public function getBranch($branchName) {
  47. $branch = $this->api('/refs/branches/' . $branchName);
  48. if ( is_wp_error($branch) || empty($branch) ) {
  49. return null;
  50. }
  51. return new Puc_v4p1_Vcs_Reference(array(
  52. 'name' => $branch->name,
  53. 'updated' => $branch->target->date,
  54. 'downloadUrl' => $this->getDownloadUrl($branch->name),
  55. ));
  56. }
  57. /**
  58. * Get a specific tag.
  59. *
  60. * @param string $tagName
  61. * @return Puc_v4p1_Vcs_Reference|null
  62. */
  63. public function getTag($tagName) {
  64. $tag = $this->api('/refs/tags/' . $tagName);
  65. if ( is_wp_error($tag) || empty($tag) ) {
  66. return null;
  67. }
  68. return new Puc_v4p1_Vcs_Reference(array(
  69. 'name' => $tag->name,
  70. 'version' => ltrim($tag->name, 'v'),
  71. 'updated' => $tag->target->date,
  72. 'downloadUrl' => $this->getDownloadUrl($tag->name),
  73. ));
  74. }
  75. /**
  76. * Get the tag that looks like the highest version number.
  77. *
  78. * @return Puc_v4p1_Vcs_Reference|null
  79. */
  80. public function getLatestTag() {
  81. $tags = $this->api('/refs/tags?sort=-target.date');
  82. if ( !isset($tags, $tags->values) || !is_array($tags->values) ) {
  83. return null;
  84. }
  85. //Filter and sort the list of tags.
  86. $versionTags = $this->sortTagsByVersion($tags->values);
  87. //Return the first result.
  88. if ( !empty($versionTags) ) {
  89. $tag = $versionTags[0];
  90. return new Puc_v4p1_Vcs_Reference(array(
  91. 'name' => $tag->name,
  92. 'version' => ltrim($tag->name, 'v'),
  93. 'updated' => $tag->target->date,
  94. 'downloadUrl' => $this->getDownloadUrl($tag->name),
  95. ));
  96. }
  97. return null;
  98. }
  99. /**
  100. * Get the tag/ref specified by the "Stable tag" header in the readme.txt of a given branch.
  101. *
  102. * @param string $branch
  103. * @return null|Puc_v4p1_Vcs_Reference
  104. */
  105. protected function getStableTag($branch) {
  106. $remoteReadme = $this->getRemoteReadme($branch);
  107. if ( !empty($remoteReadme['stable_tag']) ) {
  108. $tag = $remoteReadme['stable_tag'];
  109. //You can explicitly opt out of using tags by setting "Stable tag" to
  110. //"trunk" or the name of the current branch.
  111. if ( ($tag === $branch) || ($tag === 'trunk') ) {
  112. return $this->getBranch($branch);
  113. }
  114. return $this->getTag($tag);
  115. }
  116. return null;
  117. }
  118. /**
  119. * @param string $ref
  120. * @return string
  121. */
  122. protected function getDownloadUrl($ref) {
  123. return sprintf(
  124. 'https://bitbucket.org/%s/%s/get/%s.zip',
  125. $this->username,
  126. $this->repository,
  127. $ref
  128. );
  129. }
  130. /**
  131. * Get the contents of a file from a specific branch or tag.
  132. *
  133. * @param string $path File name.
  134. * @param string $ref
  135. * @return null|string Either the contents of the file, or null if the file doesn't exist or there's an error.
  136. */
  137. public function getRemoteFile($path, $ref = 'master') {
  138. $response = $this->api('src/' . $ref . '/' . ltrim($path), '1.0');
  139. if ( is_wp_error($response) || !isset($response, $response->data) ) {
  140. return null;
  141. }
  142. return $response->data;
  143. }
  144. /**
  145. * Get the timestamp of the latest commit that changed the specified branch or tag.
  146. *
  147. * @param string $ref Reference name (e.g. branch or tag).
  148. * @return string|null
  149. */
  150. public function getLatestCommitTime($ref) {
  151. $response = $this->api('commits/' . $ref);
  152. if ( isset($response->values, $response->values[0], $response->values[0]->date) ) {
  153. return $response->values[0]->date;
  154. }
  155. return null;
  156. }
  157. /**
  158. * Perform a BitBucket API 2.0 request.
  159. *
  160. * @param string $url
  161. * @param string $version
  162. * @return mixed|WP_Error
  163. */
  164. public function api($url, $version = '2.0') {
  165. $url = implode('/', array(
  166. 'https://api.bitbucket.org',
  167. $version,
  168. 'repositories',
  169. $this->username,
  170. $this->repository,
  171. ltrim($url, '/')
  172. ));
  173. if ( $this->oauth ) {
  174. $url = $this->oauth->sign($url,'GET');
  175. }
  176. $options = array('timeout' => 10);
  177. if ( !empty($this->httpFilterName) ) {
  178. $options = apply_filters($this->httpFilterName, $options);
  179. }
  180. $response = wp_remote_get($url, $options);
  181. if ( is_wp_error($response) ) {
  182. return $response;
  183. }
  184. $code = wp_remote_retrieve_response_code($response);
  185. $body = wp_remote_retrieve_body($response);
  186. if ( $code === 200 ) {
  187. $document = json_decode($body);
  188. return $document;
  189. }
  190. return new WP_Error(
  191. 'puc-bitbucket-http-error',
  192. 'BitBucket API error. HTTP status: ' . $code
  193. );
  194. }
  195. /**
  196. * @param array $credentials
  197. */
  198. public function setAuthentication($credentials) {
  199. parent::setAuthentication($credentials);
  200. if ( !empty($credentials) && !empty($credentials['consumer_key']) ) {
  201. $this->oauth = new Puc_v4p1_OAuthSignature(
  202. $credentials['consumer_key'],
  203. $credentials['consumer_secret']
  204. );
  205. } else {
  206. $this->oauth = null;
  207. }
  208. }
  209. public function signDownloadUrl($url) {
  210. //Add authentication data to download URLs. Since OAuth signatures incorporate
  211. //timestamps, we have to do this immediately before inserting the update. Otherwise
  212. //authentication could fail due to a stale timestamp.
  213. if ( $this->oauth ) {
  214. $url = $this->oauth->sign($url);
  215. }
  216. return $url;
  217. }
  218. }
  219. endif;