<?php
/*
 *todo.php 
 used to provide a web interface to todo.sh from todotxt.com
 Original code from http://tech.groups.yahoo.com/group/todotxt/message/1320
 posted by joshdcurry
 *
 */

// Change your username here    
    
define("USERNAME""nesman");
    
    
    
$output null;
    
$input $_POST['input'];
    
$runonce true;
    if (
$input == '' ) { $input 'list'; }
    if (isset(
$_GET['mode'])){
        
//do stuff
        
switch ($_GET['mode']) {
            case 
"buy":
            case 
"don":
            case 
"ria":
                
$input "list @".$_GET['mode'];
                break;
            case 
"do":
                
$input "do ".$_GET['num'];
                break;
            case 
"list":
            case 
"print":
                
$input $_GET['mode'];
                break;
            default:
                
$input 'list';
        }    

    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<?php if ( (strtolower($input) != 'list') && (strtolower($input) !=
    
'ls') && (strtolower($input) != 'listall') && (strtolower($input) !=
    
'lsa') && (strtolower($input) != 'listpri') && (strtolower($input) !=
    
'lsp') && (strtolower($input) != '-h') ) { 
        
//echo "<meta http-equiv=\"refresh\" content=\"3\">\n";
    
$runonce false;
           } 
?>
<title><?php echo USERNAME ?> Todo List</title>
<style type="text/css">
#menu li {display:inline;padding:0.2em 1em;}
</style>

</head>
<body onload="document.todo.input.focus()">
<?php
    
if ($input == 'print' ) { 
        if (
$_GET['really'] == 'yes'){
            
print_groceries(); 
        }
        else {
        
confirm_print();
        }
    
$input='list @buy';
    }
?>
<ul id="menu">
<li><a href="todo.php?mode=buy">@buy</a>
<li><a href="todo.php?mode=don">@don</a>
<li><a href="todo.php?mode=ria">@ria</a>
<li><a href="todo.php?mode=list">list items</a>
<li><a href="todo.php?mode=print">print</a>
<li><a href="todo.php?mode="></a>
</ul>
<br>
<form name="todo" action="todo.php" method="post">
<input type="text" size=75 name="input" value="add @buy ">
<input type="submit" value="Submit">
</form>
<hr>
<?php
    
function t_echo($value)
    {
        
$num "";
        
$firstspace strpos($value" ");
        
$length strlen($value);
        if (
$firstspace === false){
            print 
"$value <br>\n";
        }
        else {
            for(
$i=0$i<$firstspace$i++){
                
$num .= $value[$i];
            }
            if (
is_numeric($num)){
            echo 
"<a href=\"todo.php?mode=do&amp;num=$num\">Do $num</a>";
            }
            else {
                echo 
$num;
            }
            for(
$i$i<$length$i++){
                 
$line .= $value[$i];
            }

            print 
"$line <br>\n";
        }

    }
    function 
main_page($input)
    {
    
exec('/home/'.USERNAME.'/bin/todo.sh -p -v -f -d /home/'.USERNAME.'/.todo '.escapeshellcmd($input), $output);
    if (
$input == '-h') { echo "<pre>"; }
    
array_walk($output't_echo');
    if (
$input == '-h') { echo "</pre>"; }
    }
    function 
confirm_print()
    {
        echo 
"Really send grocery list to printer?<br />";
        echo 
'<a href="todo.php?mode=print&amp;really=yes">Print</a> or <a href="todo.php">Cancel</a><br /><br />';
        echo 
"Or, print groceries with this command: <br> <code>todo list @buy | cut -f3- -d\  | lp</code><br><br>";
    }
    function 
print_groceries()
    {
        
exec('/home/'.USERNAME.'/bin/todo.sh -p -v -f -d /home/'.USERNAME.'/.todo list @buy | cut -f3- -d\  | lp');

        echo 
"<meta http-equiv=\"refresh\" content=\"3;url=todo.php\">\n";
        echo 
"Groceries sent to printer...";
        
//exit;
    
}
    
main_page($input);
    if ( 
$runonce == false ){
        
$input 'list';
        echo 
"<br><hr><br>";
        
main_page($input);
    }
    
?>
</body></html>