diff --git a/phpseclib/Crypt/DES.php b/phpseclib/Crypt/DES.php index 0d6dd00e..6a6d3389 100644 --- a/phpseclib/Crypt/DES.php +++ b/phpseclib/Crypt/DES.php @@ -937,7 +937,7 @@ class Crypt_DES { if (($length & 7) == 0) { return $text; } else { - user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE); + $this->_handle_error("The plaintext's length ($length) is not a multiple of the block size (8)"); $this->padding = true; } } @@ -1289,6 +1289,24 @@ class Crypt_DES { $string = substr($string, $index); return $substr; } + + /** + * Error Handler + * + * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined. + * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions. + * + * @param String $string + * @access private + */ + function _handle_error($err_msg) { + if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) { + $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception'; + throw(new $class($err_msg)); + } else { + user_error($err_msg); + } + } } // vim: ts=4:sw=4:et: diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 98f6d15f..df090857 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -1746,7 +1746,7 @@ class Crypt_RSA { { $x = $x->toBytes(); if (strlen($x) > $xLen) { - user_error('Integer too large', E_USER_NOTICE); + $this->_handle_error('Integer too large'); return false; } return str_pad($x, $xLen, chr(0), STR_PAD_LEFT); @@ -1908,7 +1908,7 @@ class Crypt_RSA { function _rsaep($m) { if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) { - user_error('Message representative out of range', E_USER_NOTICE); + $this->_handle_error('Message representative out of range'); return false; } return $this->_exponentiate($m); @@ -1926,7 +1926,7 @@ class Crypt_RSA { function _rsadp($c) { if ($c->compare($this->zero) < 0 || $c->compare($this->modulus) > 0) { - user_error('Ciphertext representative out of range', E_USER_NOTICE); + $this->_handle_error('Ciphertext representative out of range'); return false; } return $this->_exponentiate($c); @@ -1944,7 +1944,7 @@ class Crypt_RSA { function _rsasp1($m) { if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) { - user_error('Message representative out of range', E_USER_NOTICE); + $this->_handle_error('Message representative out of range'); return false; } return $this->_exponentiate($m); @@ -1962,7 +1962,7 @@ class Crypt_RSA { function _rsavp1($s) { if ($s->compare($this->zero) < 0 || $s->compare($this->modulus) > 0) { - user_error('Signature representative out of range', E_USER_NOTICE); + $this->_handle_error('Signature representative out of range'); return false; } return $this->_exponentiate($s); @@ -2013,7 +2013,7 @@ class Crypt_RSA { // be output. if ($mLen > $this->k - 2 * $this->hLen - 2) { - user_error('Message too long', E_USER_NOTICE); + $this->_handle_error('Message too long'); return false; } @@ -2074,7 +2074,7 @@ class Crypt_RSA { // be output. if (strlen($c) != $this->k || $this->k < 2 * $this->hLen + 2) { - user_error('Decryption error', E_USER_NOTICE); + $this->_handle_error('Decryption error'); return false; } @@ -2083,7 +2083,7 @@ class Crypt_RSA { $c = $this->_os2ip($c); $m = $this->_rsadp($c); if ($m === false) { - user_error('Decryption error', E_USER_NOTICE); + $this->_handle_error('Decryption error'); return false; } $em = $this->_i2osp($m, $this->k); @@ -2101,12 +2101,12 @@ class Crypt_RSA { $lHash2 = substr($db, 0, $this->hLen); $m = substr($db, $this->hLen); if ($lHash != $lHash2) { - user_error('Decryption error', E_USER_NOTICE); + $this->_handle_error('Decryption error'); return false; } $m = ltrim($m, chr(0)); if (ord($m[0]) != 1) { - user_error('Decryption error', E_USER_NOTICE); + $this->_handle_error('Decryption error'); return false; } @@ -2131,7 +2131,7 @@ class Crypt_RSA { // Length checking if ($mLen > $this->k - 11) { - user_error('Message too long', E_USER_NOTICE); + $this->_handle_error('Message too long'); return false; } @@ -2175,7 +2175,7 @@ class Crypt_RSA { // Length checking if (strlen($c) != $this->k) { // or if k < 11 - user_error('Decryption error', E_USER_NOTICE); + $this->_handle_error('Decryption error'); return false; } @@ -2185,7 +2185,7 @@ class Crypt_RSA { $m = $this->_rsadp($c); if ($m === false) { - user_error('Decryption error', E_USER_NOTICE); + $this->_handle_error('Decryption error'); return false; } $em = $this->_i2osp($m, $this->k); @@ -2193,7 +2193,7 @@ class Crypt_RSA { // EME-PKCS1-v1_5 decoding if (ord($em[0]) != 0 || ord($em[1]) > 2) { - user_error('Decryption error', E_USER_NOTICE); + $this->_handle_error('Decryption error'); return false; } @@ -2201,7 +2201,7 @@ class Crypt_RSA { $m = substr($em, strlen($ps) + 3); if (strlen($ps) < 8) { - user_error('Decryption error', E_USER_NOTICE); + $this->_handle_error('Decryption error'); return false; } @@ -2229,7 +2229,7 @@ class Crypt_RSA { $mHash = $this->hash->hash($m); if ($emLen < $this->hLen + $sLen + 2) { - user_error('Encoding error', E_USER_NOTICE); + $this->_handle_error('Encoding error'); return false; } @@ -2334,7 +2334,7 @@ class Crypt_RSA { // Length checking if (strlen($s) != $this->k) { - user_error('Invalid signature', E_USER_NOTICE); + $this->_handle_error('Invalid signature'); return false; } @@ -2345,12 +2345,12 @@ class Crypt_RSA { $s2 = $this->_os2ip($s); $m2 = $this->_rsavp1($s2); if ($m2 === false) { - user_error('Invalid signature', E_USER_NOTICE); + $this->_handle_error('Invalid signature'); return false; } $em = $this->_i2osp($m2, $modBits >> 3); if ($em === false) { - user_error('Invalid signature', E_USER_NOTICE); + $this->_handle_error('Invalid signature'); return false; } @@ -2400,7 +2400,7 @@ class Crypt_RSA { $tLen = strlen($t); if ($emLen < $tLen + 11) { - user_error('Intended encoded message length too short', E_USER_NOTICE); + $this->_handle_error('Intended encoded message length too short'); return false; } @@ -2426,7 +2426,7 @@ class Crypt_RSA { $em = $this->_emsa_pkcs1_v1_5_encode($m, $this->k); if ($em === false) { - user_error('RSA modulus too short', E_USER_NOTICE); + $this->_handle_error('RSA modulus too short'); return false; } @@ -2455,7 +2455,7 @@ class Crypt_RSA { // Length checking if (strlen($s) != $this->k) { - user_error('Invalid signature', E_USER_NOTICE); + $this->_handle_error('Invalid signature'); return false; } @@ -2464,12 +2464,12 @@ class Crypt_RSA { $s = $this->_os2ip($s); $m2 = $this->_rsavp1($s); if ($m2 === false) { - user_error('Invalid signature', E_USER_NOTICE); + $this->_handle_error('Invalid signature'); return false; } $em = $this->_i2osp($m2, $this->k); if ($em === false) { - user_error('Invalid signature', E_USER_NOTICE); + $this->_handle_error('Invalid signature'); return false; } @@ -2477,7 +2477,7 @@ class Crypt_RSA { $em2 = $this->_emsa_pkcs1_v1_5_encode($m, $this->k); if ($em2 === false) { - user_error('RSA modulus too short', E_USER_NOTICE); + $this->_handle_error('RSA modulus too short'); return false; } @@ -2639,4 +2639,22 @@ class Crypt_RSA { return $this->_rsassa_pss_verify($message, $signature); } } + + /** + * Error Handler + * + * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined. + * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions. + * + * @param String $string + * @access private + */ + function _handle_error($err_msg) { + if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) { + $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception'; + throw(new $class($err_msg)); + } else { + $this->_handle_error($err_msg); + } + } } \ No newline at end of file diff --git a/phpseclib/Crypt/Rijndael.php b/phpseclib/Crypt/Rijndael.php index e7f17a6f..71155c89 100644 --- a/phpseclib/Crypt/Rijndael.php +++ b/phpseclib/Crypt/Rijndael.php @@ -582,7 +582,7 @@ class Crypt_Rijndael { * * Depending on what $method is set to, setPassword()'s (optional) parameters are as follows: * {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2}: - * $hash, $salt, $count + * $hash, $salt, $method * Set $dkLen by calling setKeyLength() * * @param String $password @@ -601,7 +601,7 @@ class Crypt_Rijndael { } // WPA and WPA use the SSID as the salt if (!isset($salt)) { - $salt = 'phpseclib/salt'; + $salt = 'phpseclib'; } // RFC2898#section-4.2 uses 1,000 iterations by default // WPA and WPA2 use 4,096. @@ -1365,7 +1365,7 @@ class Crypt_Rijndael { if ($length % $this->block_size == 0) { return $text; } else { - user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})", E_USER_NOTICE); + $this->_handle_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})"); $this->padding = true; } } @@ -1472,7 +1472,25 @@ class Crypt_Rijndael { $string = substr($string, $index); return $substr; } + + /** + * Error Handler + * + * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined. + * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions. + * + * @param String $string + * @access private + */ + function _handle_error($err_msg) { + if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) { + $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception'; + throw(new $class($err_msg)); + } else { + user_error($err_msg); + } + } } // vim: ts=4:sw=4:et: -// vim6: fdl=1: +// vim6: fdl=1: \ No newline at end of file diff --git a/phpseclib/File/ANSI.php b/phpseclib/File/ANSI.php index 29ad949e..b601e222 100644 --- a/phpseclib/File/ANSI.php +++ b/phpseclib/File/ANSI.php @@ -409,7 +409,7 @@ class File_ANSI { case 47: $back = 'white'; break; default: - user_error('Unsupported attribute: ' . $mod); + $this->_handle_error('Unsupported attribute: ' . $mod); $this->ansi = ''; break 2; } @@ -537,4 +537,22 @@ class File_ANSI { return '
' . $scrollback . ''; } + + /** + * Error Handler + * + * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined. + * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions. + * + * @param String $string + * @access private + */ + function _handle_error($err_msg) { + if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) { + $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception'; + throw(new $class($err_msg)); + } else { + $this->_handle_error($err_msg); + } + } } diff --git a/phpseclib/File/ASN1.php b/phpseclib/File/ASN1.php index 0ed044fd..dcc8da45 100644 --- a/phpseclib/File/ASN1.php +++ b/phpseclib/File/ASN1.php @@ -955,7 +955,7 @@ class File_ASN1 { case FILE_ASN1_TYPE_OBJECT_IDENTIFIER: $oid = preg_match('#(?:\d+\.)+#', $source) ? $source : array_search($source, $this->oids); if ($oid === false) { - user_error('Invalid OID', E_USER_NOTICE); + $this->_handle_error('Invalid OID'); return false; } $value = ''; @@ -1008,7 +1008,7 @@ class File_ASN1 { $filters = $filters[$part]; } if ($filters === false) { - user_error('No filters defined for ' . implode('/', $loc), E_USER_NOTICE); + $this->_handle_error('No filters defined for ' . implode('/', $loc)); return false; } return $this->_encode_der($source, $filters + $mapping); @@ -1032,7 +1032,7 @@ class File_ASN1 { $value = $source ? "\xFF" : "\x00"; break; default: - user_error('Mapping provides no type definition for ' . implode('/', $this->location), E_USER_NOTICE); + $this->_handle_error('Mapping provides no type definition for ' . implode('/', $this->location)); return false; } @@ -1270,4 +1270,22 @@ class File_ASN1 { } return $out; } + + /** + * Error Handler + * + * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined. + * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions. + * + * @param String $string + * @access private + */ + function _handle_error($err_msg) { + if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) { + $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception'; + throw(new $class($err_msg)); + } else { + $this->_handle_error($err_msg); + } + } } diff --git a/phpseclib/File/X509.php b/phpseclib/File/X509.php index 2f0ce3c1..7e1e6b8d 100644 --- a/phpseclib/File/X509.php +++ b/phpseclib/File/X509.php @@ -1569,7 +1569,7 @@ class File_X509 { $map = $this->_getMapping($id); if (is_bool($map)) { if (!$map) { - user_error($id . ' is not a currently supported extension', E_USER_NOTICE); + $this->_handle_error($id . ' is not a currently supported extension'); unset($extensions[$i]); } } else { @@ -3839,4 +3839,22 @@ class File_X509 { return false; } + + /** + * Error Handler + * + * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined. + * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions. + * + * @param String $string + * @access private + */ + function _handle_error($err_msg) { + if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) { + $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception'; + throw(new $class($err_msg)); + } else { + $this->_handle_error($err_msg); + } + } } diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index fc2bf2e9..3fb149df 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -399,7 +399,7 @@ class Net_SFTP extends Net_SSH2 { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_VERSION) { - user_error('Expected SSH_FXP_VERSION', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_VERSION'); return false; } @@ -588,7 +588,7 @@ class Net_SFTP extends Net_SSH2 { $this->_logError($response); return false; default: - user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS'); return false; } @@ -645,7 +645,7 @@ class Net_SFTP extends Net_SSH2 { $this->_logError($response); return false; default: - user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } @@ -655,7 +655,7 @@ class Net_SFTP extends Net_SSH2 { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } @@ -731,7 +731,7 @@ class Net_SFTP extends Net_SSH2 { $this->_logError($response); return false; default: - user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } @@ -780,7 +780,7 @@ class Net_SFTP extends Net_SSH2 { } break 2; default: - user_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_NAME or SSH_FXP_STATUS'); return false; } } @@ -793,7 +793,7 @@ class Net_SFTP extends Net_SSH2 { // -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.3 $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } @@ -996,7 +996,7 @@ class Net_SFTP extends Net_SSH2 { return false; } - user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS'); return false; } @@ -1089,7 +1089,7 @@ class Net_SFTP extends Net_SSH2 { */ $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } @@ -1116,7 +1116,7 @@ class Net_SFTP extends Net_SSH2 { return false; } - user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS'); return false; } @@ -1249,7 +1249,7 @@ class Net_SFTP extends Net_SSH2 { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } @@ -1288,7 +1288,7 @@ class Net_SFTP extends Net_SSH2 { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } @@ -1365,7 +1365,7 @@ class Net_SFTP extends Net_SSH2 { $this->_logError($response); return false; default: - user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } @@ -1374,7 +1374,7 @@ class Net_SFTP extends Net_SSH2 { // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3 if ($mode & NET_SFTP_LOCAL_FILE) { if (!is_file($data)) { - user_error("$data is not a valid file", E_USER_NOTICE); + $this->_handle_error("$data is not a valid file"); return false; } $fp = @fopen($data, 'rb'); @@ -1425,7 +1425,7 @@ class Net_SFTP extends Net_SSH2 { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } @@ -1453,7 +1453,7 @@ class Net_SFTP extends Net_SSH2 { while ($i--) { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } @@ -1504,7 +1504,7 @@ class Net_SFTP extends Net_SSH2 { $this->_logError($response); return false; default: - user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); return false; } @@ -1542,7 +1542,7 @@ class Net_SFTP extends Net_SSH2 { $this->_logError($response); break 2; default: - user_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS'); if ($local_file !== false) { fclose($fp); } @@ -1560,7 +1560,7 @@ class Net_SFTP extends Net_SSH2 { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } @@ -1604,7 +1604,7 @@ class Net_SFTP extends Net_SSH2 { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } @@ -1723,7 +1723,7 @@ class Net_SFTP extends Net_SSH2 { $response = $this->_get_sftp_packet(); if ($this->packet_type != NET_SFTP_STATUS) { - user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_FXP_STATUS'); return false; } diff --git a/phpseclib/Net/SSH1.php b/phpseclib/Net/SSH1.php index 67e09652..b6a5e652 100644 --- a/phpseclib/Net/SSH1.php +++ b/phpseclib/Net/SSH1.php @@ -467,7 +467,7 @@ class Net_SSH1 { $this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout); if (!$this->fsock) { - user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"), E_USER_NOTICE); + $this->_handle_error(rtrim("Cannot connect to $host. Error $errno. $errstr")); return; } @@ -484,11 +484,11 @@ class Net_SSH1 { } if (!preg_match('#SSH-([0-9\.]+)-(.+)#', $init_line, $parts)) { - user_error('Can only connect to SSH servers', E_USER_NOTICE); + $this->_handle_error('Can only connect to SSH servers'); return; } if ($parts[1][0] != 1) { - user_error("Cannot connect to SSH $parts[1] servers", E_USER_NOTICE); + $this->_handle_error("Cannot connect to SSH $parts[1] servers"); return; } @@ -496,7 +496,7 @@ class Net_SSH1 { $response = $this->_get_binary_packet(); if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) { - user_error('Expected SSH_SMSG_PUBLIC_KEY', E_USER_NOTICE); + $this->_handle_error('Expected SSH_SMSG_PUBLIC_KEY'); return; } @@ -584,7 +584,7 @@ class Net_SSH1 { $data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0); if (!$this->_send_binary_packet($data)) { - user_error('Error sending SSH_CMSG_SESSION_KEY', E_USER_NOTICE); + $this->_handle_error('Error sending SSH_CMSG_SESSION_KEY'); return; } @@ -614,7 +614,7 @@ class Net_SSH1 { $response = $this->_get_binary_packet(); if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) { - user_error('Expected SSH_SMSG_SUCCESS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_SMSG_SUCCESS'); return; } @@ -638,7 +638,7 @@ class Net_SSH1 { $data = pack('CNa*', NET_SSH1_CMSG_USER, strlen($username), $username); if (!$this->_send_binary_packet($data)) { - user_error('Error sending SSH_CMSG_USER', E_USER_NOTICE); + $this->_handle_error('Error sending SSH_CMSG_USER'); return false; } @@ -648,14 +648,14 @@ class Net_SSH1 { $this->bitmap |= NET_SSH1_MASK_LOGIN; return true; } else if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_FAILURE) { - user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE', E_USER_NOTICE); + $this->_handle_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE'); return false; } $data = pack('CNa*', NET_SSH1_CMSG_AUTH_PASSWORD, strlen($password), $password); if (!$this->_send_binary_packet($data)) { - user_error('Error sending SSH_CMSG_AUTH_PASSWORD', E_USER_NOTICE); + $this->_handle_error('Error sending SSH_CMSG_AUTH_PASSWORD'); return false; } @@ -673,7 +673,7 @@ class Net_SSH1 { } else if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_FAILURE) { return false; } else { - user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE', E_USER_NOTICE); + $this->_handle_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE'); return false; } } @@ -701,14 +701,14 @@ class Net_SSH1 { function exec($cmd, $block = true) { if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) { - user_error('Operation disallowed prior to login()', E_USER_NOTICE); + $this->_handle_error('Operation disallowed prior to login()'); return false; } $data = pack('CNa*', NET_SSH1_CMSG_EXEC_CMD, strlen($cmd), $cmd); if (!$this->_send_binary_packet($data)) { - user_error('Error sending SSH_CMSG_EXEC_CMD', E_USER_NOTICE); + $this->_handle_error('Error sending SSH_CMSG_EXEC_CMD'); return false; } @@ -753,21 +753,21 @@ class Net_SSH1 { $data = pack('CNa*N4C', NET_SSH1_CMSG_REQUEST_PTY, strlen('vt100'), 'vt100', 24, 80, 0, 0, NET_SSH1_TTY_OP_END); if (!$this->_send_binary_packet($data)) { - user_error('Error sending SSH_CMSG_REQUEST_PTY', E_USER_NOTICE); + $this->_handle_error('Error sending SSH_CMSG_REQUEST_PTY'); return false; } $response = $this->_get_binary_packet(); if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) { - user_error('Expected SSH_SMSG_SUCCESS', E_USER_NOTICE); + $this->_handle_error('Expected SSH_SMSG_SUCCESS'); return false; } $data = pack('C', NET_SSH1_CMSG_EXEC_SHELL); if (!$this->_send_binary_packet($data)) { - user_error('Error sending SSH_CMSG_EXEC_SHELL', E_USER_NOTICE); + $this->_handle_error('Error sending SSH_CMSG_EXEC_SHELL'); return false; } @@ -806,12 +806,12 @@ class Net_SSH1 { function read($expect, $mode = NET_SSH1_READ_SIMPLE) { if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) { - user_error('Operation disallowed prior to login()', E_USER_NOTICE); + $this->_handle_error('Operation disallowed prior to login()'); return false; } if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) { - user_error('Unable to initiate an interactive shell session', E_USER_NOTICE); + $this->_handle_error('Unable to initiate an interactive shell session'); return false; } @@ -841,19 +841,19 @@ class Net_SSH1 { function interactiveWrite($cmd) { if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) { - user_error('Operation disallowed prior to login()', E_USER_NOTICE); + $this->_handle_error('Operation disallowed prior to login()'); return false; } if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) { - user_error('Unable to initiate an interactive shell session', E_USER_NOTICE); + $this->_handle_error('Unable to initiate an interactive shell session'); return false; } $data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($cmd), $cmd); if (!$this->_send_binary_packet($data)) { - user_error('Error sending SSH_CMSG_STDIN', E_USER_NOTICE); + $this->_handle_error('Error sending SSH_CMSG_STDIN'); return false; } @@ -876,12 +876,12 @@ class Net_SSH1 { function interactiveRead() { if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) { - user_error('Operation disallowed prior to login()', E_USER_NOTICE); + $this->_handle_error('Operation disallowed prior to login()'); return false; } if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) { - user_error('Unable to initiate an interactive shell session', E_USER_NOTICE); + $this->_handle_error('Unable to initiate an interactive shell session'); return false; } @@ -960,7 +960,7 @@ class Net_SSH1 { function _get_binary_packet() { if (feof($this->fsock)) { - //user_error('connection closed prematurely', E_USER_NOTICE); + //$this->_handle_error('connection closed prematurely'); return false; } @@ -984,7 +984,7 @@ class Net_SSH1 { $temp = unpack('Ncrc', substr($raw, -4)); //if ( $temp['crc'] != $this->_crc($padding . $type . $data) ) { - // user_error('Bad CRC in packet from server', E_USER_NOTICE); + // $this->_handle_error('Bad CRC in packet from server'); // return false; //} @@ -1017,7 +1017,7 @@ class Net_SSH1 { */ function _send_binary_packet($data) { if (feof($this->fsock)) { - //user_error('connection closed prematurely', E_USER_NOTICE); + //$this->_handle_error('connection closed prematurely'); return false; } @@ -1418,4 +1418,22 @@ class Net_SSH1 { { return rtrim($this->server_identification); } + + /** + * Error Handler + * + * Throws exceptions if PHPSECLIB_USE_EXCEPTIONS is defined. + * Unless PHPSECLIB_EXCEPTION_CLASS is set it'll throw generic Exceptions. + * + * @param String $string + * @access private + */ + function _handle_error($err_msg) { + if (defined('PHPSECLIB_USE_EXCEPTIONS') && version_compare(PHP_VERSION, '5.1.0', '>=')) { + $class = defined('PHPSECLIB_EXCEPTION_CLASS') && class_exists(PHPSECLIB_EXCEPTION_CLASS) ? PHPSECLIB_EXCEPTION_CLASS : 'Exception'; + throw(new $class($err_msg)); + } else { + $this->_handle_error($err_msg); + } + } } diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 29a8f0f4..7ca86f43 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -793,7 +793,7 @@ class Net_SSH2 { $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 $this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout); if (!$this->fsock) { - user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"), E_USER_NOTICE); + $this->_handle_error(rtrim("Cannot connect to $host. Error $errno. $errstr")); return; } $elapsed = strtok(microtime(), ' ') + strtok('') - $start; @@ -801,7 +801,7 @@ class Net_SSH2 { $timeout-= $elapsed; if ($timeout <= 0) { - user_error(rtrim("Cannot connect to $host. Timeout error"), E_USER_NOTICE); + $this->_handle_error(rtrim("Cannot connect to $host. Timeout error")); return; } @@ -814,7 +814,7 @@ class Net_SSH2 { // on windows this returns a "Warning: Invalid CRT parameters detected" error // the !count() is done as a workaround for