Skip to content

Commit 5bf94f4

Browse files
committed
Added better url validation
1 parent e97f76d commit 5bf94f4

5 files changed

Lines changed: 59 additions & 35 deletions

File tree

loadnoncomposer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* along with MegaOptim Image Optimizer. If not, see <https://www.gnu.org/licenses/>.
1919
**********************************************************************/
2020

21+
require_once('src/Tools/PATH.php');
22+
require_once('src/Tools/URL.php');
2123
require_once('src/Interfaces/IFile.php');
2224
require_once('src/Tools/FileSystem.php');
2325
require_once('src/Http/HTTP.php');

src/Optimizer.php

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use MegaOptim\Responses\Response;
2525
use MegaOptim\Services\OptimizerService;
2626
use MegaOptim\Tools\FileSystem;
27+
use MegaOptim\Tools\PATH;
28+
use MegaOptim\Tools\URL;
2729

2830
class Optimizer {
2931

@@ -163,11 +165,11 @@ public function run( $resource, $args = array(), $local_wait = false ) {
163165

164166
/**
165167
* Returns the results of the process
166-
*
167168
* @param $process_id
168-
* @param $max_wait_seconds
169+
* @param int $max_wait_seconds
169170
*
170171
* @return Response
172+
* @throws \Exception
171173
*/
172174
public function get_result( $process_id, $max_wait_seconds = 5 ) {
173175
$result = $this->service->get_result( $process_id, $max_wait_seconds );
@@ -201,38 +203,7 @@ public static function validate( $resource, $args ) {
201203
* @return bool
202204
*/
203205
public static function is_url( $resource ) {
204-
return ! ! filter_var( $resource, FILTER_VALIDATE_URL );
205-
}
206-
207-
/**
208-
* Check if the given path is support image type (jpg,png,gif,svg)
209-
*
210-
* @param string $path - The local temporary path
211-
*
212-
* @return bool
213-
*/
214-
public static function is_supported( $path ) {
215-
return array_key_exists( pathinfo( $path, PATHINFO_EXTENSION ), self::accepted_types() );
216-
}
217-
218-
/**
219-
* Return the accepted file types
220-
* @return array
221-
*/
222-
public static function accepted_types() {
223-
return array(
224-
'png' => 'image/png',
225-
'jpe' => 'image/jpeg',
226-
'jpeg' => 'image/jpeg',
227-
'jpg' => 'image/jpeg',
228-
'gif' => 'image/gif',
229-
//'bmp' => 'image/bmp',
230-
//'ico' => 'image/vnd.microsoft.icon',
231-
//'tiff' => 'image/tiff',
232-
//'tif' => 'image/tiff',
233-
//'svg' => 'image/svg+xml',
234-
//'svgz' => 'image/svg+xml',
235-
);
206+
return URL::validate($resource);
236207
}
237208

238209
/**

src/Tools/FileSystem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function scan_dir( $dir ) {
3737
if ( ! is_dir( $dir ) ) {
3838
return $resources;
3939
} else {
40-
foreach ( Optimizer::accepted_types() as $extension => $mime_type ) {
40+
foreach ( PATH::accepted_types() as $extension => $mime_type ) {
4141
$paths = glob( $dir . DIRECTORY_SEPARATOR . '*.' . $extension );
4242
if ( is_array( $paths ) ) {
4343
$resources = array_merge( $resources, $paths );

src/Tools/PATH.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace MegaOptim\Tools;
4+
5+
class PATH {
6+
/**
7+
* Check if the given path is support image type (jpg,png,gif,svg)
8+
*
9+
* @param string $path - The local temporary path
10+
*
11+
* @return bool
12+
*/
13+
public static function is_supported( $path ) {
14+
return array_key_exists( pathinfo( $path, PATHINFO_EXTENSION ), self::accepted_types() );
15+
}
16+
/**
17+
* Return the accepted file types
18+
* @return array
19+
*/
20+
public static function accepted_types() {
21+
return array(
22+
'png' => 'image/png',
23+
'jpe' => 'image/jpeg',
24+
'jpeg' => 'image/jpeg',
25+
'jpg' => 'image/jpeg',
26+
'gif' => 'image/gif',
27+
//'bmp' => 'image/bmp',
28+
//'ico' => 'image/vnd.microsoft.icon',
29+
//'tiff' => 'image/tiff',
30+
//'tif' => 'image/tiff',
31+
//'svg' => 'image/svg+xml',
32+
//'svgz' => 'image/svg+xml',
33+
);
34+
}
35+
}

src/Tools/URL.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace MegaOptim\Tools;
3+
4+
class URL {
5+
/**
6+
* Validates url
7+
* @param $resource
8+
*
9+
* @return bool
10+
*/
11+
public static function validate($resource) {
12+
$pattern = "#(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))#iS";
13+
preg_match( $pattern, $resource, $matches );
14+
return !empty($matches);
15+
}
16+
}

0 commit comments

Comments
 (0)