Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 620ba2e

Browse files
author
Joshua Moon
committed
PSR-2, SWF test fix, WEBP test added
1 parent d776213 commit 620ba2e

File tree

5 files changed

+437
-417
lines changed

5 files changed

+437
-417
lines changed

src/Detectives/ImageGDDetective.php

Lines changed: 101 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,108 @@
1-
<?php namespace InfinityNext\Sleuth\Detectives;
1+
<?php
2+
3+
namespace InfinityNext\Sleuth\Detectives;
24

35
use InfinityNext\Sleuth\Contracts\DetectiveContract;
46
use InfinityNext\Sleuth\Traits\DetectiveTrait;
57

68
class ImageGDDetective implements DetectiveContract
79
{
8-
use DetectiveTrait;
9-
10-
/**
11-
* Checks if thise file is a GIF.
12-
*
13-
* @return boolean|null
14-
*/
15-
protected function leadGIF()
16-
{
17-
$exif = exif_imagetype($this->file) === IMAGETYPE_GIF;
18-
19-
if ($exif)
20-
{
21-
return $this->closeCase("gif", "image/gif");
22-
}
23-
24-
return null;
25-
}
26-
27-
/**
28-
* Checks if thise file is a JPG.
29-
*
30-
* @return boolean|null
31-
*/
32-
protected function leadJPG()
33-
{
34-
$exif = exif_imagetype($this->file) === IMAGETYPE_JPEG;
35-
36-
if ($exif)
37-
{
38-
return $this->closeCase("jpg", "image/jpg");
39-
}
40-
41-
return null;
42-
}
43-
44-
/**
45-
* Checks if thise file is a PNG.
46-
*
47-
* @return boolean|null
48-
*/
49-
protected function leadPNG()
50-
{
51-
$exif = exif_imagetype($this->file) === IMAGETYPE_PNG;
52-
53-
if ($exif)
54-
{
55-
return $this->closeCase("png", "image/png");
56-
}
57-
58-
return null;
59-
}
60-
61-
/**
62-
* Checks if the file is a SWF.
63-
*
64-
* @return boolean|null
65-
*/
66-
protected function leadSWF()
67-
{
68-
$exif = exif_imagetype($this->file);
69-
$flash = ($exif === IMAGETYPE_SWF || $exif === IMAGETYPE_SWC);
70-
71-
if ($exif)
72-
{
73-
return $this->closeCase("swf", "application/x-shockwave-flash");
74-
}
75-
76-
return null;
77-
}
78-
79-
/**
80-
* Can this file type potentially cause damage or intrude on a user's privacy?
81-
* This means executable programs, or file formats that can contact remote servers in any way (even SVGs).
82-
*
83-
* @return boolean
84-
* @throws \InfinityNext\Sleuth\Exceptions\CaseNotSolved
85-
*/
86-
public function isRisky()
87-
{
88-
parent::isRisky();
89-
90-
return false;
91-
}
92-
93-
/**
94-
* Can the system run this Detective?
95-
*
96-
* @return boolean True if we can run, False if not.
97-
*/
98-
public static function on()
99-
{
100-
return function_exists("exif_imagetype");
101-
}
10+
use DetectiveTrait;
11+
12+
/**
13+
* Checks if thise file is a GIF.
14+
*
15+
* @return boolean|null
16+
*/
17+
protected function leadGIF()
18+
{
19+
if (exif_imagetype($this->file) === IMAGETYPE_GIF) {
20+
return $this->closeCase("gif", "image/gif");
21+
}
22+
23+
return null;
24+
}
25+
26+
/**
27+
* Checks if thise file is a JPG.
28+
*
29+
* @return boolean|null
30+
*/
31+
protected function leadJPG()
32+
{
33+
if (exif_imagetype($this->file) === IMAGETYPE_JPEG) {
34+
return $this->closeCase("jpg", "image/jpg");
35+
}
36+
37+
return null;
38+
}
39+
40+
/**
41+
* Checks if thise file is a PNG.
42+
*
43+
* @return boolean|null
44+
*/
45+
protected function leadPNG()
46+
{
47+
if (exif_imagetype($this->file) === IMAGETYPE_PNG) {
48+
return $this->closeCase("png", "image/png");
49+
}
50+
51+
return null;
52+
}
53+
54+
/**
55+
* Checks if the file is a SWF.
56+
*
57+
* @return boolean|null
58+
*/
59+
protected function leadSWF()
60+
{
61+
$exif = exif_imagetype($this->file);
62+
$flash = ($exif === IMAGETYPE_SWF || $exif === IMAGETYPE_SWC);
63+
64+
if ($flash) {
65+
return $this->closeCase("swf", "application/x-shockwave-flash");
66+
}
67+
68+
return null;
69+
}
70+
71+
/**
72+
* Checks if thise file is a WEBP.
73+
*
74+
* @return boolean|null
75+
*/
76+
protected function leadWEBP()
77+
{
78+
if (exif_imagetype($this->file) === IMAGETYPE_WEBP) {
79+
return $this->closeCase("webp", "image/webp");
80+
}
81+
82+
return null;
83+
}
84+
85+
/**
86+
* Can this file type potentially cause damage or intrude on a user's privacy?
87+
* This means executable programs, or file formats that can contact remote servers in any way (even SVGs).
88+
*
89+
* @return boolean
90+
* @throws \InfinityNext\Sleuth\Exceptions\CaseNotSolved
91+
*/
92+
public function isRisky()
93+
{
94+
parent::isRisky();
95+
96+
return false;
97+
}
98+
99+
/**
100+
* Can the system run this Detective?
101+
*
102+
* @return boolean True if we can run, False if not.
103+
*/
104+
public static function on()
105+
{
106+
return function_exists("exif_imagetype");
107+
}
102108
}

src/FileSleuth.php

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace InfinityNext\Sleuth;
1+
<?php
2+
3+
namespace InfinityNext\Sleuth;
24

35
use InfinityNext\Sleuth\Contracts\DetectiveContract;
46
use InfinityNext\Sleuth\Detectives\ImageGDDetective;
@@ -7,74 +9,68 @@
79

810
class FileSleuth
911
{
10-
/**
11-
* Detectives to be used to check the file.
12-
*
13-
* @var array
14-
*/
15-
protected $detectives = [
16-
ImageGDDetective::class,
17-
ffmpegDetective::class,
18-
svgDetective::class,
19-
];
12+
/**
13+
* Detectives to be used to check the file.
14+
*
15+
* @var array
16+
*/
17+
protected $detectives = [
18+
ImageGDDetective::class,
19+
ffmpegDetective::class,
20+
svgDetective::class,
21+
];
2022

21-
/**
22-
* The file we're checking.
23-
*
24-
* @var string
25-
*/
26-
protected $file;
23+
/**
24+
* The file we're checking.
25+
*
26+
* @var string
27+
*/
28+
protected $file;
2729

28-
/**
29-
* Instantiates the model.
30-
*
31-
* @param string $file Optional parameter to automatically run a check.
32-
* @return null|boolean
33-
*/
34-
public function __construct($file = null)
35-
{
36-
if (is_null($file))
37-
{
38-
return $this->check($file);
39-
}
40-
}
30+
/**
31+
* Instantiates the model.
32+
*
33+
* @param string $file Optional parameter to automatically run a check.
34+
* @return null|boolean
35+
*/
36+
public function __construct($file = null)
37+
{
38+
if (is_null($file)) {
39+
return $this->check($file);
40+
}
41+
}
4142

42-
/**
43-
* Runs the file against all dectives.
44-
*
45-
* @param string $file Optional parameter to automatically run a check.
46-
* @param string|null $verify Extension to verify against. Checks all possible if unset.
47-
* @return null|boolean
48-
*/
49-
public function check($file, $verify = null)
50-
{
51-
if (is_string($verify))
52-
{
53-
$verify = "lead" . strtoupper($verify);
54-
}
43+
/**
44+
* Runs the file against all dectives.
45+
*
46+
* @param string $file Optional parameter to automatically run a check.
47+
* @param string|null $verify Extension to verify against. Checks all possible if unset.
48+
* @return null|boolean
49+
*/
50+
public function check($file, $verify = null)
51+
{
52+
if (is_string($verify)) {
53+
$verify = "lead" . strtoupper($verify);
54+
}
5555

56-
$detectives = [];
57-
foreach ($this->detectives as $detectiveClass)
58-
{
59-
if ($detectiveClass::on())
60-
{
61-
$detectives[] = $detectiveClass;
62-
}
63-
}
56+
$detectives = [];
57+
foreach ($this->detectives as $detectiveClass) {
58+
if ($detectiveClass::on()) {
59+
$detectives[] = $detectiveClass;
60+
}
61+
}
6462

65-
$case = null;
63+
$case = null;
6664

67-
foreach ($detectives as $detectiveClass)
68-
{
69-
$detective = new $detectiveClass();
70-
$case = $detective->check($file, $verify);
65+
foreach ($detectives as $detectiveClass) {
66+
$detective = new $detectiveClass();
67+
$case = $detective->check($file, $verify);
7168

72-
if ($case)
73-
{
74-
return $detective;
75-
}
76-
}
69+
if ($case) {
70+
return $detective;
71+
}
72+
}
7773

78-
return false;
79-
}
74+
return false;
75+
}
8076
}

0 commit comments

Comments
 (0)