if(!fileexists('whenob_db')) { $db = new SQLiteDatabase('whenob_db'); $db->exec("create table whenob_list(title text)"); $db->exec("create table whenob_item(list_id integer,title text,content text,index integer)"); } $db = new SQLiteDatabase('whenob_db'); switch($_GET['action']) { case 'crt_list': create_list();exit; case 'ret_list': retrieve_list();exit; case 'upd_list': update_list();exit; case 'del_list': delete_list();exit; case 'crt_item': create_item();exit; case 'ret_item': retrieve_item();exit; case 'upd_item': update_item();exit; case 'del_item': delete_item();exit; default: } /************************************************ send_error ************************************************/ function send_error($err_msg = 'An error occured') { echo '{error:"'.$err_msg.'"}'; } /************************************************ send_success ************************************************/ function send_success($suc_msg = 'true') { echo '{success:"'.$err_msg.'"}'; } /************************************************ list_exists ************************************************/ function list_exists($list_id) { global $db; if(!is_numeric($list_id)) { send_error('invalid list id'); return false; } $result = $db->arrayQuery("select * from whenob_list where OID=$list_id"); if(count($result) != 1) { send_error('unexisting list'); return false; } return true; } /************************************************ item_exists ************************************************/ function item_exists($item_id) { global $db; if(!is_numeric($item_id)) { send_error('invalid item id'); return false; } $result = $db->arrayQuery("select * from whenob_item where OID=$item_id"); if(count($result) != 1) { send_error('unexisting item'); return false; } return true; } /************************************************ create_list ************************************************/ function create_list() { global $db; $list_title = sqlite_escape_string($_POST['title']); // check if no list have the same title $result = $db->arrayQuery("select * from whenob_list where title='$list_title'"); if(count($result) != 0) { send_error('list name already in use'); } else { if($db->exec("insert into whenob_list (title) values ('$list_title')")) { $result = $db->arrayQuery("select OID from whenob_list where title='$list_title'"); send_success($result[0]['OID']); } else { send_error('an error occured while creating list'); } } } /************************************************ retrieve_list ************************************************/ function retrieve_list() { global $db; $list_id = $_GET['list']; if(!list_exists($list_id)) return; $result = $db->arrayQuery("select OID, title, content from whenob_item where list_id=$list_id order by index"); echo '{'; foreach($result as $item) { echo $item['OID'].':{'; echo 'title:"'.$item['title'].'",'; echo 'content:"'.$item['content'].'"},'; } echo '}'; } /************************************************ update_list ************************************************/ function update_list() { global $db; $list_id = $_GET['list']; $list_title = $_GET['title']; // check if the list exists if(!list_exists($list_id)) return; // check if no list have the same title $result = $db->arrayQuery("select * from whenob_list where title='$list_title'"); if(count($result) != 0) { echo send_error('list name already in use'); return; } if($db->exec("update whenob_list set title='$list_title' where OID=$list_id")) { send_success(); } else { send_error('an error occured while updating list name'); } } /************************************************ delete_list ************************************************/ function delete_list() { global $db; $list_id = $_GET['list']; if(!list_exists($list_id)) return; if($db->exec("delete from whenob_list where OID=$list_id")) { send_success(); } else { send_error('an error occured while deleting list'); } } /************************************************ create_item ************************************************/ function create_item() { global $db; $list_id = $_GET['list']; $item_title = sqlite_escape_string($_GET['title']); $item_content = sqlite_escape_string($_GET['content']); if(!list_exists($list_id)) return; if($db->exec("insert into whenob_item (list_id,title,content,index)" ."(select $list_id,'$item_title','$item_content',max(index)+1 from whenob_item where list_id=$list_id)")) { $result = $db->arrayQuery("select OID from whenob_item where list_id=$list_id and title='$item_title' and content='$item_content'"); send_success($result[0]['OID']); } else { send_error('an error occured while creating item'); } } /************************************************ retrieve_item ************************************************/ function retrieve_item() { global $db; $item_id = $_GET['item']; if(is_numeric($item_id)) { $result = $db->arrayQuery("select OID, title, content from whenob_item where OID=$item_id"); if(count($result) == 0) { send_error('no matching item'); } else { echo '{title:"'.$result[0]['title'].'",content:"'.$result[0]['content'].'"}'; } } else { send_error('invalid item id'); } } /************************************************ update_item ************************************************/ function update_item() { global $db; $item_id = $_GET['item']; $item_title = sqlite_escape_string($_GET['title']); $item_content = sqlite_escape_string($_GET['content']); if(!item_exists($item_id)) return; if($db->exec("update whenob_item set title='$item_title', content='$item_content' where OID=$item_id")) { send_success(); } else { send_error('an error occured while updating item'); } } /************************************************ delete_item ************************************************/ function delete_item() { global $db; $item_id = $_GET['item']; if(!item_exists($item_id)) return; if($db->exec("delete from whenob_item where OID=$item_id")) { send_success(); } else { send_error('an error occured while deleting item'); } } ?>