dolibarr 22.0.5
ftpclient.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2022 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30
39$langs->loadLangs(array("admin", "other"));
40
41$def = array();
42$lastftpentry = 0;
43
44$action = GETPOST('action', 'aZ09');
45$entry = GETPOST('numero_entry', 'alpha');
46
47// Security check
48if (!$user->admin) {
50}
51
52// Initialise variables
53$result1 = 0;
54$result2 = 0;
55$result3 = 0;
56$result4 = 0;
57$result5 = 0;
58$result6 = 0;
59
60
61/*
62 * Action
63 */
64
65// Get value for $lastftpentry
66$sql = "select MAX(name) as name from ".MAIN_DB_PREFIX."const";
67$sql .= " WHERE name like 'FTP_SERVER_%'";
68$result = $db->query($sql);
69if ($result) {
70 $obj = $db->fetch_object($result);
71 $reg = array();
72 preg_match('/([0-9]+)$/i', $obj->name, $reg);
73 if (!empty($reg[1])) {
74 $lastftpentry = $reg[1];
75 }
76} else {
77 dol_print_error($db);
78}
79
80if ($action == 'add' || GETPOST('modify', 'alpha')) {
81 $ftp_name = "FTP_NAME_".$entry;
82 $ftp_server = "FTP_SERVER_".$entry;
83
84 $error = 0;
85
86 if (!GETPOST($ftp_name, 'alpha')) {
87 $error = 1;
88 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Label")), null, 'errors');
89 }
90
91 if (!GETPOST($ftp_server, 'alpha')) {
92 $error = 1;
93 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Server")), null, 'errors');
94 }
95
96 if (!$error) {
97 $ftp_port = "FTP_PORT_".$entry;
98 $ftp_user = "FTP_USER_".$entry;
99 $ftp_password = "FTP_PASSWORD_".$entry;
100 $ftp_passive = "FTP_PASSIVE_".$entry;
101
102 $db->begin();
103
104 $result1 = dolibarr_set_const($db, "FTP_PORT_".$entry, GETPOST($ftp_port, 'alpha'), 'chaine', 0, '', $conf->entity);
105 if ($result1) {
106 $result2 = dolibarr_set_const($db, "FTP_SERVER_".$entry, GETPOST($ftp_server, 'alpha'), 'chaine', 0, '', $conf->entity);
107 }
108 if ($result2) {
109 $result3 = dolibarr_set_const($db, "FTP_USER_".$entry, GETPOST($ftp_user, 'alpha'), 'chaine', 0, '', $conf->entity);
110 }
111 if ($result3) {
112 $result4 = dolibarr_set_const($db, "FTP_PASSWORD_".$entry, GETPOST($ftp_password, 'alpha'), 'chaine', 0, '', $conf->entity);
113 }
114 if ($result4) {
115 $result5 = dolibarr_set_const($db, "FTP_NAME_".$entry, GETPOST($ftp_name, 'alpha'), 'chaine', 0, '', $conf->entity);
116 }
117 if ($result5) {
118 $result6 = dolibarr_set_const($db, "FTP_PASSIVE_".$entry, GETPOST($ftp_passive, 'alpha'), 'chaine', 0, '', $conf->entity);
119 }
120
121 if ($result1 && $result2 && $result3 && $result4 && $result5 && $result6) {
122 $db->commit();
123 header("Location: ".$_SERVER["PHP_SELF"]);
124 exit;
125 } else {
126 $db->rollback();
127 dol_print_error($db);
128 }
129 }
130}
131
132if (GETPOST('delete', 'alpha')) {
133 if ($entry) {
134 $db->begin();
135
136 $result1 = dolibarr_del_const($db, "FTP_PORT_".$entry, $conf->entity);
137 if ($result1) {
138 $result2 = dolibarr_del_const($db, "FTP_SERVER_".$entry, $conf->entity);
139 }
140 if ($result2) {
141 $result3 = dolibarr_del_const($db, "FTP_USER_".$entry, $conf->entity);
142 }
143 if ($result3) {
144 $result4 = dolibarr_del_const($db, "FTP_PASSWORD_".$entry, $conf->entity);
145 }
146 if ($result4) {
147 $result5 = dolibarr_del_const($db, "FTP_NAME_".$entry, $conf->entity);
148 }
149 if ($result4) {
150 $result6 = dolibarr_del_const($db, "FTP_PASSIVE_".$entry, $conf->entity);
151 }
152
153 if ($result1 && $result2 && $result3 && $result4 && $result5 && $result6) {
154 $db->commit();
155 header("Location: ".$_SERVER["PHP_SELF"]);
156 exit;
157 } else {
158 $db->rollback();
159 dol_print_error($db);
160 }
161 }
162}
163
164
165/*
166 * View
167 */
168
169$form = new Form($db);
170
171
172$help_url = 'EN:Module_FTP_En|FR:Module_FTP|ES:Módulo_FTP';
173
174llxHeader('', 'FTP', $help_url);
175
176$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
177print load_fiche_titre($langs->trans("FTPClientSetup"), $linkback, 'title_setup');
178print '<br>';
179
180if (!function_exists('ftp_connect')) {
181 print $langs->trans("FTPFeatureNotSupportedByYourPHP");
182} else {
183 // Formulaire ajout
184 print '<form name="ftpconfig" action="ftpclient.php" method="post">';
185 print '<input type="hidden" name="token" value="'.newToken().'">';
186
187 print '<table class="noborder centpercent">';
188 print '<tr class="liste_titre">';
189 print '<td colspan="2">'.$langs->trans("NewFTPClient").'</td>';
190 print '<td>'.$langs->trans("Example").'</td>';
191 print '</tr>';
192
193 print '<tr class="oddeven">';
194 print '<td>'.$langs->trans("Label").'</td>';
195 print '<td><input type="text" name="FTP_NAME_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_NAME_".($lastftpentry + 1)).'" size="64"></td>';
196 print '<td>My FTP access</td>';
197 print '</tr>';
198
199 print '<tr class="oddeven">';
200 print '<td>'.$langs->trans("Server").'</td>';
201 print '<td><input type="text" name="FTP_SERVER_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_SERVER_".($lastftpentry + 1)).'" size="64"></td>';
202 print '<td>localhost</td>';
203 print '</tr>';
204
205 print '<tr class="oddeven">';
206 print '<td width="100">'.$langs->trans("Port").'</td>';
207 print '<td><input type="text" name="FTP_PORT_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_PORT_".($lastftpentry + 1)).'" size="64"></td>';
208 print '<td>21 for pure non encrypted FTP or if option FTP_CONNECT_WITH_SSL (See Home-Setup-Other) is on (FTPS)<br>22 if option FTP_CONNECT_WITH_SFTP (See Home-Setup-Other) is on (SFTP)</td>';
209 print '</tr>';
210
211 print '<tr class="oddeven">';
212 print '<td>'.$langs->trans("User").'</td>';
213 print '<td><input type="text" name="FTP_USER_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_USER_".($lastftpentry + 1)).'" class="minwidth175"></td>';
214 print '<td>myftplogin</td>';
215 print '</tr>';
216
217 print '<tr class="oddeven">';
218 print '<td>'.$langs->trans("Password").'</td>';
219 print '<td><input type="password" name="FTP_PASSWORD_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_PASSWORD_".($lastftpentry + 1)).'" class="minwidth175"></td>';
220 print '<td>myftppassword</td>';
221 print '</tr>';
222
223 print '<tr class="oddeven">';
224 print '<td>'.$langs->trans("FTPPassiveMode").'</td>';
225 $defaultpassive = GETPOST("FTP_PASSIVE_".($lastftpentry + 1));
226 if (!GETPOSTISSET("FTP_PASSIVE_".($lastftpentry + 1))) {
227 $defaultpassive = !getDolGlobalString('FTP_SUGGEST_PASSIVE_BYDEFAULT') ? 0 : 1;
228 }
229 print '<td>'.$form->selectyesno('FTP_PASSIVE_'.($lastftpentry + 1), $defaultpassive, 2).'</td>';
230 print '<td>'.$langs->trans("No").'</td>';
231 print '</tr>';
232
233 print '</table>'; ?>
234 <div class="center">
235 <input type="submit" class="button" value="<?php echo $langs->trans("Add") ?>"></div>
236 <input type="hidden" name="action" value="add">
237 <input type="hidden" name="numero_entry" value="<?php echo($lastftpentry + 1) ?>">
238 <?php
239 print '</form>';
240 print '<br>'; ?>
241
242 <br>
243
244 <?php
245
246 $sql = "select name, value, note from ".MAIN_DB_PREFIX."const";
247 $sql .= " WHERE name like 'FTP_SERVER_%'";
248 $sql .= " ORDER BY name";
249
250 dol_syslog("ftpclient select ftp setup", LOG_DEBUG);
251 $resql = $db->query($sql);
252 if ($resql) {
253 $num = $db->num_rows($resql);
254 $i = 0;
255
256 while ($i < $num) {
257 $obj = $db->fetch_object($resql);
258
259 $reg = array();
260 preg_match('/([0-9]+)$/i', $obj->name, $reg);
261 $idrss = $reg[0];
262 //print "x".join(',',$reg)."=".$obj->name."=".$idrss;
263
264 print '<br>';
265 print '<form name="externalrssconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">';
266 print '<input type="hidden" name="token" value="'.newToken().'">';
267 print '<input type="hidden" name="numero_entry" value="'.$idrss.'">';
268
269 print '<div class="div-table-responsive-no-min">';
270 print '<table class="noborder centpercent">'."\n";
271
272 print '<tr class="liste_titre">';
273 print '<td class="fieldtitle">'.$langs->trans("FTP")." ".($idrss)."</td>";
274 print '<td></td>';
275 print "</tr>";
276
277 $keyforname = "FTP_NAME_".$idrss;
278 $keyforserver = "FTP_SERVER_".$idrss;
279 $keyforport = "FTP_PORT_".$idrss;
280 $keyforuser = "FTP_USER_".$idrss;
281 $keyforpassword = "FTP_PASSWORD_".$idrss;
282 $keyforpassive = "FTP_PASSIVE_".$idrss;
283
284 print '<tr class="oddeven">';
285 print "<td>".$langs->trans("Name")."</td>";
286 print "<td><input type=\"text\" class=\"flat\" name=\"FTP_NAME_".$idrss."\" value=\"".getDolGlobalString($keyforname)."\" size=\"64\"></td>";
287 print "</tr>";
288
289
290 print '<tr class="oddeven">';
291 print "<td>".$langs->trans("Server")."</td>";
292 print "<td><input type=\"text\" class=\"flat\" name=\"FTP_SERVER_".$idrss."\" value=\"".getDolGlobalString($keyforserver)."\" size=\"64\"></td>";
293 print "</tr>";
294
295
296 print '<tr class="oddeven">';
297 print "<td width=\"100\">".$langs->trans("Port")."</td>";
298 print "<td><input type=\"text\" class=\"flat\" name=\"FTP_PORT_".$idrss."\" value=\"".getDolGlobalString($keyforport)."\" size=\"64\"></td>";
299 print "</tr>";
300
301
302 print '<tr class="oddeven">';
303 print "<td width=\"100\">".$langs->trans("User")."</td>";
304 print "<td><input type=\"text\" class=\"flat\" name=\"FTP_USER_".$idrss."\" value=\"".getDolGlobalString($keyforuser)."\" size=\"24\"></td>";
305 print "</tr>";
306
307
308 print '<tr class="oddeven">';
309 print "<td width=\"100\">".$langs->trans("Password")."</td>";
310 print "<td><input type=\"password\" class=\"flat\" name=\"FTP_PASSWORD_".$idrss."\" value=\"".getDolGlobalString($keyforpassword)."\" size=\"24\"></td>";
311 print "</tr>";
312
313
314 print '<tr class="oddeven">';
315 print "<td width=\"100\">".$langs->trans("FTPPassiveMode")."</td>";
316 print '<td>'.$form->selectyesno('FTP_PASSIVE_'.$idrss, getDolGlobalString($keyforpassive), 1).'</td>';
317 print "</tr>";
318
319 print '</table>';
320 print '</div>';
321
322 print '<div class="center">';
323 print '<input type="submit" class="button" name="modify" value="'.$langs->trans("Modify").'">';
324 print " &nbsp; ";
325 print '<input type="submit" class="button" name="delete" value="'.$langs->trans("Delete").'">';
326 print '</center>';
327
328 print "</form>";
329 print '<br><br>';
330
331 $i++;
332 }
333 } else {
334 dol_print_error($db);
335 }
336}
337
338// End of page
339llxFooter();
340$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:158
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:161
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.