source: trunk/upload.php @ 354

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

Migration of common.php in the include directory to fit the new coding rules

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                           upload.php                                |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : 1.4                                                   |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-02-07 19:36:44 +0000 (Sat, 07 Feb 2004) $
10// | last modifier : $Author: gweltas $
11// | revision      : $Revision: 354 $
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// +-----------------------------------------------------------------------+
27$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//----------------------------------------------------- template initialization
136//
137// Start output of page
138//
139$title= $lang['upload_title'];
140include('include/page_header.php');
141$handle = $vtp->Open( './template/'.$user['template'].'/upload.vtp' );
142initialize_template();
143
144$tpl = array( 'upload_title', 'upload_username', 'mail_address', 'submit',
145              'upload_successful', 'search_return_main_page','upload_author',
146              'upload_name','upload_creation_date','upload_comment',
147              'mandatory' );
148templatize_array( $tpl, 'lang', $handle );
149
150$error = array();
151$page['upload_successful'] = false;
152if ( isset( $_GET['waiting_id'] ) )
153{
154  $page['waiting_id'] = $_GET['waiting_id'];
155}
156//-------------------------------------------------------------- picture upload
157// verfying fields
158if ( isset( $_POST['submit'] ) and !isset( $_GET['waiting_id'] ) )
159{
160  $path = $page['cat_dir'].$_FILES['picture']['name'];
161  if ( @is_file( $path ) )
162  {
163    array_push( $error, $lang['upload_file_exists'] );
164  }
165  // test de la présence des champs obligatoires
166  if ( $_FILES['picture']['name'] == '' )
167  {
168    array_push( $error, $lang['upload_filenotfound'] );
169  }
170  if ( !ereg( "([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)",
171             $_POST['mail_address'] ) )
172  {
173    array_push( $error, $lang['reg_err_mail_address'] );
174  }
175  if ( $_POST['username'] == '' )
176  {
177    array_push( $error, $lang['upload_err_username'] );
178  }
179 
180  $date_creation = '';
181  if ( $_POST['date_creation'] != '' )
182  {
183    list( $day,$month,$year ) = explode( '/', $_POST['date_creation'] );
184    // int checkdate ( int month, int day, int year)
185    if ( checkdate( $month, $day, $year ) )
186    {
187      // int mktime ( int hour, int minute, int second,
188      //              int month, int day, int year [, int is_dst])
189      $date_creation = mktime( 0, 0, 0, $month, $day, $year );
190    }
191    else
192    {
193      array_push( $error, $lang['err_date'] );
194    }
195  }
196  // creation of the "infos" field :
197  // <infos author="Pierrick LE GALL" comment="my comment"
198  //        date_creation="1056891767" name="" />
199  $xml_infos = '<infos';
200  $xml_infos.= ' author="'.htmlspecialchars($_POST['author'],ENT_QUOTES).'"';
201  $xml_infos.= ' comment="'.htmlspecialchars($_POST['comment'],ENT_QUOTES).'"';
202  $xml_infos.= ' date_creation="'.$date_creation.'"';
203  $xml_infos.= ' name="'.htmlspecialchars( $_POST['name'], ENT_QUOTES).'"';
204  $xml_infos.= ' />';
205
206  if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $_FILES['picture']['name'] ) )
207  {
208    // reload language file with administration labels
209    $isadmin = true;
210    include( './language/'.$user['language'].'.php' );
211    array_push( $error, $lang['update_wrong_dirname'] );
212  }
213 
214  if ( sizeof( $error ) == 0 )
215  {
216    $result = validate_upload( $path, $conf['upload_maxfilesize'],
217                               $conf['upload_maxwidth'],
218                               $conf['upload_maxheight']  );
219    for ( $j = 0; $j < sizeof( $result['error'] ); $j++ )
220    {
221      array_push( $error, $result['error'][$j] );
222    }
223  }
224
225  if ( sizeof( $error ) == 0 )
226  {
227    $query = 'insert into '.PREFIX_TABLE.'waiting';
228    $query.= ' (storage_category_id,file,username,mail_address,date,infos)';
229    $query.= ' values ';
230    $query.= '('.$page['cat'].",'".$_FILES['picture']['name']."'";
231    $query.= ",'".htmlspecialchars( $_POST['username'], ENT_QUOTES)."'";
232    $query.= ",'".$_POST['mail_address']."',".time().",'".$xml_infos."')";
233    $query.= ';';
234    mysql_query( $query );
235    $page['waiting_id'] = mysql_insert_id();
236    // mail notification for administrators
237    if ( $conf['mail_notification'] )
238    {
239      notify( 'upload' );
240    }
241  }
242}
243//------------------------------------------------------------ thumbnail upload
244if ( isset( $_POST['submit'] ) and isset( $_GET['waiting_id'] ) )
245{
246  // upload of the thumbnail
247  $query = 'select file';
248  $query.= ' from '.PREFIX_TABLE.'waiting';
249  $query.= ' where id = '.$_GET['waiting_id'];
250  $query.= ';';
251  $result= mysql_query( $query );
252  $row = mysql_fetch_array( $result );
253  $file = substr ( $row['file'], 0, strrpos ( $row['file'], ".") );
254  $extension = get_extension( $_FILES['picture']['name'] );
255  $path = $page['cat_dir'].'thumbnail/';
256  $path.= $conf['prefix_thumbnail'].$file.'.'.$extension;
257  $result = validate_upload( $path, $conf['upload_maxfilesize'],
258                             $conf['upload_maxwidth_thumbnail'],
259                             $conf['upload_maxheight_thumbnail']  );
260  for ( $j = 0; $j < sizeof( $result['error'] ); $j++ )
261  {
262    array_push( $error, $result['error'][$j] );
263  }
264  if ( sizeof( $error ) == 0 )
265  {
266    $query = 'update '.PREFIX_TABLE.'waiting';
267    $query.= " set tn_ext = '".$extension."'";
268    $query.= ' where id = '.$_GET['waiting_id'];
269    $query.= ';';
270    mysql_query( $query );
271    $page['upload_successful'] = true;
272  }
273}
274
275if ( !$page['upload_successful'] )
276{
277  $vtp->addSession( $handle, 'upload_not_successful' );
278//-------------------------------------------------------------- errors display
279  if ( sizeof( $error ) != 0 )
280  {
281    $vtp->addSession( $handle, 'errors' );
282    for ( $i = 0; $i < sizeof( $error ); $i++ )
283    {
284      $vtp->addSession( $handle, 'li' );
285      $vtp->setVar( $handle, 'li.li', $error[$i] );
286      $vtp->closeSession( $handle, 'li' );
287    }
288    $vtp->closeSession( $handle, 'errors' );
289  }
290//----------------------------------------------------------------- form action
291  $url = './upload.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
292  if ( isset( $page['waiting_id'] ) )
293  {
294    $url.= '&amp;waiting_id='.$page['waiting_id'];
295  }
296  $vtp->setGlobalVar( $handle, 'form_action', add_session_id( $url ) );
297//--------------------------------------------------------------------- advises
298  if ( $conf['upload_maxfilesize'] != '' )
299  {
300    $vtp->addSession( $handle, 'advise' );
301    $content = $lang['upload_advise_filesize'];
302    $content.= $conf['upload_maxfilesize'].' KB';
303    $vtp->setVar( $handle, 'advise.content', $content );
304    $vtp->closeSession( $handle, 'advise' );
305  }
306  if ( isset( $page['waiting_id'] ) )
307  {
308    $advise_title=$lang['upload_advise_thumbnail'].$_FILES['picture']['name'];
309    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
310
311    if ( $conf['upload_maxwidth_thumbnail'] != '' )
312    {
313      $vtp->addSession( $handle, 'advise' );
314      $content = $lang['upload_advise_width'];
315      $content.= $conf['upload_maxwidth_thumbnail'].' px';
316      $vtp->setVar( $handle, 'advise.content', $content );
317      $vtp->closeSession( $handle, 'advise' );
318    }
319    if ( $conf['upload_maxheight_thumbnail'] != '' )
320    {
321      $vtp->addSession( $handle, 'advise' );
322      $content = $lang['upload_advise_height'];
323      $content.= $conf['upload_maxheight_thumbnail'].' px';
324      $vtp->setVar( $handle, 'advise.content', $content );
325      $vtp->closeSession( $handle, 'advise' );
326    }
327  }
328  else
329  {
330    $advise_title = $lang['upload_advise'];
331    $advise_title.= get_cat_display_name( $page['cat_name'], ' - ',
332                                          'font-style:italic;' );
333    $vtp->setGlobalVar( $handle, 'advise_title', $advise_title );
334
335    if ( $conf['upload_maxwidth'] != '' )
336    {
337      $vtp->addSession( $handle, 'advise' );
338      $content = $lang['upload_advise_width'];
339      $content.= $conf['upload_maxwidth'].' px';
340      $vtp->setVar( $handle, 'advise.content', $content );
341      $vtp->closeSession( $handle, 'advise' );
342    }
343    if ( $conf['upload_maxheight'] != '' )
344    {
345      $vtp->addSession( $handle, 'advise' );
346      $content = $lang['upload_advise_height'];
347      $content.= $conf['upload_maxheight'].' px';
348      $vtp->setVar( $handle, 'advise.content', $content );
349      $vtp->closeSession( $handle, 'advise' );
350    }
351  }
352  $vtp->addSession( $handle, 'advise' );
353  $content = $lang['upload_advise_filetype'];
354  $vtp->setVar( $handle, 'advise.content', $content );
355  $vtp->closeSession( $handle, 'advise' );
356//----------------------------------------- optionnal username and mail address
357  if ( !isset( $page['waiting_id'] ) )
358  {
359    $vtp->addSession( $handle, 'fields' );
360    // username
361    if ( isset( $_POST['username'] ) ) $username = $_POST['username'];
362    else                               $username = $user['username'];
363    $vtp->setVar( $handle, 'fields.username',  $username );
364    // mail address
365    if ( isset( $_POST['mail_address'] ) )$mail_address=$_POST['mail_address'];
366    else                                  $mail_address=$user['mail_address'];
367    $vtp->setGlobalVar( $handle, 'user_mail_address',$user['mail_address'] );
368    // name of the picture
369        if (isset($_POST['name']))
370    $vtp->setVar( $handle, 'fields.name', $_POST['name'] );
371    // author
372        if (isset($_POST['author']))
373    $vtp->setVar( $handle, 'fields.author', $_POST['author'] );
374    // date of creation
375        if (isset($_POST['date_creation']))
376    $vtp->setVar( $handle, 'fields.date_creation', $_POST['date_creation'] );
377    // comment
378        if (isset($_POST['comment']))
379    $vtp->setVar( $handle, 'fields.comment', $_POST['comment'] );
380
381    $vtp->closeSession( $handle, 'fields' );
382
383    $vtp->addSession( $handle, 'note' );
384    $vtp->closeSession( $handle, 'note' );
385  }
386  $vtp->closeSession( $handle, 'upload_not_successful' );
387}
388else
389{
390  $vtp->addSession( $handle, 'upload_successful' );
391  $vtp->closeSession( $handle, 'upload_successful' );
392}
393//----------------------------------------------------- return to main page url
394$url = './category.php?cat='.$page['cat'].'&amp;expand='.$_GET['expand'];
395$vtp->setGlobalVar( $handle, 'return_url', add_session_id( $url ) );
396//----------------------------------------------------------- html code display
397$code = $vtp->Display( $handle, 0 );
398echo $code;
399include('include/page_tail.php');
400?>
Note: See TracBrowser for help on using the repository browser.