dolibarr 21.0.0-alpha
ftp.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2022-2023 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2022 Anthony Berton <bertonanthony@gmail.com>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
39function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
40{
41 global $langs, $conf;
42
43 $ok = 1;
44 $error = 0;
45 $connect_id = null;
46 $newsectioniso = '';
47 $mesg = "";
48
49 if (!is_numeric($ftp_port)) {
50 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
51 $ok = 0;
52 }
53
54 if ($ok) {
55 $connecttimeout = (!getDolGlobalString('FTP_CONNECT_TIMEOUT') ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
56 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
57 dol_syslog('Try to connect with ssh2_connect');
58 $tmp_conn_id = ssh2_connect($ftp_server, (int) $ftp_port);
59 } elseif (getDolGlobalString('FTP_CONNECT_WITH_SSL')) {
60 dol_syslog('Try to connect with ftp_ssl_connect');
61 $connect_id = ftp_ssl_connect($ftp_server, (int) $ftp_port, $connecttimeout);
62 } else {
63 dol_syslog('Try to connect with ftp_connect');
64 $connect_id = ftp_connect($ftp_server, (int) $ftp_port, $connecttimeout);
65 }
66 if (!empty($connect_id) || !empty($tmp_conn_id)) {
67 if ($ftp_user) {
68 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
69 dol_syslog('Try to authenticate with ssh2_auth_password');
70 if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
71 // Turn on passive mode transfers (must be after a successful login
72 //if ($ftp_passive) ftp_pasv($connect_id, true);
73
74 // Change the dir
75 $newsectioniso = mb_convert_encoding($section, 'ISO-8859-1');
76 //ftp_chdir($connect_id, $newsectioniso);
77 $connect_id = ssh2_sftp($tmp_conn_id);
78 if (!$connect_id) {
79 dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
80 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication");
81 $ok = 0;
82 $error++;
83 }
84 } else {
85 dol_syslog('Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
86 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
87 $ok = 0;
88 $error++;
89 }
90 } else {
91 if (ftp_login($connect_id, $ftp_user, $ftp_password)) {
92 // Turn on passive mode transfers (must be after a successful login)
93 if ($ftp_passive) {
94 ftp_pasv($connect_id, true);
95 }
96
97 // Change the dir
98 $newsectioniso = mb_convert_encoding($section, 'ISO-8859-1');
99 if (!ftp_chdir($connect_id, $newsectioniso)) {
100 $ok = 0;
101 $mesg = $langs->transnoentitiesnoconv("FailedToChdirOnFTPServer");
102 }
103 } else {
104 $ok = 0;
105 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
106 }
107 }
108 }
109 } else {
110 dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
111 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
112 $ok = 0;
113 }
114 }
115
116 $arrayresult = array('conn_id' => $connect_id, 'ok' => $ok, 'mesg' => $mesg, 'curdir' => $section, 'curdiriso' => $newsectioniso);
117 return $arrayresult;
118}
119
120
128function ftp_isdir($connect_id, $dir)
129{
130 if (@ftp_chdir($connect_id, $dir)) {
131 ftp_cdup($connect_id);
132 return 1;
133 } else {
134 return 0;
135 }
136}
137
144function dol_ftp_close($connect_id)
145{
146 global $conf;
147
148 // Close FTP connection
149 if ($connect_id) {
150 if (!getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
151 return ftp_close($connect_id);
152 }
153 }
154 return true;
155}
156
165function dol_ftp_delete($connect_id, $file, $newsection)
166{
167 global $conf;
168
169 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
170 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
171 }
172
173 // Remote file
174 $filename = $file;
175 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
176 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
177
178 //print "x".$newremotefileiso;
179 dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso);
180 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
181 return ssh2_sftp_unlink($connect_id, $newremotefileiso);
182 } else {
183 return @ftp_delete($connect_id, $newremotefileiso);
184 }
185}
186
196function dol_ftp_get($connect_id, $localfile, $file, $newsection)
197{
198 global $conf;
199
200 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
201 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
202 }
203
204 // Remote file
205 $filename = $file;
206 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
207 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
208
209 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
210 return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r');
211 } else {
212 return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
213 }
214}
215
225function dol_ftp_put($connect_id, $file, $localfile, $newsection)
226{
227 global $conf;
228
229 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
230 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
231 }
232
233 // Remote file
234 $filename = $file;
235 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
236 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
237
238 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
239 return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
240 } else {
241 return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
242 }
243}
244
253function dol_ftp_rmdir($connect_id, $file, $newsection)
254{
255 global $conf;
256
257 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
258 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
259 }
260
261 // Remote file
262 $filename = $file;
263 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
264 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
265
266 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
267 return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
268 } else {
269 return @ftp_rmdir($connect_id, $newremotefileiso);
270 }
271}
272
273
282function dol_ftp_mkdir($connect_id, $newdir, $newsection)
283{
284 global $conf;
285
286 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
287 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
288 }
289
290 // Remote file
291 $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir;
292 $newremotefileiso = mb_convert_encoding($newremotefileiso, 'ISO-8859-1');
293
294 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
295 return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
296 } else {
297 return @ftp_mkdir($connect_id, $newremotefileiso);
298 }
299}
dol_ftp_mkdir($connect_id, $newdir, $newsection)
Remove FTP directory.
Definition ftp.lib.php:282
ftp_isdir($connect_id, $dir)
Tell if an entry is a FTP directory.
Definition ftp.lib.php:128
dol_ftp_close($connect_id)
Tell if an entry is a FTP directory.
Definition ftp.lib.php:144
dol_ftp_delete($connect_id, $file, $newsection)
Delete a FTP file.
Definition ftp.lib.php:165
dol_ftp_rmdir($connect_id, $file, $newsection)
Remove FTP directory.
Definition ftp.lib.php:253
dol_ftp_put($connect_id, $file, $localfile, $newsection)
Upload a FTP file.
Definition ftp.lib.php:225
dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive=0)
Connect to FTP server.
Definition ftp.lib.php:39
dol_ftp_get($connect_id, $localfile, $file, $newsection)
Download a FTP file.
Definition ftp.lib.php:196
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.