<?php

session_start();

require_once('scripts/sb_functions.php');


global $logged_in;
$logged_in = logged_in( false, true );

read_config();
sb_language( 'comments' );


// Verify information being passed:
$ok = false;
if ( array_key_exists( 'y', $_REQUEST ) && 
     array_key_exists( 'm', $_REQUEST ) && 
     array_key_exists( 'entry', $_REQUEST ) ) {
    // Dis-allow dots, and slashes to make sure the
    // user is not able to back-up a directory.
    //
    // Make sure the string lengths are correct.
    if ( strpos( $_REQUEST[ 'y' ], array( '/', '.', '\\', '%' ) ) === false && strlen( $_REQUEST[ 'y' ] ) == 2 &&
	 strpos( $_REQUEST[ 'm' ], array( '/', '.', '\\', '%' ) ) === false && strlen( $_REQUEST[ 'm' ] ) == 2 &&
	 strpos( $_REQUEST[ 'entry' ], array( '/', '.', '\\', '%' ) ) === false && strlen( $_REQUEST[ 'entry' ] ) == 18 ) {
	
	// Verify that the file exists.
	if ( entry_exists ( $_REQUEST[ 'y' ], $_REQUEST[ 'm' ], $_REQUEST[ 'entry' ] ) ) {

##--------------------------------------------------------
## comment submitted!

	  global $blog_config;
	    if ( array_key_exists( 'submit', $_POST ) && 
		 ( $blog_config['disable_comment_capcha'] ||
		   array_key_exists( 'comment_capcha', $_POST ) )){

	      global $signed_in, $blog_permissions;
	      if ( $logged_in || 
		   ($blog_config[ 'blog_enable_comments' ]) ||
		   ($signed_in && $blog_permissions['comment'])){


		if ( $_POST['blog_text'] != '' && 
		     $_POST['comment_name'] != '' &&
		     ( $blog_config['disable_comment_capcha'] ||
		       ($_SESSION[ 'capcha_' . $_POST[ 'entry' ] ] != '' &&
			$_POST[ 'comment_capcha' ] == 
			$_SESSION[ 'capcha_' . $_POST[ 'entry' ] ] ))
		     ) {

		  $ok = write_comment($_REQUEST[ 'y' ], 
				      $_REQUEST[ 'm' ], 
				      $_REQUEST[ 'entry' ], 
				      sb_stripslashes($_POST['comment_name']),
				      sb_stripslashes($_POST['comment_email']),
				      sb_stripslashes($_POST['comment_url']), 
				      $_POST[ 'comment_remember' ], 
				      sb_stripslashes($_POST['blog_text']));

		  @session_unregister( 'capcha_' . $_REQUEST[ 'entry' ] );
		}
		else{
		  if ($_POST['blog_text'] == ''){
		    $ok = $lang_string['error_empty'];
		  }
		  elseif ($_POST['comment_name'] == ''){
		    $ok = $lang_string['error_no_name'];
		  }
		  elseif (! $blog_config['disable_comment_capcha']) {
		    $ok = $lang_string['error_captcha'];
		  }
		  else{
		    $ok = $lang_string['other_error'];
		  }
		}
	      }
	      else{
		if ($signed_in && !$blog_config['blog_enable_comments']){
		  $ok = $lang_string['error_permissions'];
		}
		else{
		  $ok = $lang_string['error_login'];
		}
	      }
	    }

##--------------------------------------------------------
## view comments!

	    else{
		$ok = true;
	    }
	}
    }

    if ( (! array_key_exists( 'submit', $_POST )) &&  $ok !== true ) {
	redirect_to_url( 'index.php' );
    }
}


/////////////////////////////////////////////////////////////////


$extra_header = '
		<script type="text/javascript">
		<!--
		// Validate the Form
		function validate_comment(theform) {
			if ( theform.blog_text.value=="" || theform.comment_name.value=="" || theform.comment_capcha.value=="" ) {
				alert("<?php echo( $lang_string[ "form_error" ] ); ?>");
				return false;
			} else {
				return true;
			}
		}
		-->
	</script>
';

html_header($extra_header,true);





function page_content() {
    global $lang_string, $user_colors, $logged_in, $blog_config;
    global $signed_in,$blog_permissions;
    global $ok;

    if ( array_key_exists( 'submit', $_POST ) &&  $ok !== true ) {
	echo $lang_string[ 'error_add' ].$ok.'<p />';
	echo( '<a href="index.php">' . $lang_string[ 'home' ] . '</a><br /><br />' );
    }
    else{

      $y = $_REQUEST[ 'y' ];
      $m = $_REQUEST[ 'm' ];
      $entry = $_REQUEST[ 'entry' ];

      $entry_array = read_entry( $y,$m,$entry,'comments' );
      echo (theme_blogentry( $entry_array ));

      echo( read_comments( $y, $m, $entry, $logged_in ) );



	if ( $logged_in || 
	     ($blog_config[ 'blog_enable_comments' ] == true) ||
	     ($signed_in && $blog_permissions['comment'])){
	    require_once('scripts/sb_blogeditor.php');
	    if (! $blog_config['disable_comment_capcha']){
	      @session_unregister( 'capcha_' . $_REQUEST[ 'entry' ] );
	      $_SESSION[ 'capcha_' . $_REQUEST[ 'entry' ] ]=sb_get_capcha();
	    }
	    editor_comments();
	}
    }

}

global $blog_config;
if ( $blog_config[ 'blog_comments_popup' ] == 1 ) {
    theme_popuplayout();
} else {
    theme_pagelayout();
}

html_footer();

?>
