1 | <?php |
---|
2 | if (!defined('INSTAG_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | function instagram_add_ws_method($arr) |
---|
5 | { |
---|
6 | $service = &$arr[0]; |
---|
7 | |
---|
8 | $service->addMethod( |
---|
9 | 'pwg.images.addInstagram', |
---|
10 | 'ws_images_addInstagram', |
---|
11 | array( |
---|
12 | 'id' => array(), |
---|
13 | 'category' => array(), |
---|
14 | 'fills' => array('default' =>null), |
---|
15 | ), |
---|
16 | 'Used by Instagram2Piwigo' |
---|
17 | ); |
---|
18 | } |
---|
19 | |
---|
20 | function ws_images_addInstagram($param, &$service) |
---|
21 | { |
---|
22 | if (!is_admin()) |
---|
23 | { |
---|
24 | return new PwgError(403, 'Forbidden'); |
---|
25 | } |
---|
26 | |
---|
27 | global $conf; |
---|
28 | $conf['Instagram2Piwigo'] = unserialize($conf['Instagram2Piwigo']); |
---|
29 | |
---|
30 | if ( empty($conf['Instagram2Piwigo']['api_key']) or empty($conf['Instagram2Piwigo']['secret_key']) ) |
---|
31 | { |
---|
32 | return new PwgError(null, l10n('Please fill your API keys on the configuration tab')); |
---|
33 | } |
---|
34 | |
---|
35 | include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php'); |
---|
36 | include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php'); |
---|
37 | include_once(INSTAG_PATH . 'include/functions.inc.php'); |
---|
38 | |
---|
39 | if (!function_exists('curl_init')) |
---|
40 | { |
---|
41 | return new PwgError(null, l10n('No download method available')); |
---|
42 | } |
---|
43 | |
---|
44 | // init instagram API |
---|
45 | if (empty($_SESSION['instagram_access_token'])) |
---|
46 | { |
---|
47 | return new PwgError(403, l10n('API not authenticated')); |
---|
48 | } |
---|
49 | |
---|
50 | require_once(INSTAG_PATH . 'include/Instagram/Instagram.php'); |
---|
51 | |
---|
52 | $instagram = new Instagram($_SESSION['instagram_access_token']); |
---|
53 | $instagram->enableCache(INSTAG_FS_CACHE); |
---|
54 | |
---|
55 | $current_user = $instagram->getCurrentUser(); |
---|
56 | $username = $current_user->getData()->username; |
---|
57 | |
---|
58 | // photos infos |
---|
59 | $photo_f = $instagram->getMedia($param['id']); |
---|
60 | $photo = array( |
---|
61 | 'id' => $photo_f->id, |
---|
62 | 'title' => $photo_f->getCaption(), |
---|
63 | 'url' => $photo_f->getStandardRes()->url, |
---|
64 | 'date' => $photo_f->getCreatedTime('Y-d-m H:i:s'), |
---|
65 | 'username' => $photo_f->getUser()->getFullName(), |
---|
66 | 'tags' => $photo_f->getTags()->toArray(), |
---|
67 | ); |
---|
68 | $photo = array_merge($param, $photo); |
---|
69 | |
---|
70 | if (!empty($photo['title'])) |
---|
71 | { |
---|
72 | $photo['title'] = $photo['title']->getText(); |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | $photo['title'] = $photo['id']; |
---|
77 | } |
---|
78 | |
---|
79 | $photo['path'] = INSTAG_FS_CACHE . 'instagram-'.$username.'-'.$photo['id'].'.'.get_extension($photo['url']); |
---|
80 | |
---|
81 | // copy file |
---|
82 | if (download_remote_file($photo['url'], $photo['path']) == false) |
---|
83 | { |
---|
84 | return new PwgError(null, l10n('Can\'t download file')); |
---|
85 | } |
---|
86 | |
---|
87 | // add photo |
---|
88 | $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), array($photo['category'])); |
---|
89 | |
---|
90 | // do some updates |
---|
91 | if (!empty($photo['fills'])) |
---|
92 | { |
---|
93 | $photo['fills'] = rtrim($photo['fills'], ','); |
---|
94 | $photo['fills'] = explode(',', $photo['fills']); |
---|
95 | |
---|
96 | $updates = array(); |
---|
97 | if (in_array('fill_name', $photo['fills'])) $updates['name'] = $photo['title']; |
---|
98 | if (in_array('fill_taken', $photo['fills'])) $updates['date_creation'] = $photo['date']; |
---|
99 | if (in_array('fill_author', $photo['fills'])) $updates['author'] = $photo['username']; |
---|
100 | |
---|
101 | if (count($updates)) |
---|
102 | { |
---|
103 | single_update( |
---|
104 | IMAGES_TABLE, |
---|
105 | $updates, |
---|
106 | array('id' => $photo['image_id']) |
---|
107 | ); |
---|
108 | } |
---|
109 | |
---|
110 | if ( !empty($photo['tags']) and in_array('fill_tags', $photo['fills']) ) |
---|
111 | { |
---|
112 | $raw_tags = implode(',', $photo['tags']); |
---|
113 | set_tags(get_tag_ids($raw_tags), $photo['image_id']); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | return sprintf(l10n('Photo "%s" imported'), $photo['title']); |
---|
118 | } |
---|
119 | |
---|
120 | ?> |
---|