mirror of
https://github.com/HaschekSolutions/pictshare.git
synced 2025-11-13 19:56:23 +00:00
added delete codes and fixed various bugs related to custom hashes
This commit is contained in:
@@ -24,6 +24,7 @@ Table of contents
|
|||||||
## Breaking changes
|
## Breaking changes
|
||||||
|
|
||||||
- New API system. Only single file uploads now via /api/upload.php (POST var name is "file"). [read more..](/rtfm/API.md)
|
- New API system. Only single file uploads now via /api/upload.php (POST var name is "file"). [read more..](/rtfm/API.md)
|
||||||
|
- Data directory changed from ```upload``` to ```data```
|
||||||
|
|
||||||
## Status
|
## Status
|
||||||
|
|
||||||
|
|||||||
@@ -28,9 +28,10 @@ if ($_FILES['file']["error"] == UPLOAD_ERR_OK)
|
|||||||
{
|
{
|
||||||
//check for duplicates
|
//check for duplicates
|
||||||
$sha1 = sha1_file($_FILES['file']["tmp_name"]);
|
$sha1 = sha1_file($_FILES['file']["tmp_name"]);
|
||||||
$hash = sha1Exists($sha1);
|
$ehash = sha1Exists($sha1);
|
||||||
if($hash)
|
if($ehash && file_exists(ROOT.DS.'data'.DS.$ehash.DS.$ehash))
|
||||||
exit(json_encode(array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash)));
|
exit(json_encode(array('status'=>'ok','hash'=>$ehash,'url'=>URL.$ehash)));
|
||||||
|
|
||||||
|
|
||||||
//get the file type
|
//get the file type
|
||||||
$type = getTypeOfFile($_FILES['file']["tmp_name"]);
|
$type = getTypeOfFile($_FILES['file']["tmp_name"]);
|
||||||
@@ -56,11 +57,18 @@ if ($_FILES['file']["error"] == UPLOAD_ERR_OK)
|
|||||||
if(!$answer)
|
if(!$answer)
|
||||||
$answer = array('status'=>'err','reason'=>'Unsupported filetype');
|
$answer = array('status'=>'err','reason'=>'Unsupported filetype');
|
||||||
|
|
||||||
if($answer['hash'])
|
if($answer['hash'] && $answer['status']=='ok')
|
||||||
{
|
{
|
||||||
//add this sha1 to the list
|
//add this sha1 to the list
|
||||||
addSha1($answer['hash'],$sha1);
|
addSha1($answer['hash'],$sha1);
|
||||||
|
|
||||||
|
if(getDeleteCodeOfHash($answer['hash']))
|
||||||
|
{
|
||||||
|
$answer['delete_code'] = getDeleteCodeOfHash($answer['hash']);
|
||||||
|
$answer['delete_url'] = URL.'delete_'.getDeleteCodeOfHash($answer['hash']).'/'.$answer['hash'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Lets' check all storage controllers and tell them that a new file was uploaded
|
// Lets' check all storage controllers and tell them that a new file was uploaded
|
||||||
$sc = getStorageControllers();
|
$sc = getStorageControllers();
|
||||||
foreach($sc as $contr)
|
foreach($sc as $contr)
|
||||||
|
|||||||
@@ -28,8 +28,6 @@ class ImageController implements ContentController
|
|||||||
$res = imagecreatefromjpeg($tmpfile);
|
$res = imagecreatefromjpeg($tmpfile);
|
||||||
imagejpeg($res, $tmpfile, (defined('JPEG_COMPRESSION')?JPEG_COMPRESSION:90));
|
imagejpeg($res, $tmpfile, (defined('JPEG_COMPRESSION')?JPEG_COMPRESSION:90));
|
||||||
$ext = 'jpg';
|
$ext = 'jpg';
|
||||||
|
|
||||||
$newsha1 = sha1_file($tmpfile);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -42,26 +40,13 @@ class ImageController implements ContentController
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if(!endswith($hash,'.'.$ext))
|
||||||
$hash.='.'.$ext;
|
$hash.='.'.$ext;
|
||||||
if(isExistingHash($hash))
|
if(isExistingHash($hash))
|
||||||
return array('status'=>'err','reason'=>'Custom hash already exists');
|
return array('status'=>'err','hash'=>$hash,'reason'=>'Custom hash already exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
if($newsha1)
|
storeFile($tmpfile,$hash,true);
|
||||||
addSha1($hash,$newsha1);
|
|
||||||
|
|
||||||
mkdir(ROOT.DS.'data'.DS.$hash);
|
|
||||||
$file = ROOT.DS.'data'.DS.$hash.DS.$hash;
|
|
||||||
|
|
||||||
copy($tmpfile, $file);
|
|
||||||
unlink($tmpfile);
|
|
||||||
|
|
||||||
if(defined('LOG_UPLOADER') && LOG_UPLOADER)
|
|
||||||
{
|
|
||||||
$fh = fopen(ROOT.DS.'data'.DS.'uploads.txt', 'a');
|
|
||||||
fwrite($fh, time().';'.$url.';'.$hash.';'.getUserIP()."\n");
|
|
||||||
fclose($fh);
|
|
||||||
}
|
|
||||||
|
|
||||||
return array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash);
|
return array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,21 +43,10 @@ class TextController implements ContentController
|
|||||||
if(!endswith($hash,'.txt'))
|
if(!endswith($hash,'.txt'))
|
||||||
$hash.='.txt';
|
$hash.='.txt';
|
||||||
if(isExistingHash($hash))
|
if(isExistingHash($hash))
|
||||||
return array('status'=>'err','reason'=>'Custom hash already exists');
|
return array('status'=>'err','hash'=>$hash,'reason'=>'Custom hash already exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
mkdir(ROOT.DS.'data'.DS.$hash);
|
storeFile($tmpfile,$hash,true);
|
||||||
$file = ROOT.DS.'data'.DS.$hash.DS.$hash;
|
|
||||||
|
|
||||||
copy($tmpfile, $file);
|
|
||||||
unlink($tmpfile);
|
|
||||||
|
|
||||||
if(defined('LOG_UPLOADER') && LOG_UPLOADER)
|
|
||||||
{
|
|
||||||
$fh = fopen(ROOT.DS.'data'.DS.'uploads.txt', 'a');
|
|
||||||
fwrite($fh, time().';'.$url.';'.$hash.';'.getUserIP()."\n");
|
|
||||||
fclose($fh);
|
|
||||||
}
|
|
||||||
|
|
||||||
return array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash);
|
return array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,25 +69,14 @@ class VideoController implements ContentController
|
|||||||
{
|
{
|
||||||
$hash.='.mp4';
|
$hash.='.mp4';
|
||||||
if(isExistingHash($hash))
|
if(isExistingHash($hash))
|
||||||
return array('status'=>'err','reason'=>'Custom hash already exists');
|
return array('status'=>'err','hash'=>$hash,'reason'=>'Custom hash already exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
mkdir(ROOT.DS.'data'.DS.$hash);
|
storeFile($tmpfile,$hash,true);
|
||||||
$file = ROOT.DS.'data'.DS.$hash.DS.$hash;
|
|
||||||
|
|
||||||
copy($tmpfile, $file);
|
|
||||||
unlink($tmpfile);
|
|
||||||
|
|
||||||
if(!$this->rightEncodedMP4($file))
|
if(!$this->rightEncodedMP4($file))
|
||||||
system("nohup php ".ROOT.DS.'tools'.DS.'re-encode_mp4.php force '.$hash." > /dev/null 2> /dev/null &");
|
system("nohup php ".ROOT.DS.'tools'.DS.'re-encode_mp4.php force '.$hash." > /dev/null 2> /dev/null &");
|
||||||
|
|
||||||
if(defined('LOG_UPLOADER') && LOG_UPLOADER)
|
|
||||||
{
|
|
||||||
$fh = fopen(ROOT.DS.'data'.DS.'uploads.txt', 'a');
|
|
||||||
fwrite($fh, time().';'.$url.';'.$hash.';'.getUserIP()."\n");
|
|
||||||
fclose($fh);
|
|
||||||
}
|
|
||||||
|
|
||||||
return array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash);
|
return array('status'=>'ok','hash'=>$hash,'url'=>URL.$hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
86
inc/core.php
86
inc/core.php
@@ -37,6 +37,7 @@ function architect($url)
|
|||||||
$hash = $el;
|
$hash = $el;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// if we don't have a hash yet but the element looks like it could be a hash
|
||||||
if($hash === false && mightBeAHash($el))
|
if($hash === false && mightBeAHash($el))
|
||||||
{
|
{
|
||||||
if(!$sc)
|
if(!$sc)
|
||||||
@@ -48,7 +49,7 @@ function architect($url)
|
|||||||
{
|
{
|
||||||
$c->pullFile($el);
|
$c->pullFile($el);
|
||||||
$hash = $el;
|
$hash = $el;
|
||||||
break;
|
break; // we brake here because we already have the file. no need to check other storage controllers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,7 +63,25 @@ function architect($url)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//ok we have a valid hash. Now let's check the extension to find out which controller will be handling this request
|
//ok we have a valid hash.
|
||||||
|
|
||||||
|
//is the user requesting this file to be deleted?
|
||||||
|
foreach($u as $el)
|
||||||
|
{
|
||||||
|
if(startsWith($el,'delete_'))
|
||||||
|
{
|
||||||
|
$code = substr($el,7);
|
||||||
|
//@todo: allow MASTER_DELETE_IP to be CIDR range or coma separated
|
||||||
|
if(getDeleteCodeOfHash($hash)==$code || (defined('MASTER_DELETE_CODE') && MASTER_DELETE_CODE==$code ) || (defined('MASTER_DELETE_IP') && MASTER_DELETE_IP==getUserIP()) )
|
||||||
|
{
|
||||||
|
deleteHash($hash);
|
||||||
|
exit($hash.' deleted successfully');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Now let's check the extension to find out which controller will be handling this request
|
||||||
$extension = pathinfo($hash, PATHINFO_EXTENSION);
|
$extension = pathinfo($hash, PATHINFO_EXTENSION);
|
||||||
|
|
||||||
|
|
||||||
@@ -385,3 +404,66 @@ function getStorageControllers()
|
|||||||
|
|
||||||
return $controllers;
|
return $controllers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function rrmdir($dir) {
|
||||||
|
if (is_dir($dir)) {
|
||||||
|
$objects = scandir($dir);
|
||||||
|
foreach ($objects as $object) {
|
||||||
|
if ($object != "." && $object != "..") {
|
||||||
|
if (is_dir($dir."/".$object))
|
||||||
|
rrmdir($dir."/".$object);
|
||||||
|
else
|
||||||
|
unlink($dir."/".$object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rmdir($dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function storeFile($srcfile,$hash,$deleteoriginal=false)
|
||||||
|
{
|
||||||
|
if(is_dir(ROOT.DS.'data'.DS.$hash) && file_exists(ROOT.DS.'data'.DS.$hash.DS.$hash)) return;
|
||||||
|
mkdir(ROOT.DS.'data'.DS.$hash);
|
||||||
|
$file = ROOT.DS.'data'.DS.$hash.DS.$hash;
|
||||||
|
|
||||||
|
copy($srcfile, $file);
|
||||||
|
if($deleteoriginal===true)
|
||||||
|
unlink($srcfile);
|
||||||
|
|
||||||
|
addSha1($hash,sha1_file($file));
|
||||||
|
|
||||||
|
//creating a delete code
|
||||||
|
$deletecode = getRandomString(32);
|
||||||
|
$fh = fopen(ROOT.DS.'data'.DS.$hash.DS.'deletecode', 'w');
|
||||||
|
fwrite($fh, $deletecode);
|
||||||
|
fclose($fh);
|
||||||
|
|
||||||
|
if(defined('LOG_UPLOADER') && LOG_UPLOADER)
|
||||||
|
{
|
||||||
|
$fh = fopen(ROOT.DS.'data'.DS.'uploads.txt', 'a');
|
||||||
|
fwrite($fh, time().';'.$url.';'.$hash.';'.getUserIP()."\n");
|
||||||
|
fclose($fh);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDeleteCodeOfHash($hash)
|
||||||
|
{
|
||||||
|
return file_get_contents(ROOT.DS.'data'.DS.$hash.DS.'deletecode');
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteHash($hash)
|
||||||
|
{
|
||||||
|
//delete all local images
|
||||||
|
rrmdir(ROOT.DS.'data'.DS.$hash);
|
||||||
|
|
||||||
|
//tell every storage controller to delete theirs as well
|
||||||
|
$sc = getStorageControllers();
|
||||||
|
foreach($sc as $contr)
|
||||||
|
{
|
||||||
|
$c = new $contr();
|
||||||
|
if($c->isEnabled()===true && $c->hashExists($el))
|
||||||
|
{
|
||||||
|
$c->deleteFile($el);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,11 +18,7 @@ class AltfolderStorage implements StorageController
|
|||||||
$altname=ALT_FOLDER.DS.$hash;
|
$altname=ALT_FOLDER.DS.$hash;
|
||||||
if(file_exists($altname))
|
if(file_exists($altname))
|
||||||
{
|
{
|
||||||
mkdir(ROOT.DS.'data'.DS.$hash);
|
storeFile($altname,$hash,false);
|
||||||
copy($altname,ROOT.DS.'data'.DS.$hash.DS.$hash);
|
|
||||||
|
|
||||||
//and don't forget to add it to the duplicate detection system
|
|
||||||
addSha1($hash,sha1_file($altname));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user