Created
November 8, 2009 10:45
-
-
Save Westie/229207 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Class that aids you with using ControlV. | |
* | |
* You will need cURL installed on your web server, along with an API key, | |
* which you can get issued from http://controlv.net/getapi.php . | |
* | |
* You can find the documentation here: http://docs.typefish.co.uk/ControlV/ControlV.html | |
* | |
* @package ControlV | |
* @author David Weston and Felix Lundin | |
* @copyright 2009; David Weston (http://www.typefish.co.uk/licence/) and Felix Lundin | |
* @version 1.2a | |
*/ | |
class ControlV | |
{ | |
/** | |
* Make the paste expire in a day. | |
*/ | |
const EXPIRE_DAY = 'd'; | |
/** | |
* Make the paste expire after a week. | |
*/ | |
const EXPIRE_WEEK = 'w'; | |
/** | |
* Make the paste expire after a month. | |
*/ | |
const EXPIRE_MONTH = 'm'; | |
/** | |
* Make the paste expire after a year. | |
*/ | |
const EXPIRE_YEAR = 'y'; | |
/** | |
* Make the paste never expire. | |
*/ | |
const EXPIRE_NEVER = 'n'; | |
/** | |
* Initiate a pasting object. | |
* | |
* The pasting object allows you with ease to paste code through | |
* the API, and retrieve the pasted code. Follow the 'uses' link | |
* to see the associated documentation with the paste object. | |
* | |
* <code> | |
* $paste = ControlV::initPaste('0012332123'); | |
* $paste->setAuthor('Westie'); | |
* $paste->setTitle('Testing the API'); | |
* $paste->setExpiry(ControlV::EXPIRY_YEAR); | |
* | |
* // This will return the URL of the new paste. | |
* // An example is: http://www.controlv.net/jjv333w | |
* $sWebsite = $paste->Post('.. content here please ..'); | |
* </code> | |
* | |
* @param string $sAPIKey Automatically generated API Key | |
* @return object $paste The pasting object (ControlV_Paste) | |
* @uses ControlV_Paste::__construct() | |
*/ | |
static function initPaste($sAPIKey) | |
{ | |
return new ControlV_Paste($sAPIKey); | |
} | |
/** | |
* Download a paste from the the ControlV website. | |
* | |
* Using this function, you can with ease download information | |
* about the paste, including its author, date it was posted, etc. | |
* | |
* A request like the one below: | |
* <code>ControlV::getPost('0012332123', 'jjv333w');</code> | |
* | |
* .. will return an array like the one below. | |
* <code> | |
* Array | |
* ( | |
* [NAME] => LOLHAI | |
* [TITLE] => HTML | |
* [DATE] => 2009-11-06 16:23:54 | |
* [PASTE] => (.. the string of the paste ..) | |
* )</code> | |
* | |
* @param string $sAPIKey Automatically generated API Key | |
* @param string $sPostID ControlV post ID | |
* @return array Array of post information | |
*/ | |
static function getPost($sAPIKey, $sPostID) | |
{ | |
$aConfig = array | |
( | |
'api_key' => $sAPIKey, | |
'function' => 'view', | |
'url' => $sPostID, | |
); | |
$sOutput = self::Post($aConfig); | |
preg_match('/<name>(.*)<\/name>/', $sOutput, $aMatches['name']); | |
preg_match('/<title>(.*)<\/title>/', $sOutput, $aMatches['title']); | |
preg_match('/<date>(.*)<\/date>/', $sOutput, $aMatches['date']); | |
preg_match('/<paste>(.*)<\/paste>/s', $sOutput, $aMatches['paste']); | |
return array | |
( | |
"NAME" => $aMatches['name'][1], | |
"TITLE" => $aMatches['title'][1], | |
"DATE" => $aMatches['date'][1], | |
"PASTE" => html_entity_decode($aMatches['paste'][1]), | |
); | |
} | |
/** | |
* Used to send a request. | |
* | |
* This ensures that each of the child classes uses the same | |
* uploading function in order to help maintain compatibility | |
* between classes. | |
* | |
* @param array $aConfig Array of arguments to pass through | |
* @return string XML Output | |
*/ | |
static function Post($aConfig) | |
{ | |
$cURL = curl_init('http://controlv.net/api/api.php'); | |
curl_setopt($cURL, CURLOPT_POST, true); | |
curl_setopt($cURL, CURLOPT_POSTFIELDS, $aConfig); | |
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true); | |
$sOutput = curl_exec($cURL); | |
curl_close($cURL); | |
return $sOutput; | |
} | |
} | |
/** | |
* This class enables you to paste to ControlV. | |
* | |
* You can use this class manually, but the new official method is using | |
* the main ControlV class in order to initiate the plugin. However, the | |
* function names remain the same, and this is the object that ControlV | |
* returns when called like so: | |
* | |
* <code>$paste = ControlV::initPaste('0012332123');</code> | |
* | |
* @package ControlV | |
* @author David Weston and Felix Lundin | |
* @copyright 2009; David Weston (http://www.typefish.co.uk/licence/) and Felix Lundin | |
* @version 1.1a | |
* @subpackage Child-Classes | |
*/ | |
class ControlV_Paste | |
{ | |
/** | |
* @ignore | |
*/ | |
private $aConfig = array(); | |
/** | |
* @ignore | |
*/ | |
public $sOutput; | |
/** | |
* Initiate the class. | |
* | |
* Due to the new loading system, you can initiate the class like | |
* below, which is the new 'official' method. This is in order to | |
* merge all the different aspects that ControlV offers right now, | |
* as well as might offer in the future. | |
* | |
* <code>$paste = ControlV::initPaste('0012332123');</code> | |
* | |
* @param string $sAPIKey Automatically generated API Key | |
*/ | |
public function __construct($sAPIKey) | |
{ | |
$this->aConfig = array | |
( | |
'api_key' => $sAPIKey, | |
'function' => 'paste', | |
'title' => 'Untitled', | |
'name' => 'Anonymous', | |
'expiration' => ControlV::EXPIRE_YEAR, | |
'private' => 'n', | |
'paste' => "", | |
); | |
} | |
/** | |
* Set the title of the paste. | |
* | |
* <code>$paste->setTitle('Testing paste script');</code> | |
* | |
* @param string $sTitle Title of the paste. | |
*/ | |
public function setTitle($sTitle) | |
{ | |
$this->aConfig['title'] = $sTitle; | |
} | |
/** | |
* Set the author of the paste. | |
* | |
* <code>$paste->setAuthor('Little Red Riding Hood');</code> | |
* | |
* @param string $sAuthor Author of the paste | |
*/ | |
public function setAuthor($sAuthor) | |
{ | |
$this->aConfig['name'] = $sAuthor; | |
} | |
/** | |
* Set the privacy options of the paste. | |
* | |
* <code>$paste->setPrivate(true);</code> | |
* | |
* @param boolean $bPrivate False to be public, true to be private. | |
*/ | |
public function setPrivate($bPrivate = false) | |
{ | |
$this->aConfig['private'] = ($bPrivate ? 'n' : 'y'); | |
} | |
/** | |
* Set the expiry date of the paste. | |
* | |
* <code>$paste->setExpiry(ControlV::EXPIRE_WEEK);</code> | |
* | |
* @param character $cDuration Duration, look at the EXPIRE_* constants. | |
*/ | |
public function setExpiry($cDuration) | |
{ | |
$this->aConfig['expiration'] = $cDuration; | |
} | |
/** | |
* Send the request for the paste. | |
* | |
* @uses ControlV::Post() | |
* @param string $sPaste The post content you want to send. | |
* @return string Paste URL if successful, false if failure. | |
*/ | |
public function Post($sPaste) | |
{ | |
$this->aConfig['paste'] = $sPaste; | |
$this->sOutput = ControlV::Post($this->aConfig); | |
$bMatch = preg_match('/<url>(.*)<\/url>/', $this->sOutput, $aMatches); | |
return ($bMatch ? $aMatches[1] : false); | |
} | |
/** | |
* Getting the output XML, perfect if you want to fully analyse the output. | |
* | |
* @return string XML Output | |
*/ | |
public function getOutput() | |
{ | |
return $this->sOutput; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment