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