Created
July 9, 2012 01:03
-
-
Save jrbasso/3073671 to your computer and use it in GitHub Desktop.
Example how to create bin in bin.cakephp.org
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 Cake\Bin; | |
class Push { | |
const DEFAULT_TYPE = 'text'; | |
const CAKEBIN_URL = 'http://bin.cakephp.org'; | |
protected $allowedTypes = array( | |
'actionscript', 'ada', 'apache', 'applescript', 'asm', 'asp', 'autoit', | |
'bash', 'blitzbasic', 'c', 'c_map', 'caddcl', 'cadlisp', 'cfdg', 'cfm', | |
'cpp', 'csharp', 'css-gen', 'css', 'd', 'delphi', 'diff', 'div', 'dos', | |
'eiffel', 'fortran', 'freebasic', 'gml', 'groovy', 'html4strict', 'idl', | |
'ini', 'inno', 'java', 'java5', 'javascript', 'latex', 'lisp', 'lua', | |
'matlab', 'mpasm', 'mysql', 'nsis', 'objc', 'ocaml-brief', 'ocaml', | |
'oobas', 'oracle8', 'pascal', 'perl', 'php-brief', 'php', 'python', | |
'qbasic', 'reg', 'robots', 'ruby', 'sas', 'scheme', 'sdlbasic', | |
'smalltalk', 'smarty', 'sql', 'tcl', 'text', 'thinbasic', 'tsql', 'vb', | |
'vbnet', 'vhdl', 'visualfoxpro', 'winbatch', 'xml' | |
); | |
protected $options = array(); | |
protected $content; | |
public function setOptions($options) { | |
if (empty($options['lang']) || !in_array($options['lang'], $this->allowedTypes)) { | |
$options['lang'] = 'text'; | |
} | |
if (empty($options['nick'])) { | |
$options['nick'] = get_current_user(); | |
} | |
if (isset($options['save'])) { | |
$options['save'] = true; | |
} else { | |
$options['save'] = false; | |
} | |
$this->options = $options; | |
} | |
public function contentFromInput() { | |
$input = ''; | |
while ($line = fgets(STDIN)) { | |
$input .= $line; | |
} | |
$this->content = $input; | |
} | |
public function submit() { | |
$emptyPage = file_get_contents(static::CAKEBIN_URL); | |
if (!preg_match('/data\[_Token\]\[key\]" value\="([^"]+)"/', $emptyPage, $matches)) { | |
throw new \Exception('Failed to request an empty page to ' . static::CAKEBIN_URL); | |
} | |
$token = $matches[1]; | |
if (!preg_match('/data\[_Token\]\[fields\]" value\="([^"]+)"/', $emptyPage, $matches)) { | |
throw new \Exception('Failed to request an empty page to ' . static::CAKEBIN_URL); | |
} | |
$fieldsToken = $matches[1]; | |
$params = array( | |
'_method' => 'POST', | |
'data[_Token][key]' => $token, | |
'data[NewPaste][body]' => $this->content, | |
'data[NewPaste][nick]' => $this->options['nick'], | |
'data[NewPaste][lang]' => $this->options['lang'], | |
'data[NewPaste][note]' => '', | |
'data[NewPaste][save]' => (int)$this->options['save'], | |
'data[NewPaste][tags]' => '', | |
'data[Other][comment]' => '', | |
'data[Other][title]' => '', | |
'data[Other][date]' => '', | |
'data[_Token][fields]' => $fieldsToken | |
); | |
$curl = curl_init(static::CAKEBIN_URL); | |
curl_setopt($curl, \CURLOPT_POST, true); | |
curl_setopt($curl, \CURLOPT_FOLLOWLOCATION, false); | |
curl_setopt($curl, \CURLOPT_POSTFIELDS, $params); | |
curl_setopt($curl, \CURLOPT_HEADER, true); | |
curl_setopt($curl, \CURLOPT_RETURNTRANSFER, true); | |
$response = curl_exec($curl); | |
if (curl_getinfo($curl, \CURLINFO_HTTP_CODE) !== 302) { | |
throw new \Exception('Failed to submit the bin'); | |
} | |
if (!preg_match("/Location: (.*)\n/", $response, $matches)) { | |
throw new \Exception('Failed to submit the bin'); | |
} | |
curl_close($curl); | |
return $matches[1]; | |
} | |
} | |
$push = new Push(); | |
$push->setOptions(getopt('', array('lang:', 'nick:', 'save'))); | |
$push->contentFromInput(); | |
try { | |
echo 'URL: ', $push->submit(), "\n"; | |
} catch (\Exception $e) { | |
echo $e->getMessage(), "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment