forked from thePlatform/thePlatform-video-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thePlatform-API.php
executable file
·709 lines (542 loc) · 20.6 KB
/
thePlatform-API.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<?php
/* thePlatform Video Manager Wordpress Plugin
Copyright (C) 2013-2014 thePlatform for Media Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
defined('JSON_UNESCAPED_SLASHES') or define('JSON_UNESCAPED_SLASHES', 64);
/**
* Wrapper class around Wordpress HTTP methods
*/
class ThePlatform_API_HTTP {
/**
* HTTP GET wrapper
* @param string $url URL to make the request to
* @param array $data Data to send with the request, default is a blank array
* @return wp_response Results of the GET request
*/
static function get( $url, $data = array() ) {
// esc_url_raw eats []'s , so I'm forced to skip it for urls containing
// those characters - at this time only the account list request
if(!strpos($url, '[0]'))
$url = esc_url_raw($url);
$response = wp_remote_get( $url, $data );
return $response;
}
/**
* HTTP PUT wrapper
* @param string $url URL to make the request to
* @param array $data Data to send with the request, default is a blank array
* @return wp_response Results of the GET request
*/
static function put( $url, $data = array() ) {
return ThePlatform_API_HTTP::post( $url, $data, TRUE, 'PUT' );
}
/**
* HTTP POST wrapper
* @param string $url URL to make the request to
* @param array $data Data to send with the request, default is a blank array
* @param boolean $isJSON Whether our data is JSON encoded or not, default is FALSE
* @param string $method Sets the header HTTP request method, default is POST
* @return wp_response Results of the GET request
*/
static function post( $url, $data, $isJSON = FALSE, $method = 'POST' ) {
$url = esc_url_raw( $url );
$args = array(
'method' => $method,
'body' => $data,
'timeout' => 10,
);
if ( $isJSON ) {
$args['headers'] = array( 'Content-Type' => 'application/json; charset=UTF-8' );
}
$response = wp_remote_post( $url, $args );
return $response;
}
}
/**
* Wrapper for MPX's API calls
* @package default
*/
class ThePlatform_API {
private $auth;
private $token;
// Plugin preferences option table key
private $preferences_options_key = 'theplatform_preferences_options';
/**
* Class constructor
*/
function __construct() {
$this->preferences = get_option( $this->preferences_options_key );
}
/**
* Construct a Basic Authorization header
*
* @return array
*/
function basicAuthHeader() {
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$encoded = base64_encode( $this->preferences['mpx_username'] . ':' . $this->preferences['mpx_password'] );
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . $encoded
)
);
return $args;
}
/**
* Convert a MIME type to an MPX-compliant format identifier
*
* @param string $mime A MIME-type string
* @return string MPX-compliant format string
*/
function get_format( $mime ) {
$response = ThePlatform_API_HTTP::get( TP_API_FORMATS_XML_URL );
$xmlString = "<?xml version='1.0'?>" . wp_remote_retrieve_body( $response );
$formats = simplexml_load_string( $xmlString );
foreach ( $formats->format as $format ) {
foreach ( $format->mimeTypes->mimeType as $mimetype ) {
if ( $mimetype == $mime ) {
return $format;
}
}
}
return 'Unknown';
}
/**
* Signs into MPX and retrieves an access token.
*
* @return string An access token
*/
function mpx_signin() {
$response = ThePlatform_API_HTTP::get( TP_API_SIGNIN_URL, $this->basicAuthHeader() );
$payload = decode_json_from_server( $response, TRUE );
$this->token = $payload['signInResponse']['token'];
return $this->token;
}
/**
* Deactivates an MPX access token.
*
* @param string $token The token to deactivate
* @return WP_Error|array The response or WP_Error on failure.
*/
function mpx_signout( $token ) {
$response = ThePlatform_API_HTTP::get( TP_API_SIGNOUT_URL . $token );
return $response;
}
/**
* Update a media asset in MPX
*
* @param string $mediaID The ID of the media asset to update
* @param array $payload JSON payload containing field-data pairs to update
* @return string A message indicating whether or not the update succeeded
*/
function update_media( $args ) {
$token = $this->mpx_signin();
$this->create_media_placeholder( $args, $token );
$this->mpx_signout( $token );
}
/**
* Creates a placeholder Media object in MPX.
*
* @param array $args URL arguments to pass to the Media data service
* @param string $token The token for this upload session
* @return string JSON response from the Media data service
*/
function create_media_placeholder( $args, $token ) {
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$fields = json_decode( stripslashes( $args['fields'] ), TRUE );
$custom_fields = json_decode( stripslashes( $args['custom_fields'] ), TRUE );
if ( empty( $fields ) ) {
wp_die( 'No fields are set, unable to upload Media' );
}
$custom_field_ns = array();
$custom_field_values = array();
if ( !empty( $custom_fields ) ) {
$fieldKeys = implode( '|', array_keys( $custom_fields ) );
$customfield_info = $this->get_customfield_info( $fieldKeys );
foreach ( $customfield_info['entries'] as $value ) {
if ( $value['namespacePrefix'] !== '' ) {
$custom_field_ns[$value['namespacePrefix']] = $value['namespace'];
$custom_field_values[$value['namespacePrefix'] . '$' . $value['fieldName']] = $custom_fields[$value['fieldName']];
}
}
}
$payload = array_merge( array(
'$xmlns' => array_merge( array(), $custom_field_ns )
), array_merge( $fields, $custom_field_values )
);
$url = TP_API_MEDIA_ENDPOINT;
$url .= '&account=' . urlencode( $this->preferences['mpx_account_id'] );
$url .= '&token=' . $token;
$response = ThePlatform_API_HTTP::post( $url, json_encode( $payload, JSON_UNESCAPED_SLASHES ), true );
$data = decode_json_from_server( $response, TRUE );
return $data;
}
/**
* Gets custom fields namespaces and prefixes
*
* @param string $fields A pipe separated list of mediafields
* @param string $token The token for this upload session
* @return string Default server returned from the Media Account Settings data service
*/
function get_customfield_info( $fields ) {
$token = $this->mpx_signin();
$url = TP_API_MEDIA_FIELD_ENDPOINT;
$url .= '&fields=namespace,namespacePrefix,fieldName';
$url .= '&byFieldName=' . $fields;
$url .= '&token=' . $token;
$response = ThePlatform_API_HTTP::get( $url );
$this->mpx_signout( $token );
return decode_json_from_server( $response, TRUE );
}
/**
* Get the upload server URLs configured for the current user.
*
* @param string $server_id The current user's default server identifier
* @param string $token The token for this upload session
* @return string A valid upload server URL
*/
function get_upload_urls( $server_id, $token ) {
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$url = TP_API_FMS_GET_UPLOAD_URLS_ENDPOINT;
$url .= '&token=' . urlencode( $token );
$url .= '&account=' . urlencode( $this->preferences['mpx_account_id'] );
$url .= '&_serverId=' . urlencode( $server_id );
$response = ThePlatform_API_HTTP::get( $url );
$data = decode_json_from_server( $response, TRUE );
return $data['getUploadUrlsResponse'][0];
}
/**
* Initialize a media upload session.
*
* @param array $args URL arguments to pass to the Media data service
* @return array An array of parameters for the fragmented uploader service
*/
function initialize_media_upload() {
check_admin_referer( 'theplatform-ajax-nonce' );
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$args = array(
'filesize' => $_POST['filesize'],
'filetype' => $_POST['filetype'],
'filename' => $_POST['filename'],
'fields' => $_POST['fields'],
'profile' => $_POST['profile'],
'custom_fields' => $_POST['custom_fields']
);
$token = $this->mpx_signin();
$response = $this->create_media_placeholder( $args, $token );
$media_guid = $response['guid'];
$media_id = $response['id'];
$format = $this->get_format( $args['filetype'] );
$upload_server_id = $this->preferences['mpx_server_id'];
$upload_server_base_url = $this->get_upload_urls( $upload_server_id, $token );
if ( is_wp_error( $upload_server_base_url ) ) {
return $upload_server_base_url;
}
$params = array(
'token' => $token,
'media_id' => $media_id,
'guid' => $media_guid,
'account_id' => $this->preferences['mpx_account_id'],
'server_id' => $upload_server_id,
'upload_base' => $upload_server_base_url,
'format' => (string) $format->title,
'contentType' => (string) $format->defaultContentType,
'success' => 'true'
);
echo json_encode( $params );
die();
}
/**
* Get the first Streaming Release form MPX based on a Media ID
* @param string $media_id the MPX Media ID
* @return string The Release PID
*/
function get_release_by_id( $media_id ) {
$token = $this->mpx_signin();
$url = TP_API_MEDIA_RELEASE_ENDPOINT . '&fields=pid';
$url .= '&byMediaId=' . $media_id;
$url .= '&token=' . $token;
$response = ThePlatform_API_HTTP::get( $url );
$payload = decode_json_from_server( $response, TRUE );
$releasePID = $payload['entries'][0]['plrelease$pid'];
$this->mpx_signout( $token );
return $releasePID;
}
/**
* Query MPX for videos
*
* @param string $query Query fields to append to the request URL, default empty
* @param string $sort Sort parameters to pass to the data service, default empty
* @param array $fields Optional set of fields to request from the data service, default empty
* @return array The Media data service response
*/
function get_videos() {
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$token = $this->mpx_signin();
$fields = get_query_fields( $this->get_metadata_fields() );
$url = TP_API_MEDIA_ENDPOINT . '&count=true&fields=guid,' . $fields . '&token=' . $token . '&range=' . $_POST['range'];
if ( $_POST['isEmbed'] === "1" ) {
$url .= '&byAvailabilityState=available&byApproved=true&count=true&byContent=byReleases=byDelivery%253Dstreaming';
}
if ( !empty( $_POST['myContent'] ) && $_POST['myContent'] === 'true' ) {
$url .= '&byCustomValue=' . urlencode( '{' . $this->preferences['user_id_customfield'] . '}{' . wp_get_current_user()->ID . '}' );
}
if ( !empty( $this->preferences['mpx_account_id'] ) ) {
$url .= '&account=' . urlencode( $this->preferences['mpx_account_id'] );
} else {
wp_die( '<p>' . __( 'MPX Account is not set, unable to retrieve videos.' ) . '</p>' );
}
if ( !empty( $_POST['query'] ) ) {
$url .= '&' . $_POST['query'];
}
$response = ThePlatform_API_HTTP::get( $url, array( "timeout" => 120 ) );
$this->mpx_signout( $token );
die(wp_remote_retrieve_body( $response ));
}
/**
* Query MPX for a specific video
*
* @param string $id The Media ID associated with the asset we are requesting
* @return array The Media data service response
*/
function get_video_by_id( $id ) {
$token = $this->mpx_signin();
$fields = get_query_fields( $this->get_metadata_fields() );
$url = TP_API_MEDIA_ENDPOINT . '&fields=' . $fields . ' &token=' . $token . '&byId=' . $id;
$response = ThePlatform_API_HTTP::get( $url );
$data = decode_json_from_server( $response, TRUE );
$this->mpx_signout( $token );
return $data['entries'][0];
}
/**
* Query MPX for players
*
* @param array $fields Optional set of fields to request from the data service
* @param array $query Query fields to append to the request URL
* @param array $sort Sort parameters to pass to the data service
* @return array The Player data service response
*/
function get_players( $fields = array(), $query = array(), $sort = array() ) {
$default_fields = array( 'id', 'title', 'plplayer$pid' );
$fields = array_merge( $default_fields, $fields );
$fields = implode( ',', $fields );
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$token = $this->mpx_signin();
$url = TP_API_PLAYER_PLAYER_ENDPOINT . '&sort=title&fields=' . $fields . '&token=' . $token;
if ( !empty( $this->preferences['mpx_account_id'] ) ) {
$url .= '&account=' . urlencode( $this->preferences['mpx_account_id'] );
}
$response = ThePlatform_API_HTTP::get( $url );
$data = decode_json_from_server( $response, TRUE );
$ret = $data['entries'];
$this->mpx_signout( $token );
return $ret;
}
/**
* Query MPX for custom metadata fields
*
* @param array $fields Optional set of fields to request from the data service
* @param array $query Query fields to append to the request URL
* @param array $sort Sort parameters to pass to the data service
* @return array The Media Field data service response
*/
function get_metadata_fields( $fields = array(), $query = array(), $sort = array() ) {
$default_fields = array( 'id', 'title', 'description', 'added', 'allowedValues', 'dataStructure', 'dataType', 'fieldName', 'defaultValue', 'namespace', 'namespacePrefix' );
$fields = array_merge( $default_fields, $fields );
$fields = implode( ',', $fields );
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$token = $this->mpx_signin();
$url = TP_API_MEDIA_FIELD_ENDPOINT . '&fields=' . $fields . '&token=' . $token;
if ( !empty( $this->preferences['mpx_namespace'] ) ) {
$url .= '&byNamespace=' . $this->preferences['mpx_namespace'];
}
if ( !empty( $this->preferences['mpx_account_id'] ) ) {
$url .= '&account=' . urlencode( $this->preferences['mpx_account_id'] );
}
$response = ThePlatform_API_HTTP::get( $url );
$data = decode_json_from_server( $response, TRUE );
$this->mpx_signout( $token );
return $data['entries'];
}
/**
* Query MPX for available servers
*
* @param array $fields Optional set of fields to request from the data service
* @param array $query Query fields to append to the request URL
* @param array $sort Sort parameters to pass to the data service
* @return array The Media data service response
*/
function get_servers( $fields = array(), $query = array(), $sort = array() ) {
$default_fields = array( 'id', 'title', 'description', 'added' );
$fields = array_merge( $default_fields, $fields );
$fields = implode( ',', $fields );
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$token = $this->mpx_signin();
$url = TP_API_MEDIA_SERVER_ENDPOINT . '&fields=' . $fields . '&token=' . $token;
if ( !empty( $this->preferences['mpx_account_id'] ) ) {
$url .= '&account=' . urlencode( $this->preferences['mpx_account_id'] );
}
$response = ThePlatform_API_HTTP::get( $url );
$data = decode_json_from_server( $response, TRUE );
$this->mpx_signout( $token );
return $data['entries'];
}
/**
* Returns the account setting objects, this is actually used to test our connection
* @return array AccountSettings response
*/
function get_account_settings( ) {
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$token = $this->mpx_signin();
$url = TP_API_MEDIA_ACCOUNTSETTINGS_ENDPOINT . '&token=' . $token;
if ( !empty( $this->preferences['mpx_account_id'] ) ) {
$url .= '&account=' . urlencode( $this->preferences['mpx_account_id'] );
}
$response = ThePlatform_API_HTTP::get( $url );
$data = decode_json_from_server( $response, TRUE, FALSE );
$this->mpx_signout( $token );
return $data;
}
/**
* Query MPX for account categories
*
* @param array $query Query fields to append to the request URL
* @param array $sort Sort parameters to pass to the data service
* @param array $fields Optional set of fields to request from the data service
* @return array The Media data service response
*/
function get_categories( $returnResponse = false ) {
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$token = $this->mpx_signin();
$url = TP_API_MEDIA_CATEGORY_ENDPOINT . '&fields=title,fullTitle&sort=title,order&token=' . $token;
if ( !empty( $this->preferences['mpx_account_id'] ) ) {
$url .= '&account=' . urlencode( $this->preferences['mpx_account_id'] );
}
$response = ThePlatform_API_HTTP::get( $url );
$this->mpx_signout( $token );
if ( !$returnResponse ) {
echo(wp_remote_retrieve_body( $response ));
die();
}
$data = decode_json_from_server( $response, TRUE );
return $data['entries'];
}
/**
* Query MPX for subaccounts associated with the configured account
*
* @param array $fields Optional set of fields to request from the data service
* @param array $query Query fields to append to the request URL
* @param array $sort Sort parameters to pass to the data service
* @return array The Media data service response
*/
function get_subaccounts( $fields = array(), $query = array(), $sort = array() ) {
$default_fields = array( 'id', 'title', 'description', 'placcount$pid' );
$fields = array_merge( $default_fields, $fields );
$fields = implode( ',', $fields );
$token = $this->mpx_signin();
$url = TP_API_ACCESS_AUTH_ENDPOINT . '&_operations[0].service=Media%20Data%20Service&_operations[0].method=GET&_operations[0].endpoint=Media&token=' . $token . '&sort=title&range=1-1000';
$response = ThePlatform_API_HTTP::get( $url );
$data = decode_json_from_server( $response, TRUE );
$this->mpx_signout( $token );
return $data['authorizeResponse']['accounts'];
}
/**
* Query MPX for Publishing Profiles associated with the configured account
*
* @param array $fields Optional set of fields to request from the data service
* @param array $query Query fields to append to the request URL
* @param array $sort Sort parameters to pass to the data service
* @return array The Media data service response
*/
function get_publish_profiles( $fields = array(), $query = array(), $sort = array() ) {
$default_fields = array( 'id', 'title' );
$fields = array_merge( $default_fields, $fields );
$fields = implode( ',', $fields );
$token = $this->mpx_signin();
$url = TP_API_PUBLISH_PROFILE_ENDPOINT . '&fields=' . $fields . '&token=' . $token . '&sort=title';
if ( !empty( $this->preferences['mpx_account_id'] ) ) {
$url .= '&account=' . urlencode( $this->preferences['mpx_account_id'] );
}
$response = ThePlatform_API_HTTP::get( $url );
$data = decode_json_from_server( $response, TRUE );
$this->mpx_signout( $token );
return $data['entries'];
}
/**
* Used to verify the account server settings on the server side
* @return type
*/
function internal_verify_account_settings() {
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
$username = trim( $this->preferences['mpx_username'] );
$password = trim( $this->preferences['mpx_password'] );
if ( $username === "mpx/" || $username === "" || $password === "" ) {
return FALSE;
}
$hash = base64_encode( $username . ':' . $password );
$response = ThePlatform_API_HTTP::get( TP_API_SIGNIN_URL, array( 'headers' => array( 'Authorization' => 'Basic ' . $hash ) ) );
$payload = decode_json_from_server( $response, TRUE, FALSE );
if ( is_null( $response ) ) {
return FALSE;
}
if ( !array_key_exists( 'isException', $payload ) ) {
return TRUE;
} else {
return FALSE;
}
}
/**
* Verify that the account you've selected is within the region you've selected
* @return bool account is within the same region
*/
function internal_verify_account_region() {
if ( !$this->preferences ) {
$this->preferences = get_option( $this->preferences_options_key );
}
if ( $this->preferences['mpx_account_id'] === '' ) {
return FALSE;
}
$response = $this->get_account_settings();
if ( is_null( $response ) && !is_array( $response ) ) {
return FALSE;
}
if ( !array_key_exists( 'isException', $response ) ) {
return TRUE;
} else {
return FALSE;
}
}
}
?>