diff --git a/database.php b/database.php index db920d6..d8e2635 100644 --- a/database.php +++ b/database.php @@ -1 +1 @@ -$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; } } \ No newline at end of file +$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; } } \ No newline at end of file diff --git a/gearman/workers/amazonWorker.php b/gearman/workers/amazonWorker.php index ea8e58a..0d4a55c 100644 --- a/gearman/workers/amazonWorker.php +++ b/gearman/workers/amazonWorker.php @@ -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) { @@ -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"]; @@ -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( @@ -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)); } -} \ No newline at end of file + +} +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; + } + } \ No newline at end of file diff --git a/gearman/workers/driveWorker.php b/gearman/workers/driveWorker.php index 280bbb7..1d9b8b5 100644 --- a/gearman/workers/driveWorker.php +++ b/gearman/workers/driveWorker.php @@ -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"]); @@ -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")); @@ -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; } } diff --git a/gearman/workers/dropboxWorker.php b/gearman/workers/dropboxWorker.php index 0e171a4..00e56dc 100644 --- a/gearman/workers/dropboxWorker.php +++ b/gearman/workers/dropboxWorker.php @@ -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"]; @@ -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){ diff --git a/index.css b/index.css index 6538d41..cd8e537 100644 --- a/index.css +++ b/index.css @@ -62,7 +62,7 @@ color: #4a85ef; border: 1px solid #4a85ef; background-color: #ecececad; - box-shadow: 0 0 5px 2px gray; + } .uploadItem{ font-size: 15px; @@ -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; @@ -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; diff --git a/index.html b/index.html index 49f3580..f825e22 100644 --- a/index.html +++ b/index.html @@ -34,7 +34,7 @@ - +
@@ -116,15 +116,15 @@ } function updateSigninStatus(isSignedIn) { if (isSignedIn) { - console.log("signed"); + // console.log("signed"); } addEventListener { - console.log("error"); + // console.log("error"); } } function handleAuthClick() { - console.log("handleAuthClick"); + // console.log("handleAuthClick"); gapi.auth2.getAuthInstance().signIn(); gapi.auth2.getAuthInstance().grantOfflineAccess().then(function(code){ key = JSON.stringify(code); @@ -185,21 +185,33 @@ for(var x = 0;x < input.files.length; x++){ var file = input.files[x]; + upload(form_id,qid,file,filekey,x); - // console.log("hll"); - upload(form_id,qid,file,filekey,x); } - $("#url").change(function(){ + $("#url").change(function(e){ returnSubmit(JSON.stringify(document.getElementById("url").value),true); - }) + }) // var folder = document.getElementById("folder").value; // removeFiles(form_id,folder); }); JFCustomWidget.subscribe("submit",function(){ + var children = document.getElementById("xup").children; + console.log(children); + console.log(children.length); + + for(let i = 0; i <= children.length - 1; i++){ + if(children[i].id.indexOf("uploadItem") == -1){ + if(i == (children.length-1)){ + console.log(children[i].id); + document.getElementById("url").value = null; + returnSubmit(null,false); + } + } + } var folder = document.getElementById("folder").value; - if(document.getElementById("url").value != "false"){ - removeFiles(form_id,folder); + if(!empty(document.getElementById("url").value)){ + // removeFiles(form_id,folder); returnSubmit(JSON.stringify(document.getElementById("url").value),true); } else{ @@ -225,40 +237,43 @@ formdata.append("key",document.getElementById("aws").value); var progressBar = "progressBar-"+id; save(formdata,filekey,progressBar).done(function(result){ - if(empty(document.getElementById("folder").value)){ + if(empty(document.getElementById("folder").value)){ document.getElementById("folder").value = result; folder = JSON.parse(document.getElementById("folder").value); document.getElementById("folder").value = folder["folder"]; } let qid = JFCustomWidget.getWidgetSetting("qid"); folder = document.getElementById("folder").value; + var callback = null; var folderKey = document.getElementById("folderKey").value; - var url = JSON.parse(sendJob(qid,file.name,formid,folder+"-"+filekey,folderKey,document.getElementById("aws").value)); //key, - console.log(url); - dropbox = JSON.parse(url.Dropbox); + callBack = JSON.parse(sendJob(qid,file.name,formid,folder,folderKey,getAwsKeys())); //key, + // debugger; + if(empty(document.getElementById('folderKey').value)){ + let folderId = JSON.parse(callBack.Drive); + folderId = folderId.Folder; + document.getElementById("folderKey").value = folderId; + } + console.log(callBack); + dropbox = JSON.parse(callBack.Dropbox); dropboxUrl = dropbox.Url; dropboxRemove = dropbox.Remove; console.log(dropboxUrl +"\n"+dropboxRemove); - drive = JSON.parse(url.Drive); + drive = JSON.parse(callBack.Drive); driveUrl = drive.Url; driveRemove = drive.Remove; console.log(driveUrl +"\n"+driveRemove); - amazon = JSON.parse(url.AmazonWebServices); + amazon = JSON.parse(callBack.AmazonWebServices); amazonUrl = amazon.Url; amazonRemove = amazon.Remove; console.log(amazonUrl +"\n"+amazonRemove); - var remove = JSON.parse(JSON.stringify({"Dropbox": dropboxRemove,"Drive": driveRemove,"Amazon": amazonRemove})); - // url.forEach(function(e){ - // remove.push(e); - // }) - document.getElementById("url").value = url; + document.getElementById("url").value = "Dropbox:" + dropboxUrl +"
Drive:" + driveUrl + "
Amazon Web Services:" + amazonUrl; + var remove = JSON.parse(JSON.stringify({"Dropbox": dropboxRemove,"Drive": driveRemove,"Amazon": amazonRemove})); document.getElementById("remove-"+id).value = JSON.stringify({"formid": formid,"qid": qid,"Remove":remove}); $("#url").trigger("change"); }).fail(function(){ console.log("An Error Occured"); }); - return true; } function save(formdata,filekey,progressId){ @@ -415,7 +430,6 @@ document.getElementById(progressSection.id).appendChild(progressBar); document.getElementById(uploadItem.id).appendChild(remove); // document.getElementById(remove.id).appendChild(removeImage); - // console.log($(".progressSection progress::-webkit-progress-value ").css("background-color")); $("#"+remove.id).click(function(e){ e.preventDefault(); var params = JSON.parse(document.getElementById(remove.id).value); @@ -427,7 +441,15 @@ formdata.append("formid", params.formid); formdata.append("qid", params.qid); formdata.append("remove", path); + formdata.append("aws",getAwsKeys()); ajaxRequest("database.php",formdata,true); + var elements = document.getElementById("xup").children; + var totalItemHeights = (elements.length - 1) - 80; + if(totalItemHeights <= 500){ + var height = totalItemHeights; + setFrameSize(height); + } + $("#"+uploadItem.id).remove(); }) return id; } @@ -435,7 +457,7 @@ return filename.split('.').pop(); } function setFrameSize(height){ - if(height < 510){ + if(height < 530){ var size ={}; size.width = 500; size.height = height; @@ -444,7 +466,7 @@ else{ var size ={}; size.width = 500; - size.height = 510; + size.height = 530; JFCustomWidget.requestFrameResize(size); } return true; diff --git a/save.php b/save.php index 6e31252..90258a8 100644 --- a/save.php +++ b/save.php @@ -12,7 +12,7 @@ } $qid = injection($_POST["qid"]); $path = DIRECTORY_SEPARATOR . "tmp"; -$file_path = implode(DIRECTORY_SEPARATOR, array($path,$formid,$folder."-".$key,"questionid".$qid)); +$file_path = implode(DIRECTORY_SEPARATOR, array($path,$formid,$folder,"questionid".$qid)); if(realpath($file_path) !== true) { diff --git a/src/XUP/adapters/amazonwebservices.php b/src/XUP/adapters/amazonwebservices.php index d6f8b90..29c017d 100644 --- a/src/XUP/adapters/amazonwebservices.php +++ b/src/XUP/adapters/amazonwebservices.php @@ -16,7 +16,11 @@ public function save($formid,$qid,$key) { return null; } public function remove($params) { - return false; + $params = (array) json_decode($params); + $job = json_encode(array("key" => $params["aws"],"remove" => $params["remove"])); + $client = new \GearmanClient(); + $client->addServer("127.0.0.1","4730"); + return $client->doBackground("toprakAWSRemove",$job); } public function upload($params) { $client = new \GearmanClient();