| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | require_once 'Phergie/Event/Handler.php'; |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class Phergie_Plugin_Url extends Phergie_Event_Handler |
|---|
| 13 | { |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | protected $format = '%title% [ %link% ]'; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | public function init() |
|---|
| 30 | { |
|---|
| 31 | $format = $this->getIni('format'); |
|---|
| 32 | if ($format) { |
|---|
| 33 | $this->format = $format; |
|---|
| 34 | } |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | public function onPrivmsg() |
|---|
| 45 | { |
|---|
| 46 | |
|---|
| 47 | if (preg_match('#(https?://(?:[a-z0-9_-]+\.)+[a-z]{2,6}[^\s]*)#is', $event->getArgument(1), $m)) { |
|---|
| 48 | $url = rtrim($m[1], '), ]'); |
|---|
| 49 | |
|---|
| 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 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 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 | } |
|---|