source: extensions/Google2Piwigo/include/ws_functions.inc.php @ 20670

Last change on this file since 20670 was 20670, checked in by mistic100, 11 years ago

get geoloc ig rv_gmaps installed, compatible with safe_mode

File size: 5.2 KB
Line 
1<?php
2if (!defined('PICASA_WA_PATH')) die('Hacking attempt!');
3
4function 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    );
19}
20
21function ws_images_addPicasa($params, &$service)
22{
23  if (!is_admin())
24  {
25    return new PwgError(401, 'Forbidden');
26  }
27 
28  global $conf, $pwg_loaded_plugins;
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->getPhotoEntry($query);
60 
61  $photo = array(
62    'id' =>           $params['id'],
63    'url' =>          $photoEntry->getMediaGroup()->content[0]->getUrl(),
64    'title' =>        get_filename_wo_extension($photoEntry->getMediaGroup()->getTitle()->getText()),
65    'author' =>       $photoEntry->getMediaGroup()->credit[0]->getText(),
66    'description' =>  $photoEntry->getMediaGroup()->getDescription()->getText(),
67    'tags' =>         $photoEntry->getMediaGroup()->getKeywords()->getText(),
68    'timestamp' =>    substr($photoEntry->getGphotoTimestamp(), 0, -3),
69    'latlon' =>       null,
70    );
71 
72  $photo['path'] = PICASA_WA_CACHE . 'picasa-'.$photo['id'].'.'.get_extension($photo['url']);
73 
74  if ($photoEntry->getGeoRssWhere() !== null && !empty($pwg_loaded_plugins['rv_gmaps']))
75  {
76    $photo['latlon'] = $photoEntry->getGeoRssWhere()->getPoint()->getPos()->getText();
77  }
78 
79  // copy file
80  $ti = microtime(true);
81  if (picasa_wa_download_remote_file($photo['url'], $photo['path']) !== true)
82  {
83    return new PwgError(null, l10n('Can\'t download file'));
84  }
85 
86  // category
87  if ($params['category'] == '<!-- create -->')
88  {
89    // search existing category
90    $query = '
91SELECT id FROM '.CATEGORIES_TABLE.'
92  WHERE name LIKE("%<!-- picasa-'.$params['pwa_album'].' -->")
93;';
94    $result = pwg_query($query);
95   
96    if (pwg_db_num_rows($result))
97    {
98      list($cat_id) = pwg_db_fetch_row($result);
99      $photo['category'] = $cat_id;
100    }
101    else
102    {
103      // create new category
104      $query = $picasa->newAlbumQuery();
105      $query->setUser('default');
106      $query->setAlbumId($params['pwa_album']);
107      $albumEntry = $picasa->getAlbumEntry($query);
108     
109      $category = array(
110        'name' => pwg_db_real_escape_string($albumEntry->mediaGroup->title->text).' <!-- picasa-'.$params['pwa_album'].' -->',
111        'comment' => pwg_db_real_escape_string($albumEntry->mediaGroup->description->text),
112        'parent' => 0,
113        );
114 
115      $cat = ws_categories_add($category, $service);
116      $photo['category'] = $cat['id'];
117    }
118  }
119  else
120  {
121    $photo['category'] = $params['category'];
122  }
123 
124  // add photo
125  $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), array($photo['category']));
126 
127  // do some updates
128  if (!empty($params['fills']))
129  {
130    $params['fills'] = rtrim($params['fills'], ',');
131    $params['fills'] = explode(',', $params['fills']);
132 
133    $updates = array();
134    if (in_array('fill_name', $params['fills']))        $updates['name'] = pwg_db_real_escape_string($photo['title']); 
135    if (in_array('fill_date', $params['fills']))        $updates['date_creation'] = date('Y-m-d H:i:s', $photo['timestamp']);
136    if (in_array('fill_author', $params['fills']))      $updates['author'] = pwg_db_real_escape_string($photo['author']);
137    if (in_array('fill_description', $params['fills'])) $updates['comment'] = pwg_db_real_escape_string($photo['description']);
138    if (in_array('fill_geotag', $params['fills']) and !empty($photo['latlon']) )
139    {
140      $latlon = explode(' ', $photo['latlon']);
141      if (count($latlon) == 2)
142      {
143        $updates['lat'] = pwg_db_real_escape_string($latlon[0]);
144        $updates['lon'] = pwg_db_real_escape_string($latlon[1]);
145      }
146    }
147   
148    if (count($updates))
149    {
150      single_update(
151        IMAGES_TABLE,
152        $updates,
153        array('id' => $photo['image_id'])
154        );
155    }
156   
157    if ( !empty($photo['tags']) and in_array('fill_tags', $params['fills']) )
158    {
159      set_tags(get_tag_ids($photo['tags']), $photo['image_id']);
160    }
161  }
162 
163  return sprintf(l10n('Photo "%s" imported'), $photo['title']);
164}
165
166?>
Note: See TracBrowser for help on using the repository browser.