PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/github_download/pi.github_download.php

https://github.com/ericbarnes/github_download.ee_addon
PHP | 197 lines | 109 code | 43 blank | 45 comment | 14 complexity | 72b24ddc61b6e4f78a4b3f9fc48aa2b7 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * GitHub Download
  4. *
  5. * @package Plugin
  6. * @author Eric Barnes
  7. * @copyright Copyright (c) 2010, Eric Barnes
  8. * @link http://ericlbarnes.com
  9. * @purpose Create a download link from a tag
  10. */
  11. // ------------------------------------------------------------------------
  12. $plugin_info = array(
  13. 'pi_name' => 'Github Download',
  14. 'pi_version' => '1.0.1',
  15. 'pi_author' => 'Eric Barnes',
  16. 'pi_author_url' => 'http://ericlbarnes.com/',
  17. 'pi_description' => 'Create a download link from a git tag',
  18. 'pi_usage' => Github_download::usage()
  19. );
  20. // ------------------------------------------------------------------------
  21. class Github_download {
  22. protected $EE = '';
  23. public $return_data = '';
  24. protected $user = '';
  25. protected $repo = '';
  26. protected $tag = 'latest';
  27. protected $type = 'zipball';
  28. protected $class = 'git_download';
  29. // ------------------------------------------------------------------------
  30. /**
  31. * Constructor
  32. *
  33. * {github_download repo="mycode" tag="latest"}
  34. */
  35. public function Github_download()
  36. {
  37. $this->EE =& get_instance();
  38. if ( ! $this->_requirements())
  39. {
  40. return 'You must have json_decode';
  41. }
  42. $this->user = $this->_get_param('user');
  43. $this->repo = $this->_get_param('repo');
  44. $this->tag = $this->_get_param('tag');
  45. $this->type = $this->_get_param('type');
  46. $this->class = $this->_get_param('class');
  47. if ( ! $this->user OR ! $this->repo)
  48. {
  49. return 'ERROR NO USER OR REPO GIVEN';
  50. }
  51. // Yes this is redundant but you never know
  52. if ($this->type == 'tarball')
  53. {
  54. $type = 'tarball';
  55. }
  56. else
  57. {
  58. $type = 'zipball';
  59. }
  60. $url = 'http://github.com/'.$this->user.'/'.$this->repo.'/'.$type;
  61. if ($this->tag == 'latest')
  62. {
  63. $git_tags = $this->_fetch_tags();
  64. // Get the latest element
  65. $tag = array_pop(array_keys($git_tags['tags']));
  66. }
  67. else
  68. {
  69. $tag = $this->tag;
  70. }
  71. $this->return_data = '<a class="'.$this->class.'" href="'.$url.'/'.$tag.'">';
  72. $this->return_data .= $this->EE->TMPL->tagdata;
  73. $this->return_data .= '</a>';
  74. }
  75. // ------------------------------------------------------------------------
  76. /**
  77. * Check Server Requirements
  78. */
  79. private function _requirements()
  80. {
  81. if ( ! function_exists("json_decode"))
  82. {
  83. return FALSE;
  84. }
  85. return TRUE;
  86. }
  87. // ------------------------------------------------------------------------
  88. /**
  89. * Helper function for getting a parameter
  90. *
  91. * @param string
  92. * @return mixed
  93. */
  94. private function _get_param($key)
  95. {
  96. $val = $this->EE->TMPL->fetch_param($key);
  97. if ($val == '')
  98. {
  99. return FALSE;
  100. }
  101. return $val;
  102. }
  103. // ------------------------------------------------------------------------
  104. /**
  105. * Fetch a list of github tags.
  106. */
  107. private function _fetch_tags()
  108. {
  109. $responce = $this->_fetch_data('http://github.com/api/v2/json/repos/show/'.$this->user.'/'.$this->repo.'/tags');
  110. if (empty($responce))
  111. {
  112. return FALSE;
  113. }
  114. return $responce;
  115. }
  116. // ------------------------------------------------------------------------
  117. /**
  118. * Fetch the data from GitHub
  119. *
  120. * @access private
  121. * @param string - the API URL to use
  122. * @return object - a decoded JSON object with all the information (FALSE if nothing is returned)
  123. */
  124. private function _fetch_data($url)
  125. {
  126. $ch = curl_init();
  127. curl_setopt($ch, CURLOPT_URL, $url);
  128. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  129. $returned = curl_exec($ch);
  130. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  131. curl_close ($ch);
  132. if ($status == '200')
  133. {
  134. return json_decode($returned, TRUE);
  135. }
  136. else
  137. {
  138. return FALSE;
  139. }
  140. }
  141. // ------------------------------------------------------------------------
  142. public function usage()
  143. {
  144. ob_start();
  145. ?>
  146. {exp:github_download user="github_user" repo="mycode" tag="latest"}
  147. <?php
  148. $buffer = ob_get_contents();
  149. ob_end_clean();
  150. return $buffer;
  151. }
  152. }
  153. /* End of file pi.github_download.php */
  154. /* Location: ./system/expressionengine/third_party/github_download/pi.github_download.php */