| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | require_once 'Phergie/Plugin/Abstract/Command.php'; |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class Phergie_Plugin_UrbanDictionary extends Phergie_Plugin_Abstract_Command |
|---|
| 13 | { |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | public function onDoUd($term) |
|---|
| 21 | { |
|---|
| 22 | $url = 'http://www.urbandictionary.com/define.php?term=' . urlencode($term); |
|---|
| 23 | $contents = file_get_contents($url); |
|---|
| 24 | |
|---|
| 25 | if (strpos($contents, '<i>' . $term . '</i> isn\'t defined') !== false) { |
|---|
| 26 | $url = $this->tinyUrl('http://urbandictionary.com/insert.php?word=' . $term); |
|---|
| 27 | $this->doPrivmsg( |
|---|
| 28 | $this->getSource(), |
|---|
| 29 | $term . ' is not defined yet [ ' . $url . ' ]' |
|---|
| 30 | ); |
|---|
| 31 | } else { |
|---|
| 32 | $start = strpos($contents, '<div class="def_p">'); |
|---|
| 33 | $end = strpos($contents, '<div', $start + 1); |
|---|
| 34 | if ($end === false) { |
|---|
| 35 | $end = strpos($contents, '</div>', $start); |
|---|
| 36 | } |
|---|
| 37 | $contents = substr($contents, $start, $end - $start); |
|---|
| 38 | $contents = html_entity_decode(strip_tags($contents)); |
|---|
| 39 | $contents = $term . ': ' . trim(preg_replace('/[\r\n\t ]+/', ' ', $contents)); |
|---|
| 40 | |
|---|
| 41 | $url = ' [ ' . $this->tinyUrl($url) . ' ]'; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | $max = 445 - strlen($channel) - strlen($url); |
|---|
| 50 | if (strlen($contents) > $max) { |
|---|
| 51 | $contents = substr($contents, 0, $max); |
|---|
| 52 | $end = strrpos($contents, ' '); |
|---|
| 53 | if ($end === false) { |
|---|
| 54 | $end = $max; |
|---|
| 55 | } |
|---|
| 56 | $contents = substr($contents, 0, $end) . '...' . $url; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | $this->doPrivmsg($this->getSource(), $contents); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | } |
|---|