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