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
Line 
1<?php
2defined('FLICKR_PATH') or die('Hacking attempt!');
3
4function flickr_add_ws_method($arr)
5{
6  $service = &$arr[0];
7
8  $service->addMethod(
9    'pwg.images.addFlickr',
10    'ws_images_addFlickr',
11    array(
12      'id' => array(),
13      'category' => array(),
14      'fills' => array('default' =>null),
15      ),
16    'Used by Flickr2Piwigo',
17    null,
18    array('hidden'=>true)
19    );
20}
21
22function ws_images_addFlickr($photo, &$service)
23{
24  if (!is_admin())
25  {
26    return new PwgError(403, 'Forbidden');
27  }
28
29  global $conf;
30
31  if (empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']))
32  {
33    return new PwgError(null, l10n('Please fill your API keys on the configuration tab'));
34  }
35
36  include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
37  include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php');
38  include_once(FLICKR_PATH . 'include/functions.inc.php');
39
40  if (test_remote_download() === false)
41  {
42    return new PwgError(null, l10n('No download method available'));
43  }
44
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);
49
50  // user
51  $u = $flickr->test_login();
52  if ($u === false or empty($_SESSION['phpFlickr_auth_token']))
53  {
54    return new PwgError(403, l10n('API not authenticated'));
55  }
56
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']);
62
63  // copy file
64  if (download_remote_file($photo['url'], $photo['path']) == false)
65  {
66    return new PwgError(null, l10n('Can\'t download file'));
67  }
68
69  // category
70  if (!preg_match('#^[0-9]+$#', $photo['category']))
71  {
72    $categories_names = explode(',', $photo['category']);
73
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);
82
83      if (pwg_db_num_rows($result))
84      {
85        list($cat_id) = pwg_db_fetch_row($result);
86        $photo['category'][] = $cat_id;
87      }
88      else
89      {
90
91        $cat = create_virtual_category($category_name);
92        $photo['category'][] = $cat['id'];
93      }
94    }
95  }
96  else
97  {
98    $photo['category'] = array($photo['category']);
99  }
100
101  // add photo
102  $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), $photo['category']);
103
104  // do some updates
105  if (!empty($photo['fills']))
106  {
107    $photo['fills'] = rtrim($photo['fills'], ',');
108    $photo['fills'] = explode(',', $photo['fills']);
109
110    $updates = array();
111    if (in_array('fill_name', $photo['fills']))   $updates['name'] = pwg_db_real_escape_string($photo['title']);
112    if (in_array('fill_posted', $photo['fills'])) $updates['date_available'] = date('Y-m-d H:i:s', $photo['dates']['posted']);
113    if (in_array('fill_taken', $photo['fills']))  $updates['date_creation'] = $photo['dates']['taken'];
114    if (in_array('fill_author', $photo['fills'])) $updates['author'] = pwg_db_real_escape_string($photo['owner']['username']);
115    if (in_array('fill_description', $photo['fills'])) $updates['comment'] = pwg_db_real_escape_string(@$photo['description']);
116    if (in_array('fill_geotag', $photo['fills']) and !empty($photo['location']) )
117    {
118      $updates['latitude'] = pwg_db_real_escape_string($photo['location']['latitude']);
119      $updates['longitude'] = pwg_db_real_escape_string($photo['location']['longitude']);
120    }
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    }
127
128    if (count($updates))
129    {
130      single_update(
131        IMAGES_TABLE,
132        $updates,
133        array('id' => $photo['image_id'])
134        );
135    }
136
137    if (!empty($photo['tags']['tag']) and in_array('fill_tags', $photo['fills']))
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  }
144
145  return l10n('Photo "%s" imported', $photo['title']);
146}
Note: See TracBrowser for help on using the repository browser.