<?php 

include_once "scripts/sb_gallery_config.php";
include_once "scripts/sb_gallery.php";

require_once('scripts/sb_functions.php');
global $logged_in;
$logged_in = logged_in( false, true );


if (!$logged_in){

  // Verify Password
  // two types possible: use check_password of check password.txt
  //
  // 1) this will start a new session and therefore logout from previous one!
  //
  $logged_in = check_password($_POST['user'],$_POST['pass']);
  // .....
  // 2) the following checks the password without creating a new session
  // $passFile = 'config/password.txt';
  // $hashed = sb_read_file( $passFile );
		
  // $mypasswd = $_POST['user'].$_POST['pass'];
  // $chkpass = crypt($mypasswd, $hashed); 
  // if ($hashed === $chkpass) { $logged_in = true; }
  // end of password check
}

if ($logged_in){

  /*
    $test=base64_decode($_POST['img']);
    header('Content-Type: image/gif');
    echo $test;
    exit;
  */

  if (isset($_POST['pic']) && isset($_POST['img'])){
    save_pic($_POST['pic'],base64_decode($_POST['img']));
  }

  /*
    if (!file_exists($pic)){
      $url='http://'.$_SERVER['HTTP_HOST'].$_SESSION['site_path'].'/'.$_REQUEST['pic'];

      $image = http_get($url,$pic);
      save_pic($pic,$image);
    }
  */
}

// wrong password? re-direct!
if (!$logged_in){
  redirect_to_url( "index.php" );
}


	
read_config();	
sb_language( 'add' );


if (isset($_REQUEST['pic'])){
  if ( file_exists( $_REQUEST['pic'] ) ) {

    // Initial passthrough
    global $auto_discovery_confirm;
    global $album_directory, $data_directory;


    $pic = $_REQUEST['pic'];

    // split picture and paths

    $elements = explode('/',$pic);
    $file = array_pop($elements);
    if ($elements[0] == $data_directory){  // if the first dir is the data-dir
      array_shift($elements);              // --> remove it
      if ($elements[0] == 'scaled'){       // ... and the 2nd for scaled img's
	array_shift($elements);            // --> remove it
      }
    }
    if ($elements[0] == $album_directory){ // remove the album root dir
      array_shift($elements);
      $album = implode('/',$elements);     // put album path together
    }


    // subject line = picture name

    $subject = 'Picture of the day (';
    $subject .= str_replace('.jpg.jpg','.jpg',$file);
    $subject .= ')';

    //    if (is_file($album_directory.'/'.$album.'/'.$subject)){
    //      $original = $album_directory.'/'.$album.'/'.$subject;
    //    }
    // if (is_dir($album_directory.'/'.$album)){
    //   $subject .= 
    //	' [url=gallery.php?page=1&album='.$album.' new=false]... more[/url]';
    // }

    // entry contents = centalized image, linked to non-scaled image
    //  + link to thumbnail pagee

    $text = '[html]<div align="center">[/html]';

    if (is_dir($album_directory.'/'.$album)){
      $text .= '[url=gallery.php?page=1&album='.$album.' new=false]';
    }

    /*
    if (isset($original)){
      $text .= '[html]<a href="'.$original.'" target="_blank">[/html]';
    }
    */
    $text .= '[img=' . $pic;

    $elements = getimagesize($pic);
    $width = $elements[0];
    $height = $elements[1];
    global $blog_config;
    if ($width > $blog_config["blog_max_pic_width"]){
      $text .= ' width='.$blog_config["blog_max_pic_width"];
    }
    elseif ($height > $blog_config["blog_max_pic_height"]){
      $text .= ' height='.$blog_config["blog_max_pic_height"];
    }
    $text .= ' popup=false]';


    // $text .= '[img=' . $pic . ' float=left popup=false]';
    /*
    if (isset($original)){
      $text .= '[html]</a>[/html]';
    }
    if (is_dir($album_directory.'/'.$album)){
      $text .= "\n".' [url=gallery.php?page=1&album='.$album.' new=false]... more[/url]';
    }
    */

    if (is_dir($album_directory.'/'.$album)){
      global $lang_string;
      $text .= "\n".$lang_string[ 'more' ];
      $text .= '[/url]';
    }

    $text .= '[html]</div>[/html]';


    // create the blog entry

    $category = array();
    $ok = write_entry( stripslashes( $subject ), 
		       stripslashes( $text ), 
		       '', false, $category );


    // success!

    if ( $ok === true ) {

      $y = date('y');
      $m = date('m');
      $stamp = date('ymd-His');   // hopefully it's still the same ....

      $permalink = 'comments.php?y='.$y.'&m='.$m.'&entry=entry'.$stamp;
      save_pic_entry($pic,$y,$m,'entry'.$stamp);

      if( strlen($auto_discovery_confirm['text']) > 0 ) {
	// Find the trackback URLs
	$ad_array = trackback_autodiscover( $auto_discovery_confirm['text'] );
      }
    }		
  }
}

redirect_to_url( "index.php" );


?>