Created
September 17, 2019 20:18
-
-
Save AdnanHussainTurki/ee98fed26134c823391633aa105cf773 to your computer and use it in GitHub Desktop.
Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @Author: Adnan Hussain Turki | |
* @Website: www.myphpnotes.com | |
* @Email Address: [email protected] | |
===================================== | |
* @Creation Time: 2019-09-06 19:47:42 | |
* @Last Modified by: Adnan | |
* @Last Modified time: 2019-09-18 01:00:35 | |
===================================== | |
PROPERTY OF WWW.MYPHPNOTES.COM | |
*/ | |
namespace myPHPnotes; | |
/** | |
* | |
*/ | |
class Gist | |
{ | |
protected $token; | |
protected $username; | |
protected $ssl; | |
protected $host = "https://api.github.com"; | |
function __construct(string $username = null, string $token = null, $ssl = true) | |
{ | |
$this->username = $username; | |
$this->token = $token; | |
$this->ssl = $ssl; | |
} | |
public function getPublicGists($page = 1, $perPage = 100) | |
{ | |
return $this->curl($this->host . "/gists", ["page" => $page, 'per_page' => $perPage]); | |
} | |
public function getPublicStarred() | |
{ | |
// TO BE BUILT | |
} | |
public function myGists($page = 1, $perPage = 100) | |
{ | |
$this->checkAuth(); | |
return $this->curl($this->host . "/users/{$this->username}/gists", ["access_token" => $this->token, "page" => $page, 'per_page' => $perPage]); | |
} | |
public function view(string $gist_id) | |
{ | |
return $this->curl($this->host . "/gists/" . $gist_id, ["access_token" => $this->token]); | |
} | |
public function viewPrivate(string $gist_id) | |
{ | |
$this->checkAuth(); | |
return $this->curl($this->host . "/gists/" . $gist_id, ["access_token" => $this->token]); | |
} | |
public function create(GistObject $gist) | |
{ | |
$this->checkAuth(); | |
return $this->curl($this->host . "/gists?access_token=" . $this->token, json_encode($gist, JSON_FORCE_OBJECT), "application/json", true); | |
} | |
public function delete(string $gist_id) | |
{ | |
$this->checkAuth(); | |
return $this->curl($this->host . "/gists/{$gist_id}?access_token=" . $this->token, [], "application/json", false, false, true); | |
} | |
public function edit(string $gist_id, GistObject $gist) | |
{ | |
// PRIVACY OF THE GIST CANNOT BE CHANGED BY THIS | |
$this->checkAuth(); | |
return $this->curl($this->host . "/gists/{$gist_id}?access_token=" . $this->token, json_encode($gist, JSON_FORCE_OBJECT), "application/json", false, true); | |
} | |
protected function curl($url, $parameters = [], $content_type = "application/json", $post = false, $patch = false, $delete = false) | |
{ | |
$ch = curl_init(); | |
if ($post) { | |
curl_setopt($ch, CURLOPT_POST, $post); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); | |
} elseif($patch) { | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); | |
} elseif($delete) { | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); | |
} else { | |
$http_parameters = http_build_query($parameters); | |
$joiner = "?"; | |
if (stripos($url, "?")) { | |
$joiner = "&"; | |
} | |
$url.= $joiner . $http_parameters; | |
} | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$headers = []; | |
$headers[] = "Content-Type: {$content_type}"; | |
$headers[] = "user-agent: myPHPnotes PHP wrapper"; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
return $result; | |
} | |
protected function checkAuth() { | |
if (!$this->username or !$this->token) { | |
die("UNAUTHORISED"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace myPHPnotes; | |
use JsonSerializable; | |
class GistObject implements JsonSerializable | |
{ | |
protected $objectArray; | |
function __construct(string $description, bool $public) | |
{ | |
$this->objectArray['description'] = $description; | |
$this->objectArray['public'] = $public; | |
} | |
public function addContent(string $file_name, string $file_content) | |
{ | |
$this->objectArray['files'][$file_name] = ["content" =>$file_content]; | |
} | |
public function removeContent(string $filename) | |
{ | |
if (isset($this->objectArray['files'][$filename])) { | |
unset($this->objectArray['files'][$filename]); | |
} else { | |
die("Content with this filename not found. File Name: " . $filename); | |
} | |
} | |
public function jsonSerialize() { | |
if ((!isset($this->objectArray['files'])) OR (count($this->objectArray['files']) === 0)) { | |
die("Gist Object does not contains any content."); | |
} | |
return $this->objectArray; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require "init.php"; | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Gist API Example by myPHPnotes.com</title> | |
<!-- <link rel="stylesheet" href="/css/dropzone.css"> --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> | |
<!-- <script src="/js/dropzone.js"></script> --> | |
<style> | |
body,html { | |
height: 100%; | |
} | |
.bg { | |
/* The image used */ | |
background-image: url("/images/bg.jpg"); | |
/* Full height */ | |
height: 100%; | |
/* Center and scale the image nicely */ | |
background-position: center; | |
background-repeat: no-repeat; | |
background-size: cover; | |
} | |
.caption { | |
font-family: 'Times New Roman'; | |
text-transform: uppercase; | |
position: absolute; | |
left: 0; | |
top: 50%; | |
width: 100%; | |
text-align: center; | |
color: #000; | |
} | |
.caption span.border { | |
background-color: #111; | |
color: #fff; | |
padding: 18px; | |
font-size: 25px; | |
} | |
</style> | |
</head> | |
<body class="bg" > | |
<div class="container" id="panel"> | |
<br><br><br> | |
<div class="row" > | |
<div class="col-md-6 offset-md-3" style="background: white; padding: 20px; box-shadow: 10px 10px 5px #888888;"> | |
<div class="panel-heading"> | |
<h1>Gist API</h1> | |
<p>Logged In!! <a href="logout.php">Log Out!!</a></p> | |
</div> | |
<hr> | |
<h6>Create Gist</h6> | |
<br> | |
<form action="create.php" method="post" autocomplete="off" enctype="multipart/form-data"> | |
<input type="text" style="border-radius: 0px;" name="description" id="text" class="form-control" placeholder="Gist Description" required="required" value=""> | |
<br> | |
<select name="privacy" id="" class="form-control" required="required" > | |
<option value="true">Public</option> | |
<option value="false">Private</option> | |
</select> | |
<br> | |
<input type="file" id="fileInput" name="files[]" multiple="multiple" class="form-control" /> | |
<br> | |
<div id="dropContainer" style="border: 2px dashed #0087F7; color: #0087F7; font-weight: bolder; border-radius: 5px;background: white;height:100px; text-align: center; vertical-align: middle;"><br> | |
Drop Here | |
</div> | |
<br> | |
<input type="submit" style="border-radius: 0px;" class="btn btn-sm btn-block btn-danger" value="Create Gist And Show"/> | |
<a id="blank"></a> | |
</form> | |
</div> | |
</div> | |
</div> | |
<script> | |
dropContainer.ondragover = dropContainer.ondragenter = function(evt) { | |
evt.preventDefault(); | |
}; | |
dropContainer.ondrop = function(evt) { | |
// pretty simple -- but not for IE :( | |
fileInput.files = evt.dataTransfer.files; | |
evt.preventDefault(); | |
}; | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
if (isset($_SESSION['gusername']) AND isset($_SESSION['gtoken'])) { | |
header("location: home.php"); | |
die(); | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Gist API Example by myPHPnotes.com</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> | |
<style> | |
body,html { | |
height: 100%; | |
} | |
.bg { | |
/* The image used */ | |
background-image: url("/images/bg.jpg"); | |
/* Full height */ | |
height: 100%; | |
/* Center and scale the image nicely */ | |
background-position: center; | |
background-repeat: no-repeat; | |
background-size: cover; | |
} | |
.caption { | |
font-family: 'Times New Roman'; | |
text-transform: uppercase; | |
position: absolute; | |
left: 0; | |
top: 50%; | |
width: 100%; | |
text-align: center; | |
color: #000; | |
} | |
.caption span.border { | |
background-color: #111; | |
color: #fff; | |
padding: 18px; | |
font-size: 25px; | |
} | |
</style> | |
</head> | |
<body class="bg" > | |
<div class="container" id="panel"> | |
<br><br><br> | |
<div class="row" > | |
<div class="col-md-6 offset-md-3" style="background: white; padding: 20px; box-shadow: 10px 10px 5px #888888;"> | |
<div class="panel-heading"><h1>Gist API</h1></div> | |
<hr> | |
<form action="login.php" method="post" autocomplete="off"> | |
<input type="text" style="border-radius: 0px;" name="gusername" id="text" class="form-control" placeholder="GitHub Username" value=""> | |
<br> | |
<input type="password" style="border-radius: 0px;" name="gtoken" id="text" class="form-control" placeholder="GitHub Personal Access Token" value=""> | |
<br> | |
<div class="alert alert-warning"> | |
No non-consent use of access token will be done from our end. The above access token will be stored as a file session on the server and cleared within a week among all sessions. | |
</div> | |
<input type="submit" style="border-radius: 0px;" class="btn btn-lg btn-block btn-outline-success" value="Log Me In"/> | |
<a id="blank"></a> | |
</form> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
use myPHPnotes\Gist; | |
use myPHPnotes\GistObject; | |
require_once "Gist.php"; | |
require_once "GistObject.php"; | |
$username = $_SESSION['gusername']; | |
$token = $_SESSION['gtoken']; | |
$gist = new Gist($username, $token); | |
$gists = $gist->myGists(); | |
$gists = json_decode($gists); | |
if ($gists->message == "Bad credentials") { | |
session_destroy(); | |
header("location: index.php?badcredentials=true"); | |
die(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment