1 | <?php |
---|
2 | if (!defined('FLICKR_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | function flickr_add_ws_method($arr) |
---|
5 | { |
---|
6 | $service = &$arr[0]; |
---|
7 | |
---|
8 | $service->addMethod( |
---|
9 | 'pwg.images.addFlickr', |
---|
10 | 'ws_images_addFlickr', |
---|
11 | array( |
---|
12 | 'category' => array('default' => null), |
---|
13 | 'id' => array('default' => null), |
---|
14 | 'fills' => array('default' =>null), |
---|
15 | ), |
---|
16 | 'Used by Flickr2Piwigo, fills € (fill_name,fill_posted,fill_taken,fill_author,fill_tags)' |
---|
17 | ); |
---|
18 | } |
---|
19 | |
---|
20 | function ws_images_addFlickr($photo, &$service) |
---|
21 | { |
---|
22 | if (!is_admin()) |
---|
23 | { |
---|
24 | return new PwgError(403, 'Forbidden'); |
---|
25 | } |
---|
26 | |
---|
27 | global $conf; |
---|
28 | $conf['flickr2piwigo'] = unserialize($conf['flickr2piwigo']); |
---|
29 | |
---|
30 | if ( empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']) ) |
---|
31 | { |
---|
32 | return new PwgError(500, 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 | |
---|
38 | // init flickr API |
---|
39 | include_once(FLICKR_PATH . 'include/phpFlickr/phpFlickr.php'); |
---|
40 | $flickr = new phpFlickr($conf['flickr2piwigo']['api_key'], $conf['flickr2piwigo']['secret_key']); |
---|
41 | $flickr->enableCache('fs', FLICKR_FS_CACHE); |
---|
42 | |
---|
43 | // user |
---|
44 | $u = $flickr->test_login(); |
---|
45 | if ( $u === false or empty($_SESSION['phpFlickr_auth_token']) ) |
---|
46 | { |
---|
47 | return new PwgError(403, l10n('API not authenticated')); |
---|
48 | } |
---|
49 | |
---|
50 | // photos infos |
---|
51 | $photo_f = $flickr->photos_getInfo($photo['id']); |
---|
52 | $photo = array_merge($photo, $photo_f['photo']); |
---|
53 | $photo['url'] = $flickr->get_biggest_size($photo['id'], 'original'); |
---|
54 | $photo['path'] = FLICKR_FS_CACHE . 'flickr-'.$u['username'].'-'.$photo['id'].'.'.get_extension($photo['url']); |
---|
55 | |
---|
56 | // copy file |
---|
57 | if (download_remote_file($photo['url'], $photo['path']) == false) |
---|
58 | { |
---|
59 | return new PwgError(500, l10n('No download method available')); |
---|
60 | } |
---|
61 | |
---|
62 | // category |
---|
63 | if (!preg_match('#^[0-9]+$#', $photo['category'])) |
---|
64 | { |
---|
65 | $categories_names = explode(',', $photo['category']); |
---|
66 | |
---|
67 | $photo['category'] = array(); |
---|
68 | foreach ($categories_names as $category_name) |
---|
69 | { |
---|
70 | $query = ' |
---|
71 | SELECT id FROM '.CATEGORIES_TABLE.' |
---|
72 | WHERE LOWER(name) = "'.strtolower($category_name).'" |
---|
73 | ;'; |
---|
74 | $result = pwg_query($query); |
---|
75 | |
---|
76 | if (pwg_db_num_rows($result)) |
---|
77 | { |
---|
78 | list($cat_id) = pwg_db_fetch_row($result); |
---|
79 | array_push($photo['category'], $cat_id); |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | |
---|
84 | $cat = create_virtual_category($category_name); |
---|
85 | array_push($photo['category'], $cat['id']); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | $photo['category'] = array($photo['category']); |
---|
92 | } |
---|
93 | |
---|
94 | // add photo |
---|
95 | $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), $photo['category']); |
---|
96 | |
---|
97 | // do some updates |
---|
98 | if (!empty($photo['fills'])) |
---|
99 | { |
---|
100 | $photo['fills'] = rtrim($photo['fills'], ','); |
---|
101 | $photo['fills'] = explode(',', $photo['fills']); |
---|
102 | |
---|
103 | $updates = array(); |
---|
104 | if (in_array('fill_name', $photo['fills'])) $updates['name'] = $photo['title']; |
---|
105 | if (in_array('fill_posted', $photo['fills'])) $updates['date_available'] = date('Y-d-m H:i:s', $photo['dates']['posted']); |
---|
106 | if (in_array('fill_taken', $photo['fills'])) $updates['date_creation'] = $photo['dates']['taken']; |
---|
107 | if (in_array('fill_author', $photo['fills'])) $updates['author'] = $photo['owner']['username']; |
---|
108 | |
---|
109 | if (count($updates)) |
---|
110 | { |
---|
111 | single_update( |
---|
112 | IMAGES_TABLE, |
---|
113 | $updates, |
---|
114 | array('id' => $photo['image_id']) |
---|
115 | ); |
---|
116 | } |
---|
117 | |
---|
118 | if ( !empty($photo['tags']['tag']) and in_array('fill_tags', $photo['fills']) ) |
---|
119 | { |
---|
120 | $raw_tags = array_map(create_function('$t', 'return $t["_content"];'), $photo['tags']['tag']); |
---|
121 | $raw_tags = implode(',', $raw_tags); |
---|
122 | set_tags(get_tag_ids($raw_tags), $photo['image_id']); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | return sprintf(l10n('%s imported'), $photo['title']); |
---|
127 | } |
---|
128 | |
---|
129 | function download_remote_file($src, $dest) |
---|
130 | { |
---|
131 | if (function_exists('curl_init')) |
---|
132 | { |
---|
133 | $newf = fopen($dest, "wb"); |
---|
134 | $ch = curl_init(); |
---|
135 | |
---|
136 | curl_setopt($ch, CURLOPT_URL, $src); |
---|
137 | curl_setopt($ch, CURLOPT_HEADER, 0); |
---|
138 | curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'); |
---|
139 | curl_setopt($ch, CURLOPT_FILE, $newf); |
---|
140 | |
---|
141 | curl_exec($ch); |
---|
142 | curl_close($ch); |
---|
143 | fclose($newf); |
---|
144 | |
---|
145 | return true; |
---|
146 | } |
---|
147 | else if (ini_get('allow_url_fopen')) |
---|
148 | { |
---|
149 | $file = fopen($src, "rb"); |
---|
150 | $newf = fopen($dest, "wb"); |
---|
151 | |
---|
152 | while (!feof($file)) |
---|
153 | { |
---|
154 | fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 ); |
---|
155 | } |
---|
156 | |
---|
157 | fclose($file); |
---|
158 | fclose($newf); |
---|
159 | |
---|
160 | return true; |
---|
161 | } |
---|
162 | |
---|
163 | return false; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | ?> |
---|