Skip to content

Commit e7e4f6d

Browse files
committed
Improved URL validation
1 parent 8e876c2 commit e7e4f6d

2 files changed

Lines changed: 29 additions & 28 deletions

File tree

src/Optimizer.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Optimizer {
3939
const RESOURCE_FILES = 'files';
4040
const MAX_ALLOWED_RESOURCES = 5;
4141

42-
const VERSION = '1.0.4';
42+
const VERSION = '1.0.5';
4343
/**
4444
* @var string
4545
*/
@@ -166,6 +166,7 @@ public function run( $resource, $args = array(), $local_wait = false ) {
166166

167167
/**
168168
* Returns the results of the process
169+
*
169170
* @param $process_id
170171
* @param int $max_wait_seconds
171172
*
@@ -211,17 +212,19 @@ public static function prepare_data( $resource, $args ) {
211212
$is_url = 0;
212213
$is_file = 0;
213214
foreach ( $resource as $file ) {
214-
if ( URL::validate( $file ) ) {
215-
$is_url = 1;
215+
216+
if ( file_exists( $file ) && is_file( $file ) ) {
217+
$is_file = 1;
218+
$is_url = 0;
219+
} else if ( file_exists( $file ) && is_dir( $file ) ) {
220+
$is_file = 0;
221+
$is_url = 0;
222+
} else if ( URL::validate( $file ) ) {
216223
$is_file = 0;
224+
$is_url = 1;
217225
} else {
218-
if ( file_exists( $file ) ) {
219-
$is_url = 0;
220-
$is_file = 1;
221-
} else {
222-
$is_url = 0;
223-
$is_file = 0;
224-
}
226+
$is_file = 0;
227+
$is_url = 0;
225228
}
226229
}
227230
if ( $is_url ) {
@@ -313,11 +316,11 @@ public function get_user_info() {
313316
*
314317
* @return bool
315318
*/
316-
public static function valid_compression_level($level) {
317-
return in_array($level, array(
319+
public static function valid_compression_level( $level ) {
320+
return in_array( $level, array(
318321
self::COMPRESSION_LOSSLESS,
319322
self::COMPRESSION_INTELLIGENT,
320323
self::COMPRESSION_ULTRA,
321-
));
324+
) );
322325
}
323326
}

src/Tools/URL.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
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-
}
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+
return (bool) preg_match('_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS',$resource);
13+
}
1614
}

0 commit comments

Comments
 (0)