#!/usr/local/bin/php
#
# Scriptname: todo.php
# Description: A simple todo-/tasklist for CLI
# Author: Johan Lundqvist
# Version: 1.0
# License: GPL GNU General Public License
#
# Long Description:
# A menubased tasklistprogram. Stores all tasks "line-wise" in
# todo.data.
#
# Installation:
# Place this script in a directory writeable for all users of "ToDo"
# Check exec-permissions on todo.php. e.g. (chmod a+x todo.php)
# Run: ./todo.php
#
# Customize:
# Change permissions and paths below to fit your needs...
#
#######################################################
<?php
$datadir 
dirname($_SERVER['PHP_SELF']);
$file "todo.data";
$datafile $datadir ."/" .$file;

function 
getInput($length 255) {
        
$fr fopen("php://stdin""r");
        
$input fgets($fr$length);
        
$input rtrim($input);
        
fclose($fr);
        return 
$input;
}


function 
newTask() {
        global 
$datafile;
        echo 
"\nTell me what has to be done? ";
        
$do getInput();
        echo 
"\nPriority (1-3)? ";
        
$prio getInput();
        
$todo "Task: " .$do ." - Prio:" .$prio ."\n";
        
$fp fopen($datafile,"a");
        
fputs($fp$todo);
        
fclose($fp);
        
chmod($datafile,0666);
}

function 
clearTask() {
        global 
$datafile;
        echo 
"\nWhich Task is completed (Line #)? ";
        
$del_line getInput();
        
$lines file($datafile);
        
$tot_lines count($lines);

        if ((
ctype_digit($del_line)) && ($del_line <= $tot_lines)) {
        
unlink($datafile);
        }
        foreach (
$lines as $line_num => $line) {
            
$line_num++;
            
$fp fopen($datafile"a");
            if (
$line_num != $del_line) {
                
fwrite($fp,$line);
            }
            
fclose($fp);
        }

}

function 
viewToDo() {
        global 
$datafile;
        
system("clear");
        echo 
"---=== Todo-List ===---\n-----------------------------------------\n";
        if (
file_exists($datafile)) {
        
$lines file($datafile);
        foreach (
$lines as $line_num => $line) {
        
$line_num++;
        echo 
"#{$line_num}) " htmlspecialchars($line);
        }
        }
        echo 
"\n-----------------------------------------";
}

while(
true) {
viewTodo();
echo 
"\n(1) Add a new task... ";
echo 
"\n(2) Complete/Remove a task.. ";
echo 
"\n(q) Quit! ";
echo 
"\nYour Choice?:  ";
$choice getInput();

switch(
$choice) {
        case 
1:
                
newTask();
                break;
        case 
2:
                
clearTask();
                break;
        case 
3||"q"||"x"||"Q"||"X"||"exit"||"EXIT":
                echo 
"\nBye bye...\n\n";
                echo 
"ToDo-List by Johan Lundqvist\n";
                exit;
        default:
                echo 
"\nWrong choice stupid, try again...\n";
                
sleep(2);
                break;
  }
}
?>