Skip to content

Commit d1db0ab

Browse files
committed
Minor optimisation/refactoring.
Plus some prepwork for adding better PDF support in the near future.
1 parent 0680548 commit d1db0ab

File tree

3 files changed

+58
-30
lines changed

3 files changed

+58
-30
lines changed

src/Loader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The loader (last modified: 2020.10.15).
11+
* This file: The loader (last modified: 2020.11.20).
1212
*/
1313

1414
namespace phpMussel\Core;
@@ -275,7 +275,7 @@ public function __construct(
275275
}
276276

277277
/** Read the phpMussel configuration file. */
278-
if (preg_match('~\.ini$~i', $this->ConfigurationPath)) {
278+
if (strtolower(substr($this->ConfigurationPath, -4)) === '.ini') {
279279
$this->Configuration = parse_ini_file($this->ConfigurationPath, true);
280280
} elseif (preg_match('~\.ya?ml$~i', $this->ConfigurationPath)) {
281281
if ($Configuration = $this->readFile($this->ConfigurationPath)) {
@@ -1323,7 +1323,7 @@ public function getFavicon(): string
13231323
*/
13241324
public function updateConfiguration(): bool
13251325
{
1326-
if (preg_match('~\.ini$~i', $this->ConfigurationPath)) {
1326+
if (strtolower(substr($this->ConfigurationPath, -4)) === '.ini') {
13271327
$Reconstructed = '';
13281328
foreach ($this->Configuration as $CatKey => $CatValue) {
13291329
if (!is_array($CatValue)) {

src/Scanner.php

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The scanner (last modified: 2020.10.30).
11+
* This file: The scanner (last modified: 2020.11.20).
1212
*/
1313

1414
namespace phpMussel\Core;
@@ -581,7 +581,7 @@ private function recursor($Files = '', int $Depth = -1)
581581
}
582582

583583
/** Cleanup. */
584-
unset($CompressionResults, $CompressionObject);
584+
unset($CompressionObject);
585585
}
586586

587587
$inLen = strlen($in);
@@ -736,6 +736,15 @@ private function dataHandler(string $str = '', int $Depth = 0, string $OriginalF
736736
$$Algo = hash($Algo, $str);
737737
}
738738

739+
/** Scan target has no name? That's a little suspicious. */
740+
if (!$OriginalFilename) {
741+
$this->Loader->atHit($sha256, $StringLength, '', sprintf(
742+
$this->Loader->L10N->getString('grammar_exclamation_mark'),
743+
$this->Loader->L10N->getString('scan_missing_filename')
744+
), 2, $Depth);
745+
return;
746+
}
747+
739748
/** Needed for hash caching plus some other checks. */
740749
$AtInstanceLookupKey = sprintf('%s:%d:%s', $sha256, $StringLength, $OriginalFilename);
741750

@@ -751,21 +760,12 @@ private function dataHandler(string $str = '', int $Depth = 0, string $OriginalF
751760
*/
752761
$CoExMeta = '';
753762
foreach (['OriginalFilename', 'Depth', 'StringLength', 'md5', 'sha1', 'sha256', 'crc32b', 'fourcc', 'twocc'] as $AppendToCoExMeta) {
754-
if (!empty($$$AppendToCoExMeta)) {
763+
if (!empty($$AppendToCoExMeta)) {
755764
$CoExMeta .= '$' . $AppendToCoExMeta . ':' . $$AppendToCoExMeta . ';';
756765
}
757766
}
758767
unset($AppendToCoExMeta);
759768

760-
/** Scan target has no name? That's a little suspicious. */
761-
if (!$OriginalFilename) {
762-
$this->Loader->atHit($sha256, $StringLength, '', sprintf(
763-
$this->Loader->L10N->getString('grammar_exclamation_mark'),
764-
$this->Loader->L10N->getString('scan_missing_filename')
765-
), 2, $Depth);
766-
return;
767-
}
768-
769769
/**
770770
* Check whether the file being scanned has already been recently
771771
* scanned before, to reduce needless work.
@@ -2414,10 +2414,14 @@ private function archiveRecursor(string $Data, string $File = '', int $ScanDepth
24142414
$Handler = 'TarHandler';
24152415
$ConType = 'TarFile';
24162416
$this->Loader->InstanceCache['container'] = 'tarfile';
2417-
} elseif (substr($Data, 0, 4) === 'Rar!' || substr($Data, 0, 4) === "\x52\x45\x7e\x5e") {
2417+
} elseif (substr($Data, 0, 4) === 'Rar!' || substr($Data, 0, 4) === 'RE~^') {
24182418
$Handler = 'RarHandler';
24192419
$ConType = 'RarFile';
24202420
$this->Loader->InstanceCache['container'] = 'rarfile';
2421+
} elseif (substr($Data, 0, 4) === "\x25PDF") {
2422+
$Handler = 'PdfHandler';
2423+
$ConType = 'PdfFile';
2424+
$this->Loader->InstanceCache['container'] = 'pdffile';
24212425
}
24222426

24232427
/** Not an archive. Exit early. */
@@ -2454,7 +2458,7 @@ private function archiveRecursor(string $Data, string $File = '', int $ScanDepth
24542458
}
24552459

24562460
/** Guard. */
2457-
if (!class_exists('ZipArchive')) {
2461+
if (!class_exists('\ZipArchive')) {
24582462
if (!$this->Loader->Configuration['signatures']['fail_extensions_silently']) {
24592463
$this->Loader->atHit($DataHash, $DataLen, $ItemRef, $this->Loader->L10N->getString('scan_extensions_missing'), -1, $ScanDepth);
24602464
return;
@@ -2494,7 +2498,7 @@ private function archiveRecursor(string $Data, string $File = '', int $ScanDepth
24942498
if ($Handler === 'RarHandler') {
24952499

24962500
/** Guard. */
2497-
if (!class_exists('RarArchive') || !class_exists('RarEntry')) {
2501+
if (!class_exists('\RarArchive') || !class_exists('\RarEntry')) {
24982502
if (!$this->Loader->Configuration['signatures']['fail_extensions_silently']) {
24992503
$this->Loader->atHit($DataHash, $DataLen, $ItemRef, $this->Loader->L10N->getString('scan_extensions_missing'), -1, $ScanDepth);
25002504
return;
@@ -2523,6 +2527,28 @@ private function archiveRecursor(string $Data, string $File = '', int $ScanDepth
25232527
}
25242528
}
25252529

2530+
/** Handle PDF files. */
2531+
if ($Handler === 'PdfHandler') {
2532+
2533+
/** Encryption guard. */
2534+
if ($this->Loader->Configuration['files']['block_encrypted_archives']) {
2535+
if (preg_match('~xref.*/Encrypt .*startxref$~', $Data)) {
2536+
$this->Loader->atHit($DataHash, $DataLen, $ItemRef, sprintf(
2537+
$this->Loader->L10N->getString('grammar_exclamation_mark'),
2538+
sprintf(
2539+
$this->Loader->L10N->getString('grammar_brackets'),
2540+
$this->Loader->L10N->getString('encrypted_archive'),
2541+
$ItemRef
2542+
)
2543+
), -4, $ScanDepth);
2544+
return;
2545+
}
2546+
}
2547+
2548+
/** PdfHandler can work with data directly. */
2549+
$ArchiveObject = new PdfHandler($Data);
2550+
}
2551+
25262552
/** Archive object has been instantiated. Let's proceed. */
25272553
if (isset($ArchiveObject) && is_object($ArchiveObject)) {
25282554

@@ -3006,7 +3032,7 @@ private function memoryUse(string $Path, int $Delete = 0, int $DeleteFiles = 0):
30063032
$List = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($Path), \RecursiveIteratorIterator::SELF_FIRST);
30073033
foreach ($List as $Item => $List) {
30083034
$File = str_replace("\\", '/', substr($Item, $Offset));
3009-
if ($File && preg_match('~\.qfu$~i', $Item) && is_file($Item) && !is_link($Item) && is_readable($Item)) {
3035+
if ($File && strtolower(substr($Item, -4)) === '.qfu' && is_file($Item) && !is_link($Item) && is_readable($Item)) {
30103036
$Files[$File] = filemtime($Item);
30113037
}
30123038
}
@@ -3088,7 +3114,7 @@ function_exists('gzinflate') &&
30883114
}
30893115
if ($c = preg_match_all(
30903116
'/(hex2bin\s*\(\s*["\'])([\da-f]{1,4096})(["\']\s*\))/i',
3091-
$str, $matches )) {
3117+
$str, $matches)) {
30923118
for ($i = 0; $c > $i; $i++) {
30933119
$str = str_ireplace(
30943120
$matches[0][$i],
@@ -3758,7 +3784,7 @@ private function metaDataScan(string $ItemRef, string $Filename, string &$Data,
37583784

37593785
/** Determine whether the file being scanned is a macro. */
37603786
$this->Loader->InstanceCache['file_is_macro'] = (
3761-
preg_match('~vbaProject\.bin$~i', $Filename) ||
3787+
strtolower(substr($Filename, -14)) === 'vbaproject.bin' ||
37623788
preg_match('~^\xd0\xcf|\x00Attribut|\x01CompObj|\x05Document~', $Data)
37633789
);
37643790

@@ -3805,7 +3831,7 @@ private function metaDataScan(string $ItemRef, string $Filename, string &$Data,
38053831
}
38063832

38073833
/** Cleanup. */
3808-
unset($CompressionResults, $CompressionObject);
3834+
unset($CompressionObject);
38093835
}
38103836

38113837
/** Reset Crx variables. */

src/TarHandler.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: Tar handler (last modified: 2020.10.26).
11+
* This file: Tar handler (last modified: 2020.11.19).
1212
*/
1313

1414
namespace phpMussel\Core;
@@ -38,21 +38,21 @@ class TarHandler extends ArchiveHandler
3838
/**
3939
* Construct the tar archive object.
4040
*
41-
* @param string $Pointer
41+
* @param string $File
4242
*/
43-
public function __construct($Pointer)
43+
public function __construct($File)
4444
{
45-
/** Guard against wrong type of file used as pointer. */
46-
if (empty($Pointer) || substr($Pointer, 257, 6) !== "ustar\0") {
45+
/** Guard against the wrong type of file being used as pointer. */
46+
if (substr($File, 257, 6) !== "ustar\0") {
4747
$this->ErrorState = 2;
4848
return;
4949
}
5050

5151
/** Set total size. */
52-
$this->TotalSize = strlen($Pointer);
52+
$this->TotalSize = strlen($File);
5353

5454
/** Set archive data. */
55-
$this->Data = $Pointer;
55+
$this->Data = $File;
5656

5757
/** All is good. */
5858
$this->ErrorState = 0;
@@ -105,7 +105,7 @@ public function EntryIsDirectory(): bool
105105
/**
106106
* Return whether the entry at the current entry pointer is encrypted.
107107
*
108-
* @return bool Tar doesn't use encryption, therefore always false.
108+
* @return false Tar doesn't use encryption.
109109
*/
110110
public function EntryIsEncrypted(): bool
111111
{
@@ -114,6 +114,8 @@ public function EntryIsEncrypted(): bool
114114

115115
/**
116116
* Return the reported internal CRC hash for the entry, if it exists.
117+
*
118+
* @return false Tar doesn't provide internal CRCs.
117119
*/
118120
public function EntryCRC()
119121
{

0 commit comments

Comments
 (0)