Browse Source

More Fixes and DB location support

Fixed #33 Fixed #13
causefx 9 years ago
parent
commit
12d6db4116
3 changed files with 213 additions and 112 deletions
  1. 208 105
      index.php
  2. 2 4
      settings.php
  3. 3 3
      user.php

+ 208 - 105
index.php

@@ -1,20 +1,35 @@
 <?php 
-
-date_default_timezone_set('America/Los_Angeles');
-
-$data = false;
-
+//Set some variables
 ini_set("display_errors", 1);
 ini_set("error_reporting", E_ALL | E_STRICT);
-
-function registration_callback($username, $email, $userdir)
-{
+date_default_timezone_set('America/Los_Angeles');
+$data = false;
+$databaseLocation = "databaseLocation.ini.php";
+$needSetup = "Yes";
+$tabSetup = "Yes";	
+$hasOptions = "No";
+$settingsicon = "No";
+$settingsActive = "";
+$action = "";
+$title = "Organizr";
+$topbar = "#eb6363"; 
+$topbartext = "#FFFFFF";
+$bottombar = "#eb6363";
+$sidebar = "#000000";
+$hoverbg = "#eb6363";
+$activetabBG = "#eb6363";
+$activetabicon = "#FFFFFF";
+$activetabtext = "#FFFFFF";
+$inactiveicon = "#FFFFFF";
+$inactivetext = "#FFFFFF";
+
+function registration_callback($username, $email, $userdir){
+    
     global $data;
+    
     $data = array($username, $email, $userdir);
-}
 
-require_once("user.php");
-$USER = new User("registration_callback");
+}
 
 function printArray($arrayName){
     
@@ -25,126 +40,160 @@ function printArray($arrayName){
     endforeach;
     
 }
+
+function write_ini_file($content, $path) { 
+    
+    if (!$handle = fopen($path, 'w')) {
+        
+        return false; 
     
-$dbfile = DATABASE_LOCATION  . constant('User::DATABASE_NAME') . ".db";
+    }
+    
+    $success = fwrite($handle, $content);
+    
+    fclose($handle); 
+    
+    return $success; 
 
-$database = new PDO("sqlite:" . $dbfile);
+}
+                
+if(isset($_POST['action'])) :
 
-$needSetup = "Yes";
+    $action = $_POST['action'];
+    
+endif;
 
-$query = "SELECT * FROM users";
-			
-foreach($database->query($query) as $data) {
+if($action == "createLocation") :
 
-    $needSetup = "No";
+    $databaseData = '; <?php die("Access denied"); ?>' . "\r\n";
 
-}
+    foreach ($_POST as $postName => $postValue) {
+            
+        if($postName !== "action") :
+        
+            if(substr($postValue, -1) == "/") : $postValue = rtrim($postValue, "/"); endif;
+        
+            $databaseData .= $postName . " = \"" . $postValue . "\"\r\n";
+        
+        endif;
+        
+    }
 
-$db = DATABASE_LOCATION  . constant('User::DATABASE_NAME') . ".db";
-$file_db = new PDO("sqlite:" . $db);
-$file_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+    write_ini_file($databaseData, $databaseLocation);
 
-$dbTab = $file_db->query('SELECT name FROM sqlite_master WHERE type="table" AND name="tabs"');
-$dbOptions = $file_db->query('SELECT name FROM sqlite_master WHERE type="table" AND name="options"');
+endif;
 
-$tabSetup = "Yes";	
-$hasOptions = "No";
-$settingsicon = "No";
+if(!file_exists($databaseLocation)) :
 
-foreach($dbTab as $row) :
+    $configReady = "No";
+    $userpic = "";
 
-    if (in_array("tabs", $row)) :
-    
-        $tabSetup = "No";
-    
-    endif;
+else :
 
-endforeach;
+    $configReady = "Yes";
 
-foreach($dbOptions as $row) :
+    require_once("user.php");
 
-    if (in_array("options", $row)) :
-    
-        $hasOptions = "Yes";
+    $USER = new User("registration_callback");
+
+    $dbfile = DATABASE_LOCATION  . constant('User::DATABASE_NAME') . ".db";
+
+    $database = new PDO("sqlite:" . $dbfile);
+
+    $query = "SELECT * FROM users";
+
+    foreach($database->query($query) as $data) {
+
+        $needSetup = "No";
+
+    }
+
+    $db = DATABASE_LOCATION  . constant('User::DATABASE_NAME') . ".db";
+    $file_db = new PDO("sqlite:" . $db);
+    $file_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+    $dbTab = $file_db->query('SELECT name FROM sqlite_master WHERE type="table" AND name="tabs"');
+    $dbOptions = $file_db->query('SELECT name FROM sqlite_master WHERE type="table" AND name="options"');
+
+    foreach($dbTab as $row) :
+
+        if (in_array("tabs", $row)) :
+
+            $tabSetup = "No";
+
+        endif;
+
+    endforeach;
+
+    if($tabSetup == "Yes") :
+
+        $settingsActive = "active";
     
     endif;
 
-endforeach;
+    foreach($dbOptions as $row) :
 
-if($tabSetup == "No") :
+        if (in_array("options", $row)) :
 
-    if($USER->authenticated && $USER->role == "admin") :
+            $hasOptions = "Yes";
 
-        $result = $file_db->query('SELECT * FROM tabs WHERE active = "true"');
-        $getsettings = $file_db->query('SELECT * FROM tabs WHERE active = "true"');
+        endif;
 
-        foreach($getsettings as $row) :
+    endforeach;
 
-            if(!empty($row['iconurl']) && $settingsicon == "No") :
+    if($tabSetup == "No") :
 
-                $settingsicon = "Yes";
+        if($USER->authenticated && $USER->role == "admin") :
 
-            endif;
+            $result = $file_db->query('SELECT * FROM tabs WHERE active = "true"');
+            $getsettings = $file_db->query('SELECT * FROM tabs WHERE active = "true"');
 
-        endforeach;
+            foreach($getsettings as $row) :
 
-    elseif($USER->authenticated && $USER->role == "user") :
+                if(!empty($row['iconurl']) && $settingsicon == "No") :
 
-        $result = $file_db->query('SELECT * FROM tabs WHERE active = "true" AND user = "true"');
+                    $settingsicon = "Yes";
 
-    else :
+                endif;
 
-        $result = $file_db->query('SELECT * FROM tabs WHERE active = "true" AND guest = "true"');
+            endforeach;
 
-    endif;
-    
-endif;
+        elseif($USER->authenticated && $USER->role == "user") :
 
-$settingsActive = "";
+            $result = $file_db->query('SELECT * FROM tabs WHERE active = "true" AND user = "true"');
 
-if($tabSetup == "Yes") :
+        else :
 
-    $settingsActive = "active";
-    
-endif;
+            $result = $file_db->query('SELECT * FROM tabs WHERE active = "true" AND guest = "true"');
 
-if($hasOptions == "Yes") :
+        endif;
 
-    $resulto = $file_db->query('SELECT * FROM options');
+    endif;
 
-    foreach($resulto as $row) : 
-                                    
-        $title = $row['title'];
-        $topbartext = $row['topbartext'];
-        $topbar = $row['topbar'];
-        $bottombar = $row['bottombar'];
-        $sidebar = $row['sidebar'];
-        $hoverbg = $row['hoverbg'];
-        $activetabBG = $row['activetabBG'];
-        $activetabicon = $row['activetabicon'];
-        $activetabtext = $row['activetabtext'];
-        $inactiveicon = $row['inactiveicon'];
-        $inactivetext = $row['inactivetext'];
+    if($hasOptions == "Yes") :
 
-    endforeach;
+        $resulto = $file_db->query('SELECT * FROM options');
 
-elseif($hasOptions == "No") :
+        foreach($resulto as $row) : 
 
-    $title = "Organizr";
-    $topbar = "#eb6363"; 
-    $topbartext = "#FFFFFF";
-    $bottombar = "#eb6363";
-    $sidebar = "#000000";
-    $hoverbg = "#eb6363";
-    $activetabBG = "#eb6363";
-    $activetabicon = "#FFFFFF";
-    $activetabtext = "#FFFFFF";
-    $inactiveicon = "#FFFFFF";
-    $inactivetext = "#FFFFFF";
+            $title = $row['title'];
+            $topbartext = $row['topbartext'];
+            $topbar = $row['topbar'];
+            $bottombar = $row['bottombar'];
+            $sidebar = $row['sidebar'];
+            $hoverbg = $row['hoverbg'];
+            $activetabBG = $row['activetabBG'];
+            $activetabicon = $row['activetabicon'];
+            $activetabtext = $row['activetabtext'];
+            $inactiveicon = $row['inactiveicon'];
+            $inactivetext = $row['inactivetext'];
 
-endif;
+        endforeach;
+
+    endif;
 
-$userpic = md5( strtolower( trim( $USER->email ) ) );
+    $userpic = md5( strtolower( trim( $USER->email ) ) );
+
+endif;
 
 ?>
 
@@ -399,7 +448,7 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
                                 
                                 <?php endforeach; endif;?>
                                 
-                                <?php if($USER->authenticated && $USER->role == "admin") :?>
+                                <?php if($configReady == "Yes") : if($USER->authenticated && $USER->role == "admin") :?>
                                 <li class="tab-item <?=$settingsActive;?>" id="settings.phpx">
                                                             
                                     <a class="tab-link">
@@ -420,7 +469,7 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
                                     </a>
                                 
                                 </li>
-                                <?php endif;?>
+                                <?php endif; endif;?>
                                 
                                 <!--End Tab List-->
                            
@@ -451,17 +500,17 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
                         
                         <li class="dropdown notifications">
                             
-                            <?php if(!$USER->authenticated) : ?>
+                            <?php if($configReady == "Yes") : if(!$USER->authenticated) : ?>
                             
                             <a class="log-in">
                             
-                            <?php endif; ?>
+                            <?php endif; endif;?>
                             
-                            <?php if($USER->authenticated) : ?>
+                            <?php if($configReady == "Yes") : if($USER->authenticated) : ?>
                             
                             <a class="show-members">
                                 
-                            <?php endif; ?>
+                            <?php endif; endif;?>
                                 
                                 <i class="userpic"><img src="https://www.gravatar.com/avatar/<?=$userpic;?>?s=40&d=mm" class="img-circle"></i> 
                                 
@@ -498,7 +547,7 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
             <div id="content" class="content" style="">
 
                 <!--Load Framed Content-->
-                <?php if($needSetup == "Yes") : ?>
+                <?php if($needSetup == "Yes" && $configReady == "Yes") : ?>
                 <div class="table-wrapper">
 
                     <div class="table-row">
@@ -564,7 +613,61 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
 
                 </div>
                 <?php endif; ?>
-                <?php if(!$USER->authenticated && $tabSetup == "Yes" && $needSetup == "No") :?>
+                
+                
+                <?php if($needSetup == "Yes" && $configReady == "No") : ?>
+                <div class="table-wrapper">
+
+                    <div class="table-row">
+
+                        <div class="table-cell text-center">
+
+                            <div class="login i-block">
+
+                                <div class="content-box">
+
+                                    <div class="green-bg biggest-box">
+
+                                        <h1 class="zero-m text-uppercase">Database Path</h1>
+
+                                    </div>
+
+                                    <div class="big-box text-left registration-form">
+
+                                        <h3 class="text-center">Specify the location of which you want to save your database files.</h3>
+                                        <h5 class="text-left"><strong>Current Direcotry: <?php echo __DIR__; ?> <br>Parent Directory: <?php echo dirname(__DIR__); ?></strong></h5>
+                                        
+                                        <form class="controlbox" name="setupDatabase" id="setupDatabase" action="" method="POST" data-smk-icon="glyphicon-remove-sign">
+                                            
+                                            <input type="hidden" name="action" value="createLocation" />
+
+                                            <div class="form-group">
+
+                                                <input type="text" class="form-control material" name="databaseLocation" autofocus value="<?php echo dirname(__DIR__);?>" autocorrect="off" autocapitalize="off" required>
+                                                
+                                                <?php if(file_exists(dirname(__DIR__) . '/users.db') || file_exists(__DIR__ . '/users.db')) : echo '<h5 class="text-center red">Don\'t worry, you\'re database is still there.  Just use the same location you have it in.</h5>'; endif;?>
+
+                                            </div>
+
+                                            <input id="databaseLocationSubmit" type="submit" class="btn green-bg btn-block btn-sm text-uppercase waves waves-effect waves-float" value="Save Location">
+
+                                        </form>
+
+                                    </div>
+
+                                </div>
+
+                            </div>
+
+                        </div>
+
+                    </div>
+
+                </div>
+                <?php endif; ?>
+                
+                
+                <?php if($configReady == "Yes") : if(!$USER->authenticated && $tabSetup == "Yes" && $needSetup == "No") :?>
                 <div class="table-wrapper">
                 
                     <div class="table-row">
@@ -598,7 +701,7 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
                     </div>
                 
                 </div>
-                <?php endif; ?>
+                <?php endif; endif; ?>
                 <?php if($tabSetup == "No" && $needSetup == "No") :?>        
                 <div id="tabEmpty" class="table-wrapper" style="display: none">
                 
@@ -653,7 +756,7 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
                 
                 <div class="clearfix"><br/></div>
                 
-                <?php if($USER->authenticated) : ?>
+                <?php if($configReady == "Yes") : if($USER->authenticated) : ?>
                 
                 <div class="content-box profile-sidebar box-shadow">
                 
@@ -725,12 +828,12 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
                     
                 </div>
 
-                <?php endif;?>
+                <?php endif; endif;?>
 
             </div>
 
         </div>
-        <?php if(!$USER->authenticated) : ?>
+        <?php if($configReady == "Yes") : if(!$USER->authenticated && $configReady == "Yes") : ?>
         <div class="login-modal modal fade">
             
             <div style="background:<?=$sidebar;?>;" class="table-wrapper">
@@ -826,8 +929,8 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
             </div>
         
         </div>
-        <?php endif;?>
-        <?php if($USER->authenticated) : ?>
+        <?php endif; endif;?>
+        <?php if($configReady == "Yes") : if($USER->authenticated) : ?>
         <div style="background:<?=$topbar;?>;" class="logout-modal modal fade">
             
             <div class="table-wrapper" style="background: <?=$topbar;?>">
@@ -869,7 +972,7 @@ $userpic = md5( strtolower( trim( $USER->email ) ) );
             </div>
     
         </div>
-        <?php endif;?>
+        <?php endif; endif;?>
 
         <!--Scripts-->
         <script src="bower_components/jquery/dist/jquery.min.js"></script>

+ 2 - 4
settings.php

@@ -438,8 +438,6 @@ if($action == "addTabz") :
         $stmt->execute();
         
     endforeach;
-
-    //echo "<script>window.onload = function() {if(!window.location.hash) {window.location = window.location + '#loaded';window.location.reload();}}</script>";
     
 endif;
 
@@ -1679,7 +1677,7 @@ endif;
                 dataType: "json",
                 success: function(github) {
                    
-                    var currentVersion = "0.98";
+                    var currentVersion = "0.985";
                     var githubVersion = github.tag_name;
                     var githubDescription = github.body;
                     var githubName = github.name;
@@ -1724,7 +1722,7 @@ endif;
                     
                     }
 
-                    $(infoTabVersion).html("<strong>Installed Version: </strong>" + currentVersion + " <strong>Current Version: </strong>" + githubVersion);
+                    $(infoTabVersion).html("<strong>Installed Version: </strong>" + currentVersion + " <strong>Current Version: </strong>" + githubVersion + " <strong>Database Location:  </strong> <?php echo DATABASE_LOCATION;?>");
                     
                 }
                 

+ 3 - 3
user.php

@@ -7,9 +7,9 @@
 	 *	entry is assigned a new random token,  which is used in
 	 * salting subsequent password checks.
 	 */
-
-    define('USER_HOME', dirname(__DIR__) . '/users/');
-    define('DATABASE_LOCATION', dirname(__DIR__) . '/');
+    $databaseConfig = parse_ini_file('databaseLocation.ini.php', true);
+    define('USER_HOME', $databaseConfig['databaseLocation'] . '/users/');
+    define('DATABASE_LOCATION', $databaseConfig['databaseLocation'] . '/');
 
 	class User
 	{