source: extensions/url_uploader/include/ws_functions.inc.php @ 26387

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

update for 2.6

File size: 3.3 KB
Line 
1<?php
2defined('URLUPLOADER_PATH') or die('Hacking attempt!');
3
4function urluploader_ws_add_methods($arr)
5{
6  global $conf;
7  $service = &$arr[0];
8 
9  $service->addMethod(
10    'pwg.images.addRemote',
11    'ws_images_addRemote',
12    array(
13      'file_url' => array(),
14      'category' => array(),
15      'name' => array('default' => null),
16      'level' => array(
17        'default' => 0,
18        'maxValue' => $conf['available_permission_levels']
19        ),
20      'url_in_comment' => array('default' => true),
21      ),
22    'Add image from remote URL.',
23    null,
24    array('admin_only'=>true)
25    );
26}
27
28function ws_images_addRemote($params, &$service)
29{
30  global $conf;
31 
32  if (!is_admin())
33  {
34    return new PwgError(401, 'Access denied');
35  }
36 
37  load_language('plugin.lang', URLUPLOADER_PATH);
38 
39  $params = array_map('trim', $params);
40 
41  $allowed_extensions = array('jpg','jpeg','png','gif');
42  $allowed_mimes = array('image/jpeg', 'image/png', 'image/gif');
43 
44  // check empty url
45  if (empty($params['file_url']))
46  {
47    return new PwgError(WS_ERR_INVALID_PARAM, l10n('File URL is empty'));
48  }
49  // check remote url
50  if (!url_is_remote($params['file_url']))
51  {
52    return new PwgError(WS_ERR_INVALID_PARAM, l10n('Invalid file URL'));
53  }
54  // check file extension
55  if (!in_array(strtolower(get_extension($params['file_url'])), $allowed_extensions))
56  {
57    return new PwgError(WS_ERR_INVALID_PARAM, l10n('Invalid file type'));
58  }
59
60  // download file
61  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
62 
63  $temp_filename = $conf['data_location'].basename($params['file_url']);
64  $file = fopen($temp_filename, 'w+');
65  $result = fetchRemote($params['file_url'], $file);
66  fclose($file);
67 
68  // download failed ?
69  if (!$result)
70  {
71    @unlink($temp_filename);
72    return new PwgError(WS_ERR_INVALID_PARAM, l10n('Unable to download file'));
73  }
74  // check mime-type
75  if (!in_array(get_mime($temp_filename, $allowed_mimes[0]), $allowed_mimes))
76  {
77    @unlink($temp_filename);
78    return new PwgError(WS_ERR_INVALID_PARAM, l10n('Invalid file type'));
79  }
80
81  // add photo
82  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
83 
84  $image_id = add_uploaded_file(
85    $temp_filename, 
86    basename($temp_filename), 
87    array($params['category']), 
88    $params['level']
89    );
90 
91  $updates = array();
92  if (!empty($params['name']))
93  {
94    $updates['name'] = $params['name'];
95  }
96  if ($params['url_in_comment']=='true')
97  {
98    $url = parse_url($params['file_url']);
99    $url = $url['scheme'].'://'.$url['host'];
100    $updates['comment'] = '<a href="'. $url . '">'. $url .'</a>';
101  }
102 
103  single_update(
104    IMAGES_TABLE,
105    $updates,
106    array('id' => $image_id)
107    );
108 
109 
110  // return infos
111  $query = '
112SELECT id, name, permalink
113  FROM '.CATEGORIES_TABLE.'
114  WHERE id = '.$params['category'].'
115;';
116  $category = pwg_db_fetch_assoc(pwg_query($query));
117
118  $url_params = array(
119    'image_id' => $image_id,
120    'section' => 'categories',
121    'category' => $category,
122    );
123   
124  $query = '
125SELECT id, path
126  FROM '.IMAGES_TABLE.'
127  WHERE id = '.$image_id.'
128;';
129  $image_infos = pwg_db_fetch_assoc(pwg_query($query));
130
131  return array(
132    'image_id' => $image_id,
133    'url' => make_picture_url($url_params),
134    'thumbnail_url' => preg_replace('#^'.PHPWG_ROOT_PATH.'#', './', DerivativeImage::thumb_url($image_infos)),
135    );
136}
Note: See TracBrowser for help on using the repository browser.