]> lifelog.hopto.org Git - LifeLog.git/commitdiff
Initial multi driver support settings, added.
authorWill Budicm <redacted>
Thu, 17 Sep 2020 00:15:28 +0000 (10:15 +1000)
committerWill Budicm <redacted>
Thu, 17 Sep 2020 00:15:28 +0000 (10:15 +1000)
dbLifeLog/main.cnf
htdocs/cgi-bin/config.cgi
htdocs/cgi-bin/data.cgi
htdocs/cgi-bin/login_ctr.cgi
htdocs/cgi-bin/main.cgi

index d5a4b356296ccd198b33c7bd868e1d28cdb79aeb..69d3d28d88424d5c2da4fb1ccd53bb6a0ae4e212 100644 (file)
@@ -3,10 +3,12 @@
 This is the main configuration file for the LifeLog applications settings.
 https://github.com/wbudic/LifeLog
 This is an Open Source License project -> https://choosealicense.com/licenses/isc/
-Credential format:<<AUTO_LOGIN <{alias}/{password}> , dont enable here using AUTO_LOGIN option bellow, use config in app.
+Credential format:<<AUTO_LOGIN <{alias}/{password}> , don't enable here using AUTO_LOGIN option bellow, use config in app.
 <<AUTO_LOGIN</>
 # BACKUP_ENABLED -> Enable (1), disable (0) backups to be restored from config page.
 <<BACKUP_ENABLED<1>
+# Default database driver prefix, is SQLite.
+<<DBI_DRV_PRFIX<DBI:SQLite:dbname=>
 <<CONFIG<4>
 00|$RELEASE_VER = 2.1`LifeLog Application Version.
 01|$REC_LIMIT   = 25`Records shown per page.
@@ -27,6 +29,7 @@ Credential format:<<AUTO_LOGIN <{alias}/{password}> , dont enable here using AUT
 34|$VIEW_ALL_LMT=1000`Limit of all records displayed for large logs. Set to 0, for unlimited.
 36|$TRACK_LOGINS=1`Create system logs on login/logout of Life Log.
 38|$COMPRESS_ENC=0`Compress Encode pages, default -> 0=false, 1=true.
+40|$SUBPAGEDIR  =docs`Directory to scan for subpages like documents.
 <<CAT<3>
 01|Unspecified `For quick uncategorised entries.
 03|File System `Operating file system/Application short log.
index 709e77516947ffe4b6bd5319dfe0547c8c12ae0b..3d3c5527bd20a5c7f80ca277b2f1b157058b5970 100755 (executable)
@@ -20,7 +20,7 @@ use DateTime::Duration;
 use Date::Language;
 use Text::CSV;
 use Scalar::Util qw(looks_like_number);
-use Sys::Syslog qw(:DEFAULT :standard :macros);
+use Sys::Syslog qw(:DEFAULT :standard :macros); #openLog, closelog macros
 
 #DEFAULT SETTINGS HERE!
 use lib "system/modules";
@@ -44,9 +44,7 @@ if(!$userid||!$dbname){
     exit;
 }
 
-my $database = &Settings::logPath.$dbname;
-my $dsn= "DBI:SQLite:dbname=$database";
-my $db = DBI->connect($dsn, $userid, $pass, { RaiseError => 1 }) or die "<p>Error->"& $DBI::errstri &"</p>";
+my $db = Settings::connectDB($userid, $pass);
 
 ### Fetch settings
     Settings::getConfiguration($db);
@@ -600,7 +598,7 @@ if($passch){
         }
     }
 
-    openlog($dsn, 'cons,pid', "user");
+    openlog(Settings::dsn(), 'cons,pid', "user");
         syslog('info', 'Status:%s', $status);
         syslog('info', 'Password change request for %s', $$userid);
     closelog();
@@ -780,7 +778,7 @@ catch{
 
 }
 
-    openlog($dsn, 'cons,pid', "user");
+    openlog(Settings::dsn(), 'cons,pid', "user");
         syslog('info', 'Status:%s', $status);
         syslog('err', '%s', $ERROR) if ($ERROR);
     closelog();
@@ -863,7 +861,7 @@ try{
 
         $db->do('COMMIT;');
         $db->disconnect();
-        $db = DBI->connect($dsn, $userid, $pass, { RaiseError => 1 }) or LifeLogException->throw($DBI::errstri);
+        $db =Settings::connectDB($userid, $pass);
         $dbs = $db->do("VACUUM;");
 
 
@@ -1022,7 +1020,7 @@ try{
 sub backup {
 
    my $ball = 'bck__'.$today->strftime('%Y%m%d%H%M%S_')."$dbname.osz";
-   my $pipe = "tar czf - ".&Settings::logPath.'main.cnf' ." $database | openssl enc -k $pass:$userid -e -des-ede3-cfb -out ".Settings::logPath().$ball." 2>/dev/null";
+   my $pipe = "tar czf - ".Settings::logPath().'main.cnf' ." ". Settings::dbFile()." | openssl enc -k $pass:$userid -e -des-ede3-cfb -out ".Settings::logPath().$ball." 2>/dev/null";
    my $rez = `$pipe`;
 
     #print $cgi->header;
index 1d5a1befdfed65b56d7aa3da46fbe5779fbdc87b..7a52bddc9f27c7ee6fee01ae9c1b31501a3729fc 100755 (executable)
@@ -41,9 +41,8 @@ if(!$userid||!$dbname){
 # $userid  = "admin";
 # $password= "admin";
 
-my $database = Settings::logPath().$dbname;
-my $dsn= "DBI:SQLite:dbname=$database";
-my $db = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })  or LifeLogException->throw($DBI::errstri);
+
+my $db = Settings::connectDB($userid, $password);
 
 #Fetch settings
 my $imgw = 210;
index ef4f4fdfe4ef3b4ff5ef5a5faa87d3b6d17955dc..8bd061d14641d54a66fd50fa6337b948fe6c271a 100755 (executable)
@@ -29,7 +29,7 @@ my $sssCreatedDB = $session->param("cdb");
 my $sid=$session->id();
 my $cookie = $cgi->cookie(CGISESSID => $sid);
 
-
+my $db;
 my $alias = $cgi->param('alias');
 my $passw = $cgi->param('passw');
 my ($debug,$frm) = "";
@@ -127,47 +127,47 @@ sub processSubmit {
     return 0;
 }
 
-sub checkAutologinSet {
+sub parseAutonom { #Parses autonom tag for its crest value, returns undef if tag not found or wrong for passed line.
+    my $t = '<<'.shift.'<';
+    my $line = shift;
+    if(rindex ($line, $t, 0)==0){#@TODO change the following to regex parsing:
+        my $l = length $t;
+        my $e = index $line, ">", $l + 1;
+        return substr $line, $l, $e - $l;
+    }
+    return undef;
+}
 
-    #We don't need to slurp as it is expected setting in header.
-    my (@cre, $end,$crest);
+sub checkAutologinSet {
+    my (@cre, $v);
+    # We don't need to slurp whole file as next are expected settings in begining of the config file.
     open(my $fh, '<', &Settings::logPath.'main.cnf' ) or LifeLogException->throw("Can't open main.cnf: $!");
     while (my $line = <$fh>) {
                 chomp $line;
-                if(rindex ($line, "<<AUTO_LOGIN<", 0)==0){
-                        $end = index $line, ">", 14;
-                        $crest = substr $line, 13, $end - 13;
-                        @cre = split '/', $crest;
-                       next;
-                }
-                elsif(rindex ($line, "<<BACKUP_ENABLED<", 0)==0){
-                        $end = index $line, ">", 18;
-                        $BACKUP_ENABLED = substr $line, 17, $end - 17;
-                    last; #we expect as last anon to be set.
-                }
-                elsif(rindex ($line, "<<CONFIG<",0) == 0){last;}
+                $v = parseAutonom('AUTO_LOGIN',$line);
+                if($v){ @cre = split '/', $v; next}
+                $v = parseAutonom('BACKUP_ENABLED',$line);
+                if($v){ $BACKUP_ENABLED = $v; next}
+                $v = parseAutonom('DBI_DRV_PRFIX',$line);
+                if($v){Settings::DBIPrefix($v); next} 
+                last if parseAutonom('CONFIG',$line); #By specs the config tag, is not an autonom, if found we stop reading. So better be last one spec. in file.
     }
     close $fh;
-    if(@cre &&scalar(@cre)>1){##TODO we already connected here to the db, why do it again later?
+    if(@cre &&scalar(@cre)>1){
 
-            if($alias && $passw && $alias ne $cre[0]){ # Is this an new alias login attempt?
+            if($alias && $passw && $alias ne $cre[0]){ # Is this an new alias login attempt? If so, autologin is of the agenda.
                 return;                                # Note, we do assign entered password even passw as autologin is set. Not entering one bypasses this.
             }                                          # If stricter access is required set it to zero in main.cnf, or disable in config.
             $passw = $cre[1] if (!$passw);
-            my $database = &Settings::logPath.'data_'.$cre[0].'_log.db';
-            my $dsn= "DBI:SQLite:dbname=$database";
-            my $db = DBI->connect($dsn, $cre[0], $cre[1], { RaiseError => 1 })
-                       or LifeLogException->throw("<p>Error->"& $DBI::errstri &"</p>");
-                #check if enabled.
-            my $st = $db->prepare("SELECT VALUE FROM CONFIG WHERE NAME='AUTO_LOGIN';");
-            $st->execute();
-            my @set = $st->fetchrow_array();
-            if(@set && $set[0]=="1"){
+            $db = Settings::connectDB($alias, $passw);            
+            #check if autologin enabled.
+            my $st = Settings::selectRecords($db,"SELECT VALUE FROM CONFIG WHERE NAME='AUTO_LOGIN';");            
+            my @set = $st->fetchrow_array() if $st;
+            if($set[0]=="1"){
                     $alias = $cre[0];
-                    $passw = $passw; #same as entered, by the not knowing to leave it blank.
+                    $passw = $passw; 
                     &Settings::removeOldSessions;
-            }
-            $db->disconnect();
+            }            
     }
 
 }
@@ -176,10 +176,8 @@ sub checkCreateTables {
 
     my $today = DateTime->now;
        $today-> set_time_zone( &Settings::timezone );
-    my $database = &Settings::logPath.'data_'.$alias.'_log.db';
-    my $dsn= "DBI:SQLite:dbname=$database";
-    my $db = DBI->connect($dsn, $alias, $passw, { RaiseError => 1 }) or LifeLogException->throw($DBI::errstri);
     my ($pst, $sql,$rv, $changed) = 0;
+    $db = Settings::connectDB($alias, $passw) if !$db; 
     # We live check database for available tables now only once.
     # If brand new database, this sill returns fine an empty array.
     $pst = Settings::selectRecords($db,"SELECT name FROM sqlite_master WHERE type='table' or type='view';");
index 66825bec9ee19a81e001c9f56693faf455ec3bce..02fa5970fc37f5d26f0ad3ec833835c327341617 100755 (executable)
@@ -43,11 +43,7 @@ if ( !$userid || !$dbname ) {
     print $cgi->redirect("login_ctr.cgi?CGISESSID=$sid");
     exit;
 }
-
-my $database = &Settings::logPath . $dbname;
-my $dsn      = "DBI:SQLite:dbname=$database";
-my $db       = DBI->connect( $dsn, $userid, $password, { PrintError => 0, RaiseError => 1 } )
-                      or LifeLogException->throw("Connection failed [$DBI::errstri]");
+my $db       = Settings::connectDB($userid, $password);
 my ( $imgw, $imgh );
 #Fetch settings
  Settings::getConfiguration($db);
@@ -220,7 +216,7 @@ my $st;
 my $sqlCAT = "SELECT ID, NAME, DESCRIPTION FROM CAT ORDER BY ID;";
 my $sqlVWL = "$stmS STICKY = 1 $stmE";
 
-toBuf (qq(## Using db -> $dsn\n)) if $DEBUG;
+toBuf ("## Using db ->". Settings::dsn(). "\n") if $DEBUG;
 
 $st = $db->prepare($sqlCAT);
 $st->execute() or LifeLogException->throw($DBI::errstri);
@@ -1065,7 +1061,7 @@ try {
                 toBuf $stm if $DEBUG;
                 #
 
-                my $dbUpd = DBI->connect( $dsn, $userid, $password, { RaiseError => 1 } )  or LifeLogException->throw("Execute failed [$DBI::errstri]");
+                my $dbUpd = DBI->connect(Settings::dsn(), $userid, $password, { RaiseError => 1 } )  or LifeLogException->throw("Execute failed [$DBI::errstri]");
                 traceDBExe($stm);
                 return;
             }
@@ -1163,7 +1159,7 @@ my $pwd = `pwd`;
 $pwd =~ s/\s*$//;
 
 my $dbg = qq(--DEBUG OUTPUT--\n
-    DSN:$dsn
+    DSN:) . Settings::dsn(). qq(
     stm:$stm
     \@DB::args:@DB::args
     \$DBI::err:$DBI::errstr