Changeset 119

Show
Ignore:
Timestamp:
03/04/08 04:02:48 (5 years ago)
Author:
tobias382
Message:

Added support for messages containing multiple URLs to the Url plugin (thanks Slynderdale)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/Phergie/Plugin/Url.php

    r111 r119  
    2121    */ 
    2222    protected $format = '%title% [ %link% ]'; 
    23  
    24         /** 
    25          * Saves the last urls parsed to avoid spamming/looping with multiple bots 
    26          * 
    27          * It's saved in a flat array containing (url, tinyurl) couples so both are checked 
    28          * 
    29          * @var array 
    30          */ 
    31         protected $lastUrls = array(); 
    3223 
    3324    /** 
     
    5445    { 
    5546        // URL Match 
    56         if (preg_match('#(https?://(?:[a-z0-9_-]+\.)+[a-z0-9]{1,6}[^\s]*)#is', $this->event->getArgument(1), $m)) { 
    57             $url = rtrim($m[1], '), ]'); 
    58             // Spam check on base URL 
    59                         if(array_search($url, $this->lastUrls) !== false) 
    60                                 return; 
    61             // Convert url 
    62             $tinyUrl = $this->tinyUrl($url); 
    63                         // Spam check on tinyURL 
    64                         if(array_search($tinyUrl, $this->lastUrls) !== false) 
    65                                 return; 
     47        if (preg_match_all('#(https?://(?:[a-z0-9_-]+\.)+[a-z0-9]{1,6}[^\s]*)#is', $this->event->getArgument(1), $matches, PREG_SET_ORDER)) { 
     48            $titleLength = $this->getPluginIni('title_length'); 
     49            foreach ($matches as $m) { 
     50                $url = rtrim($m[1], '), ]'); 
     51                // Convert url 
     52                $tinyUrl = $this->tinyUrl($url); 
    6653 
    67             $titleLength = $this->getPluginIni('title_length'); 
     54                $opts = array('http' => 
     55                    array( 
     56                        'timeout' => 3, 
     57                        'method' => 'GET', 
     58                        'header' => 'Content-type: application/x-www-form-urlencoded', 
     59                        'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12' 
     60                    ) 
     61                ); 
     62                $context = stream_context_create($opts); 
    6863 
    69             $opts = array('http' => 
    70                 array( 
    71                     'timeout' => 5, 
    72                     'method' => 'GET', 
    73                     'header' => 'Content-type: application/x-www-form-urlencoded', 
    74                     'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12' 
    75                 ) 
    76             ); 
    77             $context = stream_context_create($opts); 
    78  
    79             if ($page = @fopen($url, 'r', false, $context)) { 
    80                 $data = stream_get_meta_data($page); 
    81                 foreach ($data['wrapper_data'] as $header) { 
    82                     if (preg_match('/^Content-Type: ([^;]+)/', $header, $match) 
    83                         && ! preg_match('/^text\\/x?html$/', $match[1])) { 
    84                         $title = $match[1]; 
    85                     } 
    86                 } 
    87                 if (!isset ($title)) { 
    88                     $content = ''; 
    89                     while ($chunk = fread($page, 512)) { 
    90                         $content .= $chunk; 
    91                         if (preg_match('#<title[^>]*>([^<]*)#is', $content, $m)) { 
    92                                 $content .= fread($page, 256); 
    93                                 preg_match('#<title[^>]*>([^<]*)#is', $content, $m); 
    94                                 $title = preg_replace('#\s+#', ' ', $m[1]); 
    95                             $title = $this->decode($title, $titleLength); 
    96                             break; 
    97                         } 
    98                         if (preg_match('#</head>|<body#i', $content)) { 
    99                             break; 
     64                if ($page = @fopen($url, 'r', false, $context)) { 
     65                    $data = stream_get_meta_data($page); 
     66                    foreach ($data['wrapper_data'] as $header) { 
     67                        if (preg_match('/^Content-Type: ([^;]+)/', $header, $match) 
     68                            && ! preg_match('/^text\\/x?html$/', $match[1])) { 
     69                            $title = $match[1]; 
    10070                        } 
    10171                    } 
     72                    if (!isset ($title)) { 
     73                        $content = ''; 
     74                        while ($chunk = fread($page, 512)) { 
     75                            $content .= $chunk; 
     76                            if (preg_match('#<title[^>]*>([^<]*)#is', $content, $m)) { 
     77                                $content .= fread($page, 256); 
     78                                preg_match('#<title[^>]*>([^<]*)#is', $content, $m); 
     79                                $title = preg_replace('#\s+#', ' ', $m[1]); 
     80                                $title = $this->decode($title, $titleLength); 
     81                                break; 
     82                            } 
     83                            if (preg_match('#</head>|<body#i', $content)) { 
     84                                break; 
     85                            } 
     86                        } 
     87                    } 
     88                    fclose($page); 
    10289                } 
    103                 fclose($page); 
     90 
     91                if (!isset($title)) { 
     92                    $title = '[ No Title ]'; 
     93                } 
     94 
     95                $this->doPrivmsg( 
     96                    $this->event->getSource(), 
     97                    str_replace( 
     98                        array('%title%', '%link%'), 
     99                        array($title, $tinyUrl), 
     100                        $this->format 
     101                    ) 
     102                ); 
     103 
     104                unset($title, $tinyUrl); 
    104105            } 
    105  
    106             if (!isset($title)) { 
    107                 $title = '[ No Title ]'; 
    108             } 
    109  
    110             $this->doPrivmsg( 
    111                 $this->event->getSource(), 
    112                 str_replace( 
    113                     array('%title%', '%link%'), 
    114                     array($title, $tinyUrl), 
    115                     $this->format 
    116                 ) 
    117             ); 
    118  
    119                         // Remove one url couple if it exceeds 5 couples 
    120                         if (count($this->lastUrls) > 10) { 
    121                                 array_shift($this->lastUrls); 
    122                                 array_shift($this->lastUrls); 
    123                         } 
    124  
    125                         // Add the latest couple 
    126                         $this->lastUrls[] = $url; 
    127                         $this->lastUrls[] = $tinyUrl; 
    128106        } 
    129107    }