source: extensions/flickr2piwigo/include/ws_functions.inc.php @ 27265

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

fix bugs in "import all" and level sync

File size: 4.5 KB
RevLine 
[16071]1<?php
[26180]2defined('FLICKR_PATH') or die('Hacking attempt!');
[16071]3
4function flickr_add_ws_method($arr)
5{
6  $service = &$arr[0];
[26180]7
[16071]8  $service->addMethod(
9    'pwg.images.addFlickr',
10    'ws_images_addFlickr',
11    array(
[17476]12      'id' => array(),
13      'category' => array(),
[16071]14      'fills' => array('default' =>null),
15      ),
[26180]16    'Used by Flickr2Piwigo',
17    null,
18    array('hidden'=>true)
[16071]19    );
20}
21
22function ws_images_addFlickr($photo, &$service)
23{
24  if (!is_admin())
25  {
26    return new PwgError(403, 'Forbidden');
27  }
[26180]28
[16071]29  global $conf;
[26180]30
31  if (empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']))
[16071]32  {
[17476]33    return new PwgError(null, l10n('Please fill your API keys on the configuration tab'));
[16071]34  }
[26180]35
[16071]36  include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
37  include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php');
[17312]38  include_once(FLICKR_PATH . 'include/functions.inc.php');
[26180]39
[17476]40  if (test_remote_download() === false)
41  {
42    return new PwgError(null, l10n('No download method available'));
43  }
[26180]44
[16071]45  // init flickr API
46  include_once(FLICKR_PATH . 'include/phpFlickr/phpFlickr.php');
47  $flickr = new phpFlickr($conf['flickr2piwigo']['api_key'], $conf['flickr2piwigo']['secret_key']);
48  $flickr->enableCache('fs', FLICKR_FS_CACHE);
[26180]49
[16071]50  // user
51  $u = $flickr->test_login();
[26180]52  if ($u === false or empty($_SESSION['phpFlickr_auth_token']))
[16071]53  {
54    return new PwgError(403, l10n('API not authenticated'));
55  }
[26180]56
[16071]57  // photos infos
58  $photo_f = $flickr->photos_getInfo($photo['id']);
59  $photo = array_merge($photo, $photo_f['photo']);
60  $photo['url'] = $flickr->get_biggest_size($photo['id'], 'original');
61  $photo['path'] = FLICKR_FS_CACHE . 'flickr-'.$u['username'].'-'.$photo['id'].'.'.get_extension($photo['url']);
[26180]62
[16071]63  // copy file
[16085]64  if (download_remote_file($photo['url'], $photo['path']) == false)
[16071]65  {
[17476]66    return new PwgError(null, l10n('Can\'t download file'));
[16071]67  }
[26180]68
[16071]69  // category
70  if (!preg_match('#^[0-9]+$#', $photo['category']))
71  {
72    $categories_names = explode(',', $photo['category']);
[26180]73
[16071]74    $photo['category'] = array();
75    foreach ($categories_names as $category_name)
76    {
77      $query = '
78SELECT id FROM '.CATEGORIES_TABLE.'
79  WHERE LOWER(name) = "'.strtolower($category_name).'"
80;';
81      $result = pwg_query($query);
[26180]82
[16071]83      if (pwg_db_num_rows($result))
84      {
85        list($cat_id) = pwg_db_fetch_row($result);
[26180]86        $photo['category'][] = $cat_id;
[16071]87      }
88      else
89      {
[26180]90
[16071]91        $cat = create_virtual_category($category_name);
[26180]92        $photo['category'][] = $cat['id'];
[16071]93      }
94    }
95  }
96  else
97  {
98    $photo['category'] = array($photo['category']);
99  }
[26180]100
[16071]101  // add photo
[27265]102  $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), $photo['category']);
[26180]103
[16071]104  // do some updates
105  if (!empty($photo['fills']))
106  {
107    $photo['fills'] = rtrim($photo['fills'], ',');
108    $photo['fills'] = explode(',', $photo['fills']);
[26180]109
[16071]110    $updates = array();
[26180]111    if (in_array('fill_name', $photo['fills']))   $updates['name'] = pwg_db_real_escape_string($photo['title']);
[24660]112    if (in_array('fill_posted', $photo['fills'])) $updates['date_available'] = date('Y-m-d H:i:s', $photo['dates']['posted']);
[16071]113    if (in_array('fill_taken', $photo['fills']))  $updates['date_creation'] = $photo['dates']['taken'];
[24815]114    if (in_array('fill_author', $photo['fills'])) $updates['author'] = pwg_db_real_escape_string($photo['owner']['username']);
[25790]115    if (in_array('fill_description', $photo['fills'])) $updates['comment'] = pwg_db_real_escape_string(@$photo['description']);
[25789]116    if (in_array('fill_geotag', $photo['fills']) and !empty($photo['location']) )
117    {
[26180]118      $updates['latitude'] = pwg_db_real_escape_string($photo['location']['latitude']);
119      $updates['longitude'] = pwg_db_real_escape_string($photo['location']['longitude']);
[25789]120    }
[27265]121    if (in_array('level', $photo['fills']) && !$photo['visibility']['ispublic'])
122    {
123      $updates['level'] = 8;
124      if ($photo['visibility']['isfamily']) $updates['level'] = 4;
125      if ($photo['visibility']['isfriend']) $updates['level'] = 2;
126    }
[26180]127
[16071]128    if (count($updates))
129    {
130      single_update(
131        IMAGES_TABLE,
132        $updates,
133        array('id' => $photo['image_id'])
134        );
135    }
[26180]136
137    if (!empty($photo['tags']['tag']) and in_array('fill_tags', $photo['fills']))
[16071]138    {
139      $raw_tags = array_map(create_function('$t', 'return $t["_content"];'), $photo['tags']['tag']);
140      $raw_tags = implode(',', $raw_tags);
141      set_tags(get_tag_ids($raw_tags), $photo['image_id']);
142    }
143  }
[26180]144
145  return l10n('Photo "%s" imported', $photo['title']);
[16071]146}
Note: See TracBrowser for help on using the repository browser.