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

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

import privacy level

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  }
[27242]100 
101  // level
102  $level = 0;
103  if (in_array('level', $photo['fills']) && !$photo['visibility']['ispublic'])
104  {
105    $level = 8;
106    if ($photo['visibility']['isfamily']) $level = 4;
107    if ($photo['visibility']['isfriend']) $level = 2;
108  }
[26180]109
[16071]110  // add photo
[27242]111  $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), $photo['category'], $level);
[26180]112
[16071]113  // do some updates
114  if (!empty($photo['fills']))
115  {
116    $photo['fills'] = rtrim($photo['fills'], ',');
117    $photo['fills'] = explode(',', $photo['fills']);
[26180]118
[16071]119    $updates = array();
[26180]120    if (in_array('fill_name', $photo['fills']))   $updates['name'] = pwg_db_real_escape_string($photo['title']);
[24660]121    if (in_array('fill_posted', $photo['fills'])) $updates['date_available'] = date('Y-m-d H:i:s', $photo['dates']['posted']);
[16071]122    if (in_array('fill_taken', $photo['fills']))  $updates['date_creation'] = $photo['dates']['taken'];
[24815]123    if (in_array('fill_author', $photo['fills'])) $updates['author'] = pwg_db_real_escape_string($photo['owner']['username']);
[25790]124    if (in_array('fill_description', $photo['fills'])) $updates['comment'] = pwg_db_real_escape_string(@$photo['description']);
[25789]125    if (in_array('fill_geotag', $photo['fills']) and !empty($photo['location']) )
126    {
[26180]127      $updates['latitude'] = pwg_db_real_escape_string($photo['location']['latitude']);
128      $updates['longitude'] = pwg_db_real_escape_string($photo['location']['longitude']);
[25789]129    }
[26180]130
[16071]131    if (count($updates))
132    {
133      single_update(
134        IMAGES_TABLE,
135        $updates,
136        array('id' => $photo['image_id'])
137        );
138    }
[26180]139
140    if (!empty($photo['tags']['tag']) and in_array('fill_tags', $photo['fills']))
[16071]141    {
142      $raw_tags = array_map(create_function('$t', 'return $t["_content"];'), $photo['tags']['tag']);
143      $raw_tags = implode(',', $raw_tags);
144      set_tags(get_tag_ids($raw_tags), $photo['image_id']);
145    }
146  }
[26180]147
148  return l10n('Photo "%s" imported', $photo['title']);
[16071]149}
Note: See TracBrowser for help on using the repository browser.