Index: branches/5.2.x/core/units/email_events/email_events_event_handler.php =================================================================== diff -u -r15222 -r15225 --- branches/5.2.x/core/units/email_events/email_events_event_handler.php (.../email_events_event_handler.php) (revision 15222) +++ branches/5.2.x/core/units/email_events/email_events_event_handler.php (.../email_events_event_handler.php) (revision 15225) @@ -1,6 +1,6 @@ getObject(); /* @var $object kDBItem */ - // validate email subject and body for parsing errors - $this->_validateEmailTemplate($object); - - // validate sender and recipient addresses - if ( $object->GetDBField('CustomSender') ) { - $this->_validateAddress($event, 'Sender'); - } - $this->_validateAddress($event, 'Recipient'); - if ( !$this->Application->isDebugMode(false) ) { // only allow to enable/disable event while in debug mode $to_restore = Array ('Enabled', 'AllowChangingSender', 'AllowChangingRecipient'); @@ -475,53 +466,6 @@ } /** - * Validates address using given field prefix - * - * @param kEvent $event - * @param string $field_prefix - */ - function _validateAddress($event, $field_prefix) - { - $object = $event->getObject(); - /* @var $object kDBItem */ - - $address_type = $object->GetDBField($field_prefix . 'AddressType'); - $object->setRequired($field_prefix . 'Address', $address_type > 0); - $address = $object->GetDBField($field_prefix . 'Address'); - - if ( !$address ) { - // don't validate against empty address - return; - } - - switch ($address_type) { - case EmailEvent::ADDRESS_TYPE_EMAIL: - if ( !preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $address) ) { - $object->SetError($field_prefix . 'Address', 'invalid_email'); - } - break; - - case EmailEvent::ADDRESS_TYPE_USER: - $sql = 'SELECT PortalUserId - FROM ' . TABLE_PREFIX . 'Users - WHERE Username = ' . $this->Conn->qstr($address); - if ( !$this->Conn->GetOne($sql) ) { - $object->SetError($field_prefix . 'Address', 'invalid_user'); - } - break; - - case EmailEvent::ADDRESS_TYPE_GROUP: - $sql = 'SELECT GroupId - FROM ' . TABLE_PREFIX . 'UserGroups - WHERE Name = ' . $this->Conn->qstr($address); - if ( !$this->Conn->GetOne($sql) ) { - $object->SetError($field_prefix . 'Address', 'invalid_group'); - } - break; - } - } - - /** * Don't allow to enable/disable events in non-debug mode * * @param kEvent $event @@ -611,6 +555,31 @@ } /** + * Does custom validation + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnBeforeItemValidate(kEvent $event) + { + parent::OnBeforeItemValidate($event); + + $object = $event->getObject(); + /* @var $object kDBItem */ + + // validate email subject and body for parsing errors + $this->_validateEmailTemplate($object); + + // validate sender and recipient addresses + if ( $object->GetDBField('CustomSender') ) { + $this->_validateAddress($event, 'Sender'); + } + + $this->_validateAddress($event, 'Recipient'); + } + + /** * Validates subject and body fields of Email template * * @param kDBItem $object @@ -619,31 +588,60 @@ */ protected function _validateEmailTemplate($object) { - $this->parseField($object, 'Subject'); - $this->parseField($object, 'HtmlBody'); - $this->parseField($object, 'PlainTextBody'); + $email_message_helper = $this->Application->recallObject('kEmailMessageHelper'); + /* @var $email_message_helper kEmailMessageHelper */ + + $email_message_helper->parseField($object, 'Subject'); + $email_message_helper->parseField($object, 'HtmlBody'); + $email_message_helper->parseField($object, 'PlainTextBody'); } /** - * Parses contents of given object field and sets error, when invalid in-portal tags found - * @param kDBItem $object - * @param string $field + * Validates address using given field prefix + * + * @param kEvent $event + * @param string $field_prefix * @return void * @access protected */ - protected function parseField($object, $field) + protected function _validateAddress($event, $field_prefix) { - $this->Application->InitParser(); + $object = $event->getObject(); + /* @var $object kDBItem */ - try { - $this->Application->Parser->CompileRaw($object->GetField($field), 'email_template'); + $address_type = $object->GetDBField($field_prefix . 'AddressType'); + $object->setRequired($field_prefix . 'Address', $address_type > 0); + $address = $object->GetDBField($field_prefix . 'Address'); + + if ( !$address ) { + // don't validate against empty address + return; } - catch (ParserException $e) { - if ( $this->Application->isDebugMode() ) { - $this->Application->Debugger->appendHTML('Error in Email Template: ' . $e->getMessage() . ' (line: ' . $e->getLine() . ')'); - } - $object->SetError($field, 'parsing_error'); + switch ($address_type) { + case EmailEvent::ADDRESS_TYPE_EMAIL: + if ( !preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $address) ) { + $object->SetError($field_prefix . 'Address', 'invalid_email'); + } + break; + + case EmailEvent::ADDRESS_TYPE_USER: + $sql = 'SELECT PortalUserId + FROM ' . TABLE_PREFIX . 'Users + WHERE Username = ' . $this->Conn->qstr($address); + if ( !$this->Conn->GetOne($sql) ) { + $object->SetError($field_prefix . 'Address', 'invalid_user'); + } + break; + + case EmailEvent::ADDRESS_TYPE_GROUP: + $sql = 'SELECT GroupId + FROM ' . TABLE_PREFIX . 'UserGroups + WHERE Name = ' . $this->Conn->qstr($address); + if ( !$this->Conn->GetOne($sql) ) { + $object->SetError($field_prefix . 'Address', 'invalid_group'); + } + break; } }