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

Last change on this file since 16071 was 16071, checked in by mistic100, 12 years ago

code cleanup, don't ask username anymore (prohibit importing photos from another account)

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