| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | if ($_SERVER['SCRIPT_NAME'] == __FILE__) { |
|---|
| 13 | return; |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | define('PHERGIE_VERSION', '1.0.2'); |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | define('PHERGIE_INI', 'phergie.ini'); |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | define('PHERGIE_DIR', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | set_include_path( |
|---|
| 42 | get_include_path() |
|---|
| 43 | . PATH_SEPARATOR . |
|---|
| 44 | PHERGIE_DIR |
|---|
| 45 | ); |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | if (php_sapi_name() != 'cli') { |
|---|
| 51 | trigger_error('Phergie is intended to be run using the CLI SAPI for PHP', E_USER_ERROR); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | set_time_limit(0); |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | if (!ini_get('register_argc_argv')) { |
|---|
| 63 | echo 'The register_argc_argv setting in php.ini is disabled, defaulting to ' . PHERGIE_INI . "\n"; |
|---|
| 64 | $ini = PHERGIE_INI; |
|---|
| 65 | } else if ($argc == 1) { |
|---|
| 66 | echo 'No configuration file specified, defaulting to ' . PHERGIE_INI . "\n"; |
|---|
| 67 | $ini = PHERGIE_INI; |
|---|
| 68 | } else { |
|---|
| 69 | $ini = $argv[1]; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | $required = array('server', 'username', 'nick'); |
|---|
| 76 | $config = @parse_ini_file(PHERGIE_DIR . 'Phergie' . DIRECTORY_SEPARATOR . $ini); |
|---|
| 77 | |
|---|
| 78 | if (count($config) == 0) { |
|---|
| 79 | echo 'Configuration file inaccessible or empty: ' . $ini . "\n"; |
|---|
| 80 | return; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | foreach ($required as $setting) { |
|---|
| 84 | if (!isset($config[$setting]) || empty($config[$setting])) { |
|---|
| 85 | echo 'Required configuration setting missing: ' . $setting . "\n"; |
|---|
| 86 | return; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | if (isset($config['driver'])) { |
|---|
| 94 | $driver = ucfirst(strtolower($config['driver'])); |
|---|
| 95 | } else { |
|---|
| 96 | $driver = 'Streams'; |
|---|
| 97 | } |
|---|
| 98 | require_once 'Phergie/Driver/' . $driver . '.php'; |
|---|
| 99 | $class = 'Phergie_Driver_' . $driver; |
|---|
| 100 | $client = new $class(); |
|---|
| 101 | |
|---|
| 102 | foreach ($config as $setting => $value) { |
|---|
| 103 | $client->setIni($setting, $value); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | $all = true; |
|---|
| 110 | $include = array(); |
|---|
| 111 | if (isset($config['plugins']) |
|---|
| 112 | && preg_match('/(all|none)(?: except (.+))?/ADi', $config['plugins'], $match)) { |
|---|
| 113 | $all = $match[1] != 'none'; |
|---|
| 114 | if (isset($match[2])) { |
|---|
| 115 | $include = array_map('strtolower', preg_split('/[, ]+/', $match[2])); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | unset($required, $config, $setting, $driver, $class); |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | $iterator = new DirectoryIterator(PHERGIE_DIR . '/Phergie/Plugin'); |
|---|
| 128 | foreach ($iterator as $entry) { |
|---|
| 129 | if ($iterator->isFile() |
|---|
| 130 | && substr($entry, -4) == '.php' |
|---|
| 131 | && ($all xor in_array(strtolower(substr($entry, 0, -4)), $include))) { |
|---|
| 132 | require_once 'Phergie/Plugin/' . $entry; |
|---|
| 133 | $class = 'Phergie_Plugin_' . str_replace('.php', '', $entry); |
|---|
| 134 | $instance = new $class($client); |
|---|
| 135 | $client->addPlugin($instance); |
|---|
| 136 | $client->debug('Loaded ' . $instance->getName()); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | unset($iterator, $class, $entry, $all, $exclude, $reflector); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | $client->run(); |
|---|