<?php

require_once('scripts/sb_functions.php');
if (!signed_in()){
  global $logged_in;
  $logged_in = logged_in( true, true );
}
else{
    global $blog_permissions;
    if (! $blog_permissions['add']==1 ){ 
	redirect_to_url('index.php');
    }
}
	
read_config();	
sb_language( 'add' );
	


if ( array_key_exists( 'submit', $_POST ) ){
    
// "CGI" Functions
if ( array_key_exists( 'no', $_POST ) || array_key_exists( 'yes', $_POST ) ) {
    if ( array_key_exists( 'no', $_POST ) ) {
	// User clicked the "Cancel" button
	redirect_to_url( 'index.php' );
	
    } else {
	global $auto_discovery_confirm;
         
	if ( array_key_exists( 'yes', $_POST ) ) {
	    // User clicked the "OK" button
	    $ad_array = $_POST[ 'ad_array' ];
	    foreach ($_POST[ 'confirm' ] as $name => $value) {
		sb_tb_ping ( $ad_array[$name], 
			     $_POST[ 'title' ], 
			     $_POST[ 'permalink' ], 
			     $_POST[ 'excerpt' ] );
	    }
	    redirect_to_url( 'index.php' );
	}
    }
} else {
    // Initial passthrough
    global $auto_discovery_confirm, $blog_config;

    // new: add translations to en entry

    if (isset($_POST['entry_language'])){

	$entry_lang = $_POST['entry_language'];
	$iso639 = iso639();
	if (isset($iso639[$entry_lang])){
	    $entry_lang = strtolower($iso639[$entry_lang]);
	}
	if ($blog_config[ 'blog_default_language' ] != $entry_lang){


	if (isset($_POST[ 'entry' ])){
	  if (!is_dir($_POST[ 'entry' ])){
	    my_mkdir($_POST[ 'entry' ]);
	  }
	  if (is_dir($_POST[ 'entry' ])){
	    if (!is_dir($_POST[ 'entry' ].'/trans')){
	      my_mkdir($_POST[ 'entry' ].'/trans');
	    }
	    if (is_dir($_POST[ 'entry' ].'/trans')){
	      $trans = array();
	      if (file_exists($_POST[ 'entry' ].'/trans.txt')){
		$trans = @file($_POST[ 'entry' ].'/trans.txt');
	      }
	      $iso639 = iso639();
	      $langname = $iso639[$_POST['entry_language']];
	      $langid = $_POST['entry_language'];
	      $key = array_search($langid.'_'.$langname."\n",$trans);
	      if (!is_int($key)){
		append_to_file($_POST[ 'entry' ].'/trans.txt',
			       $langid.'_'.$langname."\n");
	      }
	      $_POST['entry'] = 
		$_POST[ 'entry' ].'/trans/'.strtolower($langname);
	    }
	  }
	}
      }
    }

		
    $ok = write_entry( sb_stripslashes( $_POST[ 'blog_subject' ] ), 
		       sb_stripslashes( $_POST[ 'blog_text' ] ), 
		       sb_stripslashes( $_POST[ 'tb_ping' ] ), 
		       $_POST[ 'entry' ], $_POST[ 'catlist' ],
		       $_POST[ 'blog_editor' ]);
		
    if ( $ok === true ) {
	if( strlen($auto_discovery_confirm[ 'text' ]) > 0 ) {
	    // Find the trackback URLs
	    $ad_array = trackback_autodiscover( $auto_discovery_confirm[ 'text' ] );
				
	    // Is there anything to be confirmed?
	    if ( is_array( $ad_array ) ) {
		if( count( $ad_array ) === 0 ) {
		    redirect_to_url( 'index.php' );
		}
	    } else {
		redirect_to_url( 'index.php' );
	    }
	} else {
	    redirect_to_url( 'index.php' );
	}
    }
    else {
	redirect_to_url( "index.php" );
    }
}
// send header with language specific charater set!

 html_header('',false);

}
else{
    html_header('',true);
}


function page_content() {


    ## entry submitted

    if ( array_key_exists( 'submit', $_POST ) ){


	global $lang_string, $user_colors, $ad_array, $auto_discovery_confirm;
		
	if ( array_key_exists( 'no', $_POST ) || 
	     array_key_exists('yes',$_POST)) {
	    // Check to see if we're posting data...
	    global $ok;
	    if ( $ok !== true ) {
		echo( $lang_string[ 'error' ] . $ok . '<p />' );
	    }
	    echo( '<a href="index.php">' . $lang_string[ 'home' ] . '</a><br /><br />' );
	} else {
    ?>
			
	<h2><?php echo( $lang_string[ 'title_ad' ] ); ?></h2>
	<?php echo( $lang_string[ 'instructions_ad' ] ); ?><p />
			
	<div class="hr"><hr /></div>
			
	<form accept-charset="<?php echo( $lang_string[ 'html_charset' ] ); ?>,iso-8859-1,utf-8" action='add_cgi.php' method="post">
			
	<?php
        if ( is_array( $ad_array ) ) {
	    echo "<table width=\"100%\">";
	    for ( $k = 0; $k < count( $ad_array ); $k++ ) {
		echo "<tr><td><input type=\"checkbox\" name=\"confirm[$k]\" checked></td>\n";
		echo "<td>" . $ad_array[$k];
		echo "<input type=\"hidden\" name=\"ad_array[$k]\" value=\"" . $ad_array[$k] . "\">\n";
		echo "</td></tr>\n";
	    }
	    echo "</table><br />\n";
	}
	?>
   			
        <input type="hidden" name="title" value="<?php echo( $auto_discovery_confirm[ 'title' ] ); ?>" />
        <input type="hidden" name="permalink" value="<?php echo( $auto_discovery_confirm[ 'permalink' ] ); ?>" />
        <input type="hidden" name="excerpt" value="<?php echo( $auto_discovery_confirm[ 'excerpt' ] ); ?>" />
	<input type="submit" name="yes" value="<?php echo( $lang_string[ 'ok_btn' ] ); ?>" />
	<input type="submit" name="no" value="<?php echo( $lang_string[ 'cancel_btn' ] ); ?>" />
	</form>
	<?php 
	     }



    }

    ## editing mode

    else{

	require_once('scripts/sb_blogeditor.php');
	editor_add();

    }
}



theme_pagelayout();
html_footer();

?>
