Changeset 200
- Timestamp:
- 03/31/08 03:49:48 (8 months ago)
- Files:
-
- trunk/Phergie/Bot.php (modified) (5 diffs)
- trunk/Phergie/Driver/Abstract.php (modified) (1 diff)
- trunk/Phergie/Plugin/Abstract/Base.php (modified) (13 diffs)
- trunk/Phergie/Plugin/Abstract/Command.php (modified) (5 diffs)
- trunk/Phergie/Plugin/Abstract/Cron.php (modified) (1 diff)
- trunk/Phergie/Plugin/Acronym.php (modified) (6 diffs)
- trunk/Phergie/Plugin/Altnick.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Autojoin.php (modified) (3 diffs)
- trunk/Phergie/Plugin/ChuckNorris.php (modified) (12 diffs)
- trunk/Phergie/Plugin/Convert.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Ctcp.php (modified) (7 diffs)
- trunk/Phergie/Plugin/Daddy.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Debug.php (modified) (5 diffs)
- trunk/Phergie/Plugin/Dice.php (modified) (8 diffs)
- trunk/Phergie/Plugin/Dns.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Drink.php (modified) (5 diffs)
- trunk/Phergie/Plugin/Eval.php (modified) (3 diffs)
- trunk/Phergie/Plugin/FeedTicker.php (modified) (7 diffs)
- trunk/Phergie/Plugin/JoinPart.php (modified) (4 diffs)
- trunk/Phergie/Plugin/Karma.php (modified) (16 diffs)
- trunk/Phergie/Plugin/Lart.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Logging.php (modified) (8 diffs)
- trunk/Phergie/Plugin/Math.php (modified) (2 diffs)
- trunk/Phergie/Plugin/ModuleList.php (modified) (8 diffs)
- trunk/Phergie/Plugin/Nickserv.php (modified) (6 diffs)
- trunk/Phergie/Plugin/Php.php (modified) (1 diff)
- trunk/Phergie/Plugin/Pong.php (modified) (3 diffs)
- trunk/Phergie/Plugin/Puppet.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Quit.php (modified) (4 diffs)
- trunk/Phergie/Plugin/Sed.php (modified) (3 diffs)
- trunk/Phergie/Plugin/Seen.php (modified) (10 diffs)
- trunk/Phergie/Plugin/Set.php (modified) (3 diffs)
- trunk/Phergie/Plugin/Spellcheck.php (modified) (1 diff)
- trunk/Phergie/Plugin/Tld.php (modified) (8 diffs)
- trunk/Phergie/Plugin/Toggle.php (modified) (2 diffs)
- trunk/Phergie/Plugin/UrbanDictionary.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Url.php (modified) (14 diffs)
- trunk/Phergie/Plugin/Users.php (modified) (9 diffs)
- trunk/Phergie/Plugin/Weather.php (modified) (4 diffs)
- trunk/Phergie/phergie.ini (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Phergie/Bot.php
r191 r200 10 10 * Check to see if the version of PHP meets the minimun requirement 11 11 */ 12 if (version_compare("5.1.2", PHP_VERSION, '>')) { 13 trigger_error('Fatal Error: Invalid version of PHP! You\'re using PHP ' . PHP_VERSION . ' while PHP version 5.1+ is required to run Phergie.', E_USER_ERROR); 14 } 12 if (version_compare('5.1.2', PHP_VERSION, '>')) { 13 trigger_error('Fatal error: PHP 5.1.2+ is required, current version: ' . PHP_VERSION, E_USER_ERROR); 15 14 16 15 /** 17 16 * Backwards compatibility check to see if the PHP version is lower than 5.2 18 17 */ 19 if (version_compare("5.2", PHP_VERSION, '>')) { 20 $constants = array( 21 'M_EULER' => '0.57721566490153286061', 22 'M_LNPI' => '1.14472988584940017414', 23 'M_SQRT3' => '1.73205080756887729352', 24 'M_SQRTPI' => '1.77245385090551602729' 25 ); 26 foreach($constants as $constant => $value) { 27 if (!defined($constant)) { 28 define($constant, $value); 29 } 30 } 31 unset($constants, $constant, $value); 32 33 trigger_error('Warning: PHP version is ' . PHP_VERSION . '. PHP version 5.2+ is recommended to run Phergie.', E_USER_WARNING); 34 } 35 36 /** 37 * If this file is being included rather than executed, just terminate 38 */ 39 if ($_SERVER['SCRIPT_NAME'] == __FILE__) { 40 return; 18 } elseif (version_compare('5.2', PHP_VERSION, '>')) { 19 trigger_error('Warning: PHP 5.2+ is recommended, current version: ' . PHP_VERSION, E_USER_WARNING); 41 20 } 42 21 … … 57 36 58 37 /** 59 * Short name for DIRECTORY_SEPARATOR60 *61 * @const string62 */63 define('DS', DIRECTORY_SEPARATOR);64 65 /**66 38 * Path to the directory containing the Phergie directory 67 39 * 68 40 * @const string 69 41 */ 70 define('PHERGIE_DIR', dirname(__FILE__) . D S);71 72 /** 73 * Path to the directory containing the Plugin files74 * 75 * @const string 76 */ 77 define('PHERGIE_PLUGIN_DIR', PHERGIE_DIR . 'Plugin' . D S);78 79 /** 80 * Path to the directory containing the Util files81 * 82 * @const string 83 */ 84 define('PHERGIE_UTIL_DIR', PHERGIE_DIR . 'Util' . D S);85 86 /** 87 * Path to the directory containing the Driver files88 * 89 * @const string 90 */ 91 define('PHERGIE_DRIVER_DIR', PHERGIE_DIR . 'Driver' . D S);92 93 /** 94 * Path to the directory containing the Event files95 * 96 * @const string 97 */ 98 define('PHERGIE_EVENT_DIR', PHERGIE_DIR . 'Event' . D S);42 define('PHERGIE_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR); 43 44 /** 45 * Path to the directory containing the plugins 46 * 47 * @const string 48 */ 49 define('PHERGIE_PLUGIN_DIR', PHERGIE_DIR . 'Plugin' . DIRECTORY_SEPARATOR); 50 51 /** 52 * Path to the directory containing the utilities 53 * 54 * @const string 55 */ 56 define('PHERGIE_UTIL_DIR', PHERGIE_DIR . 'Util' . DIRECTORY_SEPARATOR); 57 58 /** 59 * Path to the directory containing the drivers 60 * 61 * @const string 62 */ 63 define('PHERGIE_DRIVER_DIR', PHERGIE_DIR . 'Driver' . DIRECTORY_SEPARATOR); 64 65 /** 66 * Path to the directory containing the event data structures 67 * 68 * @const string 69 */ 70 define('PHERGIE_EVENT_DIR', PHERGIE_DIR . 'Event' . DIRECTORY_SEPARATOR); 99 71 100 72 /** … … 111 83 */ 112 84 if (strtolower(php_sapi_name()) != 'cli') { 113 trigger_error('Phergie is intended to be run using the CLI SAPI for PHP', E_USER_ERROR);85 trigger_error('Phergie requires the CLI SAPI in order to run', E_USER_ERROR); 114 86 } 115 87 … … 137 109 138 110 /** 139 * Name of the configuration file 111 * Name of the configuration file currently in use 140 112 * 141 113 * @const string … … 151 123 152 124 /** 153 * A class auto loader handler that attempts to find the specified classand154 * load it if it exists else produce a fetal error.155 * 156 * @param string $class The class name to check and attempt to load157 * @return bool Returns true if successful, else produce a fatal error125 * Loader to automate inclusion of classes based on directory structure and 126 * class naming conventions. 127 * 128 * @param string $class Class name to check and attempt to load 129 * @return void 158 130 */ 159 131 function phergieAutoLoader($class) { 160 if (substr(strtolower($class), 0, 8) == 'phergie_') { 161 list (, $type, $name) = explode('_', $class); 162 163 switch (strtolower($type)) { 164 case 'plugin': 165 $file = PHERGIE_PLUGIN_DIR . $name . '.php'; 166 break; 167 case 'util': 168 $file = PHERGIE_UTIL_DIR . $name . '.php'; 169 break; 170 } 171 172 if (file_exists($file) && is_readable($file)) { 173 require_once($file); 174 if (class_exists($class)) { 175 return true; 176 } 177 } 178 } 179 trigger_error('Fatal Error: Could not load class "' . $class . '". Class is either invalid, doesn\'t exist or is not supported by Phergie.', E_USER_ERROR); 180 } 181 spl_autoload_register("phergieAutoLoader"); 182 183 /** 184 * Start a runtime loop, if the bot disconnects and reconnects it will reload 185 * all settings from the ini file, allowing you to modify it without really shutting 186 * it down 187 */ 132 if (strpos(strtolower($class), 'phergie_') === 0) { 133 list (, $type, $name) = explode('_', $class, 3); 134 $name = str_replace('_', DIRECTORY_SEPARATOR, $name); 135 136 switch (strtolower($type)) { 137 case 'driver': 138 case 'event': 139 case 'plugin': 140 case 'util': 141 $file = constant('PHERGIE_' . strtoupper($type) . '_DIR') . $name . '.php'; 142 break; 143 } 144 145 if (file_exists($file) && is_readable($file)) { 146 require_once($file); 147 if (class_exists($class)) { 148 return; 149 } 150 } 151 } 152 trigger_error('Fatal error: Could not load class "' . $class . '"', E_USER_ERROR); 153 } 154 155 spl_autoload_register('phergieAutoLoader'); 156 157 /** 158 * Start a runtime loop that will reload all settings from the configuration 159 * file if the bot disconnects and reconnects, allowing for flushing of the 160 * configuration without a full shutdown of the bot 161 */ 188 162 while (true) { 189 /**190 * Obtain and validate the contents of the configuration file191 */192 $required = array('server', 'username', 'nick');193 $config = parse_ini_file(PHERGIE_INI_PATH);194 195 if (count($config) == 0) {196 trigger_error('Fatal Error: Configuration file inaccessible or empty: ' . $ini, E_USER_ERROR);197 }163 /** 164 * Obtain and validate the contents of the configuration file 165 */ 166 $required = array('server', 'username', 'nick'); 167 $config = parse_ini_file(PHERGIE_INI_PATH); 168 169 if (count($config) == 0) { 170 trigger_error('Fatal error: Configuration file inaccessible or empty: ' . $ini, E_USER_ERROR); 171 } 198 172 199 173 $missing = array(); 200 foreach ($required as $value) { 201 if (empty($config[$value])) { 202 $missing[] = $value; 203 } 204 } 205 if (count($missing) > 0) { 206 trigger_error('Fatal Error: Required configuration settings missing: ' . implode(', ', $missing), E_USER_ERROR); 207 } 208 unset($required, $missing, $value); 209 210 if (empty($config['autojoin.channels'])) { 211 trigger_error('Warning: The setting "autojoin.channels" is empty. Without it, you will have to manually join a channel.', E_USER_WARNING); 212 } 213 214 /** 215 * Set the error reporting value to E_ALL (Minus E_STRICT) and display errors in 216 * the output if debug mode is enabled. 174 foreach ($required as $value) { 175 if (empty($config[$value])) { 176 $missing[] = $value; 177 } 178 } 179 if (count($missing) > 0) { 180 trigger_error('Fatal error: Required configuration settings missing: ' . implode(', ', $missing), E_USER_ERROR); 181 } 182 unset($required, $missing, $value); 183 184 /** 185 * Set error reporting to display errors if debug mode is enabled 217 186 */ 218 187 if ($config['debug']) { 219 error_reporting(E_ALL & ~E_STRICT);188 error_reporting(E_ALL | E_STRICT); 220 189 ini_set('display_errors', true); 221 190 ini_set('ignore_repeated_errors', true); 222 191 } 223 192 224 /** 225 * Configure the client 226 */ 227 if (isset($config['driver'])) { 228 $driver = ucfirst(strtolower($config['driver'])); 229 } else { 230 $driver = 'Streams'; 231 } 232 require_once PHERGIE_DRIVER_DIR . $driver . '.php'; 233 $class = 'Phergie_Driver_' . $driver; 234 $client = new $class(); 235 236 foreach ($config as $setting => $value) { 237 $client->setIni($setting, $value); 238 } 239 240 unset ($setting, $value, $driver, $class); 241 242 /** 243 * Determine which plugins should be loaded 244 */ 245 $all = true; 246 $include = array(); 247 if (!empty($config['plugins']) 248 && preg_match('/(all|none)(?: except (.+))?/ADi', $config['plugins'], $match)) { 249 $all = $match[1] != 'none'; 250 if (isset($match[2])) { 251 $include = array_map('strtolower', preg_split('/[, ]+/', $match[2])); 252 } 253 } 254 255 unset ($config); 256 257 /** 258 * Set up plugins 259 */ 260 $iterator = new DirectoryIterator(PHERGIE_PLUGIN_DIR); 261 $plugins = array(); 262 foreach ($iterator as $entry) { 263 if ($iterator->isFile() && pathinfo($entry, PATHINFO_EXTENSION) == 'php') { 264 $name = basename((string) $entry, '.php'); 265 if ($all xor in_array(strtolower($name), $include)) { 266 $plugins[] = $name; 267 } 268 } 269 } 270 271 ksort($plugins); 272 273 unset ($iterator, $entry, $name, $all, $include); 274 275 foreach ($plugins as $plugin) { 276 require_once PHERGIE_PLUGIN_DIR . $plugin . '.php'; 277 $class = 'Phergie_Plugin_' . $plugin; 278 if (call_user_func(array($class, 'checkDependencies'), $client, $plugins)) { 279 $instance = new $class($client); 280 $client->addPlugin($instance); 281 $client->debug('Loaded ' . $plugin); 282 } else { 283 $client->debug('Unable to load ' . $plugin); 284 } 285 } 286 287 unset ($plugins, $plugin, $class, $instance); 288 289 /** 290 * Execute the event handling loop for the client 291 */ 292 $state = $client->run(); 293 unset($client); 294 295 switch($state) 296 { 297 case Phergie_Driver_Abstract::RETURN_RECONNECT: 298 sleep(1); 299 break; 300 case Phergie_Driver_Abstract::RETURN_KEEPALIVE: 301 sleep(15); 302 break; 303 case Phergie_Driver_Abstract::RETURN_END: 304 break 2; 305 } 306 } 193 /** 194 * Configure the client 195 */ 196 if (isset($config['driver'])) { 197 $driver = ucfirst(strtolower($config['driver'])); 198 } 199 if (!isset($driver) || !file_exists(PHERGIE_DRIVER_DIR . $driver . '.php')) { 200 trigger_error('Driver not specified or not found, defaulting to Streams', E_USER_NOTICE); 201 $driver = 'Streams'; 202 } 203 require_once PHERGIE_DRIVER_DIR . $driver . '.php'; 204 $class = 'Phergie_Driver_' . $driver; 205 $client = new $class(); 206 207 foreach ($config as $setting => $value) { 208 $client->setIni($setting, $value); 209 } 210 211 unset ($setting, $value, $driver, $class); 212 213 /** 214 * Determine which plugins should be loaded 215 */ 216 $all = true; 217 $include = array(); 218 if (!empty($config['plugins']) 219 && preg_match('/(all|none)(?: except (.+))?/ADi', $config['plugins'], $match)) { 220 $all = $match[1] != 'none'; 221 if (isset($match[2])) { 222 $include = array_map('strtolower', preg_split('/[, ]+/', $match[2])); 223 } 224 } 225 226 unset ($config, $match); 227 228 /** 229 * Set up plugins 230 */ 231 $iterator = new DirectoryIterator(PHERGIE_PLUGIN_DIR); 232 $plugins = array(); 233 foreach ($iterator as $entry) { 234 if ($iterator->isFile() && pathinfo($entry, PATHINFO_EXTENSION) == 'php') { 235 $name = basename($entry, '.php'); 236 if ($all xor in_array(strtolower($name), $include)) { 237 $plugins[] = $name; 238 } 239 } 240 } 241 242 ksort($plugins); 243 244 unset ($iterator, $entry, $name, $all, $include); 245 246 foreach ($plugins as $plugin) { 247 require_once PHERGIE_PLUGIN_DIR . $plugin . '.php'; 248 $class = 'Phergie_Plugin_' . $plugin; 249 /** 250 * @todo When PHP 5.3 is a stable release, change this to 251 * $class::checkDependencies($client, $plugins); 252 */ 253 if (call_user_func(array($class, 'checkDependencies'), $client, $plugins)) { 254 $instance = new $class($client); 255 $client->addPlugin($instance); 256 $client->debug('Loaded ' . $plugin); 257 } else { 258 $client->debug('Unable to load ' . $plugin); 259 } 260 } 261 262 unset ($plugins, $plugin, $class, $instance); 263 264 /** 265 * Execute the event handling loop for the client 266 */ 267 $state = $client->run(); 268 unset($client); 269 270 switch($state) 271 { 272 case Phergie_Driver_Abstract::RETURN_RECONNECT: 273 sleep(1); 274 break; 275 case Phergie_Driver_Abstract::RETURN_KEEPALIVE: 276 sleep(15); 277 break; 278 case Phergie_Driver_Abstract::RETURN_END: 279 break 2; 280 } 281 } trunk/Phergie/Driver/Abstract.php
r199 r200 1 1 <?php 2 3 /**4 * @see Phergie_Plugin_Abstract_Base5 */6 require_once PHERGIE_PLUGIN_DIR.'Abstract'.DS.'Base.php';7 2 8 3 /** trunk/Phergie/Plugin/Abstract/Base.php
r199 r200 1 1 <?php 2 3 /**4 * @see Phergie_Driver_Abstract5 */6 require_once PHERGIE_DRIVER_DIR.'Abstract.php';7 8 /**9 * @see Phergie_Event_Request10 */11 require_once PHERGIE_EVENT_DIR.'Request.php';12 13 /**14 * @see Phergie_Event_Response15 */16 require_once PHERGIE_EVENT_DIR.'Response.php';17 2 18 3 /** … … 102 87 final public function __construct(Phergie_Driver_Abstract $client) 103 88 { 104 set_error_handler(array(__CLASS__, 'onBaseError'));89 set_error_handler(array($this, 'onBaseError')); 105 90 $this->client = $client; 106 91 … … 120 105 121 106 /** 122 * Base error handler for PHP errors. This functions called onP HPError for107 * Base error handler for PHP errors. This functions called onPhpError for 123 108 * any clas extending it to give each plugin its own error handler. 124 109 * … … 126 111 */ 127 112 public final function onBaseError($errno, $errstr, $errfile, $errline) { 128 if (!isset($this)) {129 return false;130 }131 return call_user_func_array(array($this, 'onPHPError'), array($errno, $errstr, $errfile, $errline));113 if (!isset($this)) { 114 return false; 115 } 116 return call_user_func_array(array($this, 'onPhpError'), array($errno, $errstr, $errfile, $errline)); 132 117 } 133 118 … … 225 210 { 226 211 if (strlen($url) > 30) { 227 $opts = array('http' =>228 array(229 'timeout' => 4,230 'method' => 'POST',231 'header' => 'Content-type: application/x-www-form-urlencoded',232 'content' => http_build_query(array('url' => $url))233 )234 );235 $context = stream_context_create($opts);236 $tiny = @file_get_contents('http://tinyurl.com/api-create.php', false, $context);212 $opts = array('http' => 213 array( 214 'timeout' => 4, 215 'method' => 'POST', 216 'header' => 'Content-type: application/x-www-form-urlencoded', 217 'content' => http_build_query(array('url' => $url)) 218 ) 219 ); 220 $context = stream_context_create($opts); 221 $tiny = @file_get_contents('http://tinyurl.com/api-create.php', false, $context); 237 222 if (empty($tiny)) { 238 223 $tiny = $url; … … 255 240 */ 256 241 public function decodeTranslit($text, $charSetFrom = 'UTF-8', $charSetTo = 'ISO-8859-1') { 257 $text = html_entity_decode($text, ENT_QUOTES, $charSetFrom);258 if (strpos($text, '&#') !== false) {259 $text = preg_replace('/�*([0-9]+);/me', '$this->codeToUtf(\\1)', $text);260 $text = preg_replace('/�*([a-f0-9]+);/mei', '$this->codeToUtf(hexdec(\\1))', $text);242 $text = html_entity_decode($text, ENT_QUOTES, $charSetFrom); 243 if (strpos($text, '&#') !== false) { 244 $text = preg_replace('/�*([0-9]+);/me', '$this->codeToUtf(\\1)', $text); 245 $text = preg_replace('/�*([a-f0-9]+);/mei', '$this->codeToUtf(hexdec(\\1))', $text); 261 246 } 262 247 263 248 // Use the translit extension if installed else fallback on to basic transliteration 264 249 if (extension_loaded('iconv')) { 265 $text = iconv($charSetFrom, $charSetTo.'//TRANSLIT', $text);250 $text = iconv($charSetFrom, $charSetTo.'//TRANSLIT', $text); 266 251 // Transliteration supprt via the translit extension is still experimental 267 } else if (false && extension_loaded('translit')) {268 $text = transliterate($text, array('han_transliterate', 'diacritical_remove'), $charSetFrom, $charSetTo);269 } else {252 } else if (false && extension_loaded('translit')) { 253 $text = transliterate($text, array('han_transliterate', 'diacritical_remove'), $charSetFrom, $charSetTo); 254 } else { 270 255 $text = strtr($text, 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', 271 256 'AAAAAAACEEEEIIIIDNOOOOOOUUUUYPYaaaaaaaceeeeiiiidnoooooouuuuypy'); 272 257 $text = preg_replace('{[^a-z0-9&|"#\'\{\}()§^!°\[\]$*¨µ£%´`~=+:/;.,?><\\ _-]}i', '', $text); 273 258 $text = utf8_decode($text); 274 }275 276 return $text;259 } 260 261 return $text; 277 262 } 278 263 … … 284 269 */ 285 270 public function codeToUtf($code) { 286 $code = intval($code);271 $code = intval($code); 287 272 switch ($code) { 288 273 // 1 byte, 7 bits 289 274 case 0: 290 275 return chr(0); 291 case ($code & 0x7F):292 return chr($code);293 294 // 2 bytes, 11 bits295 case ($code & 0x7FF):296 return chr(0xC0 | (($code >> 6) & 0x1F)) .276 case ($code & 0x7F): 277 return chr($code); 278 279 // 2 bytes, 11 bits 280 case ($code & 0x7FF): 281 return chr(0xC0 | (($code >> 6) & 0x1F)) . 297 282 chr(0x80 | ($code & 0x3F)); 298 283 299 // 3 bytes, 16 bits300 case ($code & 0xFFFF):301 return chr(0xE0 | (($code >> 12) & 0x0F)) .284 // 3 bytes, 16 bits 285 case ($code & 0xFFFF): 286 return chr(0xE0 | (($code >> 12) & 0x0F)) . 302 287 chr(0x80 | (($code >> 6) & 0x3F)) . 303 288 chr(0x80 | ($code & 0x3F)); 304 289 305 // 4 bytes, 21 bits306 case ($code & 0x1FFFFF):307 return chr(0xF0 | ($code >> 18)) .290 // 4 bytes, 21 bits 291 case ($code & 0x1FFFFF): 292 return chr(0xF0 | ($code >> 18)) . 308 293 chr(0x80 | (($code >> 12) & 0x3F)) . 309 294 chr(0x80 | (($code >> 6) & 0x3F)) . 310 295 chr(0x80 | ($code & 0x3F)); 311 }312 } 313 314 /**315 * Parses a string of command line like arguments and returns them as an array316 * Parses the following:317 * -Strings: "" | "foo" | "foo bar" | "foo \" bar" | foo318 * -Options: --opt | --opt foo | --opt= | --opt=foo | --opt="" | --opt="foo" | --opt="foo bar"319 * @Note: If no parameter is given, the value returned by the option defaults to320 * 1 unless its an empty value such as --opt="" or --opt=321 * -Flags: -flag +flag322 * @Note: Flags are handled using bitwise. 1 = -flag, 2 = +flag, 3 = Both a +flag and -flag323 *324 * The results get stored in an array for the following structure:325 * Array -> strings [] -Array containing all the string matches326 * Array -> commands [command] => value -Array containing all the string matches327 * Array -> flags [flag] => bitwise -Array containing all the string matches328 * Array -> all [strings|commands|flags] -Array containing a list of all the329 * strings, commands and flags330 *331 * @param string $args String of arguments that will be parsed332 * @return array333 */334 public function parseArguments($args) {335 // Strip away multiple spaces336 $args = ' '.preg_replace('#\s+#', ' ', trim($args)).' ';337 338 // Check the args string for agurments339 preg_match_all('{'.340 // String Regex: Matches "" | "foo" | "foo bar" | "foo \" bar" | foo341 '(".*?(?<!\\\)" | [^-+\s]+) | '.342 // Option Regex: Matches --opt | --opt foo | --opt= | --opt=foo | --opt="" | --opt="foo" | --opt="foo bar"343 '(--\w+ (?:=".*?(?<!\\\)" | [=\s][^-+"\s]*)?) | '.344 // Flag Regex: Matches -flag +flag345 '([-+]\w+)'.346 '}xi',347 $args, $matches);348 // Shifts the first group matches element off of matches349 $matches = array_shift($matches);350 351 // Returned data structure352 $data = array(353 'strings' => array(),354 'commands' => array(),355 'flags' => array(),356 'all' => array(357 'strings' => '',358 'commands' => array(),359 'flags' => array()360 )361 );362 363 // Loop through the results364 foreach ($matches as $match) {365 $match = trim($match);366 // Check to see if the current match an an --option367 if (substr($match, 0, 2 ) === '--') {368 $value = preg_split('/[=\s]/', $match, 2);369 $com = substr(array_shift($value), 2);370 $value = trim(join($value));371 // Strip quotes from the option's value372 $realValue = (substr($value, 0, 1) === '"' && substr($value, -1) === '"' || substr($match, -1) === '=');373 if(substr($value, 0, 1) === '"' && substr($value, -1) === '"') {374 $value = trim(substr($value, 1, -1));375 }376 if (!in_array($com, $data['all']['commands']))377 $data['all']['commands'][] = $com;378 $data['commands'][$com] = (!empty($value) || $realValue? str_replace('\"', '"', $value) : true);379 continue;380 }381 382 // Check to see if the current match is a -flag383 if (substr($match, 0, 1) === '-' || substr($match, 0, 1) === '+') {384 $flag = substr($match, 1);385 if (!in_array($flag, $data['all']['flags']))386 $data['all']['flags'][] = $flag;387 // Handle the flags using bitwise operations. 1=-flag, 2=+flag, 3=Both a plus and minus388 $data['flags'][$flag] |= (substr($match, 0, 1) === '-' ? 0x1 : 0x2);389 continue;390 }391 392 // Strip the quotes away from match393 if(substr($match, 0, 1) === '"' && substr($match, -1) === '"') {394 $match = trim(substr($match, 1, -1));395 }396 // The match value isn't a flag or an option so consider it a string397 if (!empty($match))398 $data['strings'][] = str_replace('\"', '"', $match);399 }400 401 $data['all']['strings'] = trim(implode(' ', $data['strings']));402 return $data;403 }296 } 297 } 298 299 /** 300 * Parses a string of command line like arguments and returns them as an array 301 * Parses the following: 302 * -Strings: "" | "foo" | "foo bar" | "foo \" bar" | foo 303 * -Options: --opt | --opt foo | --opt= | --opt=foo | --opt="" | --opt="foo" | --opt="foo bar" 304 * @Note: If no parameter is given, the value returned by the option defaults to 305 * 1 unless its an empty value such as --opt="" or --opt= 306 * -Flags: -flag +flag 307 * @Note: Flags are handled using bitwise. 1 = -flag, 2 = +flag, 3 = Both a +flag and -flag 308 * 309 * The results get stored in an array for the following structure: 310 * Array -> strings [] -Array containing all the string matches 311 * Array -> commands [command] => value -Array containing all the string matches 312 * Array -> flags [flag] => bitwise -Array containing all the string matches 313 * Array -> all [strings|commands|flags] -Array containing a list of all the 314 * strings, commands and flags 315 * 316 * @param string $args String of arguments that will be parsed 317 * @return array 318 */ 319 public function parseArguments($args) { 320 // Strip away multiple spaces 321 $args = ' '.preg_replace('#\s+#', ' ', trim($args)).' '; 322 323 // Check the args string for agurments 324 preg_match_all('{'. 325 // String Regex: Matches "" | "foo" | "foo bar" | "foo \" bar" | foo 326 '(".*?(?<!\\\)" | [^-+\s]+) | '. 327 // Option Regex: Matches --opt | --opt foo | --opt= | --opt=foo | --opt="" | --opt="foo" | --opt="foo bar" 328 '(--\w+ (?:=".*?(?<!\\\)" | [=\s][^-+"\s]*)?) | '. 329 // Flag Regex: Matches -flag +flag 330 '([-+]\w+)'. 331 '}xi', 332 $args, $matches); 333 // Shifts the first group matches element off of matches 334 $matches = array_shift($matches); 335 336 // Returned data structure 337 $data = array( 338 'strings' => array(), 339 'commands' => array(), 340 'flags' => array(), 341 'all' => array( 342 'strings' => '', 343 'commands' => array(), 344 'flags' => array() 345 ) 346 ); 347 348 // Loop through the results 349 foreach ($matches as $match) { 350 $match = trim($match); 351 // Check to see if the current match an an --option 352 if (substr($match, 0, 2 ) === '--') { 353 $value = preg_split('/[=\s]/', $match, 2); 354 $com = substr(array_shift($value), 2); 355 $value = trim(join($value)); 356 // Strip quotes from the option's value 357 $realValue = (substr($value, 0, 1) === '"' && substr($value, -1) === '"' || substr($match, -1) === '='); 358 if(substr($value, 0, 1) === '"' && substr($value, -1) === '"') { 359 $value = trim(substr($value, 1, -1)); 360 } 361 if (!in_array($com, $data['all']['commands'])) 362 $data['all']['commands'][] = $com; 363 $data['commands'][$com] = (!empty($value) || $realValue? str_replace('\"', '"', $value) : true); 364 continue; 365 } 366 367 // Check to see if the current match is a -flag 368 if (substr($match, 0, 1) === '-' || substr($match, 0, 1) === '+') { 369 $flag = substr($match, 1); 370 if (!in_array($flag, $data['all']['flags'])) 371 $data['all']['flags'][] = $flag; 372 // Handle the flags using bitwise operations. 1=-flag, 2=+flag, 3=Both a plus and minus 373 $data['flags'][$flag] |= (substr($match, 0, 1) === '-' ? 0x1 : 0x2); 374 continue; 375 } 376 377 // Strip the quotes away from match 378 if(substr($match, 0, 1) === '"' && substr($match, -1) === '"') { 379 $match = trim(substr($match, 1, -1)); 380 } 381 // The match value isn't a flag or an option so consider it a string 382 if (!empty($match)) 383 $data['strings'][] = str_replace('\"', '"', $match); 384 } 385 386 $data['all']['strings'] = trim(implode(' ', $data['strings'])); 387 return $data; 388 } 404 389 405 390 /** … … 431 416 } 432 417 433 if ($time > 0 || count($return) <= 0) {434 $return[] = $time . 's';435 }418 if ($time > 0 || count($return) <= 0) { 419 $return[] = $time . 's'; 420 } 436 421 437 422 return implode(' ', $return); … … 448 433 */ 449 434 public function pluginLoaded($plugin, $plugins = null, $client = null) { 450 $plugin = trim($plugin);451 if (!empty($plugin)) {452 if (!$client) {453 $client = $this->client;454 }455 if (!is_array($plugins) || count($plugins) <= 0) {456 $plugins = $this->getPluginList();457 }458 $plugins = array_map('strtolower', $plugins);459 if (in_array(strtolower($plugin), $plugins) && class_exists('Phergie_Plugin_' . $plugin) &&460 call_user_func(array('Phergie_Plugin_' . $plugin, 'checkDependencies'), $client, $plugins)) {461 return true;462 }463 }435 $plugin = trim($plugin); 436 if (!empty($plugin)) { 437 if (!$client) { 438 $client = $this->client; 439 } 440 if (!is_array($plugins) || count($plugins) <= 0) { 441 $plugins = $this->getPluginList(); 442 } 443 $plugins = array_map('strtolower', $plugins); 444 if (in_array(strtolower($plugin), $plugins) && class_exists('Phergie_Plugin_' . $plugin) && 445 call_user_func(array('Phergie_Plugin_' . $plugin, 'checkDependencies'), $client, $plugins)) { 446 return true; 447 } 448 } 464 449 return false; 465 450 } … … 475 460 */ 476 461 public static function staticPluginLoaded($plugin, $plugins, $client) { 477 $plugin = trim($plugin);478 if (!empty($plugin) && is_array($plugins)) {479 $plugins = array_map('strtolower', $plugins);480 if (in_array(strtolower($plugin), $plugins) && class_exists('Phergie_Plugin_' . $plugin) &&481 call_user_func(array('Phergie_Plugin_' . $plugin, 'checkDependencies'), $client, $plugins)) {482 return true;483 }484 }462 $plugin = trim($plugin); 463 if (!empty($plugin) && is_array($plugins)) { 464 $plugins = array_map('strtolower', $plugins); 465 if (in_array(strtolower($plugin), $plugins) && class_exists('Phergie_Plugin_' . $plugin) && 466 call_user_func(array('Phergie_Plugin_' . $plugin, 'checkDependencies'), $client, $plugins)) { 467 return true; 468 } 469 } 485 470 return false; 486 471 } … … 499 484 // Check to see if the current class has any admin or ops settings specified 500 485 if (!isset($this->adminList[$c