ftp.lib.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /* Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2022 Anthony Berton <bertonanthony@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/lib/ftp.lib.php
  21. * \brief Set of functions used for FTP
  22. * \ingroup core
  23. */
  24. /**
  25. * Connect to FTP server
  26. *
  27. * @param string $ftp_server Server name
  28. * @param string $ftp_port Server port
  29. * @param string $ftp_user FTP user
  30. * @param string $ftp_password FTP password
  31. * @param string $section Directory
  32. * @param integer $ftp_passive Use a passive mode
  33. * @return int <0 if OK, >0 if KO
  34. */
  35. function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
  36. {
  37. global $langs, $conf;
  38. $ok = 1;
  39. $error = 0;
  40. $connect_id = null;
  41. $newsectioniso = '';
  42. $mesg="";
  43. if (!is_numeric($ftp_port)) {
  44. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
  45. $ok = 0;
  46. }
  47. if ($ok) {
  48. $connecttimeout = (empty($conf->global->FTP_CONNECT_TIMEOUT) ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
  49. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  50. dol_syslog('Try to connect with ssh2_connect');
  51. $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port);
  52. } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
  53. dol_syslog('Try to connect with ftp_ssl_connect');
  54. $connect_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
  55. } else {
  56. dol_syslog('Try to connect with ftp_connect');
  57. $connect_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
  58. }
  59. if (!empty($connect_id) || !empty($tmp_conn_id)) {
  60. if ($ftp_user) {
  61. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  62. dol_syslog('Try to authenticate with ssh2_auth_password');
  63. if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
  64. // Turn on passive mode transfers (must be after a successful login
  65. //if ($ftp_passive) ftp_pasv($connect_id, true);
  66. // Change the dir
  67. $newsectioniso = utf8_decode($section);
  68. //ftp_chdir($connect_id, $newsectioniso);
  69. $connect_id = ssh2_sftp($tmp_conn_id);
  70. if (!$connect_id) {
  71. dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
  72. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication");
  73. $ok = 0;
  74. $error++;
  75. }
  76. } else {
  77. dol_syslog('Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
  78. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
  79. $ok = 0;
  80. $error++;
  81. }
  82. } else {
  83. if (ftp_login($connect_id, $ftp_user, $ftp_password)) {
  84. // Turn on passive mode transfers (must be after a successful login
  85. if ($ftp_passive) {
  86. ftp_pasv($connect_id, true);
  87. }
  88. // Change the dir
  89. $newsectioniso = utf8_decode($section);
  90. ftp_chdir($connect_id, $newsectioniso);
  91. } else {
  92. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
  93. $ok = 0;
  94. $error++;
  95. }
  96. }
  97. }
  98. } else {
  99. dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
  100. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
  101. $ok = 0;
  102. }
  103. }
  104. $arrayresult = array('conn_id'=>$connect_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso);
  105. return $arrayresult;
  106. }
  107. /**
  108. * Tell if an entry is a FTP directory
  109. *
  110. * @param resource $connect_id Connection handler
  111. * @param string $dir Directory
  112. * @return int 1=directory, 0=not a directory
  113. */
  114. function ftp_isdir($connect_id, $dir)
  115. {
  116. if (@ftp_chdir($connect_id, $dir)) {
  117. ftp_cdup($connect_id);
  118. return 1;
  119. } else {
  120. return 0;
  121. }
  122. }
  123. /**
  124. * Tell if an entry is a FTP directory
  125. *
  126. * @param resource $connect_id Connection handler
  127. * @return result
  128. */
  129. function dol_ftp_close($connect_id)
  130. {
  131. global $conf;
  132. // Close FTP connection
  133. if ($connect_id) {
  134. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  135. } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
  136. return ftp_close($connect_id);
  137. } else {
  138. return ftp_close($connect_id);
  139. }
  140. }
  141. }
  142. /**
  143. * Delete a FTP file
  144. *
  145. * @param resource $connect_id Connection handler
  146. * @param string $file File
  147. * @param string $newsection $newsection
  148. * @return result
  149. */
  150. function dol_ftp_delete($connect_id, $file, $newsection)
  151. {
  152. global $conf;
  153. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  154. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  155. }
  156. // Remote file
  157. $filename = $file;
  158. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  159. $newremotefileiso = utf8_decode($remotefile);
  160. //print "x".$newremotefileiso;
  161. dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso);
  162. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  163. return ssh2_sftp_unlink($connect_id, $newremotefileiso);
  164. } else {
  165. return @ftp_delete($connect_id, $newremotefileiso);
  166. }
  167. }
  168. /**
  169. * Download a FTP file
  170. *
  171. * @param resource $connect_id Connection handler
  172. * @param string $localfile The local file path
  173. * @param string $file The remote file path
  174. * @param string $newsection $newsection
  175. * @return result
  176. */
  177. function dol_ftp_get($connect_id, $localfile, $file, $newsection)
  178. {
  179. global $conf;
  180. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  181. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  182. }
  183. // Remote file
  184. $filename = $file;
  185. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  186. $newremotefileiso = utf8_decode($remotefile);
  187. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  188. return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r');
  189. } else {
  190. return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
  191. }
  192. }
  193. /**
  194. * Upload a FTP file
  195. *
  196. * @param resource $connect_id Connection handler
  197. * @param string $file File name
  198. * @param string $localfile The path to the local file
  199. * @param string $newsection $newsection
  200. * @return result
  201. */
  202. function dol_ftp_put($connect_id, $file, $localfile, $newsection)
  203. {
  204. global $conf;
  205. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  206. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  207. }
  208. // Remote file
  209. $filename = $file;
  210. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  211. $newremotefileiso = utf8_decode($remotefile);
  212. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  213. return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
  214. } else {
  215. return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
  216. }
  217. }
  218. /**
  219. * Remove FTP directory
  220. *
  221. * @param resource $connect_id Connection handler
  222. * @param string $file File
  223. * @param string $newsection $newsection
  224. * @return result
  225. */
  226. function dol_ftp_rmdir($connect_id, $file, $newsection)
  227. {
  228. global $conf;
  229. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  230. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  231. }
  232. // Remote file
  233. $filename = $file;
  234. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  235. $newremotefileiso = utf8_decode($remotefile);
  236. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  237. return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
  238. } else {
  239. return @ftp_rmdir($connect_id, $newremotefileiso);
  240. }
  241. }
  242. /**
  243. * Remove FTP directory
  244. *
  245. * @param resource $connect_id Connection handler
  246. * @param string $newdir Dir create
  247. * @param string $newsection $newsection
  248. * @return result
  249. */
  250. function dol_ftp_mkdir($connect_id, $newdir, $newsection)
  251. {
  252. global $conf;
  253. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  254. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  255. }
  256. // Remote file
  257. $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir;
  258. $newremotefileiso = utf8_decode($newremotefileiso);
  259. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  260. return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
  261. } else {
  262. return @ftp_mkdir($connect_id, $newremotefileiso);
  263. }
  264. }