Athlon1600 / php-proxy

A web proxy script written in PHP and built as an alternative to Glype.
https://www.php-proxy.com
MIT License
298 stars 158 forks source link

Possible improvement for proxify_url() #51

Open webaddicto opened 7 years ago

webaddicto commented 7 years ago

Instead of doing all the checks for magnet: or data: inside each function on ProxifyPlugin.php:

    private function html_src($matches){

        if(stripos(trim($matches[2]), 'data:') === 0){
            return $matches[0];
        }

        return str_replace($matches[2], proxify_url($matches[2], $this->base_url), $matches[0]);
    }
    private function css_url($matches){

        $url = trim($matches[1]);

        if(stripos($url, 'data:') === 0){
            return $matches[0];
        }

        return str_replace($matches[1], proxify_url($matches[1], $this->base_url), $matches[0]);
    }

We could integrate all of them directly inside proxify_url() function, example taken from Glype:

function proxyURL($url, $givenFlag = false) {
    global $CONFIG, $options, $bitfield, $flag;

    # Remove excess whitespace
    $url = trim($url);

    # check for binary images
    if (stripos($url,'data:image')===0) {
        return $url;
    }

    # handle javascript
    if (stripos($url,'javascript:')===0 || stripos($url,'livescript:')===0) {
        return '';
    }

    # Validate the input
    if ( empty($url) || $url[0]=='#' || $url=='about:' || stripos($url,'data:')===0 || stripos($url,'file:')===0 || stripos($url,'res:')===0 || stripos($url,'C:')===0 || strpos($url, GLYPE_BROWSE)===0 ) {
        return '';
    }

    # Extract any #anchor since we don't want to encode that
    if ( $tmp = strpos($url, '#') ) {
        $anchor = substr($url, $tmp);
        $url = substr($url, 0, $tmp);
    } else {
        $anchor = '';
    }

    # Convert to absolute URL (if not already)
    $url = absoluteURL($url);

    # Add encoding
    if ( $options['encodeURL'] ) {

        # Part of our encoding is to remove HTTP (saves space and helps avoid detection)
        $url = substr($url, 4);

        # Encrypt
        if ( isset($GLOBALS['unique_salt']) ) {
            $url = arcfour('encrypt',$GLOBALS['unique_salt'],$url);
        }
    }

    # Protect chars that have other meaning in URLs
    $url = rawurlencode($url);

    # Determine flag to use - $givenFlag is passed into function, $flag
    # is global flag currently in use (used here for persisting the frame state)
    $addFlag = $givenFlag ? $givenFlag : ( $flag == 'frame' ? 'frame' : '' );

    # Return in path info format (only when encoding is on)
    if ( $CONFIG['path_info_urls'] && $options['encodeURL'] ) {
        return GLYPE_BROWSE . '/' . str_replace('%', '_', chunk_split($url, 8, '/')) . 'b' . $bitfield . '/' . ( $addFlag ? 'f' . $addFlag : '') . $anchor;
    }

    # Otherwise, return in 'normal' (query string) format
    return GLYPE_BROWSE . '?u=' . $url . '&b=' . $bitfield . ( $addFlag ? '&f=' . $addFlag : '' ) . $anchor;
}

So then we would rewrite our functions like this (much simpler):

    private function css_url($matches){
        return str_replace($matches[1], proxify_url($matches[1], $this->base_url), $matches[0]);
    }
    private function html_src($matches){
        return str_replace($matches[2], proxify_url($matches[2], $this->base_url), $matches[0]);
    }

What do you think?