Skip to content

Instantly share code, notes, and snippets.

@gmanricks
Created April 13, 2013 19:15
Show Gist options
  • Save gmanricks/5379675 to your computer and use it in GitHub Desktop.
Save gmanricks/5379675 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
define("CLIENT_ID", "297cf352e9dc1c8237a2");
define("CLIENT_SECRET", "517cf1a00237974b9c5cd50a516e5a674160c4f0");
$token = getToken();
if ($token) {
if (getopt("h", array("help"))) {
echo helpMessage();
} else {
$public = (getopt("p", array("private"))) ? false : true;
array_shift($argv);
$props = array();
foreach ($argv as $command) {
if (strpos($command, "-") !== 0) {
array_push($props, $command);
}
}
if (count($props) === 1) {
if (file_exists($props[0])) {
postGist(array_pop(explode("/", $props[0])), file_get_contents($props[0]), $public, $token);
} else {
echo "File '" . $props[0] . "'does not exist !\n";
}
} else if (count($props) === 2) {
postGist($props[0], $props[1], $public, $token);
} else {
echo "Wrong parameter count, run 'gistit -h' for the info on usage\n";
}
}
}
function getToken()
{
if (file_exists($_SERVER["HOME"] . "/.gistit")) {
return file_get_contents($_SERVER["HOME"] . "/.gistit");
} else {
echo "This seems to be your first run I will need the following info:\n";
echo "Github Username: ";
$username = trim(fgets(STDIN));
echo "Github Password: ";
system('stty -echo');
$password = trim(fgets(STDIN));
system('stty echo');
echo "\n";
return attemptLogin($username, $password);
}
}
function postGist($name, $contents, $public, $token)
{
$data = array(
"client_id" => '"' . CLIENT_ID . '"',
"client_secret" => '"' . CLIENT_SECRET . '"',
"public" => $public,
"files" => array($name => array("content" => $contents))
);
$resp = getCurl("/gists?access_token=" . $token, $data);
echo "Gist Created, URL => " . $resp->html_url;
}
function getCurl($endpoint, $data, $curlArgs = array())
{
$defaults = array(CURLOPT_SSLVERSION => 3, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => json_encode($data));
$ch = curl_init("https://api.github.com" . $endpoint);
curl_setopt_array($ch, $defaults);
if (count($curlArgs)) {
curl_setopt_array($ch, $curlArgs);
}
return json_decode(curl_exec($ch));
}
function attemptLogin($user, $pass)
{
$data = array("client_id" => CLIENT_ID, "client_secret" => CLIENT_SECRET, "scopes" => array("gist"));
$curlArgs = array(CURLOPT_USERPWD => "$user:$pass", CURLOPT_HTTPAUTH => CURLAUTH_BASIC);
$resp = getCurl("/authorizations", $data, $curlArgs);
if (isset($resp->token)) {
echo "Connected to Github !!\n";
file_put_contents($_SERVER["HOME"] . "/.gistit", $resp->token);
return $resp->token;
} else {
echo "Login Failed: " . $resp->message . "\n";
return false;
}
}
function helpMessage()
{
return <<<HELP
GistIt Help
=============
usage: gistit [filepath | (filename + string)]
examples
----------
gistit file.php
gistit gistname.php "<?php phpinfo(); ?>"
options
----------
-h | --help shows this message
-p | --private creates a private gist
HELP;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment