| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
class Phergie_Plugin_Php extends Phergie_Plugin_Abstract_Command |
|---|
| 11 |
{ |
|---|
| 12 |
|
|---|
| 13 |
* Mapping of function names to corresponding prototypes to serve as an |
|---|
| 14 |
* in-memory cache |
|---|
| 15 |
* |
|---|
| 16 |
* @var array |
|---|
| 17 |
*/ |
|---|
| 18 |
protected $cache; |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
* Set to true by the custom error handler if an HTTP error code has been received |
|---|
| 22 |
* |
|---|
| 23 |
* @var boolean |
|---|
| 24 |
*/ |
|---|
| 25 |
protected $errorStatus = false; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
* Returns whether or not the plugin's dependencies are met. |
|---|
| 29 |
* |
|---|
| 30 |
* @param Phergie_Driver_Abstract $client Client instance |
|---|
| 31 |
* @param array $plugins List of short names for plugins that the |
|---|
| 32 |
* bootstrap file intends to instantiate |
|---|
| 33 |
* @see Phergie_Plugin_Abstract_Base::checkDependencies() |
|---|
| 34 |
* @return bool TRUE if dependencies are met, FALSE otherwise |
|---|
| 35 |
*/ |
|---|
| 36 |
public static function checkDependencies(Phergie_Driver_Abstract $client, array $plugins) |
|---|
| 37 |
{ |
|---|
| 38 |
if (!self::staticPluginLoaded('TinyUrl', $client, $plugins)) { |
|---|
| 39 |
return 'TinyUrl plugin must be enabled'; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
return true; |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
* Initializes an internal cache used to store prototypes obtained from |
|---|
| 47 |
* lookups so as to eliminate the need to perform repeat lookups. |
|---|
| 48 |
* |
|---|
| 49 |
* @return void |
|---|
| 50 |
*/ |
|---|
| 51 |
public function onInit() |
|---|
| 52 |
{ |
|---|
| 53 |
$this->cache = array(); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
* Cleans up the given string and decodes and transliterates a UTF-8 string |
|---|
| 58 |
* into corresponding ASCII characters |
|---|
| 59 |
* |
|---|
| 60 |
* @return string |
|---|
| 61 |
*/ |
|---|
| 62 |
private function decode($str) |
|---|
| 63 |
{ |
|---|
| 64 |
$str = str_replace('—', '-', $str); |
|---|
| 65 |
$str = trim(preg_replace(array('/<[^>]+>/ms', '/\s+/ms'), array( '', ' ' ), $str)); |
|---|
| 66 |
return $this->decodeTranslit($str); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
* Intercepts, processes, and responds with the result of prototype lookup |
|---|
| 71 |
* requests. |
|---|
| 72 |
* |
|---|
| 73 |
* @param string $function Name of the function to look up |
|---|
| 74 |
* @return void |
|---|
| 75 |
*/ |
|---|
| 76 |
public function onDoPhp($function) |
|---|
| 77 |
{ |
|---|
| 78 |
$name = preg_replace(array( '/[\s\(\);]*$/', '/\s+/', '/_/', '/\s/'), array('', ' ', '-', '-'), $function); |
|---|
| 79 |
$name = trim(strtolower($name)); |
|---|
| 80 |
if (!isset($this->cache[$name])) { |
|---|
| 81 |
$tmp = file_get_contents('http://php.net/manual/en/function.' . $name . '.php'); |
|---|
| 82 |
if (!$this->errorStatus && strpos($tmp, '<p class="refpurpose">') !== false) { |
|---|
| 83 |
$contents = '[ ' . Phergie_Plugin_TinyUrl::get('http://php.net/' . $name) . ' ] '; |
|---|
| 84 |
if (preg_match('/<div class="methodsynopsis dc-description">(.*?)<\/div>/mis', $tmp, $m)) { |
|---|
| 85 |
$contents .= $this->decode($m[1]); |
|---|
| 86 |
} |
|---|
| 87 |
if (preg_match('/<p class="refpurpose">(.*?)<\/p>/mis', $tmp, $m)) { |
|---|
| 88 |
$m = explode('-', $this->decode($m[1]), 2); |
|---|
| 89 |
$contents .= ' ' . trim($m[1]); |
|---|
| 90 |
} |
|---|
| 91 |
$contents = trim(str_replace(' ,', ',', $contents)); |
|---|
| 92 |
unset($tmp, $m); |
|---|
| 93 |
} else { |
|---|
| 94 |
$this->errorStatus = false; |
|---|
| 95 |
$contents = false; |
|---|
| 96 |
} |
|---|
| 97 |
$this->cache[$name] = $contents; |
|---|
| 98 |
} |
|---|
| 99 |
if (!empty($this->cache[$name])) { |
|---|
| 100 |
$this->doPrivmsg($this->event->getSource(), $this->cache[$name]); |
|---|
| 101 |
} |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
* Custom error handler meant to handle 404 errors and such |
|---|
| 106 |
*/ |
|---|
| 107 |
public function onPhpError($errno, $errstr, $errfile, $errline) |
|---|
| 108 |
{ |
|---|
| 109 |
if ($errno === E_WARNING) { |
|---|
| 110 |
|
|---|
| 111 |
if (preg_match('{HTTP/1\.[01] ([0-9]{3})}i', $errstr, $m)) { |
|---|
| 112 |
$this->errorStatus = true; |
|---|
| 113 |
$this->debug('PHP Warning: ' . $errstr . 'in ' . $errfile . ' on line ' . $errline); |
|---|
| 114 |
return true; |
|---|
| 115 |
|
|---|
| 116 |
} else if (stripos($errstr, 'failed to open stream') !== false || |
|---|
| 117 |
stripos($errstr, 'HTTP request failed') !== false || |
|---|
| 118 |
stripos($errstr, 'unable to connect to') !== false) { |
|---|
| 119 |
$this->errorStatus = true; |
|---|
| 120 |
$this->debug('PHP Warning: ' . $errstr . 'in ' . $errfile . ' on line ' . $errline); |
|---|
| 121 |
return true; |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|
| 124 |
return false; |
|---|
| 125 |
} |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|