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

Last change on this file since 8734 was 8734, checked in by plg, 10 years ago

bug fixed: no more categories.uploadable column when creating a new category

split the admin/photos_add_direct.php script into
admin/include/photos_add_direct_prepare.php (prepare the upload form)
+ admin/include/photos_add_direct_process.inc.php (process the submitted form)
: it makes the upload form backend easier to reuse in the future Community
plugin.

File size: 9.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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    array_push(
38      $page['errors'],
39      sprintf(
40        l10n('The uploaded files exceed the post_max_size directive in php.ini: %sB'),
41        ini_get('post_max_size')
42        )
43      );
44  }
45 
46  $category_id = null;
47  if (!isset($_POST['category_type']))
48  {
49    // nothing to do, we certainly have the post_max_size issue
50  }
51  elseif ('existing' == $_POST['category_type'])
52  {
53    $category_id = $_POST['category'];
54  }
55  elseif ('new' == $_POST['category_type'])
56  {
57    $output_create = create_virtual_category(
58      $_POST['category_name'],
59      (0 == $_POST['category_parent'] ? null : $_POST['category_parent'])
60      );
61   
62    $category_id = $output_create['id'];
63
64    if (isset($output_create['error']))
65    {
66      array_push($page['errors'], $output_create['error']);
67    }
68    else
69    {
70      $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=cat_modify&amp;cat_id=');
71      // information
72      array_push(
73        $page['infos'],
74        sprintf(
75          l10n('Album "%s" has been added'),
76          '<em>'.$category_name.'</em>'
77          )
78        );
79      // TODO: add the onclick="window.open(this.href); return false;"
80      // attribute with jQuery on upload.tpl side for href containing
81      // "cat_modify"
82    }
83  }
84
85  $image_ids = array();
86       
87  if (isset($_FILES) and !empty($_FILES['image_upload']))
88  {
89    $starttime = get_moment();
90
91  foreach ($_FILES['image_upload']['error'] as $idx => $error)
92  {
93    if (UPLOAD_ERR_OK == $error)
94    {
95      $images_to_add = array();
96     
97      $extension = pathinfo($_FILES['image_upload']['name'][$idx], PATHINFO_EXTENSION);
98      if ('zip' == strtolower($extension))
99      {
100        $upload_dir = $conf['upload_dir'].'/buffer';
101        prepare_directory($upload_dir);
102       
103        $temporary_archive_name = date('YmdHis').'-'.generate_key(10);
104        $archive_path = $upload_dir.'/'.$temporary_archive_name.'.zip';
105       
106        move_uploaded_file(
107          $_FILES['image_upload']['tmp_name'][$idx],
108          $archive_path
109          );
110
111        define('PCLZIP_TEMPORARY_DIR', $upload_dir.'/');
112        include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
113        $zip = new PclZip($archive_path);
114        if ($list = $zip->listContent())
115        {
116          $indexes_to_extract = array();
117         
118          foreach ($list as $node)
119          {
120            if (1 == $node['folder'])
121            {
122              continue;
123            }
124
125            if (is_valid_image_extension(pathinfo($node['filename'], PATHINFO_EXTENSION)))
126            {
127              array_push($indexes_to_extract, $node['index']);
128             
129              array_push(
130                $images_to_add,
131                array(
132                  'source_filepath' => $upload_dir.'/'.$temporary_archive_name.'/'.$node['filename'],
133                  'original_filename' => basename($node['filename']),
134                  )
135                );
136            }
137          }
138     
139          if (count($indexes_to_extract) > 0)
140          {
141            $zip->extract(
142              PCLZIP_OPT_PATH, $upload_dir.'/'.$temporary_archive_name,
143              PCLZIP_OPT_BY_INDEX, $indexes_to_extract,
144              PCLZIP_OPT_ADD_TEMP_FILE_ON
145              );
146          }
147        }
148      }
149      elseif (is_valid_image_extension($extension))
150      {
151        array_push(
152          $images_to_add,
153          array(
154            'source_filepath' => $_FILES['image_upload']['tmp_name'][$idx],
155            'original_filename' => $_FILES['image_upload']['name'][$idx],
156            )
157          );
158      }
159
160      foreach ($images_to_add as $image_to_add)
161      {
162        $image_id = add_uploaded_file(
163          $image_to_add['source_filepath'],
164          $image_to_add['original_filename'],
165          array($category_id),
166          $_POST['level']
167          );
168
169        array_push($image_ids, $image_id);
170
171        // TODO: if $image_id is not an integer, something went wrong
172      }
173    }
174    else
175    {
176      $error_message = file_upload_error_message($error);
177     
178      array_push(
179        $page['errors'],
180        sprintf(
181          l10n('Error on file "%s" : %s'),
182          $_FILES['image_upload']['name'][$idx],
183          $error_message
184          )
185        );
186    }
187  }
188 
189  $endtime = get_moment();
190  $elapsed = ($endtime - $starttime) * 1000;
191  // printf('%.2f ms', $elapsed);
192
193  } // if (!empty($_FILES))
194
195  if (isset($_POST['upload_id']))
196  {
197    // we're on a multiple upload, with uploadify and so on
198    if (isset($_SESSION['uploads_error'][ $_POST['upload_id'] ]))
199    {
200      foreach ($_SESSION['uploads_error'][ $_POST['upload_id'] ] as $error)
201      {
202        array_push($page['errors'], $error);
203      }
204    }
205
206    if (isset($_SESSION['uploads'][ $_POST['upload_id'] ]))
207    {
208      $image_ids = $_SESSION['uploads'][ $_POST['upload_id'] ];
209
210      associate_images_to_categories(
211        $image_ids,
212        array($category_id)
213        );
214
215      $query = '
216UPDATE '.IMAGES_TABLE.'
217  SET level = '.$_POST['level'].'
218  WHERE id IN ('.implode(', ', $image_ids).')
219;';
220      pwg_query($query);
221   
222      invalidate_user_cache();
223    }
224  }
225 
226  $page['thumbnails'] = array();
227  foreach ($image_ids as $image_id)
228  {
229    // we could return the list of properties from the add_uploaded_file
230    // function, but I like the "double check". And it costs nothing
231    // compared to the upload process.
232    $thumbnail = array();
233     
234    $query = '
235SELECT
236    file,
237    path,
238    tn_ext
239  FROM '.IMAGES_TABLE.'
240  WHERE id = '.$image_id.'
241;';
242    $image_infos = pwg_db_fetch_assoc(pwg_query($query));
243
244    $thumbnail['file'] = $image_infos['file'];
245   
246    $thumbnail['src'] = get_thumbnail_location(
247      array(
248        'path' => $image_infos['path'],
249        'tn_ext' => $image_infos['tn_ext'],
250        )
251      );
252
253    // TODO: when implementing this plugin in Piwigo core, we should have
254    // a function get_image_name($name, $file) (if name is null, then
255    // compute a temporary name from filename) that would be also used in
256    // picture.php. UPDATE: in fact, "get_name_from_file($file)" already
257    // exists and is used twice (batch_manager_unit + comments, but not in
258    // picture.php I don't know why) with the same pattern if
259    // (empty($name)) {$name = get_name_from_file($file)}, a clean
260    // function get_image_name($name, $file) would be better
261    $thumbnail['title'] = get_name_from_file($image_infos['file']);
262
263    $thumbnail['link'] = PHPWG_ROOT_PATH.'admin.php?page=picture_modify'
264      .'&amp;image_id='.$image_id
265      .'&amp;cat_id='.$category_id
266      ;
267
268    array_push($page['thumbnails'], $thumbnail);
269  }
270 
271  if (!empty($page['thumbnails']))
272  {
273    array_push(
274      $page['infos'],
275      sprintf(
276        l10n('%d photos uploaded'),
277        count($page['thumbnails'])
278        )
279      );
280   
281    if (0 != $_POST['level'])
282    {
283      array_push(
284        $page['infos'],
285        sprintf(
286          l10n('Privacy level set to "%s"'),
287          l10n(
288            sprintf('Level %d', $_POST['level'])
289            )
290          )
291        );
292    }
293
294    if ('existing' == $_POST['category_type'])
295    {
296      $query = '
297SELECT
298    COUNT(*)
299  FROM '.IMAGE_CATEGORY_TABLE.'
300  WHERE category_id = '.$category_id.'
301;';
302      list($count) = pwg_db_fetch_row(pwg_query($query));
303      $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=cat_modify&amp;cat_id=');
304     
305      // information
306      array_push(
307        $page['infos'],
308        sprintf(
309          l10n('Album "%s" now contains %d photos'),
310          '<em>'.$category_name.'</em>',
311          $count
312          )
313        );
314    }
315
316    $page['batch_link'] = PHOTOS_ADD_BASE_URL.'&batch='.implode(',', $image_ids);
317  }
318}
319
320?>
Note: See TracBrowser for help on using the repository browser.