root/trunk/Phergie/Plugin/Url.php @ 59

Revision 59, 3.0 KB (checked in by tobias382, 5 years ago)

Unstable commit - driver seems to work, most plugins still need testing
* Modified the streams driver to correctly parse the first argument for

PRIVMSG events into an array

* Added a new debug flag setting to the bundled configuration file
* Moved the debug method from the streams driver to the base driver and

modified it to use the new debug flag setting

* Renamed CtcpAction? methods to Action in order to make logic for

construction and parsing of related requests more compatible with the
same for other commands

* Moved Event_Handler::getEventSource to Event_Request::getSource
* Added Event_Handler::tinyUrl convenience method for converting URLs to

their TinyURL equivalents

* Improved docblock coverage in various classes
* Changed all plugin calls to getConnection()->getNick() to getIni('nick')
* Fixed a memory leak in the Command plugin class
* Added alternate nick support to the Nickserv plugin
* Too many more to name!

Line 
1<?php
2
3/**
4* @see Phergie_Event_Handler
5*/
6require_once 'Phergie/Event/Handler.php';
7
8/**
9* Monitors incoming messages for instances of URLs and responds with messages
10* containing relevant information about detected URLs.
11*/
12class Phergie_Plugin_Url extends Phergie_Event_Handler
13{
14    /**
15    * Links output format
16    *
17    * Can use the variables %title% and %link% in it to display page titles
18    * and links
19    *
20    * @var string
21    */
22    protected $format = '%title% [ %link% ]';
23
24    /**
25    * Initializes settings
26    *
27    * @return void
28    */
29    public function init()
30    {
31        $format = $this->getIni('format');
32        if ($format) {
33            $this->format = $format;
34        }
35    }
36
37    /**
38    * Checks an incoming message for the presence of a URL and, if one is
39    * found, responds with its title if it is an HTML document and the
40    * TinyURL equivalent of its original URL if it meets length requirements.
41    *
42    * @return void
43    */
44    public function onPrivmsg()
45    {
46        // URL Match
47        if (preg_match('#(https?://(?:[a-z0-9_-]+\.)+[a-z]{2,6}[^\s]*)#is', $event->getArgument(1), $m)) {
48            $url = rtrim($m[1], '), ]');
49            // @todo if image or something, output content type
50            $tinyUrl = $this->tinyUrl($url);
51            $title = '';
52            $titleLength = $this->getIni('title_length');
53            if ($page = @fopen($url, 'r')) {
54                $content = '';
55                while ($chunk = fread($page, 512)) {
56                    $content .= $chunk;
57                    if (preg_match('#<title[^>]*>([^<]*)#is', $content, $m)) {
58                        $title = $this->decode($m[1], $titleLength);
59                        break;
60                    }
61                    if (preg_match('#</head>|<body#i', $content)) {
62                        break;
63                    }
64                }
65                fclose($page);
66            }
67
68            $this->doPrivmsg(
69                $this->getSource(),
70                str_replace(
71                    array('%title%', '%link%'),
72                    array($title, $tinyUrl),
73                    $this->format
74                )
75            );
76        }
77    }
78
79    /**
80    * Transliterates a UTF-8 string into corresponding ASCII characters and
81    * truncates and appends an ellipsis to the string if it exceeds a given
82    * length.
83    *
84    * @param string $str String to decode
85    * @param int $trim Maximum string length, optional
86    * @return string
87    */
88    protected function decode($str, $trim=null)
89    {
90        $out = utf8_decode($str);
91        $out = html_entity_decode($out, ENT_QUOTES);
92        $out = strtr($out, 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', 'AAAAAAACEEEEIIIIDNOOOOOOUUUUYPYaaaaaaaceeeeiiiidnoooooouuuuypy');
93        $out = preg_replace('{[^a-z0-9&|"#\'\{\}()§^!°\[\]$*¨µ£%´`~=+:/;.,?><\\ _-]}i', '', $out);
94        if($trim > 0) {
95            $out = substr($out, 0, $trim) . (strlen($out) > $trim ? '...' : '');
96        }
97        return $out;
98    }
99}
Note: See TracBrowser for help on using the browser.