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

Last change on this file since 17225 was 17225, checked in by ddtddt, 12 years ago

[extensions] - pbase2piwigo - new extension by mistic - first release

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