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

Last change on this file since 18912 was 18912, checked in by mistic100, 11 years ago

correct english

File size: 8.0 KB
Line 
1<?php
2if (!defined('PICASA_WA_PATH')) 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  array_push($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'] = 'logued';
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'])) $_GET['action'] = 'main';
42
43switch ($_GET['action'])
44{
45  // button to login page
46  case 'login':
47  {
48    $login_url = Zend_Gdata_AuthSub::getAuthSubTokenUri(
49      get_absolute_root_url().PICASA_WA_ADMIN . '-import',
50      'https://picasaweb.google.com/data', 
51      false, true
52      );
53       
54    $template->assign('picasa_login', $login_url);
55    break;
56  }
57 
58  // message after login
59  case 'logued':
60  {
61    $_SESSION['page_infos'][] = l10n('Successfully logued to you Google account');
62    redirect(PICASA_WA_ADMIN . '-import');
63    break;
64  }
65 
66  // logout
67  case 'logout':
68  {
69    Zend_Gdata_AuthSub::AuthSubRevokeToken(
70      $_SESSION['gdata_auth_token'],
71      $client
72      );
73    unset($_SESSION['gdata_auth_token']);
74   
75    $_SESSION['page_infos'][] = l10n('Logged out');
76    redirect(PICASA_WA_ADMIN . '-import');
77    break;
78  }
79 
80  // main menu
81  case 'main':
82  {
83    $template->assign(array(
84      'logout_url' => PICASA_WA_ADMIN . '-import&amp;action=logout',
85      'list_albums_url' => PICASA_WA_ADMIN . '-import&amp;action=list_albums',
86      'import_all_url' => PICASA_WA_ADMIN . '-import&amp;action=list_all',
87      ));
88    break;
89  }
90 
91  // list user albums
92  case 'list_albums':
93  {
94    // get all albums
95    $userFeed = $picasa->getUserFeed("default");
96   
97    $albums = array();
98    foreach ($userFeed as $userEntry)
99    {
100      array_push($albums, array(
101        'title' => $userEntry->title->text,
102        'description' => $userEntry->mediaGroup->description->text,
103        'photos' => $userEntry->gphotoNumPhotos->text,
104        'U_LIST' => PICASA_WA_ADMIN . '-import&amp;action=list_photos&amp;album='.$userEntry->gphotoId->text,
105        ));
106    }
107   
108    $template->assign(array(
109      'total_albums' => count($albums),
110      'albums' => $albums,
111      ));
112    break;
113  }
114 
115  // list photos of an album
116  case 'list_photos':
117  {
118    $self_url = PICASA_WA_ADMIN . '-import&amp;action=list_photos&amp;album='.$_GET['album'];
119    $picasa_prefix = 'picasa-';
120   
121    // pagination
122    if (isset($_GET['start']))   $page['start'] = intval($_GET['start']);
123    else                         $page['start'] = 0;
124    if (isset($_GET['display'])) $page['display'] = $_GET['display']=='all' ? 500 : intval($_GET['display']);
125    else                         $page['display'] = 20;
126   
127    // get photos
128    $query = $picasa->newAlbumQuery();
129    $query->setUser('default');
130    $query->setAlbumId($_GET['album']);
131    $query->setImgMax('800');
132    $albumFeed = $picasa->getAlbumFeed($query);
133   
134    $all_photos = array();
135    foreach ($albumFeed as $albumEntry)
136    {
137      array_push($all_photos, array(
138        'id' => $albumEntry->getGphotoId()->getText(),
139        'name' => $albumEntry->mediaGroup->title->text,
140        'thumb' => $albumEntry->mediaGroup->thumbnail[1]->url,
141        'src' => $albumEntry->mediaGroup->content[0]->url,
142        'url' => $albumEntry->link[2]->href,
143        ));
144    }
145   
146    // get existing photos
147    $query = '
148SELECT id, file
149  FROM '.IMAGES_TABLE.'
150  WHERE file LIKE "'.$picasa_prefix.'%"
151;';
152    $existing_photos = simple_hash_from_query($query, 'id', 'file');
153    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$picasa_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
154   
155    // remove existing photos
156    $duplicates = 0;
157    foreach ($all_photos as $i => $photo)
158    {
159      if (in_array($photo['id'], $existing_photos))
160      {
161        unset($all_photos[$i]);
162        $duplicates++;
163      }
164    }
165   
166    if ($duplicates>0)
167    {
168      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));
169    }
170   
171    // displayed photos
172    $page_photos = array_slice($all_photos, $page['start'], $page['display']);
173    $all_elements = array_map(create_function('$p', 'return  \'"\'.$p["id"].\'"\';'), $all_photos);
174 
175    $template->assign(array(
176      'nb_thumbs_set' => count($all_photos),
177      'nb_thumbs_page' => count($page_photos),
178      'thumbnails' => $page_photos,
179      'all_elements' => $all_elements,
180      'album' => $_GET['album'],
181      'F_ACTION' => PICASA_WA_ADMIN.'-import&amp;action=import_set',
182      'U_DISPLAY' => $self_url,
183      ));
184     
185    // get piwigo categories
186    $query = '
187SELECT id, name, uppercats, global_rank
188  FROM '.CATEGORIES_TABLE.'
189;';
190    display_select_cat_wrapper($query, array(), 'category_parent_options');
191   
192    // get navbar
193    $nav_bar = create_navigation_bar(
194      $self_url,
195      count($all_elements),
196      $page['start'],
197      $page['display']
198      );
199    $template->assign('navbar', $nav_bar);
200    break;
201  }
202 
203  // list all photos of the user
204  case 'list_all':
205  {
206    $picasa_prefix = 'picasa-';
207   
208    // get all photos in all albums
209    $userFeed = $picasa->getUserFeed("default");
210
211    $all_photos = array();
212    foreach ($userFeed as $userEntry)
213    {
214      $query = $picasa->newAlbumQuery();
215      $query->setUser('default');
216      $query->setAlbumId( $userEntry->gphotoId->text );
217      $albumFeed = $picasa->getAlbumFeed($query);
218     
219      foreach ($albumFeed as $albumEntry)
220      {
221        $all_photos[ $albumEntry->getGphotoId()->getText() ] = $userEntry->gphotoId->text;
222      }
223    }
224   
225    // get existing photos
226    $query = '
227SELECT id, file
228  FROM '.IMAGES_TABLE.'
229  WHERE file LIKE "'.$picasa_prefix.'%"
230;';
231    $existing_photos = simple_hash_from_query($query, 'id', 'file');
232    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$picasa_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
233   
234    // remove existing photos
235    $duplicates = 0;
236    foreach ($all_photos as $id => &$photo)
237    {
238      if (in_array($id, $existing_photos))
239      {
240        unset($all_photos[$id]);
241        $duplicates++;
242      }
243      else
244      {
245        $photo = array(
246          'id' => $id,
247          'album' => $photo,
248          );
249      }
250    }
251    unset($photo);
252    $all_photos = array_values($all_photos);
253   
254    if ($duplicates>0)
255    {
256      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));
257    }
258   
259    $template->assign(array(
260      'nb_elements' => count($all_photos),
261      'all_elements' => json_encode($all_photos),
262      'F_ACTION' => PICASA_WA_ADMIN . '-import&amp;action=import_set',
263      ));
264     
265    // get piwigo categories
266    $query = '
267SELECT id, name, uppercats, global_rank
268  FROM '.CATEGORIES_TABLE.'
269;';
270    display_select_cat_wrapper($query, array(), 'category_parent_options');
271    break;
272  }
273 
274  // success message after import
275  case 'import_set':
276  {
277    if (isset($_POST['done']))
278    {
279      $_SESSION['page_infos'][] = sprintf(l10n('%d pictures imported'), $_POST['done']);
280    }
281    redirect(PICASA_WA_ADMIN . '-import');
282  }
283}
284
285
286$template->assign('ACTION', $_GET['action']);
287
288$template->set_filename('picasa_web_albums', dirname(__FILE__) . '/template/import.tpl');
289
290?>
Note: See TracBrowser for help on using the repository browser.