1 | <?php |
---|
2 | if (!defined('FLICKR_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | set_time_limit(600); |
---|
5 | |
---|
6 | include_once(PICASA_WA_PATH . 'include/functions.inc.php'); |
---|
7 | |
---|
8 | // check API parameters and connect to flickr |
---|
9 | if ( empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']) ) |
---|
10 | { |
---|
11 | array_push($page['warnings'], l10n('Please fill your API keys on the configuration tab')); |
---|
12 | $_GET['action'] = 'error'; |
---|
13 | } |
---|
14 | else if ( !test_remote_download() ) |
---|
15 | { |
---|
16 | array_push($page['errors'], l10n('No download method available')); |
---|
17 | $_GET['action'] = 'error'; |
---|
18 | } |
---|
19 | else |
---|
20 | { |
---|
21 | // init flickr API |
---|
22 | include_once(FLICKR_PATH . 'include/phpFlickr/phpFlickr.php'); |
---|
23 | $flickr = new phpFlickr($conf['flickr2piwigo']['api_key'], $conf['flickr2piwigo']['secret_key']); |
---|
24 | $flickr->enableCache('fs', FLICKR_FS_CACHE); |
---|
25 | |
---|
26 | // must authenticate |
---|
27 | $u = $flickr->test_login(); |
---|
28 | if ( ($u === false or empty($_SESSION['phpFlickr_auth_token'])) and @$_GET['action']!='login' ) |
---|
29 | { |
---|
30 | $_GET['action'] = 'init_login'; |
---|
31 | } |
---|
32 | |
---|
33 | // generate token after authentication |
---|
34 | if (!empty($_GET['frob'])) |
---|
35 | { |
---|
36 | $flickr->auth_getToken($_GET['frob']); |
---|
37 | $_GET['action'] = 'logued'; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | if (!isset($_GET['action'])) $_GET['action'] = 'main'; |
---|
43 | |
---|
44 | |
---|
45 | switch ($_GET['action']) |
---|
46 | { |
---|
47 | // button to login page |
---|
48 | case 'init_login': |
---|
49 | { |
---|
50 | $template->assign('flickr_login', FLICKR_ADMIN . '-import&action=login'); |
---|
51 | break; |
---|
52 | } |
---|
53 | |
---|
54 | // call flickr login procedure |
---|
55 | case 'login': |
---|
56 | { |
---|
57 | $flickr->auth('read', false); |
---|
58 | break; |
---|
59 | } |
---|
60 | |
---|
61 | // message after login |
---|
62 | case 'logued': |
---|
63 | { |
---|
64 | $_SESSION['page_infos'][] = l10n('Successfully logged in to you Flickr account'); |
---|
65 | redirect(FLICKR_ADMIN . '-import'); |
---|
66 | break; |
---|
67 | } |
---|
68 | |
---|
69 | // logout |
---|
70 | case 'logout': |
---|
71 | { |
---|
72 | unset($_SESSION['phpFlickr_auth_token']); |
---|
73 | $_SESSION['page_infos'][] = l10n('Logged out'); |
---|
74 | redirect(FLICKR_ADMIN . '-import'); |
---|
75 | break; |
---|
76 | } |
---|
77 | |
---|
78 | // main menu |
---|
79 | case 'main': |
---|
80 | { |
---|
81 | $u = $flickr->people_getInfo($u['id']); |
---|
82 | $template->assign(array( |
---|
83 | 'username' => $u['username'], |
---|
84 | 'profile_url' => $u['profileurl'], |
---|
85 | 'logout_url' => FLICKR_ADMIN . '-import&action=logout', |
---|
86 | 'list_albums_url' => FLICKR_ADMIN . '-import&action=list_albums', |
---|
87 | 'import_all_url' => FLICKR_ADMIN . '-import&action=list_all', |
---|
88 | )); |
---|
89 | break; |
---|
90 | } |
---|
91 | |
---|
92 | // list user albums |
---|
93 | case 'list_albums': |
---|
94 | { |
---|
95 | // all albums |
---|
96 | $albums = $flickr->photosets_getList($u['id']); |
---|
97 | $total_albums = $albums['total']; |
---|
98 | $albums = $albums['photoset']; |
---|
99 | |
---|
100 | foreach ($albums as &$album) |
---|
101 | { |
---|
102 | $album['U_LIST'] = FLICKR_ADMIN . '-import&action=list_photos&album='.$album['id']; |
---|
103 | } |
---|
104 | unset($album); |
---|
105 | |
---|
106 | // not classed |
---|
107 | $wo_albums = $flickr->photos_getNotInSet(NULL, NULL, NULL, NULL, 'photos', NULL, NULL, 1); |
---|
108 | if ($wo_albums['photos']['total'] > 0) |
---|
109 | { |
---|
110 | array_push($albums, array( |
---|
111 | 'id' => 'not_in_set', |
---|
112 | 'title' => l10n('Pictures without album'), |
---|
113 | 'description' => null, |
---|
114 | 'photos' => $wo_albums['photos']['total'], |
---|
115 | 'U_LIST' => FLICKR_ADMIN . '-import&action=list_photos&album=not_in_set', |
---|
116 | )); |
---|
117 | } |
---|
118 | |
---|
119 | $template->assign(array( |
---|
120 | 'total_albums' => $total_albums, |
---|
121 | 'albums' => $albums, |
---|
122 | )); |
---|
123 | break; |
---|
124 | } |
---|
125 | |
---|
126 | // list photos of an album |
---|
127 | case 'list_photos': |
---|
128 | { |
---|
129 | $self_url = FLICKR_ADMIN . '-import&action=list_photos&album='.$_GET['album']; |
---|
130 | $flickr_prefix = 'flickr-'.$u['username'].'-'; |
---|
131 | $flickr_root_url = $flickr->urls_getUserPhotos($u['id']); |
---|
132 | |
---|
133 | // pagination |
---|
134 | if (isset($_GET['start'])) $page['start'] = intval($_GET['start']); |
---|
135 | else $page['start'] = 0; |
---|
136 | if (isset($_GET['display'])) $page['display'] = $_GET['display']=='all' ? 500 : intval($_GET['display']); |
---|
137 | else $page['display'] = 20; |
---|
138 | |
---|
139 | // get photos |
---|
140 | if ($_GET['album'] == 'not_in_set') |
---|
141 | { |
---|
142 | $all_photos = $flickr->photos_getNotInSet(NULL, NULL, NULL, NULL, 'photos', NULL, NULL, 500); |
---|
143 | $all_photos = $all_photos['photos']['photo']; |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | $all_photos = $flickr->photosets_getPhotos($_GET['album'], NULL, NULL, 500, NULL, 'photos'); |
---|
148 | $all_photos = $all_photos['photoset']['photo']; |
---|
149 | } |
---|
150 | |
---|
151 | // get existing photos |
---|
152 | $query = ' |
---|
153 | SELECT id, file |
---|
154 | FROM '.IMAGES_TABLE.' |
---|
155 | WHERE file LIKE "'.$flickr_prefix.'%" |
---|
156 | ;'; |
---|
157 | $existing_photos = simple_hash_from_query($query, 'id', 'file'); |
---|
158 | $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos); |
---|
159 | |
---|
160 | // remove existing photos |
---|
161 | $duplicates = 0; |
---|
162 | foreach ($all_photos as $i => $photo) |
---|
163 | { |
---|
164 | if (in_array($photo['id'], $existing_photos)) |
---|
165 | { |
---|
166 | unset($all_photos[$i]); |
---|
167 | $duplicates++; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | if ($duplicates>0) |
---|
172 | { |
---|
173 | array_push($page['infos'], l10n_dec('One picture is not displayed because already existing in the database.', '%d pictures are not displayed because already existing in the database.', $duplicates)); |
---|
174 | } |
---|
175 | |
---|
176 | // displayed photos |
---|
177 | $page_photos = array_slice($all_photos, $page['start'], $page['display']); |
---|
178 | $all_elements = array_map(create_function('$p', 'return \'"\'.$p["id"].\'"\';'), $all_photos); |
---|
179 | |
---|
180 | foreach ($page_photos as &$photo) |
---|
181 | { |
---|
182 | $photo['thumb'] = $flickr->buildPhotoURL($photo, "thumbnail"); |
---|
183 | $photo['src'] = $flickr->get_biggest_size($photo['id'], "medium_800"); |
---|
184 | $photo['url'] = $flickr_root_url.$photo['id']; |
---|
185 | } |
---|
186 | unset($photo); |
---|
187 | |
---|
188 | $template->assign(array( |
---|
189 | 'nb_thumbs_set' => count($all_photos), |
---|
190 | 'nb_thumbs_page' => count($page_photos), |
---|
191 | 'thumbnails' => $page_photos, |
---|
192 | 'all_elements' => $all_elements, |
---|
193 | 'album' => $_GET['album'], |
---|
194 | 'F_ACTION' => FLICKR_ADMIN.'-import&action=import_set', |
---|
195 | 'U_DISPLAY' => $self_url, |
---|
196 | )); |
---|
197 | |
---|
198 | // get piwigo categories |
---|
199 | $query = ' |
---|
200 | SELECT id, name, uppercats, global_rank |
---|
201 | FROM '.CATEGORIES_TABLE.' |
---|
202 | ;'; |
---|
203 | display_select_cat_wrapper($query, array(), 'category_parent_options'); |
---|
204 | |
---|
205 | // get navbar |
---|
206 | $nav_bar = create_navigation_bar( |
---|
207 | $self_url, |
---|
208 | count($all_elements), |
---|
209 | $page['start'], |
---|
210 | $page['display'] |
---|
211 | ); |
---|
212 | $template->assign('navbar', $nav_bar); |
---|
213 | break; |
---|
214 | } |
---|
215 | |
---|
216 | // list all photos of the user |
---|
217 | case 'list_all': |
---|
218 | { |
---|
219 | $flickr_prefix = 'flickr-'.$u['username'].'-'; |
---|
220 | |
---|
221 | // get all photos in all albums |
---|
222 | $all_albums = $flickr->photosets_getList($u['id']); |
---|
223 | $all_albums = $all_albums['photoset']; |
---|
224 | |
---|
225 | $all_photos = array(); |
---|
226 | foreach ($all_albums as $album) |
---|
227 | { |
---|
228 | $album_photos = $flickr->photosets_getPhotos($album['id'], NULL, NULL, 500, NULL, 'photos'); |
---|
229 | $album_photos = $album_photos['photoset']['photo']; |
---|
230 | |
---|
231 | foreach ($album_photos as $photo) |
---|
232 | { |
---|
233 | $all_photos[ $photo['id'] ][] = $album['title']; |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | // get existing photos |
---|
238 | $query = ' |
---|
239 | SELECT id, file |
---|
240 | FROM '.IMAGES_TABLE.' |
---|
241 | WHERE file LIKE "'.$flickr_prefix.'%" |
---|
242 | ;'; |
---|
243 | $existing_photos = simple_hash_from_query($query, 'id', 'file'); |
---|
244 | $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos); |
---|
245 | |
---|
246 | // remove existing photos |
---|
247 | $duplicates = 0; |
---|
248 | foreach ($all_photos as $id => &$photo) |
---|
249 | { |
---|
250 | if (in_array($id, $existing_photos)) |
---|
251 | { |
---|
252 | unset($all_photos[$id]); |
---|
253 | $duplicates++; |
---|
254 | } |
---|
255 | else |
---|
256 | { |
---|
257 | $photo = array( |
---|
258 | 'id' => $id, |
---|
259 | 'albums' => implode(',', $photo), |
---|
260 | ); |
---|
261 | } |
---|
262 | } |
---|
263 | unset($photo); |
---|
264 | $all_photos = array_values($all_photos); |
---|
265 | |
---|
266 | if ($duplicates>0) |
---|
267 | { |
---|
268 | array_push($page['infos'], l10n_dec('%d picture is not displayed because already existing in the database.', '%d pictures are not displayed because already existing in the database.', $duplicates)); |
---|
269 | } |
---|
270 | |
---|
271 | $template->assign(array( |
---|
272 | 'nb_elements' => count($all_photos), |
---|
273 | 'all_elements' => json_encode($all_photos), |
---|
274 | 'F_ACTION' => FLICKR_ADMIN . '-import&action=import_set', |
---|
275 | )); |
---|
276 | |
---|
277 | // get piwigo categories |
---|
278 | $query = ' |
---|
279 | SELECT id, name, uppercats, global_rank |
---|
280 | FROM '.CATEGORIES_TABLE.' |
---|
281 | ;'; |
---|
282 | display_select_cat_wrapper($query, array(), 'category_parent_options'); |
---|
283 | break; |
---|
284 | } |
---|
285 | |
---|
286 | // success message after import |
---|
287 | case 'import_set': |
---|
288 | { |
---|
289 | if (isset($_POST['done'])) |
---|
290 | { |
---|
291 | $_SESSION['page_infos'][] = sprintf(l10n('%d pictures imported'), $_POST['done']); |
---|
292 | } |
---|
293 | redirect(FLICKR_ADMIN . '-import'); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | $template->assign('ACTION', $_GET['action']); |
---|
299 | |
---|
300 | $template->set_filename('flickr2piwigo', dirname(__FILE__) . '/template/import.tpl'); |
---|
301 | |
---|
302 | ?> |
---|