-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflickrsilo.plugin.php
1122 lines (1014 loc) · 33.3 KB
/
flickrsilo.plugin.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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Habari;
if ( !defined( 'HABARI_PATH' ) ) { die( 'No direct access' ); }
class flickrAPI
{
function __construct()
{
$this->key = 'cd0ae46b1332aa2bd52ba3063f0db41c';
$this->secret = '76cf747f70be9029';
$this->endpoint = 'https://www.flickr.com/services/rest/?';
$this->authendpoint = 'https://www.flickr.com/services/auth/?';
$this->uploadendpoint = 'https://api.flickr.com/services/upload/?';
$this->conntimeout = 20;
}
public function set_timeout( $timeout )
{
$this->conntimeout = $timeout;
}
public function sign( $args )
{
ksort( $args );
unset( $args['photo'] );
$a = '';
foreach( $args as $key => $value ){
$a .= $key . $value;
}
return md5( $this->secret . $a );
}
public function encode( $args )
{
$encoded = array();
foreach ( $args as $key => $value ){
$encoded[] = urlencode( $key ) . '=' . urlencode( $value );
}
return $encoded;
}
function call( $method, $args = array () )
{
$args = array_merge( array ( 'method' => $method,
'api_key' => $this->key ), $args );
ksort( $args );
$args = array_merge( $args, array ( 'api_sig' => $this->sign( $args ) ) );
ksort( $args );
$call = new RemoteRequest( $this->endpoint, 'POST' );
$args['api_key'] = $this->key;
if ( $method == 'upload' ){
$call = new RemoteRequest( $this->uploadendpoint, 'POST' );
if ( is_file( $args['photo'] ) ){
// we have a valid file and filename
$call->set_file( 'photo', $args['photo'] );
unset( $args['photo'] );
}
}
$call->set_timeout( $this->conntimeout );
$call->set_postdata( $args );
try {
$result = $call->execute();
}
catch ( RemoteRequest_Timeout $t ) {
Session::error( 'Currently unable to connect to Flickr.', 'flickr API' );
return false;
}
catch ( \Exception $e ) {
// at the moment we're using the same error message, though this is more catastrophic
Session::error( 'Currently unable to connect to Flickr.', 'flickr API' );
return false;
}
$response = $call->get_response_body();
try{
$xml = new \SimpleXMLElement( $response );
return $xml;
}
catch( \Exception $e ) {
Session::error( 'Unable to process Flickr response.', 'flickr API' );
return false;
}
}
}
class Flickr extends flickrAPI
{
private $user_id;
function __construct( $params = array() )
{
parent::__construct( $params );
$this->user_id = ( isset( $params['user_id'] ) ) ? $params['user_id'] : User::identify()->id;
}
/**
* get a URL for a photo
* @param mixed $photo object, array, or integer
* object: a SimpleXMLElement as returned by photosGetInfo()
* array: the 'photo' element of the XML from photosGetInfo
* int: a Flickr photo ID
* @param string $size the size of the photo to fetch
* @return string the URL of the requested photo
*/
function getPhotoURL( $photo, $size = '' )
{
if ( is_int( $photo ) ) {
$photo = $this->photosGetInfo( $photo );
}
if ( $photo instanceof SimpleXMLElement ) {
$p = (array) $photo->photo->attributes();
$photo = $p['@attributes'];
}
$markup = '//farm';
$markup .= $photo['farm'];
$markup .= '.staticflickr.com/';
$markup .= $photo['server'];
$markup .= '/';
$markup .= $photo['id'];
$markup .= '_';
$markup .= $photo['secret'];
$markup .= $size;
$markup .= '.jpg';
return $markup;
}
// authentication and approval
public function getFrob()
{
$xml = $this->call( 'flickr.auth.getFrob', array() );
return $xml->frob;
}
public function authLink( $frob )
{
$params['api_key'] = $this->key;
$params['frob'] = $frob;
$params['perms'] = 'write';
$params['api_sig'] = md5( $this->secret . 'api_key' . $params['api_key'] . 'frob' . $params['frob'] . 'permswrite' );
$link = $this->authendpoint . implode( '&', $this->encode( $params ) );
return $link;
}
function getToken( $frob )
{
$xml = $this->call( 'flickr.auth.getToken', array( 'frob' => $frob ) );
return $xml;
}
// grab the token from our db.
function cachedToken()
{
$token = Options::get( 'flickr_token_' . $this->user_id );
return $token;
}
// get photo sizes
function getSizes( $photo_id )
{
$params = array( 'api_key' => $this->key,
'photo_id' => $photo_id );
$xml = $this->call( 'flickr.photos.getSizes', $params );
return $xml;
}
// get publicly available photos
function getPublicPhotos( $nsid, $extras = '', $per_page = '', $page = '' )
{
$params = array( 'user_id' => $nsid );
if ( $extras ){
$params['extras'] = $extras;
}
if ( $per_page ){
$params['per_page'] = $per_page;
}
if ( $page ){
$params['page'] = $page;
}
$xml = $this->call( 'flickr.people.getPublicPhotos' , $params );
foreach( $xml->photos->attributes() as $key => $value ){
$pic[$key] = (string)$value;
}
$i = 0;
foreach( $xml->photos->photo as $photo ){
foreach( $photo->attributes() as $key => $value ){
$pic['photos'][(string)$photo['id']][$key] = (string)$value;
}
$i++;
}
return $pic;
}
// Photosets methods
function photosetsGetList( $nsid = '' )
{
$params = array();
if ( $nsid ){
$params['user_id'] = $nsid;
}
$xml = $this->call( 'flickr.photosets.getList', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
return $xml;
}
function photosetsGetInfo( $photoset_id )
{
$params = array( 'photoset_id' => $photoset_id );
$xml = $this->call( 'flickr.photosets.getInfo', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
return $xml;
}
function photosetGetPrimary( $p, $size = 'm', $ext = '.jpg' )
{
return '//static.flickr.com/' . $p['server'] . '/' . $p['primary'] . '_' . $p['secret'] . '_' . $size . $ext;
}
function photosetsGetPhotos( $photoset_id )
{
$params = array( 'photoset_id' => $photoset_id );
$xml = $this->call( 'flickr.photosets.getPhotos', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
return $xml;
}
function photosetsAddPhoto( $photoset_id, $photo_id )
{
$params = array( 'photoset_id' => $photoset_id,
'photo_id' => $photo_id );
return $this->call( 'flickr.photosets.addPhoto', $params );
}
function photosRecentlyUpdated()
{
$params = array();
if ( $this->cachedToken() ){
$params['auth_token'] = $this->cachedToken();
}
$params['secret'] = $this->secret;
$params['min_date'] = time() - 31536000; // Within the last year
$params['per_page'] = 10;
$xml = $this->call( 'flickr.photos.recentlyUpdated', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
return $xml;
}
function mediaSearch( $params = array() )
{
if ( $this->cachedToken() ){
$params['auth_token'] = $this->cachedToken();
}
$params['secret'] = $this->secret;
$params['user_id'] = 'me';
$params['sort'] = 'date-posted-desc';
$params['per_page'] = 20;
$xml = $this->call( 'flickr.photos.search', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
return $xml;
}
function photosSearch( $params = array() )
{
if ( $this->cachedToken() ){
$params['auth_token'] = $this->cachedToken();
}
$defaults = array(
'secret' => $this->secret,
'user_id' => 'me',
'sort' => 'date-posted-desc',
'per_page' => 20,
'media' => 'photos',
'extras' => 'original_format',
);
$params = array_merge( $defaults, $params );
$xml = $this->call( 'flickr.photos.search', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
return $xml;
}
function videoSearch( $params = array() )
{
if ( $this->cachedToken() ){
$params['auth_token'] = $this->cachedToken();
}
$params['secret'] = $this->secret;
$params['user_id'] = 'me';
$params['sort'] = 'date-posted-desc';
$params['per_page'] = 20;
$params['media'] = 'videos';
$xml = $this->call( 'flickr.photos.search', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
return $xml;
}
function tagsGetListUser( $userid = null )
{
$params = array();
if ( isset( $userid ) ) {
$params['user_id'] = $userid;
}
$xml = $this->call( 'flickr.tags.getListUser', $params );
return $xml;
}
function photosGetInfo( $photo_id )
{
$params = array();
if ( $this->cachedToken() ){
$params['auth_token'] = $this->cachedToken();
}
$params['photo_id'] = $photo_id;
$params['secret'] = $this->secret;
$xml = $this->call( 'flickr.photos.getInfo', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
return $xml;
}
/**
* upload an image to Flickr
* @param mixed path to the file to upload, or the raw data itself
* @param String title of the file
* @param String description of the file
* @param String a comma-separated list of tags
* @param Array an array of permissions to apply
* @param Boolean whether to perform an asynchronous upload or not
* @return String a Flickr photo ID or a ticket ID
**/
function upload( $photo, $title = '', $description = '', $tags = '', $perms = '', $async = 1 )
{
$params = array( 'auth_token' => $this->cachedToken() );
$params['photo'] = $photo;
if ( $title ){
$params['title'] = $title;
} elseif( is_file( $photo ) ) {
$params['title'] = basename( $photo );
} else {
$params['title'] = date( 'Y-m-d' );
}
if ( $description ){
$params['description'] = $description;
}
if ( $tags ){
$params['tags'] = $tags;
}
if ( $perms ){
if ( isset( $perms['is_public'] ) ){
$params['is_public'] = $perms['is_public'];
}
if ( isset( $perms['is_friend'] ) ){
$params['is_friend'] = $perms['is_friend'];
}
if ( isset( $perms['is_family'] ) ){
$params['is_family'] = $perms['is_family'];
}
}
if ( $async ){
$params['async'] = $async;
}
// call the upload method.
$xml = $this->call( 'upload', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
if ( $async ){
return( (string)$xml->ticketid );
}
else{
return( (string)$xml->photoid );
}
}
function photosUploadCheckTickets( $tickets )
{
$params = array();
if ( is_array( $tickets ) ){
foreach( $tickets as $key => $value ){
if ( $key ){
$params['tickets'] .= ' ';
}
$params['tickets'] .= $value;
}
}
else{
$params['tickets'] = $tickets;
}
$xml = $this->call( 'flickr.photos.upload.checkTickets', $params );
if ( Error::is_error( $xml ) ){
throw $xml;
}
foreach( $xml->uploader->ticket as $ticket ){
foreach( $ticket->attributes() as $key => $value ){
$uptick[(string)$ticket['id']][$key] = (string)$value;
}
}
return $uptick;
}
function reflectionGetMethods()
{
$params = array();
$xml = $this->call( 'flickr.reflection.getMethods', $params );
if ( !$xml ){
return false;
}
$ret = (array)$xml->methods->method;
return $ret;
}
}
/**
* Flickr Silo
*/
class FlickrSilo extends Plugin implements MediaSilo
{
const SILO_NAME = 'Flickr';
static $cache = array();
private $params;
/**
* Initialize some internal values when plugin initializes
*/
public function action_init()
{
$this->params = array();
}
public function set_user( $user_id )
{
$this->params['user_id'] = $user_id;
}
/**
* Return basic information about this silo
* name- The name of the silo, used as the root directory for media in this silo
* icon- An icon to represent the 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 )
{
$flickr = new Flickr( $this->params );
$results = array();
$size = Options::get( 'flickrsilo__flickr_size' );
$section = strtok( $path, '/' );
switch ( $section ) {
case 'attrib-sa':
$xml = $flickr->photosSearch( array( 'user_id' => '', 'license' => '4,5', 'text'=>$_SESSION['flickrsearch'] ) );
foreach( $xml->photos->photo as $photo ) {
$props = array();
foreach( $photo->attributes() as $name => $value ) {
$props[$name] = (string)$value;
}
$props = array_merge( $props, self::element_props( $photo, "http://www.flickr.com/photos/{$photo['owner']}/{$photo['id']}", $size ) );
$results[] = new MediaAsset(
self::SILO_NAME . '/photos/' . $photo['id'],
false,
$props
);
}
break;
case 'search':
$xml = $flickr->photosSearch( array( 'text'=>$_SESSION['flickrsearch'] ) );
foreach( $xml->photos->photo as $photo ) {
$props = array();
foreach( $photo->attributes() as $name => $value ) {
$props[$name] = (string)$value;
}
$props = array_merge( $props, self::element_props( $photo, "http://www.flickr.com/photos/{$_SESSION['nsid']}/{$photo['id']}", $size ) );
$results[] = new MediaAsset(
self::SILO_NAME . '/photos/' . $photo['id'],
false,
$props
);
}
break;
case 'photos':
$xml = $flickr->photosSearch();
foreach( $xml->photos->photo as $photo ) {
$props = array();
foreach( $photo->attributes() as $name => $value ) {
$props[$name] = (string)$value;
}
$props = array_merge( $props, self::element_props( $photo, "http://www.flickr.com/photos/{$_SESSION['nsid']}/{$photo['id']}", $size ) );
$results[] = new MediaAsset(
self::SILO_NAME . '/photos/' . $photo['id'],
false,
$props
);
}
break;
case 'videos':
$xml = $flickr->videoSearch();
foreach( $xml->photos->photo as $photo ) {
$props = array();
foreach( $photo->attributes() as $name => $value ) {
$props[$name] = (string)$value;
}
$props = array_merge( $props, self::element_props( $photo, "http://www.flickr.com/photos/{$_SESSION['nsid']}/{$photo['id']}", $size ) );
$props['filetype'] = 'flickrvideo';
$results[] = new MediaAsset(
self::SILO_NAME . '/photos/' . $photo['id'],
false,
$props
);
}
break;
case 'tags':
$selected_tag = strtok('/');
if ( $selected_tag ) {
$xml = $flickr->photosSearch( array( 'tags'=>$selected_tag ) );
foreach( $xml->photos->photo as $photo ) {
$props = array();
foreach( $photo->attributes() as $name => $value ) {
$props[$name] = (string)$value;
}
$props = array_merge( $props, self::element_props( $photo, "http://www.flickr.com/photos/{$_SESSION['nsid']}/{$photo['id']}", $size ) );
$results[] = new MediaAsset(
self::SILO_NAME . '/photos/' . $photo['id'],
false,
$props
);
}
}
else {
$xml = $flickr->tagsGetListUser( $_SESSION['nsid'] );
foreach( $xml->who->tags->tag as $tag ) {
$results[] = new MediaAsset(
self::SILO_NAME . '/tags/' . (string)$tag,
true,
array( 'title' => (string)$tag )
);
}
}
break;
case 'sets':
$selected_set = strtok('/');
if ( $selected_set ) {
$xml = $flickr->photosetsGetPhotos( $selected_set );
foreach( $xml->photoset->photo as $photo ) {
$props = array();
foreach( $photo->attributes() as $name => $value ) {
$props[$name] = (string)$value;
}
$props = array_merge( $props, self::element_props( $photo, "http://www.flickr.com/photos/{$_SESSION['nsid']}/{$photo['id']}", $size ) );
$results[] = new MediaAsset(
self::SILO_NAME . '/photos/' . $photo['id'],
false,
$props
);
}
}
else {
$xml = $flickr->photosetsGetList( $_SESSION['nsid'] );
foreach( $xml->photosets->photoset as $set ) {
$results[] = new MediaAsset(
self::SILO_NAME . '/sets/' . (string)$set['id'],
true,
array( 'title' => (string)$set->title )
);
}
}
break;
case '$search':
$path = strtok( '/' );
$dosearch = Utils::slugify( $path );
$_SESSION['flickrsearch'] = $path;
$section = $path;
case '':
if ( isset( $_SESSION['flickrsearch'] ) ) {
$results[] = new MediaAsset(
self::SILO_NAME . '/search',
true,
array( 'title' => _t( 'Search' ) )
);
$results[] = new MediaAsset(
self::SILO_NAME . '/attrib-sa',
true,
array( 'title' => _t( 'Search CC' ) )
);
}
$results[] = new MediaAsset(
self::SILO_NAME . '/photos',
true,
array('title' => _t( 'Photos' ) )
);
$results[] = new MediaAsset(
self::SILO_NAME . '/videos',
true,
array('title' => _t( 'Videos' ) )
);
$results[] = new MediaAsset(
self::SILO_NAME . '/tags',
true,
array('title' => _t( 'Tags' ) )
);
$results[] = new MediaAsset(
self::SILO_NAME . '/sets',
true,
array('title' => _t( 'Sets' ) )
);
break;
}
return $results;
}
/**
* Function that populates the element properties for use in the silo.
*
* This is to reduce the amount of duplicate code.
*
* @param array $photo The photo element array
* @param string $url The Flickr URL to link to
* @param string $size The size of the image to display.
* @return array
*/
private static function element_props( $photo, $url, $size )
{
$props = array();
$props['title'] = ( $photo['title'] != '' ) ? (string)$photo['title'] : (string)$photo['id'];
$props['url'] = "//farm{$photo['farm']}.static.flickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}{$size}.jpg";
$props['thumbnail_url'] = "//farm{$photo['farm']}.static.flickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}_m.jpg";
$props['flickr_url'] = $url;
$props['filetype'] = 'flickr';
return $props;
}
/**
* Get the file from the specified path
*
* @param string $path The path of the file to retrieve
* @param array $qualities Qualities that specify the version of the file to retrieve.
* @return MediaAsset The requested asset
*/
public function silo_get( $path, $qualities = null )
{
$flickr = new Flickr( $this->params );
$results = array();
$size = Options::get( 'flickrsilo__flickr_size' );
list($unused, $photoid) = explode( '/', $path );
$xml = $flickr->photosGetInfo($photoid);
$photo = $xml->photo;
$props = array();
foreach( $photo->attributes() as $name => $value ) {
$props[$name] = (string)$value;
}
$props = array_merge( $props, self::element_props( $photo, (string) $xml->photo->urls[0]->url[0], $size ) );
$asset = new MediaAsset(
self::SILO_NAME . '/photos/' . $photo['id'],
false,
$props
);
// now get the actual image data
$url = $flickr->getPhotoURL( $xml );
$call = new RemoteRequest( $url );
try {
$result = $call->execute();
}
catch ( RemoteRequest_Timeout $t ) {
Session::error( 'Currently unable to connect to Flickr.', 'flickr API' );
return false;
}
catch ( \Exception $e ) {
// at the moment we're using the same error message, though this is more catastrophic
Session::error( 'Currently unable to connect to Flickr.', 'flickr API' );
return false;
}
if ( $result ) {
$asset->content = $result;
}
return $asset;
}
/**
* Get the direct URL of the file of the specified path
*
* @param string $path The path of the file to retrieve
* @param array $qualities Qualities that specify the version of the file to retrieve.
* @return string The requested url
*/
public function silo_url( $path, $qualities = null )
{
$photo = false;
if ( preg_match( '%^photos/(.+)$%', $path, $matches ) ) {
$id = $matches[1];
$photo = self::$cache[$id];
}
$size = '';
if ( isset( $qualities['size'] ) && $qualities['size'] == 'thumbnail' ) {
$size = '_m';
}
$url = "//farm{$photo['farm']}.static.flickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}{$size}.jpg";
return $url;
}
/**
* Create a new asset instance for the specified path
*
* @param string $path The path of the new file to create
* @return MediaAsset The requested asset
*/
public function silo_new( $path )
{
}
/**
* Store the specified media at the specified path
*
* @param string $path The path of the file to upload
* @param MediaAsset $filedata The asset to upload
*/
public function silo_put( $path, $filedata )
{
$flickr = new Flickr( $this->params );
return $flickr->upload( $path, $filedata['title'], $filedata['description'], $filedata['tags'], $filedata['perms'], 0 );
}
/**
* Delete the file at the specified path
*
* @param string $path The path of the file to retrieve
*/
public function silo_delete( $path )
{
}
/**
* Retrieve a set of highlights from this silo
* This would include things like recently uploaded assets, or top downloads
*
* @return array An array of MediaAssets to highlihgt from this silo
*/
public function silo_highlights()
{
}
/**
* Retrieve the permissions for the current user to access the specified path
*
* @param string $path The path to retrieve permissions for
* @return array An array of permissions constants (MediaSilo::PERM_READ, MediaSilo::PERM_WRITE)
*/
public function silo_permissions( $path )
{
}
/**
* 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_contents()
{
$flickr = new Flickr( $this->params );
$token = Options::get( 'flickr_token_' . User::identify()->id );
$result = $flickr->call( 'flickr.auth.checkToken',
array( 'api_key' => $flickr->key,
'auth_token' => $token ) );
$photos = $flickr->GetPublicPhotos( $result->auth->user['nsid'], null, 5 );
foreach( $photos['photos'] as $photo ){
$url = $flickr->getPhotoURL( $photo );
echo '<img src="' . $url . '" width="150px" alt="' . ( isset( $photo['title'] ) ? $photo['title'] : _t('This photo has no title') ) . '">';
}
}
/**
* Add actions to the plugin page for this plugin
* The authorization should probably be done per-user.
*
* @param array $actions An array of actions that apply to this plugin
* @param string $plugin_id The string id of a plugin, generated by the system
* @return array The array of actions to attach to the specified $plugin_id
*/
public function filter_plugin_config( $actions, $plugin_id )
{
$flickr_ok = $this->is_auth();
if ( $flickr_ok ){
$actions['deauthorize'] = _t( 'De-Authorize' );
}
else{
$actions['authorize'] = _t( 'Authorize' );
}
$actions['configure'] = _t( 'Configure' );
return $actions;
}
/**
* Respond to the user selecting the authorize action on the plugin page
*
*/
public function action_plugin_ui_authorize()
{
if ( $this->is_auth() ){
$deauth_url = URL::get( 'admin', array( 'page' => 'plugins', 'configure' => $this->plugin_id(), 'configaction' => 'deauthorize' ) ) . '#plugin_options';
echo '<p>' . _t( 'You have already successfully authorized Habari to access your Flickr account.') . '</p>';
echo '<p>' . _t( 'Do you want to <a href="%1$s">revoke authorization</a>?', array( $deauth_url ) ) . '</p>';
}
else{
$flickr = new Flickr( $this->params );
$_SESSION['flickr_frob'] = '' . $flickr->getFrob();
$auth_url = $flickr->authLink( $_SESSION['flickr_frob'] );
$confirm_url = URL::get( 'display_plugins', array( 'page' => 'plugins', 'configure' => $this->plugin_id, 'action' => 'confirm') );
echo '<p>' . _t( 'To use this plugin, you must <a target="_blank" href="%1$s">authorize Habari to access your Flickr account</a>.', array( $auth_url ) ) . '</p>';
echo '<p>' . _t( 'When you have completed the authorization on Flickr, return here and <a href="%1$s">confirm that the authorization was successful</a>.', array( $confirm_url ) ) . '</p>';
}
}
/**
* Respond to the user selecting the confirm action
*
*/
public function action_plugin_ui_confirm()
{
$flickr = new Flickr( $this->params );
if ( !isset( $_SESSION['flickr_frob'] ) ){
$auth_url = URL::get( 'admin', array( 'page' => 'plugins', 'configure' => $this->plugin_id(), 'configaction' => 'authorize' ) ) . '#plugin_options';
echo '<p>' . _t( 'Data submission was unsuccessful and it could not be determined if you have already authorized Habari to access your flickr account, or you have not yet done so. Please <a href="%1$s">try again</a>.', array( $auth_url ) ) . '</p>';
}
else{
$token = $flickr->getToken( $_SESSION['flickr_frob'] );
if ( isset( $token->auth->perms ) ){
Options::set( 'flickr_token_' . User::identify()->id, '' . $token->auth->token );
echo '<p>' . _t( 'Your authorization was set successfully.' ) . '</p>';
}
else{
echo '<p>' . _t( 'There was a problem with your authorization:' ) . '</p>';
echo Utils::htmlspecialchars( $token->asXML() );
}
unset( $_SESSION['flickr_frob'] );
}
}
/**
* Respond to the user selecting the deauthorize action
*
*/
public function action_plugin_ui_deauthorize()
{
Options::set( 'flickr_token_' . User::identify()->id );
$reauth_url = URL::get( 'admin', array( 'page' => 'plugins', 'configure' => $this->plugin_id(), 'configaction' => 'authorize' ) ) . '#plugin_options';
echo '<p>' . _t( 'The Flickr Silo Plugin authorization has been deleted.' ) . '<p>';
echo '<p>' . _t( 'Do you want to ' ) . "<a href=\"{$reauth_url}\">" . _t( 're-authorize this plugin' ) . "</a>?<p>";
}
/**
* Respond to the user selecting the configure action
*
*/
public function action_plugin_ui_configure()
{
$ui = new FormUI( strtolower( get_class( $this ) ) );
$ui->append( FormControlLabel::wrap( _t( 'Default size for images in Posts:' ),
FormControlSelect::create( 'flickr_size', 'option:flickrsilo__flickr_size' )->set_options( array(
'_s' => _t( 'Square' ) . ' (75x75)',
'_t' => _t( 'Thumbnail' ) . ' (100px)',
'_m' => _t( 'Small' ) . ' (240px)',
'' => _t( 'Medium' ) . ' (500px)',
'_b' => _t( 'Large') . ' (1024px)',
'_o' => _t( 'Original Size' )
))
));
$ui->append( FormControlSubmit::create( 'save') )->set_caption( _t( 'Save' ) );
$ui->set_settings(array('success_message' => _t( 'Options saved' )));
$ui->out();
}
public function action_admin_footer( $theme )
{
if ( Controller::get_var( 'page' ) == 'publish' ) {
$size = Options::get( 'flickrsilo__flickr_size' );
switch ( $size ) {
case '_s':
$vsizex = 75;
break;
case '_t':
$vsizex = 100;
break;
case '_m':
$vsizex = 240;
break;
case '':
$vsizex = 500;
break;
case '_b':
$vsizex = 1024;
break;
case '_o':
$vsizex = 400;
break;
}
$vsizey = intval( $vsizex/4*3 );
// Translation strings for used in embedding Javascript. This is quite messy, but it leads to cleaner code than doing it inline.
$embed_photo = _t( 'embed_photo' );
$embed_video = _t( 'embed_video' );
$thumbnail = _t( 'thumbnail' );
$title = _t( 'Open in new window' );
echo <<< FLICKR
<script type="text/javascript">
if(habari.media.output.flickr === undefined) {
habari.media.output.flickr = {};
}
if(habari.media.output.flickrvideo === undefined) {
habari.media.output.flickrvideo = {};
}
$.extend(habari.media.output.flickr, {
{$embed_photo}: function(fileindex, fileobj) {
habari.editor.insertSelection('<figure><img alt="' + fileobj.title + '" src="' + fileobj.url + '"><figcaption><a href="' + fileobj.flickr_url + '">' + fileobj.title + '</a></figcaption></figure>');
}
});
$.extend(habari.media.output.flickrvideo, {
{$embed_video}: function(fileindex, fileobj) {
habari.editor.insertSelection('<object type="application/x-shockwave-flash" width="{$vsizex}" height="{$vsizey}" data="//www.flickr.com/apps/video/stewart.swf?v=49235" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="flashvars" value="intl_lang=en-us&photo_secret=' + fileobj.secret + '&photo_id=' + fileobj.id + '&show_info_box=true"></param> <param name="movie" value="//www.flickr.com/apps/video/stewart.swf?v=49235"></param> <param name="bgcolor" value="#000000"></param> <param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="//www.flickr.com/apps/video/stewart.swf?v=49235" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&photo_secret=' + fileobj.secret + '&photo_id=' + fileobj.id + '&flickr_show_info_box=true" height="{$vsizey}" width="{$vsizex}"></embed></object>');
},
{$thumbnail}: function(fileindex, fileobj) {
habari.editor.insertSelection('<a href="' + fileobj.flickr_url + '"><img alt="' + fileobj.title + '" src="' + fileobj.url + '"></a>');
}
});