diff --git a/composer.json b/composer.json index 2bf58ab..5695afb 100644 --- a/composer.json +++ b/composer.json @@ -2,10 +2,10 @@ "name": "athlon1600/php-proxy", "type": "library", "keywords": ["php proxy", "proxy script", "php web proxy", "web proxy", "php proxy script"], + "license": "MIT", "homepage": "https://www.php-proxy.com/", "require": { "ext-curl": "*", - "symfony/event-dispatcher": "~3.2" }, "suggest": { "predis/predis": "For caching purposes" diff --git a/src/Event/ProxyEvent.php b/src/Event/ProxyEvent.php index 1cfddec..8c707bb 100644 --- a/src/Event/ProxyEvent.php +++ b/src/Event/ProxyEvent.php @@ -2,11 +2,7 @@ namespace Proxy\Event; -use Symfony\Component\EventDispatcher\Event; - -// http://symfony.com/doc/current/components/event_dispatcher/generic_event.html -class ProxyEvent extends Event implements \ArrayAccess { - +class ProxyEvent implements \ArrayAccess { private $data; public function __construct($data = array()){ diff --git a/src/Plugin/AbstractPlugin.php b/src/Plugin/AbstractPlugin.php index 405c33b..605cee3 100644 --- a/src/Plugin/AbstractPlugin.php +++ b/src/Plugin/AbstractPlugin.php @@ -2,12 +2,10 @@ namespace Proxy\Plugin; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Proxy\Event\ProxyEvent; -abstract class AbstractPlugin implements EventSubscriberInterface { - +abstract class AbstractPlugin { + // apply these methods only to those events whose request URL passes this filter protected $url_pattern; @@ -27,26 +25,40 @@ public function onCompleted(ProxyEvent $event){ // fired after the full response=headers+body has been read - will only be called on "non-streaming" responses } - // dispatch based on filter - final public function route(ProxyEvent $event, $event_name, EventDispatcherInterface $dispatcher){ + final public function subscribe($dispatcher){ + + $dispatcher->addListener('request.before_send', function($event){ + $this->route('request.before_send', $event); + }); + + $dispatcher->addListener('request.sent', function($event){ + $this->route('request.sent', $event); + }); + + $dispatcher->addListener('curl.callback.write', function($event){ + $this->route('curl.callback.write', $event); + }); + + $dispatcher->addListener('request.complete', function($event){ + $this->route('request.complete', $event); + }); + } + // dispatch based on filter + final private function route($event_name, ProxyEvent $event){ $url = $event['request']->getUri(); // 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)) + if(starts_with($this->url_pattern, '/') && preg_match($this->url_pattern, $url) !== 1){ return; - } - else - { - if(stripos($url, $this->url_pattern) === false) + } else if(stripos($url, $this->url_pattern) === false){ return; } } switch($event_name){ - + case 'request.before_send': $this->onBeforeRequest($event); break; @@ -64,17 +76,6 @@ final public function route(ProxyEvent $event, $event_name, EventDispatcherInter break; } } - - // This method returns an array indexed by event names and whose values are either the method name to call - // or an array composed of the method name to call and a priority. - final public static function getSubscribedEvents(){ - return array( - 'request.before_send' => 'route', - 'request.sent' => 'route', - 'curl.callback.write' => 'route', - 'request.complete' => 'route' - ); - } } ?> diff --git a/src/Plugin/ProxifyPlugin.php b/src/Plugin/ProxifyPlugin.php index cf82354..3165428 100644 --- a/src/Plugin/ProxifyPlugin.php +++ b/src/Plugin/ProxifyPlugin.php @@ -12,10 +12,9 @@ class ProxifyPlugin extends AbstractPlugin { private $base_url = ''; private function css_url($matches){ - - $url = trim($matches[1]); - if(stripos($url, 'data:') === 0){ + $url = trim($matches[1]); + if(starts_with($url, 'data:')){ return $matches[0]; } @@ -33,7 +32,8 @@ private function html_attr($matches){ // could be empty? $url = trim($matches[2]); - if(stripos($url, 'data:') === 0 || stripos($url, 'magnet:') === 0 || stripos($url, 'about:') === 0 || stripos($url, 'javascript:') === 0 || stripos($url, 'mailto:') === 0 || stripos($url, 'tel:') === 0 || stripos($url, 'ios-app:') === 0 || stripos($url, 'android-app:') === 0){ + $schemes = array('data:', 'magnet:', 'about:', 'javascript:', 'mailto:', 'tel:', 'ios-app:', 'android-app:'); + if(starts_with($url, $schemes)){ return $matches[0]; } @@ -101,7 +101,7 @@ private function meta_refresh($matches){ //