wehandler

Github api create repo, upload files to repo, create gist v0.1 by wehandler

Nov 16th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.24 KB | None | 0 0
  1. <?php
  2. /* GIST FUNCTION PHP CLASS GITHUB API V3
  3.  * simple php class gist github api v3
  4.  *
  5.  * @description very simple Php class to interact with gist github api v3
  6.  * @version 0.1 beta version
  7.  * @author azars aka wehandler
  8.  * @created date 30 Oct 2017
  9.  *
  10.  * @usage: available usage is Create gist files
  11.   you can recoded or add available function by using the class code.
  12.  * special thanks to akshay9 github. script fixed by azars aka wehandler
  13.  * @licence: THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND INCLUDING BUT NOT LIMITED THE                                                                                                            
  14. WARRANTY (IMPLIED OR OTHERWISE) OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.                                                                                                                                                                                                      
  15. YOU ARE FREE TO MODIFY AND OR USE THIS SOFTWARE FOR OPEN SOURCE AND COMMERCIAL PROJECTS.                                                                                                                                                                                                  
  16. NO ATTRIBUTION IS REQUIRED.
  17.  */
  18.  
  19. class GistEdit {
  20.  
  21.     private $data;
  22.    
  23.     private static $_instance = NULL ;
  24.    
  25.     public static function init () {
  26.         if (self::$_instance === NULL) {
  27.             self::$_instance = new self;
  28.         }
  29.         self::$_instance->data = array();
  30.         return self::$_instance;
  31.     }
  32.    
  33.     public function edit ($file, $newContent = NULL, $newFileName = NULL) {
  34.        
  35.         if ($newContent !== NULL) {
  36.             $this->data[$file]['content'] = $newContent ;
  37.         }
  38.         if ($newFileName !== NULL) {
  39.             $this->data[$file]['filename'] = $newFileName ;
  40.         }
  41.         return $this;
  42.     }
  43.    
  44.     public function deleteFile ($file) {
  45.         $this->data[$file] = NULL ;
  46.         return $this;
  47.     }
  48.    
  49.     public function newFile ($file, $content){
  50.         $this->data[$file]['content'] = $content;
  51.         return $this;
  52.     }
  53.    
  54.     public function get () {
  55.         return $this->data;
  56.     }
  57.  
  58. }
  59.  
  60. class gistAPI {
  61.    
  62.     private $url = "https://api.github.com" ;
  63.    
  64.     private $user = "github" ;
  65.    
  66.     public $ch ;
  67.    
  68.     private $response ;
  69.    
  70.     public $loginInfo ;
  71.    
  72.    
  73.     function __construct($id = NULL, $pass = NULL) {
  74.         if($id === NULL || $pass === NULL){
  75.             $loginInfo = NULL;
  76.         } else {
  77.             $loginInfo = array('username' => $id,
  78.                                'password' => $pass);
  79.         }
  80.         $this->loginInfo = $loginInfo;
  81.         $this->chReset();
  82.     }
  83.    
  84.     public function listGists ($type = "public", $user = NULL) {
  85.    
  86.         switch ($type) {
  87.             case "public":
  88.                 curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/public");
  89.                 break;
  90.             case "user":
  91.                 curl_setopt($this->ch, CURLOPT_URL, $this->url . "/users/" . ($user === NULL ? $this->user:$user) ."/gists");
  92.                 break;
  93.             case "starred":
  94.                 curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/starred");
  95.                 break;
  96.         }
  97.         return $this->returnCode();
  98.    
  99.     }
  100.    
  101.     public function getGist ($gistId) {
  102.    
  103.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/".$gistId);
  104.         return $this->returnCode();
  105.    
  106.     }
  107.    
  108.     public function createGist ($files, $description = "", $content, $public = false) {
  109.    
  110.         $postArray = array(
  111.     'description' => $description,
  112.     'public' => 'true',
  113.     'files' => array(
  114.         $files => array(
  115.             'content' => $content
  116.         )
  117.     )
  118. );
  119.         $jsonArray = json_encode($postArray);
  120.        
  121.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists");
  122.         curl_setopt($this->ch, CURLOPT_POST, 1);
  123.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  124.         return $this->returnCode();
  125.    
  126.     }
  127.  
  128. public function createrepo ($name, $description = false, $hmpage = false, $private = false) {
  129.    
  130.         $postArray = array(
  131.     'name' => $name,
  132.     'description' => $description,
  133.     'homepage' => $hmpage,
  134.     'private' => $private,
  135.    
  136. );
  137.         $jsonArray = json_encode($postArray);
  138.        
  139.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/user/repos");
  140.         curl_setopt($this->ch, CURLOPT_POST, 1);
  141.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  142.         return $this->returnCode();
  143.    
  144.     }
  145. public function createrepofiles ($name, $user, $repo, $msg, $content, $branch = null, array $committer = null) {
  146.    
  147.         $postArray = array(
  148.     'content' => base64_encode($content),
  149.     'message' => $msg
  150.    
  151. );
  152.  
  153. if(null!== $branch) {
  154. if(!isset($committer['name'], $committer['email'])) {
  155. throw new MissingArgumentException(array('name' ,'email'));
  156. }
  157. $postArray['committer'] = $committer;
  158. }  
  159. $jsonArray = json_encode($postArray);
  160.        
  161.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/repos/" . $user . "/" . $repo . "/contents/" . $name);
  162.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  163.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  164.         return $this->returnCode();
  165.    
  166.     }
  167.        
  168.  
  169.  
  170.    
  171.     public function editGist ($gistId, $files = NULL, $description = NULL) {
  172.    
  173.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
  174.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
  175.         if ($files === NULL && $description !== NULL) {
  176.             $jsonArray = json_encode(array("description" => $description));
  177.         } elseif ($description === NULL && $files !== NULL) {
  178.             $jsonArray = json_encode(array("files" => $files));
  179.         } elseif ($description !== NULL && $files !== NULL) {
  180.             $jsonArray = json_encode(array("description" => $description, "files" => $files));
  181.         } else {
  182.             $this->chReset();
  183.             return 0;
  184.         }
  185.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  186.         return $this->returnCode();
  187.    
  188.     }
  189.    
  190.     public function gistCommits ($gistId) {
  191.    
  192.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/" . $gistId . "/commits");
  193.         return $this->returnCode();
  194.    
  195.     }
  196.    
  197.     public function starGist ($gistId) {
  198.    
  199.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
  200.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  201.         return $this->returnCode();
  202.    
  203.     }
  204.    
  205.     public function unstarGist ($gistId) {
  206.    
  207.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
  208.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  209.         return $this->returnCode();
  210.    
  211.     }
  212.    
  213.     public function checkStarGist ($gistId) {
  214.    
  215.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
  216.         return $this->returnCode();
  217.    
  218.     }
  219.    
  220.     public function forkGist ($gistId) {
  221.    
  222.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
  223.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'POST');
  224.         return $this->returnCode();
  225.    
  226.     }
  227.    
  228.     public function listForkGist ($gistId) {
  229.    
  230.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
  231.         return $this->returnCode();
  232.    
  233.     }
  234.    
  235.     public function deleteGist ($gistId) {
  236.    
  237.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
  238.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  239.         return $this->returnCode();
  240.    
  241.     }
  242.    
  243.     public function gistComments ($gistId) {
  244.    
  245.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/".$gistId."/comments");
  246.         return $this->returnCode();
  247.    
  248.     }
  249.    
  250.     public function getComment ($gistId, $commentId) {
  251.    
  252.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
  253.         return $this->returnCode();
  254.    
  255.     }
  256.    
  257.     public function createComment ($gistId, $comment){
  258.    
  259.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments");
  260.         curl_setopt($this->ch, CURLOPT_POST, 1);
  261.         $jsonArray = json_encode(array("body" => $comment));
  262.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  263.         return $this->returnCode();
  264.    
  265.     }
  266.    
  267.     public function editComment ($gistId, $commentId, $comment) {
  268.    
  269.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
  270.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
  271.         $jsonArray = json_encode(array("body" => $comment));
  272.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  273.         return $this->returnCode();
  274.    
  275.     }
  276.    
  277.     public function deleteComment ($gistId, $commentId) {
  278.    
  279.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
  280.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  281.         return $this->returnCode();
  282.    
  283.     }
  284.    
  285.     public function getLimits() {
  286.    
  287.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/rate_limit");
  288.         return $this->returnCode();
  289.     }
  290.    
  291.     private function parseHeader ($header_text) {
  292.        
  293.         $headers = array();
  294.         foreach (explode("\r\n", $header_text) as $i => $line){
  295.             if (strlen($line) > 1 && $i != 0){
  296.                 list ($key, $value) = explode(': ', $line);
  297.                 $headers[$key] = $value;
  298.             } else if ($i == 0){
  299.                 $headers['http_code'] = $line;
  300.             }
  301.         }
  302.         return $headers;
  303.     }
  304.    
  305.     private function returnCode () {
  306.    
  307.         $this->response = curl_exec($this->ch);
  308.         $header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
  309.         $header = substr($this->response, 0, $header_size);
  310.         $body = substr($this->response, $header_size);
  311.         $return = array("header" => $this->parseHeader($header),
  312.                      "body"   => json_decode($body, true),
  313.                      "raw"    => $this->response);
  314.         $this->chReset();
  315.         return $return;
  316.     }
  317.    
  318.     public function chReset () {
  319.    
  320.         $this->ch = curl_init();
  321.         curl_setopt($this->ch, CURLOPT_HEADER,         true);
  322.         curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
  323.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  324.         curl_setopt($this->ch, CURLOPT_TIMEOUT,        30);
  325.         curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  326.         if ($this->loginInfo !== NULL){
  327.             $this->user = $this->loginInfo['username'];
  328.             curl_setopt($this->ch, CURLOPT_USERAGENT, $this->loginInfo['username']);
  329.             curl_setopt($this->ch, CURLOPT_USERPWD, $this->loginInfo['username'].":".$this->loginInfo['password']);
  330.         } else {
  331.             curl_setopt($this->ch, CURLOPT_USERAGENT, "gistAPI v1.0");
  332.         }
  333.         unset($this->response);
  334.    
  335.     }
  336.    
  337.     function __destruct() {
  338.         curl_close($this->ch);
  339.     }
  340.  
  341. }
  342. /* Create gist files */
  343. echo '<center>
  344.  
  345. <form method="post">
  346.  
  347. <b>Create gist files github api v3 by azars aka wehandler</b>
  348.  
  349. <br/>Filename with .ext<input class="inp-text" type="text" name="flname"
  350.  
  351. value="testing.php">
  352.  
  353. <br/>Description <input class="inp-text" type="text" name="desc"
  354.  
  355. value="this php files examples">
  356.  
  357. <br/>Files content
  358. <textarea name="content"> Enter content here... </textarea>
  359.  
  360. <br/>
  361. <input name="input" class="inp-btn" type="submit" value="submit">
  362.  
  363. </form></center>';
  364.  
  365.  
  366.  
  367. if(isset($_POST['input'])) {
  368.  
  369. $flname=$_POST['flname'];
  370. $desc=$_POST['desc'];
  371. $content=$_POST['content'];
  372.  
  373. //$gistAPI = new gistAPI (); //To Authenticate anonymous is enable
  374. $gistAPI = new gistAPI('xxxxx','610dbf267c3224ef9b2593deff5bd3f7071b26bd');
  375. //disable to Authenticate with username and password
  376. $limits = $gistAPI->getLimits();
  377.  
  378. //Create A New Public Gist
  379. //public is true or false is private
  380. //private gists are hidden from search engines but visible to anyone you give the URL
  381.  
  382. $newgist = $gistAPI->createGist($flname, $desc, $content, true);
  383.  
  384. print_r($newgist);
  385. }
  386. /* Create repository */
  387. echo '<center>
  388.  
  389. <form method="post">
  390.  
  391. <b>Create repository files github api v3 by azars aka wehandler</b>
  392.  
  393. <br/>Filename <input class="inp-text" type="text" name="flname"
  394.  
  395. value="new repo test">
  396.  
  397. <br/>Description <input class="inp-text" type="text" name="desc"
  398.  
  399. value="this php files examples">
  400.  
  401. <br/>homepage
  402. <textarea name="homepage"> github.com </textarea>
  403.  
  404. <br/>
  405. <input name="inputnewrepo" class="inp-btn" type="submit" value="submit">
  406.  
  407. </form></center>';
  408.  
  409.  
  410.  
  411. if(isset($_POST['inputnewrepo'])) {
  412.  
  413. $flname=$_POST['flname'];
  414. $desc=$_POST['desc'];
  415. $content=$_POST['homepage'];
  416.  
  417. //$gistAPI = new gistAPI (); //Authenticate with anonymous is disable
  418. $gistAPI = new gistAPI('xxxxxxx','xxxx12345');
  419. //Authenticate with username and password is enable
  420. $limits = $gistAPI->getLimits();
  421.  
  422. //Create A New Public repository
  423. //public is false or true is private
  424.  
  425. $newgist = $gistAPI->createrepo($flname, $desc, $content, true);
  426.  
  427. print_r($newgist);
  428. }
  429.  
  430. /* Create repository files */
  431. echo '<center>
  432.  
  433. <form method="post">
  434.  
  435. <b>Create repository new files github api v3 by azars aka wehandler</b>
  436.  
  437. <br/>Filename <input class="inp-text" type="text" name="flname"
  438.  
  439. value="newfl.txt">
  440.  
  441. <br/>user <input class="inp-text" type="text" name="user"
  442.  
  443. value="aznoisib">
  444.  
  445.  
  446. <br/>repo <input class="inp-text" type="text" name="repo"
  447.  
  448. value="new-repo-test">
  449. <br/>msg <input class="inp-text" type="text" name="msg"
  450.  
  451. value="Initial Commit">
  452. <br/>content
  453. <textarea name="ctn"> bXkgbmV3IGZpbGUgY29udGVudHM= </textarea>
  454.  
  455. <br/>
  456. <input name="inputnewrepofiles" class="inp-btn" type="submit" value="submit">
  457.  
  458. </form></center>';
  459.  
  460.  
  461.  
  462. if(isset($_POST['inputnewrepofiles'])) {
  463.  
  464. $flname=$_POST['flname'];
  465. $user=$_POST['user'];
  466. $repo=$_POST['repo'];
  467. $msg=$_POST['msg'];
  468. $content=$_POST['ctn'];
  469.  
  470. //$gistAPI = new gistAPI (); //Authenticate with anonymous is disable
  471. $gistAPI = new gistAPI('xxxxx','610dbf267c3224ef9b2593deff5bd3f7071b26bd');
  472. //Authenticate with username and password is enable
  473. $limits = $gistAPI->getLimits();
  474.  
  475. //Create A New Public repository
  476. //public is false or true is private
  477.  
  478. $newgist = $gistAPI->createrepofiles($flname, $user, $repo, $msg, $content);
  479.  
  480. print_r($newgist);
  481. }
  482.  
  483.  
  484. ?>
Add Comment
Please, Sign In to add comment