sftp.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. class sftp
  3. {
  4. private static $authent;
  5. private static $connexion;
  6. private static $dataSend;
  7. private static function connectHost(int $_sec = 2)
  8. {
  9. $originalConnectionTimeout = ini_get('default_socket_timeout');
  10. ini_set('default_socket_timeout', $_sec);
  11. if (self::$authent = @ssh2_connect(SFTP_HOST, 22)) {
  12. ini_set('default_socket_timeout', $originalConnectionTimeout);
  13. return TRUE;
  14. } else {
  15. return FALSE;
  16. }
  17. }
  18. private static function authentHost()
  19. {
  20. return (@ssh2_auth_password(self::$authent, SFTP_USER, SFTP_PASS)) ? TRUE : FALSE;
  21. }
  22. private static function initializeHost()
  23. {
  24. return (self::$connexion = @ssh2_sftp(self::$authent)) ? TRUE : FALSE;
  25. }
  26. private static function fileExist(string $_file = NULL)
  27. {
  28. return (file_exists($_file)) ? TRUE : FALSE;
  29. }
  30. private static function fileGetContents(string $_file = NULL)
  31. {
  32. return (self::$dataSend = @file_get_contents($_file)) ? TRUE : FALSE;
  33. }
  34. private static function accessHost(bool $_showAlert = TRUE)
  35. {
  36. if (!self::connectHost()) {
  37. ($_showAlert == TRUE) ? alert::recError("Erreur de connection au serveur distant") : NULL;
  38. return FALSE;
  39. }
  40. if (!self::authentHost()) {
  41. ($_showAlert == TRUE) ? alert::recError("Erreur d'authentification au serveur distant") : NULL;
  42. return FALSE;
  43. }
  44. if (!self::initializeHost()) {
  45. ($_showAlert == TRUE) ? alert::recError("Erreur d'initialisation au serveur distant") : NULL;
  46. return FALSE;
  47. }
  48. ($_showAlert == TRUE) ? alert::recSuccess("Accès au serveur distant") : NULL;
  49. return TRUE;
  50. }
  51. public static function testAccessHost()
  52. {
  53. return (self::accessHost(FALSE)) ? TRUE : FALSE;
  54. }
  55. private static function sendFile(string $_file)
  56. {
  57. self::accessHost();
  58. $stream = @fopen("ssh2.sftp://" . self::$authent . SFTP_REMOTE . $_file, "w");
  59. $data = @file_get_contents(SFTP_LOCAL . $_file);
  60. if (@fwrite($stream, $data) === false) {
  61. alert::recError($_file . " n'a pas pu être transféré au serveur distant");
  62. return FALSE;
  63. } else {
  64. alert::recSuccess($_file . " a été trasféré avec succès");
  65. return TRUE;
  66. }
  67. @fclose($stream);
  68. }
  69. public static function deleteFileToRemote(string $_file)
  70. {
  71. db::query("UPDATE " . DB_T_EXCEL . " SET createForSFTP = NULL WHERE id = :id");
  72. db::bind(':id', salaries::lastExcel());
  73. try {
  74. db::execute();
  75. alert::recSuccess($_file . " a été supprimé du serveur local.");
  76. return TRUE;
  77. } catch (Exception $e) {
  78. alert::recError($_file . " n'a pas pu être supprimé");
  79. return FALSE;
  80. }
  81. }
  82. public static function sendFileToRemote($_file = NULL)
  83. {
  84. if ($_file == NULL) {
  85. alert::recError("Le fichier à envoyé n'a pas été défini");
  86. return FALSE;
  87. } elseif (!self::fileExist(SFTP_LOCAL . $_file)) {
  88. alert::recError($_file . " n'est pas présent sur le serveur");
  89. return FALSE;
  90. } else {
  91. return self::sendFile($_file);
  92. }
  93. }
  94. public static function scanFolderRemote()
  95. {
  96. self::accessHost(FALSE);
  97. $realpath = @ssh2_sftp_realpath(self::$connexion, SFTP_REMOTE);
  98. $dir = "ssh2.sftp://" . self::$connexion . $realpath;
  99. $tempArray = array();
  100. if (is_dir($dir)) {
  101. if ($dh = opendir($dir)) {
  102. while (($file = readdir($dh)) !== false) {
  103. $tempFile = NULL;
  104. $realpath = @ssh2_sftp_realpath(self::$connexion, $file);
  105. $filetype = filetype($dir . $realpath);
  106. $size = sprintf("%.2f", filesize($dir . $realpath));
  107. if ($filetype != "dir") {
  108. $infos = @ssh2_sftp_stat(self::$connexion, $file);
  109. $tempFile["file"] = $file;
  110. $tempFile["size"] = core::formatFileSize($infos["size"], 2);
  111. $tempFile["date"] = core::dateFromTimestamp($infos["atime"]);
  112. $tempArray[] = $tempFile;
  113. }
  114. }
  115. closedir($dh);
  116. }
  117. }
  118. return $tempArray;
  119. }
  120. public static function scanFolderLocal()
  121. {
  122. $tempArray = array();
  123. if ($scandir = scandir(SFTP_LOCAL)) {
  124. foreach ($scandir as $file) {
  125. $tempFile = NULL;
  126. if (is_file(SFTP_LOCAL . $file) === TRUE and $file != "index.html") {
  127. $tempFile["file"] = $file;
  128. $tempFile["size"] = core::formatFileSize(filesize(SFTP_LOCAL . $file), 2);
  129. $tempFile["date"] = core::dateFromTimestamp(filemtime(SFTP_LOCAL . $file));
  130. $tempArray[] = $tempFile;
  131. }
  132. }
  133. }
  134. return $tempArray;
  135. }
  136. public static function deleteFormRemote(string $_file)
  137. {
  138. self::accessHost(FALSE);
  139. if (@ssh2_sftp_unlink(self::$connexion, SFTP_REMOTE . $_file)) {
  140. alert::recSuccess($_file . " a été supprimé du serveur distant.");
  141. return TRUE;
  142. } else {
  143. alert::recError($_file . " n'a pas été supprimé du serveur distant");
  144. return FALSE;
  145. }
  146. }
  147. public static function downloadFormRemote(string $_file)
  148. {
  149. self::accessHost(FALSE);
  150. //ssh2_scp_recv(self::$authent, SFTP_REMOTE.$_file, SFTP_LOCAL.$_file);
  151. $stream = @fopen("ssh2.sftp://" . self::$authent . SFTP_REMOTE . $_file, 'r');
  152. $contents = stream_get_contents($stream);
  153. file_put_contents(DIR_TEMP . $_file, $contents);
  154. @fclose($stream);
  155. }
  156. }