/www/include/lib_flickr_api.php

https://github.com/straup/parallel-flickr · PHP · 217 lines · 123 code · 72 blank · 22 comment · 19 complexity · 2a54b98b3673788bdb59b0d416960b4a MD5 · raw file

  1. <?php
  2. #
  3. # $Id$
  4. #
  5. loadlib("http");
  6. #################################################################
  7. $GLOBALS['cfg']['flickr_api_endpoint'] = 'http://api.flickr.com/services/rest/';
  8. $GLOBALS['cfg']['flickr_upload_endpoint'] = 'http://api.flickr.com/services/upload/';
  9. $GLOBALS['cfg']['flickr_auth_endpoint'] = 'http://api.flickr.com/services/auth/';
  10. #################################################################
  11. function flickr_api_authtoken_perms_map($string_keys=0){
  12. $map = array(
  13. '0' => 'read',
  14. '1' => 'write',
  15. '2' => 'delete',
  16. );
  17. if ($string_keys){
  18. $map = array_flip($map);
  19. }
  20. return $map;
  21. }
  22. #################################################################
  23. function flickr_api_auth_url($perms, $extra=null, $frob=null){
  24. $args = array(
  25. 'api_key' => $GLOBALS['cfg']['flickr_api_key'],
  26. 'perms' => $perms,
  27. );
  28. # OH GOD... fix me...
  29. if ($frob){
  30. $args['frob'] = $frob;
  31. }
  32. if ($extra){
  33. $extra = http_build_query($extra);
  34. $args['extra'] = $extra;
  35. }
  36. $api_sig = _flickr_api_sign_args($args);
  37. $args['api_sig'] = $api_sig;
  38. $url = $GLOBALS['cfg']['flickr_auth_endpoint'] . "?" . http_build_query($args);
  39. return $url;
  40. }
  41. #################################################################
  42. function flickr_api_call_build($method, $args=array(), $more=array()){
  43. # untested (20130627/straup)
  44. if ((0) && ($GLOBALS['cfg']['flickr_api_use_oauth_bridge'])){
  45. $GLOBALS['cfg']['flickr_api_key'] = $GLOBALS['cfg']['flickr_oauth_key'];
  46. $GLOBALS['cfg']['flickr_api_key'] = $GLOBALS['cfg']['flickr_oauth_secret'];
  47. if (isset($args['auth_token'])){
  48. $flickr_user = flickr_users_get_by_auth_token($args['auth_token']);
  49. $more['user_key'] = $flickr_user['oauth_token'];
  50. $more['user_secret'] = $flickr_user['oauth_secret'];
  51. }
  52. loadlib("flickr_api_oauth");
  53. return flickr_api_oauth_call($method, $args, $more);
  54. }
  55. $args['api_key'] = $GLOBALS['cfg']['flickr_api_key'];
  56. $args['method'] = $method;
  57. $args['format'] = 'json';
  58. $args['nojsoncallback'] = 1;
  59. if ((isset($args['auth_token'])) || (isset($more['sign']))){
  60. $api_sig = _flickr_api_sign_args($args);
  61. $args['api_sig'] = $api_sig;
  62. }
  63. $url = $GLOBALS['cfg']['flickr_api_endpoint'];
  64. return array($url, $args);
  65. }
  66. #################################################################
  67. function flickr_api_call($method, $args=array(), $more=array()){
  68. list($url, $args) = flickr_api_call_build($method, $args, $more);
  69. $defaults = array(
  70. 'http_timeout' => 10,
  71. );
  72. $more = array_merge($defaults, $more);
  73. $headers = array();
  74. $rsp = http_post($url, $args, $headers, $more);
  75. # $url = $url . "?" . http_build_query($args);
  76. # $rsp = http_get($url);
  77. return flickr_api_parse_response($rsp);
  78. }
  79. #################################################################
  80. function flickr_api_parse_response($rsp){
  81. if (! $rsp['ok']){
  82. return $rsp;
  83. }
  84. if (isset($more['raw'])){
  85. return $rsp;
  86. }
  87. $json = json_decode($rsp['body'], 'as a hash');
  88. if (! $json){
  89. return array( 'ok' => 0, 'error' => 'failed to parse response' );
  90. }
  91. if ($json['stat'] != 'ok'){
  92. return array( 'ok' => 0, 'error' => $json['message'], 'error_code' => $json['code']);
  93. }
  94. unset($json['stat']);
  95. return array( 'ok' => 1, 'rsp' => $json );
  96. }
  97. #################################################################
  98. function flickr_api_upload($file, $args, $more=array()){
  99. $args['api_key'] = $GLOBALS['cfg']['flickr_api_key'];
  100. # did we really never add json output for uploads?
  101. # why did we do that... (20120208/straup)
  102. #
  103. # $args['format'] = 'json';
  104. # $args['nojsoncallback'] = 1;
  105. $sig = _flickr_api_sign_args($args);
  106. $args['api_sig'] = $sig;
  107. $args['photo'] = "@{$file}";
  108. $defaults = array(
  109. 'http_timeout' => 10,
  110. );
  111. $more = array_merge($defaults, $more);
  112. $headers = array();
  113. $url = $GLOBALS['cfg']['flickr_upload_endpoint'];
  114. $rsp = http_post($url, $args, $headers, $more);
  115. if (! $rsp['ok']){
  116. return $rsp;
  117. }
  118. # sigh... see above
  119. if ((isset($args['async'])) && ($args['async'])){
  120. if (preg_match("/<ticketid>(\d+-\d+)<\/ticketid>/m", $rsp['body'], $m)){
  121. return okay(array("ticket_id" => $m[1]));
  122. }
  123. }
  124. else {
  125. if (preg_match("/<photoid>(\d+)<\/photoid>/m", $rsp['body'], $m)){
  126. return okay(array("photo_id" => $m[1]));
  127. }
  128. }
  129. return not_okay("failed to parse response '{$rsp['body']}'");
  130. }
  131. #################################################################
  132. function _flickr_api_sign_args($args){
  133. $parts = array(
  134. $GLOBALS['cfg']['flickr_api_secret']
  135. );
  136. $keys = array_keys($args);
  137. sort($keys);
  138. foreach ($keys as $k){
  139. $parts[] = $k . $args[$k];
  140. }
  141. $raw = implode("", $parts);
  142. return md5($raw);
  143. }
  144. #################################################################
  145. ?>