source: extensions/instagram2piwigo/admin/import.php @ 19711

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

now fetch the good data

File size: 5.6 KB
Line 
1<?php
2if (!defined('INSTAG_PATH')) die('Hacking attempt!');
3
4set_time_limit(600);
5
6include_once(INSTAG_PATH . 'include/functions.inc.php');
7
8// check API parameters and connect to instagram
9if ( empty($conf['Instagram2Piwigo']['api_key']) or empty($conf['Instagram2Piwigo']['secret_key']) )
10{
11  array_push($page['warnings'], l10n('Please fill your API keys on the configuration tab'));
12  $_GET['action'] = 'error';
13}
14else if (!function_exists('curl_init'))
15{
16  array_push($page['errors'], l10n('No download method available').' (cURL)');
17  $_GET['action'] = 'error';
18}
19else
20{
21  // init instagram API
22  if (empty($_SESSION['instagram_access_token']))
23  {
24    require_once(INSTAG_PATH . 'include/Instagram/Auth.php');
25   
26    $auth_config = array(
27        'client_id'         => $conf['Instagram2Piwigo']['api_key'],
28        'client_secret'     => $conf['Instagram2Piwigo']['secret_key'],
29        'redirect_uri'      => get_absolute_root_url() . INSTAG_ADMIN . '-import',
30    );
31   
32    $auth = new Instagram_Auth($auth_config);
33 
34    // must authenticate
35    if (@$_GET['action'] != 'login')
36    {
37      $_GET['action'] = 'init_login';
38    }
39   
40    // generate token after authentication
41    if (!empty($_GET['code']))
42    {
43      $_SESSION['instagram_access_token'] = $auth->getAccessToken($_GET['code']);
44      $_GET['action'] = 'logued';
45    }
46  }
47  else
48  {
49    require_once(INSTAG_PATH . 'include/Instagram/Instagram.php');
50    $instagram = new Instagram($_SESSION['instagram_access_token']);
51    $instagram->enableCache(INSTAG_FS_CACHE);
52   
53    $current_user = $instagram->getCurrentUser();
54    $username = $current_user->getData()->username;
55  }
56}
57
58
59if (!isset($_GET['action'])) $_GET['action'] = 'main';
60
61
62switch ($_GET['action'])
63{
64  // button to login page
65  case 'init_login':
66  {
67    $template->assign('instagram_login', INSTAG_ADMIN . '-import&amp;action=login');
68    break;
69  }
70 
71  // call instagram login procedure
72  case 'login':
73  {
74    $auth->authorize();
75    break;
76  }
77 
78  // message after login
79  case 'logued':
80  {
81    $_SESSION['page_infos'][] = l10n('Successfully logged in to you Instagram account');
82    redirect(INSTAG_ADMIN . '-import');
83    break;
84  }
85 
86  // logout
87  case 'logout':
88  {
89    unset($_SESSION['instagram_access_token']);
90    $_SESSION['page_infos'][] = l10n('Logged out');
91    redirect(INSTAG_ADMIN . '-import');
92    break;
93  }
94 
95  // main menu
96  case 'main':
97  {
98    $template->assign(array(
99      'username' => $username,
100      'profile_url' => 'http://instagram.com/'.$username,
101      'logout_url' => INSTAG_ADMIN . '-import&amp;action=logout',
102      'list_photos_url' => INSTAG_ADMIN . '-import&amp;action=list_photos',
103      ));
104    break;
105  }
106 
107  // list photos
108  case 'list_photos':
109  {
110    $self_url = INSTAG_ADMIN . '-import&amp;action=list_photos';
111    $instagram_prefix = 'instagram-'.$username.'-';
112   
113    // pagination
114    if (isset($_GET['start']))   $page['start'] = intval($_GET['start']);
115    else                         $page['start'] = 0;
116    if (isset($_GET['display'])) $page['display'] = $_GET['display']=='all' ? 500 : intval($_GET['display']);
117    else                         $page['display'] = 20;
118   
119    $all_photos = $current_user->getAllMedia();
120   
121    // get existing photos
122    $query = '
123SELECT id, file
124  FROM '.IMAGES_TABLE.'
125  WHERE file LIKE "'.$instagram_prefix.'%"
126;';
127   
128    $existing_photos = simple_hash_from_query($query, 'id', 'file');
129    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$instagram_prefix.'([0-9_]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
130   
131    // remove existing photos
132    $duplicates = 0;
133    foreach ($all_photos as $i => $photo)
134    {
135      if (in_array($photo->id, $existing_photos))
136      {
137        $all_photos->unsetItem($i);
138        $duplicates++;
139      }
140    }
141   
142    if ($duplicates>0)
143    {
144      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));
145    }
146   
147    // displayed photos
148    $page_photos = $all_photos->getSlice($page['start'], $page['display']);
149    $all_elements = array_map(create_function('$p', 'return  \'"\'.$p->id.\'"\';'), $all_photos->getData());
150   
151    $tpl_vars = array();
152    foreach ($page_photos as $photo)
153    {
154      $tpl_vars[] = array(
155        'id' => $photo->id,
156        'title' => $photo->getCaption(),
157        'thumb' => $photo->getThumbnail()->url,
158        'src' => $photo->getLowRes()->url,
159        'url' => $photo->getLink(),
160        );
161    }
162   
163    $template->assign(array(
164      'nb_thumbs_set' => count($all_photos),
165      'nb_thumbs_page' => count($page_photos),
166      'thumbnails' => $tpl_vars,
167      'all_elements' => $all_elements,
168      'F_ACTION' => INSTAG_ADMIN.'-import&amp;action=import_set',
169      'U_DISPLAY' => $self_url,
170      ));
171     
172    // get piwigo categories
173    $query = '
174SELECT id, name, uppercats, global_rank
175  FROM '.CATEGORIES_TABLE.'
176;';
177    display_select_cat_wrapper($query, array(), 'category_parent_options');
178   
179    // get navbar
180    $nav_bar = create_navigation_bar(
181      $self_url,
182      count($all_elements),
183      $page['start'],
184      $page['display']
185      );
186    $template->assign('navbar', $nav_bar);
187    break;
188  }
189 
190  // success message after import
191  case 'import_set':
192  {
193    if (isset($_POST['done']))
194    {
195      $_SESSION['page_infos'][] = sprintf(l10n('%d pictures imported'), $_POST['done']);
196    }
197    redirect(INSTAG_ADMIN . '-import');
198  }
199}
200
201
202$template->assign('ACTION', $_GET['action']);
203
204$template->set_filename('Instagram2Piwigo', dirname(__FILE__) . '/template/import.tpl');
205
206?>
Note: See TracBrowser for help on using the repository browser.