source: extensions/Google2Piwigo/admin/import.php @ 26198

Last change on this file since 26198 was 26198, checked in by mistic100, 10 years ago

update for Piwigo 2.6

File size: 8.5 KB
Line 
1<?php
2defined('PICASA_WA_PATH') or die('Hacking attempt!');
3
4set_time_limit(600);
5
6include_once(PICASA_WA_PATH . 'include/functions.inc.php');
7
8if (!test_remote_download())
9{
10  $page['errors'][] = l10n('No download method available');
11  $_GET['action'] = 'error';
12}
13else
14{
15  // init Gdata API
16  set_include_path(get_include_path() . PATH_SEPARATOR . PICASA_WA_PATH.'include');
17  require_once('Zend/Loader.php');
18  Zend_Loader::loadClass('Zend_Gdata_AuthSub');
19  Zend_Loader::loadClass('Zend_Gdata_Photos');
20 
21  // generate token after authentication
22  if (!empty($_GET['token']))
23  {
24    $_SESSION['gdata_auth_token'] = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
25    $_GET['action'] = 'logged';
26  }
27 
28  // authentication
29  if (empty($_SESSION['gdata_auth_token']))
30  {
31    $_GET['action'] = 'login';
32  }
33  else
34  {
35    $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['gdata_auth_token']);
36    $picasa = new Zend_Gdata_Photos($client, "Piwigo-Google2Piwigo-1.0");
37  }
38}
39
40
41if (!isset($_GET['action']))
42{
43  $_GET['action'] = 'main';
44}
45
46switch ($_GET['action'])
47{
48  // button to login page
49  case 'login':
50  {
51    $login_url = Zend_Gdata_AuthSub::getAuthSubTokenUri(
52      get_absolute_root_url() . PICASA_WA_ADMIN . '-import',
53      'https://picasaweb.google.com/data', 
54      false, true
55      );
56       
57    $template->assign(array(
58      'picasa_login' => $login_url,
59      'HELP_CONTENT' => load_language('help.lang.html', PICASA_WA_PATH, array('return'=>true)),
60      ));
61    break;
62  }
63 
64  // message after login
65  case 'logged':
66  {
67    $_SESSION['page_infos'][] = l10n('Successfully logged to you Google account');
68    redirect(PICASA_WA_ADMIN . '-import');
69    break;
70  }
71 
72  // logout
73  case 'logout':
74  {
75    Zend_Gdata_AuthSub::AuthSubRevokeToken(
76      $_SESSION['gdata_auth_token'],
77      $client
78      );
79    unset($_SESSION['gdata_auth_token']);
80   
81    $_SESSION['page_infos'][] = l10n('Logged out');
82    redirect(PICASA_WA_ADMIN . '-import');
83    break;
84  }
85 
86  // main menu
87  case 'main':
88  {
89    $template->assign(array(
90      'username' => $picasa->getUserEntry( $picasa->newUserQuery() )->getGphotoNickname()->getText(),
91      'logout_url' =>      PICASA_WA_ADMIN . '-import&amp;action=logout',
92      'list_albums_url' => PICASA_WA_ADMIN . '-import&amp;action=list_albums',
93      'import_all_url' =>  PICASA_WA_ADMIN . '-import&amp;action=list_all',
94      ));
95    break;
96  }
97 
98  // list user albums
99  case 'list_albums':
100  {
101    // get all albums
102    $userFeed = $picasa->getUserFeed("default");
103   
104    $albums = array();
105    foreach ($userFeed as $userEntry)
106    {
107      $albums[] = array(
108        'title' =>       $userEntry->title->text,
109        'description' => $userEntry->mediaGroup->description->text,
110        'photos' =>      $userEntry->gphotoNumPhotos->text,
111        'U_LIST' => PICASA_WA_ADMIN . '-import&amp;action=list_photos&amp;album=' . $userEntry->gphotoId->text,
112        );
113    }
114   
115    $template->assign(array(
116      'total_albums' => count($albums),
117      'albums' => $albums,
118      ));
119    break;
120  }
121 
122  // list photos of an album
123  case 'list_photos':
124  {
125    $self_url = PICASA_WA_ADMIN . '-import&amp;action=list_photos&amp;album='.$_GET['album'];
126    $picasa_prefix = 'picasa-';
127   
128    // pagination
129    if (isset($_GET['start']))   $page['start'] = intval($_GET['start']);
130    else                         $page['start'] = 0;
131    if (isset($_GET['display'])) $page['display'] = $_GET['display']=='all' ? 500 : intval($_GET['display']);
132    else                         $page['display'] = 20;
133   
134    // get photos
135    $query = $picasa->newAlbumQuery();
136    $query->setUser('default');
137    $query->setAlbumId($_GET['album']);
138    $query->setImgMax('800');
139    $albumFeed = $picasa->getAlbumFeed($query);
140   
141    $all_photos = array();
142    foreach ($albumFeed as $albumEntry)
143    {
144      $all_photos[] = array(
145        'id' =>    $albumEntry->getGphotoId()->getText(),
146        'name' =>  $albumEntry->mediaGroup->title->text,
147        'thumb' => $albumEntry->mediaGroup->thumbnail[1]->url,
148        'src' =>   $albumEntry->mediaGroup->content[0]->url,
149        'url' =>   $albumEntry->link[2]->href,
150        );
151    }
152   
153    // get existing photos
154    $query = '
155SELECT id, file
156  FROM '.IMAGES_TABLE.'
157  WHERE file LIKE "'.$picasa_prefix.'%"
158;';
159    $existing_photos = simple_hash_from_query($query, 'id', 'file');
160    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$picasa_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
161   
162    // remove existing photos
163    $duplicates = 0;
164    foreach ($all_photos as $i => $photo)
165    {
166      if (in_array($photo['id'], $existing_photos))
167      {
168        unset($all_photos[$i]);
169        $duplicates++;
170      }
171    }
172   
173    if ($duplicates>0)
174    {
175      $page['infos'][] = '<a href="admin.php?page=batch_manager&amp;filter=prefilter-picasa">'
176          .l10n_dec(
177            'One picture is not displayed because already existing in the database.',
178            '%d pictures are not displayed because already existing in the database.',
179            $duplicates)
180        .'</a>';
181    }
182   
183    // displayed photos
184    $page_photos = array_slice($all_photos, $page['start'], $page['display']);
185    $all_elements = array_map(create_function('$p', 'return  \'"\'.$p["id"].\'"\';'), $all_photos);
186 
187    $template->assign(array(
188      'nb_thumbs_set' =>  count($all_photos),
189      'nb_thumbs_page' => count($page_photos),
190      'thumbnails' =>     $page_photos,
191      'all_elements' =>   $all_elements,
192      'album' =>          $_GET['album'],
193      'F_ACTION' =>       PICASA_WA_ADMIN.'-import&amp;action=import_set',
194      'U_DISPLAY' =>      $self_url,
195      ));
196     
197    // get piwigo categories
198    $query = '
199SELECT id, name, uppercats, global_rank
200  FROM '.CATEGORIES_TABLE.'
201;';
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    $picasa_prefix = 'picasa-';
219   
220    // get all photos in all albums
221    $userFeed = $picasa->getUserFeed("default");
222
223    $all_photos = array();
224    foreach ($userFeed as $userEntry)
225    {
226      $query = $picasa->newAlbumQuery();
227      $query->setUser('default');
228      $query->setAlbumId( $userEntry->gphotoId->text );
229      $albumFeed = $picasa->getAlbumFeed($query);
230     
231      foreach ($albumFeed as $albumEntry)
232      {
233        $all_photos[ $albumEntry->getGphotoId()->getText() ] = $userEntry->gphotoId->text;
234      }
235    }
236   
237    // get existing photos
238    $query = '
239SELECT id, file
240  FROM '.IMAGES_TABLE.'
241  WHERE file LIKE "'.$picasa_prefix.'%"
242;';
243    $existing_photos = simple_hash_from_query($query, 'id', 'file');
244    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$picasa_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
245   
246    // remove existing photos
247    $duplicates = 0;
248    foreach ($all_photos as $id => &$photo)
249    {
250      if (in_array($id, $existing_photos))
251      {
252        unset($all_photos[$id]);
253        $duplicates++;
254      }
255      else
256      {
257        $photo = array(
258          'id' => $id,
259          'album' => $photo,
260          );
261      }
262    }
263    unset($photo);
264    $all_photos = array_values($all_photos);
265   
266    if ($duplicates>0)
267    {
268      $page['infos'][] = '<a href="admin.php?page=batch_manager&amp;filter=prefilter-picasa">'
269          .l10n_dec(
270            'One picture is not displayed because already existing in the database.',
271            '%d pictures are not displayed because already existing in the database.',
272            $duplicates)
273        .'</a>';
274    }
275   
276    $template->assign(array(
277      'nb_elements' =>  count($all_photos),
278      'all_elements' => $all_photos,
279      'F_ACTION' =>     PICASA_WA_ADMIN . '-import&amp;action=import_set',
280      ));
281     
282    // get piwigo categories
283    $query = '
284SELECT id, name, uppercats, global_rank
285  FROM '.CATEGORIES_TABLE.'
286;';
287    display_select_cat_wrapper($query, array(), 'category_parent_options');
288    break;
289  }
290 
291  // success message after import
292  case 'import_set':
293  {
294    if (isset($_POST['done']))
295    {
296      $_SESSION['page_infos'][] = l10n('%d pictures imported', $_POST['done']);
297    }
298    redirect(PICASA_WA_ADMIN . '-import');
299  }
300}
301
302
303$template->assign('ACTION', $_GET['action']);
304
305$template->set_filename('picasa_web_albums', realpath(PICASA_WA_PATH . '/admin/template/import.tpl'));
Note: See TracBrowser for help on using the repository browser.