source: trunk/upload.php @ 2

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

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 KB
Line 
1<?php
2/***************************************************************************
3 *             upload.php is a part of PhpWebGallery                       *
4 *                            -------------------                          *
5 *   last update          : Sunday, October 27, 2002                       *
6 *   email                : pierrick@z0rglub.com                           *
7 *                                                                         *
8 ***************************************************************************/
9
10/***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17function get_extension( $filename )
18{
19  return substr ( strrchr( $filename, "." ), 1, strlen ( $filename ) );
20}
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  $i = 0;
38  //echo $_FILES['picture']['name']."<br />".$temp_name;
39  $extension = get_extension( $_FILES['picture']['name'] );
40  if ( $extension != 'gif' && $extension != 'jpg' && $extension != 'png' )
41  {
42    $result['error'][$i++] = $lang['upload_advise_filetype'];
43    return $result;
44  }
45  if ( !isset( $_FILES['picture'] ) )
46  {
47    // do we even have a file?
48    $result['error'][$i++] = "You did not upload anything!";
49  }
50  else if ( $_FILES['picture']['size'] > $my_max_file_size * 1024 )
51  {
52    $result['error'][$i++] = $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      $result['error'][$i++] = $lang['upload_cannot_upload'];
61    }
62    else
63    {
64      $size = getimagesize( $temp_name );
65      if ( isset( $image_max_width )
66           && $image_max_width != ""
67           && $size[0] > $image_max_width )
68      {
69        $result['error'][$i++] = $lang['upload_advise_width'].$image_max_width." px";
70      }
71      if ( isset( $image_max_height )
72           && $image_max_height != ""
73           && $size[1] > $image_max_height )
74      {
75        $result['error'][$i++] = $lang['upload_advise_height'].$image_max_height." px";
76      }
77      // $size[2] == 1 means GIF
78      // $size[2] == 2 means JPG
79      // $size[2] == 3 means PNG
80      if ( $size[2] != 1 && $size[2] != 2 && $size[2] != 3 )
81      {
82        $result['error'][$i++] = $lang['upload_advise_filetype'];
83      }
84      else
85      {
86        switch ( $size[2] )
87        {
88        case 1 :
89        {
90          $result['type'] = 'gif';
91          break;
92        }
93        case 2 :
94        {
95          $result['type'] = 'jpg';
96          break;
97        }
98        case 3 :
99        {
100          $result['type'] = 'png';
101          break;
102        }
103        }
104      }
105    }
106  }
107  if ( sizeof( $result['error'] ) > 0 )
108  {
109    // destruction de l'image avec le nom temporaire
110    @unlink( $temp_name );
111  }
112  return $result;
113}       
114//----------------------------------------------------------- personnal include
115include_once( './include/init.inc.php' );
116//-------------------------------------------------- access authorization check
117check_login_authorization();
118check_cat_id( $_GET['cat'] );
119if ( isset( $page['cat'] ) && is_numeric( $page['cat'] ) )
120{
121  check_restrictions( $page['cat'] );
122  $result = get_cat_info( $page['cat'] );
123  $page['cat_dir'] = $result['dir'];
124  $page['cat_site_id'] = $result['site_id'];
125  $page['cat_name'] = $result['name'];
126}
127else
128{
129  $access_forbidden = true;
130}
131if ( $access_forbidden == true
132     || $page['cat_site_id'] != 1
133     || $conf['upload_available'] == 'false' )
134{
135  echo"<div style=\"text-align:center;\">".$lang['upload_forbidden']."<br />";
136  echo "<a href=\"".add_session_id_to_url( "./diapo.php" )."\">".$lang['thumbnails']."</a></div>";
137  exit();
138}
139//----------------------------------------------------- template initialization
140$vtp = new VTemplate;
141$handle = $vtp->Open( './template/default/upload.vtp' );
142// language
143$vtp->setGlobalVar( $handle, 'upload_page_title',$lang['upload_title'] );
144$vtp->setGlobalVar( $handle, 'upload_title',     $lang['upload_title'] );
145$vtp->setGlobalVar( $handle, 'upload_username',  $lang['upload_username'] );
146$vtp->setGlobalVar( $handle, 'reg_mail_address', $lang['reg_mail_address'] );
147$vtp->setGlobalVar( $handle, 'submit',           $lang['submit'] );
148$vtp->setGlobalVar( $handle, 'upload_successful',$lang['upload_successful'] );
149$vtp->setGlobalVar( $handle, 'search_return_main_page',
150                    $lang['search_return_main_page'] );
151// user
152$vtp->setGlobalVar( $handle, 'page_style',       $user['style'] );
153$vtp->setGlobalVar( $handle, 'user_login',       $user['pseudo'] );
154$vtp->setGlobalVar( $handle, 'user_mail_address',$user['mail_address'] );
155// structure
156$vtp->setGlobalVar( $handle, 'frame_start',      get_frame_start() );
157$vtp->setGlobalVar( $handle, 'frame_begin',      get_frame_begin() );
158$vtp->setGlobalVar( $handle, 'frame_end',        get_frame_end() );
159
160$error = array();
161$i = 0;
162$page['upload_successful'] = false;
163if ( isset( $_GET['waiting_id'] ) )
164{
165  $page['waiting_id'] = $_GET['waiting_id'];
166}
167//-------------------------------------------------------------- picture upload
168// vérification de la présence et de la validité des champs.
169if ( isset( $_POST['submit'] ) && !isset( $_GET['waiting_id'] ) )
170{
171  $path = $page['cat_dir'].$_FILES['picture']['name'];
172  if ( @is_file( $path ) )
173  {
174    $error[$i++] = $lang['upload_file_exists'];
175  }
176  // test de la présence des champs obligatoires
177  if ( $_FILES['picture']['name'] == "" )
178  {
179    $error[$i++] = $lang['upload_filenotfound'];
180  }
181  if ( !ereg( "([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)",
182             $_POST['mail_address'] ) )
183  {
184    $error[$i++] = $lang['reg_err_mail_address'];
185  }
186  if ( $_POST['username'] == '' )
187  {
188    $error[$i++] = $lang['upload_err_username'];
189  }
190
191  if ( sizeof( $error ) == 0 )
192  {
193    $result = validate_upload( $path, $conf['upload_maxfilesize'],
194                               $conf['upload_maxwidth'],
195                               $conf['upload_maxheight']  );
196    $upload_type = $result['type'];
197    for ( $j = 0; $j < sizeof( $result['error'] ); $j++ )
198    {
199      $error[$i++] = $result['error'][$j];
200    }
201  }
202
203  if ( sizeof( $error ) == 0 )
204  {
205    $query = 'insert into '.$prefixeTable.'waiting';
206    $query.= ' (cat_id,file,username,mail_address,date) values';
207    $query.= " (".$page['cat'].",'".$_FILES['picture']['name']."'";
208    $query.= ",'".htmlspecialchars( $_POST['username'], ENT_QUOTES)."'";
209    $query.= ",'".$_POST['mail_address']."',".time().")";
210    $query.= ';';
211    mysql_query( $query );
212    $page['waiting_id'] = mysql_insert_id();
213  }
214}
215//------------------------------------------------------------ thumbnail upload
216if ( isset( $_POST['submit'] ) && isset( $_GET['waiting_id'] ) )
217{
218  // upload of the thumbnail
219  $query = 'select file';
220  $query.= ' from '.$prefixeTable.'waiting';
221  $query.= ' where id = '.$_GET['waiting_id'];
222  $query.= ';';
223  $result= mysql_query( $query );
224  $row = mysql_fetch_array( $result );
225  $file = substr ( $row['file'], 0, strrpos ( $row['file'], ".") );
226  $extension = get_extension( $_FILES['picture']['name'] );
227  $path = $page['cat_dir'].'thumbnail/';
228  $path.= $conf['prefixe_thumbnail'].$file.'.'.$extension;
229  $result = validate_upload( $path, $conf['upload_maxfilesize'],
230                             $conf['upload_maxwidth_thumbnail'],
231                             $conf['upload_maxheight_thumbnail']  );
232  $upload_type = $result['type'];
233  for ( $j = 0; $j < sizeof( $result['error'] ); $j++ )
234  {
235    $error[$i++] = $result['error'][$j];
236  }
237  if ( sizeof( $error ) == 0 )
238  {
239    $query = 'update '.$prefixeTable.'waiting';
240    $query.= " set tn_ext = '".$extension."'";
241    $query.= ' where id = '.$_GET['waiting_id'];
242    $query.= ';';
243    mysql_query( $query );
244    $page['upload_successful'] = true;
245  }
246}
247
248if ( !$page['upload_successful'] )
249{
250  $vtp->addSession( $handle, 'upload_not_successful' );
251//-------------------------------------------------------------- errors display
252  if ( sizeof( $error ) != 0 )
253  {
254    $vtp->addSession( $handle, 'errors' );
255    for ( $i = 0; $i < sizeof( $error ); $i++ )
256    {
257      $vtp->addSession( $handle, 'li' );
258      $vtp->setVar( $handle, 'li.li', $error[$i] );
259      $vtp->closeSession( $handle, 'li' );
260    }
261    $vtp->closeSession( $handle, 'errors' );
262  }
263//----------------------------------------------------------------- form action
264  $url = './upload.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
265  if ( isset( $page['waiting_id'] ) )
266  {
267    $url.= '&amp;waiting_id='.$page['waiting_id'];
268  }
269  $vtp->setGlobalVar( $handle, 'form_action', $url );
270//--------------------------------------------------------------------- advises
271  if ( $conf['upload_maxfilesize'] != '' )
272  {
273    $vtp->addSession( $handle, 'advise' );
274    $content = $lang['upload_advise_filesize'];
275    $content.= $conf['upload_maxfilesize'].' KB';
276    $vtp->setVar( $handle, 'advise.content', $content );
277    $vtp->closeSession( $handle, 'advise' );
278  }
279  if ( isset( $page['waiting_id'] ) )
280  {
281    $advise_title=$lang['upload_advise_thumbnail'].$_FILES['picture']['name'];
282    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
283
284    if ( $conf['upload_maxwidth_thumbnail'] != '' )
285    {
286      $vtp->addSession( $handle, 'advise' );
287      $content = $lang['upload_advise_width'];
288      $content.= $conf['upload_maxwidth_thumbnail'].' px';
289      $vtp->setVar( $handle, 'advise.content', $content );
290      $vtp->closeSession( $handle, 'advise' );
291    }
292    if ( $conf['upload_maxheight_thumbnail'] != '' )
293    {
294      $vtp->addSession( $handle, 'advise' );
295      $content = $lang['upload_advise_height'];
296      $content.= $conf['upload_maxheight_thumbnail'].' px';
297      $vtp->setVar( $handle, 'advise.content', $content );
298      $vtp->closeSession( $handle, 'advise' );
299    }
300  }
301  else
302  {
303    $advise_title = $lang['upload_advise'];
304    $advise_title.= get_cat_display_name( $page['cat_name'], ' - ',
305                                          'font-style:italic;' );
306    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
307
308    if ( $conf['upload_maxwidth'] != '' )
309    {
310      $vtp->addSession( $handle, 'advise' );
311      $content = $lang['upload_advise_width'];
312      $content.= $conf['upload_maxwidth'].' px';
313      $vtp->setVar( $handle, 'advise.content', $content );
314      $vtp->closeSession( $handle, 'advise' );
315    }
316    if ( $conf['upload_maxheight'] != '' )
317    {
318      $vtp->addSession( $handle, 'advise' );
319      $content = $lang['upload_advise_height'];
320      $content.= $conf['upload_maxheight'].' px';
321      $vtp->setVar( $handle, 'advise.content', $content );
322      $vtp->closeSession( $handle, 'advise' );
323    }
324  }
325  $vtp->addSession( $handle, 'advise' );
326  $content = $lang['upload_advise_filetype'];
327  $vtp->setVar( $handle, 'advise.content', $content );
328  $vtp->closeSession( $handle, 'advise' );
329//----------------------------------------- optionnal username and mail address
330  if ( !isset( $page['waiting_id'] ) )
331  {
332    $vtp->addSession( $handle, 'fields' );
333    $vtp->closeSession( $handle, 'fields' );
334  }
335  $vtp->closeSession( $handle, 'upload_not_successful' );
336}
337else
338{
339  $vtp->addSession( $handle, 'upload_successful' );
340  $vtp->closeSession( $handle, 'upload_successful' );
341}
342//----------------------------------------------------- return to main page url
343$url = './category.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
344$vtp->setGlobalVar( $handle, 'return_url', add_session_id( $url ) );
345//----------------------------------------------------------- html code display
346$code = $vtp->Display( $handle, 0 );
347echo $code;
348?>
Note: See TracBrowser for help on using the repository browser.