[2] | 1 | <?php |
---|
| 2 | /*************************************************************************** |
---|
[10] | 3 | * upload.php * |
---|
[2] | 4 | * ------------------- * |
---|
[57] | 5 | * application : PhpWebGallery 1.3 <http://phpwebgallery.net> * |
---|
| 6 | * author : Pierrick LE GALL <pierrick@z0rglub.com> * |
---|
[2] | 7 | * * |
---|
[57] | 8 | * $Id: upload.php 85 2003-09-10 22:24:03Z z0rglub $ |
---|
| 9 | * * |
---|
[2] | 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 | ***************************************************************************/ |
---|
[10] | 19 | |
---|
| 20 | //------------------------------------------------------------------- functions |
---|
[2] | 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 |
---|
| 30 | function 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'] ); |
---|
[10] | 39 | if ( $extension != 'gif' and $extension != 'jpg' and $extension != 'png' ) |
---|
[2] | 40 | { |
---|
[19] | 41 | array_push( $result['error'], $lang['upload_advise_filetype'] ); |
---|
[2] | 42 | return $result; |
---|
| 43 | } |
---|
| 44 | if ( !isset( $_FILES['picture'] ) ) |
---|
| 45 | { |
---|
| 46 | // do we even have a file? |
---|
[19] | 47 | array_push( $result['error'], "You did not upload anything!" ); |
---|
[2] | 48 | } |
---|
| 49 | else if ( $_FILES['picture']['size'] > $my_max_file_size * 1024 ) |
---|
| 50 | { |
---|
[19] | 51 | array_push( $result['error'], |
---|
| 52 | $lang['upload_advise_width'].$my_max_file_size.' KB' ); |
---|
[2] | 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 | { |
---|
[19] | 60 | array_push( $result['error'], $lang['upload_cannot_upload'] ); |
---|
[2] | 61 | } |
---|
| 62 | else |
---|
| 63 | { |
---|
| 64 | $size = getimagesize( $temp_name ); |
---|
| 65 | if ( isset( $image_max_width ) |
---|
[10] | 66 | and $image_max_width != "" |
---|
| 67 | and $size[0] > $image_max_width ) |
---|
[2] | 68 | { |
---|
[19] | 69 | array_push( $result['error'], |
---|
| 70 | $lang['upload_advise_width'].$image_max_width.' px' ); |
---|
[2] | 71 | } |
---|
| 72 | if ( isset( $image_max_height ) |
---|
[10] | 73 | and $image_max_height != "" |
---|
| 74 | and $size[1] > $image_max_height ) |
---|
[2] | 75 | { |
---|
[19] | 76 | array_push( $result['error'], |
---|
| 77 | $lang['upload_advise_height'].$image_max_height.' px' ); |
---|
[2] | 78 | } |
---|
| 79 | // $size[2] == 1 means GIF |
---|
| 80 | // $size[2] == 2 means JPG |
---|
| 81 | // $size[2] == 3 means PNG |
---|
[19] | 82 | switch ( $size[2] ) |
---|
[2] | 83 | { |
---|
[19] | 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'] ); |
---|
[2] | 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 |
---|
| 100 | include_once( './include/init.inc.php' ); |
---|
| 101 | //-------------------------------------------------- access authorization check |
---|
| 102 | check_login_authorization(); |
---|
| 103 | check_cat_id( $_GET['cat'] ); |
---|
[10] | 104 | if ( isset( $page['cat'] ) and is_numeric( $page['cat'] ) ) |
---|
[2] | 105 | { |
---|
| 106 | check_restrictions( $page['cat'] ); |
---|
| 107 | $result = get_cat_info( $page['cat'] ); |
---|
[82] | 108 | $page['cat_dir'] = get_complete_dir( $page['cat'] ); |
---|
[38] | 109 | $page['cat_site_id'] = $result['site_id']; |
---|
| 110 | $page['cat_name'] = $result['name']; |
---|
| 111 | $page['cat_uploadable'] = $result['uploadable']; |
---|
[2] | 112 | } |
---|
| 113 | else |
---|
| 114 | { |
---|
| 115 | $access_forbidden = true; |
---|
| 116 | } |
---|
| 117 | if ( $access_forbidden == true |
---|
[10] | 118 | or $page['cat_site_id'] != 1 |
---|
[38] | 119 | or !$conf['upload_available'] |
---|
| 120 | or !$page['cat_uploadable'] ) |
---|
[2] | 121 | { |
---|
[10] | 122 | echo '<div style="text-align:center;">'.$lang['upload_forbidden'].'<br />'; |
---|
[26] | 123 | echo '<a href="'.add_session_id( './category.php' ).'">'; |
---|
[10] | 124 | echo $lang['thumbnails'].'</a></div>'; |
---|
[2] | 125 | exit(); |
---|
| 126 | } |
---|
| 127 | //----------------------------------------------------- template initialization |
---|
| 128 | $vtp = new VTemplate; |
---|
[10] | 129 | $handle = $vtp->Open( './template/'.$user['template'].'/upload.vtp' ); |
---|
| 130 | initialize_template(); |
---|
| 131 | |
---|
| 132 | $tpl = array( 'upload_title', 'upload_username', 'mail_address', 'submit', |
---|
[26] | 133 | 'upload_successful', 'search_return_main_page','upload_author', |
---|
| 134 | 'upload_name','upload_creation_date','upload_comment', |
---|
| 135 | 'mandatory' ); |
---|
| 136 | templatize_array( $tpl, 'lang', $handle ); |
---|
[2] | 137 | |
---|
| 138 | $error = array(); |
---|
| 139 | $page['upload_successful'] = false; |
---|
| 140 | if ( isset( $_GET['waiting_id'] ) ) |
---|
| 141 | { |
---|
| 142 | $page['waiting_id'] = $_GET['waiting_id']; |
---|
| 143 | } |
---|
| 144 | //-------------------------------------------------------------- picture upload |
---|
[26] | 145 | // verfying fields |
---|
[10] | 146 | if ( isset( $_POST['submit'] ) and !isset( $_GET['waiting_id'] ) ) |
---|
[2] | 147 | { |
---|
| 148 | $path = $page['cat_dir'].$_FILES['picture']['name']; |
---|
| 149 | if ( @is_file( $path ) ) |
---|
| 150 | { |
---|
[26] | 151 | array_push( $error, $lang['upload_file_exists'] ); |
---|
[2] | 152 | } |
---|
| 153 | // test de la présence des champs obligatoires |
---|
[26] | 154 | if ( $_FILES['picture']['name'] == '' ) |
---|
[2] | 155 | { |
---|
[26] | 156 | array_push( $error, $lang['upload_filenotfound'] ); |
---|
[2] | 157 | } |
---|
| 158 | if ( !ereg( "([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)", |
---|
| 159 | $_POST['mail_address'] ) ) |
---|
| 160 | { |
---|
[26] | 161 | array_push( $error, $lang['reg_err_mail_address'] ); |
---|
[2] | 162 | } |
---|
| 163 | if ( $_POST['username'] == '' ) |
---|
| 164 | { |
---|
[26] | 165 | array_push( $error, $lang['upload_err_username'] ); |
---|
[2] | 166 | } |
---|
| 167 | |
---|
[26] | 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 | |
---|
[2] | 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 | { |
---|
[26] | 201 | array_push( $error, $result['error'][$j] ); |
---|
[2] | 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | if ( sizeof( $error ) == 0 ) |
---|
| 206 | { |
---|
[10] | 207 | $query = 'insert into '.PREFIX_TABLE.'waiting'; |
---|
[61] | 208 | $query.= ' (storage_category_id,file,username,mail_address,date,infos)'; |
---|
| 209 | $query.= ' values '; |
---|
| 210 | $query.= '('.$page['cat'].",'".$_FILES['picture']['name']."'"; |
---|
[2] | 211 | $query.= ",'".htmlspecialchars( $_POST['username'], ENT_QUOTES)."'"; |
---|
[26] | 212 | $query.= ",'".$_POST['mail_address']."',".time().",'".$xml_infos."')"; |
---|
[2] | 213 | $query.= ';'; |
---|
| 214 | mysql_query( $query ); |
---|
| 215 | $page['waiting_id'] = mysql_insert_id(); |
---|
[85] | 216 | // mail notification for administrators |
---|
| 217 | if ( $conf['mail_notification'] ) |
---|
| 218 | { |
---|
| 219 | notify( 'comment' ); |
---|
| 220 | } |
---|
[2] | 221 | } |
---|
| 222 | } |
---|
| 223 | //------------------------------------------------------------ thumbnail upload |
---|
[10] | 224 | if ( isset( $_POST['submit'] ) and isset( $_GET['waiting_id'] ) ) |
---|
[2] | 225 | { |
---|
| 226 | // upload of the thumbnail |
---|
| 227 | $query = 'select file'; |
---|
[10] | 228 | $query.= ' from '.PREFIX_TABLE.'waiting'; |
---|
[2] | 229 | $query.= ' where id = '.$_GET['waiting_id']; |
---|
| 230 | $query.= ';'; |
---|
| 231 | $result= mysql_query( $query ); |
---|
| 232 | $row = mysql_fetch_array( $result ); |
---|
| 233 | $file = substr ( $row['file'], 0, strrpos ( $row['file'], ".") ); |
---|
| 234 | $extension = get_extension( $_FILES['picture']['name'] ); |
---|
| 235 | $path = $page['cat_dir'].'thumbnail/'; |
---|
[20] | 236 | $path.= $conf['prefix_thumbnail'].$file.'.'.$extension; |
---|
[2] | 237 | $result = validate_upload( $path, $conf['upload_maxfilesize'], |
---|
| 238 | $conf['upload_maxwidth_thumbnail'], |
---|
| 239 | $conf['upload_maxheight_thumbnail'] ); |
---|
| 240 | $upload_type = $result['type']; |
---|
| 241 | for ( $j = 0; $j < sizeof( $result['error'] ); $j++ ) |
---|
| 242 | { |
---|
[26] | 243 | array_push( $error, $result['error'][$j] ); |
---|
[2] | 244 | } |
---|
| 245 | if ( sizeof( $error ) == 0 ) |
---|
| 246 | { |
---|
[10] | 247 | $query = 'update '.PREFIX_TABLE.'waiting'; |
---|
[2] | 248 | $query.= " set tn_ext = '".$extension."'"; |
---|
| 249 | $query.= ' where id = '.$_GET['waiting_id']; |
---|
| 250 | $query.= ';'; |
---|
| 251 | mysql_query( $query ); |
---|
| 252 | $page['upload_successful'] = true; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | if ( !$page['upload_successful'] ) |
---|
| 257 | { |
---|
| 258 | $vtp->addSession( $handle, 'upload_not_successful' ); |
---|
| 259 | //-------------------------------------------------------------- errors display |
---|
| 260 | if ( sizeof( $error ) != 0 ) |
---|
| 261 | { |
---|
| 262 | $vtp->addSession( $handle, 'errors' ); |
---|
| 263 | for ( $i = 0; $i < sizeof( $error ); $i++ ) |
---|
| 264 | { |
---|
| 265 | $vtp->addSession( $handle, 'li' ); |
---|
| 266 | $vtp->setVar( $handle, 'li.li', $error[$i] ); |
---|
| 267 | $vtp->closeSession( $handle, 'li' ); |
---|
| 268 | } |
---|
| 269 | $vtp->closeSession( $handle, 'errors' ); |
---|
| 270 | } |
---|
| 271 | //----------------------------------------------------------------- form action |
---|
| 272 | $url = './upload.php?cat='.$page['cat'].'&expand='.$_GET['expand']; |
---|
| 273 | if ( isset( $page['waiting_id'] ) ) |
---|
| 274 | { |
---|
| 275 | $url.= '&waiting_id='.$page['waiting_id']; |
---|
| 276 | } |
---|
[26] | 277 | $vtp->setGlobalVar( $handle, 'form_action', add_session_id( $url ) ); |
---|
[2] | 278 | //--------------------------------------------------------------------- advises |
---|
| 279 | if ( $conf['upload_maxfilesize'] != '' ) |
---|
| 280 | { |
---|
| 281 | $vtp->addSession( $handle, 'advise' ); |
---|
| 282 | $content = $lang['upload_advise_filesize']; |
---|
| 283 | $content.= $conf['upload_maxfilesize'].' KB'; |
---|
| 284 | $vtp->setVar( $handle, 'advise.content', $content ); |
---|
| 285 | $vtp->closeSession( $handle, 'advise' ); |
---|
| 286 | } |
---|
| 287 | if ( isset( $page['waiting_id'] ) ) |
---|
| 288 | { |
---|
| 289 | $advise_title=$lang['upload_advise_thumbnail'].$_FILES['picture']['name']; |
---|
| 290 | $vtp->setGlobalVar( $handle, 'advise_title', $advise_title ); |
---|
| 291 | |
---|
| 292 | if ( $conf['upload_maxwidth_thumbnail'] != '' ) |
---|
| 293 | { |
---|
| 294 | $vtp->addSession( $handle, 'advise' ); |
---|
| 295 | $content = $lang['upload_advise_width']; |
---|
| 296 | $content.= $conf['upload_maxwidth_thumbnail'].' px'; |
---|
| 297 | $vtp->setVar( $handle, 'advise.content', $content ); |
---|
| 298 | $vtp->closeSession( $handle, 'advise' ); |
---|
| 299 | } |
---|
| 300 | if ( $conf['upload_maxheight_thumbnail'] != '' ) |
---|
| 301 | { |
---|
| 302 | $vtp->addSession( $handle, 'advise' ); |
---|
| 303 | $content = $lang['upload_advise_height']; |
---|
| 304 | $content.= $conf['upload_maxheight_thumbnail'].' px'; |
---|
| 305 | $vtp->setVar( $handle, 'advise.content', $content ); |
---|
| 306 | $vtp->closeSession( $handle, 'advise' ); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | else |
---|
| 310 | { |
---|
| 311 | $advise_title = $lang['upload_advise']; |
---|
| 312 | $advise_title.= get_cat_display_name( $page['cat_name'], ' - ', |
---|
| 313 | 'font-style:italic;' ); |
---|
| 314 | $vtp->setGlobalVar( $handle, 'advise_title', $advise_title ); |
---|
| 315 | |
---|
| 316 | if ( $conf['upload_maxwidth'] != '' ) |
---|
| 317 | { |
---|
| 318 | $vtp->addSession( $handle, 'advise' ); |
---|
| 319 | $content = $lang['upload_advise_width']; |
---|
| 320 | $content.= $conf['upload_maxwidth'].' px'; |
---|
| 321 | $vtp->setVar( $handle, 'advise.content', $content ); |
---|
| 322 | $vtp->closeSession( $handle, 'advise' ); |
---|
| 323 | } |
---|
| 324 | if ( $conf['upload_maxheight'] != '' ) |
---|
| 325 | { |
---|
| 326 | $vtp->addSession( $handle, 'advise' ); |
---|
| 327 | $content = $lang['upload_advise_height']; |
---|
| 328 | $content.= $conf['upload_maxheight'].' px'; |
---|
| 329 | $vtp->setVar( $handle, 'advise.content', $content ); |
---|
| 330 | $vtp->closeSession( $handle, 'advise' ); |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | $vtp->addSession( $handle, 'advise' ); |
---|
| 334 | $content = $lang['upload_advise_filetype']; |
---|
| 335 | $vtp->setVar( $handle, 'advise.content', $content ); |
---|
| 336 | $vtp->closeSession( $handle, 'advise' ); |
---|
| 337 | //----------------------------------------- optionnal username and mail address |
---|
| 338 | if ( !isset( $page['waiting_id'] ) ) |
---|
| 339 | { |
---|
| 340 | $vtp->addSession( $handle, 'fields' ); |
---|
[26] | 341 | // username |
---|
| 342 | if ( isset( $_POST['username'] ) ) $username = $_POST['username']; |
---|
| 343 | else $username = $user['username']; |
---|
| 344 | $vtp->setVar( $handle, 'fields.username', $username ); |
---|
| 345 | // mail address |
---|
| 346 | if ( isset( $_POST['mail_address'] ) )$mail_address=$_POST['mail_address']; |
---|
| 347 | else $mail_address=$user['mail_address']; |
---|
| 348 | $vtp->setGlobalVar( $handle, 'user_mail_address',$user['mail_address'] ); |
---|
| 349 | // name of the picture |
---|
| 350 | $vtp->setVar( $handle, 'fields.name', $_POST['name'] ); |
---|
| 351 | // author |
---|
| 352 | $vtp->setVar( $handle, 'fields.author', $_POST['author'] ); |
---|
| 353 | // date of creation |
---|
| 354 | $vtp->setVar( $handle, 'fields.date_creation', $_POST['date_creation'] ); |
---|
| 355 | // comment |
---|
| 356 | $vtp->setVar( $handle, 'fields.comment', $_POST['comment'] ); |
---|
| 357 | |
---|
[2] | 358 | $vtp->closeSession( $handle, 'fields' ); |
---|
[26] | 359 | |
---|
| 360 | $vtp->addSession( $handle, 'note' ); |
---|
| 361 | $vtp->closeSession( $handle, 'note' ); |
---|
[2] | 362 | } |
---|
| 363 | $vtp->closeSession( $handle, 'upload_not_successful' ); |
---|
| 364 | } |
---|
| 365 | else |
---|
| 366 | { |
---|
| 367 | $vtp->addSession( $handle, 'upload_successful' ); |
---|
| 368 | $vtp->closeSession( $handle, 'upload_successful' ); |
---|
| 369 | } |
---|
| 370 | //----------------------------------------------------- return to main page url |
---|
| 371 | $url = './category.php?cat='.$page['cat'].'&expand='.$_GET['expand']; |
---|
| 372 | $vtp->setGlobalVar( $handle, 'return_url', add_session_id( $url ) ); |
---|
| 373 | //----------------------------------------------------------- html code display |
---|
| 374 | $code = $vtp->Display( $handle, 0 ); |
---|
| 375 | echo $code; |
---|
| 376 | ?> |
---|