Skip to content

Commit

Permalink
BRANCHING FROM ORIGINAL jagenjo/litefilesystem
Browse files Browse the repository at this point in the history
Mods to solve:
- jagenjo#12
- jagenjo#7

Added debug calls, modified default TIMESTAMP column creation, few other things
  • Loading branch information
atlasan committed Dec 8, 2020
1 parent 88c3e8b commit fe6e5e2
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ html,body {

#forgotpassword-dialog {
width: 300px;
background-color: white;
/*background-color: white;*/
}

.login-dialog input.form-control, .login-dialog .btn-lg {
Expand Down
10 changes: 7 additions & 3 deletions src/include/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,24 @@ function loadModules($str)
return $result;
}

//returns false if any of the events returned false, otherwise true
//returns false if any of the events returned false, otherwise return an array of returns, if it called only one module keep the array simple
function dispatchEventToModules($event_type, &$data )
{
$modules = loadModules("*");
$result = true;
$result = array();
foreach($modules as $module)
{
if( !method_exists($module, $event_type) )
continue;

$r = call_user_func_array( array($module , $event_type), array(&$data));
if( $r === false)
if($r === false){
$result = false;
}elseif($result!==false){
$result[] = $r;
}
}
if ($result && count($result)==1) $result = $result[0];
return $result;
}

Expand Down
43 changes: 23 additions & 20 deletions src/include/modules/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FilesModule

//called always
function __construct() {
$_REQUEST["folder"] = $_REQUEST["folder"] == "" ? "." : $_REQUEST["folder"];
if (isset($_REQUEST["folder"])) $_REQUEST["folder"] = $_REQUEST["folder"] == "" ? "." : $_REQUEST["folder"];
}

public function processAction($action)
Expand Down Expand Up @@ -2575,16 +2575,18 @@ public function createUnit( $user_id, $unit_name, $size, $desc_name = "", $chang

$database = getSQLDB();
$result = $database->query( $query );
if(!$result)
if(!$result){
debug("error in INSERT unit: ".$database->error,"red");
return null;
}

$unit_id = -1;
if ($database->insert_id != 0)
$unit_id = $database->insert_id;

if ($unit_id == -1)
{
debug("error inserting in the db a unit");
debug("error inserting in the db a unit","red");
$this->last_error = "DB PROBLEM";
return null;
}
Expand Down Expand Up @@ -2624,12 +2626,12 @@ public function deleteUnit($id, $change_user_quota = false)
$result = $database->query( $query );
if(!$result)
{
debug("error deleting");
debug("error deleting","red");
return false;
}
if($database->affected_rows == 0)
{
debug("weird deleting");
debug("weird deleting","red");
return false;
}

Expand All @@ -2639,7 +2641,7 @@ public function deleteUnit($id, $change_user_quota = false)
$result = $database->query( $query );
if(!$result)
{
debug("error deleting");
debug("error deleting","red");
return false;
}

Expand Down Expand Up @@ -2757,7 +2759,7 @@ public function setPrivileges($unit_id, $user_id, $mode = NULL)
$id = $database->insert_id;
if ($id == -1)
{
debug("error inserting privileges in the db");
debug("error inserting privileges in the db","red");
$this->last_error = "DB PROBLEM";
return false;
}
Expand Down Expand Up @@ -2912,7 +2914,7 @@ public function storeFile( $user_id, $unit_id, $folder, $filename, $fileData, $c
//SAFETY FIRST
if(!$this->validateFilename( $filename) || strpos($folder,"..") != FALSE)
{
debug("Filename contains invalid characters");
debug("Filename contains invalid characters","red");
$this->last_error = "Invalid filename";
return null;
}
Expand All @@ -2934,7 +2936,7 @@ public function storeFile( $user_id, $unit_id, $folder, $filename, $fileData, $c
$unit = $this->getUnit( $unit_id ); // $user_id) //this functions doesnt control privileges
if(!$unit)
{
debug("ERROR: Unit not found: " . $unit_id);
debug("ERROR: Unit not found: " . $unit_id,"red");
return null;
}

Expand All @@ -2949,7 +2951,7 @@ public function storeFile( $user_id, $unit_id, $folder, $filename, $fileData, $c
$file_info = $this->getFileInfoByFullpath($fullpath);
if(!$file_info)
{
debug("something weird happened");
debug("something weird happened","red");
$this->last_error = "ERROR: file found but no SQL entry";
return null;
}
Expand All @@ -2960,7 +2962,7 @@ public function storeFile( $user_id, $unit_id, $folder, $filename, $fileData, $c
//WARNING!!! WHAT ABOUT THE QUOTA, IT WILL BE APPLYED TO HIM INSTEAD OF THE AUTHOR
if( !$unit->mode || $unit->mode == "" || $unit->mode == "READ")
{
debug("user modifying file that doesnt belongs to him");
debug("user modifying file that doesnt belongs to him","red");
$this->last_error = "File belongs to other user";
return null;
}
Expand Down Expand Up @@ -2989,7 +2991,7 @@ public function storeFile( $user_id, $unit_id, $folder, $filename, $fileData, $c
$id = $database->insert_id;
if ($id == -1)
{
debug("error inserting in the db");
debug("error inserting in the db","red");
$this->last_error = "DB PROBLEM";
return null;
}
Expand All @@ -2999,7 +3001,7 @@ public function storeFile( $user_id, $unit_id, $folder, $filename, $fileData, $c
$this->createFolder( $unit->name . "/" . $folder );
if( !$this->folderExist( $unit->name . "/" . $folder ) )
{
debug("wrong folder name");
debug("wrong folder name","red");
$this->last_error = "Error in Folder name";
return null;
}
Expand All @@ -3019,7 +3021,8 @@ public function storeFile( $user_id, $unit_id, $folder, $filename, $fileData, $c

if( $created == false )
{
debug( "file size is 0 after trying to write it to HD: " . $fullpath );
debug( "file size is 0 after trying to write it to HD: " . $fullpath ,"red");
debug( "file size is 0 after trying to write it to HD: " . $fullpath ,"red");
$this->last_error = "PROBLEM WRITTING FILE";
$query = "DELETE FROM `".DB_PREFIX."files` WHERE 'id' = " . $id;
$result = $database->query( $query );
Expand Down Expand Up @@ -3060,7 +3063,7 @@ public function updateFile( $file_id, $fileData )
//save file in hard drive
if( !self::writeFile( $fullpath, $fileData ) )
{
debug("file couldnt be written");
debug("file couldnt be written","red");
$this->last_error = "PROBLEM WRITTING FILE";
return false;
}
Expand Down Expand Up @@ -3103,7 +3106,7 @@ public function updateFilePart( $file_id, $fileData, $offset )
//save file in hard drive
if( !self::writeFilePart( $fullpath, $fileData, $offset ) )
{
debug("file couldnt be written");
debug("file couldnt be written","red");
$this->last_error = "PROBLEM WRITTING FILE";
return false;
}
Expand Down Expand Up @@ -3314,7 +3317,7 @@ public function generateFilePreview( $file_id )

if( !$result )
{
debug("Error saving generated preview: " . $tn_path );
debug("Error saving generated preview: " . $tn_path ,"red");
return false;
}

Expand Down Expand Up @@ -3793,7 +3796,7 @@ public function createTables()
`metadata` TEXT NOT NULL,
`used_size` INT NOT NULL,
`total_size` INT NOT NULL,
`timestamp` TIMESTAMP NOT NULL)
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";

$result = $database->query( $query );
Expand All @@ -3812,7 +3815,7 @@ public function createTables()
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`unit_id` INT NOT NULL,
`timestamp` TIMESTAMP NOT NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`mode` ENUM('READ','WRITE','ADMIN') NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";

Expand All @@ -3837,7 +3840,7 @@ public function createTables()
`metadata` TEXT NOT NULL,
`author_id` INT NOT NULL,
`size` INT NOT NULL,
`timestamp` TIMESTAMP NOT NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` ENUM('DRAFT','PRIVATE','PUBLIC','BLOCKED') NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";

Expand Down
16 changes: 9 additions & 7 deletions src/include/modules/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,14 @@ public function actionCheckReady()
public function checkReady()
{
//check global info
$owner = posix_getgrgid( filegroup( __FILE__ ) );
if($owner)
{
if($owner["name"] != "www-data")
if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN'){
$owner = posix_getgrgid( filegroup( __FILE__ ) );
if($owner)
{
debug("The group of this script is not 'www-data', this could be a problem. Ensure that all files inside this folder belong to the group 'www-data' by running this command from inside the folder: su chown -R :www-data *");
if($owner["name"] != "www-data")
{
debug("The group of this script is not 'www-data', this could be a problem. Ensure that all files inside this folder belong to the group 'www-data' by running this command from inside the folder: su chown -R :www-data *");
}
}
}

Expand All @@ -246,11 +248,11 @@ public function checkReady()
public function restartSystem()
{
$tmp = Array();
debug("Restarting system" );
debug("Resetting system" );
dispatchEventToModules("preRestart",$tmp); //remove all
dispatchEventToModules("restart",$tmp); //create tables and folders
dispatchEventToModules("postRestart",$tmp); //fill stuff
debug("System restarted" );
debug("System reset done" );
return true;
}

Expand Down
27 changes: 20 additions & 7 deletions src/include/modules/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,10 @@ public function loginUser( $username, $password )
$query = "SELECT * FROM `".DB_PREFIX."users` ".$userquery." ".$passquery." LIMIT 1";
$result = $database->query( $query );

if ($result === false)
if ($result === false){
debug("err on login query: ".$database->error);
return null;
}

$user = $result->fetch_object();
if(!$user)
Expand All @@ -899,6 +901,10 @@ public function loginUser( $username, $password )
$token = md5( $user->username . time() . GLOBAL_PASS_SALT . rand() );
$query = "INSERT INTO `".DB_PREFIX."sessions` (`id` , `user_id` , `token`) VALUES ( NULL , ". intval($user->id).", '".$token."')";
$result = $database->query( $query );
if($result === false){
debug("error creating userkey: ".$database->error,"red");
return null;
}
if ($database->insert_id == 0)
{
debug("cannot insert session");
Expand Down Expand Up @@ -1164,10 +1170,11 @@ public function createUser( $username, $password, $email, $roles = "", $data = "
$result = dispatchEventToModules("onUserCreated",$user);

//something went wrong creating the user
if ( $result == false )
if ( $result === false || (is_array($result) && isset($result["status"]) && $result["status"]==-1) )
{
if (isset($result["msg"])) debug("something went wrong creating the user: ".$result["msg"]);
$this->deleteUser( $user );
return false;
return false;
}

return $id;
Expand Down Expand Up @@ -1474,7 +1481,7 @@ public function createTables()
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) NOT NULL,
`token` varchar(255) NOT NULL,
`timestamp` TIMESTAMP NOT NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";

Expand All @@ -1494,7 +1501,7 @@ public function createTables()
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) NOT NULL,
`token` varchar(255) NOT NULL,
`timestamp` TIMESTAMP NOT NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";

Expand All @@ -1521,7 +1528,8 @@ public function postRestart()
{
$this->result["msg"] = "Admin user not created";
$this->result["status"] = -1;
return;
debug($this->result["msg"],"red");
return $this->result;
}

//create public unit
Expand All @@ -1531,8 +1539,13 @@ public function postRestart()
{
$this->result["msg"] = "Guest user not created";
$this->result["status"] = -1;
return;
debug($this->result["msg"],"red");
return $this->result;
}

$this->result["msg"] = "Default users created";
$this->result["status"] = 1;
return $this->result;
}

//used to upgrade tables and so
Expand Down
18 changes: 14 additions & 4 deletions src/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,20 @@ function read_input( $msg )
}

//test folder owner
$owner = posix_getgrgid( filegroup( __FILE__) );
if($owner && $owner["name"] != "www-data")
{
showMessage("The group of this script is not 'www-data', this could be a problem. Ensure that all files inside this folder belong to the www-data by running this command from inside the folder: su chown -R :www-data *","danger");
// XXX: could check for write permission instead, or additionally
if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN'){
$owner = posix_getgrgid( filegroup( __FILE__) );
if($owner && $owner["name"] != "www-data")
{
showMessage("The group of this script is not 'www-data', this could be a problem. Ensure that all files inside this folder belong to the www-data by running this command from inside the folder: su chown -R :www-data *","danger");
}
}

// check if the admin user has been created (when getting errors on first lines, it will stop)
$users = getModule("user");
if(!$users->getUserByName("admin")){
showMessage("No admin found, launch with force option to wipe all data and begin fresh.","warning");
$is_ready = false;
}

if( $is_ready && !$force )
Expand Down
2 changes: 1 addition & 1 deletion src/js/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function systemReady()
$(this).css("opacity",0.5);
login_button.start();
var values = getFormValues(this);
console.log(values);
//console.log(values); // please dont

e.preventDefault();

Expand Down

0 comments on commit fe6e5e2

Please sign in to comment.