[17475] | 1 | <?php |
---|
| 2 | if (!defined('PICASA_WA_PATH')) die('Hacking attempt!'); |
---|
| 3 | |
---|
| 4 | set_time_limit(600); |
---|
| 5 | |
---|
| 6 | include_once(PICASA_WA_PATH . 'include/functions.inc.php'); |
---|
| 7 | |
---|
| 8 | if ( !test_remote_download() ) |
---|
| 9 | { |
---|
| 10 | array_push($page['errors'], l10n('No download method available')); |
---|
| 11 | $_GET['action'] = 'error'; |
---|
| 12 | } |
---|
| 13 | else |
---|
| 14 | { |
---|
| 15 | // init Gdata API |
---|
| 16 | set_include_path(get_include_path() . PATH_SEPARATOR . PICASA_WA_PATH.'include'); |
---|
| 17 | require_once('Zend/Loader.php'); |
---|
| 18 | Zend_Loader::loadClass('Zend_Gdata_AuthSub'); |
---|
| 19 | Zend_Loader::loadClass('Zend_Gdata_Photos'); |
---|
| 20 | |
---|
| 21 | // generate token after authentication |
---|
| 22 | if (!empty($_GET['token'])) |
---|
| 23 | { |
---|
| 24 | $_SESSION['gdata_auth_token'] = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']); |
---|
[20670] | 25 | $_GET['action'] = 'logged'; |
---|
[17475] | 26 | } |
---|
| 27 | |
---|
| 28 | // authentication |
---|
| 29 | if (empty($_SESSION['gdata_auth_token'])) |
---|
| 30 | { |
---|
| 31 | $_GET['action'] = 'login'; |
---|
| 32 | } |
---|
| 33 | else |
---|
| 34 | { |
---|
| 35 | $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['gdata_auth_token']); |
---|
| 36 | $picasa = new Zend_Gdata_Photos($client, "Piwigo-Google2Piwigo-1.0"); |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | if (!isset($_GET['action'])) $_GET['action'] = 'main'; |
---|
| 42 | |
---|
| 43 | switch ($_GET['action']) |
---|
| 44 | { |
---|
| 45 | // button to login page |
---|
| 46 | case 'login': |
---|
| 47 | { |
---|
| 48 | $login_url = Zend_Gdata_AuthSub::getAuthSubTokenUri( |
---|
[20670] | 49 | get_absolute_root_url() . PICASA_WA_ADMIN . '-import', |
---|
[17475] | 50 | 'https://picasaweb.google.com/data', |
---|
| 51 | false, true |
---|
| 52 | ); |
---|
| 53 | |
---|
[20645] | 54 | $template->assign(array( |
---|
| 55 | 'picasa_login' => $login_url, |
---|
| 56 | 'HELP_CONTENT' => load_language('help.lang.html', PICASA_WA_PATH, array('return'=>true)), |
---|
| 57 | )); |
---|
[17475] | 58 | break; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // message after login |
---|
[20670] | 62 | case 'logged': |
---|
[17475] | 63 | { |
---|
[20670] | 64 | $_SESSION['page_infos'][] = l10n('Successfully logged to you Google account'); |
---|
[17475] | 65 | redirect(PICASA_WA_ADMIN . '-import'); |
---|
| 66 | break; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | // logout |
---|
| 70 | case 'logout': |
---|
| 71 | { |
---|
| 72 | Zend_Gdata_AuthSub::AuthSubRevokeToken( |
---|
| 73 | $_SESSION['gdata_auth_token'], |
---|
| 74 | $client |
---|
| 75 | ); |
---|
| 76 | unset($_SESSION['gdata_auth_token']); |
---|
| 77 | |
---|
[18912] | 78 | $_SESSION['page_infos'][] = l10n('Logged out'); |
---|
[17475] | 79 | redirect(PICASA_WA_ADMIN . '-import'); |
---|
| 80 | break; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // main menu |
---|
| 84 | case 'main': |
---|
| 85 | { |
---|
| 86 | $template->assign(array( |
---|
[20670] | 87 | 'username' => $picasa->getUserEntry( $picasa->newUserQuery() )->getGphotoNickname()->getText(), |
---|
| 88 | 'logout_url' => PICASA_WA_ADMIN . '-import&action=logout', |
---|
[17475] | 89 | 'list_albums_url' => PICASA_WA_ADMIN . '-import&action=list_albums', |
---|
[20670] | 90 | 'import_all_url' => PICASA_WA_ADMIN . '-import&action=list_all', |
---|
[17475] | 91 | )); |
---|
| 92 | break; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | // list user albums |
---|
| 96 | case 'list_albums': |
---|
| 97 | { |
---|
| 98 | // get all albums |
---|
| 99 | $userFeed = $picasa->getUserFeed("default"); |
---|
| 100 | |
---|
| 101 | $albums = array(); |
---|
| 102 | foreach ($userFeed as $userEntry) |
---|
| 103 | { |
---|
| 104 | array_push($albums, array( |
---|
[20670] | 105 | 'title' => $userEntry->title->text, |
---|
[17475] | 106 | 'description' => $userEntry->mediaGroup->description->text, |
---|
[20670] | 107 | 'photos' => $userEntry->gphotoNumPhotos->text, |
---|
| 108 | 'U_LIST' => PICASA_WA_ADMIN . '-import&action=list_photos&album=' . $userEntry->gphotoId->text, |
---|
[17475] | 109 | )); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | $template->assign(array( |
---|
| 113 | 'total_albums' => count($albums), |
---|
| 114 | 'albums' => $albums, |
---|
| 115 | )); |
---|
| 116 | break; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | // list photos of an album |
---|
| 120 | case 'list_photos': |
---|
| 121 | { |
---|
| 122 | $self_url = PICASA_WA_ADMIN . '-import&action=list_photos&album='.$_GET['album']; |
---|
| 123 | $picasa_prefix = 'picasa-'; |
---|
| 124 | |
---|
| 125 | // pagination |
---|
| 126 | if (isset($_GET['start'])) $page['start'] = intval($_GET['start']); |
---|
| 127 | else $page['start'] = 0; |
---|
| 128 | if (isset($_GET['display'])) $page['display'] = $_GET['display']=='all' ? 500 : intval($_GET['display']); |
---|
| 129 | else $page['display'] = 20; |
---|
| 130 | |
---|
| 131 | // get photos |
---|
| 132 | $query = $picasa->newAlbumQuery(); |
---|
| 133 | $query->setUser('default'); |
---|
| 134 | $query->setAlbumId($_GET['album']); |
---|
| 135 | $query->setImgMax('800'); |
---|
| 136 | $albumFeed = $picasa->getAlbumFeed($query); |
---|
| 137 | |
---|
| 138 | $all_photos = array(); |
---|
| 139 | foreach ($albumFeed as $albumEntry) |
---|
| 140 | { |
---|
| 141 | array_push($all_photos, array( |
---|
[20670] | 142 | 'id' => $albumEntry->getGphotoId()->getText(), |
---|
| 143 | 'name' => $albumEntry->mediaGroup->title->text, |
---|
[17475] | 144 | 'thumb' => $albumEntry->mediaGroup->thumbnail[1]->url, |
---|
[20670] | 145 | 'src' => $albumEntry->mediaGroup->content[0]->url, |
---|
| 146 | 'url' => $albumEntry->link[2]->href, |
---|
[17475] | 147 | )); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | // get existing photos |
---|
| 151 | $query = ' |
---|
| 152 | SELECT id, file |
---|
| 153 | FROM '.IMAGES_TABLE.' |
---|
| 154 | WHERE file LIKE "'.$picasa_prefix.'%" |
---|
| 155 | ;'; |
---|
| 156 | $existing_photos = simple_hash_from_query($query, 'id', 'file'); |
---|
| 157 | $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$picasa_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos); |
---|
| 158 | |
---|
| 159 | // remove existing photos |
---|
| 160 | $duplicates = 0; |
---|
| 161 | foreach ($all_photos as $i => $photo) |
---|
| 162 | { |
---|
| 163 | if (in_array($photo['id'], $existing_photos)) |
---|
| 164 | { |
---|
| 165 | unset($all_photos[$i]); |
---|
| 166 | $duplicates++; |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | if ($duplicates>0) |
---|
| 171 | { |
---|
[20670] | 172 | array_push($page['infos'], l10n_dec( |
---|
| 173 | 'One picture is not displayed because already existing in the database.', |
---|
| 174 | '%d pictures are not displayed because already existing in the database.', |
---|
| 175 | $duplicates |
---|
| 176 | )); |
---|
[17475] | 177 | } |
---|
| 178 | |
---|
| 179 | // displayed photos |
---|
| 180 | $page_photos = array_slice($all_photos, $page['start'], $page['display']); |
---|
| 181 | $all_elements = array_map(create_function('$p', 'return \'"\'.$p["id"].\'"\';'), $all_photos); |
---|
| 182 | |
---|
| 183 | $template->assign(array( |
---|
[20670] | 184 | 'nb_thumbs_set' => count($all_photos), |
---|
[17475] | 185 | 'nb_thumbs_page' => count($page_photos), |
---|
[20670] | 186 | 'thumbnails' => $page_photos, |
---|
| 187 | 'all_elements' => $all_elements, |
---|
| 188 | 'album' => $_GET['album'], |
---|
| 189 | 'F_ACTION' => PICASA_WA_ADMIN.'-import&action=import_set', |
---|
| 190 | 'U_DISPLAY' => $self_url, |
---|
[17475] | 191 | )); |
---|
| 192 | |
---|
| 193 | // get piwigo categories |
---|
| 194 | $query = ' |
---|
| 195 | SELECT id, name, uppercats, global_rank |
---|
| 196 | FROM '.CATEGORIES_TABLE.' |
---|
| 197 | ;'; |
---|
| 198 | display_select_cat_wrapper($query, array(), 'category_parent_options'); |
---|
| 199 | |
---|
| 200 | // get navbar |
---|
| 201 | $nav_bar = create_navigation_bar( |
---|
| 202 | $self_url, |
---|
| 203 | count($all_elements), |
---|
| 204 | $page['start'], |
---|
| 205 | $page['display'] |
---|
| 206 | ); |
---|
| 207 | $template->assign('navbar', $nav_bar); |
---|
| 208 | break; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | // list all photos of the user |
---|
| 212 | case 'list_all': |
---|
| 213 | { |
---|
| 214 | $picasa_prefix = 'picasa-'; |
---|
| 215 | |
---|
| 216 | // get all photos in all albums |
---|
| 217 | $userFeed = $picasa->getUserFeed("default"); |
---|
| 218 | |
---|
| 219 | $all_photos = array(); |
---|
| 220 | foreach ($userFeed as $userEntry) |
---|
| 221 | { |
---|
| 222 | $query = $picasa->newAlbumQuery(); |
---|
| 223 | $query->setUser('default'); |
---|
| 224 | $query->setAlbumId( $userEntry->gphotoId->text ); |
---|
| 225 | $albumFeed = $picasa->getAlbumFeed($query); |
---|
| 226 | |
---|
| 227 | foreach ($albumFeed as $albumEntry) |
---|
| 228 | { |
---|
| 229 | $all_photos[ $albumEntry->getGphotoId()->getText() ] = $userEntry->gphotoId->text; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | // get existing photos |
---|
| 234 | $query = ' |
---|
| 235 | SELECT id, file |
---|
| 236 | FROM '.IMAGES_TABLE.' |
---|
| 237 | WHERE file LIKE "'.$picasa_prefix.'%" |
---|
| 238 | ;'; |
---|
| 239 | $existing_photos = simple_hash_from_query($query, 'id', 'file'); |
---|
| 240 | $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$picasa_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos); |
---|
| 241 | |
---|
| 242 | // remove existing photos |
---|
| 243 | $duplicates = 0; |
---|
| 244 | foreach ($all_photos as $id => &$photo) |
---|
| 245 | { |
---|
| 246 | if (in_array($id, $existing_photos)) |
---|
| 247 | { |
---|
| 248 | unset($all_photos[$id]); |
---|
| 249 | $duplicates++; |
---|
| 250 | } |
---|
| 251 | else |
---|
| 252 | { |
---|
| 253 | $photo = array( |
---|
| 254 | 'id' => $id, |
---|
| 255 | 'album' => $photo, |
---|
| 256 | ); |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | unset($photo); |
---|
| 260 | $all_photos = array_values($all_photos); |
---|
| 261 | |
---|
| 262 | if ($duplicates>0) |
---|
| 263 | { |
---|
[20670] | 264 | array_push($page['infos'], l10n_dec( |
---|
| 265 | '%d picture is not displayed because already existing in the database.', |
---|
| 266 | '%d pictures are not displayed because already existing in the database.', |
---|
| 267 | $duplicates |
---|
| 268 | )); |
---|
[17475] | 269 | } |
---|
| 270 | |
---|
| 271 | $template->assign(array( |
---|
[20670] | 272 | 'nb_elements' => count($all_photos), |
---|
[17475] | 273 | 'all_elements' => json_encode($all_photos), |
---|
[20670] | 274 | 'F_ACTION' => PICASA_WA_ADMIN . '-import&action=import_set', |
---|
[17475] | 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(PICASA_WA_ADMIN . '-import'); |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | |
---|
[20670] | 298 | $template->assign(array( |
---|
| 299 | 'ACTION' => $_GET['action'], |
---|
| 300 | 'GMAPS_LOADED' => !empty($pwg_loaded_plugins['rv_gmaps']), |
---|
| 301 | )); |
---|
[17475] | 302 | |
---|
[20670] | 303 | $template->set_filename('picasa_web_albums', realpath(PICASA_WA_PATH . '/admin/template/import.tpl')); |
---|
[17475] | 304 | |
---|
| 305 | ?> |
---|