controller.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * Send a mail to test the settings
  71. */
  72. public static function sendTestMail() {
  73. \OC_Util::checkAdminUser();
  74. \OCP\JSON::callCheck();
  75. $l = \OC_L10N::get('settings');
  76. $email = \OC_Preferences::getValue(\OC_User::getUser(), 'settings', 'email', '');
  77. if (!empty($email)) {
  78. $defaults = new \OC_Defaults();
  79. try {
  80. \OC_Mail::send($email, $_POST['user'],
  81. $l->t('test email settings'),
  82. $l->t('If you received this email, the settings seem to be correct.'),
  83. \OCP\Util::getDefaultEmailAddress('no-reply'), $defaults->getName());
  84. } catch (\Exception $e) {
  85. $message = $l->t('A problem occurred while sending the e-mail. Please revisit your settings.');
  86. \OC_JSON::error( array( "data" => array( "message" => $message)) );
  87. exit;
  88. }
  89. \OC_JSON::success(array("data" => array( "message" => $l->t("Email sent") )));
  90. } else {
  91. $message = $l->t('You need to set your user email before being able to send test emails.');
  92. \OC_JSON::error( array( "data" => array( "message" => $message)) );
  93. }
  94. }
  95. /**
  96. * Get the field name to use it in error messages
  97. *
  98. * @param string $setting
  99. * @param \OC_L10N $l
  100. * @return string
  101. */
  102. public static function getFieldname($setting, $l) {
  103. switch ($setting) {
  104. case 'mail_smtpmode':
  105. return $l->t( 'Send mode' );
  106. case 'mail_smtpsecure':
  107. return $l->t( 'Encryption' );
  108. case 'mail_smtpauthtype':
  109. return $l->t( 'Authentication method' );
  110. }
  111. }
  112. }