initial commit file manager project
This commit is contained in:
118
WebService/lib/Authenticator.php
Normal file
118
WebService/lib/Authenticator.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
class Authenticator{
|
||||
|
||||
private $dbDN = "sqlite:auth.db";
|
||||
private $tokenValidity = 600;
|
||||
|
||||
private function createInitalDB(){
|
||||
$db = new PDO($this->dbDN);
|
||||
$db->exec("CREATE TABLE users (user TEXT, pass TEXT)");
|
||||
$db->exec("INSERT INTO users VALUES('admin','".md5("admin")."')");
|
||||
$db->exec("CREATE TABLE tokens (username TEXT, token TEXT, validTo INT)");
|
||||
unset($db);
|
||||
}
|
||||
|
||||
private function createToken($username){
|
||||
|
||||
$token = md5($username.time());
|
||||
$validTo = time()+$this->tokenValidity;
|
||||
|
||||
$db = new PDO($this->dbDN);
|
||||
$stmt = $db->prepare("INSERT INTO tokens VALUES (:USER, :TOKEN, :VALIDTO)");
|
||||
$result = $stmt->execute(array(
|
||||
":USER" => $username,
|
||||
":TOKEN" => $token,
|
||||
":VALIDTO" => $validTo
|
||||
));
|
||||
unset($db);
|
||||
if($result){
|
||||
return $token;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function createUser($username,$password){
|
||||
$db = new PDO($this->dbDN);
|
||||
$stmt = $db->prepare("INSERT INTO users VALUES(:USER,:PASS)");
|
||||
$result = $stmt->execute(array(
|
||||
":USER" => $username,
|
||||
":PASS" => md5($password)
|
||||
));
|
||||
unset($db);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function deleteUser($username){
|
||||
$db = new PDO($this->dbDN);
|
||||
$stmt = $db->prepare("DELETE FROM users WHERE user = :USER");
|
||||
$result = $stmt->execute(array(
|
||||
":USER" => $username
|
||||
));
|
||||
unset($db);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function authUser($username,$password){
|
||||
if(!file_exists("auth.db")){
|
||||
$this->createInitalDB();
|
||||
}
|
||||
$db = new PDO($this->dbDN);
|
||||
$stmt = $db->prepare("SELECT COUNT(*) AS NUMUSER FROM users WHERE user = :USER and pass = :PASS");
|
||||
$result = $stmt->execute(array(
|
||||
":USER" => $username,
|
||||
":PASS" => md5($password)
|
||||
));
|
||||
if($result){
|
||||
$temp = $stmt->fetchAll();
|
||||
if(intval($temp[0]["NUMUSER"]) == 1){
|
||||
return $this->createToken($username);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function verifyToken($username,$token){
|
||||
$db = new PDO($this->dbDN);
|
||||
$stmt = $db->prepare("SELECT COUNT(*) AS NUMTOK FROM tokens WHERE username = :USER and token = :TOKEN and validTo >= :VALIDTO");
|
||||
$result = $stmt->execute(array(
|
||||
":USER" => $username,
|
||||
":TOKEN" => $token,
|
||||
":VALIDTO" => time()
|
||||
));
|
||||
if($result){
|
||||
$temp = $stmt->fetchAll();
|
||||
if(intval($temp[0]["NUMTOK"]) == 1){
|
||||
$stmt2 = $db->prepare("UPDATE tokens SET validTo = :VALIDTO WHERE token = :TOKEN");
|
||||
$result = $stmt2->execute(array(
|
||||
":VALIDTO" => time()+$this->tokenValidity,
|
||||
":TOKEN" => $token
|
||||
));
|
||||
unset($db);
|
||||
return $result;
|
||||
} else {
|
||||
unset($db);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
unset($db);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function logoutToken($username, $token){
|
||||
$db = new PDO($this->dbDN);
|
||||
$stmt = $db->prepare("DELETE FROM tokens WHERE username = :USER AND token = :TOKEN");
|
||||
$result = $stmt->execute(array(
|
||||
":USER" => $username,
|
||||
":TOKEN" => $token
|
||||
));
|
||||
return $result;
|
||||
}
|
||||
}
|
49
WebService/lib/File.php
Normal file
49
WebService/lib/File.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
class File {
|
||||
private $fullFilename = "";
|
||||
|
||||
public function __construct($fullFilename){
|
||||
$this->fullFilename = $fullFilename;
|
||||
}
|
||||
|
||||
public function doExists(){
|
||||
return file_exists($this->fullFilename);
|
||||
}
|
||||
|
||||
public function getMimeType(){
|
||||
return mime_content_type($this->fullFilename);
|
||||
}
|
||||
|
||||
public function getContent(){
|
||||
return file_get_contents($this->fullFilename);
|
||||
}
|
||||
|
||||
public function delete(){
|
||||
return unlink($this->fullFilename);
|
||||
}
|
||||
|
||||
public function getFilePath(){
|
||||
return dirname($this->fullFilename);
|
||||
}
|
||||
|
||||
public function getFilename(){
|
||||
return basename($this->fullFilename);
|
||||
}
|
||||
|
||||
protected function writeToFile($data,$flag){
|
||||
return file_put_contents($this->fullFilename,$data,$flag);
|
||||
}
|
||||
|
||||
public function addContent($data){
|
||||
return $this->writeToFile($data,FILE_APPEND);
|
||||
}
|
||||
|
||||
public function writeContent($data){
|
||||
return $this->writeToFile($data,0);
|
||||
}
|
||||
|
||||
public function createFromUpload($file){
|
||||
move_uploaded_file($file['tmp_name'],$this->fullFilename);
|
||||
}
|
||||
}
|
63
WebService/lib/Folder.php
Normal file
63
WebService/lib/Folder.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
class Folder{
|
||||
private $fullDirname = "";
|
||||
|
||||
public static function isDirectory($fullPath){
|
||||
return is_dir($fullPath);
|
||||
}
|
||||
|
||||
public function __construct($fullDirname){
|
||||
if(substr($fullDirname,-1,1)=="/" && $fullDirname != "/"){
|
||||
$fullDirname=substr($fullDirname,0,-1);
|
||||
}
|
||||
$this->fullDirname = $fullDirname;
|
||||
}
|
||||
|
||||
public function create(){
|
||||
return mkdir($this->fullDirname);
|
||||
}
|
||||
|
||||
public function isEmpty(){
|
||||
return (count(scandir($this->fullDirname)) <= 2);
|
||||
}
|
||||
|
||||
public function delete(){
|
||||
if($this->isEmpty()){
|
||||
return rmdir($this->fullDirname);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getFoldername(){
|
||||
return basename($this->fullDirname);
|
||||
}
|
||||
|
||||
public function getFolderpath(){
|
||||
return dirname($this->fullDirname);
|
||||
}
|
||||
|
||||
public function getEntries(){
|
||||
$entries = array();
|
||||
if($dir = opendir($this->fullDirname)){
|
||||
|
||||
while (false !== ($entry = readdir($dir))) {
|
||||
if($entry != "." && $entry != ".."){
|
||||
if(is_dir($this->fullDirname."/".$entry)){
|
||||
$temp = array("Name" => $entry, "Type" => "dir");
|
||||
}else{
|
||||
$temp = array("Name" => $entry, "Type" => mime_content_type($this->fullDirname."/".$entry));
|
||||
}
|
||||
array_push($entries,$temp);
|
||||
}
|
||||
}
|
||||
|
||||
closedir($dir);
|
||||
return $entries;
|
||||
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user