forked from vimeo/vimeo-php-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvimeo.php
569 lines (497 loc) · 18.8 KB
/
vimeo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<?php
class phpVimeo
{
const API_REST_URL = 'http://vimeo.com/api/rest/v2';
const API_AUTH_URL = 'http://vimeo.com/oauth/authorize';
const API_ACCESS_TOKEN_URL = 'http://vimeo.com/oauth/access_token';
const API_REQUEST_TOKEN_URL = 'http://vimeo.com/oauth/request_token';
const CACHE_FILE = 'file';
private $_consumer_key = false;
private $_consumer_secret = false;
private $_cache_enabled = false;
private $_cache_dir = false;
private $_token = false;
private $_token_secret = false;
private $_upload_md5s = array();
public function __construct($consumer_key, $consumer_secret, $token = null, $token_secret = null)
{
$this->_consumer_key = $consumer_key;
$this->_consumer_secret = $consumer_secret;
if ($token && $token_secret) {
$this->setToken($token, $token_secret);
}
}
/**
* Cache a response.
*
* @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)
{
// Remove some unique things
unset($params['oauth_nonce']);
unset($params['oauth_signature']);
unset($params['oauth_timestamp']);
$hash = md5(serialize($params));
if ($this->_cache_enabled == self::CACHE_FILE) {
$file = $this->_cache_dir.'/'.$hash.'.cache';
if (file_exists($file)) {
unlink($file);
}
return file_put_contents($file, $response);
}
}
/**
* Create the authorization header for a set of params.
*
* @param array $oauth_params The OAuth parameters for the call.
* @return string The OAuth Authorization header.
*/
private function _generateAuthHeader($oauth_params)
{
$auth_header = 'Authorization: OAuth realm=""';
foreach ($oauth_params as $k => $v) {
$auth_header .= ','.self::url_encode_rfc3986($k).'="'.self::url_encode_rfc3986($v).'"';
}
return $auth_header;
}
/**
* Generate a nonce for the call.
*
* @return string The nonce.
*/
private function _generateNonce()
{
return md5(uniqid(microtime()));
}
/**
* Generate the OAuth signature.
*
* @param array $args The full list of args to generate the signature for.
* @param string $request_method The request method, either POST or GET.
* @param string $url The base URL to use.
* @return string The OAuth signature.
*/
private function _generateSignature($params, $request_method = 'GET', $url = self::API_REST_URL)
{
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(
$this->_consumer_secret,
($this->_token_secret) ? $this->_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));
}
/**
* 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)
{
// Remove some unique things
unset($params['oauth_nonce']);
unset($params['oauth_signature']);
unset($params['oauth_timestamp']);
$hash = md5(serialize($params));
if ($this->_cache_enabled == self::CACHE_FILE) {
$file = $this->_cache_dir.'/'.$hash.'.cache';
if (file_exists($file)) {
return unserialize(file_get_contents($file));
}
}
}
/**
* Call an API method.
*
* @param string $method The method to call.
* @param array $call_params The parameters to pass to the method.
* @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.
* @param boolean $use_auth_header Use the OAuth Authorization header to pass the OAuth params.
* @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)
{
// Prepare oauth arguments
$oauth_params = array(
'oauth_consumer_key' => $this->_consumer_key,
'oauth_version' => '1.0',
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_nonce' => $this->_generateNonce()
);
// If we have a token, include it
if ($this->_token) {
$oauth_params['oauth_token'] = $this->_token;
}
// Regular args
$api_params = array('format' => 'php');
if (!empty($method)) {
$api_params['method'] = $method;
}
// 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;
}
else if ($v !== null) {
$api_params[$k] = $v;
}
}
// Generate the signature
$oauth_params['oauth_signature'] = $this->_generateSignature(array_merge($oauth_params, $api_params), $request_method, $url);
// Merge all args
$all_params = array_merge($oauth_params, $api_params);
// Returned cached value
if ($this->_cache_enabled && ($cache && ($response = $this->_getCached($all_params)) !== NULL)) {
return $response;
}
// Curl options
if ($use_auth_header) {
$params = $api_params;
}
else {
$params = $all_params;
}
if (strtoupper($request_method) == 'GET') {
$curl_url = $url.'?'.http_build_query($params, '', '&');
$curl_opts = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30
);
}
else if (strtoupper($request_method) == 'POST') {
$curl_url = $url;
$curl_opts = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($params, '', '&')
);
}
// Authorization header
if ($use_auth_header) {
$curl_opts[CURLOPT_HTTPHEADER] = array($this->_generateAuthHeader($oauth_params));
}
// Call the API
$curl = curl_init($curl_url);
curl_setopt_array($curl, $curl_opts);
$response = curl_exec($curl);
$curl_info = curl_getinfo($curl);
curl_close($curl);
// Cache the response
if ($this->_cache_enabled && $cache) {
$this->_cache($all_params, $response);
}
// 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 (isset($response->err)) {
throw new VimeoAPIException($response->err->expl, intval($response->err->code));
}
return false;
}
return $response;
}
/**
* Send the user to Vimeo to authorize your app.
* 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')
{
$t = $this->getRequestToken($callback_url);
$this->setToken($t['oauth_token'], $t['oauth_token_secret'], 'request', true);
$url = $this->getAuthorizeUrl($this->_token, $permission);
header("Location: {$url}");
}
/**
* Call a method.
*
* @param string $method The name of the method to call.
* @param array $params The parameters to pass to the method.
* @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 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)
{
$method = (substr($method, 0, 6) != 'vimeo.') ? "vimeo.{$method}" : $method;
return $this->_request($method, $params, $request_method, $url, $cache);
}
/**
* Enable the cache.
*
* @param string $type The type of cache to use (phpVimeo::CACHE_FILE is built in)
* @param string $path The path to the cache (the directory for CACHE_FILE)
* @param int $expire The amount of time to cache responses (default 10 minutes)
*/
public function enableCache($type, $path, $expire = 600)
{
$this->_cache_enabled = $type;
if ($this->_cache_enabled == self::CACHE_FILE) {
$this->_cache_dir = $path;
$files = scandir($this->_cache_dir);
foreach ($files as $file) {
$last_modified = filemtime($this->_cache_dir.'/'.$file);
if (substr($file, -6) == '.cache' && ($last_modified + $expire) < time()) {
unlink($this->_cache_dir.'/'.$file);
}
}
}
return false;
}
/**
* Get an access token. Make sure to call setToken() with the
* 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)
{
$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;
}
/**
* Get the URL of the authorization page.
*
* @param string $token The request token.
* @param string $permission The level of permissions to request: read, write, or delete.
* @return string The Authorization URL.
*/
public function getAuthorizeUrl($token, $permission = 'read')
{
return self::API_AUTH_URL."?oauth_token={$token}&permission={$permission}";
}
/**
* Get a request token.
*
* @param string $callback_url The callback URL.
* @return array The request token.
*/
public function getRequestToken($callback_url = 'oob')
{
$request_token = $this->_request(
null,
array('oauth_callback' => $callback_url),
'GET',
self::API_REQUEST_TOKEN_URL,
false,
false
);
$parsed = array();
parse_str($request_token, $parsed);
return $parsed;
}
/**
* Get the stored auth token.
*
* @return array An array with the token and token secret.
*/
public function getToken()
{
return array($this->_token, $this->_token_secret);
}
/**
* 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.
*/
public function setToken($token, $token_secret, $type = 'access', $session_store = false)
{
$this->_token = $token;
$this->_token_secret = $token_secret;
if ($session_store) {
$_SESSION["{$type}_token"] = $token;
$_SESSION["{$type}_token_secret"] = $token_secret;
}
return true;
}
/**
* 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).
* @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)
{
if (!file_exists($file_path)) {
return false;
}
// Figure out the filename and full size
$path_parts = pathinfo($file_path);
$file_name = $path_parts['basename'];
$file_size = filesize($file_path);
// Make sure we have enough room left in the user's quota
$quota = $this->call('vimeo.videos.upload.getQuota');
if (intval($quota->user->upload_space->free) < $file_size) {
throw new VimeoAPIException('The file is larger than the user\'s remaining quota.', 707);
}
// Get an upload ticket
$params = array();
if ($replace_id) {
$params['video_id'] = $replace_id;
}
$rsp = $this->call('vimeo.videos.upload.getTicket', $params, 'GET', self::API_REST_URL, false);
$ticket = $rsp->ticket->id;
$endpoint = $rsp->ticket->endpoint;
// Make sure we're allowed to upload this size file
if ($file_size > intval($rsp->ticket->max_file_size)) {
throw new VimeoAPIException('File exceeds maximum allowed size.', 710);
}
// Split up the file if using multiple pieces
$chunks = array();
if ($use_multiple_chunks) {
if (!is_writeable($chunk_temp_dir)) {
throw new Exception('Could not write chunks. Make sure the specified folder has write access.');
}
// Create pieces
$number_of_chunks = ceil(filesize($file_path) / $size);
for ($i = 0; $i < $number_of_chunks; $i++) {
$chunk_file_name = "{$chunk_temp_dir}/{$file_name}.{$i}";
// Break it up
$chunk = file_get_contents($file_path, FILE_BINARY, null, $i * $size, $size);
$file = file_put_contents($chunk_file_name, $chunk);
$chunks[] = array(
'file' => realpath($chunk_file_name),
'size' => filesize($chunk_file_name)
);
}
}
else {
$chunks[] = array(
'file' => realpath($file_path),
'size' => filesize($file_path)
);
}
// Upload each piece
foreach ($chunks as $i => $chunk) {
$params = array(
'oauth_consumer_key' => $this->_consumer_key,
'oauth_token' => $this->_token,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_nonce' => $this->_generateNonce(),
'oauth_version' => '1.0',
'ticket_id' => $ticket,
'chunk_id' => $i
);
// Generate the OAuth signature
$params = array_merge($params, array(
'oauth_signature' => $this->_generateSignature($params, 'POST', self::API_REST_URL),
'file_data' => '@'.$chunk['file'] // don't include the file in the signature
));
// Post the file
$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$rsp = curl_exec($curl);
curl_close($curl);
}
// Verify
$verify = $this->call('vimeo.videos.upload.verifyChunks', array('ticket_id' => $ticket));
// Make sure our file sizes match up
foreach ($verify->ticket->chunks as $chunk_check) {
$chunk = $chunks[$chunk_check['id']];
if ($chunk['size'] != intval($chunk_check['size'])) {
// size incorrect, uh oh
throw new VimeoAPIException("Chunk {$chunk_check['id']} is actually {$chunk['size']} but uploaded as {$chunk_check['size']}", -1);
}
}
// Complete the upload
$complete = $this->call('vimeo.videos.upload.complete', array(
'filename' => $file_name,
'ticket_id' => $ticket
));
// Clean up
if (count($chunks) > 1) {
foreach ($chunks as $chunk) {
unlink($chunk['file']);
}
}
// Confirmation successful, return video id
if ($complete->stat == 'ok') {
return $complete->ticket->video_id;
}
else if ($complete->err) {
throw new VimeoAPIException($complete->err->expl, $complete->err->code);
}
}
/**
* Upload a video in multiple pieces.
*
* @deprecated
*/
public function uploadMulti($file_name, $size = 1048576)
{
// for compatibility with old library
return $this->upload($file_name, true, '.', $size);
}
/**
* URL encode a parameter or array of parameters.
*
* @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)
{
if (is_array($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 {
return '';
}
}
}
class VimeoAPIException extends Exception {}