ftpclient.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /* Copyright (C) 2004-2016 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. /**
  19. * \file htdocs/ftp/admin/ftpclient.php
  20. * \ingroup ftp
  21. * \brief Admin page to setup FTP client module
  22. */
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  25. $langs->loadLangs(array("admin", "other"));
  26. // Security check
  27. if (!$user->admin) {
  28. accessforbidden();
  29. }
  30. $def = array();
  31. $lastftpentry = 0;
  32. $action = GETPOST('action', 'aZ09');
  33. $entry = GETPOST('numero_entry', 'alpha');
  34. /*
  35. * Action
  36. */
  37. // Get value for $lastftpentry
  38. $sql = "select MAX(name) as name from ".MAIN_DB_PREFIX."const";
  39. $sql .= " WHERE name like 'FTP_SERVER_%'";
  40. $result = $db->query($sql);
  41. if ($result) {
  42. $obj = $db->fetch_object($result);
  43. preg_match('/([0-9]+)$/i', $obj->name, $reg);
  44. if ($reg[1]) {
  45. $lastftpentry = $reg[1];
  46. }
  47. } else {
  48. dol_print_error($db);
  49. }
  50. if ($action == 'add' || GETPOST('modify', 'alpha')) {
  51. $ftp_name = "FTP_NAME_".$entry;
  52. $ftp_server = "FTP_SERVER_".$entry;
  53. $error = 0;
  54. if (!GETPOST("$ftp_name", 'alpha')) {
  55. $error = 1;
  56. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Label")), null, 'errors');
  57. }
  58. if (!GETPOST("$ftp_server", 'alpha')) {
  59. $error = 1;
  60. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Server")), null, 'errors');
  61. }
  62. if (!$error) {
  63. $ftp_port = "FTP_PORT_".$entry;
  64. $ftp_user = "FTP_USER_".$entry;
  65. $ftp_password = "FTP_PASSWORD_".$entry;
  66. $ftp_passive = "FTP_PASSIVE_".$entry;
  67. $db->begin();
  68. $result1 = dolibarr_set_const($db, "FTP_PORT_".$entry, GETPOST($ftp_port, 'alpha'), 'chaine', 0, '', $conf->entity);
  69. if ($result1) {
  70. $result2 = dolibarr_set_const($db, "FTP_SERVER_".$entry, GETPOST($ftp_server, 'alpha'), 'chaine', 0, '', $conf->entity);
  71. }
  72. if ($result2) {
  73. $result3 = dolibarr_set_const($db, "FTP_USER_".$entry, GETPOST($ftp_user, 'alpha'), 'chaine', 0, '', $conf->entity);
  74. }
  75. if ($result3) {
  76. $result4 = dolibarr_set_const($db, "FTP_PASSWORD_".$entry, GETPOST($ftp_password, 'alpha'), 'chaine', 0, '', $conf->entity);
  77. }
  78. if ($result4) {
  79. $result5 = dolibarr_set_const($db, "FTP_NAME_".$entry, GETPOST($ftp_name, 'alpha'), 'chaine', 0, '', $conf->entity);
  80. }
  81. if ($result5) {
  82. $result6 = dolibarr_set_const($db, "FTP_PASSIVE_".$entry, GETPOST($ftp_passive, 'alpha'), 'chaine', 0, '', $conf->entity);
  83. }
  84. if ($result1 && $result2 && $result3 && $result4 && $result5 && $result6) {
  85. $db->commit();
  86. header("Location: ".$_SERVER["PHP_SELF"]);
  87. exit;
  88. } else {
  89. $db->rollback();
  90. dol_print_error($db);
  91. }
  92. }
  93. }
  94. if (GETPOST('delete', 'alpha')) {
  95. if ($entry) {
  96. $db->begin();
  97. $result1 = dolibarr_del_const($db, "FTP_PORT_".$entry, $conf->entity);
  98. if ($result1) {
  99. $result2 = dolibarr_del_const($db, "FTP_SERVER_".$entry, $conf->entity);
  100. }
  101. if ($result2) {
  102. $result3 = dolibarr_del_const($db, "FTP_USER_".$entry, $conf->entity);
  103. }
  104. if ($result3) {
  105. $result4 = dolibarr_del_const($db, "FTP_PASSWORD_".$entry, $conf->entity);
  106. }
  107. if ($result4) {
  108. $result5 = dolibarr_del_const($db, "FTP_NAME_".$entry, $conf->entity);
  109. }
  110. if ($result4) {
  111. $result6 = dolibarr_del_const($db, "FTP_PASSIVE_".$entry, $conf->entity);
  112. }
  113. if ($result1 && $result2 && $result3 && $result4 && $result5 && $result6) {
  114. $db->commit();
  115. header("Location: ".$_SERVER["PHP_SELF"]);
  116. exit;
  117. } else {
  118. $db->rollback();
  119. dol_print_error($db);
  120. }
  121. }
  122. }
  123. /*
  124. * View
  125. */
  126. $form = new Form($db);
  127. $help_url = 'EN:Module_FTP_En|FR:Module_FTP|ES:Módulo_FTP';
  128. llxHeader('', 'FTP', $help_url);
  129. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  130. print load_fiche_titre($langs->trans("FTPClientSetup"), $linkback, 'title_setup');
  131. print '<br>';
  132. if (!function_exists('ftp_connect')) {
  133. print $langs->trans("FTPFeatureNotSupportedByYourPHP");
  134. } else {
  135. // Formulaire ajout
  136. print '<form name="ftpconfig" action="ftpclient.php" method="post">';
  137. print '<input type="hidden" name="token" value="'.newToken().'">';
  138. print '<table class="noborder centpercent">';
  139. print '<tr class="liste_titre">';
  140. print '<td colspan="2">'.$langs->trans("NewFTPClient").'</td>';
  141. print '<td>'.$langs->trans("Example").'</td>';
  142. print '</tr>';
  143. print '<tr class="pair">';
  144. print '<td>'.$langs->trans("Label").'</td>';
  145. print '<td><input type="text" name="FTP_NAME_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_NAME_".($lastftpentry + 1)).'" size="64"></td>';
  146. print '<td>My FTP access</td>';
  147. print '</tr>';
  148. print '<tr class="impair">';
  149. print '<td>'.$langs->trans("Server").'</td>';
  150. print '<td><input type="text" name="FTP_SERVER_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_SERVER_".($lastftpentry + 1)).'" size="64"></td>';
  151. print '<td>localhost</td>';
  152. print '</tr>';
  153. print '<tr class="pair">';
  154. print '<td width="100">'.$langs->trans("Port").'</td>';
  155. print '<td><input type="text" name="FTP_PORT_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_PORT_".($lastftpentry + 1)).'" size="64"></td>';
  156. 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>';
  157. print '</tr>';
  158. print '<tr class="impair">';
  159. print '<td>'.$langs->trans("User").'</td>';
  160. print '<td><input type="text" name="FTP_USER_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_USER_".($lastftpentry + 1)).'" class="minwidth175"></td>';
  161. print '<td>myftplogin</td>';
  162. print '</tr>';
  163. print '<tr class="pair">';
  164. print '<td>'.$langs->trans("Password").'</td>';
  165. print '<td><input type="password" name="FTP_PASSWORD_'.($lastftpentry + 1).'" value="'.GETPOST("FTP_PASSWORD_".($lastftpentry + 1)).'" class="minwidth175"></td>';
  166. print '<td>myftppassword</td>';
  167. print '</tr>';
  168. print '<tr class="impair">';
  169. print '<td>'.$langs->trans("FTPPassiveMode").'</td>';
  170. $defaultpassive = GETPOST("FTP_PASSIVE_".($lastftpentry + 1));
  171. if (!GETPOSTISSET("FTP_PASSIVE_".($lastftpentry + 1))) {
  172. $defaultpassive = empty($conf->global->FTP_SUGGEST_PASSIVE_BYDEFAULT) ? 0 : 1;
  173. }
  174. print '<td>'.$form->selectyesno('FTP_PASSIVE_'.($lastftpentry + 1), $defaultpassive, 2).'</td>';
  175. print '<td>'.$langs->trans("No").'</td>';
  176. print '</tr>';
  177. print '</table>';
  178. ?>
  179. <br><div class="center"><input type="submit" class="button" value="<?php echo $langs->trans("Add") ?>"></div>
  180. <input type="hidden" name="action" value="add">
  181. <input type="hidden" name="numero_entry" value="<?php echo ($lastftpentry + 1) ?>">
  182. <?php
  183. print '</form>';
  184. ?>
  185. <br>
  186. <?php
  187. $sql = "select name, value, note from ".MAIN_DB_PREFIX."const";
  188. $sql .= " WHERE name like 'FTP_SERVER_%'";
  189. $sql .= " ORDER BY name";
  190. dol_syslog("ftpclient select ftp setup", LOG_DEBUG);
  191. $resql = $db->query($sql);
  192. if ($resql) {
  193. $num = $db->num_rows($resql);
  194. $i = 0;
  195. while ($i < $num) {
  196. $obj = $db->fetch_object($resql);
  197. $reg = array();
  198. preg_match('/([0-9]+)$/i', $obj->name, $reg);
  199. $idrss = $reg[0];
  200. //print "x".join(',',$reg)."=".$obj->name."=".$idrss;
  201. print "<form name=\"externalrssconfig\" action=\"".$_SERVER["PHP_SELF"]."\" method=\"post\">";
  202. print '<input type="hidden" name="token" value="'.newToken().'">';
  203. print '<input type="hidden" name="numero_entry" value="'.$idrss.'">';
  204. print '<table class="noborder centpercent">'."\n";
  205. print '<tr class="liste_titre">';
  206. print '<td class="fieldtitle">'.$langs->trans("FTP")." ".($idrss)."</td>";
  207. print '<td></td>';
  208. print "</tr>";
  209. $keyforname = "FTP_NAME_".$idrss;
  210. $keyforserver = "FTP_SERVER_".$idrss;
  211. $keyforport = "FTP_PORT_".$idrss;
  212. $keyforuser = "FTP_USER_".$idrss;
  213. $keyforpassword = "FTP_PASSWORD_".$idrss;
  214. $keyforpassive = "FTP_PASSIVE_".$idrss;
  215. print '<tr class="oddeven">';
  216. print "<td>".$langs->trans("Name")."</td>";
  217. print "<td><input type=\"text\" class=\"flat\" name=\"FTP_NAME_".$idrss."\" value=\"".$conf->global->$keyforname."\" size=\"64\"></td>";
  218. print "</tr>";
  219. print '<tr class="oddeven">';
  220. print "<td>".$langs->trans("Server")."</td>";
  221. print "<td><input type=\"text\" class=\"flat\" name=\"FTP_SERVER_".$idrss."\" value=\"".$conf->global->$keyforserver."\" size=\"64\"></td>";
  222. print "</tr>";
  223. print '<tr class="oddeven">';
  224. print "<td width=\"100\">".$langs->trans("Port")."</td>";
  225. print "<td><input type=\"text\" class=\"flat\" name=\"FTP_PORT_".$idrss."\" value=\"".$conf->global->$keyforport."\" size=\"64\"></td>";
  226. print "</tr>";
  227. print '<tr class="oddeven">';
  228. print "<td width=\"100\">".$langs->trans("User")."</td>";
  229. print "<td><input type=\"text\" class=\"flat\" name=\"FTP_USER_".$idrss."\" value=\"".$conf->global->$keyforuser."\" size=\"24\"></td>";
  230. print "</tr>";
  231. print '<tr class="oddeven">';
  232. print "<td width=\"100\">".$langs->trans("Password")."</td>";
  233. print "<td><input type=\"password\" class=\"flat\" name=\"FTP_PASSWORD_".$idrss."\" value=\"".$conf->global->$keyforpassword."\" size=\"24\"></td>";
  234. print "</tr>";
  235. print '<tr class="oddeven">';
  236. print "<td width=\"100\">".$langs->trans("FTPPassiveMode")."</td>";
  237. print '<td>'.$form->selectyesno('FTP_PASSIVE_'.$idrss, $conf->global->$keyforpassive, 1).'</td>';
  238. print "</tr>";
  239. print "<tr>";
  240. print "<td colspan=\"2\" class=\"center\">";
  241. print "<input type=\"submit\" class=\"button\" name=\"modify\" value=\"".$langs->trans("Modify")."\">";
  242. print " &nbsp; ";
  243. print "<input type=\"submit\" class=\"button\" name=\"delete\" value=\"".$langs->trans("Delete")."\">";
  244. print "</td>";
  245. print "</tr>";
  246. print '</table>';
  247. print "</form>";
  248. print '<br>';
  249. $i++;
  250. }
  251. } else {
  252. dol_print_error($db);
  253. }
  254. }
  255. // End of page
  256. llxFooter();
  257. $db->close();