[16063] | 1 | <?php |
---|
| 2 | if (!defined('FLICKR_PATH')) die('Hacking attempt!'); |
---|
| 3 | |
---|
[17313] | 4 | set_time_limit(600); |
---|
| 5 | |
---|
[19537] | 6 | include_once(FLICKR_PATH . 'include/functions.inc.php'); |
---|
[17476] | 7 | |
---|
[16063] | 8 | // check API parameters and connect to flickr |
---|
[16071] | 9 | if ( empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']) ) |
---|
[16063] | 10 | { |
---|
| 11 | array_push($page['warnings'], l10n('Please fill your API keys on the configuration tab')); |
---|
| 12 | $_GET['action'] = 'error'; |
---|
| 13 | } |
---|
[17476] | 14 | else if ( !test_remote_download() ) |
---|
[16085] | 15 | { |
---|
| 16 | array_push($page['errors'], l10n('No download method available')); |
---|
| 17 | $_GET['action'] = 'error'; |
---|
| 18 | } |
---|
[16063] | 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 |
---|
[16071] | 27 | $u = $flickr->test_login(); |
---|
| 28 | if ( ($u === false or empty($_SESSION['phpFlickr_auth_token'])) and @$_GET['action']!='login' ) |
---|
[16063] | 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 | |
---|
[16085] | 41 | |
---|
[16064] | 42 | if (!isset($_GET['action'])) $_GET['action'] = 'main'; |
---|
[16063] | 43 | |
---|
| 44 | |
---|
| 45 | switch ($_GET['action']) |
---|
| 46 | { |
---|
| 47 | // button to login page |
---|
| 48 | case 'init_login': |
---|
| 49 | { |
---|
[16071] | 50 | $template->assign('flickr_login', FLICKR_ADMIN . '-import&action=login'); |
---|
[16063] | 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 | { |
---|
[18911] | 64 | $_SESSION['page_infos'][] = l10n('Successfully logged in to you Flickr account'); |
---|
[16071] | 65 | redirect(FLICKR_ADMIN . '-import'); |
---|
[16063] | 66 | break; |
---|
| 67 | } |
---|
| 68 | |
---|
[16064] | 69 | // logout |
---|
| 70 | case 'logout': |
---|
| 71 | { |
---|
| 72 | unset($_SESSION['phpFlickr_auth_token']); |
---|
[18911] | 73 | $_SESSION['page_infos'][] = l10n('Logged out'); |
---|
[16071] | 74 | redirect(FLICKR_ADMIN . '-import'); |
---|
[16064] | 75 | break; |
---|
| 76 | } |
---|
| 77 | |
---|
[16063] | 78 | // main menu |
---|
[16064] | 79 | case 'main': |
---|
[16063] | 80 | { |
---|
[16064] | 81 | $u = $flickr->people_getInfo($u['id']); |
---|
| 82 | $template->assign(array( |
---|
[16071] | 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', |
---|
[16064] | 88 | )); |
---|
[16063] | 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 | { |
---|
[16071] | 102 | $album['U_LIST'] = FLICKR_ADMIN . '-import&action=list_photos&album='.$album['id']; |
---|
[16063] | 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'], |
---|
[16071] | 115 | 'U_LIST' => FLICKR_ADMIN . '-import&action=list_photos&album=not_in_set', |
---|
[16063] | 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 | { |
---|
[16071] | 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 |
---|
[16063] | 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 | |
---|
[16071] | 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'); |
---|
[17476] | 158 | $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos); |
---|
[16071] | 159 | |
---|
| 160 | // remove existing photos |
---|
[16063] | 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 | } |
---|
[16071] | 170 | |
---|
[16063] | 171 | if ($duplicates>0) |
---|
| 172 | { |
---|
[24815] | 173 | $page['infos'][] = '<a href="admin.php?page=batch_manager&prefilter=flickr">' |
---|
| 174 | .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) |
---|
| 175 | .'</a>'; |
---|
[16063] | 176 | } |
---|
| 177 | |
---|
| 178 | // displayed photos |
---|
| 179 | $page_photos = array_slice($all_photos, $page['start'], $page['display']); |
---|
| 180 | $all_elements = array_map(create_function('$p', 'return \'"\'.$p["id"].\'"\';'), $all_photos); |
---|
| 181 | |
---|
| 182 | foreach ($page_photos as &$photo) |
---|
| 183 | { |
---|
| 184 | $photo['thumb'] = $flickr->buildPhotoURL($photo, "thumbnail"); |
---|
| 185 | $photo['src'] = $flickr->get_biggest_size($photo['id'], "medium_800"); |
---|
| 186 | $photo['url'] = $flickr_root_url.$photo['id']; |
---|
| 187 | } |
---|
| 188 | unset($photo); |
---|
| 189 | |
---|
| 190 | $template->assign(array( |
---|
| 191 | 'nb_thumbs_set' => count($all_photos), |
---|
| 192 | 'nb_thumbs_page' => count($page_photos), |
---|
| 193 | 'thumbnails' => $page_photos, |
---|
| 194 | 'all_elements' => $all_elements, |
---|
| 195 | 'album' => $_GET['album'], |
---|
| 196 | 'F_ACTION' => FLICKR_ADMIN.'-import&action=import_set', |
---|
| 197 | 'U_DISPLAY' => $self_url, |
---|
| 198 | )); |
---|
| 199 | |
---|
| 200 | // get piwigo categories |
---|
| 201 | $query = ' |
---|
| 202 | SELECT id, name, uppercats, global_rank |
---|
| 203 | FROM '.CATEGORIES_TABLE.' |
---|
| 204 | ;'; |
---|
| 205 | display_select_cat_wrapper($query, array(), 'category_parent_options'); |
---|
| 206 | |
---|
| 207 | // get navbar |
---|
| 208 | $nav_bar = create_navigation_bar( |
---|
| 209 | $self_url, |
---|
| 210 | count($all_elements), |
---|
| 211 | $page['start'], |
---|
| 212 | $page['display'] |
---|
| 213 | ); |
---|
| 214 | $template->assign('navbar', $nav_bar); |
---|
| 215 | break; |
---|
| 216 | } |
---|
[16071] | 217 | |
---|
| 218 | // list all photos of the user |
---|
[16063] | 219 | case 'list_all': |
---|
| 220 | { |
---|
[16071] | 221 | $flickr_prefix = 'flickr-'.$u['username'].'-'; |
---|
[16063] | 222 | |
---|
| 223 | // get all photos in all albums |
---|
| 224 | $all_albums = $flickr->photosets_getList($u['id']); |
---|
| 225 | $all_albums = $all_albums['photoset']; |
---|
| 226 | |
---|
| 227 | $all_photos = array(); |
---|
[17476] | 228 | foreach ($all_albums as $album) |
---|
[16063] | 229 | { |
---|
| 230 | $album_photos = $flickr->photosets_getPhotos($album['id'], NULL, NULL, 500, NULL, 'photos'); |
---|
| 231 | $album_photos = $album_photos['photoset']['photo']; |
---|
| 232 | |
---|
[17476] | 233 | foreach ($album_photos as $photo) |
---|
[16063] | 234 | { |
---|
| 235 | $all_photos[ $photo['id'] ][] = $album['title']; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | // get existing photos |
---|
| 240 | $query = ' |
---|
| 241 | SELECT id, file |
---|
| 242 | FROM '.IMAGES_TABLE.' |
---|
| 243 | WHERE file LIKE "'.$flickr_prefix.'%" |
---|
| 244 | ;'; |
---|
| 245 | $existing_photos = simple_hash_from_query($query, 'id', 'file'); |
---|
[17476] | 246 | $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos); |
---|
[16063] | 247 | |
---|
[16071] | 248 | // remove existing photos |
---|
[16063] | 249 | $duplicates = 0; |
---|
| 250 | foreach ($all_photos as $id => &$photo) |
---|
| 251 | { |
---|
| 252 | if (in_array($id, $existing_photos)) |
---|
| 253 | { |
---|
| 254 | unset($all_photos[$id]); |
---|
| 255 | $duplicates++; |
---|
| 256 | } |
---|
| 257 | else |
---|
| 258 | { |
---|
| 259 | $photo = array( |
---|
| 260 | 'id' => $id, |
---|
| 261 | 'albums' => implode(',', $photo), |
---|
| 262 | ); |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | unset($photo); |
---|
[16071] | 266 | $all_photos = array_values($all_photos); |
---|
| 267 | |
---|
[16063] | 268 | if ($duplicates>0) |
---|
| 269 | { |
---|
[24815] | 270 | $page['infos'][] = '<a href="admin.php?page=batch_manager&prefilter=flickr">' |
---|
| 271 | .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) |
---|
| 272 | .'</a>'; |
---|
[16063] | 273 | } |
---|
| 274 | |
---|
| 275 | $template->assign(array( |
---|
| 276 | 'nb_elements' => count($all_photos), |
---|
| 277 | 'all_elements' => json_encode($all_photos), |
---|
[16071] | 278 | 'F_ACTION' => FLICKR_ADMIN . '-import&action=import_set', |
---|
[16063] | 279 | )); |
---|
| 280 | |
---|
| 281 | // get piwigo categories |
---|
| 282 | $query = ' |
---|
| 283 | SELECT id, name, uppercats, global_rank |
---|
| 284 | FROM '.CATEGORIES_TABLE.' |
---|
| 285 | ;'; |
---|
| 286 | display_select_cat_wrapper($query, array(), 'category_parent_options'); |
---|
| 287 | break; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | // success message after import |
---|
| 291 | case 'import_set': |
---|
| 292 | { |
---|
| 293 | if (isset($_POST['done'])) |
---|
| 294 | { |
---|
| 295 | $_SESSION['page_infos'][] = sprintf(l10n('%d pictures imported'), $_POST['done']); |
---|
| 296 | } |
---|
[16071] | 297 | redirect(FLICKR_ADMIN . '-import'); |
---|
[16063] | 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
[16071] | 301 | |
---|
[25789] | 302 | $template->assign(array( |
---|
| 303 | 'ACTION' => $_GET['action'], |
---|
| 304 | 'GMAPS_LOADED' => !empty($pwg_loaded_plugins['rv_gmaps']) || !empty($pwg_loaded_plugins['piwigo-openstreetmap']), |
---|
| 305 | )); |
---|
[16063] | 306 | |
---|
[16071] | 307 | $template->set_filename('flickr2piwigo', dirname(__FILE__) . '/template/import.tpl'); |
---|
[16063] | 308 | |
---|
| 309 | ?> |
---|