Assembla home | Assembla project page
 

Changeset 274

Show
Ignore:
Timestamp:
08/01/08 13:47:58 (4 months ago)
Author:
Seldaek
Message:

* some cleanups to Url, added schemeless option. fixes #40

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Plugin/Url.php

    r262 r274  
    7474     */ 
    7575    protected $showErrors = true; 
     76 
     77    /** 
     78     * Whether or not to detect schemeless urls (i.e. "example.com") 
     79     * 
     80     * @var boolean 
     81     */ 
     82    protected $detectSchemeless = false; 
    7683 
    7784    /** 
     
    119126 
    120127    /** 
     128     * Obtains configuration settings 
     129     * 
     130     * @return void 
     131     */ 
     132    public function onInit() 
     133    { 
     134        $this->updateSetting('base_format', 'baseFormat'); 
     135        $this->updateSetting('message_format', 'messageFormat'); 
     136        $this->updateSetting('merge_links', 'mergeLinks'); 
     137        $this->updateSetting('title_length', 'titleLength', true); 
     138        $this->updateSetting('show_errors', 'showErrors'); 
     139        $this->updateSetting('detect_schemeless', 'detectSchemeless'); 
     140    } 
     141 
     142    /** 
    121143     * Initializes settings 
    122144     * 
     
    153175 
    154176        // URL Match 
    155         if (preg_match_all('#((?:https?://(?:(?:[a-z0-9_-]+\.?)+\.[a-z0-9]{1,6} | 
    156                             ([0-9]{1,3}(?:\.[0-9]{1,3}){3})) | 
    157                             ([@/\\\])?(?:[a-z0-9_-]+\.?)+\.[a-z0-9]{1,6})[^\s]*)#xis', 
     177        if (preg_match_all('#'.($this->detectSchemeless ? '' : 'https?://').'(?:([0-9]{1,3}(?:\.[0-9]{1,3}){3})(?![^/]) | 
     178                            ('.($this->detectSchemeless ? '(?<!http:/|https:/)[@/\\\]' : '').')?(?:(?:[a-z0-9_-]+\.?)+\.[a-z0-9]{1,6}))[^\s]*#xis', 
    158179                            $this->event->getArgument(1), $matches, PREG_SET_ORDER)) { 
    159  
    160             // Updates the URL settings against the ini settings 
    161             $this->updateSetting('base_format', 'baseFormat'); 
    162             $this->updateSetting('message_format', 'messageFormat'); 
    163             $this->updateSetting('merge_links', 'mergeLinks'); 
    164             $this->updateSetting('title_length', 'titleLength', true); 
    165             $this->updateSetting('show_errors', 'showErrors'); 
    166180 
    167181            $responses = array(); 
     
    170184 
    171185                // Check to see if the URL was from an email address, is a directory, etc 
    172                 if (!empty($m[3])) { 
     186                if (!empty($m[2])) { 
    173187                    $this->debug('Invalid Url: URL is either an email or a directory path. (' . $url . ')'); 
    174188                    continue; 
     
    182196 
    183197                // Check to see if the given IP/Host is valid 
    184                 if (!empty($m[2]) and $this->checkValidIP($m[2])) { 
    185                     $this->debug('Invalid Url: ' . $m[2] . ' is not a valid IP address. (' . $url . ')'); 
     198                if (!empty($m[1]) and !$this->checkValidIP($m[1])) { 
     199                    $this->debug('Invalid Url: ' . $m[1] . ' is not a valid IP address. (' . $url . ')'); 
    186200                    continue; 
    187201                } 
    188202 
    189                 // Get the TLD from the host 
    190                 $pos = strrpos($parsed['host'], '.'); 
    191                 $parsed['tld'] = ($pos !== false ? substr($parsed['host'], ($pos+1)) : ''); 
    192  
    193                 // Check to see if the URL has a valid TLD 
    194                 if (is_array($this->tldList) && !in_array(strtolower($parsed['tld']), $this->tldList)) { 
    195                     $this->debug('Invalid Url: ' . $parsed['tld'] . ' is not a supported TLD. (' . $url . ')'); 
    196                     continue; 
     203                // Process TLD if it's not an IP 
     204                if (empty($m[1])) { 
     205                        // Get the TLD from the host 
     206                        $pos = strrpos($parsed['host'], '.'); 
     207                        $parsed['tld'] = ($pos !== false ? substr($parsed['host'], ($pos+1)) : ''); 
     208 
     209                        // Check to see if the URL has a valid TLD 
     210                        if (is_array($this->tldList) && !in_array(strtolower($parsed['tld']), $this->tldList)) { 
     211                            $this->debug('Invalid Url: ' . $parsed['tld'] . ' is not a supported TLD. (' . $url . ')'); 
     212                            continue; 
     213                        } 
    197214                } 
    198215 
     
    549566    */ 
    550567    protected function checkValidIP($ip) { 
    551         $regex = "(?:(?:(?:25[0-5]|2[0-4][0-9]|19[0-1]|19[3-9]|18[0-9]|17[0-1]|17[3-9]". 
    552                  "|1[3-6][0-9]|12[8-9]|12[0-9]|1[0-1][0-9]|1[1-9]|[2-9][0-9]|[0-9". 
    553                  "])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|(?:192\.(?:". 
    554                  "25[0-5]|2[0-4][0-9]|16[0-7]|169|1[0-5][0-9]|1[7-9][0-9]|[1-9][0". 
    555                  "-9]|[0-9]))|(?:172\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|1[0-5]|3[2-9". 
    556                  "]|[4-9][0-9]|[0-9])))\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0". 
    557                  "-9]|[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])"; 
    558  
    559         if(preg_match('/' . $regex . '/im', $ip, $matches)) { 
    560             return true; 
    561         } 
    562  
    563         return false; 
     568        return long2ip(ip2long($ip)) === $ip; 
    564569    } 
    565570 
  • trunk/Phergie/phergie.ini

    r263 r274  
    323323url.title_length = 40 
    324324 
     325; url.detect_schemeless : 
     326;    if set to true, URLs that do not start with http:// such as "example.com" 
     327;    will be detected 
     328;    if empty or unset it defaults to false 
     329url.detect_schemeless = false 
     330 
    325331;----------------------------------------------------------------------------- 
    326332; Weather