Advertisement
wehandler

Create new gist files with github api v3 v0.1

Oct 30th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.80 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' => $public,
  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 editGist ($gistId, $files = NULL, $description = NULL) {
  129.    
  130.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
  131.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
  132.         if ($files === NULL && $description !== NULL) {
  133.             $jsonArray = json_encode(array("description" => $description));
  134.         } elseif ($description === NULL && $files !== NULL) {
  135.             $jsonArray = json_encode(array("files" => $files));
  136.         } elseif ($description !== NULL && $files !== NULL) {
  137.             $jsonArray = json_encode(array("description" => $description, "files" => $files));
  138.         } else {
  139.             $this->chReset();
  140.             return 0;
  141.         }
  142.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  143.         return $this->returnCode();
  144.    
  145.     }
  146.    
  147.     public function gistCommits ($gistId) {
  148.    
  149.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/" . $gistId . "/commits");
  150.         return $this->returnCode();
  151.    
  152.     }
  153.    
  154.     public function starGist ($gistId) {
  155.    
  156.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
  157.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  158.         return $this->returnCode();
  159.    
  160.     }
  161.    
  162.     public function unstarGist ($gistId) {
  163.    
  164.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
  165.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  166.         return $this->returnCode();
  167.    
  168.     }
  169.    
  170.     public function checkStarGist ($gistId) {
  171.    
  172.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
  173.         return $this->returnCode();
  174.    
  175.     }
  176.    
  177.     public function forkGist ($gistId) {
  178.    
  179.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
  180.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'POST');
  181.         return $this->returnCode();
  182.    
  183.     }
  184.    
  185.     public function listForkGist ($gistId) {
  186.    
  187.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
  188.         return $this->returnCode();
  189.    
  190.     }
  191.    
  192.     public function deleteGist ($gistId) {
  193.    
  194.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
  195.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  196.         return $this->returnCode();
  197.    
  198.     }
  199.    
  200.     public function gistComments ($gistId) {
  201.    
  202.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/".$gistId."/comments");
  203.         return $this->returnCode();
  204.    
  205.     }
  206.    
  207.     public function getComment ($gistId, $commentId) {
  208.    
  209.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
  210.         return $this->returnCode();
  211.    
  212.     }
  213.    
  214.     public function createComment ($gistId, $comment){
  215.    
  216.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments");
  217.         curl_setopt($this->ch, CURLOPT_POST, 1);
  218.         $jsonArray = json_encode(array("body" => $comment));
  219.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  220.         return $this->returnCode();
  221.    
  222.     }
  223.    
  224.     public function editComment ($gistId, $commentId, $comment) {
  225.    
  226.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
  227.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
  228.         $jsonArray = json_encode(array("body" => $comment));
  229.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
  230.         return $this->returnCode();
  231.    
  232.     }
  233.    
  234.     public function deleteComment ($gistId, $commentId) {
  235.    
  236.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
  237.         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  238.         return $this->returnCode();
  239.    
  240.     }
  241.    
  242.     public function getLimits() {
  243.    
  244.         curl_setopt($this->ch, CURLOPT_URL, $this->url . "/rate_limit");
  245.         return $this->returnCode();
  246.     }
  247.    
  248.     private function parseHeader ($header_text) {
  249.        
  250.         $headers = array();
  251.         foreach (explode("\r\n", $header_text) as $i => $line){
  252.             if (strlen($line) > 1 && $i != 0){
  253.                 list ($key, $value) = explode(': ', $line);
  254.                 $headers[$key] = $value;
  255.             } else if ($i == 0){
  256.                 $headers['http_code'] = $line;
  257.             }
  258.         }
  259.         return $headers;
  260.     }
  261.    
  262.     private function returnCode () {
  263.    
  264.         $this->response = curl_exec($this->ch);
  265.         $header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
  266.         $header = substr($this->response, 0, $header_size);
  267.         $body = substr($this->response, $header_size);
  268.         $return = array("header" => $this->parseHeader($header),
  269.                      "body"   => json_decode($body, true),
  270.                      "raw"    => $this->response);
  271.         $this->chReset();
  272.         return $return;
  273.     }
  274.    
  275.     public function chReset () {
  276.    
  277.         $this->ch = curl_init();
  278.         curl_setopt($this->ch, CURLOPT_HEADER,         true);
  279.         curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
  280.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  281.         curl_setopt($this->ch, CURLOPT_TIMEOUT,        30);
  282.         curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  283.         if ($this->loginInfo !== NULL){
  284.             $this->user = $this->loginInfo['username'];
  285.             curl_setopt($this->ch, CURLOPT_USERAGENT, $this->loginInfo['username']);
  286.             curl_setopt($this->ch, CURLOPT_USERPWD, $this->loginInfo['username'].":".$this->loginInfo['password']);
  287.         } else {
  288.             curl_setopt($this->ch, CURLOPT_USERAGENT, "gistAPI v0.1");
  289.         }
  290.         unset($this->response);
  291.    
  292.     }
  293.    
  294.     function __destruct() {
  295.         curl_close($this->ch);
  296.     }
  297.  
  298. }
  299. /* Create gist files */
  300. echo '<center>
  301.  
  302. <form method="post">
  303.  
  304. <b>Create gist files github api v3 by azars aka wehandler</b>
  305.  
  306. <br/>Filename with .ext<input class="inp-text" type="text" name="flname"
  307.  
  308. value="testing.php">
  309.  
  310. <br/>Description <input class="inp-text" type="text" name="desc"
  311.  
  312. value="this php files examples">
  313.  
  314. <br/>Files content
  315. <textarea name="content"> Enter content here... </textarea>
  316.  
  317. <br/>
  318. <input name="input" class="inp-btn" type="submit" value="submit">
  319.  
  320. </form></center>';
  321.  
  322.  
  323.  
  324. if(isset($_POST['input'])) {
  325.  
  326. $flname=$_POST['flname'];
  327. $desc=$_POST['desc'];
  328. $content=$_POST['content'];
  329.  
  330. $gistAPI = new gistAPI (); //To Authenticate anonymous is enable
  331. //$gistAPI = new gistAPI('yourusername','passwordxxx');
  332. //disable to Authenticate with username and password
  333. $limits = $gistAPI->getLimits();
  334.  
  335. //Create A New Public Gist
  336. //public is true or false is private
  337. //private gists are hidden from search engines but visible to anyone you give the URL
  338.  
  339. $newgist = $gistAPI->createGist($flname, $desc, $content, true);
  340.  
  341. print_r($newgist);
  342. }
  343. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement