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