source: extensions/flickr2piwigo/main.inc.php @ 16063

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

initial version

File size: 4.6 KB
Line 
1<?php 
2/*
3Plugin Name: Flickr2Piwigo
4Version: auto
5Description: Extension for importing pictures from your Flickr account
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=
7Author: Mistic
8Author URI: http://www.strangeplanet.fr
9*/
10
11if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
12
13global $conf;
14
15define('FLICKR_PATH', PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
16define('FLICKR_ADMIN', get_root_url() . 'admin.php?page=plugin-' . basename(dirname(__FILE__)));
17define('FLICKR_FS_CACHE', $conf['data_location'].'flickr_cache/');
18
19add_event_handler('get_admin_plugin_menu_links', 'flickr2_admin_menu');
20
21function flickr2_admin_menu($menu) 
22{
23  array_push($menu, array(
24    'NAME' => 'Flickr2Piwigo',
25    'URL' => FLICKR_ADMIN,
26  ));
27  return $menu;
28}
29
30add_event_handler('ws_add_methods', 'flickr2_add_ws_method');
31
32function flickr2_add_ws_method($arr)
33{
34  $service = &$arr[0];
35 
36  $service->addMethod(
37    'pwg.images.addFlickr',
38    'ws_images_addFlickr',
39    array(
40      'category' => array('default' => null),   
41      'id' => array('default' => null),
42      'fills' => array('default' =>null),
43      ),
44    'Used by Flickr2Piwigo, fills € (fill_name,fill_posted,fill_taken,fill_author,fill_tags)'
45    );
46}
47
48function ws_images_addFlickr($photo, &$service)
49{
50  global $conf;
51 
52  if (!is_admin())
53  {
54    return new PwgError(403, 'Forbidden');
55  }
56 
57  $conf['flickr2piwigo'] = unserialize($conf['flickr2piwigo']);
58 
59  if (empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']) or empty($conf['flickr2piwigo']['username']))
60  {
61    return new PwgError(500, l10n('Please fill your API keys on the configuration tab'));
62  }
63 
64  if (empty($_SESSION['phpFlickr_auth_token']))
65  {
66    return new PwgError(403, l10n('API not authenticated'));
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        array_push($photo['category'], $cat_id);
87      }
88      else
89      {
90        include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
91        $cat = create_virtual_category($category_name);
92        array_push($photo['category'], $cat_id['id']);
93      }
94    }
95  }
96  else
97  {
98    $photo['category'] = array($photo['category']);
99  }
100 
101  // init flickr API
102  include_once(FLICKR_PATH . 'include/phpFlickr/phpFlickr.php');
103  $flickr = new phpFlickr($conf['flickr2piwigo']['api_key'], $conf['flickr2piwigo']['secret_key']);
104  $flickr->enableCache('fs', FLICKR_FS_CACHE);
105 
106  // photos infos
107  $photo_f = $flickr->photos_getInfo($photo['id']);
108  $photo = array_merge($photo, $photo_f['photo']);
109  $photo['url'] = $flickr->get_biggest_size($photo['id'], 'original');
110  $photo['path'] = FLICKR_FS_CACHE . 'flickr-'.$conf['flickr2piwigo']['username'].'-'.$photo['id'].'.'.get_extension($photo['url']);
111 
112  // copy file
113  $file = fopen($photo['url'], "rb");
114  $newf = fopen($photo['path'], "wb");
115  while (!feof($file))
116  {
117    fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
118  }
119  fclose($file);
120  fclose($newf);
121 
122  // add to database
123  include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php');
124  $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), $photo['category']);
125 
126  // do some updates
127  if (!empty($photo['fills']))
128  {
129    $photo['fills'] = rtrim($photo['fills'], ',');
130    $photo['fills'] = explode(',', $photo['fills']);
131 
132    $updates = array();
133    if (in_array('fill_name', $photo['fills']))   $updates['name'] = $photo['title']; 
134    if (in_array('fill_posted', $photo['fills'])) $updates['date_available'] = date('Y-d-m H:i:s', $photo['dates']['posted']);
135    if (in_array('fill_taken', $photo['fills']))  $updates['date_creation'] = $photo['dates']['taken'];
136    if (in_array('fill_author', $photo['fills'])) $updates['author'] = $conf['flickr2piwigo']['username'];
137   
138    if (count($updates))
139    {
140      single_update(
141        IMAGES_TABLE,
142        $updates,
143        array('id' => $photo['image_id'])
144        );
145    }
146   
147    if ( !empty($photo['tags']['tag']) and in_array('fill_tags', $photo['fills']) )
148    {
149      $raw_tags = array_map(create_function('$t', 'return $t["_content"];'), $photo['tags']['tag']);
150      $raw_tags = implode(',', $raw_tags);
151      set_tags(get_tag_ids($raw_tags), $photo['image_id']);
152    }
153  }
154 
155  return sprintf(l10n('%s imported'), $photo['title']);
156}
157
158?>
Note: See TracBrowser for help on using the repository browser.