dolibarr 19.0.3
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 *
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
38function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
39{
40 global $langs, $conf;
41
42 $ok = 1;
43 $error = 0;
44 $connect_id = null;
45 $newsectioniso = '';
46 $mesg="";
47
48 if (!is_numeric($ftp_port)) {
49 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
50 $ok = 0;
51 }
52
53 if ($ok) {
54 $connecttimeout = (!getDolGlobalString('FTP_CONNECT_TIMEOUT') ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
55 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
56 dol_syslog('Try to connect with ssh2_connect');
57 $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port);
58 } elseif (getDolGlobalString('FTP_CONNECT_WITH_SSL')) {
59 dol_syslog('Try to connect with ftp_ssl_connect');
60 $connect_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
61 } else {
62 dol_syslog('Try to connect with ftp_connect');
63 $connect_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
64 }
65 if (!empty($connect_id) || !empty($tmp_conn_id)) {
66 if ($ftp_user) {
67 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
68 dol_syslog('Try to authenticate with ssh2_auth_password');
69 if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
70 // Turn on passive mode transfers (must be after a successful login
71 //if ($ftp_passive) ftp_pasv($connect_id, true);
72
73 // Change the dir
74 $newsectioniso = mb_convert_encoding($section, 'ISO-8859-1');
75 //ftp_chdir($connect_id, $newsectioniso);
76 $connect_id = ssh2_sftp($tmp_conn_id);
77 if (!$connect_id) {
78 dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
79 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication");
80 $ok = 0;
81 $error++;
82 }
83 } else {
84 dol_syslog('Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
85 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
86 $ok = 0;
87 $error++;
88 }
89 } else {
90 if (ftp_login($connect_id, $ftp_user, $ftp_password)) {
91 // Turn on passive mode transfers (must be after a successful login
92 if ($ftp_passive) {
93 ftp_pasv($connect_id, true);
94 }
95
96 // Change the dir
97 $newsectioniso = mb_convert_encoding($section, 'ISO-8859-1');
98 ftp_chdir($connect_id, $newsectioniso);
99 } else {
100 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
101 $ok = 0;
102 $error++;
103 }
104 }
105 }
106 } else {
107 dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
108 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
109 $ok = 0;
110 }
111 }
112
113 $arrayresult = array('conn_id'=>$connect_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso);
114 return $arrayresult;
115}
116
117
125function ftp_isdir($connect_id, $dir)
126{
127 if (@ftp_chdir($connect_id, $dir)) {
128 ftp_cdup($connect_id);
129 return 1;
130 } else {
131 return 0;
132 }
133}
134
141function dol_ftp_close($connect_id)
142{
143 global $conf;
144
145 // Close FTP connection
146 if ($connect_id) {
147 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
148 } elseif (getDolGlobalString('FTP_CONNECT_WITH_SSL')) {
149 return ftp_close($connect_id);
150 } else {
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:125
dol_ftp_close($connect_id)
Tell if an entry is a FTP directory.
Definition ftp.lib.php:141
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:38
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.