Skip to content

Conversation

@webaddicto
Copy link
Contributor

Useful to create a plugin that handles more websites (i.e abc.com, abc.de, abc.eu, etc) or similar.

Useful to create a plugin that handles more websites (i.e abc.com, abc.de, abc.eu, etc) or similar.
@webaddicto
Copy link
Contributor Author

Example usage to match abc.com, abc.de and abc.pl:

namespace Proxy\Plugin;

use Proxy\Plugin\AbstractPlugin;
use Proxy\Event\ProxyEvent;

use Proxy\Html;

class MultiSiteMatchPlugin extends AbstractPlugin {

	protected $url_pattern_regex = '#^abc\.(com|de|pl)$#is';
	
	public function onCompleted(ProxyEvent $event){
	
		$response = $event['response'];
		$html = $response->getContent();
		// do your stuff here...
		$response->setContent($html);
	}
}

@Athlon1600
Copy link
Owner

What happens if both $url_pattern and $url_pattern_regex are set? I'd rather just have one $url_pattern that can be used to match both as regex and as non-regex. But first you would have to somehow detect whether $url_pattern contains regex or not and that can be tricky...

@webaddicto
Copy link
Contributor Author

webaddicto commented Mar 4, 2017

I would write in the help file or readme that user should use $url_pattern or $url_pattern_regex (not both).

An alternative option would be like this:

$url_pattern = "abc.com"; => match string
$url_pattern = "regex:'#^abc\.(com|de|pl)$#is'"; => extract regex:(.+?) and use preg_match()

What do you think?

@Athlon1600
Copy link
Owner

Why not just have it like this:
$url_pattern = 'abc.com' => treat it like a regular strpos match
$url_pattern = '/abc.(com|net)/' => treat it like a preg_match match

The laziest way of accomplishing this is just to check the first character of $url_pattern. If it's /, then you have a regex pattern.

@webaddicto
Copy link
Contributor Author

webaddicto commented Mar 4, 2017

I wrote 4 alternatives:

Alternative 1:

		// url filter provided and current request url does not match it
		if($this->url_pattern && strpos($url, $this->url_pattern) === false){
			return;
		}
		// url filter (regex) provided and current request url does not match it
		elseif($this->url_pattern_regex && !preg_match($this->url_pattern_regex, $url)){
			return;
		}

Alternative 2:

		// url filter provided and current request url does not match it
		if($this->url_pattern){
		    if(stripos($this->url_pattern, 'r:') === 0){
			    if(!preg_match(substr($this->url_pattern, 2), $url)) 
				return;
			} 
			else
			{
			    if(strpos($url, $this->url_pattern) === false) 
				return;
			}
		}

Alternative 3:

		// url filter provided and current request url does not match it
		if($this->url_pattern){
		    if(!preg_match('/^[a-zA-Z0-9]{1}/is', $this->url_pattern){
			    if(!preg_match($this->url_pattern, $url)) 
				return;
			} 
			else
			{
			    if(strpos($url, $this->url_pattern) === false) 
				return;
			}
		}

Alternative 4:

		// url filter provided and current request url does not match it
		if($this->url_pattern){
		    if(strpos($this->url_pattern, '/') === 0){
			    if(!preg_match($this->url_pattern, $url)) 
				return;
			} 
			else
			{
			    if(strpos($url, $this->url_pattern) === false) 
				return;
			}
		}

I would vote for 3 because it allows user to use any special character on preg_match:

** May not be good for unicode domain names? **

$url_pattern = '#^abc\.(com|de|pl)$#is'; => regex
$url_pattern = '/^abc\.(com|de|pl)$/is'; => regex
$url_pattern = '@^abc\.(com|de|pl)$@is'; => regex
$url_pattern = 'abc.com'; => string

Or 4 is fine too, but we need to write that user must use / character.

What do you think?

@Athlon1600
Copy link
Owner

Option 3 would also match 'abc.com' even when it was intended to be a regular match... I would go with 4 because few people use delimiters other than '/'. It's a default regex deliminator on every tutorial online.

@webaddicto
Copy link
Contributor Author

webaddicto commented Mar 4, 2017

Perfect, I have updated the PR with a new commit according to your request :)
0595954

@Athlon1600
Copy link
Owner

Athlon1600 commented Mar 4, 2017

Yup, looks good to me now!

@Athlon1600 Athlon1600 merged commit 0cd1183 into Athlon1600:master Mar 4, 2017
@webaddicto
Copy link
Contributor Author

Good, I have added info about the $url_pattern on this new PR:
#55

Also this PR may help to detect the latest php-proxy version in case users report issues
#53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants