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

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

fix oauth flow

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