<?php
/* 	
	Client-side logic for Wordpress CLI theme
   	R. McFarland, 2006
	http://blog.elinc.ca/rodcli/
	
	Sadly there is some browser sniffing that has to be done. Also
	we set the link to the GUI version of the blog, if any. This is why 
	we're serving a Javascript file via PHP.

	TODO: set username if user happens to be logged in or has cookie
*/

require_once('cli-config.inc.php');
$ua=$_SERVER['HTTP_USER_AGENT'];
/* weird hack for IE7, commented out here because it relies on changing the root index.php */
//if(isset($_GET['ua'])){
//        $ua=base64_decode($_GET['ua']);
//}
$isie=(strpos($ua,'MSIE')!==false);
$isopera=(strpos($ua,'Opera')!==false);
$issafari=(strpos($ua,'Safari')!==false);
require('../../../wp-blog-header.php'); 
header("Content-type: text/javascript");
?>
var path="/";
var prompt="$";
var user="guest"; // someday maybe we can change this to currently logged user. Maybe.
var machine="<?php echo strtolower(str_replace(' ','-',get_bloginfo('name'))); ?>";
var display=null;
var cl=null;
var frm=null;
var cmd=null;
var scrn=null;
var pr=null;
function setup(){
	display=document.getElementById('display');
	pr=document.getElementById('prompt');
	cmd=document.getElementById('command');
	scrn=document.getElementById('scr');
	frm=document.forms[0];
	ask=user+'@'+machine+":"+path+prompt+'&nbsp;';
	pr.innerHTML=ask;
}
function dokey(){
	frm.dude.focus();
	<?php if ($isie || $isopera || $issafari){ ?>
	if(event.keyCode == 13){
		docmd(frm.dude.value);
	}
	<?php } ?>
	return false;
}
var cachecmd='';
var specialCommandHandler=false;
function docmd(input){
	if(specialCommandHandler){
		specialCommandHandler(input);
		return false;
	}
	frm.dude.value='';
	cmd.innerHTML="";
	if(
		input.toLowerCase()=='cls' ||
		input.toLowerCase()=='clear'
	){
		display.innerHTML='';
		return false;
	}
	if(input.toLowerCase()=='login'){
		document.location.href="wp-admin/index.php";
		return false;
	}
	if(input.toLowerCase()=='gui'
		||input.toLowerCase()=='startx'){
<?php if (GUI_URL) { ?>
		document.location.href="<?php echo GUI_URL ?>/index.php";
<?php } else { ?>
		alert('No GUI link configured.');
<?php } ?>
		return false;
	}
	dispin=input;
	if(passwd){
                dispin="";
                for(i=0;i < input.length;i++){
                        dispin+='*';
                }
        }
	display.innerHTML+="<pre>"+user+'@'+machine+":"+path+prompt+'&nbsp;'+dispin+'</pre>';
	scroller();

/* comments are a special case */
	if(input.toLowerCase()=='c'||input.toLowerCase()=='comment'){
		comidx=0;
		display.innerHTML+='<p>Leave comment on current post</p><p>Enter / to cancel</p>';
		scroller();
		specialCommandHandler=function(input){
			//grab stuff here
			pr.style.visibility='hidden';
			commentContents[commentPrompts[comidx-1][1]]=input;
			frm.dude.value="";
			display.innerHTML += '<p><pre style=\"display:inline\">'+commentPrompts[comidx-1][0]+input+'</pre></p>';
			scroller();
			if(input=='/'){
				specialCommandHandler=false;
				comidx=0;
				commentContents=new Array();
				setup();
				pr.style.visibility='visible';
				display.innerHTML+='<p>Canceled.</p>';
				scroller();
				return false;
			}
			if(comidx == commentPrompts.length){
				qs='c=comment'
					+'&author='+unfuck(commentContents['name'])	
					+'&email='+unfuck(commentContents['email'])	
					+'&url='+unfuck(commentContents['website'])
					+'&comment='+encodeURI(commentContents['comment']);
				specialCommandHandler=false;
				comidx=0;
				commentContents=new Array();
				setup();
				execComment(qs);
				return false;
			}
			pr.innerHTML=commentPrompts[comidx++][0];
			pr.style.visibility='visible';
			scroller();
		}
		pr.innerHTML=commentPrompts[comidx++][0];
		scroller();
		return false;
	}

	pr.style.visibility='hidden';
	scroller();
	execute(input);
	return false;
}
comidx=0;
var commentContents=new Array();
var commentPrompts=[
	['Name: ','name'],
	['Email: ','email'],
	['Website: ','website'],
	['Comment: ','comment']
];
var passwd=false;

function unfuck(what){
        what=encodeURI(what);
        what=what.replace(/&amp;/g,'%26');
        what=what.replace(/&lt;/g,'<');
	return what;
}
function showtype(what){
	if(passwd){
		cmd.innerHTML="";
		for(i=0;i < what.length;i++){
			cmd.innerHTML+='*';
		}
	}else{
                what=what.replace(/</g,'&lt;');
                what=what.replace(/&$/g,'&amp;');
                frm.dude.value=what;
                cmd.innerHTML=what;
	}
}
function showpost(which){
	frm.dude.value='cat '+which;
	docmd(frm.dude.value);
}
function autosearch(which){
	frm.dude.value='find '+which;
	docmd(frm.dude.value);
}
function showlicense(){
	frm.dude.value='gpl';
	docmd(frm.dude.value);
}
function showcomments(){
	frm.dude.value='comments';
	docmd(frm.dude.value);
}
function showcat(which){
	frm.dude.value='category '+which;
	docmd(frm.dude.value);
}
var xmlhttp = false;
function init(){
        /*@cc_on @*/
        /*@if (@_jscript_version >= 5)
        // JScript gives us Conditional compilation, we can cope with old IE versions.
        // and security blocked creation of the objects.
         try {
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
          try {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (E) {
           xmlhttp = false;
          }
         }
        @end @*/
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {

          xmlhttp = new XMLHttpRequest();
        }
}

function execute(what){
        if(!xmlhttp)init();
        if (!xmlhttp){
                alert('Darn.');
                return false;
        }

        url="<?php bloginfo('stylesheet_directory') ?>/interpret.php?c="+unfuck(what);
        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange=function(){
                xstate=xmlhttp.readyState;
                if (xstate==4 && xmlhttp.status == 200){
                        display.innerHTML+=xmlhttp.responseText;
			scroller();
			pr.style.visibility='visible';
                        return true;
                }
        }
        xmlhttp.send("");
        return false;
}

function execComment(query){
        if(!xmlhttp)init();
        if (!xmlhttp){
                alert('Darn.');
                return false;
        }

        url="<?php bloginfo('stylesheet_directory') ?>/interpret.php?c=comment";
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xmlhttp.onreadystatechange=function(){
                xstate=xmlhttp.readyState;
                if (xstate==4 && xmlhttp.status == 200){
                        display.innerHTML+=xmlhttp.responseText;
			scroller();
			pr.style.visibility='visible';
                        return true;
                }
        }
        xmlhttp.send(query);
        return false;
}


function scroller(){
<?php if(SMOOTH_SCROLL) { ?>
        diff = (scrn.scrollTop + scrn.offsetHeight);
        if((scrn.scrollHeight - diff) > <?php echo SCROLL_STEP ?>){
                scrn.scrollTop += <?php echo SCROLL_STEP ?>;
                setTimeout('scroller();', 10);
        }else{
                scrn.scrollTop = scrn.scrollHeight - scrn.offsetHeight;
        }
<?php }else{ ?>
        scrn.scrollTop = scrn.scrollHeight;
<?php } ?>
}
