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

Last change on this file since 29251 was 29251, checked in by mistic100, 10 years ago

update for 2.7

File size: 5.2 KB
RevLine 
[17475]1<?php
[26198]2defined('PICASA_WA_PATH') or die('Hacking attempt!');
[17475]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(),
[20670]15      'fills' => array('default' => 'fill_name,fill_author,fill_tags,fill_date,fill_description'),
[17475]16      ),
[26198]17    'Used by Picasa Web Albums',
18    null,
19    array('hidden'=>true)
[17475]20    );
21}
22
23function ws_images_addPicasa($params, &$service)
24{
25  if (!is_admin())
26  {
27    return new PwgError(401, 'Forbidden');
28  }
29 
[29251]30  global $conf;
[17475]31 
[29251]32  $conf['google2piwigo'] = safe_unserialize($conf['google2piwigo']);
33 
[17475]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');
[20670]63  $photoEntry = $picasa->getPhotoEntry($query);
64 
[17475]65  $photo = array(
[20670]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,
[17475]74    );
[20670]75 
[17475]76  $photo['path'] = PICASA_WA_CACHE . 'picasa-'.$photo['id'].'.'.get_extension($photo['url']);
77 
[26198]78  if ($photoEntry->getGeoRssWhere() !== null)
[20670]79  {
80    $photo['latlon'] = $photoEntry->getGeoRssWhere()->getPoint()->getPos()->getText();
81  }
[17475]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 = '
95SELECT 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(
[18126]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),
[17475]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  {
[29251]134    $fills = rtrim($params['fills'], ',');
135    $fills = explode(',', $fills);
136    $fills = array_flip($fills);
[17475]137 
138    $updates = array();
[29251]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']))
[20670]144    {
145      $latlon = explode(' ', $photo['latlon']);
146      if (count($latlon) == 2)
147      {
[26198]148        $updates['latitude'] = pwg_db_real_escape_string($latlon[0]);
149        $updates['longitude'] = pwg_db_real_escape_string($latlon[1]);
[20670]150      }
151    }
[17475]152   
153    if (count($updates))
154    {
155      single_update(
156        IMAGES_TABLE,
157        $updates,
158        array('id' => $photo['image_id'])
159        );
160    }
161   
[29251]162    if (!empty($photo['tags']) and isset($fills['fill_tags']))
[17475]163    {
164      set_tags(get_tag_ids($photo['tags']), $photo['image_id']);
165    }
166  }
167 
[26198]168  return l10n('Photo "%s" imported', $photo['title']);
[17475]169}
Note: See TracBrowser for help on using the repository browser.