* * @retval boolean true if SSH is fully supported, false otherwise */ function LSaddon_ssh_support() { $retval=true; // Check PhpSecLib library if (!defined('PHPSECLIB_AUTOLOAD')) { LSerror :: addErrorCode('SSH_SUPPORT_02','PHPSECLIB_AUTOLOAD'); $retval=false; } else if(!LSsession::includeFile(PHPSECLIB_AUTOLOAD)) { LSerror :: addErrorCode('SSH_SUPPORT_01'); $retval=false; } return $retval; } /** * Connect to an SFTP server * * @author Benjamin Renard * * @param[in] $params array The SSH connexion parameters : * array ( * 'host' => '[SSH server hostname/IP]', // required * 'port' => [SSH port], // optional, default : 22 * 'timeout' => [SSH connection timeout], // optional, default : 10 * * // Authentication * 'user' => '[SSH remote user]', // required * * // Auth method : * // One of the following method configuration is required. * * // Auth using simple password * 'password' => '[secret password]' * * // Auth using a key * 'auth_key' => array ( * 'file_path' => '[SSH private key file path]', * 'password' => '[SSH private key file password]' // Only if need * ) * ) * @param[in] $sftp boolean Enable SFTP mode (default : false) * * @retval mixed SSH2/SFTP object or false */ function connectToSSH($params, $sftp=false) { if (!isset($params['host'])) { LSerror :: addErrorCode('SSH_01',"host"); return false; } $host = $params['host']; if (!isset($params['user'])) { LSerror :: addErrorCode('SSH_01',"user"); return false; } $user = $params['user']; $port = (isset($params['port'])?$params['port']:22); $timeout = (isset($params['timeout'])?$params['timeout']:22); if (isset($params['auth_key'])) { if (!isset($params['auth_key']['file_path'])) { LSerror :: addErrorCode('SSH_01',"auth_key -> file_path"); return false; } $key_file_path = $params['auth_key']['file_path']; if (!is_file($key_file_path) || !is_readable($key_file_path)) { LSerror :: addErrorCode('SSH_02', $key_file_path); return false; } $password = new RSA(); if (isset($params['auth_key']['password'])) { $password -> setPassword($params['auth_key']['password']); } $key_content = file_get_contents($key_file_path); if (!$password -> loadKey($key_content)) { LSerror :: addErrorCode('SSH_03', $key_file_path); return; } } elseif (isset($params['password'])) { $password = $params['password']; } else { LSerror :: addErrorCode('SSH_01',"authentication"); return false; } if (isset($sftp)) $cnx = new SFTP($host, $port, $timeout); else $cnx = new SSH2($host, $port, $timeout); if (!$cnx->login($user, $password)) { LSerror :: addErrorCode('SSH_04', array('host' => $host, 'port' => $port)); return false; } return $cnx; } /** * Create one or more directories throught SFTP * * @author Benjamin Renard * * @param[in] $connection_params array Connection parameters * @param[in] $dirs array|string The directory/ies to add * @param[in] $mode integer The directory/ies mode (default : default umask on the SSH server) * @param[in] $recursive boolean Enable recursive mode (default : false) * @param[in] $continue boolean Enable continue mode : do not on error (default : false) * * @retval boolean */ function createDirsBySFTP($connection_params, $dirs, $chmod=-1, $recursive=false, $continue=false) { $cnx = connectToSSH($connection_params, true); if (! $cnx){ return; } if (!is_array($dirs)) { $dirs = array($dirs); } $retval=true; foreach($dirs as $dir) { if (!$cnx -> mkdir($dir, $chmod, $recursive)) { LSerror :: addErrorCode('SSH_05',$dir); if (!$continue) return false; $retval=false; } } return $retval; } /** * Delete one or more directories throught SFTP * * @author Benjamin Renard * * @param[in] $connection_params array Connection parameters * @param[in] $dirs array|string The directory/ies to remove * @param[in] $recursive boolean Enable recursive mode (default : false) * @param[in] $continue boolean Enable continue mode : do not on error (default : false) * * @retval boolean */ function removeDirsBySFTP($connection_params, $dirs, $recursive=false) { $cnx = connectToSSH($connection_params, true); if (! $cnx){ return; } if (!is_array($dirs)) { $dirs = array($dirs); } $retval=true; foreach($dirs as $dir) { if (!$cnx -> delete($dir, $recursive)) { LSerror :: addErrorCode('SSH_06',$dir); if (!$continue) return false; $retval=false; } } return $retval; } /** * Rename a directory throught SFTP * * @author Benjamin Renard * * @param[in] $connection_params array Connection parameters * @param[in] $old string The actual directory path to rename * @param[in] $new string The new directory path * * @retval boolean */ function renameDirBySFTP($connection_params, $old, $new) { $cnx = connectToSSH($connection_params, true); if (! $cnx){ return; } if (!$cnx -> rename($old, $new)) { LSerror :: addErrorCode('SSH_07',array('old' => $old,'new' => $new)); return; } return true; } /** * Exec a command throught SSH * * @author Benjamin Renard * * @param[in] $connection_params array Connection parameters * @param[in] $cmd string The command to run on remote server * * @retval mixed False if connection fail and an array otherwise, with * exit code as first value and the command outup as second * one (stdout + stderr). */ function execBySSH($connection_params, $cmd) { $cnx = connectToSSH($connection_params); if (! $cnx){ return; } $result = $cnx -> exec($cmd); $exit_status = $cnx->getExitStatus(); return array($exit_status, $result); }