source: extensions/flickr2piwigo/admin/import.php @ 26180

Last change on this file since 26180 was 26180, checked in by mistic100, 10 years ago

update for piwigo 2.6

File size: 8.6 KB
RevLine 
[16063]1<?php
[26180]2defined('FLICKR_PATH') or die('Hacking attempt!');
[16063]3
[17313]4set_time_limit(600);
5
[19537]6include_once(FLICKR_PATH . 'include/functions.inc.php');
[17476]7
[16063]8// check API parameters and connect to flickr
[26180]9if (empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']))
[16063]10{
[26180]11  $page['warnings'][] = l10n('Please fill your API keys on the configuration tab');
[16063]12  $_GET['action'] = 'error';
13}
[26180]14else if (!test_remote_download())
[16085]15{
[26180]16  $page['errors'][] = l10n('No download method available');
[16085]17  $_GET['action'] = 'error';
18}
[16063]19else
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);
[26180]25
[16063]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  }
[26180]32
[16063]33  // generate token after authentication
34  if (!empty($_GET['frob']))
35  {
36    $flickr->auth_getToken($_GET['frob']);
[26180]37    $_GET['action'] = 'logged';
[16063]38  }
39}
40
[16085]41
[26180]42if (!isset($_GET['action']))
43{
44  $_GET['action'] = 'main';
45}
[16063]46
47
48switch ($_GET['action'])
49{
50  // button to login page
51  case 'init_login':
52  {
[16071]53    $template->assign('flickr_login', FLICKR_ADMIN . '-import&amp;action=login');
[16063]54    break;
55  }
[26180]56
[16063]57  // call flickr login procedure
58  case 'login':
59  {
60    $flickr->auth('read', false);
61    break;
62  }
[26180]63
[16063]64  // message after login
[26180]65  case 'logged':
[16063]66  {
[18911]67    $_SESSION['page_infos'][] = l10n('Successfully logged in to you Flickr account');
[16071]68    redirect(FLICKR_ADMIN . '-import');
[16063]69    break;
70  }
[26180]71
[16064]72  // logout
73  case 'logout':
74  {
75    unset($_SESSION['phpFlickr_auth_token']);
[18911]76    $_SESSION['page_infos'][] = l10n('Logged out');
[16071]77    redirect(FLICKR_ADMIN . '-import');
[16064]78    break;
79  }
[26180]80
[16063]81  // main menu
[16064]82  case 'main':
[16063]83  {
[16064]84    $u = $flickr->people_getInfo($u['id']);
85    $template->assign(array(
[16071]86      'username' => $u['username'],
87      'profile_url' => $u['profileurl'],
88      'logout_url' => FLICKR_ADMIN . '-import&amp;action=logout',
89      'list_albums_url' => FLICKR_ADMIN . '-import&amp;action=list_albums',
90      'import_all_url' => FLICKR_ADMIN . '-import&amp;action=list_all',
[16064]91      ));
[16063]92    break;
93  }
[26180]94
[16063]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'];
[26180]102
[16063]103    foreach ($albums as &$album)
104    {
[16071]105      $album['U_LIST'] = FLICKR_ADMIN . '-import&amp;action=list_photos&amp;album='.$album['id'];
[16063]106    }
107    unset($album);
[26180]108
[16063]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    {
[26180]113      $albums[] = array(
[16063]114        'id' => 'not_in_set',
115        'title' => l10n('Pictures without album'),
116        'description' => null,
117        'photos' => $wo_albums['photos']['total'],
[16071]118        'U_LIST' => FLICKR_ADMIN . '-import&amp;action=list_photos&amp;album=not_in_set',
[26180]119        );
[16063]120    }
[26180]121
[16063]122    $template->assign(array(
123      'total_albums' => $total_albums,
124      'albums' => $albums,
125      ));
126    break;
127  }
[26180]128
[16063]129  // list photos of an album
130  case 'list_photos':
131  {
[16071]132    $self_url = FLICKR_ADMIN . '-import&amp;action=list_photos&amp;album='.$_GET['album'];
133    $flickr_prefix = 'flickr-'.$u['username'].'-';
134    $flickr_root_url = $flickr->urls_getUserPhotos($u['id']);
[26180]135
[16071]136    // pagination
[16063]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;
[26180]141
[16063]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    }
[26180]153
[16071]154    // get existing photos
155    $query = '
156SELECT id, file
157  FROM '.IMAGES_TABLE.'
158  WHERE file LIKE "'.$flickr_prefix.'%"
159;';
160    $existing_photos = simple_hash_from_query($query, 'id', 'file');
[17476]161    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
[26180]162
[16071]163    // remove existing photos
[16063]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    }
[26180]173
[16063]174    if ($duplicates>0)
175    {
[26180]176      $page['infos'][] = '<a href="admin.php?page=batch_manager&amp;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)
[24815]181        .'</a>';
[16063]182    }
[26180]183
[16063]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);
[26180]187
[16063]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);
[26180]195
[16063]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&amp;action=import_set',
203      'U_DISPLAY' => $self_url,
204      ));
[26180]205
[16063]206    // get piwigo categories
207    $query = '
208SELECT id, name, uppercats, global_rank
209  FROM '.CATEGORIES_TABLE.'
210;';
211    display_select_cat_wrapper($query, array(), 'category_parent_options');
[26180]212
[16063]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  }
[26180]223
[16071]224  // list all photos of the user
[16063]225  case 'list_all':
226  {
[16071]227    $flickr_prefix = 'flickr-'.$u['username'].'-';
[26180]228
[16063]229    // get all photos in all albums
230    $all_albums = $flickr->photosets_getList($u['id']);
231    $all_albums = $all_albums['photoset'];
[26180]232
[16063]233    $all_photos = array();
[17476]234    foreach ($all_albums as $album)
[16063]235    {
236      $album_photos = $flickr->photosets_getPhotos($album['id'], NULL, NULL, 500, NULL, 'photos');
237      $album_photos = $album_photos['photoset']['photo'];
[26180]238
[17476]239      foreach ($album_photos as $photo)
[16063]240      {
241        $all_photos[ $photo['id'] ][] = $album['title'];
242      }
243    }
[26180]244
[16063]245    // get existing photos
246    $query = '
247SELECT id, file
248  FROM '.IMAGES_TABLE.'
249  WHERE file LIKE "'.$flickr_prefix.'%"
250;';
251    $existing_photos = simple_hash_from_query($query, 'id', 'file');
[17476]252    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
[26180]253
[16071]254    // remove existing photos
[16063]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);
[16071]272    $all_photos = array_values($all_photos);
[26180]273
[16063]274    if ($duplicates>0)
275    {
[26180]276      $page['infos'][] = '<a href="admin.php?page=batch_manager&amp;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)
[24815]281        .'</a>';
[16063]282    }
[26180]283
[16063]284    $template->assign(array(
285      'nb_elements' => count($all_photos),
[26180]286      'all_elements' => $all_photos,
[16071]287      'F_ACTION' => FLICKR_ADMIN . '-import&amp;action=import_set',
[16063]288      ));
[26180]289
[16063]290    // get piwigo categories
291    $query = '
292SELECT id, name, uppercats, global_rank
293  FROM '.CATEGORIES_TABLE.'
294;';
295    display_select_cat_wrapper($query, array(), 'category_parent_options');
296    break;
297  }
[26180]298
[16063]299  // success message after import
300  case 'import_set':
301  {
302    if (isset($_POST['done']))
303    {
[26180]304      $_SESSION['page_infos'][] = l10n('%d pictures imported', $_POST['done']);
[16063]305    }
[16071]306    redirect(FLICKR_ADMIN . '-import');
[16063]307  }
308}
309
[16071]310
[26180]311$template->assign('ACTION', $_GET['action']);
[16063]312
[26180]313$template->set_filename('flickr2piwigo', realpath(FLICKR_PATH . 'admin/template/import.tpl'));
Note: See TracBrowser for help on using the repository browser.