source: trunk/upload.php @ 369

Last change on this file since 369 was 369, checked in by gweltas, 20 years ago

Template migration

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