PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/xinha/plugins/ImageManager/Classes/Flickr.php

https://github.com/radicaldesigns/amp
PHP | 188 lines | 151 code | 29 blank | 8 comment | 16 complexity | a8dee263111bd3552b821095ab573ea2 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * Simple functions to access the flickr API (yes I know this is the "Classes" directory and this isn't a class).
  4. * @author $Author: gogo $
  5. * @version $Id: images.php 877 2007-08-12 15:50:03Z gogo $
  6. * @package ImageManager
  7. */
  8. require_once(dirname(__FILE__) . '/JSON_Compat.php');
  9. function flickr_request($method, $params = array())
  10. {
  11. global $IMConfig;
  12. $flickr = "http://api.flickr.com/services/rest/?method={$method}&format=json&nojsoncallback=1&api_key=" . $IMConfig['Flickr']['Key'];
  13. foreach($params as $k => $v)
  14. {
  15. $flickr .= "&{$k}=".rawurlencode($v);
  16. }
  17. $feed = file_get_contents($flickr);
  18. if($feed)
  19. {
  20. $feed = json_decode($feed, true);
  21. if(!$feed || !isset($feed['stat']) || ($feed['stat'] != 'ok'))
  22. {
  23. print_r($params);
  24. trigger_error($feed['message'], E_USER_ERROR);
  25. return FALSE;
  26. }
  27. }
  28. else
  29. {
  30. trigger_error('Null response from Flickr', E_USER_ERROR);
  31. }
  32. return $feed;
  33. }
  34. function flickr_get_licenses()
  35. {
  36. static $lics;
  37. if(!$lics)
  38. {
  39. if(0 && isset($_SESSION['flickr_licenses']))
  40. {
  41. $lics = $_SESSION['flickr_licenses'];
  42. return $lics;
  43. }
  44. $lics = array();
  45. $x = flickr_request('flickr.photos.licenses.getInfo');
  46. $x = $x['licenses']['license'];
  47. foreach($x as $l)
  48. {
  49. // Add out own descriptive "usage" text
  50. switch($l['url'])
  51. {
  52. case 'http://creativecommons.org/licenses/by/2.0/':
  53. case 'http://creativecommons.org/licenses/by-sa/2.0/':
  54. $l['usage'] = 'Attribution Required';
  55. break;
  56. case 'http://creativecommons.org/licenses/by-nd/2.0/':
  57. $l['usage'] = 'Attribution Required, No Modifications';
  58. break;
  59. case 'http://creativecommons.org/licenses/by-nc-nd/2.0/':
  60. $l['usage'] = 'Non Commercial ONLY, Attribution Required, No Modifications';
  61. break;
  62. case 'http://creativecommons.org/licenses/by-nc/2.0/':
  63. case 'http://creativecommons.org/licenses/by-nc-sa/2.0/':
  64. $l['usage'] = 'Non Commercial ONLY, Attribution Required';
  65. break;
  66. default:
  67. $l['usage'] = 'Use ONLY Permitted With Written Permission';
  68. break;
  69. }
  70. // And our own identifier
  71. switch($l['url'])
  72. {
  73. case 'http://creativecommons.org/licenses/by/2.0/':
  74. $l['x-id'] = 'cc2';
  75. break;
  76. case 'http://creativecommons.org/licenses/by-sa/2.0/':
  77. $l['x-id'] = 'ccsa2';
  78. break;
  79. case 'http://creativecommons.org/licenses/by-nd/2.0/':
  80. $l['x-id'] = 'ccnd2';
  81. break;
  82. case 'http://creativecommons.org/licenses/by-nc-nd/2.0/':
  83. $l['x-id'] = 'ccncnd2';
  84. break;
  85. case 'http://creativecommons.org/licenses/by-nc/2.0/':
  86. $l['x-id'] = 'ccnc2';
  87. break;
  88. case 'http://creativecommons.org/licenses/by-nc-sa/2.0/':
  89. $l['x-id'] = 'ccncsa2';
  90. break;
  91. default:
  92. $l['x-id'] = '';
  93. break;
  94. }
  95. $lics[$l['id']] = $l;
  96. }
  97. $_SESSION['flickr_licenses'] = $lics;
  98. }
  99. return $lics;
  100. }
  101. function flickr_get_license_id_by_usage()
  102. {
  103. $lics = flickr_get_licenses();
  104. $use = array();
  105. foreach($lics as $lic)
  106. {
  107. if(!isset($use[$lic['usage']]))
  108. {
  109. $use[$lic['usage']] = $lic['id'];
  110. }
  111. else
  112. {
  113. $use[$lic['usage']] .= "," . $lic['id'];
  114. }
  115. }
  116. return $use;
  117. }
  118. function flickr_is_default_license($licIDs)
  119. {
  120. global $IMConfig;
  121. $lics = flickr_get_licenses();
  122. foreach($lics as $lic)
  123. {
  124. if($lic['url'] == $IMConfig['Flickr']['Default License'])
  125. {
  126. if(in_array($lic['id'], explode(',', $licIDs))) return TRUE;
  127. }
  128. }
  129. return FALSE;
  130. }
  131. function flickr_get_default_usage_id()
  132. {
  133. $usages = flickr_get_license_id_by_usage();
  134. foreach($usages as $usage => $id)
  135. {
  136. if(flickr_is_default_license($id)) return $id;
  137. }
  138. return 0;
  139. }
  140. function flickr_get_user_id($NameOrEmail)
  141. {
  142. if(preg_match('/@/', $NameOrEmail))
  143. {
  144. $d = flickr_request('flickr.people.findByEmail', array('find_email' => $NameOrEmail));
  145. if($d)
  146. {
  147. return $d['user']['id'];
  148. }
  149. }
  150. else
  151. {
  152. $d = flickr_request('flickr.people.findByUsername', array('username' => $NameOrEmail));
  153. if($d)
  154. {
  155. return $d['user']['id'];
  156. }
  157. }
  158. }
  159. ?>