Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* GIST FUNCTION PHP CLASS GITHUB API V3
- * simple php class gist github api v3
- *
- * @description very simple Php class to interact with gist github api v3
- * @version 0.1 beta version
- * @author azars aka wehandler
- * @created date 30 Oct 2017
- *
- * @usage: available usage is Create gist files
- you can recoded or add available function by using the class code.
- * special thanks to akshay9 github. script fixed by azars aka wehandler
- * @licence: THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND INCLUDING BUT NOT LIMITED THE
- WARRANTY (IMPLIED OR OTHERWISE) OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
- YOU ARE FREE TO MODIFY AND OR USE THIS SOFTWARE FOR OPEN SOURCE AND COMMERCIAL PROJECTS.
- NO ATTRIBUTION IS REQUIRED.
- */
- class GistEdit {
- private $data;
- private static $_instance = NULL ;
- public static function init () {
- if (self::$_instance === NULL) {
- self::$_instance = new self;
- }
- self::$_instance->data = array();
- return self::$_instance;
- }
- public function edit ($file, $newContent = NULL, $newFileName = NULL) {
- if ($newContent !== NULL) {
- $this->data[$file]['content'] = $newContent ;
- }
- if ($newFileName !== NULL) {
- $this->data[$file]['filename'] = $newFileName ;
- }
- return $this;
- }
- public function deleteFile ($file) {
- $this->data[$file] = NULL ;
- return $this;
- }
- public function newFile ($file, $content){
- $this->data[$file]['content'] = $content;
- return $this;
- }
- public function get () {
- return $this->data;
- }
- }
- class gistAPI {
- private $url = "https://api.github.com" ;
- private $user = "github" ;
- public $ch ;
- private $response ;
- public $loginInfo ;
- function __construct($id = NULL, $pass = NULL) {
- if($id === NULL || $pass === NULL){
- $loginInfo = NULL;
- } else {
- $loginInfo = array('username' => $id,
- 'password' => $pass);
- }
- $this->loginInfo = $loginInfo;
- $this->chReset();
- }
- public function listGists ($type = "public", $user = NULL) {
- switch ($type) {
- case "public":
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/public");
- break;
- case "user":
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/users/" . ($user === NULL ? $this->user:$user) ."/gists");
- break;
- case "starred":
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/starred");
- break;
- }
- return $this->returnCode();
- }
- public function getGist ($gistId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/".$gistId);
- return $this->returnCode();
- }
- public function createGist ($files, $description = "", $content, $public = false) {
- $postArray = array(
- 'description' => $description,
- 'public' => 'true',
- 'files' => array(
- $files => array(
- 'content' => $content
- )
- )
- );
- $jsonArray = json_encode($postArray);
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists");
- curl_setopt($this->ch, CURLOPT_POST, 1);
- curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
- return $this->returnCode();
- }
- public function createrepo ($name, $description = false, $hmpage = false, $private = false) {
- $postArray = array(
- 'name' => $name,
- 'description' => $description,
- 'homepage' => $hmpage,
- 'private' => $private,
- );
- $jsonArray = json_encode($postArray);
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/user/repos");
- curl_setopt($this->ch, CURLOPT_POST, 1);
- curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
- return $this->returnCode();
- }
- public function createrepofiles ($name, $user, $repo, $msg, $content, $branch = null, array $committer = null) {
- $postArray = array(
- 'content' => base64_encode($content),
- 'message' => $msg
- );
- if(null!== $branch) {
- if(!isset($committer['name'], $committer['email'])) {
- throw new MissingArgumentException(array('name' ,'email'));
- }
- $postArray['committer'] = $committer;
- }
- $jsonArray = json_encode($postArray);
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/repos/" . $user . "/" . $repo . "/contents/" . $name);
- curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
- curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
- return $this->returnCode();
- }
- public function editGist ($gistId, $files = NULL, $description = NULL) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
- curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
- if ($files === NULL && $description !== NULL) {
- $jsonArray = json_encode(array("description" => $description));
- } elseif ($description === NULL && $files !== NULL) {
- $jsonArray = json_encode(array("files" => $files));
- } elseif ($description !== NULL && $files !== NULL) {
- $jsonArray = json_encode(array("description" => $description, "files" => $files));
- } else {
- $this->chReset();
- return 0;
- }
- curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
- return $this->returnCode();
- }
- public function gistCommits ($gistId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/" . $gistId . "/commits");
- return $this->returnCode();
- }
- public function starGist ($gistId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
- curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
- return $this->returnCode();
- }
- public function unstarGist ($gistId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
- curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
- return $this->returnCode();
- }
- public function checkStarGist ($gistId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
- return $this->returnCode();
- }
- public function forkGist ($gistId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
- curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'POST');
- return $this->returnCode();
- }
- public function listForkGist ($gistId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
- return $this->returnCode();
- }
- public function deleteGist ($gistId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
- curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
- return $this->returnCode();
- }
- public function gistComments ($gistId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/".$gistId."/comments");
- return $this->returnCode();
- }
- public function getComment ($gistId, $commentId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
- return $this->returnCode();
- }
- public function createComment ($gistId, $comment){
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments");
- curl_setopt($this->ch, CURLOPT_POST, 1);
- $jsonArray = json_encode(array("body" => $comment));
- curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
- return $this->returnCode();
- }
- public function editComment ($gistId, $commentId, $comment) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
- curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
- $jsonArray = json_encode(array("body" => $comment));
- curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
- return $this->returnCode();
- }
- public function deleteComment ($gistId, $commentId) {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
- curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
- return $this->returnCode();
- }
- public function getLimits() {
- curl_setopt($this->ch, CURLOPT_URL, $this->url . "/rate_limit");
- return $this->returnCode();
- }
- private function parseHeader ($header_text) {
- $headers = array();
- foreach (explode("\r\n", $header_text) as $i => $line){
- if (strlen($line) > 1 && $i != 0){
- list ($key, $value) = explode(': ', $line);
- $headers[$key] = $value;
- } else if ($i == 0){
- $headers['http_code'] = $line;
- }
- }
- return $headers;
- }
- private function returnCode () {
- $this->response = curl_exec($this->ch);
- $header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
- $header = substr($this->response, 0, $header_size);
- $body = substr($this->response, $header_size);
- $return = array("header" => $this->parseHeader($header),
- "body" => json_decode($body, true),
- "raw" => $this->response);
- $this->chReset();
- return $return;
- }
- public function chReset () {
- $this->ch = curl_init();
- curl_setopt($this->ch, CURLOPT_HEADER, true);
- curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);
- curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
- if ($this->loginInfo !== NULL){
- $this->user = $this->loginInfo['username'];
- curl_setopt($this->ch, CURLOPT_USERAGENT, $this->loginInfo['username']);
- curl_setopt($this->ch, CURLOPT_USERPWD, $this->loginInfo['username'].":".$this->loginInfo['password']);
- } else {
- curl_setopt($this->ch, CURLOPT_USERAGENT, "gistAPI v1.0");
- }
- unset($this->response);
- }
- function __destruct() {
- curl_close($this->ch);
- }
- }
- /* Create gist files */
- echo '<center>
- <form method="post">
- <b>Create gist files github api v3 by azars aka wehandler</b>
- <br/>Filename with .ext<input class="inp-text" type="text" name="flname"
- value="testing.php">
- <br/>Description <input class="inp-text" type="text" name="desc"
- value="this php files examples">
- <br/>Files content
- <textarea name="content"> Enter content here... </textarea>
- <br/>
- <input name="input" class="inp-btn" type="submit" value="submit">
- </form></center>';
- if(isset($_POST['input'])) {
- $flname=$_POST['flname'];
- $desc=$_POST['desc'];
- $content=$_POST['content'];
- //$gistAPI = new gistAPI (); //To Authenticate anonymous is enable
- $gistAPI = new gistAPI('xxxxx','610dbf267c3224ef9b2593deff5bd3f7071b26bd');
- //disable to Authenticate with username and password
- $limits = $gistAPI->getLimits();
- //Create A New Public Gist
- //public is true or false is private
- //private gists are hidden from search engines but visible to anyone you give the URL
- $newgist = $gistAPI->createGist($flname, $desc, $content, true);
- print_r($newgist);
- }
- /* Create repository */
- echo '<center>
- <form method="post">
- <b>Create repository files github api v3 by azars aka wehandler</b>
- <br/>Filename <input class="inp-text" type="text" name="flname"
- value="new repo test">
- <br/>Description <input class="inp-text" type="text" name="desc"
- value="this php files examples">
- <br/>homepage
- <textarea name="homepage"> github.com </textarea>
- <br/>
- <input name="inputnewrepo" class="inp-btn" type="submit" value="submit">
- </form></center>';
- if(isset($_POST['inputnewrepo'])) {
- $flname=$_POST['flname'];
- $desc=$_POST['desc'];
- $content=$_POST['homepage'];
- //$gistAPI = new gistAPI (); //Authenticate with anonymous is disable
- $gistAPI = new gistAPI('xxxxxxx','xxxx12345');
- //Authenticate with username and password is enable
- $limits = $gistAPI->getLimits();
- //Create A New Public repository
- //public is false or true is private
- $newgist = $gistAPI->createrepo($flname, $desc, $content, true);
- print_r($newgist);
- }
- /* Create repository files */
- echo '<center>
- <form method="post">
- <b>Create repository new files github api v3 by azars aka wehandler</b>
- <br/>Filename <input class="inp-text" type="text" name="flname"
- value="newfl.txt">
- <br/>user <input class="inp-text" type="text" name="user"
- value="aznoisib">
- <br/>repo <input class="inp-text" type="text" name="repo"
- value="new-repo-test">
- <br/>msg <input class="inp-text" type="text" name="msg"
- value="Initial Commit">
- <br/>content
- <textarea name="ctn"> bXkgbmV3IGZpbGUgY29udGVudHM= </textarea>
- <br/>
- <input name="inputnewrepofiles" class="inp-btn" type="submit" value="submit">
- </form></center>';
- if(isset($_POST['inputnewrepofiles'])) {
- $flname=$_POST['flname'];
- $user=$_POST['user'];
- $repo=$_POST['repo'];
- $msg=$_POST['msg'];
- $content=$_POST['ctn'];
- //$gistAPI = new gistAPI (); //Authenticate with anonymous is disable
- $gistAPI = new gistAPI('xxxxx','610dbf267c3224ef9b2593deff5bd3f7071b26bd');
- //Authenticate with username and password is enable
- $limits = $gistAPI->getLimits();
- //Create A New Public repository
- //public is false or true is private
- $newgist = $gistAPI->createrepofiles($flname, $user, $repo, $msg, $content);
- print_r($newgist);
- }
- ?>
Add Comment
Please, Sign In to add comment