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