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

Last change on this file since 24815 was 24815, checked in by mistic100, 11 years ago

fix bug with quotes, add batch manager prefilter

File size: 8.7 KB
Line 
1<?php
2if (!defined('FLICKR_PATH')) die('Hacking attempt!');
3
4set_time_limit(600);
5
6include_once(FLICKR_PATH . 'include/functions.inc.php');
7
8// check API parameters and connect to flickr
9if ( empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']) )
10{
11  array_push($page['warnings'], l10n('Please fill your API keys on the configuration tab'));
12  $_GET['action'] = 'error';
13}
14else if ( !test_remote_download() )
15{
16  array_push($page['errors'], l10n('No download method available'));
17  $_GET['action'] = 'error';
18}
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);
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'] = 'logued';
38  }
39}
40
41
42if (!isset($_GET['action'])) $_GET['action'] = 'main';
43
44
45switch ($_GET['action'])
46{
47  // button to login page
48  case 'init_login':
49  {
50    $template->assign('flickr_login', FLICKR_ADMIN . '-import&amp;action=login');
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  {
64    $_SESSION['page_infos'][] = l10n('Successfully logged in to you Flickr account');
65    redirect(FLICKR_ADMIN . '-import');
66    break;
67  }
68 
69  // logout
70  case 'logout':
71  {
72    unset($_SESSION['phpFlickr_auth_token']);
73    $_SESSION['page_infos'][] = l10n('Logged out');
74    redirect(FLICKR_ADMIN . '-import');
75    break;
76  }
77 
78  // main menu
79  case 'main':
80  {
81    $u = $flickr->people_getInfo($u['id']);
82    $template->assign(array(
83      'username' => $u['username'],
84      'profile_url' => $u['profileurl'],
85      'logout_url' => FLICKR_ADMIN . '-import&amp;action=logout',
86      'list_albums_url' => FLICKR_ADMIN . '-import&amp;action=list_albums',
87      'import_all_url' => FLICKR_ADMIN . '-import&amp;action=list_all',
88      ));
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    {
102      $album['U_LIST'] = FLICKR_ADMIN . '-import&amp;action=list_photos&amp;album='.$album['id'];
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'],
115        'U_LIST' => FLICKR_ADMIN . '-import&amp;action=list_photos&amp;album=not_in_set',
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  {
129    $self_url = FLICKR_ADMIN . '-import&amp;action=list_photos&amp;album='.$_GET['album'];
130    $flickr_prefix = 'flickr-'.$u['username'].'-';
131    $flickr_root_url = $flickr->urls_getUserPhotos($u['id']);
132   
133    // pagination
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   
151    // get existing photos
152    $query = '
153SELECT id, file
154  FROM '.IMAGES_TABLE.'
155  WHERE file LIKE "'.$flickr_prefix.'%"
156;';
157    $existing_photos = simple_hash_from_query($query, 'id', 'file');
158    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
159   
160    // remove existing photos
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    }
170   
171    if ($duplicates>0)
172    {
173      $page['infos'][] = '<a href="admin.php?page=batch_manager&amp;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>';
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&amp;action=import_set',
197      'U_DISPLAY' => $self_url,
198      ));
199     
200    // get piwigo categories
201    $query = '
202SELECT 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  }
217 
218  // list all photos of the user
219  case 'list_all':
220  {
221    $flickr_prefix = 'flickr-'.$u['username'].'-';
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();
228    foreach ($all_albums as $album)
229    {
230      $album_photos = $flickr->photosets_getPhotos($album['id'], NULL, NULL, 500, NULL, 'photos');
231      $album_photos = $album_photos['photoset']['photo'];
232     
233      foreach ($album_photos as $photo)
234      {
235        $all_photos[ $photo['id'] ][] = $album['title'];
236      }
237    }
238   
239    // get existing photos
240    $query = '
241SELECT id, file
242  FROM '.IMAGES_TABLE.'
243  WHERE file LIKE "'.$flickr_prefix.'%"
244;';
245    $existing_photos = simple_hash_from_query($query, 'id', 'file');
246    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
247   
248    // remove existing photos
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);
266    $all_photos = array_values($all_photos);
267   
268    if ($duplicates>0)
269    {
270      $page['infos'][] = '<a href="admin.php?page=batch_manager&amp;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>';
273    }
274   
275    $template->assign(array(
276      'nb_elements' => count($all_photos),
277      'all_elements' => json_encode($all_photos),
278      'F_ACTION' => FLICKR_ADMIN . '-import&amp;action=import_set',
279      ));
280     
281    // get piwigo categories
282    $query = '
283SELECT 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    }
297    redirect(FLICKR_ADMIN . '-import');
298  }
299}
300
301
302$template->assign('ACTION', $_GET['action']);
303
304$template->set_filename('flickr2piwigo', dirname(__FILE__) . '/template/import.tpl');
305
306?>
Note: See TracBrowser for help on using the repository browser.