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

Last change on this file since 17476 was 17476, checked in by mistic100, 12 years ago

some code corrections

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