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

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

update for 2.6

File size: 5.7 KB
Line 
1<?php
2defined('INSTAG_PATH') or 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  $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  $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']))
60{
61  $_GET['action'] = 'main';
62}
63
64
65switch ($_GET['action'])
66{
67  // button to login page
68  case 'init_login':
69  {
70    $template->assign('instagram_login', INSTAG_ADMIN . '-import&amp;action=login');
71    break;
72  }
73 
74  // call instagram login procedure
75  case 'login':
76  {
77    $auth->authorize();
78    break;
79  }
80 
81  // message after login
82  case 'logued':
83  {
84    $_SESSION['page_infos'][] = l10n('Successfully logged in to you Instagram account');
85    redirect(INSTAG_ADMIN . '-import');
86    break;
87  }
88 
89  // logout
90  case 'logout':
91  {
92    unset($_SESSION['instagram_access_token']);
93    $_SESSION['page_infos'][] = l10n('Logged out');
94    redirect(INSTAG_ADMIN . '-import');
95    break;
96  }
97 
98  // main menu
99  case 'main':
100  {
101    $template->assign(array(
102      'username' => $username,
103      'profile_url' => 'http://instagram.com/'.$username,
104      'logout_url' => INSTAG_ADMIN . '-import&amp;action=logout',
105      'list_photos_url' => INSTAG_ADMIN . '-import&amp;action=list_photos',
106      ));
107    break;
108  }
109 
110  // list photos
111  case 'list_photos':
112  {
113    $self_url = INSTAG_ADMIN . '-import&amp;action=list_photos';
114    $instagram_prefix = 'instagram-'.$username.'-';
115   
116    // pagination
117    if (isset($_GET['start']))   $page['start'] = intval($_GET['start']);
118    else                         $page['start'] = 0;
119    if (isset($_GET['display'])) $page['display'] = $_GET['display']=='all' ? 500 : intval($_GET['display']);
120    else                         $page['display'] = 20;
121   
122    $all_photos = $current_user->getAllMedia();
123   
124    // get existing photos
125    $query = '
126SELECT id, file
127  FROM '.IMAGES_TABLE.'
128  WHERE file LIKE "'.$instagram_prefix.'%"
129;';
130   
131    $existing_photos = simple_hash_from_query($query, 'id', 'file');
132    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$instagram_prefix.'([0-9_]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
133   
134    // remove existing photos
135    $duplicates = 0;
136    foreach ($all_photos as $i => $photo)
137    {
138      if (in_array($photo->id, $existing_photos))
139      {
140        $all_photos->unsetItem($i);
141        $duplicates++;
142      }
143    }
144   
145    if ($duplicates>0)
146    {
147      $page['infos'][] = '<a href="admin.php?page=batch_manager&amp;filter=prefilter-instagram">'
148          .l10n_dec(
149            'One picture is not displayed because already existing in the database.',
150            '%d pictures are not displayed because already existing in the database.',
151            $duplicates)
152        .'</a>';
153    }
154   
155    // displayed photos
156    $page_photos = $all_photos->getSlice($page['start'], $page['display']);
157    $all_elements = array_map(create_function('$p', 'return  \'"\'.$p->id.\'"\';'), $all_photos->getData());
158   
159    $tpl_vars = array();
160    foreach ($page_photos as $photo)
161    {
162      $tpl_vars[] = array(
163        'id' => $photo->id,
164        'title' => $photo->getCaption(),
165        'thumb' => $photo->getThumbnail()->url,
166        'src' => $photo->getLowRes()->url,
167        'url' => $photo->getLink(),
168        );
169    }
170   
171    $template->assign(array(
172      'nb_thumbs_set' => count($all_photos),
173      'nb_thumbs_page' => count($page_photos),
174      'thumbnails' => $tpl_vars,
175      'all_elements' => $all_elements,
176      'F_ACTION' => INSTAG_ADMIN.'-import&amp;action=import_set',
177      'U_DISPLAY' => $self_url,
178      ));
179     
180    // get piwigo categories
181    $query = '
182SELECT id, name, uppercats, global_rank
183  FROM '.CATEGORIES_TABLE.'
184;';
185    display_select_cat_wrapper($query, array(), 'category_parent_options');
186   
187    // get navbar
188    $nav_bar = create_navigation_bar(
189      $self_url,
190      count($all_elements),
191      $page['start'],
192      $page['display']
193      );
194    $template->assign('navbar', $nav_bar);
195    break;
196  }
197 
198  // success message after import
199  case 'import_set':
200  {
201    if (isset($_POST['done']))
202    {
203      $_SESSION['page_infos'][] = l10n('%d pictures imported', $_POST['done']);
204    }
205    redirect(INSTAG_ADMIN . '-import');
206  }
207}
208
209
210$template->assign('ACTION', $_GET['action']);
211
212$template->set_filename('Instagram2Piwigo', realpath(INSTAG_PATH . 'admin/template/import.tpl'));
Note: See TracBrowser for help on using the repository browser.