source: trunk/upload.php @ 61

Last change on this file since 61 was 61, checked in by z0rglub, 21 years ago

Multi categories for the same picture

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1<?php
2/***************************************************************************
3 *                                 upload.php                              *
4 *                            -------------------                          *
5 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: upload.php 61 2003-08-30 15:54:37Z z0rglub $
9 *                                                                         *
10 ***************************************************************************/
11
12/***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation;                                         *
17 *                                                                         *
18 ***************************************************************************/
19
20//------------------------------------------------------------------- functions
21// The validate_upload function checks if the image of the given path is valid.
22// A picture is valid when :
23//     - width, height and filesize are not higher than the maximum
24//       filesize authorized by the administrator
25//     - the type of the picture is among jpg, gif and png
26// The function returns an array containing :
27//     - $result['type'] contains the type of the image ('jpg', 'gif' or 'png')
28//     - $result['error'] contains an array with the different errors
29//       found with the picture
30function validate_upload( $temp_name, $my_max_file_size,
31                          $image_max_width, $image_max_height )
32{
33  global $lang;
34               
35  $result = array();
36  $result['error'] = array();
37  //echo $_FILES['picture']['name']."<br />".$temp_name;
38  $extension = get_extension( $_FILES['picture']['name'] );
39  if ( $extension != 'gif' and $extension != 'jpg' and $extension != 'png' )
40  {
41    array_push( $result['error'], $lang['upload_advise_filetype'] );
42    return $result;
43  }
44  if ( !isset( $_FILES['picture'] ) )
45  {
46    // do we even have a file?
47    array_push( $result['error'], "You did not upload anything!" );
48  }
49  else if ( $_FILES['picture']['size'] > $my_max_file_size * 1024 )
50  {
51    array_push( $result['error'],
52                $lang['upload_advise_width'].$my_max_file_size.' KB' );
53  }
54  else
55  {
56    // check if we are allowed to upload this file_type
57    // upload de la photo sous un nom temporaire
58    if ( !move_uploaded_file( $_FILES['picture']['tmp_name'], $temp_name ) )
59    {
60      array_push( $result['error'], $lang['upload_cannot_upload'] );
61    }
62    else
63    {
64      $size = getimagesize( $temp_name );
65      if ( isset( $image_max_width )
66           and $image_max_width != ""
67           and $size[0] > $image_max_width )
68      {
69        array_push( $result['error'],
70                    $lang['upload_advise_width'].$image_max_width.' px' );
71      }
72      if ( isset( $image_max_height )
73           and $image_max_height != ""
74           and $size[1] > $image_max_height )
75      {
76        array_push( $result['error'],
77                    $lang['upload_advise_height'].$image_max_height.' px' );
78      }
79      // $size[2] == 1 means GIF
80      // $size[2] == 2 means JPG
81      // $size[2] == 3 means PNG
82      switch ( $size[2] )
83      {
84      case 1 : $result['type'] = 'gif'; break;
85      case 2 : $result['type'] = 'jpg'; break;
86      case 3 : $result['type'] = 'png'; break;
87      default :
88        array_push( $result['error'], $lang['upload_advise_filetype'] ); 
89      }
90    }
91  }
92  if ( sizeof( $result['error'] ) > 0 )
93  {
94    // destruction de l'image avec le nom temporaire
95    @unlink( $temp_name );
96  }
97  return $result;
98}       
99//----------------------------------------------------------- personnal include
100include_once( './include/init.inc.php' );
101//-------------------------------------------------- access authorization check
102check_login_authorization();
103check_cat_id( $_GET['cat'] );
104if ( isset( $page['cat'] ) and is_numeric( $page['cat'] ) )
105{
106  check_restrictions( $page['cat'] );
107  $result = get_cat_info( $page['cat'] );
108  $page['cat_dir']        = $result['dir'];
109  $page['cat_site_id']    = $result['site_id'];
110  $page['cat_name']       = $result['name'];
111  $page['cat_uploadable'] = $result['uploadable'];
112}
113else
114{
115  $access_forbidden = true;
116}
117if ( $access_forbidden == true
118     or $page['cat_site_id'] != 1
119     or !$conf['upload_available']
120     or !$page['cat_uploadable'] )
121{
122  echo '<div style="text-align:center;">'.$lang['upload_forbidden'].'<br />';
123  echo '<a href="'.add_session_id( './category.php' ).'">';
124  echo $lang['thumbnails'].'</a></div>';
125  exit();
126}
127//----------------------------------------------------- template initialization
128$vtp = new VTemplate;
129$handle = $vtp->Open( './template/'.$user['template'].'/upload.vtp' );
130initialize_template();
131
132$tpl = array( 'upload_title', 'upload_username', 'mail_address', 'submit',
133              'upload_successful', 'search_return_main_page','upload_author',
134              'upload_name','upload_creation_date','upload_comment',
135              'mandatory' );
136templatize_array( $tpl, 'lang', $handle );
137
138$error = array();
139$page['upload_successful'] = false;
140if ( isset( $_GET['waiting_id'] ) )
141{
142  $page['waiting_id'] = $_GET['waiting_id'];
143}
144//-------------------------------------------------------------- picture upload
145// verfying fields
146if ( isset( $_POST['submit'] ) and !isset( $_GET['waiting_id'] ) )
147{
148  $path = $page['cat_dir'].$_FILES['picture']['name'];
149  if ( @is_file( $path ) )
150  {
151    array_push( $error, $lang['upload_file_exists'] );
152  }
153  // test de la présence des champs obligatoires
154  if ( $_FILES['picture']['name'] == '' )
155  {
156    array_push( $error, $lang['upload_filenotfound'] );
157  }
158  if ( !ereg( "([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)",
159             $_POST['mail_address'] ) )
160  {
161    array_push( $error, $lang['reg_err_mail_address'] );
162  }
163  if ( $_POST['username'] == '' )
164  {
165    array_push( $error, $lang['upload_err_username'] );
166  }
167
168  if ( $_POST['date_creation'] != '' )
169  {
170    list( $day,$month,$year ) = explode( '/', $_POST['date_creation'] );
171    // int checkdate ( int month, int day, int year)
172    if ( checkdate( $month, $day, $year ) )
173    {
174      // int mktime ( int hour, int minute, int second,
175      //              int month, int day, int year [, int is_dst])
176      $date_creation = mktime( 0, 0, 0, $month, $day, $year );
177    }
178    else
179    {
180      array_push( $error, $lang['err_date'] );
181    }
182  }
183  // creation of the "infos" field :
184  // <infos author="Pierrick LE GALL" comment="my comment"
185  //        date_creation="1056891767" name="" />
186  $xml_infos = '<infos';
187  $xml_infos.= ' author="'.htmlspecialchars($_POST['author'],ENT_QUOTES).'"';
188  $xml_infos.= ' comment="'.htmlspecialchars($_POST['comment'],ENT_QUOTES).'"';
189  $xml_infos.= ' date_creation="'.$date_creation.'"';
190  $xml_infos.= ' name="'.htmlspecialchars( $_POST['name'], ENT_QUOTES).'"';
191  $xml_infos.= ' />';
192 
193  if ( sizeof( $error ) == 0 )
194  {
195    $result = validate_upload( $path, $conf['upload_maxfilesize'],
196                               $conf['upload_maxwidth'],
197                               $conf['upload_maxheight']  );
198    $upload_type = $result['type'];
199    for ( $j = 0; $j < sizeof( $result['error'] ); $j++ )
200    {
201      array_push( $error, $result['error'][$j] );
202    }
203  }
204
205  if ( sizeof( $error ) == 0 )
206  {
207    $query = 'insert into '.PREFIX_TABLE.'waiting';
208    $query.= ' (storage_category_id,file,username,mail_address,date,infos)';
209    $query.= ' values ';
210    $query.= '('.$page['cat'].",'".$_FILES['picture']['name']."'";
211    $query.= ",'".htmlspecialchars( $_POST['username'], ENT_QUOTES)."'";
212    $query.= ",'".$_POST['mail_address']."',".time().",'".$xml_infos."')";
213    $query.= ';';
214    mysql_query( $query );
215    $page['waiting_id'] = mysql_insert_id();
216  }
217}
218//------------------------------------------------------------ thumbnail upload
219if ( isset( $_POST['submit'] ) and isset( $_GET['waiting_id'] ) )
220{
221  // upload of the thumbnail
222  $query = 'select file';
223  $query.= ' from '.PREFIX_TABLE.'waiting';
224  $query.= ' where id = '.$_GET['waiting_id'];
225  $query.= ';';
226  $result= mysql_query( $query );
227  $row = mysql_fetch_array( $result );
228  $file = substr ( $row['file'], 0, strrpos ( $row['file'], ".") );
229  $extension = get_extension( $_FILES['picture']['name'] );
230  $path = $page['cat_dir'].'thumbnail/';
231  $path.= $conf['prefix_thumbnail'].$file.'.'.$extension;
232  $result = validate_upload( $path, $conf['upload_maxfilesize'],
233                             $conf['upload_maxwidth_thumbnail'],
234                             $conf['upload_maxheight_thumbnail']  );
235  $upload_type = $result['type'];
236  for ( $j = 0; $j < sizeof( $result['error'] ); $j++ )
237  {
238    array_push( $error, $result['error'][$j] );
239  }
240  if ( sizeof( $error ) == 0 )
241  {
242    $query = 'update '.PREFIX_TABLE.'waiting';
243    $query.= " set tn_ext = '".$extension."'";
244    $query.= ' where id = '.$_GET['waiting_id'];
245    $query.= ';';
246    mysql_query( $query );
247    $page['upload_successful'] = true;
248  }
249}
250
251if ( !$page['upload_successful'] )
252{
253  $vtp->addSession( $handle, 'upload_not_successful' );
254//-------------------------------------------------------------- errors display
255  if ( sizeof( $error ) != 0 )
256  {
257    $vtp->addSession( $handle, 'errors' );
258    for ( $i = 0; $i < sizeof( $error ); $i++ )
259    {
260      $vtp->addSession( $handle, 'li' );
261      $vtp->setVar( $handle, 'li.li', $error[$i] );
262      $vtp->closeSession( $handle, 'li' );
263    }
264    $vtp->closeSession( $handle, 'errors' );
265  }
266//----------------------------------------------------------------- form action
267  $url = './upload.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
268  if ( isset( $page['waiting_id'] ) )
269  {
270    $url.= '&amp;waiting_id='.$page['waiting_id'];
271  }
272  $vtp->setGlobalVar( $handle, 'form_action', add_session_id( $url ) );
273//--------------------------------------------------------------------- advises
274  if ( $conf['upload_maxfilesize'] != '' )
275  {
276    $vtp->addSession( $handle, 'advise' );
277    $content = $lang['upload_advise_filesize'];
278    $content.= $conf['upload_maxfilesize'].' KB';
279    $vtp->setVar( $handle, 'advise.content', $content );
280    $vtp->closeSession( $handle, 'advise' );
281  }
282  if ( isset( $page['waiting_id'] ) )
283  {
284    $advise_title=$lang['upload_advise_thumbnail'].$_FILES['picture']['name'];
285    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
286
287    if ( $conf['upload_maxwidth_thumbnail'] != '' )
288    {
289      $vtp->addSession( $handle, 'advise' );
290      $content = $lang['upload_advise_width'];
291      $content.= $conf['upload_maxwidth_thumbnail'].' px';
292      $vtp->setVar( $handle, 'advise.content', $content );
293      $vtp->closeSession( $handle, 'advise' );
294    }
295    if ( $conf['upload_maxheight_thumbnail'] != '' )
296    {
297      $vtp->addSession( $handle, 'advise' );
298      $content = $lang['upload_advise_height'];
299      $content.= $conf['upload_maxheight_thumbnail'].' px';
300      $vtp->setVar( $handle, 'advise.content', $content );
301      $vtp->closeSession( $handle, 'advise' );
302    }
303  }
304  else
305  {
306    $advise_title = $lang['upload_advise'];
307    $advise_title.= get_cat_display_name( $page['cat_name'], ' - ',
308                                          'font-style:italic;' );
309    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
310
311    if ( $conf['upload_maxwidth'] != '' )
312    {
313      $vtp->addSession( $handle, 'advise' );
314      $content = $lang['upload_advise_width'];
315      $content.= $conf['upload_maxwidth'].' px';
316      $vtp->setVar( $handle, 'advise.content', $content );
317      $vtp->closeSession( $handle, 'advise' );
318    }
319    if ( $conf['upload_maxheight'] != '' )
320    {
321      $vtp->addSession( $handle, 'advise' );
322      $content = $lang['upload_advise_height'];
323      $content.= $conf['upload_maxheight'].' px';
324      $vtp->setVar( $handle, 'advise.content', $content );
325      $vtp->closeSession( $handle, 'advise' );
326    }
327  }
328  $vtp->addSession( $handle, 'advise' );
329  $content = $lang['upload_advise_filetype'];
330  $vtp->setVar( $handle, 'advise.content', $content );
331  $vtp->closeSession( $handle, 'advise' );
332//----------------------------------------- optionnal username and mail address
333  if ( !isset( $page['waiting_id'] ) )
334  {
335    $vtp->addSession( $handle, 'fields' );
336    // username
337    if ( isset( $_POST['username'] ) ) $username = $_POST['username'];
338    else                               $username = $user['username'];
339    $vtp->setVar( $handle, 'fields.username',  $username );
340    // mail address
341    if ( isset( $_POST['mail_address'] ) )$mail_address=$_POST['mail_address'];
342    else                                  $mail_address=$user['mail_address'];
343    $vtp->setGlobalVar( $handle, 'user_mail_address',$user['mail_address'] );
344    // name of the picture
345    $vtp->setVar( $handle, 'fields.name', $_POST['name'] );
346    // author
347    $vtp->setVar( $handle, 'fields.author', $_POST['author'] );
348    // date of creation
349    $vtp->setVar( $handle, 'fields.date_creation', $_POST['date_creation'] );
350    // comment
351    $vtp->setVar( $handle, 'fields.comment', $_POST['comment'] );
352
353    $vtp->closeSession( $handle, 'fields' );
354
355    $vtp->addSession( $handle, 'note' );
356    $vtp->closeSession( $handle, 'note' );
357  }
358  $vtp->closeSession( $handle, 'upload_not_successful' );
359}
360else
361{
362  $vtp->addSession( $handle, 'upload_successful' );
363  $vtp->closeSession( $handle, 'upload_successful' );
364}
365//----------------------------------------------------- return to main page url
366$url = './category.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
367$vtp->setGlobalVar( $handle, 'return_url', add_session_id( $url ) );
368//----------------------------------------------------------- html code display
369$code = $vtp->Display( $handle, 0 );
370echo $code;
371?>
Note: See TracBrowser for help on using the repository browser.