katie hudson
katie hudson

Reputation: 2893

PHP Composer using define

I am using phpseclib to do some SFTP stuff. At the moment I can't login, and I am trying to find out why. In the docs, it states I should do something like this

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include 'Net/SFTP.php';

define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);

$sftp = new NET_SFTP('***');
if(!$sftp->login('***', '***')) {
    print_r($sftp->getSFTPErrors());
}

So, I need to define the type of logging in order to print out the SFTPErrors.

I am doing things differently than the above because I am using composer. So as usual, I load autoload.php. Instead of include, I make use of use e.g.

use phpseclib\Net\SFTP;

I then proceed to do something like the following

define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);

$sftp = new SFTP($config::SFTP_SERVER);

if(!$sftp->login($config::SFTP_USER, $config::SFTP_PASSWORD)) {
    print_r($sftp->getSFTPErrors());
    exit('Login Failed');
}

If I do this however, I get the output

Notice:  Use of undefined constant NET_SFTP_LOG_COMPLEX - assumed 'NET_SFTP_LOG_COMPLEX' in ...
Array
(
)
Login Failed

So it appears that with composer, I cant define a constant in the same way, and the print out of the errors produces an empty array.

So, how can I define this constant in my composer project?

Thanks

Upvotes: 0

Views: 1172

Answers (1)

neubert
neubert

Reputation: 16782

A few of things.

If you're using the namespaced version of PHP (as evidenced by your use phpseclib\Net\SFTP;) then you're using the 2.0 branch. The documentation on the website is for the 1.0 branch. For the 2.0 branch you need to do as Félix Saparelli suggested - SSH2::LOG_COMPLEX.

That said, logging isn't going to show SFTP errors. Logging shows you the raw packets. Here's an example of what the logs produce:

http://phpseclib.sourceforge.net/ssh/log.txt

You get these logs by doing $ssh->getLogs().

For the errors you don't need to enable anything - it places any errors it receives from the server into an array that it's returning to you. phpseclib does this automatically and this behavior cannot be disabled.

Also, $sftp->getSFTPErrors() is great for SFTP errors but at the login process you might be getting SSH errors and not SFTP errors. You'd get SSH errors by doing $sftp->getErrors(). The thing is... SFTP operates in a higher layer than SSH. SSH won't succeed if TCP/IP can't make a connection and SFTP won't succeed if SSH can't make a connection. So per that you ought to be checking all the layers.

Finally, it's quite possible the failure is happening for reasons for which errors would not be returned. Maybe the server requires compression, which phpseclib doesn't support. eg.

http://www.frostjedi.com/phpbb3/viewtopic.php?p=221481#p221481

I also don't know if you'd get an error if the key or password you were using was simply invalid.

Really, there could be any number of causes for an inability to connect. You could be using selinux which, by default, disables outbound connections from PHP scripts running on port 80, there could be a routing issue, etc (all of these affect fsockopen, but, in all, there are just a lot of potential causes for failure).

Overall, I'd say you're on the right track with the logs. But do $sftp->getLog() instead of $sftp->getSFTPErrors() and then include the logs in your post.

Upvotes: 1

Related Questions