Source of file VimeoService.php
Size: 13,933 Bytes - Last Modified: 2021-12-23T10:11:28+00:00
/var/www/docs.ssmods.com/process/src/code/VimeoService.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 | <?php /** * Service class to interface SilverStripe with the Vimeo video sharing service. * * @author Ronald Ferguson * @version 1.0 * @package vimeoservice */ class VimeoService Extends RestfulService { /** * Default player width * @access protected * @static */ protected static $player_width = 400; /** * Default player height * @access protected * @static */ protected static $player_height = 225; /** * API key supplied by Vimeo to use the advanced api * @access protected * @static */ protected static $api_key; /** * API key secret supplied by Vimeo for use with the API key * @access protected * @static */ protected static $api_secret_key; protected static $token; protected static $token_secret; /** * Base URL for the Vimeo advanced api * @static */ public static $api_base_url = 'http://vimeo.com/api/rest/v2'; /** * @var integer * @access protected */ protected $video_count; /** * @var integer * @access protected */ protected $page_count; /** * Constructor * @param Set the cache expiry interva. Defaults to 1 hour (3600 seconds) * @see RestfulService * @return void */ function __construct($expiry=NULL) { parent::__construct(self::$api_base_url, $expiry); $this->checkErrors = true; } function errorCatch($response){ $err_msg = $response; if(strpos($err_msg, '<') === false) user_error("VimeoService Error: $err_msg", E_USER_ERROR); return $response; } /** * Set the API key supplied by Vimeo * @param string * @static */ public static function setAPIKey($value) { self::$api_key = trim($value); } /** * Set the API key secret provided by Vimeo. * @param string * @static */ public static function setSecretKey($value) { self::$api_secret_key = trim($value); } public static function setDefaultWidth($value) { $value = intval($value); if($value == 0) { $value = 400; } self::$player_width = $value; } public static function setDefaultHeight($value) { $value = intval($value); if($value == 0) { $value = 225; } self::$player_height = $value; } public static function getDefaultHeight() { return self::$player_height; } public static function getDefaultWidth() { return self::$player_width; } /** * Returns a collection of Vimeo videos for a given method. * @param string * @param array * @param integer * @return DataObjectSet or boolean false */ function getVideoFeed($method=NULL, $call_params=array(), $page_limit=10) { if(!self::$api_key || ! self::$api_secret_key) { user_error('Fatal error: Invalid API keyand/or secret specified!'); return false; } // Prepare oauth arguments $oauth_params = array( 'oauth_consumer_key' => self::$api_key, 'oauth_version' => '1.0', 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => time(), 'oauth_nonce' => $this->_generateNonce() ); $page = $call_params['start'] == 0 ? 1 : (($call_params['start']) / $page_limit + 1); if(self::$token) { $oauth_params['oauth_token'] = self::$token; } $api_params = array( 'format' => 'php', 'full_response' => 1, 'per_page' => $page_limit, 'page' => $page ); if (!empty($method)) { $api_params['method'] = $method; } // Merge args foreach ($call_params as $k => $v) { if (strpos($k, 'oauth_') === 0) { $oauth_params[$k] = $v; } else { $api_params[$k] = $v; } } // Generate the signature $oauth_params['oauth_signature'] = $this->_generateSignature(array_merge($oauth_params, $api_params), self::$api_base_url); // Merge all args $params = array_merge($oauth_params, $api_params); $this->setQueryString($params); $response = $this->request(); $videos = unserialize($response->getBody()); if(isset($videos->videos)) { $results = new ArrayList(); foreach($videos->videos->video as $video) { $data = $this->_extractVideoInfo($video); /* * @todo Add sorting */ $results->push(new ArrayData($data)); } // since we manually created the dataobjectset, we need to set the pager info manually, too. $results->limit($call_params['start'], $page_limit, intval($videos->videos->total)); } else { $results = false; } return $results; } /** * Returns a Vimeo video object * @param array * @return ArrayData or boolean false */ function getSingleVideo($call_params=array()) { if(!self::$api_key || ! self::$api_secret_key) { user_error('Fatal error: Invalid API keyand/or secret specified!'); return false; } // Prepare oauth arguments $oauth_params = array( 'oauth_consumer_key' => self::$api_key, 'oauth_version' => '1.0', 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => time(), 'oauth_nonce' => $this->_generateNonce() ); if(self::$token) { $oauth_params['oauth_token'] = self::$token; } $api_params = array( 'format' => 'php', 'method' => 'vimeo.videos.getInfo' ); // Merge args foreach ($call_params as $k => $v) { if (strpos($k, 'oauth_') === 0) { $oauth_params[$k] = $v; } else { $api_params[$k] = $v; } } // Generate the signature $oauth_params['oauth_signature'] = $this->_generateSignature(array_merge($oauth_params, $api_params), self::$api_base_url); // Merge all args $params = array_merge($oauth_params, $api_params); $this->setQueryString($params); $response = $this->request(); $video = unserialize($response->getBody()); if(isset($video->video)) { $results = new DataObjectSet(); foreach($video->video as $video) { $data = $this->_extractVideoInfo($video); $results->push(new ArrayData($data)); } // since we manually created the dataobjectset, we need to set the pager info manually, too. $results = $results->First(); } else { $results = false; } return $results; } /** * Extracts and populates an array structure with the video information returned from Vimeo for a single video object. * @param Object * @return array */ private function _extractVideoInfo($data) { $video = array(); $video['ID'] = intval($data->id); $video['Title'] = new Text(null); $video['Title']->setValue(trim($data->title)); /* * need to clean up the text returned from Vimeo and paragraph format it so we can use * all the nifty SilverStripe text functionality. :) */ $desc = nl2br(trim($data->description)); $desc = str_replace("<br />\n<br />", '<br />', $desc); $desc = '<p>' . str_replace('<br />', '</p><p>', $desc) . '</p>'; $video['Description'] = new HTMLText(); $video['Description']->setValue($desc); // video url info foreach($data->urls->url as $url) { switch($url->type) { case 'video': $video['Url'] = new Text(); $video['Url']->setValue(trim($url->_content)); break; case 'mobile': $video['MobileUrl'] = new Text(); $video['MobileUrl']->setValue(trim($url->_content)); break; default: // do nothing. break; } } // video date info $upload_date = trim($data->upload_date); // create a SS_Datetime object so we can use familiar functionality. $ud = new SS_Datetime(); $ud->setValue($upload_date); $video['UploadDate'] = $ud; $modified_date = trim($data->modified_date); // create a SS_Datetime object so we can use familiar functionality. $md = new SS_Datetime(); $md->setValue($modified_date); $video['ModifiedDate'] = $md; // video thumbnail info foreach($data->thumbnails->thumbnail as $tn) { if(strpos($tn->_content, '_100.jpg') !== false) { $video['ThumbSmall'] = new Text(); $video['ThumbSmall']->setValue(trim($tn->_content)); } elseif (strpos($tn->_content, '_200.jpg') !== false) { $video['ThumbMedium'] = new Text(); $video['ThumbMedium']->setValue(trim($tn->_content)); } elseif (strpos($tn->_content, '_640.jpg') !== false) { $video['ThumbLarge'] = new Text(); $video['ThumbLarge']->setValue(trim($tn->_content)); } } // User information $video['UserName'] = new Varchar(null, 255); $video['UserName']->setValue(trim($data->owner->username)); $video['UserDisplayName'] = new Varchar(null, 255); $video['UserDisplayName']->setValue(trim($data->owner->display_name)); $video['UserRealName'] = new Varchar(null, 255); $video['UserRealName']->setValue(trim($data->owner->realname)); $video['UserID'] = (int)trim($data->owner->id); $video['UserUrl'] = new Text(); $video['UserUrl']->setValue(trim($data->owner->profileurl)); // User photos for($i=0;$i<count($data->owner->portraits->portrait);$i++) { $tn = $data->owner->portraits->portrait[$i]; switch($i) { case 0: $video['UserPortraitSmall'] = new Text(); $video['UserPortraitSmall']->setValue(trim($tn->_content)); break; case 1: $video['UserPortraitMedium'] = new Text(); $video['UserPortraitMedium']->setValue(trim($tn->_content)); break; case 2: $video['UserPortraitLarge'] = new Text(); $video['UserPortraitLarge']->setValue(trim($tn->_content)); break; case 3: $video['UserPortraitXLarge'] = new Text(); $video['UserPortraitXLarge']->setValue(trim($tn->_content)); break; default: // do nothing. break; } } $video['NumberLikes'] = intval(trim($data->number_of_likes)); $video['NumberPlays'] = intval(trim($data->number_of_plays)); $video['NumberComments'] = intval(trim($data->number_of_comments)); $video['Duration'] = intval(trim($data->duration)); $video['Width'] = intval(trim($data->width)); $video['Height'] = intval(trim($data->height)); $video['IsHD'] = new Boolean(null); $video['IsHD']->setValue(intval($data->is_hd)); $video['EmbedPrivacy'] = new Text(); $video['EmbedPrivacy']->setValue(trim($data->embed_privacy)); return $video; } /** * Generates unique identifier used in OAuth signature. * @access protected * @return string */ protected function _generateNonce() { return md5(uniqid(microtime())); } /** * Generates OAuth signature required to call Vimeo advanced API functions. * @param array * @param string * @param string * @access protected * @return string * @see https://github.com/vimeo/vimeo-php-lib */ protected function _generateSignature($params, $url, $request_method = 'GET') { uksort($params, 'strcmp'); $params = self::url_encode_rfc3986($params); // Make the base string $base_parts = array( strtoupper($request_method), $url, urldecode(http_build_query($params, '', '&')) ); $base_parts = self::url_encode_rfc3986($base_parts); $base_string = implode('&', $base_parts); // Make the key $key_parts = array( self::$api_secret_key, (self::$token_secret) ? self::$token_secret : '' ); $key_parts = self::url_encode_rfc3986($key_parts); $key = implode('&', $key_parts); // Generate signature return base64_encode(hash_hmac('sha1', $base_string, $key, true)); } /** * Gets paged video list for videos of a specific user. * @param string * @param integer * @param integer * @param string * @return DataObjectSet or boolean false * @see getVideoFeed() */ public function getVideosByUser($userid, $start=0, $per_page=10, $sort=null) { $params = array('user_id' => $userid, 'format' => 'php', 'start' => intval($start)); if($sort) $params['sort'] = $sort; $method = 'vimeo.videos.getAll'; return $this->getVideoFeed($method, $params, $per_page); } /** * Gets paged video list for videos of a specific Vimeo group. * @param string * @param integer * @param integer * @param string * @return DataObjectSet or boolean false * @see getVideoFeed() */ public function getVideosByGroup($groupid, $start=0, $per_page=10, $sort=null) { $params = array('group_id' => $groupid, 'format' => 'php', 'start' => intval($start)); if($sort) $params['sort'] = $sort; $method = 'vimeo.groups.getVideos'; return $this->getVideoFeed($method, $params, $per_page); } /** * Gets paged video list for videos of a specific Vimeo album. * @param integer * @param integer * @param integer * @param string * @return DataObjectSet or boolean false * @see getVideoFeed() */ public function getVideosByAlbum($albumid, $start=0, $per_page=10, $sort=null) { // vimeo.groups.getVideos $params = array('album_id' => $albumid, 'format' => 'php', 'start' => intval($start)); if($sort) $params['sort'] = $sort; $method = 'vimeo.albums.getVideos'; return $this->getVideoFeed($method, $params, $per_page); } /** * Gets specific Vimeo video. * @param integer * @return ArrayData or boolean false * @see getSingleVideo() */ public function getVideoById($videoid) { $params = array('video_id' => $videoid); return $this->getSingleVideo($params); } /** * URL encode a parameter or array of parameters. * @param mixed * @return mixed */ public static function url_encode_rfc3986($input) { if (is_array($input)) { return array_map(array('VimeoService', 'url_encode_rfc3986'), $input); } else if (is_scalar($input)) { return str_replace(array('+', '%7E'), array(' ', '~'), rawurlencode($input)); } else { return ''; } } } ?> |