| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- class sftp
- {
- private static $authent;
- private static $connexion;
- private static $dataSend;
- private static function connectHost(int $_sec = 2)
- {
- $originalConnectionTimeout = ini_get('default_socket_timeout');
- ini_set('default_socket_timeout', $_sec);
- if (self::$authent = @ssh2_connect(SFTP_HOST, 22)) {
- ini_set('default_socket_timeout', $originalConnectionTimeout);
- return TRUE;
- } else {
- return FALSE;
- }
- }
- private static function authentHost()
- {
- return (@ssh2_auth_password(self::$authent, SFTP_USER, SFTP_PASS)) ? TRUE : FALSE;
- }
- private static function initializeHost()
- {
- return (self::$connexion = @ssh2_sftp(self::$authent)) ? TRUE : FALSE;
- }
- private static function fileExist(string $_file = NULL)
- {
- return (file_exists($_file)) ? TRUE : FALSE;
- }
- private static function fileGetContents(string $_file = NULL)
- {
- return (self::$dataSend = @file_get_contents($_file)) ? TRUE : FALSE;
- }
- private static function accessHost(bool $_showAlert = TRUE)
- {
- if (!self::connectHost()) {
- ($_showAlert == TRUE) ? alert::recError("Erreur de connection au serveur distant") : NULL;
- return FALSE;
- }
- if (!self::authentHost()) {
- ($_showAlert == TRUE) ? alert::recError("Erreur d'authentification au serveur distant") : NULL;
- return FALSE;
- }
- if (!self::initializeHost()) {
- ($_showAlert == TRUE) ? alert::recError("Erreur d'initialisation au serveur distant") : NULL;
- return FALSE;
- }
- ($_showAlert == TRUE) ? alert::recSuccess("Accès au serveur distant") : NULL;
- return TRUE;
- }
- public static function testAccessHost()
- {
- return (self::accessHost(FALSE)) ? TRUE : FALSE;
- }
- private static function sendFile(string $_file)
- {
- self::accessHost();
- $stream = @fopen("ssh2.sftp://" . self::$authent . SFTP_REMOTE . $_file, "w");
- $data = @file_get_contents(SFTP_LOCAL . $_file);
- if (@fwrite($stream, $data) === false) {
- alert::recError($_file . " n'a pas pu être transféré au serveur distant");
- return FALSE;
- } else {
- alert::recSuccess($_file . " a été trasféré avec succès");
- return TRUE;
- }
- @fclose($stream);
- }
- public static function deleteFileToRemote(string $_file)
- {
- db::query("UPDATE " . DB_T_EXCEL . " SET createForSFTP = NULL WHERE id = :id");
- db::bind(':id', salaries::lastExcel());
- try {
- db::execute();
- alert::recSuccess($_file . " a été supprimé du serveur local.");
- return TRUE;
- } catch (Exception $e) {
- alert::recError($_file . " n'a pas pu être supprimé");
- return FALSE;
- }
- }
- public static function sendFileToRemote($_file = NULL)
- {
- if ($_file == NULL) {
- alert::recError("Le fichier à envoyé n'a pas été défini");
- return FALSE;
- } elseif (!self::fileExist(SFTP_LOCAL . $_file)) {
- alert::recError($_file . " n'est pas présent sur le serveur");
- return FALSE;
- } else {
- return self::sendFile($_file);
- }
- }
- public static function scanFolderRemote()
- {
- self::accessHost(FALSE);
- $realpath = @ssh2_sftp_realpath(self::$connexion, SFTP_REMOTE);
- $dir = "ssh2.sftp://" . self::$connexion . $realpath;
- $tempArray = array();
- if (is_dir($dir)) {
- if ($dh = opendir($dir)) {
- while (($file = readdir($dh)) !== false) {
- $tempFile = NULL;
- $realpath = @ssh2_sftp_realpath(self::$connexion, $file);
- $filetype = filetype($dir . $realpath);
- $size = sprintf("%.2f", filesize($dir . $realpath));
- if ($filetype != "dir") {
- $infos = @ssh2_sftp_stat(self::$connexion, $file);
- $tempFile["file"] = $file;
- $tempFile["size"] = core::formatFileSize($infos["size"], 2);
- $tempFile["date"] = core::dateFromTimestamp($infos["atime"]);
- $tempArray[] = $tempFile;
- }
- }
- closedir($dh);
- }
- }
- return $tempArray;
- }
- public static function scanFolderLocal()
- {
- $tempArray = array();
- if ($scandir = scandir(SFTP_LOCAL)) {
- foreach ($scandir as $file) {
- $tempFile = NULL;
- if (is_file(SFTP_LOCAL . $file) === TRUE and $file != "index.html") {
- $tempFile["file"] = $file;
- $tempFile["size"] = core::formatFileSize(filesize(SFTP_LOCAL . $file), 2);
- $tempFile["date"] = core::dateFromTimestamp(filemtime(SFTP_LOCAL . $file));
- $tempArray[] = $tempFile;
- }
- }
- }
- return $tempArray;
- }
- public static function deleteFormRemote(string $_file)
- {
- self::accessHost(FALSE);
- if (@ssh2_sftp_unlink(self::$connexion, SFTP_REMOTE . $_file)) {
- alert::recSuccess($_file . " a été supprimé du serveur distant.");
- return TRUE;
- } else {
- alert::recError($_file . " n'a pas été supprimé du serveur distant");
- return FALSE;
- }
- }
- public static function downloadFormRemote(string $_file)
- {
- self::accessHost(FALSE);
- //ssh2_scp_recv(self::$authent, SFTP_REMOTE.$_file, SFTP_LOCAL.$_file);
- $stream = @fopen("ssh2.sftp://" . self::$authent . SFTP_REMOTE . $_file, 'r');
- $contents = stream_get_contents($stream);
- file_put_contents(DIR_TEMP . $_file, $contents);
- @fclose($stream);
- }
- }
|