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

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