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

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

small template adjustments
missing language string

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