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

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

get geoloc ig rv_gmaps installed, compatible with safe_mode

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