PageRenderTime 22ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/Tinebase/Model/EmailUser.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 162 lines | 87 code | 15 blank | 60 comment | 10 complexity | 90b2ee72f5d6b0007c6971c2a574be58 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Tinebase
  6. * @subpackage LDAP
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  8. * @copyright Copyright (c) 2009 Metaways Infosystems GmbH (http://www.metaways.de)
  9. * @author Philipp Schuele <p.schuele@metaways.de>
  10. *
  11. * @todo make default quota configurable
  12. */
  13. /**
  14. * class Tinebase_Model_EmailUser
  15. *
  16. * - this class contains all email specific user settings like quota, forwards, ...
  17. *
  18. * @package Tinebase
  19. * @subpackage LDAP
  20. */
  21. class Tinebase_Model_EmailUser extends Tinebase_Record_Abstract
  22. {
  23. protected $_identifier = 'emailUID';
  24. /**
  25. * application the record belongs to
  26. *
  27. * @var string
  28. */
  29. protected $_application = 'Tinebase';
  30. /**
  31. * validators / fields
  32. *
  33. * @var array
  34. */
  35. protected $_validators = array(
  36. 'emailUID' => array('allowEmpty' => true),
  37. 'emailGID' => array('allowEmpty' => true),
  38. 'emailMailQuota' => array('allowEmpty' => true, 'Digits'),
  39. 'emailMailSize' => array('allowEmpty' => true),
  40. 'emailMailRemoteArchiveQuota' => array('allowEmpty' => true, 'Digits'),
  41. 'emailMailRemoteArchiveSize' => array('allowEmpty' => true),
  42. 'emailSieveQuota' => array('allowEmpty' => true, 'Digits'),
  43. 'emailSieveSize' => array('allowEmpty' => true),
  44. 'emailUserId' => array('allowEmpty' => true),
  45. 'emailLastLogin' => array('allowEmpty' => true),
  46. 'emailPassword' => array('allowEmpty' => true),
  47. 'emailForwards' => array('allowEmpty' => true, Zend_Filter_Input::DEFAULT_VALUE => array()),
  48. 'emailForwardOnly' => array('allowEmpty' => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  49. 'emailAliases' => array('allowEmpty' => true, Zend_Filter_Input::DEFAULT_VALUE => array()),
  50. 'emailAddress' => array('allowEmpty' => true),
  51. // dbmail username (tine username + dbmail domain)
  52. 'emailUsername' => array('allowEmpty' => true),
  53. );
  54. /**
  55. * datetime fields
  56. *
  57. * @var array
  58. */
  59. protected $_datetimeFields = array(
  60. 'emailLastLogin'
  61. );
  62. /**
  63. * overwrite constructor to add more filters
  64. *
  65. * @param mixed $_data
  66. * @param bool $_bypassFilters
  67. * @param mixed $_convertDates
  68. * @return void
  69. */
  70. public function __construct($_data = NULL, $_bypassFilters = false, $_convertDates = true)
  71. {
  72. $this->_filters['emailForwardOnly'] = new Zend_Filter_Empty(false);
  73. $this->_filters['emailMailSize'] = new Zend_Filter_Empty(0);
  74. $this->_filters['emailMailQuota'] = new Zend_Filter_Empty(0);
  75. $this->_filters['emailForwards'] = new Zend_Filter_Empty(array());
  76. $this->_filters['emailAliases'] = new Zend_Filter_Empty(array());
  77. return parent::__construct($_data, $_bypassFilters, $_convertDates);
  78. }
  79. /**
  80. * sets the record related properties from user generated input.
  81. *
  82. * Input-filtering and validation by Zend_Filter_Input can enabled and disabled
  83. *
  84. * @param array $_data the new data to set
  85. */
  86. public function setFromArray(array $_data)
  87. {
  88. foreach (array('emailForwards', 'emailAliases') as $arrayField) {
  89. if (isset($_data[$arrayField]) && ! is_array($_data[$arrayField])) {
  90. $_data[$arrayField] = explode(',', preg_replace('/ /', '', $_data[$arrayField]));
  91. }
  92. }
  93. parent::setFromArray($_data);
  94. }
  95. /**
  96. * Additional validation
  97. *
  98. * @param $_throwExceptionOnInvalidData
  99. * @return bool
  100. * @throws Tinebase_Exception_Record_Validation
  101. * @see Tinebase_Record_Abstract::isValid()
  102. */
  103. function validateAliases($_throwExceptionOnInvalidData = false) {
  104. $errors = array();
  105. if($this->__get('emailAliases') !== NULL) {
  106. $smtpConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP, new Tinebase_Config_Struct())->toArray();
  107. $obligatorydomains = explode(',', str_replace(' ', '', $smtpConfig['obligatorydomains']));
  108. $secondaryDomains = explode(',', str_replace(' ', '', $smtpConfig['secondarydomains']));
  109. $allDomains = array_merge(strlen($smtpConfig['secondarydomains']) ? $secondaryDomains : array(),
  110. strlen($smtpConfig['obligatorydomains']) ? $obligatorydomains : array());
  111. array_push($allDomains, substr($this->__get('emailAddress'), strpos($this->__get('emailAddress'),
  112. '@')+1, strlen($this->__get('emailAddress'))));
  113. $valid = true;
  114. foreach ($this->__get('emailAliases') as $email) {
  115. $pos = strpos($email, '@');
  116. $domain = substr($email, $pos+1, strlen($email));
  117. if(! in_array($domain, $allDomains)) {
  118. $valid = false;
  119. //TODO fix translation
  120. $message = sprintf(Tinebase_Translation::getTranslation('Tinebase')->_('The email alias (%s) has not a allowed domain.'), $email);
  121. array_push($errors, array('id' => 'emailAliases', 'msg' => $message));
  122. if (Tinebase_Core::isLogLevel(Zend_Log::ERR)) Tinebase_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . $message);
  123. }
  124. }
  125. if (!$valid && $_throwExceptionOnInvalidData) {
  126. $e = new Tinebase_Exception_Record_Validation($message);
  127. Tinebase_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ":\n" . print_r($errors, true). $e);
  128. throw $e;
  129. }
  130. }
  131. return $errors;
  132. }
  133. public function validateExistentAliases(){
  134. $errors = array();
  135. $checks = Admin_Controller_User::getInstance()->checkEmail($this->__get('emailAliases'));
  136. if($checks['status'] == 'failure') {
  137. foreach ($checks['data'] as $check) {
  138. if(count($check['failures']) > 0) {
  139. //TODO fix translation
  140. $message = sprintf(Tinebase_Translation::getTranslation('Tinebase')->_('The email alias (%s) is already in system. Please choose another alias.'), $check['mail']);
  141. $errors[] = $check['mail'];
  142. }
  143. }
  144. }
  145. return $errors;
  146. }
  147. }