forked from dan-coulter/phpflickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpFlickr.php
1822 lines (1562 loc) · 83.2 KB
/
phpFlickr.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
/* phpFlickr
* Written by Dan Coulter (dan@dancoulter.com)
* Project Home Page: http://github.com/dancoulter/phpflickr
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
if ( !class_exists('phpFlickr') ) {
if (session_id() == "") {
@session_start();
}
class phpFlickr {
var $api_key;
var $secret;
var $rest_endpoint = 'https://api.flickr.com/services/rest/';
var $upload_endpoint = 'https://up.flickr.com/services/upload/';
var $replace_endpoint = 'https://up.flickr.com/services/replace/';
var $req;
var $response;
var $parsed_response;
var $cache = false;
var $cache_db = null;
var $cache_table = null;
var $cache_dir = null;
var $cache_expire = null;
var $cache_key = null;
var $last_request = null;
var $die_on_error;
var $error_code;
Var $error_msg;
var $token;
var $php_version;
var $custom_post = null, $custom_cache_get = null, $custom_cache_set = null;
/*
* When your database cache table hits this many rows, a cleanup
* will occur to get rid of all of the old rows and cleanup the
* garbage in the table. For most personal apps, 1000 rows should
* be more than enough. If your site gets hit by a lot of traffic
* or you have a lot of disk space to spare, bump this number up.
* You should try to set it high enough that the cleanup only
* happens every once in a while, so this will depend on the growth
* of your table.
*/
var $max_cache_rows = 1000;
function phpFlickr ($api_key, $secret = NULL, $die_on_error = false) {
//The API Key must be set before any calls can be made. You can
//get your own at https://www.flickr.com/services/api/misc.api_keys.html
$this->api_key = $api_key;
$this->secret = $secret;
$this->die_on_error = $die_on_error;
$this->service = "flickr";
//Find the PHP version and store it for future reference
$this->php_version = explode("-", phpversion());
$this->php_version = explode(".", $this->php_version[0]);
}
function enableCache ($type, $connection, $cache_expire = 600, $table = 'flickr_cache') {
// Turns on caching. $type must be either "db" (for database caching) or "fs" (for filesystem).
// When using db, $connection must be a PEAR::DB connection string. Example:
// "mysql://user:password@server/database"
// If the $table, doesn't exist, it will attempt to create it.
// When using file system, caching, the $connection is the folder that the web server has write
// access to. Use absolute paths for best results. Relative paths may have unexpected behavior
// when you include this. They'll usually work, you'll just want to test them.
if ($type == 'db') {
if ( preg_match('|mysql://([^:]*):([^@]*)@([^/]*)/(.*)|', $connection, $matches) ) {
//Array ( [0] => mysql://user:password@server/database [1] => user [2] => password [3] => server [4] => database )
$db = mysqli_connect($matches[3], $matches[1], $matches[2]);
mysqli_query($db, "USE $matches[4]");
/*
* If high performance is crucial, you can easily comment
* out this query once you've created your database table.
*/
mysqli_query($db, "
CREATE TABLE IF NOT EXISTS `$table` (
`request` varchar(128) NOT NULL,
`response` mediumtext NOT NULL,
`expiration` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY `request` (`request`)
)
");
$result = mysqli_query($db, "SELECT COUNT(*) 'count' FROM $table");
if( $result ) {
$result = mysqli_fetch_assoc($result);
}
if ( $result && $result['count'] > $this->max_cache_rows ) {
mysqli_query($db, "DELETE FROM $table WHERE CURRENT_TIMESTAMP > expiration");
mysqli_query($db, 'OPTIMIZE TABLE ' . $this->cache_table);
}
$this->cache = 'db';
$this->cache_db = $db;
$this->cache_table = $table;
}
} elseif ($type == 'fs') {
$this->cache = 'fs';
$connection = realpath($connection);
$this->cache_dir = $connection;
if ($dir = opendir($this->cache_dir)) {
while ($file = readdir($dir)) {
if (substr($file, -6) == '.cache' && ((filemtime($this->cache_dir . '/' . $file) + $cache_expire) < time()) ) {
unlink($this->cache_dir . '/' . $file);
}
}
}
} elseif ( $type == 'custom' ) {
$this->cache = "custom";
$this->custom_cache_get = $connection[0];
$this->custom_cache_set = $connection[1];
}
$this->cache_expire = $cache_expire;
}
function getCached ($request)
{
//Checks the database or filesystem for a cached result to the request.
//If there is no cache result, it returns a value of false. If it finds one,
//it returns the unparsed XML.
unset($request['api_sig']);
foreach ( $request as $key => $value ) {
if ( empty($value) ) unset($request[$key]);
else $request[$key] = (string) $request[$key];
}
//if ( is_user_logged_in() ) print_r($request);
$reqhash = md5(serialize($request));
$this->cache_key = $reqhash;
$this->cache_request = $request;
if ($this->cache == 'db') {
$result = mysqli_query($this->cache_db, "SELECT response FROM " . $this->cache_table . " WHERE request = '" . $reqhash . "' AND CURRENT_TIMESTAMP < expiration");
if ( $result && mysqli_num_rows($result) ) {
$result = mysqli_fetch_assoc($result);
return urldecode($result['response']);
} else {
return false;
}
} elseif ($this->cache == 'fs') {
$file = $this->cache_dir . '/' . $reqhash . '.cache';
if (file_exists($file)) {
if ($this->php_version[0] > 4 || ($this->php_version[0] == 4 && $this->php_version[1] >= 3)) {
return file_get_contents($file);
} else {
return implode('', file($file));
}
}
} elseif ( $this->cache == 'custom' ) {
return call_user_func_array($this->custom_cache_get, array($reqhash));
}
return false;
}
function cache ($request, $response)
{
//Caches the unparsed response of a request.
unset($request['api_sig']);
foreach ( $request as $key => $value ) {
if ( empty($value) ) unset($request[$key]);
else $request[$key] = (string) $request[$key];
}
$reqhash = md5(serialize($request));
if ($this->cache == 'db') {
//$this->cache_db->query("DELETE FROM $this->cache_table WHERE request = '$reqhash'");
$response = urlencode($response);
$sql = 'INSERT INTO '.$this->cache_table.' (request, response, expiration)
VALUES (\''.$reqhash.'\', \''.$response.'\', TIMESTAMPADD(SECOND,'.$this->cache_expire.',CURRENT_TIMESTAMP))
ON DUPLICATE KEY UPDATE response=\''.$response.'\',
expiration=TIMESTAMPADD(SECOND,'.$this->cache_expire.',CURRENT_TIMESTAMP) ';
$result = mysqli_query($this->cache_db, $sql);
if(!$result) {
echo mysqli_error($this->cache_db);
}
return $result;
} elseif ($this->cache == "fs") {
$file = $this->cache_dir . "/" . $reqhash . ".cache";
$fstream = fopen($file, "w");
$result = fwrite($fstream,$response);
fclose($fstream);
return $result;
} elseif ( $this->cache == "custom" ) {
return call_user_func_array($this->custom_cache_set, array($reqhash, $response, $this->cache_expire));
}
return false;
}
function setCustomPost ( $function ) {
$this->custom_post = $function;
}
function post ($data, $type = null) {
if ( is_null($type) ) {
$url = $this->rest_endpoint;
}
if ( !is_null($this->custom_post) ) {
return call_user_func($this->custom_post, $url, $data);
}
if ( !preg_match("|https://(.*?)(/.*)|", $url, $matches) ) {
die('There was some problem figuring out your endpoint');
}
if ( function_exists('curl_init') ) {
// Has curl. Use it!
$curl = curl_init($this->rest_endpoint);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
} else {
// Use sockets.
foreach ( $data as $key => $value ) {
$data[$key] = $key . '=' . urlencode($value);
}
$data = implode('&', $data);
$fp = @pfsockopen('ssl://'.$matches[1], 443);
if (!$fp) {
die('Could not connect to the web service');
}
fputs ($fp,'POST ' . $matches[2] . " HTTP/1.1\n");
fputs ($fp,'Host: ' . $matches[1] . "\n");
fputs ($fp,"Content-type: application/x-www-form-urlencoded\n");
fputs ($fp,"Content-length: ".strlen($data)."\n");
fputs ($fp,"Connection: close\r\n\r\n");
fputs ($fp,$data . "\n\n");
$response = "";
while(!feof($fp)) {
$response .= fgets($fp, 1024);
}
fclose ($fp);
$chunked = false;
$http_status = trim(substr($response, 0, strpos($response, "\n")));
if ( $http_status != 'HTTP/1.1 200 OK' ) {
die('The web service endpoint returned a "' . $http_status . '" response');
}
if ( strpos($response, 'Transfer-Encoding: chunked') !== false ) {
$temp = trim(strstr($response, "\r\n\r\n"));
$response = '';
$length = trim(substr($temp, 0, strpos($temp, "\r")));
while ( trim($temp) != "0" && ($length = trim(substr($temp, 0, strpos($temp, "\r")))) != "0" ) {
$response .= trim(substr($temp, strlen($length)+2, hexdec($length)));
$temp = trim(substr($temp, strlen($length) + 2 + hexdec($length)));
}
} elseif ( strpos($response, 'HTTP/1.1 200 OK') !== false ) {
$response = trim(strstr($response, "\r\n\r\n"));
}
}
return $response;
}
function request ($command, $args = array(), $nocache = false)
{
//Sends a request to Flickr's REST endpoint via POST.
if (substr($command,0,7) != "flickr.") {
$command = "flickr." . $command;
}
//Process arguments, including method and login data.
$args = array_merge(array("method" => $command, "format" => "json", "nojsoncallback" => "1", "api_key" => $this->api_key), $args);
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
} elseif (!empty($_SESSION['phpFlickr_auth_token'])) {
$args = array_merge($args, array("auth_token" => $_SESSION['phpFlickr_auth_token']));
}
ksort($args);
$auth_sig = "";
$this->last_request = $args;
$this->response = $this->getCached($args);
if (!($this->response) || $nocache) {
foreach ($args as $key => $data) {
if ( is_null($data) ) {
unset($args[$key]);
continue;
}
$auth_sig .= $key . $data;
}
if (!empty($this->secret)) {
$api_sig = md5($this->secret . $auth_sig);
$args['api_sig'] = $api_sig;
}
$this->response = $this->post($args);
$this->cache($args, $this->response);
}
/*
* Uncomment this line (and comment out the next one) if you're doing large queries
* and you're concerned about time. This will, however, change the structure of
* the result, so be sure that you look at the results.
*/
$this->parsed_response = json_decode($this->response, TRUE);
/* $this->parsed_response = $this->clean_text_nodes(json_decode($this->response, TRUE)); */
if ($this->parsed_response['stat'] == 'fail') {
if ($this->die_on_error) die("The Flickr API returned the following error: #{$this->parsed_response['code']} - {$this->parsed_response['message']}");
else {
$this->error_code = $this->parsed_response['code'];
$this->error_msg = $this->parsed_response['message'];
$this->parsed_response = false;
}
} else {
$this->error_code = false;
$this->error_msg = false;
}
return $this->response;
}
function clean_text_nodes ($arr) {
if (!is_array($arr)) {
return $arr;
} elseif (count($arr) == 0) {
return $arr;
} elseif (count($arr) == 1 && array_key_exists('_content', $arr)) {
return $arr['_content'];
} else {
foreach ($arr as $key => $element) {
$arr[$key] = $this->clean_text_nodes($element);
}
return($arr);
}
}
function setToken ($token) {
// Sets an authentication token to use instead of the session variable
$this->token = $token;
}
function setProxy ($server, $port) {
// Sets the proxy for all phpFlickr calls.
$this->req->setProxy($server, $port);
}
function getErrorCode () {
// Returns the error code of the last call. If the last call did not
// return an error. This will return a false boolean.
return $this->error_code;
}
function getErrorMsg () {
// Returns the error message of the last call. If the last call did not
// return an error. This will return a false boolean.
return $this->error_msg;
}
/* These functions are front ends for the flickr calls */
function buildPhotoURL ($photo, $size = "Medium") {
//receives an array (can use the individual photo data returned
//from an API call) and returns a URL (doesn't mean that the
//file size exists)
$sizes = array(
"square" => "_s",
"square_75" => "_s",
"square_150" => "_q",
"thumbnail" => "_t",
"small" => "_m",
"small_240" => "_m",
"small_320" => "_n",
"medium" => "",
"medium_500" => "",
"medium_640" => "_z",
"medium_800" => "_c",
"large" => "_b",
"large_1024" => "_b",
"large_1600" => "_h",
"large_2048" => "_k",
"original" => "_o",
);
$size = strtolower($size);
if (!array_key_exists($size, $sizes)) {
$size = "medium";
}
if ($size == "original") {
$url = "https://farm" . $photo['farm'] . ".static.flickr.com/" . $photo['server'] . "/" . $photo['id'] . "_" . $photo['originalsecret'] . "_o" . "." . $photo['originalformat'];
} else {
$url = "https://farm" . $photo['farm'] . ".static.flickr.com/" . $photo['server'] . "/" . $photo['id'] . "_" . $photo['secret'] . $sizes[$size] . ".jpg";
}
return $url;
}
function sync_upload ($photo, $title = null, $description = null, $tags = null, $is_public = null, $is_friend = null, $is_family = null) {
if ( function_exists('curl_init') ) {
// Has curl. Use it!
//Process arguments, including method and login data.
$args = array("api_key" => $this->api_key, "title" => $title, "description" => $description, "tags" => $tags, "is_public" => $is_public, "is_friend" => $is_friend, "is_family" => $is_family);
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
} elseif (!empty($_SESSION['phpFlickr_auth_token'])) {
$args = array_merge($args, array("auth_token" => $_SESSION['phpFlickr_auth_token']));
}
ksort($args);
$auth_sig = "";
foreach ($args as $key => $data) {
if ( is_null($data) ) {
unset($args[$key]);
} else {
$auth_sig .= $key . $data;
}
}
if (!empty($this->secret)) {
$api_sig = md5($this->secret . $auth_sig);
$args["api_sig"] = $api_sig;
}
$photo = realpath($photo);
$args['photo'] = '@' . $photo;
$curl = curl_init($this->upload_endpoint);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $args);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$this->response = $response;
curl_close($curl);
$rsp = explode("\n", $response);
foreach ($rsp as $line) {
if (preg_match('|<err code="([0-9]+)" msg="(.*)"|', $line, $match)) {
if ($this->die_on_error)
die("The Flickr API returned the following error: #{$match[1]} - {$match[2]}");
else {
$this->error_code = $match[1];
$this->error_msg = $match[2];
$this->parsed_response = false;
return false;
}
} elseif (preg_match("|<photoid>(.*)</photoid>|", $line, $match)) {
$this->error_code = false;
$this->error_msg = false;
return $match[1];
}
}
} else {
die("Sorry, your server must support CURL in order to upload files");
}
}
function async_upload ($photo, $title = null, $description = null, $tags = null, $is_public = null, $is_friend = null, $is_family = null) {
if ( function_exists('curl_init') ) {
// Has curl. Use it!
//Process arguments, including method and login data.
$args = array("async" => 1, "api_key" => $this->api_key, "title" => $title, "description" => $description, "tags" => $tags, "is_public" => $is_public, "is_friend" => $is_friend, "is_family" => $is_family);
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
} elseif (!empty($_SESSION['phpFlickr_auth_token'])) {
$args = array_merge($args, array("auth_token" => $_SESSION['phpFlickr_auth_token']));
}
ksort($args);
$auth_sig = "";
foreach ($args as $key => $data) {
if ( is_null($data) ) {
unset($args[$key]);
} else {
$auth_sig .= $key . $data;
}
}
if (!empty($this->secret)) {
$api_sig = md5($this->secret . $auth_sig);
$args["api_sig"] = $api_sig;
}
$photo = realpath($photo);
$args['photo'] = '@' . $photo;
$curl = curl_init($this->upload_endpoint);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $args);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$this->response = $response;
curl_close($curl);
$rsp = explode("\n", $response);
foreach ($rsp as $line) {
if (preg_match('/<err code="([0-9]+)" msg="(.*)"/', $line, $match)) {
if ($this->die_on_error)
die("The Flickr API returned the following error: #{$match[1]} - {$match[2]}");
else {
$this->error_code = $match[1];
$this->error_msg = $match[2];
$this->parsed_response = false;
return false;
}
} elseif (preg_match("/<ticketid>(.*)</", $line, $match)) {
$this->error_code = false;
$this->error_msg = false;
return $match[1];
}
}
} else {
die("Sorry, your server must support CURL in order to upload files");
}
}
// Interface for new replace API method.
function replace ($photo, $photo_id, $async = null) {
if ( function_exists('curl_init') ) {
// Has curl. Use it!
//Process arguments, including method and login data.
$args = array("api_key" => $this->api_key, "photo_id" => $photo_id, "async" => $async);
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
} elseif (!empty($_SESSION['phpFlickr_auth_token'])) {
$args = array_merge($args, array("auth_token" => $_SESSION['phpFlickr_auth_token']));
}
ksort($args);
$auth_sig = "";
foreach ($args as $key => $data) {
if ( is_null($data) ) {
unset($args[$key]);
} else {
$auth_sig .= $key . $data;
}
}
if (!empty($this->secret)) {
$api_sig = md5($this->secret . $auth_sig);
$args["api_sig"] = $api_sig;
}
$photo = realpath($photo);
$args['photo'] = '@' . $photo;
$curl = curl_init($this->replace_endpoint);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $args);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$this->response = $response;
curl_close($curl);
if ($async == 1)
$find = 'ticketid';
else
$find = 'photoid';
$rsp = explode("\n", $response);
foreach ($rsp as $line) {
if (preg_match('|<err code="([0-9]+)" msg="(.*)"|', $line, $match)) {
if ($this->die_on_error)
die("The Flickr API returned the following error: #{$match[1]} - {$match[2]}");
else {
$this->error_code = $match[1];
$this->error_msg = $match[2];
$this->parsed_response = false;
return false;
}
} elseif (preg_match("|<" . $find . ">(.*)</|", $line, $match)) {
$this->error_code = false;
$this->error_msg = false;
return $match[1];
}
}
} else {
die("Sorry, your server must support CURL in order to upload files");
}
}
function auth ($perms = "read", $remember_uri = true) {
// Redirects to Flickr's authentication piece if there is no valid token.
// If remember_uri is set to false, the callback script (included) will
// redirect to its default page.
if (empty($_SESSION['phpFlickr_auth_token']) && empty($this->token)) {
if ( $remember_uri === true ) {
$_SESSION['phpFlickr_auth_redirect'] = $_SERVER['REQUEST_URI'];
} elseif ( $remember_uri !== false ) {
$_SESSION['phpFlickr_auth_redirect'] = $remember_uri;
}
$api_sig = md5($this->secret . "api_key" . $this->api_key . "perms" . $perms);
if ($this->service == "23") {
header("Location: http://www.23hq.com/services/auth/?api_key=" . $this->api_key . "&perms=" . $perms . "&api_sig=". $api_sig);
} else {
header("Location: https://www.flickr.com/services/auth/?api_key=" . $this->api_key . "&perms=" . $perms . "&api_sig=". $api_sig);
}
exit;
} else {
$tmp = $this->die_on_error;
$this->die_on_error = false;
$rsp = $this->auth_checkToken();
if ($this->error_code !== false) {
unset($_SESSION['phpFlickr_auth_token']);
$this->auth($perms, $remember_uri);
}
$this->die_on_error = $tmp;
return $rsp['perms'];
}
}
function auth_url($frob, $perms = 'read') {
$sig = md5(sprintf('%sapi_key%sfrob%sperms%s', $this->secret, $this->api_key, $frob, $perms));
return sprintf('https://flickr.com/services/auth/?api_key=%s&perms=%s&frob=%s&api_sig=%s', $this->api_key, $perms, $frob, $sig);
}
/*******************************
To use the phpFlickr::call method, pass a string containing the API method you want
to use and an associative array of arguments. For example:
$result = $f->call("flickr.photos.comments.getList", array("photo_id"=>'34952612'));
This method will allow you to make calls to arbitrary methods that haven't been
implemented in phpFlickr yet.
*******************************/
function call ($method, $arguments) {
foreach ( $arguments as $key => $value ) {
if ( is_null($value) ) unset($arguments[$key]);
}
$this->request($method, $arguments);
return $this->parsed_response ? $this->parsed_response : false;
}
/*
These functions are the direct implementations of flickr calls.
For method documentation, including arguments, visit the address
included in a comment in the function.
*/
/* Activity methods */
function activity_userComments ($per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.activity.userComments.html */
$this->request('flickr.activity.userComments', array("per_page" => $per_page, "page" => $page));
return $this->parsed_response ? $this->parsed_response['items']['item'] : false;
}
function activity_userPhotos ($timeframe = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.activity.userPhotos.html */
$this->request('flickr.activity.userPhotos', array("timeframe" => $timeframe, "per_page" => $per_page, "page" => $page));
return $this->parsed_response ? $this->parsed_response['items']['item'] : false;
}
/* Authentication methods */
function auth_checkToken () {
/* https://www.flickr.com/services/api/flickr.auth.checkToken.html */
$this->request('flickr.auth.checkToken');
return $this->parsed_response ? $this->parsed_response['auth'] : false;
}
function auth_getFrob () {
/* https://www.flickr.com/services/api/flickr.auth.getFrob.html */
$this->request('flickr.auth.getFrob');
return $this->parsed_response ? $this->parsed_response['frob'] : false;
}
function auth_getFullToken ($mini_token) {
/* https://www.flickr.com/services/api/flickr.auth.getFullToken.html */
$this->request('flickr.auth.getFullToken', array('mini_token'=>$mini_token));
return $this->parsed_response ? $this->parsed_response['auth'] : false;
}
function auth_getToken ($frob) {
/* https://www.flickr.com/services/api/flickr.auth.getToken.html */
$this->request('flickr.auth.getToken', array('frob'=>$frob));
$_SESSION['phpFlickr_auth_token'] = $this->parsed_response['auth']['token'];
return $this->parsed_response ? $this->parsed_response['auth'] : false;
}
/* Blogs methods */
function blogs_getList ($service = NULL) {
/* https://www.flickr.com/services/api/flickr.blogs.getList.html */
$rsp = $this->call('flickr.blogs.getList', array('service' => $service));
return $rsp['blogs']['blog'];
}
function blogs_getServices () {
/* https://www.flickr.com/services/api/flickr.blogs.getServices.html */
return $this->call('flickr.blogs.getServices', array());
}
function blogs_postPhoto ($blog_id = NULL, $photo_id, $title, $description, $blog_password = NULL, $service = NULL) {
/* https://www.flickr.com/services/api/flickr.blogs.postPhoto.html */
return $this->call('flickr.blogs.postPhoto', array('blog_id' => $blog_id, 'photo_id' => $photo_id, 'title' => $title, 'description' => $description, 'blog_password' => $blog_password, 'service' => $service));
}
/* Collections Methods */
function collections_getInfo ($collection_id) {
/* https://www.flickr.com/services/api/flickr.collections.getInfo.html */
return $this->call('flickr.collections.getInfo', array('collection_id' => $collection_id));
}
function collections_getTree ($collection_id = NULL, $user_id = NULL) {
/* https://www.flickr.com/services/api/flickr.collections.getTree.html */
return $this->call('flickr.collections.getTree', array('collection_id' => $collection_id, 'user_id' => $user_id));
}
/* Commons Methods */
function commons_getInstitutions () {
/* https://www.flickr.com/services/api/flickr.commons.getInstitutions.html */
return $this->call('flickr.commons.getInstitutions', array());
}
/* Contacts Methods */
function contacts_getList ($filter = NULL, $page = NULL, $per_page = NULL) {
/* https://www.flickr.com/services/api/flickr.contacts.getList.html */
$this->request('flickr.contacts.getList', array('filter'=>$filter, 'page'=>$page, 'per_page'=>$per_page));
return $this->parsed_response ? $this->parsed_response['contacts'] : false;
}
function contacts_getPublicList ($user_id, $page = NULL, $per_page = NULL) {
/* https://www.flickr.com/services/api/flickr.contacts.getPublicList.html */
$this->request('flickr.contacts.getPublicList', array('user_id'=>$user_id, 'page'=>$page, 'per_page'=>$per_page));
return $this->parsed_response ? $this->parsed_response['contacts'] : false;
}
function contacts_getListRecentlyUploaded ($date_lastupload = NULL, $filter = NULL) {
/* https://www.flickr.com/services/api/flickr.contacts.getListRecentlyUploaded.html */
return $this->call('flickr.contacts.getListRecentlyUploaded', array('date_lastupload' => $date_lastupload, 'filter' => $filter));
}
/* Favorites Methods */
function favorites_add ($photo_id) {
/* https://www.flickr.com/services/api/flickr.favorites.add.html */
$this->request('flickr.favorites.add', array('photo_id'=>$photo_id), TRUE);
return $this->parsed_response ? true : false;
}
function favorites_getList ($user_id = NULL, $jump_to = NULL, $min_fave_date = NULL, $max_fave_date = NULL, $extras = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.favorites.getList.html */
return $this->call('flickr.favorites.getList', array('user_id' => $user_id, 'jump_to' => $jump_to, 'min_fave_date' => $min_fave_date, 'max_fave_date' => $max_fave_date, 'extras' => $extras, 'per_page' => $per_page, 'page' => $page));
}
function favorites_getPublicList ($user_id, $jump_to = NULL, $min_fave_date = NULL, $max_fave_date = NULL, $extras = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.favorites.getPublicList.html */
return $this->call('flickr.favorites.getPublicList', array('user_id' => $user_id, 'jump_to' => $jump_to, 'min_fave_date' => $min_fave_date, 'max_fave_date' => $max_fave_date, 'extras' => $extras, 'per_page' => $per_page, 'page' => $page));
}
function favorites_remove ($photo_id, $user_id = NULL) {
/* https://www.flickr.com/services/api/flickr.favorites.remove.html */
$this->request("flickr.favorites.remove", array('photo_id' => $photo_id, 'user_id' => $user_id), TRUE);
return $this->parsed_response ? true : false;
}
/* Galleries Methods */
function galleries_addPhoto ($gallery_id, $photo_id, $comment = NULL) {
/* https://www.flickr.com/services/api/flickr.galleries.addPhoto.html */
return $this->call('flickr.galleries.addPhoto', array('gallery_id' => $gallery_id, 'photo_id' => $photo_id, 'comment' => $comment));
}
function galleries_create ($title, $description, $primary_photo_id = NULL) {
/* https://www.flickr.com/services/api/flickr.galleries.create.html */
return $this->call('flickr.galleries.create', array('title' => $title, 'description' => $description, 'primary_photo_id' => $primary_photo_id));
}
function galleries_editMeta ($gallery_id, $title, $description = NULL) {
/* https://www.flickr.com/services/api/flickr.galleries.editMeta.html */
return $this->call('flickr.galleries.editMeta', array('gallery_id' => $gallery_id, 'title' => $title, 'description' => $description));
}
function galleries_editPhoto ($gallery_id, $photo_id, $comment) {
/* https://www.flickr.com/services/api/flickr.galleries.editPhoto.html */
return $this->call('flickr.galleries.editPhoto', array('gallery_id' => $gallery_id, 'photo_id' => $photo_id, 'comment' => $comment));
}
function galleries_editPhotos ($gallery_id, $primary_photo_id, $photo_ids) {
/* https://www.flickr.com/services/api/flickr.galleries.editPhotos.html */
return $this->call('flickr.galleries.editPhotos', array('gallery_id' => $gallery_id, 'primary_photo_id' => $primary_photo_id, 'photo_ids' => $photo_ids));
}
function galleries_getInfo ($gallery_id) {
/* https://www.flickr.com/services/api/flickr.galleries.getInfo.html */
return $this->call('flickr.galleries.getInfo', array('gallery_id' => $gallery_id));
}
function galleries_getList ($user_id, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.galleries.getList.html */
return $this->call('flickr.galleries.getList', array('user_id' => $user_id, 'per_page' => $per_page, 'page' => $page));
}
function galleries_getListForPhoto ($photo_id, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.galleries.getListForPhoto.html */
return $this->call('flickr.galleries.getListForPhoto', array('photo_id' => $photo_id, 'per_page' => $per_page, 'page' => $page));
}
function galleries_getPhotos ($gallery_id, $extras = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.galleries.getPhotos.html */
return $this->call('flickr.galleries.getPhotos', array('gallery_id' => $gallery_id, 'extras' => $extras, 'per_page' => $per_page, 'page' => $page));
}
/* Groups Methods */
function groups_browse ($cat_id = NULL) {
/* https://www.flickr.com/services/api/flickr.groups.browse.html */
$this->request("flickr.groups.browse", array("cat_id"=>$cat_id));
return $this->parsed_response ? $this->parsed_response['category'] : false;
}
function groups_getInfo ($group_id, $lang = NULL) {
/* https://www.flickr.com/services/api/flickr.groups.getInfo.html */
return $this->call('flickr.groups.getInfo', array('group_id' => $group_id, 'lang' => $lang));
}
function groups_search ($text, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.groups.search.html */
$this->request("flickr.groups.search", array("text"=>$text,"per_page"=>$per_page,"page"=>$page));
return $this->parsed_response ? $this->parsed_response['groups'] : false;
}
/* Groups Members Methods */
function groups_members_getList ($group_id, $membertypes = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.groups.members.getList.html */
return $this->call('flickr.groups.members.getList', array('group_id' => $group_id, 'membertypes' => $membertypes, 'per_page' => $per_page, 'page' => $page));
}
/* Groups Pools Methods */
function groups_pools_add ($photo_id, $group_id) {
/* https://www.flickr.com/services/api/flickr.groups.pools.add.html */
$this->request("flickr.groups.pools.add", array("photo_id"=>$photo_id, "group_id"=>$group_id), TRUE);
return $this->parsed_response ? true : false;
}
function groups_pools_getContext ($photo_id, $group_id, $num_prev = NULL, $num_next = NULL) {
/* https://www.flickr.com/services/api/flickr.groups.pools.getContext.html */
return $this->call('flickr.groups.pools.getContext', array('photo_id' => $photo_id, 'group_id' => $group_id, 'num_prev' => $num_prev, 'num_next' => $num_next));
}
function groups_pools_getGroups ($page = NULL, $per_page = NULL) {
/* https://www.flickr.com/services/api/flickr.groups.pools.getGroups.html */
$this->request("flickr.groups.pools.getGroups", array('page'=>$page, 'per_page'=>$per_page));
return $this->parsed_response ? $this->parsed_response['groups'] : false;
}
function groups_pools_getPhotos ($group_id, $tags = NULL, $user_id = NULL, $jump_to = NULL, $extras = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.groups.pools.getPhotos.html */
if (is_array($extras)) {
$extras = implode(",", $extras);
}
return $this->call('flickr.groups.pools.getPhotos', array('group_id' => $group_id, 'tags' => $tags, 'user_id' => $user_id, 'jump_to' => $jump_to, 'extras' => $extras, 'per_page' => $per_page, 'page' => $page));
}
function groups_pools_remove ($photo_id, $group_id) {
/* https://www.flickr.com/services/api/flickr.groups.pools.remove.html */
$this->request("flickr.groups.pools.remove", array("photo_id"=>$photo_id, "group_id"=>$group_id), TRUE);
return $this->parsed_response ? true : false;
}
/* Interestingness methods */
function interestingness_getList ($date = NULL, $use_panda = NULL, $extras = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.interestingness.getList.html */
if (is_array($extras)) {
$extras = implode(",", $extras);
}
return $this->call('flickr.interestingness.getList', array('date' => $date, 'use_panda' => $use_panda, 'extras' => $extras, 'per_page' => $per_page, 'page' => $page));
}
/* Machine Tag methods */
function machinetags_getNamespaces ($predicate = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.machinetags.getNamespaces.html */
return $this->call('flickr.machinetags.getNamespaces', array('predicate' => $predicate, 'per_page' => $per_page, 'page' => $page));
}
function machinetags_getPairs ($namespace = NULL, $predicate = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.machinetags.getPairs.html */
return $this->call('flickr.machinetags.getPairs', array('namespace' => $namespace, 'predicate' => $predicate, 'per_page' => $per_page, 'page' => $page));
}
function machinetags_getPredicates ($namespace = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.machinetags.getPredicates.html */
return $this->call('flickr.machinetags.getPredicates', array('namespace' => $namespace, 'per_page' => $per_page, 'page' => $page));
}
function machinetags_getRecentValues ($namespace = NULL, $predicate = NULL, $added_since = NULL) {
/* https://www.flickr.com/services/api/flickr.machinetags.getRecentValues.html */
return $this->call('flickr.machinetags.getRecentValues', array('namespace' => $namespace, 'predicate' => $predicate, 'added_since' => $added_since));
}
function machinetags_getValues ($namespace, $predicate, $per_page = NULL, $page = NULL, $usage = NULL) {
/* https://www.flickr.com/services/api/flickr.machinetags.getValues.html */
return $this->call('flickr.machinetags.getValues', array('namespace' => $namespace, 'predicate' => $predicate, 'per_page' => $per_page, 'page' => $page, 'usage' => $usage));
}
/* Panda methods */
function panda_getList () {
/* https://www.flickr.com/services/api/flickr.panda.getList.html */
return $this->call('flickr.panda.getList', array());
}
function panda_getPhotos ($panda_name, $extras = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.panda.getPhotos.html */
return $this->call('flickr.panda.getPhotos', array('panda_name' => $panda_name, 'extras' => $extras, 'per_page' => $per_page, 'page' => $page));
}
/* People methods */
function people_findByEmail ($find_email) {
/* https://www.flickr.com/services/api/flickr.people.findByEmail.html */
$this->request("flickr.people.findByEmail", array("find_email"=>$find_email));
return $this->parsed_response ? $this->parsed_response['user'] : false;
}
function people_findByUsername ($username) {
/* https://www.flickr.com/services/api/flickr.people.findByUsername.html */
$this->request("flickr.people.findByUsername", array("username"=>$username));
return $this->parsed_response ? $this->parsed_response['user'] : false;
}
function people_getInfo ($user_id) {
/* https://www.flickr.com/services/api/flickr.people.getInfo.html */
$this->request("flickr.people.getInfo", array("user_id"=>$user_id));
return $this->parsed_response ? $this->parsed_response['person'] : false;
}
function people_getPhotos ($user_id, $args = array()) {
/* This function strays from the method of arguments that I've
* used in the other functions for the fact that there are just
* so many arguments to this API method. What you'll need to do
* is pass an associative array to the function containing the
* arguments you want to pass to the API. For example:
* $photos = $f->photos_search(array("tags"=>"brown,cow", "tag_mode"=>"any"));
* This will return photos tagged with either "brown" or "cow"
* or both. See the API documentation (link below) for a full
* list of arguments.
*/
/* https://www.flickr.com/services/api/flickr.people.getPhotos.html */
return $this->call('flickr.people.getPhotos', array_merge(array('user_id' => $user_id), $args));
}
function people_getPhotosOf ($user_id, $extras = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.people.getPhotosOf.html */
return $this->call('flickr.people.getPhotosOf', array('user_id' => $user_id, 'extras' => $extras, 'per_page' => $per_page, 'page' => $page));
}
function people_getPublicGroups ($user_id) {
/* https://www.flickr.com/services/api/flickr.people.getPublicGroups.html */
$this->request("flickr.people.getPublicGroups", array("user_id"=>$user_id));
return $this->parsed_response ? $this->parsed_response['groups']['group'] : false;
}
function people_getPublicPhotos ($user_id, $safe_search = NULL, $extras = NULL, $per_page = NULL, $page = NULL) {
/* https://www.flickr.com/services/api/flickr.people.getPublicPhotos.html */
return $this->call('flickr.people.getPublicPhotos', array('user_id' => $user_id, 'safe_search' => $safe_search, 'extras' => $extras, 'per_page' => $per_page, 'page' => $page));
}
function people_getUploadStatus () {
/* https://www.flickr.com/services/api/flickr.people.getUploadStatus.html */
/* Requires Authentication */
$this->request("flickr.people.getUploadStatus");
return $this->parsed_response ? $this->parsed_response['user'] : false;
}
/* Photos Methods */
function photos_addTags ($photo_id, $tags) {
/* https://www.flickr.com/services/api/flickr.photos.addTags.html */
$this->request("flickr.photos.addTags", array("photo_id"=>$photo_id, "tags"=>$tags), TRUE);
return $this->parsed_response ? true : false;
}
function photos_delete ($photo_id) {
/* https://www.flickr.com/services/api/flickr.photos.delete.html */
$this->request("flickr.photos.delete", array("photo_id"=>$photo_id), TRUE);
return $this->parsed_response ? true : false;
}
function photos_getAllContexts ($photo_id) {
/* https://www.flickr.com/services/api/flickr.photos.getAllContexts.html */
$this->request("flickr.photos.getAllContexts", array("photo_id"=>$photo_id));
return $this->parsed_response ? $this->parsed_response : false;
}
function photos_getContactsPhotos ($count = NULL, $just_friends = NULL, $single_photo = NULL, $include_self = NULL, $extras = NULL) {
/* https://www.flickr.com/services/api/flickr.photos.getContactsPhotos.html */
$this->request("flickr.photos.getContactsPhotos", array("count"=>$count, "just_friends"=>$just_friends, "single_photo"=>$single_photo, "include_self"=>$include_self, "extras"=>$extras));
return $this->parsed_response ? $this->parsed_response['photos']['photo'] : false;
}
function photos_getContactsPublicPhotos ($user_id, $count = NULL, $just_friends = NULL, $single_photo = NULL, $include_self = NULL, $extras = NULL) {
/* https://www.flickr.com/services/api/flickr.photos.getContactsPublicPhotos.html */