wehandler

github api single page

Apr 12th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.47 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.     public function getauth ($cid,$token) {
  108.    
  109.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/applications/" . $cid . "/tokens/" . $token);
  110.         return $this->returnCode();
  111.    
  112.     }
  113. public function delauth ($cid,$token) {
  114.    
  115.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/applications/" . $cid . "/tokens/" . $token);
  116. curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  117.         return $this->returnCode();
  118.    
  119.     }
  120.     public function createGist ($files, $description = "", $content, $public = false) {
  121.    
  122.         $postArray = array(
  123.     'description' => $description,
  124.     'public' => 'true',
  125.     'files' => array(
  126.         $files => array(
  127.             'content' => $content
  128.         )
  129.     )
  130. );
  131.         $jsonArray = json_encode($postArray);
  132.        
  133.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists");
  134.         curl_setopt($this->ch, CURLOPT_POST, 1);
  135.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  136.         return $this->returnCode();
  137.    
  138.     }
  139. public function createauth($scopes = null, $note = null, $note_url = null, $client_id = null, $client_secret = null, $fingerprint = null)
  140.     {
  141.         $data = array();
  142.         if(!is_null($scopes))
  143.             $data['scopes'] = $scopes;
  144.         if(!is_null($note))
  145.             $data['note'] = $note;
  146.         if(!is_null($note_url))
  147.             $data['note_url'] = $note_url;
  148.         if(!is_null($client_id))
  149.             $data['client_id'] = $client_id;
  150.         if(!is_null($client_secret))
  151.             $data['client_secret'] = $client_secret;
  152.         if(!is_null($fingerprint))
  153.             $data['fingerprint'] = $fingerprint;
  154.  
  155. $jsonArray = json_encode($data);
  156.        
  157.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/authorizations");
  158.         curl_setopt($this->ch, CURLOPT_POST, 1);
  159.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  160.         return $this->returnCode();
  161.     }  
  162. public function createrepo ($name, $description = false, $hmpage = false, $private = false) {
  163.    
  164.         $postArray = array(
  165.     'name' => $name,
  166.     'description' => $description,
  167.     'homepage' => $hmpage,
  168.     'private' => $private,
  169.    
  170. );
  171.         $jsonArray = json_encode($postArray);
  172.        
  173.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/user/repos");
  174.         curl_setopt($this->ch, CURLOPT_POST, 1);
  175.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  176.         return $this->returnCode();
  177.    
  178.     }
  179. public function createrepofiles ($name, $user, $repo, $msg, $content, $branch = null, array $committer = null) {
  180.     $imagedata = file_get_contents("php codepad/rbt.php");
  181.         $postArray = array(
  182.     'content' => base64_encode($imagedata),
  183.     'message' => $msg
  184.    
  185. );
  186.  
  187. if(null!== $branch) {
  188. if(!isset($committer['name'], $committer['email'])) {
  189. throw new MissingArgumentException(array('name' ,'email'));
  190. }
  191. $postArray['committer'] = $committer;
  192. }  
  193. $jsonArray = json_encode($postArray);
  194.        
  195.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/repos/" . $user . "/" . $repo . "/contents/" . $name);
  196.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  197.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  198.         return $this->returnCode();
  199.    
  200.     }
  201.        
  202.  
  203.  
  204.    
  205.     public function editGist ($gistId, $files = NULL, $description = NULL) {
  206.    
  207.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
  208.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
  209.         if ($files === NULL && $description !== NULL) {
  210.             $jsonArray = json_encode(array("description" => $description));
  211.         } elseif ($description === NULL && $files !== NULL) {
  212.             $jsonArray = json_encode(array("files" => $files));
  213.         } elseif ($description !== NULL && $files !== NULL) {
  214.             $jsonArray = json_encode(array("description" => $description, "files" => $files));
  215.         } else {
  216.             $this->chReset();
  217.             return 0;
  218.         }
  219.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  220.         return $this->returnCode();
  221.    
  222.     }
  223.    
  224.     public function gistCommits ($gistId) {
  225.    
  226.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/" . $gistId . "/commits");
  227.         return $this->returnCode();
  228.    
  229.     }
  230.    
  231.     public function starGist ($gistId) {
  232.    
  233.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
  234.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  235.         return $this->returnCode();
  236.    
  237.     }
  238.    
  239.     public function unstarGist ($gistId) {
  240.    
  241.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
  242.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  243.         return $this->returnCode();
  244.    
  245.     }
  246.    
  247.     public function checkStarGist ($gistId) {
  248.    
  249.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
  250.         return $this->returnCode();
  251.    
  252.     }
  253.    
  254.     public function forkGist ($gistId) {
  255.    
  256.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
  257.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'POST');
  258.         return $this->returnCode();
  259.    
  260.     }
  261.    
  262.     public function listForkGist ($gistId) {
  263.    
  264.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
  265.         return $this->returnCode();
  266.    
  267.     }
  268.    
  269.     public function deleteGist ($gistId) {
  270.    
  271.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
  272.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  273.         return $this->returnCode();
  274.    
  275.     }
  276.    
  277.     public function gistComments ($gistId) {
  278.    
  279.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/".$gistId."/comments");
  280.         return $this->returnCode();
  281.    
  282.     }
  283.    
  284.     public function getComment ($gistId, $commentId) {
  285.    
  286.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
  287.         return $this->returnCode();
  288.    
  289.     }
  290.    
  291.     public function createComment ($gistId, $comment){
  292.    
  293.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments");
  294.         curl_setopt($this->ch, CURLOPT_POST, 1);
  295.         $jsonArray = json_encode(array("body" => $comment));
  296.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  297.         return $this->returnCode();
  298.    
  299.     }
  300.    
  301.     public function editComment ($gistId, $commentId, $comment) {
  302.    
  303.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
  304.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
  305.         $jsonArray = json_encode(array("body" => $comment));
  306.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  307.         return $this->returnCode();
  308.    
  309.     }
  310.    
  311.     public function deleteComment ($gistId, $commentId) {
  312.    
  313.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
  314.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  315.         return $this->returnCode();
  316.    
  317.     }
  318.    
  319.     public function getLimits() {
  320.    
  321.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/rate_limit");
  322.         return $this->returnCode();
  323.     }
  324.    
  325.     private function parseHeader ($header_text) {
  326.        
  327.         $headers = array();
  328.         foreach (explode("\r\n", $header_text) as $i => $line){
  329.             if (strlen($line) > 1 && $i != 0){
  330.                 list ($key, $value) = explode(': ', $line);
  331.                 $headers[$key] = $value;
  332.             } else if ($i == 0){
  333.                 $headers['http_code'] = $line;
  334.             }
  335.         }
  336.         return $headers;
  337.     }
  338.    
  339.     private function returnCode () {
  340.    
  341.         $this->response = curl_exec($this->ch);
  342.         $header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
  343.         $header = substr($this->response, 0, $header_size);
  344.         $body = substr($this->response, $header_size);
  345.         $return = array("header" => $this->parseHeader($header),
  346.                      "body"   => json_decode($body, true),
  347.                      "raw"    => $this->response);
  348.         $this->chReset();
  349.         return $return;
  350.     }
  351.    
  352.     public function chReset () {
  353.    
  354.         $this->ch = curl_init();
  355.         curl_setopt($this->ch, CURLOPT_HEADER,         true);
  356.         curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
  357.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  358.         curl_setopt($this->ch, CURLOPT_TIMEOUT,        30);
  359.         curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  360.         if ($this->loginInfo !== NULL){
  361.             $this->user = $this->loginInfo['username'];
  362.             curl_setopt($this->ch, CURLOPT_USERAGENT, $this->loginInfo['username']);
  363.             curl_setopt($this->ch, CURLOPT_USERPWD, $this->loginInfo['username'].":".$this->loginInfo['password']);
  364.         } else {
  365.             curl_setopt($this->ch, CURLOPT_USERAGENT, "gistAPI v1.0");
  366.         }
  367.         unset($this->response);
  368.    
  369.     }
  370.    
  371.     function __destruct() {
  372.         curl_close($this->ch);
  373.     }
  374.  
  375. }
  376. /* Create gist files */
  377. echo '<center>
  378.  
  379. <form method="post">
  380.  
  381. <b>Create gist files github api v3 by azars aka wehandler</b>
  382.  
  383. <br/>Filename with .ext<input class="inp-text" type="text" name="flname"
  384.  
  385. value="testing.php">
  386.  
  387. <br/>Description <input class="inp-text" type="text" name="desc"
  388.  
  389. value="this php files examples">
  390.  
  391. <br/>Files content
  392. <textarea name="content"> Enter content here... </textarea>
  393.  
  394. <br/>
  395. <input name="input" class="inp-btn" type="submit" value="submit">
  396.  
  397. </form></center>';
  398.  
  399.  
  400.  
  401. if(isset($_POST['input'])) {
  402.  
  403. $flname=$_POST['flname'];
  404. $desc=$_POST['desc'];
  405. $content=$_POST['content'];
  406.  
  407. //$gistAPI = new gistAPI (); //To Authenticate anonymous is enable
  408. $gistAPI = new gistAPI('aznoisib','610dbf267c3224edeff5bd3f7071b26bd');
  409. //disable to Authenticate with username and password
  410. $limits = $gistAPI->getLimits();
  411.  
  412. //Create A New Public Gist
  413. //public is true or false is private
  414. //private gists are hidden from search engines but visible to anyone you give the URL
  415.  
  416. $newgist = $gistAPI->createGist($flname, $desc, $content, true);
  417.  
  418. print_r($newgist);
  419. }
  420. /* Create repository */
  421. echo '<center>
  422.  
  423. <form method="post">
  424.  
  425. <b>Create repository files github api v3 by azars aka wehandler</b>
  426.  
  427. <br/>Filename <input class="inp-text" type="text" name="flname"
  428.  
  429. value="new repo test">
  430.  
  431. <br/>Description <input class="inp-text" type="text" name="desc"
  432.  
  433. value="this php files examples">
  434.  
  435. <br/>homepage
  436. <textarea name="homepage"> github.com </textarea>
  437.  
  438. <br/>
  439. <input name="inputnewrepo" class="inp-btn" type="submit" value="submit">
  440.  
  441. </form></center>';
  442.  
  443.  
  444.  
  445. if(isset($_POST['inputnewrepo'])) {
  446.  
  447. $flname=$_POST['flname'];
  448. $desc=$_POST['desc'];
  449. $content=$_POST['homepage'];
  450.  
  451. //$gistAPI = new gistAPI (); //Authenticate with anonymous is disable
  452. $gistAPI = new gistAPI('aznoisib','80231c6d4fcf4ea4bb7a43ad5a3e029ca3213b3b');
  453. //Authenticate with username and password is enable
  454. $limits = $gistAPI->getLimits();
  455.  
  456. //Create A New Public repository
  457. //public is false or true is private
  458.  
  459. $newgist = $gistAPI->createrepo($flname, $desc, $content, false);
  460.  
  461. print_r($newgist);
  462. }
  463.  
  464. /* create authorization apps */
  465. echo '<center>
  466.  
  467. <form method="post">
  468.  
  469. <b>create auth apps</b>
  470.  
  471. <br/>scope <input class="inp-text" type="text" name="scp"
  472.  
  473. value="new repo test">
  474.  
  475. <br/>note  <input class="inp-text" type="text" name="nte"
  476.  
  477. value="this php files examples">
  478.  
  479. <br/>note  url<input class="inp-text" type="text" name="nturl"
  480.  
  481. value="http://github.com">
  482.  
  483. <br/>client id  <input class="inp-text" type="text" name="clids"
  484.  
  485. value="jaj82829hsh">
  486.  
  487. <br/>client secret  <input class="inp-text" type="text" name="clidsc"
  488.  
  489. value="jaj82829hsh">
  490.  
  491. <br/>
  492. <input name="createauth" class="inp-btn" type="submit" value="submit">
  493.  
  494. </form></center>';
  495.  
  496.  
  497.  
  498. if(isset($_POST['createauth'])) {
  499.  
  500. $scp=$_POST['scp'];
  501. $nte=$_POST['nte'];
  502. $nturl=$_POST['nturl'];
  503. $clids=$_POST['clids'];
  504. $clidsc=$_POST['clidsc'];
  505. //$gistAPI = new gistAPI (); //Authenticate with anonymous is disable
  506. $gistAPI = new gistAPI('aznoisib');
  507. //Authenticate with username and password is enable
  508. $limits = $gistAPI->getLimits();
  509.  
  510. //Create A New Public repository
  511. //public is false or true is private
  512.  
  513.  
  514. $newgist = $gistAPI->createauth($scp,$nte,$nturl,$clids,$clidsc);
  515.  
  516. print_r($newgist);
  517. }
  518.  
  519. /* get authorization apps */
  520. echo '<center>
  521.  
  522. <form method="post">
  523.  
  524. <b>getauth list</b>
  525.  
  526. <br/>Client id <input class="inp-text" type="text" name="clid"
  527.  
  528. value="new repo test">
  529.  
  530. <br/>Tokens  <input class="inp-text" type="text" name="tken"
  531.  
  532. value="this php files examples">
  533.  
  534.  
  535.  
  536. <br/>
  537. <input name="getauth" class="inp-btn" type="submit" value="submit">
  538.  
  539. </form></center>';
  540.  
  541.  
  542.  
  543. if(isset($_POST['getauth'])) {
  544.  
  545. $clid=$_POST['clid'];
  546. $tken=$_POST['tken'];
  547.  
  548.  
  549. //$gistAPI = new gistAPI (); //Authenticate with anonymous is disable
  550. $gistAPI = new gistAPI('0165a1d9f16e1c6aa1dd','0844611fe1e9caeaa5122179ba905cb7f1c76e42');
  551. //Authenticate with username and password is enable
  552. $limits = $gistAPI->getLimits();
  553.  
  554. //Create A New Public repository
  555. //public is false or true is private
  556.  
  557. $newgist = $gistAPI->getauth($clid,$tken);
  558.  
  559. print_r($newgist);
  560. }
  561.  
  562. /* del authorization apps */
  563. echo '<center>
  564.  
  565. <form method="post">
  566.  
  567. <b>delete authorization</b>
  568.  
  569. <br/>Client id <input class="inp-text" type="text" name="clid"
  570.  
  571. value="new repo test">
  572.  
  573. <br/>Tokens  <input class="inp-text" type="text" name="tken"
  574.  
  575. value="this php files examples">
  576.  
  577.  
  578.  
  579. <br/>
  580. <input name="delauth" class="inp-btn" type="submit" value="submit">
  581.  
  582. </form></center>';
  583.  
  584.  
  585.  
  586. if(isset($_POST['delauth'])) {
  587.  
  588. $clid=$_POST['clid'];
  589. $tken=$_POST['tken'];
  590.  
  591.  
  592. //$gistAPI = new gistAPI (); //Authenticate with anonymous is disable
  593. $gistAPI = new gistAPI('0165a1d9f16e1c6aa1dd','0844611fe1e9caeaa51279ba905cb7f1c76e42');
  594. //Authenticate with username and password is enable
  595. $limits = $gistAPI->getLimits();
  596.  
  597. //Create A New Public repository
  598. //public is false or true is private
  599. $newgist1 = $gistAPI->getauth($clid,$tken);
  600. $newgist = $gistAPI->delauth($clid,$tken);
  601. print_r($newgist1);
  602. print_r($newgist);
  603. }
  604.  
  605.  
  606. /* Create repository files */
  607. echo '<center>
  608.  
  609. <form method="post">
  610.  
  611. <b>Create repository new files github api v3 by azars aka wehandler</b>
  612.  
  613. <br/>Filename <input class="inp-text" type="text" name="flname"
  614.  
  615. value="newfl.txt">
  616.  
  617. <br/>user <input class="inp-text" type="text" name="user"
  618.  
  619. value="aznoisib">
  620.  
  621.  
  622. <br/>repo <input class="inp-text" type="text" name="repo"
  623.  
  624. value="new-repo-test">
  625. <br/>msg <input class="inp-text" type="text" name="msg"
  626.  
  627. value="Initial Commit">
  628. <br/>content
  629. <textarea name="ctn"> bXkgbmV3IGZpbGUgY29udGVudHM= </textarea>
  630.  
  631. <br/>
  632. <input name="inputnewrepofiles" class="inp-btn" type="submit" value="submit">
  633.  
  634. </form></center>';
  635.  
  636.  
  637.  
  638. if(isset($_POST['inputnewrepofiles'])) {
  639.  
  640. $flname=$_POST['flname'];
  641. $user=$_POST['user'];
  642. $repo=$_POST['repo'];
  643. $msg=$_POST['msg'];
  644. $content=$_POST['ctn'];
  645.  
  646. //$gistAPI = new gistAPI (); //Authenticate with anonymous is disable
  647. $gistAPI = new gistAPI('aznoisib','80231c6d4fcf4ea4bb7ad5a3e029ca3213b3b');
  648. //Authenticate with username and password is enable
  649. $limits = $gistAPI->getLimits();
  650.  
  651. //Create A New Public repository
  652. //public is false or true is private
  653.  
  654. $newgist = $gistAPI->createrepofiles($flname, $user, $repo, $msg, $content);
  655.  
  656. print_r($newgist);
  657. }
  658.  
  659.  
  660. ?>
Add Comment
Please, Sign In to add comment