1 | <?php |
---|
2 | defined('PICASA_WA_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | function picasa_wa_add_ws_method($arr) |
---|
5 | { |
---|
6 | $service = &$arr[0]; |
---|
7 | |
---|
8 | $service->addMethod( |
---|
9 | 'pwg.images.addPicasa', |
---|
10 | 'ws_images_addPicasa', |
---|
11 | array( |
---|
12 | 'id' => array(), |
---|
13 | 'pwa_album' => array(), |
---|
14 | 'category' => array(), |
---|
15 | 'fills' => array('default' => 'fill_name,fill_author,fill_tags,fill_date,fill_description'), |
---|
16 | ), |
---|
17 | 'Used by Picasa Web Albums', |
---|
18 | null, |
---|
19 | array('hidden'=>true) |
---|
20 | ); |
---|
21 | } |
---|
22 | |
---|
23 | function ws_images_addPicasa($params, &$service) |
---|
24 | { |
---|
25 | if (!is_admin()) |
---|
26 | { |
---|
27 | return new PwgError(401, 'Forbidden'); |
---|
28 | } |
---|
29 | |
---|
30 | global $conf; |
---|
31 | |
---|
32 | $conf['google2piwigo'] = safe_unserialize($conf['google2piwigo']); |
---|
33 | |
---|
34 | include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php'); |
---|
35 | include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php'); |
---|
36 | include_once(PICASA_WA_PATH . 'include/functions.inc.php'); |
---|
37 | |
---|
38 | if (test_remote_download() === false) |
---|
39 | { |
---|
40 | return new PwgError(null, l10n('No download method available')); |
---|
41 | } |
---|
42 | |
---|
43 | if (empty($_SESSION['gdata_auth_token'])) |
---|
44 | { |
---|
45 | return new PwgError(null, l10n('API not authenticated')); |
---|
46 | } |
---|
47 | |
---|
48 | // init Gdata API |
---|
49 | set_include_path(get_include_path() . PATH_SEPARATOR . PICASA_WA_PATH.'include'); |
---|
50 | require_once('Zend/Loader.php'); |
---|
51 | Zend_Loader::loadClass('Zend_Gdata_AuthSub'); |
---|
52 | Zend_Loader::loadClass('Zend_Gdata_Photos'); |
---|
53 | |
---|
54 | $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['gdata_auth_token']); |
---|
55 | $picasa = new Zend_Gdata_Photos($client, "Piwigo-Google2Piwigo-1.0"); |
---|
56 | |
---|
57 | // photos infos |
---|
58 | $query = $picasa->newPhotoQuery(); |
---|
59 | $query->setUser('default'); |
---|
60 | $query->setAlbumId($params['pwa_album']); |
---|
61 | $query->setPhotoId($params['id']); |
---|
62 | $query->setImgMax('d'); |
---|
63 | $photoEntry = $picasa->getPhotoEntry($query); |
---|
64 | |
---|
65 | $photo = array( |
---|
66 | 'id' => $params['id'], |
---|
67 | 'url' => $photoEntry->getMediaGroup()->content[0]->getUrl(), |
---|
68 | 'title' => get_filename_wo_extension($photoEntry->getMediaGroup()->getTitle()->getText()), |
---|
69 | 'author' => $photoEntry->getMediaGroup()->credit[0]->getText(), |
---|
70 | 'description' => $photoEntry->getMediaGroup()->getDescription()->getText(), |
---|
71 | 'tags' => $photoEntry->getMediaGroup()->getKeywords()->getText(), |
---|
72 | 'timestamp' => substr($photoEntry->getGphotoTimestamp(), 0, -3), |
---|
73 | 'latlon' => null, |
---|
74 | ); |
---|
75 | |
---|
76 | $photo['path'] = PICASA_WA_CACHE . 'picasa-'.$photo['id'].'.'.get_extension($photo['url']); |
---|
77 | |
---|
78 | if ($photoEntry->getGeoRssWhere() !== null) |
---|
79 | { |
---|
80 | $photo['latlon'] = $photoEntry->getGeoRssWhere()->getPoint()->getPos()->getText(); |
---|
81 | } |
---|
82 | |
---|
83 | // copy file |
---|
84 | $ti = microtime(true); |
---|
85 | if (picasa_wa_download_remote_file($photo['url'], $photo['path']) !== true) |
---|
86 | { |
---|
87 | return new PwgError(null, l10n('Can\'t download file')); |
---|
88 | } |
---|
89 | |
---|
90 | // category |
---|
91 | if ($params['category'] == '<!-- create -->') |
---|
92 | { |
---|
93 | // search existing category |
---|
94 | $query = ' |
---|
95 | SELECT id FROM '.CATEGORIES_TABLE.' |
---|
96 | WHERE name LIKE("%<!-- picasa-'.$params['pwa_album'].' -->") |
---|
97 | ;'; |
---|
98 | $result = pwg_query($query); |
---|
99 | |
---|
100 | if (pwg_db_num_rows($result)) |
---|
101 | { |
---|
102 | list($cat_id) = pwg_db_fetch_row($result); |
---|
103 | $photo['category'] = $cat_id; |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | // create new category |
---|
108 | $query = $picasa->newAlbumQuery(); |
---|
109 | $query->setUser('default'); |
---|
110 | $query->setAlbumId($params['pwa_album']); |
---|
111 | $albumEntry = $picasa->getAlbumEntry($query); |
---|
112 | |
---|
113 | $category = array( |
---|
114 | 'name' => pwg_db_real_escape_string($albumEntry->mediaGroup->title->text).' <!-- picasa-'.$params['pwa_album'].' -->', |
---|
115 | 'comment' => pwg_db_real_escape_string($albumEntry->mediaGroup->description->text), |
---|
116 | 'parent' => 0, |
---|
117 | ); |
---|
118 | |
---|
119 | $cat = ws_categories_add($category, $service); |
---|
120 | $photo['category'] = $cat['id']; |
---|
121 | } |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | $photo['category'] = $params['category']; |
---|
126 | } |
---|
127 | |
---|
128 | // add photo |
---|
129 | $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), array($photo['category'])); |
---|
130 | |
---|
131 | // do some updates |
---|
132 | if (!empty($params['fills'])) |
---|
133 | { |
---|
134 | $fills = rtrim($params['fills'], ','); |
---|
135 | $fills = explode(',', $fills); |
---|
136 | $fills = array_flip($fills); |
---|
137 | |
---|
138 | $updates = array(); |
---|
139 | if (isset($fills['fill_name'])) $updates['name'] = pwg_db_real_escape_string($photo['title']); |
---|
140 | if (isset($fills['fill_date'])) $updates['date_creation'] = date('Y-m-d H:i:s', $photo['timestamp']); |
---|
141 | if (isset($fills['fill_author'])) $updates['author'] = pwg_db_real_escape_string($photo['author']); |
---|
142 | if (isset($fills['fill_description'])) $updates['comment'] = pwg_db_real_escape_string($photo['description']); |
---|
143 | if (isset($fills['fill_geotag']) and !empty($photo['latlon'])) |
---|
144 | { |
---|
145 | $latlon = explode(' ', $photo['latlon']); |
---|
146 | if (count($latlon) == 2) |
---|
147 | { |
---|
148 | $updates['latitude'] = pwg_db_real_escape_string($latlon[0]); |
---|
149 | $updates['longitude'] = pwg_db_real_escape_string($latlon[1]); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | if (count($updates)) |
---|
154 | { |
---|
155 | single_update( |
---|
156 | IMAGES_TABLE, |
---|
157 | $updates, |
---|
158 | array('id' => $photo['image_id']) |
---|
159 | ); |
---|
160 | } |
---|
161 | |
---|
162 | if (!empty($photo['tags']) and isset($fills['fill_tags'])) |
---|
163 | { |
---|
164 | set_tags(get_tag_ids($photo['tags']), $photo['image_id']); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | return l10n('Photo "%s" imported', $photo['title']); |
---|
169 | } |
---|