diff --git a/README.md b/README.md index 2d15a43..eb7c287 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ Table of contents ## New Features in v2 +- Added support for external storage +- [Encryption of files in external storage](/rtfm/ENCRYPTION.md) - Added text hosting (like pastebin) - Added URL shortening - Added WebP to images (and conversion from jpg,png to webp) diff --git a/api/base64.php b/api/base64.php index 090b9da..47b5f4c 100644 --- a/api/base64.php +++ b/api/base64.php @@ -78,13 +78,7 @@ if($_REQUEST['base64']) $answer['delete_url'] = URL.'delete_'.getDeleteCodeOfHash($answer['hash']).'/'.$answer['hash']; } - // Lets' check all storage controllers and tell them that a new file was uploaded - $sc = getStorageControllers(); - foreach($sc as $contr) - { - if((new $contr())->isEnabled()===true) - (new $contr())->pushFile($answer['hash']); - } + storageControllerUpload($answer['hash']); } echo json_encode($answer); diff --git a/api/geturl.php b/api/geturl.php index 5346453..4134846 100644 --- a/api/geturl.php +++ b/api/geturl.php @@ -78,13 +78,7 @@ if($answer['hash'] && $answer['status']=='ok') } - // Lets' check all storage controllers and tell them that a new file was uploaded - $sc = getStorageControllers(); - foreach($sc as $contr) - { - if((new $contr())->isEnabled()===true) - (new $contr())->pushFile($answer['hash']); - } + storageControllerUpload($answer['hash']); } if($answer['hash'] && $answer['status']=='ok') @@ -98,13 +92,7 @@ if($answer['hash'] && $answer['status']=='ok') $answer['delete_url'] = URL.'delete_'.getDeleteCodeOfHash($answer['hash']).'/'.$answer['hash']; } - // Lets' check all storage controllers and tell them that a new file was uploaded - $sc = getStorageControllers(); - foreach($sc as $contr) - { - if((new $contr())->isEnabled()===true) - (new $contr())->pushFile($answer['hash']); - } + storageControllerUpload($answer['hash']); } echo json_encode($answer); diff --git a/api/upload.php b/api/upload.php index 6520ae9..0571de0 100644 --- a/api/upload.php +++ b/api/upload.php @@ -73,13 +73,7 @@ if ($_FILES['file']["error"] == UPLOAD_ERR_OK) } - // Lets' check all storage controllers and tell them that a new file was uploaded - $sc = getStorageControllers(); - foreach($sc as $contr) - { - if((new $contr())->isEnabled()===true) - (new $contr())->pushFile($answer['hash']); - } + storageControllerUpload($answer['hash']); } echo json_encode($answer); diff --git a/inc/core.php b/inc/core.php index b456fdd..efc693d 100644 --- a/inc/core.php +++ b/inc/core.php @@ -51,11 +51,27 @@ function architect($url) foreach($sc as $contr) { $c = new $contr(); - if($c->isEnabled()===true && $c->hashExists($el)) + if($c->isEnabled()===true && $c->hashExists($el)) { - $c->pullFile($el); $hash = $el; - break; // we brake here because we already have the file. no need to check other storage controllers + $c->pullFile($hash,ROOT.DS.'tmp'.DS.$hash); + storeFile(ROOT.DS.'tmp'.DS.$hash,$hash,true); + + break; // we break here because we already have the file. no need to check other storage controllers + } + else if($c->isEnabled()===true && defined('ENCRYPTION_KEY') && ENCRYPTION_KEY !='' && $c->hashExists($el.'.enc')) //this is an encrypted file. Let's decrypt it + { + $hash = $el.'.enc'; + $c->pullFile($hash,ROOT.DS.'tmp'.DS.$hash); + + $enc = new Encryption; + $hash = substr($hash,0,-4); + $enc->decryptFile(ROOT.DS.'tmp'.DS.$el.'.enc', ROOT.DS.'tmp'.DS.$hash,base64_decode(ENCRYPTION_KEY)); + + storeFile(ROOT.DS.'tmp'.DS.$hash,$hash,true); + unlink(ROOT.DS.'tmp'.DS.$el.'.enc'); + + break; // we break here because we already have the file. no need to check other storage controllers } } } @@ -123,6 +139,30 @@ function architect($url) //var_dump($u); } +function storageControllerUpload($hash) +{ + // Lets' check all storage controllers and tell them that a new file was uploaded + $sc = getStorageControllers(); + foreach($sc as $contr) + { + if((new $contr())->isEnabled()===true) + { + $source = ROOT.DS.'data'.DS.$hash.DS.$hash; + if(defined('ENCRYPTION_KEY') && ENCRYPTION_KEY) //ok so we got an encryption key which means we'll store only the encrypted file + { + $enc = new Encryption; + $encoded_file = ROOT.DS.'tmp'.DS.$hash.'.enc'; + $enc->encryptFile($source,$encoded_file,base64_decode(ENCRYPTION_KEY)); + (new $contr())->pushFile($encoded_file,$hash.'.enc'); + } + else // not encrypted + (new $contr())->pushFile($source,$hash); + + } + + } +} + function getNewHash($type,$length=10) { while(1) @@ -155,7 +195,9 @@ function autoload($className) if (file_exists(ROOT . DS . 'content-controllers' . DS . strtolower($className) . '.php')) require_once(ROOT . DS . 'content-controllers' . DS . strtolower($className) . '.php'); if (file_exists(ROOT . DS . 'interfaces' . DS . strtolower($className) . '.interface.php')) - require_once(ROOT . DS . 'interfaces' . DS . strtolower($className) . '.interface.php'); + require_once(ROOT . DS . 'interfaces' . DS . strtolower($className) . '.interface.php'); + if ($className=='Encryption') + require_once(ROOT . DS . 'inc' . DS . 'encryption.php'); } function renderTemplate($template,$vars=false) diff --git a/inc/encryption.php b/inc/encryption.php new file mode 100644 index 0000000..700fb8b --- /dev/null +++ b/inc/encryption.php @@ -0,0 +1,85 @@ +hashExists($hash)) { - copy($orig,$altname); - } + copy($source,$altname); + return true; + } + + return false; } function deleteFile($hash) diff --git a/storage-controllers/s3.controller.php b/storage-controllers/s3.controller.php index c490fb8..5922489 100644 --- a/storage-controllers/s3.controller.php +++ b/storage-controllers/s3.controller.php @@ -9,7 +9,7 @@ * (optional) S3_ENDPOINT */ -class S3Storage //implements StorageController +class S3Storage implements StorageController { private $s3; function connect(){ @@ -38,27 +38,28 @@ class S3Storage //implements StorageController return $this->s3->doesObjectExist(S3_BUCKET,$hash); } - function pullFile($hash) + function pullFile($hash,$location) { if(!$this->s3)$this->connect(); if(!$this->hashExists($hash)) return false; + $this->s3->getObject([ 'Bucket' => S3_BUCKET, 'Key' => $hash, - 'SaveAs' => ROOT.DS.'data'.DS.$hash.DS.$hash + 'SaveAs' => $location ]); return true; } - function pushFile($hash) + function pushFile($source,$hash) { if(!$this->s3)$this->connect(); $this->s3->putObject([ 'Bucket' => S3_BUCKET, 'Key' => $hash, - 'SourceFile' => ROOT.DS.'data'.DS.$hash.DS.$hash + 'SourceFile' => $source ]); return true;