ftp.lib.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /* Copyright (C) 2022-2023 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 array Result of connect
  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 = (!getDolGlobalString('FTP_CONNECT_TIMEOUT') ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
  49. if (getDolGlobalString('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 (getDolGlobalString('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 (getDolGlobalString('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 = mb_convert_encoding($section, 'ISO-8859-1');
  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 = mb_convert_encoding($section, 'ISO-8859-1');
  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 boolean Result of closing
  128. */
  129. function dol_ftp_close($connect_id)
  130. {
  131. global $conf;
  132. // Close FTP connection
  133. if ($connect_id) {
  134. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  135. } elseif (getDolGlobalString('FTP_CONNECT_WITH_SSL')) {
  136. return ftp_close($connect_id);
  137. } else {
  138. return ftp_close($connect_id);
  139. }
  140. }
  141. return true;
  142. }
  143. /**
  144. * Delete a FTP file
  145. *
  146. * @param resource $connect_id Connection handler
  147. * @param string $file File
  148. * @param string $newsection $newsection
  149. * @return bool
  150. */
  151. function dol_ftp_delete($connect_id, $file, $newsection)
  152. {
  153. global $conf;
  154. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  155. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  156. }
  157. // Remote file
  158. $filename = $file;
  159. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  160. $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
  161. //print "x".$newremotefileiso;
  162. dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso);
  163. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  164. return ssh2_sftp_unlink($connect_id, $newremotefileiso);
  165. } else {
  166. return @ftp_delete($connect_id, $newremotefileiso);
  167. }
  168. }
  169. /**
  170. * Download a FTP file
  171. *
  172. * @param resource $connect_id Connection handler
  173. * @param string $localfile The local file path
  174. * @param string $file The remote file path
  175. * @param string $newsection $newsection
  176. * @return bool|resource
  177. */
  178. function dol_ftp_get($connect_id, $localfile, $file, $newsection)
  179. {
  180. global $conf;
  181. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  182. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  183. }
  184. // Remote file
  185. $filename = $file;
  186. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  187. $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
  188. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  189. return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r');
  190. } else {
  191. return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
  192. }
  193. }
  194. /**
  195. * Upload a FTP file
  196. *
  197. * @param resource $connect_id Connection handler
  198. * @param string $file File name
  199. * @param string $localfile The path to the local file
  200. * @param string $newsection $newsection
  201. * @return bool
  202. */
  203. function dol_ftp_put($connect_id, $file, $localfile, $newsection)
  204. {
  205. global $conf;
  206. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  207. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  208. }
  209. // Remote file
  210. $filename = $file;
  211. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  212. $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
  213. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  214. return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
  215. } else {
  216. return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
  217. }
  218. }
  219. /**
  220. * Remove FTP directory
  221. *
  222. * @param resource $connect_id Connection handler
  223. * @param string $file File
  224. * @param string $newsection $newsection
  225. * @return bool
  226. */
  227. function dol_ftp_rmdir($connect_id, $file, $newsection)
  228. {
  229. global $conf;
  230. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  231. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  232. }
  233. // Remote file
  234. $filename = $file;
  235. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  236. $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
  237. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  238. return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
  239. } else {
  240. return @ftp_rmdir($connect_id, $newremotefileiso);
  241. }
  242. }
  243. /**
  244. * Remove FTP directory
  245. *
  246. * @param resource $connect_id Connection handler
  247. * @param string $newdir Dir create
  248. * @param string $newsection $newsection
  249. * @return bool|string
  250. */
  251. function dol_ftp_mkdir($connect_id, $newdir, $newsection)
  252. {
  253. global $conf;
  254. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  255. $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  256. }
  257. // Remote file
  258. $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir;
  259. $newremotefileiso = mb_convert_encoding($newremotefileiso, 'ISO-8859-1');
  260. if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
  261. return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
  262. } else {
  263. return @ftp_mkdir($connect_id, $newremotefileiso);
  264. }
  265. }