source: trunk/upload.php @ 10

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

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 KB
Line 
1<?php
2/***************************************************************************
3 *                                 upload.php                              *
4 *                            -------------------                          *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <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 ***************************************************************************/
17
18//------------------------------------------------------------------- functions
19
20// The validate_upload function checks if the image of the given path is valid.
21// A picture is valid when :
22//     - width, height and filesize are not higher than the maximum
23//       filesize authorized by the administrator
24//     - the type of the picture is among jpg, gif and png
25// The function returns an array containing :
26//     - $result['type'] contains the type of the image ('jpg', 'gif' or 'png')
27//     - $result['error'] contains an array with the different errors
28//       found with the picture
29function validate_upload( $temp_name, $my_max_file_size,
30                          $image_max_width, $image_max_height )
31{
32  global $lang;
33               
34  $result = array();
35  $result['error'] = array();
36  $i = 0;
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    $result['error'][$i++] = $lang['upload_advise_filetype'];
42    return $result;
43  }
44  if ( !isset( $_FILES['picture'] ) )
45  {
46    // do we even have a file?
47    $result['error'][$i++] = "You did not upload anything!";
48  }
49  else if ( $_FILES['picture']['size'] > $my_max_file_size * 1024 )
50  {
51    $result['error'][$i++] =
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      $result['error'][$i++] = $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        $result['error'][$i++] =
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        $result['error'][$i++] =
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      if ( $size[2] != 1 and $size[2] != 2 and $size[2] != 3 )
83      {
84        $result['error'][$i++] = $lang['upload_advise_filetype'];
85      }
86      else
87      {
88        switch ( $size[2] )
89        {
90        case 1 :
91          $result['type'] = 'gif'; break;
92        case 2 :
93          $result['type'] = 'jpg'; break;
94        case 3 :
95          $result['type'] = 'png'; break;
96        }
97      }
98    }
99  }
100  if ( sizeof( $result['error'] ) > 0 )
101  {
102    // destruction de l'image avec le nom temporaire
103    @unlink( $temp_name );
104  }
105  return $result;
106}       
107//----------------------------------------------------------- personnal include
108include_once( './include/init.inc.php' );
109//-------------------------------------------------- access authorization check
110check_login_authorization();
111check_cat_id( $_GET['cat'] );
112if ( isset( $page['cat'] ) and is_numeric( $page['cat'] ) )
113{
114  check_restrictions( $page['cat'] );
115  $result = get_cat_info( $page['cat'] );
116  $page['cat_dir'] = $result['dir'];
117  $page['cat_site_id'] = $result['site_id'];
118  $page['cat_name'] = $result['name'];
119}
120else
121{
122  $access_forbidden = true;
123}
124if ( $access_forbidden == true
125     or $page['cat_site_id'] != 1
126     or $conf['upload_available'] == 'false' )
127{
128  echo '<div style="text-align:center;">'.$lang['upload_forbidden'].'<br />';
129  echo '<a href="'.add_session_id_to_url( './diapo.php' ).'">';
130  echo $lang['thumbnails'].'</a></div>';
131  exit();
132}
133//----------------------------------------------------- template initialization
134$vtp = new VTemplate;
135$handle = $vtp->Open( './template/'.$user['template'].'/upload.vtp' );
136initialize_template();
137
138$tpl = array( 'upload_title', 'upload_username', 'mail_address', 'submit',
139              'upload_successful', 'search_return_main_page' );
140templatize_array( $tpl, 'lang', $sub );
141// user
142$vtp->setGlobalVar( $handle, 'style',            $user['style'] );
143$vtp->setGlobalVar( $handle, 'user_login',       $user['username'] );
144$vtp->setGlobalVar( $handle, 'user_mail_address',$user['mail_address'] );
145
146$error = array();
147$i = 0;
148$page['upload_successful'] = false;
149if ( isset( $_GET['waiting_id'] ) )
150{
151  $page['waiting_id'] = $_GET['waiting_id'];
152}
153//-------------------------------------------------------------- picture upload
154// vérification de la présence et de la validité des champs.
155if ( isset( $_POST['submit'] ) and !isset( $_GET['waiting_id'] ) )
156{
157  $path = $page['cat_dir'].$_FILES['picture']['name'];
158  if ( @is_file( $path ) )
159  {
160    $error[$i++] = $lang['upload_file_exists'];
161  }
162  // test de la présence des champs obligatoires
163  if ( $_FILES['picture']['name'] == "" )
164  {
165    $error[$i++] = $lang['upload_filenotfound'];
166  }
167  if ( !ereg( "([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)",
168             $_POST['mail_address'] ) )
169  {
170    $error[$i++] = $lang['reg_err_mail_address'];
171  }
172  if ( $_POST['username'] == '' )
173  {
174    $error[$i++] = $lang['upload_err_username'];
175  }
176
177  if ( sizeof( $error ) == 0 )
178  {
179    $result = validate_upload( $path, $conf['upload_maxfilesize'],
180                               $conf['upload_maxwidth'],
181                               $conf['upload_maxheight']  );
182    $upload_type = $result['type'];
183    for ( $j = 0; $j < sizeof( $result['error'] ); $j++ )
184    {
185      $error[$i++] = $result['error'][$j];
186    }
187  }
188
189  if ( sizeof( $error ) == 0 )
190  {
191    $query = 'insert into '.PREFIX_TABLE.'waiting';
192    $query.= ' (cat_id,file,username,mail_address,date) values';
193    $query.= " (".$page['cat'].",'".$_FILES['picture']['name']."'";
194    $query.= ",'".htmlspecialchars( $_POST['username'], ENT_QUOTES)."'";
195    $query.= ",'".$_POST['mail_address']."',".time().")";
196    $query.= ';';
197    mysql_query( $query );
198    $page['waiting_id'] = mysql_insert_id();
199  }
200}
201//------------------------------------------------------------ thumbnail upload
202if ( isset( $_POST['submit'] ) and isset( $_GET['waiting_id'] ) )
203{
204  // upload of the thumbnail
205  $query = 'select file';
206  $query.= ' from '.PREFIX_TABLE.'waiting';
207  $query.= ' where id = '.$_GET['waiting_id'];
208  $query.= ';';
209  $result= mysql_query( $query );
210  $row = mysql_fetch_array( $result );
211  $file = substr ( $row['file'], 0, strrpos ( $row['file'], ".") );
212  $extension = get_extension( $_FILES['picture']['name'] );
213  $path = $page['cat_dir'].'thumbnail/';
214  $path.= $conf['prefixe_thumbnail'].$file.'.'.$extension;
215  $result = validate_upload( $path, $conf['upload_maxfilesize'],
216                             $conf['upload_maxwidth_thumbnail'],
217                             $conf['upload_maxheight_thumbnail']  );
218  $upload_type = $result['type'];
219  for ( $j = 0; $j < sizeof( $result['error'] ); $j++ )
220  {
221    $error[$i++] = $result['error'][$j];
222  }
223  if ( sizeof( $error ) == 0 )
224  {
225    $query = 'update '.PREFIX_TABLE.'waiting';
226    $query.= " set tn_ext = '".$extension."'";
227    $query.= ' where id = '.$_GET['waiting_id'];
228    $query.= ';';
229    mysql_query( $query );
230    $page['upload_successful'] = true;
231  }
232}
233
234if ( !$page['upload_successful'] )
235{
236  $vtp->addSession( $handle, 'upload_not_successful' );
237//-------------------------------------------------------------- errors display
238  if ( sizeof( $error ) != 0 )
239  {
240    $vtp->addSession( $handle, 'errors' );
241    for ( $i = 0; $i < sizeof( $error ); $i++ )
242    {
243      $vtp->addSession( $handle, 'li' );
244      $vtp->setVar( $handle, 'li.li', $error[$i] );
245      $vtp->closeSession( $handle, 'li' );
246    }
247    $vtp->closeSession( $handle, 'errors' );
248  }
249//----------------------------------------------------------------- form action
250  $url = './upload.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
251  if ( isset( $page['waiting_id'] ) )
252  {
253    $url.= '&amp;waiting_id='.$page['waiting_id'];
254  }
255  $vtp->setGlobalVar( $handle, 'form_action', $url );
256//--------------------------------------------------------------------- advises
257  if ( $conf['upload_maxfilesize'] != '' )
258  {
259    $vtp->addSession( $handle, 'advise' );
260    $content = $lang['upload_advise_filesize'];
261    $content.= $conf['upload_maxfilesize'].' KB';
262    $vtp->setVar( $handle, 'advise.content', $content );
263    $vtp->closeSession( $handle, 'advise' );
264  }
265  if ( isset( $page['waiting_id'] ) )
266  {
267    $advise_title=$lang['upload_advise_thumbnail'].$_FILES['picture']['name'];
268    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
269
270    if ( $conf['upload_maxwidth_thumbnail'] != '' )
271    {
272      $vtp->addSession( $handle, 'advise' );
273      $content = $lang['upload_advise_width'];
274      $content.= $conf['upload_maxwidth_thumbnail'].' px';
275      $vtp->setVar( $handle, 'advise.content', $content );
276      $vtp->closeSession( $handle, 'advise' );
277    }
278    if ( $conf['upload_maxheight_thumbnail'] != '' )
279    {
280      $vtp->addSession( $handle, 'advise' );
281      $content = $lang['upload_advise_height'];
282      $content.= $conf['upload_maxheight_thumbnail'].' px';
283      $vtp->setVar( $handle, 'advise.content', $content );
284      $vtp->closeSession( $handle, 'advise' );
285    }
286  }
287  else
288  {
289    $advise_title = $lang['upload_advise'];
290    $advise_title.= get_cat_display_name( $page['cat_name'], ' - ',
291                                          'font-style:italic;' );
292    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
293
294    if ( $conf['upload_maxwidth'] != '' )
295    {
296      $vtp->addSession( $handle, 'advise' );
297      $content = $lang['upload_advise_width'];
298      $content.= $conf['upload_maxwidth'].' px';
299      $vtp->setVar( $handle, 'advise.content', $content );
300      $vtp->closeSession( $handle, 'advise' );
301    }
302    if ( $conf['upload_maxheight'] != '' )
303    {
304      $vtp->addSession( $handle, 'advise' );
305      $content = $lang['upload_advise_height'];
306      $content.= $conf['upload_maxheight'].' px';
307      $vtp->setVar( $handle, 'advise.content', $content );
308      $vtp->closeSession( $handle, 'advise' );
309    }
310  }
311  $vtp->addSession( $handle, 'advise' );
312  $content = $lang['upload_advise_filetype'];
313  $vtp->setVar( $handle, 'advise.content', $content );
314  $vtp->closeSession( $handle, 'advise' );
315//----------------------------------------- optionnal username and mail address
316  if ( !isset( $page['waiting_id'] ) )
317  {
318    $vtp->addSession( $handle, 'fields' );
319    $vtp->closeSession( $handle, 'fields' );
320  }
321  $vtp->closeSession( $handle, 'upload_not_successful' );
322}
323else
324{
325  $vtp->addSession( $handle, 'upload_successful' );
326  $vtp->closeSession( $handle, 'upload_successful' );
327}
328//----------------------------------------------------- return to main page url
329$url = './category.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
330$vtp->setGlobalVar( $handle, 'return_url', add_session_id( $url ) );
331//----------------------------------------------------------- html code display
332$code = $vtp->Display( $handle, 0 );
333echo $code;
334?>
Note: See TracBrowser for help on using the repository browser.