source: trunk/admin/include/photos_add_direct_process.inc.php @ 26461

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

Update headers to 2014. Happy new year!!

File size: 7.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24
25if (isset($_GET['processed']))
26{
27//   echo '<pre>POST'."\n"; print_r($_POST); echo '</pre>';
28//   echo '<pre>FILES'."\n"; print_r($_FILES); echo '</pre>';
29//   echo '<pre>SESSION'."\n"; print_r($_SESSION); echo '</pre>';
30//   exit();
31
32  // sometimes, you have submitted the form but you have nothing in $_POST
33  // and $_FILES. This may happen when you have an HTML upload and you
34  // exceeded the post_max_size (but not the upload_max_size)
35  if (!isset($_POST['submit_upload']))
36  {
37    $page['errors'][] = l10n(
38      'The uploaded files exceed the post_max_size directive in php.ini: %sB',
39      ini_get('post_max_size')
40      );
41  }
42  else
43  {
44    $category_id = $_POST['category'];
45  }
46
47  if (isset($_POST['onUploadError']) and is_array($_POST['onUploadError']) and count($_POST['onUploadError']) > 0)
48  {
49    foreach ($_POST['onUploadError'] as $error)
50    {
51      $page['errors'][] = $error;
52    }
53  }
54   
55  $image_ids = array();
56       
57  if (isset($_FILES) and !empty($_FILES['image_upload']))
58  {
59    $starttime = get_moment();
60
61  foreach ($_FILES['image_upload']['error'] as $idx => $error)
62  {
63    if (UPLOAD_ERR_OK == $error)
64    {
65      $images_to_add = array();
66     
67      $extension = pathinfo($_FILES['image_upload']['name'][$idx], PATHINFO_EXTENSION);
68      if ('zip' == strtolower($extension))
69      {
70        $upload_dir = $conf['upload_dir'].'/buffer';
71        prepare_directory($upload_dir);
72       
73        $temporary_archive_name = date('YmdHis').'-'.generate_key(10);
74        $archive_path = $upload_dir.'/'.$temporary_archive_name.'.zip';
75       
76        move_uploaded_file(
77          $_FILES['image_upload']['tmp_name'][$idx],
78          $archive_path
79          );
80
81        define('PCLZIP_TEMPORARY_DIR', $upload_dir.'/');
82        include_once(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
83        $zip = new PclZip($archive_path);
84        if ($list = $zip->listContent())
85        {
86          $indexes_to_extract = array();
87         
88          foreach ($list as $node)
89          {
90            if (1 == $node['folder'])
91            {
92              continue;
93            }
94
95            if (is_valid_image_extension(pathinfo($node['filename'], PATHINFO_EXTENSION)))
96            {
97              $indexes_to_extract[] = $node['index'];
98             
99              $images_to_add[] = array(
100                'source_filepath' => $upload_dir.'/'.$temporary_archive_name.'/'.$node['filename'],
101                'original_filename' => basename($node['filename']),
102                );
103            }
104          }
105     
106          if (count($indexes_to_extract) > 0)
107          {
108            $zip->extract(
109              PCLZIP_OPT_PATH, $upload_dir.'/'.$temporary_archive_name,
110              PCLZIP_OPT_BY_INDEX, $indexes_to_extract,
111              PCLZIP_OPT_ADD_TEMP_FILE_ON
112              );
113          }
114        }
115      }
116      elseif (is_valid_image_extension($extension))
117      {
118        $images_to_add[] = array(
119          'source_filepath' => $_FILES['image_upload']['tmp_name'][$idx],
120          'original_filename' => $_FILES['image_upload']['name'][$idx],
121          );
122      }
123
124      foreach ($images_to_add as $image_to_add)
125      {
126        $image_id = add_uploaded_file(
127          $image_to_add['source_filepath'],
128          $image_to_add['original_filename'],
129          array($category_id),
130          $_POST['level']
131          );
132
133        $image_ids[] = $image_id;
134
135        // TODO: if $image_id is not an integer, something went wrong
136      }
137    }
138    else
139    {
140      $error_message = file_upload_error_message($error);
141     
142      $page['errors'][] = l10n(
143        'Error on file "%s" : %s',
144        $_FILES['image_upload']['name'][$idx],
145        $error_message
146        );
147    }
148  }
149 
150  $endtime = get_moment();
151  $elapsed = ($endtime - $starttime) * 1000;
152  // printf('%.2f ms', $elapsed);
153
154  } // if (!empty($_FILES))
155
156  if (isset($_POST['upload_id']))
157  {
158    // we're on a multiple upload, with uploadify and so on
159    if (isset($_SESSION['uploads_error'][ $_POST['upload_id'] ]))
160    {
161      foreach ($_SESSION['uploads_error'][ $_POST['upload_id'] ] as $error)
162      {
163        $page['errors'][] = $error;
164      }
165    }
166
167    if (isset($_SESSION['uploads'][ $_POST['upload_id'] ]))
168    {
169      $image_ids = $_SESSION['uploads'][ $_POST['upload_id'] ];
170    }
171  }
172 
173  $page['thumbnails'] = array();
174  foreach ($image_ids as $image_id)
175  {
176    // we could return the list of properties from the add_uploaded_file
177    // function, but I like the "double check". And it costs nothing
178    // compared to the upload process.
179    $thumbnail = array();
180     
181    $query = '
182SELECT
183    id,
184    file,
185    path
186  FROM '.IMAGES_TABLE.'
187  WHERE id = '.$image_id.'
188;';
189    $image_infos = pwg_db_fetch_assoc(pwg_query($query));
190
191    $thumbnail['file'] = $image_infos['file'];
192   
193    $thumbnail['src'] = DerivativeImage::thumb_url($image_infos);
194
195    // TODO: when implementing this plugin in Piwigo core, we should have
196    // a function get_image_name($name, $file) (if name is null, then
197    // compute a temporary name from filename) that would be also used in
198    // picture.php. UPDATE: in fact, "get_name_from_file($file)" already
199    // exists and is used twice (batch_manager_unit + comments, but not in
200    // picture.php I don't know why) with the same pattern if
201    // (empty($name)) {$name = get_name_from_file($file)}, a clean
202    // function get_image_name($name, $file) would be better
203    $thumbnail['title'] = get_name_from_file($image_infos['file']);
204
205    $thumbnail['link'] = get_root_url().'admin.php?page=photo-'.$image_id.'&amp;cat_id='.$category_id;
206
207    $page['thumbnails'][] = $thumbnail;
208  }
209 
210  if (!empty($page['thumbnails']))
211  {
212    $page['infos'][] = l10n('%d photos uploaded', count($page['thumbnails']));
213   
214    if (0 != $_POST['level'])
215    {
216      $page['infos'][] = l10n(
217        'Privacy level set to "%s"',
218        l10n(sprintf('Level %d', $_POST['level']))
219        );
220    }
221
222    $query = '
223SELECT
224    COUNT(*)
225  FROM '.IMAGE_CATEGORY_TABLE.'
226  WHERE category_id = '.$category_id.'
227;';
228    list($count) = pwg_db_fetch_row(pwg_query($query));
229    $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=album-');
230   
231    // information
232    $page['infos'][] = l10n(
233      'Album "%s" now contains %d photos',
234      '<em>'.$category_name.'</em>',
235      $count
236      );
237   
238    $page['batch_link'] = PHOTOS_ADD_BASE_URL.'&batch='.implode(',', $image_ids);
239  }
240}
241
242?>
Note: See TracBrowser for help on using the repository browser.