dolibarr 21.0.0-beta
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 *
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 */
19
26// Load Dolibarr environment
27require '../../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29
38$langs->loadLangs(array("admin", "other"));
39
40$def = array();
41$lastftpentry = 0;
42
43$action = GETPOST('action', 'aZ09');
44$entry = GETPOST('numero_entry', 'alpha');
45
46// Security check
47if (!$user->admin) {
49}
50
51
52/*
53 * Action
54 */
55
56// Get value for $lastftpentry
57$sql = "select MAX(name) as name from ".MAIN_DB_PREFIX."const";
58$sql .= " WHERE name like 'FTP_SERVER_%'";
59$result = $db->query($sql);
60if ($result) {
61 $obj = $db->fetch_object($result);
62 $reg = array();
63 preg_match('/([0-9]+)$/i', $obj->name, $reg);
64 if (!empty($reg[1])) {
65 $lastftpentry = $reg[1];
66 }
67} else {
68 dol_print_error($db);
69}
70
71if ($action == 'add' || GETPOST('modify', 'alpha')) {
72 $ftp_name = "FTP_NAME_".$entry;
73 $ftp_server = "FTP_SERVER_".$entry;
74
75 $error = 0;
76
77 if (!GETPOST($ftp_name, 'alpha')) {
78 $error = 1;
79 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Label")), null, 'errors');
80 }
81
82 if (!GETPOST($ftp_server, 'alpha')) {
83 $error = 1;
84 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Server")), null, 'errors');
85 }
86
87 if (!$error) {
88 $ftp_port = "FTP_PORT_".$entry;
89 $ftp_user = "FTP_USER_".$entry;
90 $ftp_password = "FTP_PASSWORD_".$entry;
91 $ftp_passive = "FTP_PASSIVE_".$entry;
92
93 $db->begin();
94
95 $result1 = dolibarr_set_const($db, "FTP_PORT_".$entry, GETPOST($ftp_port, 'alpha'), 'chaine', 0, '', $conf->entity);
96 if ($result1) {
97 $result2 = dolibarr_set_const($db, "FTP_SERVER_".$entry, GETPOST($ftp_server, 'alpha'), 'chaine', 0, '', $conf->entity);
98 }
99 if ($result2) {
100 $result3 = dolibarr_set_const($db, "FTP_USER_".$entry, GETPOST($ftp_user, 'alpha'), 'chaine', 0, '', $conf->entity);
101 }
102 if ($result3) {
103 $result4 = dolibarr_set_const($db, "FTP_PASSWORD_".$entry, GETPOST($ftp_password, 'alpha'), 'chaine', 0, '', $conf->entity);
104 }
105 if ($result4) {
106 $result5 = dolibarr_set_const($db, "FTP_NAME_".$entry, GETPOST($ftp_name, 'alpha'), 'chaine', 0, '', $conf->entity);
107 }
108 if ($result5) {
109 $result6 = dolibarr_set_const($db, "FTP_PASSIVE_".$entry, GETPOST($ftp_passive, 'alpha'), 'chaine', 0, '', $conf->entity);
110 }
111
112 if ($result1 && $result2 && $result3 && $result4 && $result5 && $result6) {
113 $db->commit();
114 header("Location: ".$_SERVER["PHP_SELF"]);
115 exit;
116 } else {
117 $db->rollback();
118 dol_print_error($db);
119 }
120 }
121}
122
123if (GETPOST('delete', 'alpha')) {
124 if ($entry) {
125 $db->begin();
126
127 $result1 = dolibarr_del_const($db, "FTP_PORT_".$entry, $conf->entity);
128 if ($result1) {
129 $result2 = dolibarr_del_const($db, "FTP_SERVER_".$entry, $conf->entity);
130 }
131 if ($result2) {
132 $result3 = dolibarr_del_const($db, "FTP_USER_".$entry, $conf->entity);
133 }
134 if ($result3) {
135 $result4 = dolibarr_del_const($db, "FTP_PASSWORD_".$entry, $conf->entity);
136 }
137 if ($result4) {
138 $result5 = dolibarr_del_const($db, "FTP_NAME_".$entry, $conf->entity);
139 }
140 if ($result4) {
141 $result6 = dolibarr_del_const($db, "FTP_PASSIVE_".$entry, $conf->entity);
142 }
143
144 if ($result1 && $result2 && $result3 && $result4 && $result5 && $result6) {
145 $db->commit();
146 header("Location: ".$_SERVER["PHP_SELF"]);
147 exit;
148 } else {
149 $db->rollback();
150 dol_print_error($db);
151 }
152 }
153}
154
155
156/*
157 * View
158 */
159
160$form = new Form($db);
161
162
163$help_url = 'EN:Module_FTP_En|FR:Module_FTP|ES:Módulo_FTP';
164
165llxHeader('', 'FTP', $help_url);
166
167$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
168print load_fiche_titre($langs->trans("FTPClientSetup"), $linkback, 'title_setup');
169print '<br>';
170
171if (!function_exists('ftp_connect')) {
172 print $langs->trans("FTPFeatureNotSupportedByYourPHP");
173} else {
174 // Formulaire ajout
175 print '<form name="ftpconfig" action="ftpclient.php" method="post">';
176 print '<input type="hidden" name="token" value="'.newToken().'">';
177
178 print '<table class="noborder centpercent">';
179 print '<tr class="liste_titre">';
180 print '<td colspan="2">'.$langs->trans("NewFTPClient").'</td>';
181 print '<td>'.$langs->trans("Example").'</td>';
182 print '</tr>';
183
184 print '<tr class="oddeven">';
185 print '<td>'.$langs->trans("Label").'</td>';
186 print '<td><input type="text" name="FTP_NAME_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_NAME_".($lastftpentry + 1)).'" size="64"></td>';
187 print '<td>My FTP access</td>';
188 print '</tr>';
189
190 print '<tr class="oddeven">';
191 print '<td>'.$langs->trans("Server").'</td>';
192 print '<td><input type="text" name="FTP_SERVER_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_SERVER_".($lastftpentry + 1)).'" size="64"></td>';
193 print '<td>localhost</td>';
194 print '</tr>';
195
196 print '<tr class="oddeven">';
197 print '<td width="100">'.$langs->trans("Port").'</td>';
198 print '<td><input type="text" name="FTP_PORT_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_PORT_".($lastftpentry + 1)).'" size="64"></td>';
199 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>';
200 print '</tr>';
201
202 print '<tr class="oddeven">';
203 print '<td>'.$langs->trans("User").'</td>';
204 print '<td><input type="text" name="FTP_USER_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_USER_".($lastftpentry + 1)).'" class="minwidth175"></td>';
205 print '<td>myftplogin</td>';
206 print '</tr>';
207
208 print '<tr class="oddeven">';
209 print '<td>'.$langs->trans("Password").'</td>';
210 print '<td><input type="password" name="FTP_PASSWORD_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_PASSWORD_".($lastftpentry + 1)).'" class="minwidth175"></td>';
211 print '<td>myftppassword</td>';
212 print '</tr>';
213
214 print '<tr class="oddeven">';
215 print '<td>'.$langs->trans("FTPPassiveMode").'</td>';
216 $defaultpassive = GETPOST("FTP_PASSIVE_".($lastftpentry + 1));
217 if (!GETPOSTISSET("FTP_PASSIVE_".($lastftpentry + 1))) {
218 $defaultpassive = !getDolGlobalString('FTP_SUGGEST_PASSIVE_BYDEFAULT') ? 0 : 1;
219 }
220 print '<td>'.$form->selectyesno('FTP_PASSIVE_'.($lastftpentry + 1), $defaultpassive, 2).'</td>';
221 print '<td>'.$langs->trans("No").'</td>';
222 print '</tr>';
223
224 print '</table>'; ?>
225 <div class="center">
226 <input type="submit" class="button" value="<?php echo $langs->trans("Add") ?>"></div>
227 <input type="hidden" name="action" value="add">
228 <input type="hidden" name="numero_entry" value="<?php echo($lastftpentry + 1) ?>">
229 <?php
230 print '</form>';
231 print '<br>'; ?>
232
233 <br>
234
235 <?php
236
237 $sql = "select name, value, note from ".MAIN_DB_PREFIX."const";
238 $sql .= " WHERE name like 'FTP_SERVER_%'";
239 $sql .= " ORDER BY name";
240
241 dol_syslog("ftpclient select ftp setup", LOG_DEBUG);
242 $resql = $db->query($sql);
243 if ($resql) {
244 $num = $db->num_rows($resql);
245 $i = 0;
246
247 while ($i < $num) {
248 $obj = $db->fetch_object($resql);
249
250 $reg = array();
251 preg_match('/([0-9]+)$/i', $obj->name, $reg);
252 $idrss = $reg[0];
253 //print "x".join(',',$reg)."=".$obj->name."=".$idrss;
254
255 print '<br>';
256 print '<form name="externalrssconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">';
257 print '<input type="hidden" name="token" value="'.newToken().'">';
258 print '<input type="hidden" name="numero_entry" value="'.$idrss.'">';
259
260 print '<div class="div-table-responsive-no-min">';
261 print '<table class="noborder centpercent">'."\n";
262
263 print '<tr class="liste_titre">';
264 print '<td class="fieldtitle">'.$langs->trans("FTP")." ".($idrss)."</td>";
265 print '<td></td>';
266 print "</tr>";
267
268 $keyforname = "FTP_NAME_".$idrss;
269 $keyforserver = "FTP_SERVER_".$idrss;
270 $keyforport = "FTP_PORT_".$idrss;
271 $keyforuser = "FTP_USER_".$idrss;
272 $keyforpassword = "FTP_PASSWORD_".$idrss;
273 $keyforpassive = "FTP_PASSIVE_".$idrss;
274
275 print '<tr class="oddeven">';
276 print "<td>".$langs->trans("Name")."</td>";
277 print "<td><input type=\"text\" class=\"flat\" name=\"FTP_NAME_".$idrss."\" value=\"".getDolGlobalString($keyforname)."\" size=\"64\"></td>";
278 print "</tr>";
279
280
281 print '<tr class="oddeven">';
282 print "<td>".$langs->trans("Server")."</td>";
283 print "<td><input type=\"text\" class=\"flat\" name=\"FTP_SERVER_".$idrss."\" value=\"".getDolGlobalString($keyforserver)."\" size=\"64\"></td>";
284 print "</tr>";
285
286
287 print '<tr class="oddeven">';
288 print "<td width=\"100\">".$langs->trans("Port")."</td>";
289 print "<td><input type=\"text\" class=\"flat\" name=\"FTP_PORT_".$idrss."\" value=\"".getDolGlobalString($keyforport)."\" size=\"64\"></td>";
290 print "</tr>";
291
292
293 print '<tr class="oddeven">';
294 print "<td width=\"100\">".$langs->trans("User")."</td>";
295 print "<td><input type=\"text\" class=\"flat\" name=\"FTP_USER_".$idrss."\" value=\"".getDolGlobalString($keyforuser)."\" size=\"24\"></td>";
296 print "</tr>";
297
298
299 print '<tr class="oddeven">';
300 print "<td width=\"100\">".$langs->trans("Password")."</td>";
301 print "<td><input type=\"password\" class=\"flat\" name=\"FTP_PASSWORD_".$idrss."\" value=\"".getDolGlobalString($keyforpassword)."\" size=\"24\"></td>";
302 print "</tr>";
303
304
305 print '<tr class="oddeven">';
306 print "<td width=\"100\">".$langs->trans("FTPPassiveMode")."</td>";
307 print '<td>'.$form->selectyesno('FTP_PASSIVE_'.$idrss, getDolGlobalString($keyforpassive), 1).'</td>';
308 print "</tr>";
309
310 print '</table>';
311 print '</div>';
312
313 print '<div class="center">';
314 print '<input type="submit" class="button" name="modify" value="'.$langs->trans("Modify").'">';
315 print " &nbsp; ";
316 print '<input type="submit" class="button" name="delete" value="'.$langs->trans("Delete").'">';
317 print '</center>';
318
319 print "</form>";
320 print '<br><br>';
321
322 $i++;
323 }
324 } else {
325 dol_print_error($db);
326 }
327}
328
329// End of page
330llxFooter();
331$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.
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:71
Class to manage generation of HTML components Only common components must be here.
llxFooter()
Footer empty.
Definition document.php:107
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:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:149
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:152
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.