source: extensions/pbase2piwigo/include/ws_functions.inc.php @ 26201

Last change on this file since 26201 was 26201, checked in by mistic100, 10 years ago

update for 2.6

File size: 6.2 KB
Line 
1<?php
2defined('PBASE_PATH') or die('Hacking attempt!');
3
4function pbase_add_ws_method($arr)
5{
6  $service = &$arr[0];
7 
8  $service->addMethod(
9    'pwg.pBase.addImage',
10    'ws_pBase_add_image',
11    array(
12      'url' => array(),
13      'category' => array('default' => null),
14      'fills' => array('default' => 'fill_name,fill_taken,fill_author,fill_comment,fill_tags'),
15      ),
16    'Used by PBase2Piwigo',
17    null,
18    array('hidden'=>true)
19    );
20   
21  $service->addMethod(
22    'pwg.pBase.addCat',
23    'ws_pBase_add_cat',
24    array(
25      'path' => array(),   
26      'parent_id' => array('default' => null),
27      'recursive' => array('default' => true),
28      ),
29    'Used by PBase2Piwigo',
30    null,
31    array('hidden'=>true)
32    );
33   
34  $service->addMethod(
35    'pwg.pBase.parse',
36    'ws_pBase_parse',
37    array(
38      'url' => array(),
39      'path' => array(),
40      ),
41    'Used by PBase2Piwigo',
42    null,
43    array('hidden'=>true)
44    );
45}
46
47/**
48 * pBase parse category
49 */
50function ws_pBase_parse($params, &$service)
51{
52  if (!is_admin())
53  {
54    return new PwgError(401, 'Forbidden');
55  }
56 
57  if (empty($params['path']) or empty($params['url']))
58  {
59    return new PwgError(403, l10n('Missing parameters'));
60  }
61 
62  include_once(PBASE_PATH.'include/functions.inc.php');
63 
64  if (!test_remote_download())
65  {
66    return new PwgError(null, l10n('No download method available'));
67  }
68  if ($params['path'] == '/root' and check_account($params['url']) == false)
69  {
70    return new PwgError(null, l10n('Invalid user id'));
71  }
72 
73  // get current tree
74  $tree_file = PBASE_FS_CACHE.'tree.json';
75  if (file_exists($tree_file))
76  {
77    $tree = json_decode(file_get_contents($tree_file), true);
78  }
79  else
80  {
81    $tree = array(
82      'root' => array(),
83      );
84  }
85  // parse sub-tree
86  $current = &get_current_cat($tree, $params['path']);
87  $path = substr($params['path'], 0, strrpos($params['path'], '/'));
88  $current = parse_category($params['url'], $path, true, true);
89 
90  // save tree
91  file_put_contents($tree_file, json_encode($tree));
92 
93  // return datas for AJAX queue
94  return $current;
95}
96
97/**
98 * pBase add category
99 */
100function ws_pBase_add_cat($params, &$service)
101{
102  if (!is_admin())
103  {
104    return new PwgError(401, 'Forbidden');
105  }
106 
107  if (empty($params['path']))
108  {
109    return new PwgError(null, l10n('Missing parameters'));
110  }
111 
112  include_once(PBASE_PATH.'include/functions.inc.php');
113 
114  // get current tree
115  $tree_file = PBASE_FS_CACHE.'tree.json';
116  $tree = json_decode(file_get_contents($tree_file), true);
117  $category = &get_current_cat($tree, $params['path']);
118 
119  $category['title'] = pwg_db_real_escape_string($category['title']);
120 
121  // add category
122  $query = '
123SELECT id, name
124  FROM '.CATEGORIES_TABLE.'
125  WHERE name = "'.$category['title'].' <!--pbase-->"
126;';
127  $result = pwg_query($query);
128 
129  if (pwg_db_num_rows($result))
130  {
131    list($category_id) = pwg_db_fetch_row($result);
132  }
133  else
134  {
135    $new_cat = $service->invoke('pwg.categories.add', array(
136      'name' => $category['title'].' <!--pbase-->',
137      'parent' => $params['parent_id'],
138      'comment' => pwg_db_real_escape_string($category['description']),
139      ));
140    $category_id = $new_cat['id'];
141  }
142 
143  // return datas for AJAX queue
144  $output = array(
145    'category_id' => $category_id,
146    'pictures' => array(),
147    'categories' => array(),
148    'message' => l10n('Album "%s" created', $category['title']),
149    );
150   
151  foreach ($category['pictures'] as &$pict)
152  {
153    $output['pictures'][] = $pict['url'];
154  }
155  if ($params['recursive'])
156  {
157    foreach ($category['categories'] as &$cat)
158    {
159      $output['categories'][] = $cat['path'];
160    }
161  }
162 
163  return $output;
164}
165
166/**
167 * pBase add picture
168 */
169function ws_pBase_add_image($params, &$service)
170{
171  if (!is_admin())
172  {
173    return new PwgError(401, 'Forbidden');
174  }
175 
176  if (empty($params['url']))
177  {
178    return new PwgError(403, l10n('Missing parameters'));
179  }
180 
181  include_once(PBASE_PATH.'include/functions.inc.php');
182  include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
183  include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php');
184   
185  // photo infos
186  $photo = parse_image($params['url'], false);
187  if (empty($photo))
188  {
189    return new PwgError(null, l10n('Invalid picture'));
190  }
191 
192  $photo['local_path'] = PBASE_FS_CACHE . 'pbase-'.$photo['id'].'.'.get_extension($photo['path']);
193 
194  $query = '
195SELECT id
196  FROM '.IMAGES_TABLE.'
197  WHERE file = "'.basename($photo['local_path']).'"
198;';
199  $result = pwg_query($query);
200 
201  if (pwg_db_num_rows($result))
202  {
203    list($photo['image_id']) = pwg_db_fetch_row($result);
204    associate_images_to_categories(array($photo['image_id']), array($params['category']));
205  }
206  else
207  {
208    // copy file
209    if (download_remote_file($photo['path'], $photo['local_path']) === false)
210    {
211      return new PwgError(500, l10n('No download method available'));
212    }
213   
214    // add photo
215    $photo['image_id'] = add_uploaded_file($photo['local_path'], basename($photo['local_path']), array($params['category']));
216 
217    // do some updates
218    if (!empty($params['fills']))
219    {
220      $params['fills'] = rtrim($params['fills'], ',');
221      $params['fills'] = explode(',', $params['fills']);
222   
223      $updates = array();
224      if (in_array('fill_name', $params['fills']))    $updates['name'] = pwg_db_real_escape_string($photo['title']); 
225      if (in_array('fill_posted', $params['fills']))  $updates['date_available'] = date('Y-m-d H:i:s', strtotime($photo['date']));
226      if (in_array('fill_taken', $params['fills']))   $updates['date_creation'] = date('Y-m-d H:i:s', strtotime($photo['date']));
227      if (in_array('fill_author', $params['fills']))  $updates['author'] = pwg_db_real_escape_string($photo['author']);
228      if (in_array('fill_comment', $params['fills'])) $updates['comment'] = pwg_db_real_escape_string($photo['description']);
229     
230      if (count($updates))
231      {
232        single_update(
233          IMAGES_TABLE,
234          $updates,
235          array('id' => $photo['image_id'])
236          );
237      }
238     
239      if (!empty($photo['keywords']) and in_array('fill_tags', $params['fills']))
240      {
241        $raw_tags = implode(',', $photo['keywords']);
242        set_tags(get_tag_ids($raw_tags), $photo['image_id']);
243      }
244    }
245  }
246 
247  return l10n('Photo "%s" imported', $photo['title']);
248}
Note: See TracBrowser for help on using the repository browser.