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

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

mistyped "mkdir"

File size: 8.9 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'] = '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 logued 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('Logued 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' => $conf['flickr2piwigo']['username'],
84      'profile_url' => $u['photosurl'],
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_IMPORT_ALL'] = FLICKR_ADMIN.'-import&amp;action=import_album&amp;album='.$album['id'];
103      $album['U_LIST'] = FLICKR_ADMIN.'-import&amp;action=list_photos&amp;album='.$album['id'];
104    }
105    unset($album);
106   
107    // not classed
108    $wo_albums = $flickr->photos_getNotInSet(NULL, NULL, NULL, NULL, 'photos', NULL, NULL, 1);
109    if ($wo_albums['photos']['total'] > 0)
110    {
111      array_push($albums, array(
112        'id' => 'not_in_set',
113        'title' => l10n('Pictures without album'),
114        'description' => null,
115        'photos' => $wo_albums['photos']['total'],
116        //'U_IMPORT_ALL' => FLICKR_ADMIN.'-import&amp;action=import_album&amp;album='.$album['id'],
117        'U_LIST' => FLICKR_ADMIN.'-import&amp;action=list_photos&amp;album=not_in_set',
118        ));
119    }
120   
121    $template->assign(array(
122      'total_albums' => $total_albums,
123      'albums' => $albums,
124      ));
125    break;
126  }
127 
128  // list photos of an album
129  case 'list_photos':
130  {
131    if (isset($_GET['start']))   $page['start'] = intval($_GET['start']);
132    else                         $page['start'] = 0;
133    if (isset($_GET['display'])) $page['display'] = $_GET['display']=='all' ? 500 : intval($_GET['display']);
134    else                         $page['display'] = 20;
135   
136    $self_url = FLICKR_ADMIN.'-import&amp;action=list_photos&amp;album='.$_GET['album'];
137    $flickr_prefix = 'flickr-'.$conf['flickr2piwigo']['username'].'-';
138    $flickr_root_url = $flickr->urls_getUserPhotos($u['id']);
139   
140    // get existing photos
141    $query = '
142SELECT id, file
143  FROM '.IMAGES_TABLE.'
144  WHERE file LIKE "'.$flickr_prefix.'%"
145;';
146    $existing_photos = simple_hash_from_query($query, 'id', 'file');
147    $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);
148   
149    // get photos
150    if ($_GET['album'] == 'not_in_set')
151    {
152      $all_photos = $flickr->photos_getNotInSet(NULL, NULL, NULL, NULL, 'photos', NULL, NULL, 500);
153      $all_photos = $all_photos['photos']['photo'];
154    }
155    else
156    {
157      $all_photos = $flickr->photosets_getPhotos($_GET['album'], NULL, NULL, 500, NULL, 'photos');
158      $all_photos = $all_photos['photoset']['photo'];
159    }
160   
161    // remove existing
162    $duplicates = 0;
163    foreach ($all_photos as $i => $photo)
164    {
165      if (in_array($photo['id'], $existing_photos))
166      {
167        unset($all_photos[$i]);
168        $duplicates++;
169      }
170    }
171    if ($duplicates>0)
172    {
173      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));
174    }
175   
176    // displayed photos
177    $page_photos = array_slice($all_photos, $page['start'], $page['display']);
178    $all_elements = array_map(create_function('$p', 'return  \'"\'.$p["id"].\'"\';'), $all_photos);
179   
180    foreach ($page_photos as &$photo)
181    {
182      $photo['thumb'] = $flickr->buildPhotoURL($photo, "thumbnail");
183      $photo['src'] = $flickr->get_biggest_size($photo['id'], "medium_800");
184      $photo['url'] = $flickr_root_url.$photo['id'];
185    }
186    unset($photo);
187   
188    $template->assign(array(
189      'nb_thumbs_set' => count($all_photos),
190      'nb_thumbs_page' => count($page_photos),
191      'thumbnails' => $page_photos,
192      'all_elements' => $all_elements,
193      'album' => $_GET['album'],
194      'F_ACTION' => FLICKR_ADMIN.'-import&amp;action=import_set',
195      'U_DISPLAY' => $self_url,
196      ));
197     
198    // get piwigo categories
199    $query = '
200SELECT id, name, uppercats, global_rank
201  FROM '.CATEGORIES_TABLE.'
202;';
203    display_select_cat_wrapper($query, array(), 'associate_options', true);
204    display_select_cat_wrapper($query, array(), 'category_parent_options');
205   
206    // get navbar
207    $nav_bar = create_navigation_bar(
208      $self_url,
209      count($all_elements),
210      $page['start'],
211      $page['display']
212      );
213    $template->assign('navbar', $nav_bar);
214    break;
215  }
216   
217  case 'list_all':
218  {
219    $flickr_prefix = 'flickr-'.$conf['flickr2piwigo']['username'].'-';
220   
221    // get all photos in all albums
222    $all_albums = $flickr->photosets_getList($u['id']);
223    $all_albums = $all_albums['photoset'];
224   
225    $all_photos = array();
226    foreach ($all_albums as &$album)
227    {
228      $album_photos = $flickr->photosets_getPhotos($album['id'], NULL, NULL, 500, NULL, 'photos');
229      $album_photos = $album_photos['photoset']['photo'];
230     
231      foreach ($album_photos as &$photo)
232      {
233        $all_photos[ $photo['id'] ][] = $album['title'];
234      }
235      unset($photo);
236    }
237    unset($album);
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', '$p=preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p); return $p;'), $existing_photos);
247   
248    // remove duplicates
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    if ($duplicates>0)
267    {
268      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));
269    }
270    $all_photos = array_values($all_photos);
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$template->assign('ACTION', $_GET['action']);
300
301$template->set_filename('flickr2piwigo', dirname(__FILE__).'/template/import.tpl');
302
303?>
Note: See TracBrowser for help on using the repository browser.