source: extensions/url_uploader/admin.php @ 19804

Last change on this file since 19804 was 19804, checked in by mistic100, 11 years ago

add plugin URL Uploader

File size: 6.4 KB
Line 
1<?php
2defined('URLUPLOADER_PATH') or die('Hacking attempt!');
3 
4global $template, $page, $conf;
5
6load_language('plugin.lang', URLUPLOADER_PATH);
7
8// +-----------------------------------------------------------------------+
9// | URL Uploader tab                                                      |
10// +-----------------------------------------------------------------------+
11define('PHOTOS_ADD_BASE_URL', get_root_url().'admin.php?page=photos_add');
12
13include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
14include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
15include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
16include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php');
17
18$allowed_extensions = array('jpg','jpeg','png','gif');
19$allowed_mimes = array('image/jpeg', 'image/png', 'image/gif');
20
21$tabsheet = new tabsheet();
22$tabsheet->set_id('photos_add');
23$tabsheet->select('url_uploader');
24$tabsheet->assign();
25
26
27// +-----------------------------------------------------------------------+
28// |                             process form                              |
29// +-----------------------------------------------------------------------+
30if (isset($_GET['processed']))
31{
32  $category_id = $_POST['category'];
33  $image_ids = array();
34  $page['thumbnails'] = array();
35 
36  // SINGLE UPLOAD
37  if ($_GET['upload_mode'] == 'single')
38  {
39    $_POST = array_map('trim', $_POST);
40   
41    // check empty url
42    if (empty($_POST['file_url']))
43    {
44      $page['errors'][] = l10n('File URL is empty');
45    }
46    // check remote url
47    else if (!url_is_remote($_POST['file_url']))
48    {
49      $page['errors'][] = l10n('Invalid file URL');
50    }
51    // check file extension
52    else if (!in_array(strtolower(get_extension($_POST['file_url'])), $allowed_extensions))
53    {
54      $page['errors'][] = l10n('Invalid file type');
55    }
56    // continue...
57    else
58    {
59      $temp_filename = $conf['data_location'].basename($_POST['file_url']);
60      $file = fopen($temp_filename, 'w+');
61      $result = fetchRemote($_POST['file_url'], $file);
62      fclose($file);
63     
64      // download failed ?
65      if (!$result)
66      {
67        @unlink($temp_filename);
68        $page['errors'][] = l10n('Unable to download file');
69      }
70      // check mime-type
71      else if (!in_array(get_mime($temp_filename, $allowed_mimes[0]), $allowed_mimes))
72      {
73        @unlink($temp_filename);
74        $page['errors'][] = l10n('Invalid file type');
75      }
76      // continue...
77      else
78      {
79        $image_id = add_uploaded_file(
80          $temp_filename, 
81          basename($temp_filename), 
82          array($category_id), 
83          $_POST['level']
84          );
85       
86        if (!empty($_POST['photo_name']))
87        {
88          single_update(
89            IMAGES_TABLE,
90            array('name'=>$_POST['photo_name']),
91            array('id' => $image_id)
92            );
93        }
94       
95        $image_ids = array($image_id);
96      }
97    }
98  }
99  // MULTIPLE UPLOAD
100  else if ($_GET['upload_mode'] == 'multiple')
101  {
102    if (isset($_POST['onUploadError']) and is_array($_POST['onUploadError']) and count($_POST['onUploadError']) > 0)
103    {
104      array_push($page['errors'], sprintf(l10n('%d photos not imported'), count($_POST['onUploadError'])));
105      foreach ($_POST['onUploadError'] as $error)
106      {
107        array_push($page['errors'], $error);
108      }
109    }
110 
111    if (isset($_POST['imageIds']) and is_array($_POST['imageIds']) and count($_POST['imageIds']) > 0)
112    {
113      $image_ids = $_POST['imageIds'];
114    }
115  }
116
117  // DISPLAY RESULTS
118  foreach ($image_ids as $image_id)
119  {
120    $query = '
121SELECT id, file, path
122  FROM '.IMAGES_TABLE.'
123  WHERE id = '.$image_id.'
124;';
125    $image_infos = pwg_db_fetch_assoc(pwg_query($query));
126   
127    $thumbnail = array(
128      'file' =>  $image_infos['file'],
129      'src' =>   DerivativeImage::thumb_url($image_infos),
130      'title' => get_name_from_file($image_infos['file']),
131      'link' =>  get_root_url().'admin.php?page=photo-'.$image_id.'&amp;cat_id='.$category_id,
132      );
133
134    array_push($page['thumbnails'], $thumbnail);
135  }
136
137  if (!empty($page['thumbnails']))
138  {
139    // nb uploaded
140    array_push($page['infos'], sprintf(
141      l10n('%d photos uploaded'),
142      count($page['thumbnails'])
143      ));
144
145    // level
146    if (0 != $_POST['level'])
147    {
148      array_push($page['infos'], sprintf(
149        l10n('Privacy level set to "%s"'),
150        l10n(sprintf('Level %d', $_POST['level']))
151        ));
152    }
153
154    // new category count
155    $query = '
156SELECT COUNT(*)
157  FROM '.IMAGE_CATEGORY_TABLE.'
158  WHERE category_id = '.$category_id.'
159;';
160    list($count) = pwg_db_fetch_row(pwg_query($query));
161    $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=album-');
162   
163    array_push($page['infos'], sprintf(
164      l10n('Album "%s" now contains %d photos'),
165      '<em>'.$category_name.'</em>',
166      $count
167      ));
168   
169    $page['batch_link'] = PHOTOS_ADD_BASE_URL.'&batch='.implode(',', $image_ids);
170  }
171}
172
173
174// +-----------------------------------------------------------------------+
175// |                             prepare form                              |
176// +-----------------------------------------------------------------------+
177include(PHPWG_ROOT_PATH.'admin/include/photos_add_direct_prepare.inc.php');
178
179// upload mode
180$upload_modes = array('single', 'multiple');
181$upload_mode = isset($conf['url_uploader_mode']) ? $conf['url_uploader_mode'] : 'single';
182
183if ( isset($_GET['upload_mode']) and $_GET['upload_mode']!=$upload_mode and in_array($_GET['upload_mode'], $upload_modes) )
184{
185  $upload_mode = $_GET['upload_mode'];
186  conf_update_param('url_uploader_mode', $upload_mode);
187}
188
189// what is the upload switch mode
190$index_of_upload_mode = array_flip($upload_modes);
191$upload_mode_index = $index_of_upload_mode[$upload_mode];
192$upload_switch = $upload_modes[ ($upload_mode_index + 1) % 2 ];
193
194$template->assign(array(
195  'upload_mode' => $upload_mode,
196  'form_action' => URLUPLOADER_ADMIN.'&amp;upload_mode='.$upload_mode.'&amp;processed=1',
197  'switch_url' => URLUPLOADER_ADMIN.'&amp;upload_mode='.$upload_switch,
198  'another_upload_link' => URLUPLOADER_ADMIN.'&amp;upload_mode='.$upload_mode,
199  ));
200
201
202$template->set_filename('urluploader_content', realpath(URLUPLOADER_PATH . 'template/photos_add.tpl'));
203
204// template vars
205$template->assign(array(
206  'URLUPLOADER_PATH' => get_root_url() . URLUPLOADER_PATH,
207  'URLUPLOADER_ADMIN' => URLUPLOADER_ADMIN,
208  ));
209 
210// send page content
211$template->assign_var_from_handle('ADMIN_CONTENT', 'urluploader_content');
212
213?>
Note: See TracBrowser for help on using the repository browser.