'; header_javascript(); header_css(); echo '
'; } function html_footer() { echo '
'; } function header_javascript() { echo ' '; } function header_css() { echo ' '; } function timer_table() { $header = ''; if(is_logged()) $header = ' • '. $_SESSION['user']['name']; echo '

'. APP_NAME . $header .'

'; $time = 0; if(is_logged()) { $res = query("SELECT * FROM timers WHERE user_id=? ORDER BY id DESC", array($_SESSION['user']['id'])); while(nextResult($res)) { $row = currentResult($res); $time += timer_row($row['id'], $row['start_time'], $row['end_time'], $row['desc'], $row['duplicate_id']); } } else { echo ' '; } echo ' '; timer_total($time); echo '
Started Running Time Description
Register or log in to keep track of your hours!
'; } function timer_row($id = 0, $start_time = 0, $end_time = 0, $desc = '', $duplicate_id = 0) { $done = ($end_time != 0); $end = ($done ? $end_time : time()); $running_time = $end - $start_time; // controls $stop = 'stop'; $restart = $duplicate_id == 0 ? 'restart • ' : ''; $delete = 'delete'; // output the row echo ' '. date("F j, Y, g:i a", $start_time) .' '. round(($end - $start_time) / 3600, 2) .' hours '. parse_repeat_description($desc) .' '. (is_logged() ? (!$done ? $stop : $restart . $delete) : '') .' '; return $running_time; } function timer_total($total_time = 0) { $hours = floor($total_time / 3600); $hours = $hours < 1 ? 0 : $hours; $minutes = floor(($total_time - ($hours * 3600)) / 60); $minutes = $minutes < 1 ? 0 : $minutes; $seconds = $total_time - ($hours * 3600) - ($minutes * 60); echo ' Total: '. $hours .' hours, '. $minutes .' minutes, and '. $seconds .' seconds. '; } function menu() { if(is_logged()) { echo ' '; } else { echo '
'; } } function add_timer_form() { if(is_logged()) { echo '


'; } } function is_logged() { if(isset($_SESSION['user']) && $_SESSION['user']['id'] > 0) return TRUE; return FALSE; } function login($name, $pass) { $res = query("SELECT * FROM users WHERE name=? AND pass=?", array($name, hash_pass($pass))); unset($_SESSION['user']); if(nextResult($res)) { $_SESSION['user'] = currentResult($res); } } function stop_timer($id) { if(is_logged()) { try { query("UPDATE timers SET end_time=? WHERE id=?", array(time(), $id)); } catch(Exception $e) { die($e->getMessage()); } } } function add_timer($description) { if(is_logged()) { try { query("INSERT INTO timers (start_time,end_time,desc,user_id) VALUES (?,?,?,?)", array(time(), 0, parse_description($description), $_SESSION['user']['id'])); } catch(Exception $e) { die($e->getMessage()); } } } function parse_description($str, $undo = FALSE) { $str = trim($str); if(!$undo) { $str = htmlentities($str, ENT_QUOTES, 'UTF-8'); $str = preg_replace("~(\r?\n)~", "
", $str); } else { $str = str_replace('
', "\n", $str); $str = html_entity_decode($str, ENT_QUOTES, 'UTF-8'); } return $str; } function parse_repeat_description($str) { $str = preg_replace("~(\[Continued from([^\]]+))\]~", "Continued from $2", $str); return $str; } function delete_timer($id) { if(is_logged()) { try { query("DELETE FROM timers WHERE id=?", array($id)); } catch(Exception $e) { die($e->getMessage()); } } } // this technically makes a new timer ;) function restart_timer($id = 0) { if(is_logged()) { $result = query("SELECT * FROM timers WHERE end_time=0 AND user_id=?", array($_SESSION['user']['id'])); if(!nextResult($result)) try { //query("UPDATE timers SET end_time=0 WHERE id=?", array($id)); $res = query("SELECT * FROM timers WHERE id=?", array($id)); if(nextResult($res)) { $row = currentResult($res); $description = parse_description($row['desc'], TRUE); add_timer($description . "\n" . '[Continued from '. date("F j, Y, g:i a", $row['end_time']) .']', $row['id']); // update the old one so we can't restart it again :P query("UPDATE timers SET duplicate_id=? WHERE id=?", array(insertId(), $id)); } else die("The timer that you are trying to restart doesn't exist"); } catch(Exception $e) { die($e->getMessage()); } } } // database functions function database($val = FALSE) { static $conn; if($conn === NULL) { if(!file_exists(SQLITE_FILE)) { $fp = fopen(SQLITE_FILE, "w+"); if($fp === FALSE) throw new Exception("Could not create database file."); if($val) fwrite($fp, $val); @chmod(SQLITE_FILE, 0777); } if(!is_readable(SQLITE_FILE) || !is_writable(SQLITE_FILE)) throw new Exception("Database file cannot be read."); $conn = sqlite_open(basename(SQLITE_FILE), 0777, $error); if($conn === FALSE || !is_resource($conn)) throw new Exception("Could not connect to the database [{$error}]"); } return $conn; } function add_values_to_sql($sql, $args) { // there's prolly a better way of doing this but // I'm being lazy and just doing it this way. foreach($args as $key => $val) // if this is not an integer value, put quotes // around it if(!ctype_digit($val)) $args[$key] = "'". sqlite_escape_string($val) ."'"; $sql = str_replace(array('%', '?'), array('%%', '%s'), $sql); $sql = vsprintf($sql, $args); return str_replace('%%', '%', $sql); } function query($stmt = '', $args = array()) { $stmt = add_values_to_sql($stmt, $args); try { $result = sqlite_query(database(), $stmt); } catch(Exception $e) { die($e->getMessage()); } if($result === FALSE || !is_resource($result)) throw new Exception("An error occured while querying the database [". sqlite_error_string(sqlite_last_error(database())) ."]"); return $result; } function nextResult(&$result) { return sqlite_has_more($result); } function currentResult(&$result) { return sqlite_fetch_array($result, SQLITE_ASSOC); } function insertId() { return sqlite_last_insert_rowid(database()); } function hash_pass($str) { return md5($str . md5('abcdefghijklmnopqrstuvwxyz')); } $action = array_keys($_GET); if(isset($action[0])) { switch($action[0]) { case 'login': login($_POST['user_name'], $_POST['password']); break; case 'logout': if(isset($_SESSION['user'])) unset($_SESSION['user']); break; case 'register': $res = query("SELECT * FROM users WHERE name=?", array($_POST['user_name'])); if(nextResult($res)) die("A user with that name already exists. Please try another."); query("INSERT INTO users (name,pass) VALUES (?,?)", array($_POST['user_name'], hash_pass($_POST['password']))); login($_POST['user_name'], $_POST['password']); break; case 'add': add_timer($_POST['description']); break; case 'stop': stop_timer($_GET['stop']); break; case 'delete': delete_timer($_GET['delete']); break; case 'restart': restart_timer($_GET['restart']); break; case 'install': // don't want to allow website people to // drop the database. if(file_exists(SQLITE_FILE)) die("The database already exists. Please remove it manually with FTP or SSL."); try { query("CREATE TABLE timers ( id INTEGER UNSIGNED, start_time INTEGER UNSIGNED NOT NULL DEFAULT 0, end_time INTEGER UNSIGNED NOT NULL DEFAULT 0, desc TEXT, duplicate_id INTEGER UNSIGNED NOT NULL DEFAULT 0, user_id INTEGER UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (id) )", array()); query("CREATE TABLE users ( id INTEGER UNSIGNED, name VARCHAR(30) NOT NULL, pass VARCHAR(32) NOT NULL, PRIMARY KEY (id) )", array()); } catch(Exception $e) { die($e->getMessage()); } break; } header("Location: index.php"); exit; } // do the normal stuff anyway html_header(); menu(); add_timer_form(); timer_table(); html_footer(); ?>