From 02ddadf957974bdec6bd5c2f3682d60ed726f51e Mon Sep 17 00:00:00 2001 From: Darko Date: Wed, 10 Dec 2014 09:51:00 +0100 Subject: [PATCH] Added magic numbers support. --- lib/DB/patches/0105~site.sql | 3 ++ lib/Info.php | 11 +---- lib/ProcessAdditional.php | 16 +++---- lib/copy_this/www/lib/TmuxOutput.php | 2 +- lib/copy_this/www/lib/util.php | 44 +++++++++++++++++++ .../nntmux/views/admin/site-edit.tpl | 13 +++++- start.php | 2 +- 7 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 lib/DB/patches/0105~site.sql diff --git a/lib/DB/patches/0105~site.sql b/lib/DB/patches/0105~site.sql new file mode 100644 index 000000000..50f984b50 --- /dev/null +++ b/lib/DB/patches/0105~site.sql @@ -0,0 +1,3 @@ +INSERT IGNORE INTO `site` (`setting`, `value`) VALUES + ('magic_file_path', ''); +UPDATE `tmux` set `value` = '105' where `setting` = 'sqlpatch'; \ No newline at end of file diff --git a/lib/Info.php b/lib/Info.php index 18f80b2fe..ccdb38dc5 100644 --- a/lib/Info.php +++ b/lib/Info.php @@ -150,15 +150,8 @@ class Info file_put_contents($tmpPath, $possibleNFO); // Linux boxes have 'file' (so should Macs), Windows *can* have it too: see GNUWIN.txt in docs. - if (Utility::hasCommand('file')) { - exec('file -b "' . $tmpPath . '"', $result); - if (is_array($result)) { - if (count($result) > 1) { - $result = implode(',', $result[0]); - } else { - $result = $result[0]; - } - } + $result = Utility::fileInfo($tmpPath); + if (!empty($result)) { // Check if it's text. if (preg_match('/(ASCII|ISO-8859|UTF-(8|16|32).*?)\s*text/', $result)) { diff --git a/lib/ProcessAdditional.php b/lib/ProcessAdditional.php index 4ba5c3275..add52e80a 100644 --- a/lib/ProcessAdditional.php +++ b/lib/ProcessAdditional.php @@ -1212,23 +1212,17 @@ Class ProcessAdditional ) { $this->_processVideoFile($file); } // Check if it's alt.binaries.u4e file. - else if (in_array($this->_releaseGroupName, array('alt.binaries.u4e', 'alt.binaries.mom')) && + else if (in_array($this->_releaseGroupName, ['alt.binaries.u4e', 'alt.binaries.mom']) && preg_match('/Linux_2rename\.sh/i', $file) && ($this->_release['categoryID'] == \Category::CAT_MISC_HASHED || $this->_release['categoryID'] == \Category::CAT_MISC_OTHER) ) { $this->_processU4ETitle($file); - } // If we have GNU file, check the type of file and process it. - else if ($this->_hasGNUFile) { - exec('file -b "' . $file . '"', $output); - + } + // Check file's magic info. + else { + $output = Utility::fileInfo($file); if (!empty($output)) { - if (count($output) > 1) { - $output = implode(',', $output); - } else { - $output = $output[0]; - } - switch (true) { case ($this->_foundJPGSample === false && preg_match('/^JPE?G/i', $output[0])): diff --git a/lib/copy_this/www/lib/TmuxOutput.php b/lib/copy_this/www/lib/TmuxOutput.php index 816983239..b9be457ea 100644 --- a/lib/copy_this/www/lib/TmuxOutput.php +++ b/lib/copy_this/www/lib/TmuxOutput.php @@ -117,7 +117,7 @@ class TmuxOutput extends Tmux $buffer = ''; $state = ($this->runVar['settings']['is_running'] == 1) ? 'Running' : 'Disabled'; //$version = $this->_tvers . 'r' . $this->_vers; - $tversion = '0.5r0076'; + $tversion = '0.5r0078'; $buffer .= sprintf($this->tmpMasks[2], "Monitor $state v$tversion @ $this->_tvers [" . $this->_vers ."]: ", diff --git a/lib/copy_this/www/lib/util.php b/lib/copy_this/www/lib/util.php index 910c85405..0f6423d9f 100644 --- a/lib/copy_this/www/lib/util.php +++ b/lib/copy_this/www/lib/util.php @@ -507,6 +507,50 @@ class Utility return $sent; } + /** + * Return file type/info using magic numbers. + * Try using `file` program where available, fallback to using PHP's finfo class. + * + * @param string $path Path to the file / folder to check. + * + * @return string File info. Empty string on failure. + */ + static public function fileInfo($path) + { + $output = ''; + $magicPath = (new Sites())->get->magic_file_path; + if (self::hasCommand('file') && (!self::isWin() || !empty($magicPath))) { + $magicSwitch = empty($magicPath) ? '' : " -m $magicPath"; + $output = runCmd('file' . $magicSwitch . ' -b "' . $path . '"'); + + if (is_array($output)) { + switch (count($output)) { + case 0: + $output = ''; + break; + case 1: + $output = $output[0]; + break; + default: + $output = implode(' ', $output); + break; + } + } else { + $output = ''; + } + } else { + $fileInfo = empty($magicPath) ? new finfo(FILEINFO_RAW) : new finfo(FILEINFO_RAW, $magicPath); + + $output = $fileInfo->file($path); + if (empty($output)) { + $output = ''; + } + $fileInfo->close(); + } + + return $output; + } + } function checkStatus ($code) diff --git a/lib/copy_this/www/templates/nntmux/views/admin/site-edit.tpl b/lib/copy_this/www/templates/nntmux/views/admin/site-edit.tpl index eb4569369..db0152009 100644 --- a/lib/copy_this/www/templates/nntmux/views/admin/site-edit.tpl +++ b/lib/copy_this/www/templates/nntmux/views/admin/site-edit.tpl @@ -333,7 +333,7 @@ - +
The path to the timeout binary. This is used to limit the amount of time unrar/7zip/mediainfo/ffmpeg/avconv can run. You can the time limit in the process additional section. @@ -342,6 +342,17 @@
+ + + + +
Path to magic number database. Windows' users should set this if they have installed GNUWin file. *nix users can optionally set this to a file of their choice.
+ + diff --git a/start.php b/start.php index c07515d4d..1760d573d 100644 --- a/start.php +++ b/start.php @@ -35,7 +35,7 @@ if (count($nntpkill) === 0) { } // Check database patch version -if ($patch < 104) { +if ($patch < 105) { exit($c->error("\nYour database is not up to date. Please update.\nphp ${DIR}/tmux/lib/DB/patchDB.php\n")); }