<?php 

$UPLOAD_LIMIT = 300000;

require_once('scripts/sb_functions.php');
require_once('scripts/sb_gallery.php');
include_once('scripts/sb_gallery_config.php');
include_once('scripts/sb_gallery_admin.php');

if (!signed_in()){
  global $logged_in;
  $logged_in = logged_in( true, true );
}
else{
    global $blog_permissions;
    if (! $blog_permissions['upload']==1 ){ 
	redirect_to_url('index.php');
    }
}

if ( !session_id() ) {
  session_start();
}

read_config();
	
sb_language( 'comments' );
sb_language( 'upload_img' );


if ($_POST['upload'] && isset($_SESSION['capcha'])){
  if ( $_POST['comment_remember'] != 0 ){
    setcookie( "comment_name", $_POST['comment_name'], time()+60*60*24*30);
    setcookie( "comment_email", $_POST['comment_email'],time()+60*60*24*30);
  }
}

html_header('',false);



function page_content() {
   global $lang_string, $user_colors, $blog_config;
   global $album_directory,$signed_in,$UPLOAD_LIMIT;

  echo '<h2>'.$lang_string['title'].'</h2>';

  if ($_POST['upload']){
    if ($signed_in || isset($_SESSION['capcha'])){
      upload_photo();
    }
  }

  if (! $signed_in){
    $_SESSION[ 'capcha' ] = sb_get_capcha();
  }

  if (file_exists("images/upload.jpg")){
    echo '<img id="img_float_right" src="images/upload.jpg" width="250">';
  }
    ?>

<p><hr>

<?php 
   $string = $lang_string['upload_instructions']; 
  $string = str_replace('%s',abs($UPLOAD_LIMIT/1000),$string);
  echo $string;
?>
<p>
<hr>

<form enctype="multipart/form-data" action="upload_photo.php" 
      method="post">
<?php echo( $lang_string['select_file'] ); ?><br />
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $UPLOAD_LIMIT; ?>" />
<input name="userfile" type="file"><br />
<?php
 getsubdir($album_directory,'',$subdirs,$level,1);
 if (is_array($subdirs)){
     ksort($subdirs,SORT_STRING);
     echo '<label for="category">'.$lang_string['select_category'];
     echo '</label><br>';
     //     $size = min(count($subdirs),5);
     //     echo '<select size="'.$size.'" name="category">';
     echo '<select name="category">';
     foreach ($subdirs as $dir => $mtime){
	 echo '<option>'.$dir.'</option>';
     }
     echo '</select> '.$lang_string['or'].' <br>';
 }
?>
<label for="new_category"><?php echo($lang_string["new_category"]); ?>
</label><br>
<input type="text" name="new_category" ><br>
<label for="comment_name"><?php echo( $lang_string['comment_name'] );?></label>
<br />

   <?php if (!$signed_in){ ?>

<input type="text" name="comment_name" 
value="<?php echo $_COOKIE['comment_name']; ?>" autocomplete="OFF"><br />
<label for="comment_email">
<?php echo( $lang_string['comment_email'] ); ?></label><br />
<input type="text" name="comment_email" 
value="<?php echo $_COOKIE['comment_email']; ?>" autocomplete="OFF"><br />
<label for="comment_remember"><?php echo( $lang_string['comment_remember'] ); ?><input type="checkbox" 
name="comment_remember" value="1" <?php if (!empty($_COOKIE['comment_email'])) echo 'checked'; ?> autocomplete="OFF"></label><br /><br />

   <?php } ?>

   <?php if (!$signed_in){ ?>
<label for="comment_capcha"><?php printf( $lang_string['comment_capcha'], sb_str_to_ascii( $_SESSION[ 'capcha' ] ) ); ?></label><br />
<input type="text" name="comment_capcha" value="" autocomplete="OFF"><br /><br />
   <?php } ?>

<input name='upload' type="submit" 
value="<?php echo( $lang_string['upload_btn'] ); ?>">
</form>
<hr>
<?php

}



function upload_photo(){
   global $lang_string, $user_colors, $blog_config;
   global $album_directory,$signed_in,$UPLOAD_LIMIT;

   if (!$signed_in){
     if ( $_POST[ 'comment_capcha'] != $_SESSION[ 'capcha' ] ) {
       return upload_error("Anti-spam capcha doesn't match! Try again!");
     }
     
     if (!$_POST['comment_email']){
       return upload_error('No e-mail adress posted?!');
     }
   }

   if ( $_FILES['userfile']['error'] != 0 ) {
     return upload_error("There is an error with your upload!<br> Maybe the file is too big?");
   }

   if ( $_FILES['userfile']['size'] > $UPLOAD_LIMIT ) {
     return upload_error("The file is larger than $UPLOAD_LIMIT B!<br>Try to reduce its size!");
   }

   if (getimagesize($_FILES['userfile']['tmp_name']) == FALSE){
     return upload_error("This doesn't seem to be an image!");
   }

   if (!$signed_in){
     $email=$_POST['comment_email'];
     if (!preg_match('/^[a-zA-Z0-9\_\-\.]+\@[a-zA-Z0-9\_\-\.]+$/',$email)){
       return upload_error("Couldn't parse your e-mail adress!<br>Only the following characters are allowed: [a-z0-9._-]");
     }
   }

   $category=$_POST['category'];
   if (isset($_POST['new_category'])){
       $length = strlen($_POST['new_category']);
       if ($length>0 && $length<20){
	   $category=$_POST['new_category'];
       }
   }
   $category = str_replace(' ','_',$category);
   if (!preg_match('/^[a-zA-Z0-9\_\-]+$/',$category)){
     return upload_error("Category name not allowed!<br>Use the following characters only: [a-z0-9_- ]");
   }

   $album = $category;
   if (!$signed_in){
     $album .= '/';
     $album .= str_replace('@','_at_',$email);
   }

   if (!file_exists($album_directory)){
     if (!my_mkdir($album_directory)){
       return upload_error("Couldn't access the album directory");
     }
   }
   
   $file = $_FILES['userfile']['name'];
   if (!preg_match('/(\.jpg|\.gif|\.GIF|\.JPG)$/',$file)){
     return upload_error("Only files with the extension '.gif' or '.jpg' are allowed!");
   }

/*
//   if ((!isset($category)) || ($category == '')){
       if (function_exists('exif_read_data')){
	   if (exif_read_data($file,0,true)){
	       foreach ($exif as $key => $section) {
		   foreach ($section as $name => $val) {
		       echo "$key.$name: $val<br />\n";
		   }
	       }
	   }
       }
//   }
*/
   
   if (!file_exists($album_directory.'/'.$category)){
     if (!my_mkdir($album_directory.'/'.$category)){
       return upload_error("Couldn't access the album directory");
     }
   }

   if (!file_exists($album_directory.'/'.$album)){
     if (!my_mkdir($album_directory.'/'.$album)){
       return upload_error("Couldn't access your personal album directory");
     }
   }

   $dir = $album_directory.'/'.$album;
   $file = preg_replace('/[^a-zA-Z0-9\.\-\_]/','_',$file);
   if (file_exists($dir.'/'.$file)){
     $count=0;
     while (file_exists($dir.'/'."$count".$file)){
       $count++;
     }
     $uploadfile = $dir.'/'."$count".$file;
   }
   else{
     $uploadfile = $dir.'/'.$file;
   }

   if (! move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile ) ) {
     echo "Couldn't move the file to ".$uploadfile.'<br>';
     return false;
   }

   echo 'Upload ok!<br><hr>';
   echo( '<a href="index.php">' . $lang_string['home'] . '</a><br /><br />');

   if ( $blog_config[ "blog_email_notification" ] ) {
     $subject="New image uploaded at " . $blog_config[ "blog_title" ];

     $body = 'image: '.$uploadfile."\n";
     $body .= 'by '.$_POST['comment_name'].' <'.$email."> \n\n";
     $body .= 'HTTP_REFERER: '.$_SERVER['HTTP_REFERER']."\n";
     $body .= ' REMOTE_HOST: '.$_SERVER['REMOTE_HOST']."\n";
     $body .= ' REMOTE_PORT: '.$_SERVER['REMOTE_PORT']."\n";
     $body .= ' REMOTE_ADDR: '.$_SERVER['REMOTE_ADDR']."\n\n";
     $body .= 'link: http://';
     $body .= $_SERVER['SERVER_NAME'];
     $body .= dirname($_SERVER['PHP_SELF']);
     $body .= 'gallery.php?album='.urlencode($album);
     $body .= ';pic='.urlencode($file);

     sb_mail( $blog_config[ 'blog_email' ], 
	      $blog_config[ 'blog_email' ], $subject, $body, true );
   }
}


function upload_error($string){
  echo '<span bgcolor="red">';
  echo $string;
  echo '</span><hr>';
}



theme_pagelayout();
html_footer();


?>
