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

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

bug with albums with close names
bug with apostrophe in albums name and description

File size: 6.2 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  $category['title'] = pwg_db_real_escape_string($category['title']);
114 
115  // add category
116  $query = '
117SELECT id, name
118  FROM '.CATEGORIES_TABLE.'
119  WHERE name = "'.$category['title'].' <!--pbase-->"
120;';
121  $result = pwg_query($query);
122 
123  if (pwg_db_num_rows($result))
124  {
125    list($category_id) = pwg_db_fetch_row($result);
126  }
127  else
128  {
129    $category_id = ws_categories_add(array(
130      'name' => $category['title'].' <!--pbase-->',
131      'parent' => $params['parent_id'],
132      'comment' => pwg_db_real_escape_string($category['description']),
133      ), $service);
134    $category_id = $category_id['id'];
135  }
136 
137  // return datas for AJAX queue
138  $output = array(
139    'category_id' => $category_id,
140    'pictures' => array(),
141    'categories' => array(),
142    'message' => sprintf(l10n('Album "%s" created'), $category['title']),
143    );
144   
145  foreach ($category['pictures'] as &$pict)
146  {
147    array_push($output['pictures'], $pict['url']);
148  }
149  if ($params['recursive'])
150  {
151    foreach ($category['categories'] as &$cat)
152    {
153      array_push($output['categories'], $cat['path']);
154    }
155  }
156 
157  return $output;
158}
159
160/**
161 * pBase add picture
162 */
163function ws_pBase_add_image($params, &$service)
164{
165  if (!is_admin())
166  {
167    return new PwgError(401, 'Forbidden');
168  }
169 
170  if (empty($params['url']))
171  {
172    return new PwgError(403, l10n('Missing parameters'));
173  }
174 
175  include_once(PBASE_PATH.'include/functions.inc.php');
176  include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
177  include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php');
178   
179  // photo infos
180  $photo = parse_image($params['url'], false);
181  if (empty($photo))
182  {
183    return new PwgError(null, l10n('Invalid picture'));
184  }
185 
186  $photo['local_path'] = PBASE_FS_CACHE . 'pbase-'.$photo['id'].'.'.get_extension($photo['path']);
187 
188  $query = '
189SELECT id
190  FROM '.IMAGES_TABLE.'
191  WHERE file = "'.basename($photo['local_path']).'"
192;';
193  $result = pwg_query($query);
194 
195  if (pwg_db_num_rows($result))
196  {
197    list($photo['image_id']) = pwg_db_fetch_row($result);
198    associate_images_to_categories(array($photo['image_id']), array($params['category']));
199  }
200  else
201  {
202    // copy file
203    if (download_remote_file($photo['path'], $photo['local_path']) === false)
204    {
205      return new PwgError(500, l10n('No download method available'));
206    }
207   
208    // add photo
209    $photo['image_id'] = add_uploaded_file($photo['local_path'], basename($photo['local_path']), array($params['category']));
210 
211    // do some updates
212    if (!empty($params['fills']))
213    {
214      $params['fills'] = rtrim($params['fills'], ',');
215      $params['fills'] = explode(',', $params['fills']);
216   
217      $updates = array();
218      if (in_array('fill_name', $params['fills']))    $updates['name'] = pwg_db_real_escape_string($photo['title']); 
219      if (in_array('fill_posted', $params['fills']))  $updates['date_available'] = date('Y-d-m H:i:s', strtotime($photo['date']));
220      if (in_array('fill_taken', $params['fills']))   $updates['date_creation'] = date('Y-d-m H:i:s', strtotime($photo['date']));
221      if (in_array('fill_author', $params['fills']))  $updates['author'] = pwg_db_real_escape_string($photo['author']);
222      if (in_array('fill_comment', $params['fills'])) $updates['comment'] = pwg_db_real_escape_string($photo['description']);
223     
224      if (count($updates))
225      {
226        single_update(
227          IMAGES_TABLE,
228          $updates,
229          array('id' => $photo['image_id'])
230          );
231      }
232     
233      if ( !empty($photo['keywords']) and in_array('fill_tags', $params['fills']) )
234      {
235        $raw_tags = implode(',', $photo['keywords']);
236        set_tags(get_tag_ids($raw_tags), $photo['image_id']);
237      }
238    }
239  }
240 
241  return sprintf(l10n('Photo "%s" imported'), $photo['title']);
242}
243
244?>
Note: See TracBrowser for help on using the repository browser.