dolibarr 21.0.0-beta
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 $tmp_conn_id = 0;
57 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
58 dol_syslog('Try to connect with ssh2_connect');
59 $tmp_conn_id = ssh2_connect($ftp_server, (int) $ftp_port);
60 } elseif (getDolGlobalString('FTP_CONNECT_WITH_SSL')) {
61 dol_syslog('Try to connect with ftp_ssl_connect');
62 $connect_id = ftp_ssl_connect($ftp_server, (int) $ftp_port, $connecttimeout);
63 } else {
64 dol_syslog('Try to connect with ftp_connect');
65 $connect_id = ftp_connect($ftp_server, (int) $ftp_port, $connecttimeout);
66 }
67 if (!empty($connect_id) || !empty($tmp_conn_id)) {
68 if ($ftp_user) {
69 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
70 dol_syslog('Try to authenticate with ssh2_auth_password');
71 if (!empty($tmp_conn_id) && ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
72 // Turn on passive mode transfers (must be after a successful login
73 //if ($ftp_passive) ftp_pasv($connect_id, true);
74
75 // Change the dir
76 $newsectioniso = mb_convert_encoding($section, 'ISO-8859-1');
77 //ftp_chdir($connect_id, $newsectioniso);
78 $connect_id = ssh2_sftp($tmp_conn_id);
79 if (!$connect_id) {
80 dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
81 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication");
82 $ok = 0;
83 $error++;
84 }
85 } else {
86 dol_syslog('Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
87 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
88 $ok = 0;
89 $error++;
90 }
91 } else {
92 if (!empty($connect_id) && ftp_login($connect_id, $ftp_user, $ftp_password)) {
93 // Turn on passive mode transfers (must be after a successful login)
94 if ($ftp_passive) {
95 ftp_pasv($connect_id, true);
96 }
97
98 // Change the dir
99 $newsectioniso = mb_convert_encoding($section, 'ISO-8859-1');
100 if (!ftp_chdir($connect_id, $newsectioniso)) {
101 $ok = 0;
102 $mesg = $langs->transnoentitiesnoconv("FailedToChdirOnFTPServer");
103 }
104 } else {
105 $ok = 0;
106 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
107 }
108 }
109 }
110 } else {
111 dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
112 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
113 $ok = 0;
114 }
115 }
116
117 $arrayresult = array('conn_id' => $connect_id, 'ok' => $ok, 'mesg' => $mesg, 'curdir' => $section, 'curdiriso' => $newsectioniso);
118 return $arrayresult;
119}
120
121
129function ftp_isdir($connect_id, $dir)
130{
131 if (@ftp_chdir($connect_id, $dir)) {
132 ftp_cdup($connect_id);
133 return 1;
134 } else {
135 return 0;
136 }
137}
138
145function dol_ftp_close($connect_id)
146{
147 global $conf;
148
149 // Close FTP connection
150 if ($connect_id) {
151 if (!getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
152 return ftp_close($connect_id);
153 }
154 }
155 return true;
156}
157
166function dol_ftp_delete($connect_id, $file, $newsection)
167{
168 global $conf;
169
170 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
171 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
172 }
173
174 // Remote file
175 $filename = $file;
176 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
177 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
178
179 //print "x".$newremotefileiso;
180 dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso);
181 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
182 return ssh2_sftp_unlink($connect_id, $newremotefileiso);
183 } else {
184 return @ftp_delete($connect_id, $newremotefileiso);
185 }
186}
187
197function dol_ftp_get($connect_id, $localfile, $file, $newsection)
198{
199 global $conf;
200
201 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
202 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
203 }
204
205 // Remote file
206 $filename = $file;
207 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
208 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
209
210 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
211 return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r');
212 } else {
213 return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
214 }
215}
216
226function dol_ftp_put($connect_id, $file, $localfile, $newsection)
227{
228 global $conf;
229
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
234 // Remote file
235 $filename = $file;
236 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
237 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
238
239 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
240 return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
241 } else {
242 return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
243 }
244}
245
254function dol_ftp_rmdir($connect_id, $file, $newsection)
255{
256 global $conf;
257
258 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
259 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
260 }
261
262 // Remote file
263 $filename = $file;
264 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
265 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
266
267 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
268 return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
269 } else {
270 return @ftp_rmdir($connect_id, $newremotefileiso);
271 }
272}
273
274
283function dol_ftp_mkdir($connect_id, $newdir, $newsection)
284{
285 global $conf;
286
287 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
288 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
289 }
290
291 // Remote file
292 $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir;
293 $newremotefileiso = mb_convert_encoding($newremotefileiso, 'ISO-8859-1');
294
295 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
296 return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
297 } else {
298 return @ftp_mkdir($connect_id, $newremotefileiso);
299 }
300}
dol_ftp_mkdir($connect_id, $newdir, $newsection)
Remove FTP directory.
Definition ftp.lib.php:283
ftp_isdir($connect_id, $dir)
Tell if an entry is a FTP directory.
Definition ftp.lib.php:129
dol_ftp_close($connect_id)
Tell if an entry is a FTP directory.
Definition ftp.lib.php:145
dol_ftp_delete($connect_id, $file, $newsection)
Delete a FTP file.
Definition ftp.lib.php:166
dol_ftp_rmdir($connect_id, $file, $newsection)
Remove FTP directory.
Definition ftp.lib.php:254
dol_ftp_put($connect_id, $file, $localfile, $newsection)
Upload a FTP file.
Definition ftp.lib.php:226
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:197
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79