Index: install/english.lang =================================================================== --- install/english.lang (revision 14094) +++ install/english.lang (working copy) @@ -326,6 +326,7 @@ TG9naW4gRmFpbGVk RXJyb3IgbW92aW5nIHN1YnNlY3Rpb24= Q2FuJ3QgaW5oZXJpdCB0ZW1wbGF0ZSBmcm9tIHRvcCBjYXRlZ29yeQ== + VmFsaWRhdGlvbiBlcnJvciwgcGxlYXNlIGRvdWJsZS1jaGVjayBJbi1Qb3J0YWwgdGFncw== UGFzc3dvcmRzIGRvIG5vdCBtYXRjaCE= UmVxdWlyZWQgZmllbGQoLXMpIG5vdCBmaWxsZWQ= cmVxdWlyZWQgY29sdW1ucyBtaXNzaW5n Index: units/email_events/email_events_event_handler.php =================================================================== --- units/email_events/email_events_event_handler.php (revision 14094) +++ units/email_events/email_events_event_handler.php (working copy) @@ -913,10 +913,13 @@ $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'); if (!$this->Application->isDebugMode(false)) { @@ -942,16 +945,17 @@ } // process replacement tags - $minput_helper =& $this->Application->recallObject('MInputHelper'); - /* @var $minput_helper MInputHelper */ - - $replacement_tags = Array (); - $records = $minput_helper->parseMInputXML( $object->GetDBField('ReplacementTagsXML') ); - - foreach ($records as $record) { - $replacement_tags[ trim($record['Tag']) ] = trim($record['Replacement']); + if ( $object->GetDBField('ReplacementTagsXML') ) { + $minput_helper =& $this->Application->recallObject('MInputHelper'); + /* @var $minput_helper MInputHelper */ + + $replacement_tags = Array (); + $records = $minput_helper->parseMInputXML( $object->GetDBField('ReplacementTagsXML') ); + + foreach ($records as $record) { + $replacement_tags[ trim($record['Tag']) ] = trim($record['Replacement']); + } } - $object->SetDBField('ReplacementTags', $replacement_tags ? serialize($replacement_tags) : NULL); } @@ -1082,4 +1086,65 @@ echo ''; } + + /** + * Validates subject and body fields of Email template + * @param kDBItem $object + */ + function _validateEmailTemplate(&$object) + { + // 1. prepare parser and error handler + safeDefine('DBG_IGNORE_FATAL_ERRORS', 1); + + $error_handlers = $this->Application->errorHandlers; + $this->Application->errorHandlers = Array ( + Array(&$this, '_saveError'), + ); + + $this->Application->InitParser(); + $parser_params = $this->Application->Parser->Params; // backup parser params + + $this->Application->SetVar('email_parsing_error', 0); + + // 2. parse subject + $this->Application->Parser->CompileRaw($object->GetField('Subject'), 'email_subject'); + if ( $this->Application->GetVar('email_parsing_error') ) { + $object->SetError('Subject', 'subject_parsing_error', 'la_error_ParsingError'); + $this->Application->SetVar('email_parsing_error', 0); // reset the error status + } + + // 3. parse body + $this->Application->Parser->CompileRaw($object->GetField('Body'), 'email_template'); + if ( $this->Application->GetVar('email_parsing_error') ) { + $object->SetError('Body', 'body_parsing_error', 'la_error_ParsingError'); + } + + // 4. restore original settings + $this->Application->errorHandlers = $error_handlers; + $this->Application->Parser->SetParams($parser_params); + } + + /** + * Custom error handler that saves errors during email validation + * @param string $errno + * @param string $errstr + * @param string $errfile + * @param string $errline + * @param string $errcontext + */ + function _saveError($errno, $errstr, $errfile, $errline, $errcontext) + { + if ($errno != E_USER_ERROR) { + // ignore all minor errors, except fatals from parser + return true; + } + + $this->Application->SetVar('email_parsing_error', 1); + + if ( $this->Application->isDebugMode() ) { + $this->Application->Debugger->appendHTML('Error in Email Template: ' . $errstr . ' (line: ' . $errline . ')'); + } + + return true; + } } \ No newline at end of file