|
47 | 47 | */
|
48 | 48 | use PEAR2\Net\Transmitter\SocketException as SE;
|
49 | 49 |
|
| 50 | +if (PHP_SAPI !== 'cli' && count(get_included_files()) === 1) { |
| 51 | + header('Content-Type: text/plain;charset=UTF-8'); |
| 52 | + echo <<<HEREDOC |
| 53 | +For security reasons, this file can not be ran DIRECTLY, except from the |
| 54 | +command line. It can be included however, even when not using the command line. |
| 55 | +HEREDOC; |
| 56 | + return; |
| 57 | +} |
| 58 | + |
50 | 59 | //If there's no appropriate autoloader, add one
|
51 | 60 | if (!class_exists('PEAR2\Net\RouterOS\Communicator', true)) {
|
| 61 | + $cwd = chdir(__DIR__); |
| 62 | + |
| 63 | + //The composer autoloader from this package. |
| 64 | + //Also matched if the bin-dir is changed to a folder that is directly |
| 65 | + //descended from the composer project root. |
52 | 66 | $autoloader = stream_resolve_include_path('../vendor/autoload.php');
|
53 | 67 | if (false !== $autoloader) {
|
54 | 68 | include_once $autoloader;
|
55 | 69 | } else {
|
56 |
| - $autoloader = stream_resolve_include_path('PEAR2/Autoload.php'); |
| 70 | + //The composer autoloader, when this package is a dependency. |
| 71 | + $autoloader = stream_resolve_include_path( |
| 72 | + (false === ($vendorDir = getenv('COMPOSER_VENDOR_DIR')) |
| 73 | + ? '../../..' |
| 74 | + : $vendorDir) . '/autoload.php' |
| 75 | + ); |
| 76 | + unset($vendorDir); |
57 | 77 | if (false !== $autoloader) {
|
58 | 78 | include_once $autoloader;
|
59 |
| - Autoload::initialize(realpath('../src')); |
60 |
| - Autoload::initialize(realpath('../../Net_Transmitter.git/src')); |
61 |
| - Autoload::initialize(realpath('../../Cache_SHM.git/src')); |
62 |
| - Autoload::initialize(realpath('../../Console_Color.git/src')); |
63 | 79 | } else {
|
64 |
| - fwrite( |
65 |
| - STDERR, |
66 |
| - <<<HEREDOC |
| 80 | + //PEAR2_Autoload, most probably installed globally. |
| 81 | + $autoloader = stream_resolve_include_path('PEAR2/Autoload.php'); |
| 82 | + if (false !== $autoloader) { |
| 83 | + include_once $autoloader; |
| 84 | + Autoload::initialize( |
| 85 | + realpath('../src') |
| 86 | + ); |
| 87 | + Autoload::initialize( |
| 88 | + realpath('../../Net_Transmitter.git/src') |
| 89 | + ); |
| 90 | + Autoload::initialize( |
| 91 | + realpath('../../Cache_SHM.git/src') |
| 92 | + ); |
| 93 | + Autoload::initialize( |
| 94 | + realpath('../../Console_Color.git/src') |
| 95 | + ); |
| 96 | + Autoload::initialize( |
| 97 | + realpath('../../Console_CommandLine.git/src') |
| 98 | + ); |
| 99 | + } else { |
| 100 | + fwrite( |
| 101 | + STDERR, |
| 102 | + <<<HEREDOC |
67 | 103 | No recognized autoloader is available.
|
68 | 104 | Please install this package with Pyrus, PEAR or Composer.
|
69 | 105 | Alternatively, install PEAR2_Autoload, and/or add it to your include_path.
|
70 | 106 | HEREDOC
|
71 |
| - ); |
72 |
| - exit(10); |
| 107 | + ); |
| 108 | + chdir($cwd); |
| 109 | + exit(10); |
| 110 | + } |
73 | 111 | }
|
74 | 112 | }
|
75 |
| - unset($autoloader); |
| 113 | + |
| 114 | + chdir($cwd); |
| 115 | + unset($autoloader, $cwd); |
76 | 116 | }
|
77 | 117 |
|
78 | 118 | // Locate the data dir, in preference as:
|
|
117 | 157 | $cmdParser->displayUsage(13);
|
118 | 158 | }
|
119 | 159 |
|
120 |
| -$cmd->options['colors'] = $cmd->options['colors'] ?: 'auto'; |
121 |
| -$cmd->options['size'] = $cmd->options['size'] ?: 80; |
122 |
| -$cmd->options['commandMode'] = $cmd->options['commandMode'] ?: 's'; |
123 |
| -$cmd->options['replyMode'] = $cmd->options['replyMode'] ?: 's'; |
124 | 160 | $comTimeout = null === $cmd->options['conTime']
|
125 | 161 | ? (null === $cmd->options['time']
|
126 | 162 | ? (int)ini_get('default_socket_timeout')
|
|
151 | 187 | 'NOTE' => '',
|
152 | 188 | '' => ''
|
153 | 189 | );
|
154 |
| -if ('auto' === $cmd->options['colors']) { |
155 |
| - $cmd->options['colors'] = (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' |
| 190 | +if ('auto' === $cmd->options['isColored']) { |
| 191 | + $cmd->options['isColored'] = (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' |
156 | 192 | || getenv('ANSICON_VER') != false) ? 'yes' : 'no';
|
157 | 193 | }
|
158 |
| -if ('yes' === $cmd->options['colors']) { |
| 194 | +if ('yes' === $cmd->options['isColored']) { |
159 | 195 | $c_colors['SENT'] = new Color(
|
160 | 196 | Color\Fonts::BLACK,
|
161 | 197 | Color\Backgrounds::PURPLE
|
|
198 | 234 | fwrite(
|
199 | 235 | STDERR,
|
200 | 236 | "Details: ({$previous->getSocketErrorNumber()}) "
|
201 |
| - . $previous->getSocketErrorMessage() |
| 237 | + . $previous->getSocketErrorMessage() . "\n\n" |
202 | 238 | );
|
203 | 239 | }
|
| 240 | + if ($e instanceof RouterOS\SocketException |
| 241 | + && $e->getCode() === RouterOS\SocketException::CODE_CONNECTION_FAIL |
| 242 | + ) { |
| 243 | + echo <<<HEREDOC |
| 244 | +Possible reasons: |
| 245 | +
|
| 246 | +1. You haven't enabled the API service at RouterOS or you've enabled it on a |
| 247 | +different TCP port. Make sure that the "api" service at "/ip service" is |
| 248 | +enabled, and with that same TCP port (8728 by default or 8729 for "api-ssl"). |
| 249 | +
|
| 250 | +2. You've mistyped the IP and/or port. Check the IP and port you've specified |
| 251 | +are the ones you intended. |
| 252 | +
|
| 253 | +3. The router is not reachable from your web server for some reason. Try to |
| 254 | +reach the router (!!!)from the web server(!!!) by other means (e.g. Winbox, |
| 255 | +ping) using the same IP, and if you're unable to reach it, check the network |
| 256 | +settings on your server, router and any intermediate nodes under your control |
| 257 | +that may affect the connection. |
| 258 | +
|
| 259 | +4. Your web server is configured to forbid that outgoing connection. If you're |
| 260 | +the web server administrator, check your web server's firewall's settings. If |
| 261 | +you're on a hosting plan... Typically, shared hosts block all outgoing |
| 262 | +connections, but it's also possible that only connections to that port are |
| 263 | +blocked. Try to connect to a host on a popular port (21, 80, 443, etc.), and if |
| 264 | +successful, change the API service port to that port. If the connection fails |
| 265 | +even then, ask your host to configure their firewall so as to allow you to make |
| 266 | +outgoing connections to the ip:port you've set the API service on. |
| 267 | +
|
| 268 | +5. The router has a filter/mangle/nat rule that overrides the settings at |
| 269 | +"/ip service". This is a very rare scenario, but if you want to be sure, try to |
| 270 | +disable all rules that may cause such a thing, or (if you can afford it) set up |
| 271 | +a fresh RouterOS in place of the existing one, and see if you can connect to it |
| 272 | +instead. If you still can't connect, such a rule is certainly not the (only) |
| 273 | +reason. |
| 274 | +
|
| 275 | +HEREDOC; |
| 276 | + } |
204 | 277 | return;
|
205 | 278 | }
|
206 | 279 | if (null !== $cmd->args['username']) {
|
|
224 | 297 | allowed to log in from. You can check them at the "address" property
|
225 | 298 | of the user in the "/user" menu.
|
226 | 299 | 4. Mistyped password.
|
227 |
| - If the password contains non-ASCII characters, be careful of your locale. |
| 300 | + Make sure you have spelled it correctly. |
| 301 | + If it contains spaces, don't forget to quote the whole password. |
| 302 | + If it contains non-ASCII characters, be careful of your locale. |
228 | 303 | It must match that of the terminal you set your password on, or you must
|
229 | 304 | type the equivalent code points in your current locale, which may display as
|
230 | 305 | different characters.
|
|
0 commit comments