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

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

code cleanup, don't ask username anymore (prohibit importing photos from another account)

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      'category' => array('default' => null),   
13      'id' => array('default' => null),
14      'fills' => array('default' =>null),
15      ),
16    'Used by Flickr2Piwigo, fills € (fill_name,fill_posted,fill_taken,fill_author,fill_tags)'
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(500, 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 
38  // init flickr API
39  include_once(FLICKR_PATH . 'include/phpFlickr/phpFlickr.php');
40  $flickr = new phpFlickr($conf['flickr2piwigo']['api_key'], $conf['flickr2piwigo']['secret_key']);
41  $flickr->enableCache('fs', FLICKR_FS_CACHE);
42 
43  // user
44  $u = $flickr->test_login();
45  if ( $u === false or empty($_SESSION['phpFlickr_auth_token']) )
46  {
47    return new PwgError(403, l10n('API not authenticated'));
48  }
49 
50  // photos infos
51  $photo_f = $flickr->photos_getInfo($photo['id']);
52  $photo = array_merge($photo, $photo_f['photo']);
53  $photo['url'] = $flickr->get_biggest_size($photo['id'], 'original');
54  $photo['path'] = FLICKR_FS_CACHE . 'flickr-'.$u['username'].'-'.$photo['id'].'.'.get_extension($photo['url']);
55 
56  // copy file
57  $file = fopen($photo['url'], "rb");
58  $newf = fopen($photo['path'], "wb");
59  while (!feof($file))
60  {
61    fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
62  }
63  fclose($file);
64  fclose($newf);
65 
66  // category
67  if (!preg_match('#^[0-9]+$#', $photo['category']))
68  {
69    $categories_names = explode(',', $photo['category']);
70   
71    $photo['category'] = array();
72    foreach ($categories_names as $category_name)
73    {
74      $query = '
75SELECT id FROM '.CATEGORIES_TABLE.'
76  WHERE LOWER(name) = "'.strtolower($category_name).'"
77;';
78      $result = pwg_query($query);
79     
80      if (pwg_db_num_rows($result))
81      {
82        list($cat_id) = pwg_db_fetch_row($result);
83        array_push($photo['category'], $cat_id);
84      }
85      else
86      {
87       
88        $cat = create_virtual_category($category_name);
89        array_push($photo['category'], $cat['id']);
90      }
91    }
92  }
93  else
94  {
95    $photo['category'] = array($photo['category']);
96  }
97 
98  // add photo
99  $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), $photo['category']);
100 
101  // do some updates
102  if (!empty($photo['fills']))
103  {
104    $photo['fills'] = rtrim($photo['fills'], ',');
105    $photo['fills'] = explode(',', $photo['fills']);
106 
107    $updates = array();
108    if (in_array('fill_name', $photo['fills']))   $updates['name'] = $photo['title']; 
109    if (in_array('fill_posted', $photo['fills'])) $updates['date_available'] = date('Y-d-m H:i:s', $photo['dates']['posted']);
110    if (in_array('fill_taken', $photo['fills']))  $updates['date_creation'] = $photo['dates']['taken'];
111    if (in_array('fill_author', $photo['fills'])) $updates['author'] = $photo['owner']['username'];
112   
113    if (count($updates))
114    {
115      single_update(
116        IMAGES_TABLE,
117        $updates,
118        array('id' => $photo['image_id'])
119        );
120    }
121   
122    if ( !empty($photo['tags']['tag']) and in_array('fill_tags', $photo['fills']) )
123    {
124      $raw_tags = array_map(create_function('$t', 'return $t["_content"];'), $photo['tags']['tag']);
125      $raw_tags = implode(',', $raw_tags);
126      set_tags(get_tag_ids($raw_tags), $photo['image_id']);
127    }
128  }
129 
130  return sprintf(l10n('%s imported'), $photo['title']);
131}
132
133?>
Note: See TracBrowser for help on using the repository browser.