1 | <?php |
---|
2 | // |
---|
3 | // doc API Flickr: |
---|
4 | // https://www.flickr.com/services/api/ |
---|
5 | |
---|
6 | |
---|
7 | define ("_KEY_API_FLICKR_RAND", "867b665fba7129ffe540684714ab50e0"); |
---|
8 | |
---|
9 | |
---|
10 | # |
---|
11 | # function fetch_data_flickr |
---|
12 | # call the API and decode the response |
---|
13 | function fetch_data_flickr($params,$debug=false) { |
---|
14 | $encoded_params = array(); |
---|
15 | foreach ($params as $k => $v){ |
---|
16 | $encoded_params[] = urlencode($k).'='.urlencode($v); |
---|
17 | } |
---|
18 | $url = "https://api.flickr.com/services/rest/?".implode('&', $encoded_params); |
---|
19 | $rsp = file_get_contents($url); |
---|
20 | $rsp_obj = unserialize($rsp); |
---|
21 | if ($debug) var_dump($rsp_obj); |
---|
22 | |
---|
23 | if ($rsp_obj['stat'] == 'ok') return $rsp_obj; |
---|
24 | else return false; |
---|
25 | |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | // |
---|
31 | // Main |
---|
32 | // |
---|
33 | |
---|
34 | function flickr_rand($str,$tags='',$license=5,$align='',$size='Small',$safesearch=1,$id=0,$text='',$texte='',$iteration=0) { |
---|
35 | $api_key = _KEY_API_FLICKR_RAND; |
---|
36 | |
---|
37 | // etape -1: recuper config sur present |
---|
38 | if (function_exists(lire_config)) { |
---|
39 | $plage = (int) lire_config('flickr_rand/plage'); |
---|
40 | if ($plage>4000) $plage = 4000; |
---|
41 | if ($plage<0) $plage = 1; |
---|
42 | |
---|
43 | $blacklist = explode(";",lire_config('flickr_rand/blacklist', "")); |
---|
44 | } else { |
---|
45 | $plage = 100; |
---|
46 | $blacklist = array(); |
---|
47 | } |
---|
48 | |
---|
49 | // etape 0: traiter des parametres et ajouter les valeurs par defaut (le php ne les prends pas via fonction a cause du modele) |
---|
50 | $tags = strip_tags($tags); |
---|
51 | $text = strip_tags($text); |
---|
52 | $texte = strip_tags($texte); |
---|
53 | if ($texte!="") |
---|
54 | $text = $texte; |
---|
55 | |
---|
56 | $license = strip_tags($license); |
---|
57 | if ($license == 0 ) |
---|
58 | $license = 9; // valeur par defaut |
---|
59 | if ($license == -1 ) |
---|
60 | $license = 0; // copyright |
---|
61 | if ($license == 8 ) |
---|
62 | $license = "1,2,3,4,5,6"; // alias |
---|
63 | if ($license == 9 ) |
---|
64 | $license = "1,2,3,4,5,6,7"; // alias |
---|
65 | |
---|
66 | $align = strip_tags($align); |
---|
67 | if ($align=="") $align = "non_aligne"; |
---|
68 | |
---|
69 | $size = ucfirst(strip_tags($size)); |
---|
70 | if ($size=="") $size = "Small"; // valeur par defaut |
---|
71 | |
---|
72 | $safesearch = (int) $safesearch; |
---|
73 | if ($safesearch == 0 ) |
---|
74 | $safesearch = 1; // valeur par defaut |
---|
75 | |
---|
76 | // etape 1: recuperer une image donnee |
---|
77 | if ($id>0) { |
---|
78 | $id_photo_rand = $id; |
---|
79 | } else { |
---|
80 | // etape 1bis: ou recuperer une image au hasard selon nos criteres |
---|
81 | $page_rnd = rand(1, $plage); |
---|
82 | |
---|
83 | $params = array( |
---|
84 | 'api_key' => $api_key, |
---|
85 | 'method' => 'flickr.photos.search', |
---|
86 | 'tags' => $tags, |
---|
87 | 'text' => $text, |
---|
88 | 'license' => $license, |
---|
89 | 'safe_search' => $safesearch, |
---|
90 | 'format' => 'php_serial', |
---|
91 | 'per_page' => '1', |
---|
92 | 'page' => $page_rnd // page (=photo) au hasard selon la taille du pool |
---|
93 | ); |
---|
94 | |
---|
95 | $rsp_obj = fetch_data_flickr($params); |
---|
96 | |
---|
97 | if ($rsp_obj) { |
---|
98 | $photos = $rsp_obj['photos']['photo']; |
---|
99 | if (!$photos) { |
---|
100 | // pas de resultat, la plage est sans doute trop grand pour la requete |
---|
101 | // on refait une tentative en ignorant la parametre de plage |
---|
102 | $params['page'] = 1; |
---|
103 | $params['per_page'] = 100; |
---|
104 | $rsp_obj = fetch_data_flickr($params); |
---|
105 | if ($rsp_obj) { |
---|
106 | $photos = $rsp_obj['photos']['photo']; |
---|
107 | if (!$photos) |
---|
108 | return false; // vraiment pas de resultats |
---|
109 | } else { |
---|
110 | return false; |
---|
111 | } |
---|
112 | } |
---|
113 | $id_photo_rand = $photos[array_rand($rsp_obj['photos']['photo'], 1)]['id']; // on prend une image au hasard |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | // etape 2: chercher l'image et l'afficher |
---|
119 | $params = array( |
---|
120 | 'api_key' => $api_key, |
---|
121 | 'method' => 'flickr.photos.getInfo', |
---|
122 | 'photo_id' => $id_photo_rand, |
---|
123 | 'format' => 'php_serial', |
---|
124 | ); |
---|
125 | |
---|
126 | $rsp_obj = fetch_data_flickr($params); |
---|
127 | |
---|
128 | if ($rsp_obj){ |
---|
129 | $_photo_rand_title = @$rsp_obj['photo']['title']['_content']; |
---|
130 | $_photo_rand_owner = @$rsp_obj['photo']['owner']['username']; |
---|
131 | $_photo_rand_licence = @$rsp_obj['photo']['license']; |
---|
132 | $_photo_rand_url_page = @$rsp_obj['photo']['urls']['url'][0]['_content']; |
---|
133 | |
---|
134 | // si l'auteur est ds liste noire |
---|
135 | // on refait une recherche |
---|
136 | if (in_array($_photo_rand_owner, $blacklist) && strlen($_photo_rand_owner)>0) { |
---|
137 | if ($iteration<10) { |
---|
138 | return flickr_rand($str,$tags,$license,$align,$size,$safesearch,$id,$text,$texte,++$iteration); |
---|
139 | } else { |
---|
140 | return "<!-- flickr_rand: no match -->"; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | // doc licence: http://www.flickr.com/services/api/flickr.photos.licenses.getInfo.html |
---|
145 | switch ($_photo_rand_licence) { |
---|
146 | case 4: $license_name = "Creative Commons BY"; $licence_url = "http://creativecommons.org/licenses/by/2.0/"; break; |
---|
147 | case 6: $license_name = "Creative Commons BY-ND"; $licence_url = "http://creativecommons.org/licenses/by-nd/2.0/"; break; |
---|
148 | case 3: $license_name = "Creative Commons BY-NC-ND"; $licence_url = "http://creativecommons.org/licenses/by-nc-nd/2.0/"; break; |
---|
149 | case 2: $license_name = "Creative Commons BY-NC"; $licence_url = "http://creativecommons.org/licenses/by-nc/2.0/"; break; |
---|
150 | case 1: $license_name = "Creative Commons BY-NC-SA"; $licence_url = "http://creativecommons.org/licenses/by-nc-sa/2.0/"; break; |
---|
151 | case 5: $license_name = "Creative Commons BY-SA"; $licence_url = "http://creativecommons.org/licenses/by-sa/2.0/"; break; |
---|
152 | case 7: $license_name = "No known copyright restrictions"; $licence_url = "http://flickr.com/commons/usage/"; break; |
---|
153 | case 0: $license_name = "Copyright"; $licence_url = "http://www.flickr.com/help/general/#147"; break; |
---|
154 | default: $license_name = "Unknown License"; $licence_url = "http://www.flickr.com/help/general/#147"; break; |
---|
155 | } |
---|
156 | |
---|
157 | //echo "Title is $_photo_rand_title - $_photo_rand_owner - :::: $_photo_rand_licence ::: !"; |
---|
158 | |
---|
159 | // etape 3: recuperer la taille requise la plus proche |
---|
160 | $params = array( |
---|
161 | 'api_key' => $api_key, |
---|
162 | 'method' => 'flickr.photos.getSizes', |
---|
163 | 'photo_id' => $id_photo_rand, |
---|
164 | 'format' => 'php_serial', |
---|
165 | ); |
---|
166 | $rsp_obj = fetch_data_flickr($params); |
---|
167 | if ($rsp_obj){ |
---|
168 | foreach($rsp_obj['sizes']['size'] as $rsp_size) { |
---|
169 | $_photo_rand_url = $rsp_size['source']; |
---|
170 | $_photo_rand_width = $rsp_size['width']; |
---|
171 | if ($rsp_size['label']==$size) |
---|
172 | break; |
---|
173 | } |
---|
174 | |
---|
175 | if ($_photo_rand_url) { |
---|
176 | $output = "<dl class='flick_rand flickr_".strtolower($size)." spip_documents_$align' style='width:".$_photo_rand_width."px'>\n |
---|
177 | <dt><a href='$_photo_rand_url_page'><img src='$_photo_rand_url' alt='flickr' /></a></dt>\n"; |
---|
178 | if ($_photo_rand_title) |
---|
179 | $output .= "<dt class='spip_doc_titre'><strong>$_photo_rand_title</strong><dt>\n"; |
---|
180 | $output .= "<dt class='spip_doc_descriptif'>par $_photo_rand_owner<dt>\n |
---|
181 | <dt class='spip_doc_licence'><a href='$licence_url' rel='license'>$license_name</a><dt>\n |
---|
182 | </dl>"; |
---|
183 | return $output; |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | ?> |
---|