diff --git a/README.md b/README.md index 803efc6..e25bbc1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,4 @@ -# PictShare -**[Live Demo](https://www.pictshare.net)** -PictShare is a selfhostable, open source image, video and text hosting as well as URL shortening service with a simple API. - +# PictShare version 2 --- [![Apache License](https://img.shields.io/badge/license-Apache-blue.svg?style=flat)](https://github.com/HaschekSolutions/pictshare/blob/master/LICENSE) @@ -20,6 +17,18 @@ Test site: https://dev.pictshare.net/ (only sometimes on) - [x] Duplicate detection - [x] Write permission detection +### Config options + +- [x] ALT_FOLDER +- [x] URL +- [x] LOG_UPLOADER +- [x] FFMPEG_BINARY +- [ ] PNG_COMPRESSION +- [ ] JPEG_COMPRESSION +- [ ] MASTER_DELETE_CODE +- [ ] MASTER_DELETE_IP +- [ ] UPLOAD_CODE + ### Image hosting - [ ] Resizing - [ ] Filters diff --git a/api/pastebin.php b/api/pastebin.php index 2d8ee31..a623e52 100644 --- a/api/pastebin.php +++ b/api/pastebin.php @@ -10,7 +10,7 @@ include_once(ROOT.DS.'inc'.DS.'config.inc.php'); //loading core and controllers include_once(ROOT . DS . 'inc' . DS. 'core.php'); -require_once(ROOT . DS . 'controllers' . DS. 'text'. DS . 'text.controller.php'); +require_once(ROOT . DS . 'content-controllers' . DS. 'text'. DS . 'text.controller.php'); // check write permissions first if(!isFolderWritable(ROOT.DS.'data')) diff --git a/api/upload.php b/api/upload.php index 35456bb..67dfc7f 100644 --- a/api/upload.php +++ b/api/upload.php @@ -10,10 +10,10 @@ include_once(ROOT.DS.'inc'.DS.'config.inc.php'); //loading core and controllers include_once(ROOT . DS . 'inc' . DS. 'core.php'); -require_once(ROOT . DS . 'controllers' . DS. 'image'. DS . 'image.controller.php'); -require_once(ROOT . DS . 'controllers' . DS. 'text'. DS . 'text.controller.php'); -require_once(ROOT . DS . 'controllers' . DS. 'url'. DS . 'url.controller.php'); -require_once(ROOT . DS . 'controllers' . DS. 'video'. DS . 'video.controller.php'); +require_once(ROOT . DS . 'content-controllers' . DS. 'image'. DS . 'image.controller.php'); +require_once(ROOT . DS . 'content-controllers' . DS. 'text'. DS . 'text.controller.php'); +require_once(ROOT . DS . 'content-controllers' . DS. 'url'. DS . 'url.controller.php'); +require_once(ROOT . DS . 'content-controllers' . DS. 'video'. DS . 'video.controller.php'); // check write permissions first if(!isFolderWritable(ROOT.DS.'data')) @@ -57,8 +57,19 @@ if ($_FILES['file']["error"] == UPLOAD_ERR_OK) $answer = array('status'=>'err','reason'=>'Unsupported filetype'); if($answer['hash']) + { + //add this sha1 to the list addSha1($answer['hash'],$sha1); + // 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']); + } + } + echo json_encode($answer); } else diff --git a/controllers/image/image.controller.php b/content-controllers/image/image.controller.php similarity index 91% rename from controllers/image/image.controller.php rename to content-controllers/image/image.controller.php index 28f7724..6824541 100644 --- a/controllers/image/image.controller.php +++ b/content-controllers/image/image.controller.php @@ -8,7 +8,7 @@ * - Conversion jpg,png to webp */ -class ImageController +class ImageController implements ContentController { //returns all extensions registered by this type of content public function getRegisteredExtensions(){return array('png','bmp','gif','jpg','jpeg','x-png','ico','webp');} @@ -56,15 +56,6 @@ class ImageController copy($tmpfile, $file); unlink($tmpfile); - if(defined('ALT_FOLDER') && ALT_FOLDER) - { - $altname=ALT_FOLDER.DS.$hash; - if(!file_exists($altname) && is_dir(ALT_FOLDER)) - { - copy($file,$altname); - } - } - if(defined('LOG_UPLOADER') && LOG_UPLOADER) { $fh = fopen(ROOT.DS.'data'.DS.'uploads.txt', 'a'); diff --git a/controllers/text/text.controller.php b/content-controllers/text/text.controller.php similarity index 88% rename from controllers/text/text.controller.php rename to content-controllers/text/text.controller.php index 002ad6b..8aba91b 100644 --- a/controllers/text/text.controller.php +++ b/content-controllers/text/text.controller.php @@ -1,6 +1,6 @@ rightEncodedMP4($file)) system("nohup php ".ROOT.DS.'tools'.DS.'re-encode_mp4.php force '.$hash." > /dev/null 2> /dev/null &"); - if(defined('ALT_FOLDER') && ALT_FOLDER) - { - $altname=ALT_FOLDER.DS.$hash; - if(!file_exists($altname) && is_dir(ALT_FOLDER)) - { - copy($file,$altname); - } - } - if(defined('LOG_UPLOADER') && LOG_UPLOADER) { $fh = fopen(ROOT.DS.'data'.DS.'uploads.txt', 'a'); diff --git a/controllers/url/url.controller.php b/controllers/url/url.controller.php deleted file mode 100644 index 537868e..0000000 --- a/controllers/url/url.controller.php +++ /dev/null @@ -1,7 +0,0 @@ -isEnabled()===true && $c->hashExists($el)) + { + $c->pullFile($el); + $hash = $el; + break; + } + } + } } //we didn't find a hash. Well let's just display the webpage instead @@ -67,7 +85,7 @@ function architect($url) (new VideoController())->handleHash($hash,$u); } //very odd. We know it's a valid hash but no controller says it's one of their kids - //oh well, just show the main website + //oh well else { var_dump("odd err"); @@ -93,11 +111,24 @@ function isExistingHash($hash) return is_dir(ROOT.DS.'data'.DS.$hash); } +function mightBeAHash($string) +{ + $len = strlen($string); + $dot = strpos($string,'.'); + if(substr_count($string,'.')!=1) return false; + if(!$dot) return false; + $afterdot = substr($string,$dot+1); + + //@todo: maybe pull all allowed types and compare to afterdot + return ($afterdot && strlen($afterdot)>=2 && strlen($afterdot)<=5 ); +} function autoload($className) { - if (file_exists(ROOT . DS . 'controllers' . DS . strtolower($className) . '.php')) - require_once(ROOT . DS . 'controllers' . DS . strtolower($className) . '.php'); + 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'); } function renderTemplate($template,$vars=false) @@ -225,8 +256,7 @@ function getTypeOfFile($url) $fi = new finfo(FILEINFO_MIME); $type = $fi->buffer(file_get_contents($url, false, null, -1, 1024)); - //to catch a strange error for PHP7 and Alpine Linux - //if the file seems to be a stream, use unix file command + // on linux use the "file" command or it will handle everything as octet-stream if(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' && startsWith($type,'application/octet-stream')) { $content_type = exec("file -bi " . escapeshellarg($url)); @@ -333,4 +363,23 @@ function isSize($var) if(count($a)!=2 || !is_numeric($a[0]) || !is_numeric($a[1])) return false; return true; +} + +function getStorageControllers() +{ + $controllers = array(); + if ($handle = opendir(ROOT.DS.'storage-controllers')) { + while (false !== ($entry = readdir($handle))) { + if ($entry != "." && $entry != "..") { + if(endswith($entry,'.controller.php')) + { + $controllers[] = ucfirst(substr($entry,0,-15)).'Storage'; + include_once(ROOT.DS.'storage-controllers'.DS.$entry); + } + } + } + closedir($handle); + } + + return $controllers; } \ No newline at end of file diff --git a/inc/example.config.inc.php b/inc/example.config.inc.php new file mode 100644 index 0000000..f6cabb4 --- /dev/null +++ b/inc/example.config.inc.php @@ -0,0 +1,14 @@ +hashExists($hash)) + { + copy($orig,$altname); + } + } + + function deleteFile($hash) + { + $altname=ALT_FOLDER.DS.$hash; + if(file_exists($altname)) + { + unlink($altname); + } + } +} \ No newline at end of file diff --git a/tools/re-encode_mp4.php b/tools/re-encode_mp4.php index 2e3aa48..6cfad18 100644 --- a/tools/re-encode_mp4.php +++ b/tools/re-encode_mp4.php @@ -26,7 +26,7 @@ include_once(ROOT.DS.'inc'.DS.'config.inc.php'); //loading core and controllers include_once(ROOT.DS.'inc'.DS.'core.php'); -require_once(ROOT . DS . 'controllers' . DS. 'video'. DS . 'video.controller.php'); +require_once(ROOT . DS . 'content-controllers' . DS. 'video'. DS . 'video.controller.php'); if(!defined('FFMPEG_BINARY')||FFMPEG_BINARY=='' || !FFMPEG_BINARY) exit('Error: FFMPEG_BINARY not defined, no clue where to look');