From 2437fb514128f00bf3df6c3539d3ef29c58e94f9 Mon Sep 17 00:00:00 2001 From: Metabox Date: Sat, 3 Aug 2019 07:04:38 +1000 Subject: [PATCH] JSON, RTF dev and various fixes, stage 1. --- dbLifeLog/main.cnf | 1 + htdocs/cgi-bin/json.cgi | 81 +++++++++++++++++++++++++++++------- htdocs/cgi-bin/login_ctr.cgi | 40 ++++++++---------- htdocs/cgi-bin/main.cgi | 10 ++++- htdocs/cgi-bin/wsrc/main.js | 10 +++-- 5 files changed, 97 insertions(+), 45 deletions(-) diff --git a/dbLifeLog/main.cnf b/dbLifeLog/main.cnf index 0894b0c..6bd1047 100644 --- a/dbLifeLog/main.cnf +++ b/dbLifeLog/main.cnf @@ -18,6 +18,7 @@ Credential format:< , must be enabled option. 22|$AUTO_LOGIN = 0`Autologin option, that expires only if login out. Enter Credentials in main.cnf. 24|$FRAME_SIZE = 0`Youtube frame size settings, 0 - Large, 1 - Medium, 2- Small. 26|$RTF_SIZE = 2`RTF Document height, 0 - Large, 1 - Medium, 2- Small. +28|$THEME = Standard`Theme to applay, Standard, Sun, Moon, Earth. < 01|Unspecified `For quick uncategorised entries. 03|File System `Operating file system short log. diff --git a/htdocs/cgi-bin/json.cgi b/htdocs/cgi-bin/json.cgi index 7c77d4f..b33b87b 100755 --- a/htdocs/cgi-bin/json.cgi +++ b/htdocs/cgi-bin/json.cgi @@ -15,12 +15,16 @@ use DBI; use DateTime; use DateTime::Format::SQLite; +use DateTime::Format::Strptime; use DateTime::Duration; use Date::Language; use Date::Parse; use Time::localtime; use Regexp::Common qw /URI/; use JSON; +use IO::Compress::Gzip qw(gzip $GzipError); +use Compress::Zlib; + #DEFAULT SETTINGS HERE! our $REC_LIMIT = 25; @@ -41,26 +45,26 @@ my $cgi = CGI->new; my $session = new CGI::Session( "driver:File", $cgi, { Directory => $LOG_PATH } ); my $sid = $session->id(); -my $dbname = "";#$session->param('database'); +my $dbname = $session->param('database'); my $userid = $session->param('alias'); my $password = $session->param('passw'); my $action = $cgi->param('action'); my $lid = $cgi->param('id'); my $doc = $cgi->param('doc'); +my $error = ""; if ($AUTHORITY) { $userid = $password = $AUTHORITY; $dbname = 'data_' . $userid . '_log.db'; } elsif ( !$userid || !$dbname ) { - # print $cgi->redirect("login_ctr.cgi?CGISESSID=$sid"); - # exit; + print $cgi->redirect("login_ctr.cgi?CGISESSID=$sid"); + exit; } my $database = '../../dbLifeLog/' . $dbname; my $dsn = "DBI:SQLite:dbname=$database"; -my $db; -#$db = DBI->connect( $dsn, $userid, $password, { RaiseError => 1 } ); +my $db = DBI->connect( $dsn, $userid, $password, { RaiseError => 1 } ); ### Authenticate session to alias password #&authenticate; @@ -70,19 +74,24 @@ my $lang = Date::Language->new($LANGUAGE); my $today = DateTime->now; $today->set_time_zone($TIME_ZONE); +my $strp = DateTime::Format::Strptime->new( + pattern => '%F %T', + locale => 'en_AU', + time_zone => $TIME_ZONE, + on_error => 'croak', +); +my ($response, $json) = 'Feature Under Development!'; ############### -#&processSubmit; +&processSubmit; ############### -my $json = JSON->new->utf8->space_after->pretty->allow_blessed->encode - ({date => DateTime::Format::SQLite->format_datetime($today), - response_origin => "LifeLog.".$RELEASE_VER, - response => "Feature Under Development!", - alias => $userid, - log_id => $lid, - received => $doc - }); +if($action eq 'load' && !$error){ + $json = $response; +} +else{ + &defaultJSON; +} print $cgi->header( -expires => "+0s", -charset => "UTF-8" ); @@ -93,14 +102,54 @@ undef($session); exit; +sub defaultJSON{ + $json = JSON->new->utf8->space_after->pretty->allow_blessed->encode + ({date => $strp->format_datetime($today), + response_origin => "LifeLog.".$RELEASE_VER, + response => $response, + alias => $userid, + log_id => $lid, + database=>$database, action => $action, error=>$error + #received => $doc + }); +} + sub processSubmit { # my $date = $cgi->param('date'); - + my $st; + try { + if($action eq 'store'){ + + my $zip = compress($doc,Z_BEST_COMPRESSION); + $st = $db->prepare("SELECT LID FROM NOTES WHERE LID = '$lid';"); + $st -> execute(); + if($st->fetchrow_array() eq undef) { + $st = $db->prepare("INSERT INTO NOTES(LID, DOC) VALUES (?, ?);"); + $st->execute($lid, $zip); + $response = "Stored Document (id:$lid)!"; + } + else{ + $st = $db->prepare("UPDATE NOTES SET DOC = ? WHERE LID = '$lid';"); + $st->execute($zip); + $response = "Updated Document (id:$lid)!"; + } + + } + elsif($action eq 'load'){ + $st = $db->prepare("SELECT DOC FROM NOTES WHERE LID = '$lid';"); + $st -> execute(); + $doc = ($st->fetchrow_array())[0]; + $response = uncompress($doc); + } + else{ + $error = "Your action ($action) sux's a lot!"; + } + } catch { - print "ERROR:" . $_; + $error = ":LID[$lid]-> ".$_; } } diff --git a/htdocs/cgi-bin/login_ctr.cgi b/htdocs/cgi-bin/login_ctr.cgi index 65fb99b..b93920e 100755 --- a/htdocs/cgi-bin/login_ctr.cgi +++ b/htdocs/cgi-bin/login_ctr.cgi @@ -251,40 +251,34 @@ try{ $st = $db->prepare(selSQLTbl('NOTES')); $st->execute(); if(!$st->fetchrow_array()) { -my $stmt = qq( - CREATE VIRTUAL TABLE NOTES USING fts4( - ID INT PRIMARY KEY NOT NULL, - ID_LOG INT, - AUTHOR, - CONTENT TEXT NOT NULL, - compress=zip, uncompress=unzip - ); -); + my $stmt = qq( + CREATE TABLE NOTES (LID PRIMARY KEY NOT NULL, DOC TEXT); + ); $rv = $db->do($stmt); if($rv < 0){print "

Error->"& $DBI::errstri &"

"}; - $st = $db->prepare("SELECT * FROM AUTH WHERE alias='$alias' AND passw='$passw';"); - $st->execute(); - if(!$st->fetchrow_array()) { - $st = $db->prepare('INSERT INTO AUTH VALUES (?,?)'); - $st->execute($alias, $passw); - } } + $st = $db->prepare("SELECT * FROM AUTH WHERE alias='$alias' AND passw='$passw';"); + $st->execute(); + if(!$st->fetchrow_array()) { + $st = $db->prepare('INSERT INTO AUTH VALUES (?,?)'); + $st->execute($alias, $passw); + } $st = $db->prepare(selSQLTbl('CONFIG')); $st->execute(); - if(!$st->fetchrow_array()) { + if(!$st->fetchrow_array()) { #v.1.3 -> v.1.4 #alter table CONFIG add DESCRIPTION VCHAR(128); my $stmt = qq( - CREATE TABLE CONFIG( - ID TINY PRIMARY KEY NOT NULL, - NAME VCHAR(16), - VALUE VCHAR(28), - DESCRIPTION VCHAR(128) - ); - CREATE INDEX idx_config_name ON CONFIG (NAME); + CREATE TABLE CONFIG( + ID TINY PRIMARY KEY NOT NULL, + NAME VCHAR(16), + VALUE VCHAR(28), + DESCRIPTION VCHAR(128) + ); + CREATE INDEX idx_config_name ON CONFIG (NAME); ); $rv = $db->do($stmt); $st->finish(); diff --git a/htdocs/cgi-bin/main.cgi b/htdocs/cgi-bin/main.cgi index 93331d7..e27ac2b 100755 --- a/htdocs/cgi-bin/main.cgi +++ b/htdocs/cgi-bin/main.cgi @@ -35,7 +35,8 @@ our $AUTHORITY = ''; our $IMG_W_H = '210x120'; our $AUTO_WRD_LMT = 1000; our $FRAME_SIZE = 0; -our $RTF_SIZE = 0; +our $RTF_SIZE = 0; +our $THEME = 'Standard'; #END OF SETTINGS my $cgi = CGI->new; @@ -104,13 +105,18 @@ else { #defaults $imgh = 120; } +my $bgcol = '#c8fff8'; +if($THEME eq 'Sun'){ + $bgcol = '#D4AF37'; +} + print $cgi->header( -expires => "0s", -charset => "UTF-8" ); print $cgi->start_html( -title => "Personal Log", - -BGCOLOR => "#c8fff8", + -BGCOLOR => $bgcol, -onload => "loadedBody('" . $toggle . "');", -style => [ { -type => 'text/css', -src => 'wsrc/main.css' }, diff --git a/htdocs/cgi-bin/wsrc/main.js b/htdocs/cgi-bin/wsrc/main.js index 0f8846f..e66abd6 100644 --- a/htdocs/cgi-bin/wsrc/main.js +++ b/htdocs/cgi-bin/wsrc/main.js @@ -226,7 +226,7 @@ function validLog(log) { function setNow() { - var date = $("#frm_entry").date; + var date = document.getElementById("frm_entry").date; var dt = new Date(); var mm = fix0(dt.getMonth() + 1); var dd = fix0(dt.getDate()); @@ -519,11 +519,13 @@ function saveRTF(id, action) { // alert(JSON.stringify(QUILL.getContents())); //Disabled on new log entry. Save and edit, obtains id. For now. @2019-07-20 //if (id > 0) { - $.post('json.cgi?action=' + action + '&id=' + id, { doc: JSON.stringify(QUILL.getContents()) }, saveRTFResult); + $.post('json.cgi', {action:action, id:id, doc: JSON.stringify(QUILL.getContents()) }, saveRTFResult); //} } function saveRTFResult(result) { - alert("Result->" + result); - console.log(result); + //alert("Result->" + result); + console.log("Result->" + result); + var obj = JSON.parse(result); + alert(obj.response); } \ No newline at end of file -- 2.34.1