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

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

I use the same function in three plugins

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