stream_context_set_params not works with ssh2.sftp wrapper
Asked Answered
H

2

2

I want to use functionality like here. Please check the code bellow

function notify (
    $notification_code,
    $severity,
    $message,
    $message_code,
    $bytes_transferred,
    $bytes_max
) {
    echo "Runned \n";
};

$ctx = stream_context_create();
stream_set_params($ctx, array('notification' => 'notify'));
$ssh_connection = ssh2_connect('myhost');
ssh2_auth_password($ssh_connection, 'login','pass');
$sftp_resource = ssh2_sftp($ssh_connection);
$data = file_get_contents("ssh2.sftp://{$sftp_resource}/path/to/big/file",
            false, $ctx);

I expect that my notify function will be called at least once. Actually, the same code works for ftp wrappers

function notify (
    $notification_code,
    $severity,
    $message,
    $message_code, 
    $bytes_transferred,
    $bytes_max
) {
    echo "Runned \n";
};

$ctx = stream_context_create();
stream_set_params($ctx, array('notification' => 'notify'));
$scheme = 'ftp';
$data = file_get_contents("{scheme}://username:password@host:port/path/to/file",
            false, $ctx);

And it works fine! The notify function is called many times. I try to use sftp wrapper like this

$data = file_get_contents("ssh2.sftp://username:password@host:port/path/to/big/file",
            false, $ctx);

And it isn`t works too. Any ideas?

Honorine answered 12/2, 2013 at 11:36 Comment(3)
I'm not sure but I think that's one of the concepts of ssh. The crypted connection handling is done by the ssh lib, not by php. Therefore no notifications. Will investigate if the said is trueLegaspi
Yep. Seems it should not work for ssh and ssh wrappers(scp, tunnel, sftp, etc)Honorine
Yes. I've tested it too. Btw, I found a segfault in the php-ssh2 extension. :| ... I'm currently looking in the extension code to find out why notification is not called. Either it is by design or just not implementedLegaspi
L
2

The ssh2 extension doesn't support notfication callbacks. I don't know if this is by design or just not implemented, but the extensions code is missing calls to functions like:

From (PHP-5.4.10) /ext/standard/ftp_fopen_wrapper.c, line 573:

php_stream_notify_progress_init(context, 0, file_size);

A workaround, which I've not tested yet might be to use ftps:// (FTP over ssl). It should fit your security needs and - as the code looks like - will support notifications as ftp. In detail, it uses the same urlwrapper as ftp.

Legaspi answered 12/2, 2013 at 13:12 Comment(0)
F
1

Looks like a recent commit to phpseclib's pure PHP SFTP stream wrapper adds support for notifications:

https://github.com/phpseclib/phpseclib/commit/a47c1c39809a18b870b3812ce9ab84c7bd55efdd

Fingerling answered 16/4, 2013 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.