Skip to content

Commit

Permalink
Fixed bugs and more responsibility
Browse files Browse the repository at this point in the history
  • Loading branch information
nitrogenous committed Jan 19, 2018
1 parent e215243 commit 8dec35c
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 50 deletions.
2 changes: 1 addition & 1 deletion database.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php require_once(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR."XUP".DIRECTORY_SEPARATOR."main.php");require_once(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR."XUP".DIRECTORY_SEPARATOR."adapters".DIRECTORY_SEPARATOR."drive.php");require_once(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR."XUP".DIRECTORY_SEPARATOR."adapters".DIRECTORY_SEPARATOR."dropbox.php");require_once(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR."XUP".DIRECTORY_SEPARATOR."adapters".DIRECTORY_SEPARATOR."amazonwebservices.php");use XUP\Uploader\Main;use XUP\Uploader\Drive;use XUP\Uploader\Dropbox;use XUP\Uploader\AmazonWebServices;$output;$values = array("Drive","Dropbox","AmazonWebServices"); $action = $_POST["action"];$DB = new \Database();foreach ($values as $key) { $result = $DB->$action($_POST,$key); $output[$key] = $result;}$DB->send_status($output);class Database{ protected $bad = array( '<!--', '-->', "'", '"', '<', '>', '&', '$', '=', ';', '?', '/', '!', '#', '%20', //space '%22', // " '%3c', // < '%253c', // < '%3e', // > '%0e', // > '%28', // ( '%29', // ) '%2528', // ( '%26', // & '%24', // $ '%3f', // ? '%3b', // ; '%3d', // = '%2F', // / '%2E', // . // '46', // . // '47' // / ); protected $access_token; function check($post,$value) { $qid = $this->injection($post["qid"]); $formid = $this->injection($post["formid"]); $value = "XUP\Uploader\\".$value; $adapter = new $value(); return $adapter->check($formid,$qid); } function save($post,$value) { if(strtolower($post["value"]) !== strtolower($value)){ return json_encode(array($post["value"] => false)); } else{ $qid = $this->injection($post["qid"]); $key = $post["key"]; $formid = $this->injection($post["formid"]); $value = "XUP\Uploader\\".$value; $adapter = new $value(); return $adapter->save($formid,$qid,$key); } } function upload($post,$value) { $value = "XUP\Uploader\\".$value; $adapter = new $value(); $params = json_encode(array("formid" => $this->injection($post["formid"]),"folder"=>$this->injection($post["folder"]),"qid" => $this->injection($post["qid"]), "key" => $post["key"], "file" => $this->injection($post["file"]),"folderKey" => $this->injection($post["folderKey"]))); return $adapter->upload($params); } function remove($post,$value){ $value = "XUP\Uploader\\".$value; $adapter = new $value(); $params = json_encode(array("formid" => $this->injection($post["formid"]),"qid" => $this->injection($post["qid"]),"remove" => $post["remove"])); return $adapter->remove($params); } function injection($str) { do { $old = $str; $str = str_replace($this->bad, ' ', $str); } while ($old !== $str); return $str; } function tokens($post,$value) { $formid = $this->injection($post["formid"]); $qid = $this->injection($post["qid"]); $auth = $post["key"]; $value = "XUP\Uploader\\".$value; $adapter = new $value(); return $adapter->tokens($formid,$qid,$auth); } function send_status($first) { exit(json_encode($first)); } function removeKey($params){ return null; }}
<?php require_once(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR."XUP".DIRECTORY_SEPARATOR."main.php");require_once(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR."XUP".DIRECTORY_SEPARATOR."adapters".DIRECTORY_SEPARATOR."drive.php");require_once(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR."XUP".DIRECTORY_SEPARATOR."adapters".DIRECTORY_SEPARATOR."dropbox.php");require_once(__DIR__.DIRECTORY_SEPARATOR."src".DIRECTORY_SEPARATOR."XUP".DIRECTORY_SEPARATOR."adapters".DIRECTORY_SEPARATOR."amazonwebservices.php");use XUP\Uploader\Main;use XUP\Uploader\Drive;use XUP\Uploader\Dropbox;use XUP\Uploader\AmazonWebServices;$output;$values = array("Drive","Dropbox","AmazonWebServices"); $action = $_POST["action"];$DB = new \Database();foreach ($values as $key) { $result = $DB->$action($_POST,$key); $output[$key] = $result;}$DB->send_status($output);class Database{ protected $bad = array( '<!--', '-->', "'", '"', '<', '>', '&', '$', '=', ';', '?', '/', '!', '#', '%20', //space '%22', // " '%3c', // < '%253c', // < '%3e', // > '%0e', // > '%28', // ( '%29', // ) '%2528', // ( '%26', // & '%24', // $ '%3f', // ? '%3b', // ; '%3d', // = '%2F', // / '%2E', // . // '46', // . // '47' // / ); protected $access_token; function check($post,$value) { $qid = $this->injection($post["qid"]); $formid = $this->injection($post["formid"]); $value = "XUP\Uploader\\".$value; $adapter = new $value(); return $adapter->check($formid,$qid); } function save($post,$value) { if(strtolower($post["value"]) !== strtolower($value)){ return json_encode(array($post["value"] => false)); } else{ $qid = $this->injection($post["qid"]); $key = $post["key"]; $formid = $this->injection($post["formid"]); $value = "XUP\Uploader\\".$value; $adapter = new $value(); return $adapter->save($formid,$qid,$key); } } function upload($post,$value) { $value = "XUP\Uploader\\".$value; $adapter = new $value(); $params = json_encode(array("formid" => $this->injection($post["formid"]),"folder"=>$this->injection($post["folder"]),"qid" => $this->injection($post["qid"]), "key" => $post["key"], "file" => $this->injection($post["file"]),"folderKey" => $this->injection($post["folderKey"]))); return $adapter->upload($params); } function remove($post,$value){ $value = "XUP\Uploader\\".$value; $adapter = new $value(); $params = json_encode(array("formid" => $this->injection($post["formid"]),"qid" => $this->injection($post["qid"]),"remove" => $post["remove"],"aws" => $post["aws"])); return $adapter->remove($params); } function injection($str) { do { $old = $str; $str = str_replace($this->bad, ' ', $str); } while ($old !== $str); return $str; } function tokens($post,$value) { $formid = $this->injection($post["formid"]); $qid = $this->injection($post["qid"]); $auth = $post["key"]; $value = "XUP\Uploader\\".$value; $adapter = new $value(); return $adapter->tokens($formid,$qid,$auth); } function send_status($first) { exit(json_encode($first)); } function removeKey($params){ return null; }}
Expand Down
47 changes: 40 additions & 7 deletions gearman/workers/amazonWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$worker = new GearmanWorker();
$worker->addServer("127.0.0.1", "4730");
$worker->addFunction("toprakAWS", "toprakAwsUpload");

$worker->addFunction("toprakAWSRemove", "toprakAwsRemove");
while ($worker->work());

function toprakAwsUpload($job) {
Expand All @@ -19,8 +19,10 @@ function toprakAwsUpload($job) {
$qid = $params["qid"];
$file = $params["file"];
$keys = (array)json_decode($params["key"]);
if(empty($key)){
return json_encode(array("Error" => "Key Does Not Exist","File" => null,"Url" => null, "Remove" => null));
var_dump($params);
var_dump($keys);
if(empty($keys)){
return json_encode(array("Error" => "Key Does Not Exist","File" => null,'Folder' => null,"Url" => null, "Remove" => null));
}
var_dump($params);
$access = $keys["access"];
Expand All @@ -34,7 +36,7 @@ function toprakAwsUpload($job) {
$sourcepath =$base_path.DIRECTORY_SEPARATOR . $formid . DIRECTORY_SEPARATOR .$path.DIRECTORY_SEPARATOR.$file;

if(!file_exists($sourcepath)){
return json_encode(array("Error" => "File Does Not Exist","File" => null,"Url" => null, "Remove" => null));
return json_encode(array("Error" => "File Does Not Exist","File" => null,'Folder' => null,"Url" => null, "Remove" => null));
}
else{
$s3 = S3Client::factory(array(
Expand All @@ -54,10 +56,41 @@ function toprakAwsUpload($job) {
));
$url = $result["ObjectURL"];
var_dump($url);
return json_encode(array("Error" => 0,"File" => $file,"Url" => $url, "Remove" => $key));
return json_encode(array("Error" => 0,"File" => $file,'Folder' => null,"Url" => $url, "Remove" => $key));
}
}
catch(Exception $e){
return json_encode(array("Error" => $e,"File" => null,"Url" => null, "Remove" => null));
return json_encode(array("Error" => $e,"File" => null,'Folder' => null,"Url" => null, "Remove" => null));
}
}

}
function toprakAwsRemove($job){
try{
$params = (array)json_decode($job->workload());
var_dump($params);
$keys = (array)json_decode($params["key"]);
$access = $keys["access"];
$secret = $keys["secret"];
$bucket = $keys["bucket"];
$region = $keys["region"];
$remove = (array)json_decode($params["remove"]);
$remove = $remove["Amazon"];

$s3 = S3Client::factory(array(
"region" => $region,
'version' => '2006-03-01',
"credentials" => array(
"key" => $access,
"secret" => $secret
)
));
$result = $s3->deleteObject(array(
"Bucket" => $bucket,
"Key" => $remove
));
var_dump($result);
}
catch(Exception $e){
return $e;
}
}
40 changes: 31 additions & 9 deletions gearman/workers/driveWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ function toprakDriveUpload($job) {
$file_path = $base_path.DIRECTORY_SEPARATOR . $formid . DIRECTORY_SEPARATOR .$path.DIRECTORY_SEPARATOR.$file;
var_dump($params,"\n\n\n");
if(!file_exists($file_path)){
return json_encode(array("Error" => "File Does Not Exist","File" => null,"Url" => null, "Remove" => null));
return json_encode(array("Error" => "File Does Not Exist","File" => null,'Folder' => null,"Url" => null, "Remove" => null));
}
else{
$client = new Google_Client();
$client->setAuthConfig("client_secrets.json");
$client->setSubject($file);
$client->setSubject("Uploading" + $file);
$client->setScopes(["https://www.googleapis.com/auth/drive"]);
$client->setApplicationName("XUP_File_Uploader");
$client->setAccessToken((string)$tokens["access_token"]);
Expand Down Expand Up @@ -78,9 +78,7 @@ function toprakDriveUpload($job) {
"mimeType" => "application/octet-stream",
"uploadType" => "media"));
$url = "www.drive.google.com/#folders/$folderKey";
$return = json_encode(array("folderKey" => $folderKey,"url" => $url));
var_dump($return);
return $return;
return json_encode(array("Error" => 0,"File" => $file,'Folder' => $folderKey,"Url" => $url, "Remove" => $fileService->getId()));
}
if($folderid == null) {
$folderMeta = new Google_Service_Drive_DriveFile(array("name" => $formid,"mimeType" => "application/vnd.google-apps.folder"));
Expand All @@ -104,21 +102,45 @@ function toprakDriveUpload($job) {

$url = "www.drive.google.com/#folders/$folderid";
var_dump($url);
return json_encode(array("Error" => 0,"File" => $file,"Url" => $url, "Remove" => $fileService->getId()));
return json_encode(array("Error" => 0,"File" => $file,'Folder' => $folderid,"Url" => $url, "Remove" => $fileService->getId()));
}
}
catch(Exception $e){
return json_encode(array("Error" => $e,"File" => null,"Url" => null, "Remove" => null));
return json_encode(array("Error" => $e,"File" => null,'Folder' => null, "Url" => null, "Remove" => null));
}
}
function toprakDriveRemove($job) {
try{
$params = (array)json_decode($job->workload());
var_dump($params);
foreach ($params as $param) {
if(empty($param)){
return json_encode(array("Error") => "Please Check Input Variables");
return json_encode(array("Error" => "Please Check Input Variables"));
}
}
$key = $params[$key];
$tokens = (array)json_decode($params["key"]);
$accessKey = (string)$tokens["access_token"];
$refreshKey = (string)$tokens["refresh_token"];
$fileId = (array)json_decode($params["remove"]);
$fileId = $fileId["Drive"];
$client = new Google_Client();
$client->setAuthConfig("client_secrets.json");
$client->setSubject("Removing File");
$client->setScopes(["https://www.googleapis.com/auth/drive"]);
$client->setApplicationName("XUP_File_Uploader");
$client->setAccessToken($accessKey);
if($client->isAccessTokenExpired()){
$refresh = $client->refreshToken($refreshKey);
$drive = new Drive();
$drive->save($formid,$qid,json_encode(array("access_token" => (string)$refresh["access_token"],"refresh_token" => (string)$tokens["refresh_token"])));
echo "\nKey Updated\n\n";
}
$service = new Google_Service_Drive($client);
$test = $service->files->delete($fileId);
var_dump($test);
return true;
}
catch(Exception $e){
return $e;
}
}
6 changes: 3 additions & 3 deletions gearman/workers/dropboxWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function toprakDbxUpload($job) {
var_dump($params);
foreach ($params as $param) {
if(empty($param)){
return json_encode(array("Error" => "File Does Not Exist","File" => null,"Url" => null, "Remove" => null));
return json_encode(array("Error" => "File Does Not Exist","File" => null,'Folder' => null,"Url" => null, "Remove" => null));
}
}
$token = (string)$params["key"];
Expand All @@ -40,11 +40,11 @@ function toprakDbxUpload($job) {
fclose($stream);
$url = "www.dropbox.com/home/$formid/$folder/questionid$qid";
var_dump($url);
return json_encode(array("Error" => 0,"File" => $file, "Url" => $url, "Remove" => $path));
return json_encode(array("Error" => 0,"File" => $file,'Folder' => null, "Url" => $url, "Remove" => $path));
}
}
catch(Exception $e){
return json_encode(array("Error" => $e,"File" => null,"Url" => null,"Remove" => null));
return json_encode(array("Error" => $e,"File" => null,'Folder' => null,"Url" => null,"Remove" => null));
}
}
function toprakDbxRemove($job){
Expand Down
6 changes: 3 additions & 3 deletions index.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
color: #4a85ef;
border: 1px solid #4a85ef;
background-color: #ecececad;
box-shadow: 0 0 5px 2px gray;

}
.uploadItem{
font-size: 15px;
Expand Down Expand Up @@ -110,7 +110,7 @@
max-width: 200px;
text-align: left;
overflow: hidden;
margin-bottom: 3px;
margin-bottom: 4px;
padding-bottom: 1px;
white-space: nowrap;
transition: 0.3s ease;
Expand Down Expand Up @@ -145,7 +145,7 @@
margin-right: 4px;
margin-top: 16.5px;
border-radius: 50%;
padding: 1px;
padding: 2.5px;
line-height: 23px;
vertical-align: top;
transition: 0.5s ease;
Expand Down
Loading

0 comments on commit 8dec35c

Please sign in to comment.