source: trunk/upload.php @ 57

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

improve the header of each file

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 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 57 2003-08-24 07:40:56Z 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.= ' (cat_id,file,username,mail_address,date,infos) values';
209    $query.= " (".$page['cat'].",'".$_FILES['picture']['name']."'";
210    $query.= ",'".htmlspecialchars( $_POST['username'], ENT_QUOTES)."'";
211    $query.= ",'".$_POST['mail_address']."',".time().",'".$xml_infos."')";
212    $query.= ';';
213    mysql_query( $query );
214    $page['waiting_id'] = mysql_insert_id();
215  }
216}
217//------------------------------------------------------------ thumbnail upload
218if ( isset( $_POST['submit'] ) and isset( $_GET['waiting_id'] ) )
219{
220  // upload of the thumbnail
221  $query = 'select file';
222  $query.= ' from '.PREFIX_TABLE.'waiting';
223  $query.= ' where id = '.$_GET['waiting_id'];
224  $query.= ';';
225  $result= mysql_query( $query );
226  $row = mysql_fetch_array( $result );
227  $file = substr ( $row['file'], 0, strrpos ( $row['file'], ".") );
228  $extension = get_extension( $_FILES['picture']['name'] );
229  $path = $page['cat_dir'].'thumbnail/';
230  $path.= $conf['prefix_thumbnail'].$file.'.'.$extension;
231  $result = validate_upload( $path, $conf['upload_maxfilesize'],
232                             $conf['upload_maxwidth_thumbnail'],
233                             $conf['upload_maxheight_thumbnail']  );
234  $upload_type = $result['type'];
235  for ( $j = 0; $j < sizeof( $result['error'] ); $j++ )
236  {
237    array_push( $error, $result['error'][$j] );
238  }
239  if ( sizeof( $error ) == 0 )
240  {
241    $query = 'update '.PREFIX_TABLE.'waiting';
242    $query.= " set tn_ext = '".$extension."'";
243    $query.= ' where id = '.$_GET['waiting_id'];
244    $query.= ';';
245    mysql_query( $query );
246    $page['upload_successful'] = true;
247  }
248}
249
250if ( !$page['upload_successful'] )
251{
252  $vtp->addSession( $handle, 'upload_not_successful' );
253//-------------------------------------------------------------- errors display
254  if ( sizeof( $error ) != 0 )
255  {
256    $vtp->addSession( $handle, 'errors' );
257    for ( $i = 0; $i < sizeof( $error ); $i++ )
258    {
259      $vtp->addSession( $handle, 'li' );
260      $vtp->setVar( $handle, 'li.li', $error[$i] );
261      $vtp->closeSession( $handle, 'li' );
262    }
263    $vtp->closeSession( $handle, 'errors' );
264  }
265//----------------------------------------------------------------- form action
266  $url = './upload.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
267  if ( isset( $page['waiting_id'] ) )
268  {
269    $url.= '&amp;waiting_id='.$page['waiting_id'];
270  }
271  $vtp->setGlobalVar( $handle, 'form_action', add_session_id( $url ) );
272//--------------------------------------------------------------------- advises
273  if ( $conf['upload_maxfilesize'] != '' )
274  {
275    $vtp->addSession( $handle, 'advise' );
276    $content = $lang['upload_advise_filesize'];
277    $content.= $conf['upload_maxfilesize'].' KB';
278    $vtp->setVar( $handle, 'advise.content', $content );
279    $vtp->closeSession( $handle, 'advise' );
280  }
281  if ( isset( $page['waiting_id'] ) )
282  {
283    $advise_title=$lang['upload_advise_thumbnail'].$_FILES['picture']['name'];
284    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
285
286    if ( $conf['upload_maxwidth_thumbnail'] != '' )
287    {
288      $vtp->addSession( $handle, 'advise' );
289      $content = $lang['upload_advise_width'];
290      $content.= $conf['upload_maxwidth_thumbnail'].' px';
291      $vtp->setVar( $handle, 'advise.content', $content );
292      $vtp->closeSession( $handle, 'advise' );
293    }
294    if ( $conf['upload_maxheight_thumbnail'] != '' )
295    {
296      $vtp->addSession( $handle, 'advise' );
297      $content = $lang['upload_advise_height'];
298      $content.= $conf['upload_maxheight_thumbnail'].' px';
299      $vtp->setVar( $handle, 'advise.content', $content );
300      $vtp->closeSession( $handle, 'advise' );
301    }
302  }
303  else
304  {
305    $advise_title = $lang['upload_advise'];
306    $advise_title.= get_cat_display_name( $page['cat_name'], ' - ',
307                                          'font-style:italic;' );
308    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
309
310    if ( $conf['upload_maxwidth'] != '' )
311    {
312      $vtp->addSession( $handle, 'advise' );
313      $content = $lang['upload_advise_width'];
314      $content.= $conf['upload_maxwidth'].' px';
315      $vtp->setVar( $handle, 'advise.content', $content );
316      $vtp->closeSession( $handle, 'advise' );
317    }
318    if ( $conf['upload_maxheight'] != '' )
319    {
320      $vtp->addSession( $handle, 'advise' );
321      $content = $lang['upload_advise_height'];
322      $content.= $conf['upload_maxheight'].' px';
323      $vtp->setVar( $handle, 'advise.content', $content );
324      $vtp->closeSession( $handle, 'advise' );
325    }
326  }
327  $vtp->addSession( $handle, 'advise' );
328  $content = $lang['upload_advise_filetype'];
329  $vtp->setVar( $handle, 'advise.content', $content );
330  $vtp->closeSession( $handle, 'advise' );
331//----------------------------------------- optionnal username and mail address
332  if ( !isset( $page['waiting_id'] ) )
333  {
334    $vtp->addSession( $handle, 'fields' );
335    // username
336    if ( isset( $_POST['username'] ) ) $username = $_POST['username'];
337    else                               $username = $user['username'];
338    $vtp->setVar( $handle, 'fields.username',  $username );
339    // mail address
340    if ( isset( $_POST['mail_address'] ) )$mail_address=$_POST['mail_address'];
341    else                                  $mail_address=$user['mail_address'];
342    $vtp->setGlobalVar( $handle, 'user_mail_address',$user['mail_address'] );
343    // name of the picture
344    $vtp->setVar( $handle, 'fields.name', $_POST['name'] );
345    // author
346    $vtp->setVar( $handle, 'fields.author', $_POST['author'] );
347    // date of creation
348    $vtp->setVar( $handle, 'fields.date_creation', $_POST['date_creation'] );
349    // comment
350    $vtp->setVar( $handle, 'fields.comment', $_POST['comment'] );
351
352    $vtp->closeSession( $handle, 'fields' );
353
354    $vtp->addSession( $handle, 'note' );
355    $vtp->closeSession( $handle, 'note' );
356  }
357  $vtp->closeSession( $handle, 'upload_not_successful' );
358}
359else
360{
361  $vtp->addSession( $handle, 'upload_successful' );
362  $vtp->closeSession( $handle, 'upload_successful' );
363}
364//----------------------------------------------------- return to main page url
365$url = './category.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
366$vtp->setGlobalVar( $handle, 'return_url', add_session_id( $url ) );
367//----------------------------------------------------------- html code display
368$code = $vtp->Display( $handle, 0 );
369echo $code;
370?>
Note: See TracBrowser for help on using the repository browser.