From 7d7c1c961d41615652c00d53083a315f7840c86d Mon Sep 17 00:00:00 2001 From: Chris Graham Date: Sat, 12 Jan 2013 21:22:34 +0000 Subject: [PATCH 1/6] Fixed incorrect/inconsistent phpdoc. --- vimeo.php | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/vimeo.php b/vimeo.php index b845db2..1a349b7 100644 --- a/vimeo.php +++ b/vimeo.php @@ -31,6 +31,7 @@ public function __construct($consumer_key, $consumer_secret, $token = null, $tok * * @param array $params The parameters for the response. * @param string $response The serialized response data. + * @return boolean Success status, or NULL if cache not enabled. */ private function _cache($params, $response) { @@ -70,7 +71,7 @@ private function _generateAuthHeader($oauth_params) /** * Generate a nonce for the call. * - * @return string The nonce + * @return string The nonce. */ private function _generateNonce() { @@ -115,6 +116,7 @@ private function _generateSignature($params, $request_method = 'GET', $url = sel * Get the unserialized contents of the cached request. * * @param array $params The full list of api parameters for the request. + * @return object Cached request response, or NULL if not cached. */ private function _getCached($params) { @@ -142,7 +144,7 @@ private function _getCached($params) * @param string $url The base URL to use. * @param boolean $cache Whether or not to cache the response. * @param boolean $use_auth_header Use the OAuth Authorization header to pass the OAuth params. - * @return string The response from the method call. + * @return object The response from the method call, or false on error. */ private function _request($method, $call_params = array(), $request_method = 'GET', $url = self::API_REST_URL, $cache = true, $use_auth_header = true) { @@ -250,6 +252,7 @@ private function _request($method, $call_params = array(), $request_method = 'GE * http://www.vimeo.com/api/docs/oauth * * @param string $perms The level of permissions to request: read, write, or delete. + * @param string $callback_url The callback URL. */ public function auth($permission = 'read', $callback_url = 'oob') { @@ -267,7 +270,7 @@ public function auth($permission = 'read', $callback_url = 'oob') * @param string $request_method The HTTP request method to use. * @param string $url The base URL to use. * @param boolean $cache Whether or not to cache the response. - * @return array The response from the API method + * @return object The response from the API method, or false on error. */ public function call($method, $params = array(), $request_method = 'GET', $url = self::API_REST_URL, $cache = true) { @@ -303,6 +306,7 @@ public function enableCache($type, $path, $expire = 600) * request token before calling this function. * * @param string $verifier The OAuth verifier returned from the authorization page or the user. + * @return array An array with the token and token secret. */ public function getAccessToken($verifier) { @@ -326,6 +330,9 @@ public function getAuthorizeUrl($token, $permission = 'read') /** * Get a request token. + * + * @param string $callback_url The callback URL. + * @return array The request token. */ public function getRequestToken($callback_url = 'oob') { @@ -355,11 +362,11 @@ public function getToken() /** * Set the OAuth token. * - * @param string $token The OAuth token - * @param string $token_secret The OAuth token secret - * @param string $type The type of token, either request or access - * @param boolean $session_store Store the token in a session variable - * @return boolean true + * @param string $token The OAuth token. + * @param string $token_secret The OAuth token secret. + * @param string $type The type of token, either request or access. + * @param boolean $session_store Store the token in a session variable. + * @return boolean true. */ public function setToken($token, $token_secret, $type = 'access', $session_store = false) { @@ -377,11 +384,12 @@ public function setToken($token, $token_secret, $type = 'access', $session_store /** * Upload a video in one piece. * - * @param string $file_path The full path to the file - * @param boolean $use_multiple_chunks Whether or not to split the file up into smaller chunks - * @param string $chunk_temp_dir The directory to store the chunks in - * @param int $size The size of each chunk in bytes (defaults to 2MB) - * @return int The video ID + * @param string $file_path The full path to the file. + * @param boolean $use_multiple_chunks Whether or not to split the file up into smaller chunks. + * @param string $chunk_temp_dir The directory to store the chunks in. + * @param int $size The size of each chunk in bytes (defaults to 2MB). + * @param int $replace_id The ID of the video to replace, or NULL if not replacing a video. + * @return int The video ID. */ public function upload($file_path, $use_multiple_chunks = false, $chunk_temp_dir = '.', $size = 2097152, $replace_id = null) { @@ -523,6 +531,7 @@ public function uploadMulti($file_name, $size = 1048576) * URL encode a parameter or array of parameters. * * @param array/string $input A parameter or set of parameters to encode. + * @return array/string URL encoded version of input. */ public static function url_encode_rfc3986($input) { From adf5ba77b1ffc36efd835f1df79e42f592a88251 Mon Sep 17 00:00:00 2001 From: Chris Graham Date: Sat, 12 Jan 2013 21:23:45 +0000 Subject: [PATCH 2/6] Added developer hint if they don't realise that call parameters are maps not lists --- vimeo.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vimeo.php b/vimeo.php index 1a349b7..976cc79 100644 --- a/vimeo.php +++ b/vimeo.php @@ -170,6 +170,10 @@ private function _request($method, $call_params = array(), $request_method = 'GE // Merge args foreach ($call_params as $k => $v) { + if (!is_string($k)) { + throw new VimeoAPIException('API parameters require key names', 0); + } + if (strpos($k, 'oauth_') === 0) { $oauth_params[$k] = $v; } From 6843ac55e84da9c8ee34b68f59467e49655e9c6e Mon Sep 17 00:00:00 2001 From: Chris Graham Date: Sat, 12 Jan 2013 21:25:38 +0000 Subject: [PATCH 3/6] Vimeo API docs talk of booleans, but those result in hash signature errors due to the code not handling them correctly - rather than making programmers use digits where it says booleans, support booleans. --- vimeo.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/vimeo.php b/vimeo.php index 976cc79..7f4da13 100644 --- a/vimeo.php +++ b/vimeo.php @@ -324,7 +324,6 @@ public function getAccessToken($verifier) * * @param string $token The request token. * @param string $permission The level of permissions to request: read, write, or delete. - * @param string $callback_url The URL to redirect the user back to, or oob for the default. * @return string The Authorization URL. */ public function getAuthorizeUrl($token, $permission = 'read') @@ -534,7 +533,7 @@ public function uploadMulti($file_name, $size = 1048576) /** * URL encode a parameter or array of parameters. * - * @param array/string $input A parameter or set of parameters to encode. + * @param array/string/int/boolean $input A parameter or set of parameters to encode. * @return array/string URL encoded version of input. */ public static function url_encode_rfc3986($input) @@ -543,6 +542,14 @@ public static function url_encode_rfc3986($input) return array_map(array('phpVimeo', 'url_encode_rfc3986'), $input); } else if (is_scalar($input)) { + if (!is_string($input)) { + if (is_bool($input)) { + $input=$input?'1':'0'; + } else { + $input=strval($input); + } + } + return str_replace(array('+', '%7E'), array(' ', '~'), rawurlencode($input)); } else { From 11746f6d36d5447cededdb0174d410c07d2e4e32 Mon Sep 17 00:00:00 2001 From: Chris Graham Date: Sat, 12 Jan 2013 21:27:16 +0000 Subject: [PATCH 4/6] Put more meaningful API error message into the PHP thrown errors --- vimeo.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vimeo.php b/vimeo.php index 7f4da13..e6bc620 100644 --- a/vimeo.php +++ b/vimeo.php @@ -237,12 +237,16 @@ private function _request($method, $call_params = array(), $request_method = 'GE // Return if (!empty($method)) { + if ($response===false) { + throw new VimeoAPIException('API call returned false;', 0); + } + $response = unserialize($response); if ($response->stat == 'ok') { return $response; } else if ($response->err) { - throw new VimeoAPIException($response->err->msg, $response->err->code); + throw new VimeoAPIException($response->err->expl, $response->err->code); } return false; @@ -515,7 +519,7 @@ public function upload($file_path, $use_multiple_chunks = false, $chunk_temp_dir return $complete->ticket->video_id; } else if ($complete->err) { - throw new VimeoAPIException($complete->err->msg, $complete->err->code); + throw new VimeoAPIException($complete->err->expl, $complete->err->code); } } From 27d9731ebbbde5dd11e0fa3b43e7220bdbaad73b Mon Sep 17 00:00:00 2001 From: Chris Graham Date: Sat, 12 Jan 2013 21:28:26 +0000 Subject: [PATCH 5/6] Fix broken chunked upload handling, and add proper Exception support for it --- vimeo.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vimeo.php b/vimeo.php index e6bc620..bf25c49 100644 --- a/vimeo.php +++ b/vimeo.php @@ -493,11 +493,11 @@ public function upload($file_path, $use_multiple_chunks = false, $chunk_temp_dir // Make sure our file sizes match up foreach ($verify->ticket->chunks as $chunk_check) { - $chunk = $chunks[$chunk_check->id]; + $chunk = $chunks[$chunk_check['id']]; - if ($chunk['size'] != $chunk_check->size) { + if ($chunk['size'] != intval($chunk_check['size'])) { // size incorrect, uh oh - echo "Chunk {$chunk_check->id} is actually {$chunk['size']} but uploaded as {$chunk_check->size}
"; + throw new VimeoAPIException("Chunk {$chunk_check['id']} is actually {$chunk['size']} but uploaded as {$chunk_check['size']}", -1); } } From a9d2ee6b26f934b45774a9afa463b7adb9062cf9 Mon Sep 17 00:00:00 2001 From: Chris Graham Date: Sat, 12 Jan 2013 21:29:16 +0000 Subject: [PATCH 6/6] Fix sloppy implicit type conversions, declare code more carefully --- vimeo.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/vimeo.php b/vimeo.php index bf25c49..8dfcc67 100644 --- a/vimeo.php +++ b/vimeo.php @@ -177,7 +177,7 @@ private function _request($method, $call_params = array(), $request_method = 'GE if (strpos($k, 'oauth_') === 0) { $oauth_params[$k] = $v; } - else if ($call_params[$k] !== null) { + else if ($v !== null) { $api_params[$k] = $v; } } @@ -189,7 +189,7 @@ private function _request($method, $call_params = array(), $request_method = 'GE $all_params = array_merge($oauth_params, $api_params); // Returned cached value - if ($this->_cache_enabled && ($cache && $response = $this->_getCached($all_params))) { + if ($this->_cache_enabled && ($cache && ($response = $this->_getCached($all_params)) !== NULL)) { return $response; } @@ -242,11 +242,12 @@ private function _request($method, $call_params = array(), $request_method = 'GE } $response = unserialize($response); + if ($response->stat == 'ok') { return $response; } - else if ($response->err) { - throw new VimeoAPIException($response->err->expl, $response->err->code); + else if (isset($response->err)) { + throw new VimeoAPIException($response->err->expl, intval($response->err->code)); } return false; @@ -319,6 +320,7 @@ public function enableCache($type, $path, $expire = 600) public function getAccessToken($verifier) { $access_token = $this->_request(null, array('oauth_verifier' => $verifier), 'GET', self::API_ACCESS_TOKEN_URL, false, true); + $parsed = array(); parse_str($access_token, $parsed); return $parsed; } @@ -352,6 +354,7 @@ public function getRequestToken($callback_url = 'oob') false ); + $parsed = array(); parse_str($request_token, $parsed); return $parsed; } @@ -411,7 +414,7 @@ public function upload($file_path, $use_multiple_chunks = false, $chunk_temp_dir // Make sure we have enough room left in the user's quota $quota = $this->call('vimeo.videos.upload.getQuota'); - if ($quota->user->upload_space->free < $file_size) { + if (intval($quota->user->upload_space->free) < $file_size) { throw new VimeoAPIException('The file is larger than the user\'s remaining quota.', 707); } @@ -427,7 +430,7 @@ public function upload($file_path, $use_multiple_chunks = false, $chunk_temp_dir $endpoint = $rsp->ticket->endpoint; // Make sure we're allowed to upload this size file - if ($file_size > $rsp->ticket->max_file_size) { + if ($file_size > intval($rsp->ticket->max_file_size)) { throw new VimeoAPIException('File exceeds maximum allowed size.', 710); }