buildArguments($userInfo); // Arguments as string
//var_dump( $args ); exit;
$xml = $this->sendRequest('viddler.users.auth',$args); // Get XML response
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
if ($response['error']) {
return $response;
} else {
// Return record token too? Or just session id?
if (!$args['record_token']) {
return $response['auth']['sessionid'];
} else {
return $response['auth'];
}
}
} else {
return $xml;
}
return false;
}
/* viddler.users.getProfile
/ accepts: $username = string
/ returns: array - user's profile
/ doc: http://wiki.developers.viddler.com/index.php/Viddler.users.getProfile
*/
function user_profile($username) {
$xml = $this->sendRequest('viddler.users.getProfile','user='.$username); // Get XML response
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
if ($response['error']) {
return $response;
} else {
return $response['user'];
}
} else {
return $xml;
}
return false;
}
/* viddler.users.setOptions
/ accepts: $options = array
/ returns: string - number of options updated
/ doc: http://wiki.developers.viddler.com/index.php/Viddler.users.setOptions
*/
function user_setoptions($options) {
$args = '';
$args = $this->buildArguments($options); // Arguments as string
$xml = $this->sendRequest('viddler.users.setOptions',$args); // Get XML response
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
if ($response['error']) {
return $response;
} else {
return $response['user'];
}
} else {
return $xml;
}
return false;
}
/*########## Video functions ########### */
/* viddler.videos.upload
/ requires: POST HTTP Method
/ accepts: $videoInfo = array
/ returns: array - info about video uploaded
doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.upload
*/
function video_upload($videoInfo) {
$args = '';
$args = $this->buildArguments($videoInfo,'array'); // Arguments as array
$xml = $this->sendRequest('viddler.videos.upload',$args); // Get XML response
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
// Deletes the uploaded file from the server
//if ($this->uploaddir) {
// unlink($this->uploaddir.$_FILES['file']['name']);
//}
if ($response['error']) {
return $response;
} else {
return $response['video'];
}
} else {
return $xml;
}
return false;
}
/* viddler.videos.getRecordToken
/ accepts: $sessionid = string
/ returns: string - token needed for recording with webcam
}
/ doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getRecordToken
/ Instructions for use: http://wiki.developers.viddler.com/index.php/Record_With_Webcam_API
*/
function video_getrecordtoken($sessionid) {
$xml = $this->sendRequest('viddler.videos.getRecordToken','sessionid='.$sessionid); // Get XML response
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
if ($response['error']) {
return $response;
} else {
return $response['record_token'];
}
} else {
return $xml;
}
return false;
}
/* Not a method...
/ accepts: $token = string
/ returns: string - html for recorder embed with token included.
}
/ doc: None.
*/
function video_getrecordembed($token) {
if (!$token) return false;
$html = '';
return $html;
}
/* viddler.videos.getStatus
/ accepts: $videoID = string
/ returns: array - info about video's upload status
/ Method specific responses {
1: Waiting in encode queue (failed=0)
2: Encoding (failed=0)
3: Encoding process failed (failed=1)
4: Ready (failed=0)
5: Deleted (failed=0)
6: Wrong priviledges (failed=0)
}
/ doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getStatus
*/
function video_status($videoID) {
$xml = $this->sendRequest('video.status','video_id='.$videoID); // Get XML response
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
if ($response['error']) {
return $response;
} else {
return $response['video_status'];
}
} else {
return $xml;
}
return false;
}
/* viddler.videos.getDetails
/ accepts: $sessionid and $videoID = string
/ returns: array - info about video's upload status
/ Method specific responses {
1: Waiting in encode queue (failed=0)
2: Encoding (failed=0)
3: Encoding process failed (failed=1)
4: Ready (failed=0)
5: Deleted (failed=0)
6: Wrong priviledges (failed=0)
}
/ doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getDetails
*/
function video_details($sessionid,$videoID) {
$xml = $this->sendRequest('viddler.videos.getDetails','sessionid='.$sessionid.'&video_id='.$videoID); // Get XML response
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
if ($response['error']) {
return $response;
} else {
return $response['video'];
}
} else {
return $xml;
}
return false;
}
/* viddler.videos.getByUser
/ accepts: $user = string, $page = string, $per_page = string
/ returns: array - information about the videos matching this user
/ doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getByUser
*/
function videos_listbyuser($user,$page =1,$per_page =5) {
// Get XML response
$xml = $this->sendRequest('viddler.videos.getByUser','user='.$user.'&page='.$page.'&per_page='.$per_page);
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
if ($response['error']) {
return $response;
} else {
return $response['video_list'];
}
} else {
return $xml;
}
}
/* viddler.videos.getByTag
/ accepts: $tag = string, $page = string, $per_page = string
/ returns: array - information about videos matching this tag
/ doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getByTag
*/
function videos_listbytag($tag,$page =1,$per_page =5) {
// Get XML response
$xml = $this->sendRequest('viddler.videos.getByTag','tag='.$tag.'&page='.$page.'&per_page='.$per_page);
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
if ($response['error']) {
return $response;
} else {
return $response['video_list'];
}
} else {
return $xml;
}
return false;
}
/* viddler.videos.getFeatured
/ accepts: nothing.
/ returns: array - information about videos that are featured
/ doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getFeatured
*/
function videos_listfeatured() {
// Get XML response
$xml = $this->sendRequest('viddler.videos.getFeatured','');
// Use included parser?
if ($this->parser) {
// Several steps
// 1. Convert XML into an array
// 2. Check for errors
// i. Return array of error number, short, and long description
// 3. Return multidimensional array of response.
$response = $this->checkErrors(XML_unserialize($xml));
if ($response['error']) {
return $response;
} else {
return $response['video_list'];
}
} else {
return $xml;
}
return false;
}
/*########## Misc. Functions ########### */
// Error checking
function checkErrors($response) {
if ($response['error']) {
switch($response['error']) {
case '1':
$errorshort = 'An internal error has occurred.';
break;
case '2':
$errorshort = 'Bad argument format.';
break;
case '3':
$errorshort = 'Unknown argument specified.';
break;
case '4':
$errorshort = 'Missing required argument for this method.';
break;
case '5':
$errorshort = 'No method specified.';
break;
case '6':
$errorshort = 'Unknown method specified.';
break;
case '7':
$errorshort = 'API key missing.';
break;
case '8':
$errorshort = 'Invalid or unknown API key specified.';
break;
case '9':
$errorshort = 'Invalid or expired sessionid.';
break;
case '10':
$errorshort = 'HTTP method used not allowed on this method.';
break;
case '100':
$errorshort = 'Video could not be found.';
break;
case '101':
$errorshort = 'Username not found.';
break;
case '102':
$errorshort = 'This account has been suspended.';
break;
case '103':
$errorshort = 'Password incorrect for this username.';
break;
case '104':
$errorshort = 'Terms of Service not accepted by user.';
break;
case '105':
$errorshort = 'Username already in use.';
break;
case '106':
$errorshort = 'Email address already in use.';
break;
case '200':
$errorshort = 'The file is too large. Please limit to 500Mb.';
break;
default:
$errorshort = 'An unknown error has occured.';
break;
} // End switch
$error = array('error' => array('number'=>$response['error'],'shortdesc'=>$errorshort));
return $error;
}
return $response;
}
/**
* Build arguments to use for a request to the API
*
* @param array $p An associative array of parameters
* @param string $return_type Optional return type of parameters ['array'|'string']
* @return mixed The filtered arguments in the format requested
*/
public function buildArguments( $p, $return_type = 'string') {
$args = array();
foreach ($p as $key => $value) {
// Skip method request name and submit button
switch($key) {
case 'method':
case 'submit':
case 'MAX_FILE_SIZE':
continue;
}
$args[$key] = $value;
} // end foreach
switch($return_type) {
// If array assume uploading
case 'array':
$uploadfile = $this->uploaddir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
$args['file'] = "@{$uploadfile}";
}
break;
// If String, chop off last ampersand
case 'string':
default:
$args = http_build_query($args, null, '&');
break;
}
return $args;
}
// Send REST request
function sendRequest($method,$args) {
// Build Request URL
$reqURL = $this->viddlerREST.'?api_key='.$this->apiKey.'&method='.$method;
if ($method != 'viddler.videos.upload') $reqURL .= '&'.$args;
// Send request via CURL
$curl_handle = curl_init();
curl_setopt ($curl_handle, CURLOPT_URL, $reqURL); // Request URL
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1); // Return as string
curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 1); // Fail if timeout
// If sending a file, change to POST instead of GET
if ($method == 'viddler.videos.upload') {
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
}
$response = curl_exec($curl_handle); // Call!
if (!$response) $response = curl_error($curl_handle);
curl_close($curl_handle); // Close connection
// When CURL doesn't work, use this.
//$response = file_get_contents($reqURL);
// Return XML response as string
if (!$response) {
return false; // 'There is no response. CURL might not by allowed?
'.$reqURL.'
';
}
return $response;
} // End sentReq();
}
/**
* Viddler Silo
*/
class ViddlerSilo extends Plugin implements MediaSilo
{
const SILO_NAME = 'Viddler';
protected $cache = array();
/*
// add a rewrite rule for our auto-generated video page.
public function filter_rewrite_rules( $rules ) {
$rules[] = new RewriteRule(array(
'name' => 'viddlerlist',
'parse_regex' => '/^sillytv[\/]{0,1}$/i',
'build_str' => 'viddlerlist',
'handler' => 'ViddlerAdminHandler',
'action' => 'display_viddlerlist',
'priority' => 7,
'is_active' => 1,
));
return $rules;
}
*/
/**
* Initialize some internal values when plugin initializes
*/
public function action_init()
{
// add some js to the admin header
Stack::add( 'admin_header_javascript', $this->get_url(true) . 'vidfuncs.js', 'viddlerjs', array('media','jquery') );
$this->viddler = new Phpviddler();
}
/**
* Return basic information about this silo
* name- The name of the silo, used as the root directory for media in this silo
*/
public function silo_info()
{
if($this->is_auth()) {
return array('name' => self::SILO_NAME, 'icon' => URL::get_from_filesystem(__FILE__) . '/icon.png');
}
else {
return array();
}
}
/**
* Return directory contents for the silo path
*
* @param string $path The path to retrieve the contents of
* @return array An array of MediaAssets describing the contents of the directory
*/
public function silo_dir($path)
{
$user = User::identify()->info->viddler__username;
if(Cache::has('viddler:videos:' . $user)) {
$pre = Cache::get('viddler:videos:' . $user);
}
else {
$pre = $this->viddler->videos_listbyuser( $user, '', 20 );
Cache::set('viddler:videos:' . $user, $pre);
}
$xml = new SimpleXMLElement( $pre );
$results = array();
foreach( $xml->video as $video ) {
$props = array();
foreach($video->children() as $name => $value) {
$props[$name] = (string)$value;
}
$props['filetype'] = 'viddler';
$asset = new MediaAsset(
self::SILO_NAME . '/' . $path . ($path == '' ? '' : '/') . $video->id,
false,
$props
);
$results[] = $asset;
//echo '