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

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

initial version

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