diff --git a/.gitignore b/.gitignore index 60ac4891..905ddab5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ notice.txt .phpunit.result.cache docs/ .claude +.antigravity* logs data -tmp \ No newline at end of file +tmp diff --git a/src/api/info.php b/src/api/info.php index fbf2206f..7b0df461 100644 --- a/src/api/info.php +++ b/src/api/info.php @@ -41,10 +41,28 @@ function getInfoAboutHash($hash) return array('status'=>'err','reason'=>'File not found'); $size = filesize($file); $size_hr = renderSize($size); - $content_type = exec("file -bi " . escapeshellarg($file)); + + $content_type = false; + try { + if (class_exists('finfo')) { + $finfo = new finfo(FILEINFO_MIME); + $content_type = $finfo->file($file); + } + } catch (\Throwable $t) { + // ignore + } + + if (!$content_type && function_exists('mime_content_type')) { + $content_type = @mime_content_type($file); + } + + if (!$content_type && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { + $content_type = @exec("file -bi " . escapeshellarg($file)); + } + + $type = $content_type; if($content_type && strpos($content_type,'/')!==false && strpos($content_type,';')!==false) { - $type = $content_type; $c = explode(';',$type); $type = $c[0]; } diff --git a/src/content-controllers/image/image.controller.php b/src/content-controllers/image/image.controller.php index 49b27dc7..d2651171 100644 --- a/src/content-controllers/image/image.controller.php +++ b/src/content-controllers/image/image.controller.php @@ -314,7 +314,7 @@ function gifToMP4($gifpath,$target) function saveObjOfImage($im,$path,$type) { - $tmppath = '/tmp/'.getNewHash($type,12); + $tmppath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . getNewHash($type,12); switch($type) { case 'jpeg': diff --git a/src/inc/core.php b/src/inc/core.php index 72b87f3c..9d0a6185 100644 --- a/src/inc/core.php +++ b/src/inc/core.php @@ -371,10 +371,15 @@ function storageControllerUpload($hash) function stringInFile($string,$file) { - $handle = fopen($file, 'r'); + if (!file_exists($file)) return false; + $handle = @fopen($file, 'r'); + if (!$handle) return false; while (($line = fgets($handle)) !== false) { $line=trim($line); - if($line==$string) return true; + if($line==$string) { + fclose($handle); + return true; + } } fclose($handle); return false; @@ -484,18 +489,24 @@ function renderSize($byte) function getTypeOfFile($url) { - // on linux use the "file" command or it will handle everything as octet-stream - if(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') - { - $content_type = exec("file -bi " . escapeshellarg($url)); - if($content_type && strpos($content_type,'/')!==false && strpos($content_type,';')!==false) - $type = $content_type; + $type = false; + try { + if (class_exists('finfo')) { + $finfo = new finfo(FILEINFO_MIME); + $type = $finfo->file($url); + } + } catch (\Throwable $t) { + // ignore } - else - { - //for windows we'll use mime_content_type. Make sure you have enabled the "exif" extension in php.ini - $type = mime_content_type($url); + + if (!$type && function_exists('mime_content_type')) { + $type = @mime_content_type($url); } + + if (!$type && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { + $type = @exec("file -bi " . escapeshellarg($url)); + } + if(!$type) return false; if(startsWith($type,'text')) return 'text'; $arr = explode(';', trim($type)); @@ -1387,13 +1398,22 @@ function rebuildMeta(){ function getFileMimeType($file) { try { - $finfo = new finfo(FILEINFO_MIME_TYPE); - return $finfo->file($file); - } catch (Exception $e) { - //fallback to shell command if finfo is not available - $mimeType = shell_exec('file --mime-type -b ' . escapeshellarg($file)); - return trim($mimeType); + if (class_exists('finfo')) { + $finfo = new finfo(FILEINFO_MIME_TYPE); + return $finfo->file($file); + } + } catch (\Throwable $e) { + // ignore } + + if (function_exists('mime_content_type')) { + $mime = @mime_content_type($file); + if ($mime) return $mime; + } + + //fallback to shell command if finfo and mime_content_type are not available + $mimeType = shell_exec('file --mime-type -b ' . escapeshellarg($file)); + return trim($mimeType); } function getRelativeToDataPath(string $path): string diff --git a/web/index.php b/web/index.php index d1835a60..add1e9b3 100644 --- a/web/index.php +++ b/web/index.php @@ -22,13 +22,21 @@ // redis if(!defined('REDIS_CACHING') || REDIS_CACHING == true) { - $GLOBALS['redis'] = new Redis(); - $GLOBALS['redis']->connect((!defined('REDIS_SERVER'))?'localhost':REDIS_SERVER, (!defined('REDIS_PORT'))?6379:REDIS_PORT); + try { + $GLOBALS['redis'] = new Redis(); + $redis_host = (!defined('REDIS_SERVER')) ? 'localhost' : REDIS_SERVER; + $redis_port = (!defined('REDIS_PORT')) ? 6379 : REDIS_PORT; + // connect with 1.0 second timeout to prevent locking up PHP workers if Redis is down + @$GLOBALS['redis']->connect($redis_host, $redis_port, 1.0); + } catch (\Throwable $e) { + $GLOBALS['redis'] = null; + error_log("Failed to connect to Redis: " . $e->getMessage()); + } } -//parse the URL to an array and filter it -$url = array_filter(explode('/',ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? '','/'))); +//parse the URL to an array and filter it, keeping '0' values +$url = array_filter(explode('/',ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? '','/')), 'strlen'); if($url[0] == 'api') {