You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.7 KiB

  1. <?php
  2. /**
  3. * @author Joas Schilling
  4. * @copyright 2014 Joas Schilling nickvergessen@owncloud.com
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or any later version.
  10. *
  11. * This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public
  17. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. namespace OC\Settings\Admin;
  20. class Controller {
  21. /**
  22. * Set mail settings
  23. */
  24. public static function setMailSettings() {
  25. \OC_Util::checkAdminUser();
  26. \OCP\JSON::callCheck();
  27. $l = \OC_L10N::get('settings');
  28. $smtp_settings = array(
  29. 'mail_domain' => null,
  30. 'mail_from_address' => null,
  31. 'mail_smtpmode' => array('sendmail', 'smtp', 'qmail', 'php'),
  32. 'mail_smtpsecure' => array('', 'ssl', 'tls'),
  33. 'mail_smtphost' => null,
  34. 'mail_smtpport' => null,
  35. 'mail_smtpauthtype' => array('LOGIN', 'PLAIN', 'NTLM'),
  36. 'mail_smtpauth' => true,
  37. 'mail_smtpname' => null,
  38. 'mail_smtppassword' => null,
  39. );
  40. foreach ($smtp_settings as $setting => $validate) {
  41. if (!$validate) {
  42. if (!isset($_POST[$setting]) || $_POST[$setting] === '') {
  43. \OC_Config::deleteKey( $setting );
  44. } else {
  45. \OC_Config::setValue( $setting, $_POST[$setting] );
  46. }
  47. }
  48. else if (is_bool($validate)) {
  49. if (!empty($_POST[$setting])) {
  50. \OC_Config::setValue( $setting, (bool) $_POST[$setting] );
  51. } else {
  52. \OC_Config::deleteKey( $setting );
  53. }
  54. }
  55. else if (is_array($validate)) {
  56. if (!isset($_POST[$setting]) || $_POST[$setting] === '') {
  57. \OC_Config::deleteKey( $setting );
  58. } else if (in_array($_POST[$setting], $validate)) {
  59. \OC_Config::setValue( $setting, $_POST[$setting] );
  60. } else {
  61. $message = $l->t('Invalid value supplied for %s', array(self::getFieldname($setting, $l)));
  62. \OC_JSON::error( array( "data" => array( "message" => $message)) );
  63. exit;
  64. }
  65. }
  66. }
  67. \OC_JSON::success(array("data" => array( "message" => $l->t("Saved") )));
  68. }
  69. /**
  70. * Get the field name to use it in error messages
  71. *
  72. * @param $setting string
  73. * @param $l \OC_L10N
  74. * @return string
  75. */
  76. public static function getFieldname($setting, $l) {
  77. switch ($setting) {
  78. case 'mail_smtpmode':
  79. return $l->t( 'Send mode' );
  80. case 'mail_smtpsecure':
  81. return $l->t( 'Encryption' );
  82. case 'mail_smtpauthtype':
  83. return $l->t( 'Authentification method' );
  84. }
  85. }
  86. }