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

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

add curl download, and display error if no download method (curl or allow_url_fopen) available

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