/** * Validate element value * * If a translation adapter is registered, any error messages will be * translated according to the current locale, using the given error code; * if no matching translation is found, the original message will be * utilized. * * Note: The *filtered* value is validated. * * @param mixed $value * @param mixed $context * @return boolean */ public function isValid($value, $context = null) { $this->setValue($value); $value = $this->getValue(); if (('' === $value || null === $value) && !$this->isRequired() && $this->getAllowEmpty()) { return true; } if ($this->isRequired() && $this->autoInsertNotEmptyValidator() && !$this->getValidator('NotEmpty')) { $validators = $this->getValidators(); $notEmpty = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true); array_unshift($validators, $notEmpty); $this->setValidators($validators); } // Find the correct translator. Zend_Validate_Abstract::getDefaultTranslator() // will get either the static translator attached to Zend_Validate_Abstract // or the 'Zend_Translate' from Zend_Registry. if (Zend_Validate_Abstract::hasDefaultTranslator() && !Zend_Form::hasDefaultTranslator()) { $translator = Zend_Validate_Abstract::getDefaultTranslator(); if ($this->hasTranslator()) { // only pick up this element's translator if it was attached directly. $translator = $this->getTranslator(); } } else { $translator = $this->getTranslator(); } $this->_messages = array(); $this->_errors = array(); $result = true; $isArray = $this->isArray(); foreach ($this->getValidators() as $key => $validator) { if (method_exists($validator, 'setTranslator')) { if (method_exists($validator, 'hasTranslator')) { if (!$validator->hasTranslator()) { $validator->setTranslator($translator); } } else { $validator->setTranslator($translator); } } if (method_exists($validator, 'setDisableTranslator')) { $validator->setDisableTranslator($this->translatorIsDisabled()); } if ($isArray && is_array($value)) { $messages = array(); $errors = array(); if (empty($value)) { if ($this->isRequired() || !$this->isRequired() && !$this->getAllowEmpty()) { $value = ''; } } foreach ((array) $value as $val) { if (!$validator->isValid($val, $context)) { $result = false; if ($this->_hasErrorMessages()) { $messages = $this->_getErrorMessages(); $errors = $messages; } else { $messages = array_merge($messages, $validator->getMessages()); $errors = array_merge($errors, $validator->getErrors()); } } } if ($result) { continue; } } elseif ($validator->isValid($value, $context)) { continue; } else { $result = false; if ($this->_hasErrorMessages()) { $messages = $this->_getErrorMessages(); $errors = $messages; } else { $messages = $validator->getMessages(); $errors = array_keys($messages); } } $result = false; $this->_messages = array_merge($this->_messages, $messages); $this->_errors = array_merge($this->_errors, $errors); if ($validator->zfBreakChainOnFailure) { break; } } // If element manually flagged as invalid, return false if ($this->_isErrorForced) { return false; } return $result; }
/** * Returns the default translation object * * @return Zend_Translate_Adapter|null */ public static function getDefaultTranslator($translator = null) { require_once 'Zend/Validate/Abstract.php'; return Zend_Validate_Abstract::getDefaultTranslator(); }
public function testLocalTranslatorPreferredOverGlobalTranslator() { $this->testCanSetGlobalDefaultTranslator(); set_error_handler(array($this, 'errorHandlerIgnore')); $translator = new Zend_Translate('array', array(), 'en'); restore_error_handler(); $this->validator->setTranslator($translator); $this->assertNotSame(Zend_Validate_Abstract::getDefaultTranslator(), $this->validator->getTranslator()); }
public function testSetGetDefaultTranslator() { set_error_handler(array($this, 'errorHandlerIgnore')); $translator = new Zend_Translate('array', array(), 'en'); restore_error_handler(); Zend_Validate_Abstract::setDefaultTranslator($translator); $this->assertSame($translator->getAdapter(), Zend_Validate_Abstract::getDefaultTranslator()); }
/** * * @param mixed $config * @param array $replacement * @param array $attachment * @param bool $log * @param integer $vsprintfTimes * @param callback $filter * @return bool */ public function send($config = null, $replacement = array(), $attachment = array(), $log = false, $vsprintfTimes = 1, $filter = null) { if (is_scalar($config)) { $config = $this->getConfig($config); } if (isset($config['mail'])) { $config = $config['mail']; } //TODO if (count($config) > 0 && count($attachment) > 0) { foreach ($attachment as $att) { if ($att->filename) { $extension = strtolower(pathinfo(strval($att->filename), PATHINFO_EXTENSION)); if (isset(EhrlichAndreas_Util_Mime::$MIME_TYPE[$extension])) { $att->type = EhrlichAndreas_Util_Mime::$MIME_TYPE[$extension]; } } } } if (!is_null($filter)) { if ($config[0]['zfversion'] == 1) { $tmp = Zend_Json::encode($replacement); } elseif ($config[0]['zfversion'] == 2) { $callback = array('Zend\\Json\\Json', 'encode'); $param_arr = array($tmp); $tmp = call_user_func_array($callback, $param_arr); } $tmp = base64_encode($tmp); $tmp = ' replacement=\'' . $tmp . '\''; } $send = true; foreach ($config as $conf) { $fromEmail = EhrlichAndreas_Util_Vsprintf::vsprintf($conf['from']['email'], $replacement, $vsprintfTimes); $fromName = null; $replytoEmail = null; $replytoName = null; $toEmail = array(); $toName = array(); $ccEmail = array(); $ccName = array(); $bccEmail = array(); $bccName = array(); $header = array(); $subject = null; $bodyHtml = null; $bodyText = null; if (isset($conf['from']['name'])) { $fromName = EhrlichAndreas_Util_Vsprintf::vsprintf($conf['from']['name'], $replacement, $vsprintfTimes); } if (isset($conf['replyto']['email'])) { $replytoEmail = EhrlichAndreas_Util_Vsprintf::vsprintf($conf['replyto']['email'], $replacement, $vsprintfTimes); } if (isset($conf['replyto']['name'])) { $replytoName = EhrlichAndreas_Util_Vsprintf::vsprintf($conf['replyto']['name'], $replacement, $vsprintfTimes); } if (count($conf['to']) > 0) { foreach ($conf['to'] as $key => $value) { $email = EhrlichAndreas_Util_Vsprintf::vsprintf($value['email'], $replacement, $vsprintfTimes); $name = ''; if (isset($value['name'])) { $name = EhrlichAndreas_Util_Vsprintf::vsprintf($value['name'], $replacement, $vsprintfTimes); } //TODO /* if (isset($value['name']) && $value['name'] === $name) { $name = null; } */ $toEmail[$key] = $email; $toName[$key] = $name; } } if (count($conf['cc']) > 0) { foreach ($conf['cc'] as $key => $value) { $email = EhrlichAndreas_Util_Vsprintf::vsprintf($value['email'], $replacement, $vsprintfTimes); $name = ''; if (isset($value['name'])) { $name = EhrlichAndreas_Util_Vsprintf::vsprintf($value['name'], $replacement, $vsprintfTimes); } $ccEmail[$key] = $email; $ccName[$key] = $name; } } if (count($conf['bcc']) > 0) { foreach ($conf['bcc'] as $key => $value) { $email = EhrlichAndreas_Util_Vsprintf::vsprintf($value['email'], $replacement, $vsprintfTimes); $name = ''; if (isset($value['name'])) { $name = EhrlichAndreas_Util_Vsprintf::vsprintf($value['name'], $replacement, $vsprintfTimes); } $bccEmail[$key] = $email; $bccName[$key] = $name; } } if (count($conf['header']) > 0) { foreach ($conf['header'] as $key => $value) { if (!isset($value['append'])) { $value['append'] = false; } $header[] = $value; } } /* if (isset($conf['subject']) && strlen($conf['subject']) > 0) { $subject = $conf['subject']; if (!is_null($filter)) { $subject = str_replace(' replacement=\'%replacement%\'', $tmp, $subject); } else { $subject = preg_replace('#\\[base64.*\\[\\/base64\\]#ui', '', $subject); } if (!is_null($filter)) { $subject = str_replace(' replacement=\'%replacement%\'', $tmp, $subject); $subject = EhrlichAndreas_Util_Vsprintf::vsprintf($subject, $replacement, $vsprintfTimes); } else { $subject = EhrlichAndreas_Util_Vsprintf::vsprintf($subject, $replacement, 1); $subject = preg_replace('#\\[base64.*\\[\\/base64\\]#ui', '', $subject); $subject = EhrlichAndreas_Util_Vsprintf::vsprintf($subject, $replacement, $vsprintfTimes - 1); } } * */ if (isset($conf['subject']) && strlen($conf['subject']) > 0) { $subject = $conf['subject']; if (!is_null($filter)) { $subject = str_replace(' replacement=\'%replacement%\'', $tmp, $subject); $subject = EhrlichAndreas_Util_Vsprintf::vsprintf($subject, $replacement, $vsprintfTimes); } else { for ($vsprintfTime = 0; $vsprintfTime < $vsprintfTimes; $vsprintfTime++) { $subject = preg_replace('#\\[base64.*\\[\\/base64\\]#si', '', $subject); $subject = str_replace(' replacement=\'%replacement%\'', '', $subject); $subject = str_replace(' replacement=\'\'', '', $subject); $subject = EhrlichAndreas_Util_Vsprintf::vsprintf($subject, $replacement); } } } if (!is_null($subject) && !is_null($filter)) { $subject = call_user_func_array($filter, array($subject)); } /* if (isset($conf['body']['html']) && strlen($conf['body']['html']) > 0) { $bodyHtml = $conf['body']['html']; if (!is_null($filter)) { $bodyHtml = str_replace(' replacement=\'%replacement%\'', $tmp, $bodyHtml); $bodyHtml = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyHtml, $replacement, $vsprintfTimes); } else { $bodyHtml = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyHtml, $replacement, 1); $bodyHtml = preg_replace('#\\[base64.*\\[\\/base64\\]#ui', '', $bodyHtml); $bodyHtml = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyHtml, $replacement, $vsprintfTimes - 1); } } * */ if (isset($conf['body']['html']) && strlen($conf['body']['html']) > 0) { $bodyHtml = $conf['body']['html']; if (!is_null($filter)) { $bodyHtml = str_replace(' replacement=\'%replacement%\'', $tmp, $bodyHtml); $bodyHtml = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyHtml, $replacement, $vsprintfTimes); } else { for ($vsprintfTime = 0; $vsprintfTime < $vsprintfTimes; $vsprintfTime++) { $bodyHtml = preg_replace('#\\[base64.*\\[\\/base64\\]#si', '', $bodyHtml); $bodyHtml = str_replace(' replacement=\'%replacement%\'', '', $bodyHtml); $bodyHtml = str_replace(' replacement=\'\'', '', $bodyHtml); $bodyHtml = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyHtml, $replacement); } } } if (!is_null($bodyHtml) && !is_null($filter)) { $bodyHtml = call_user_func_array($filter, array($bodyHtml)); } if (!is_null($bodyHtml)) { $bodyHtml = preg_replace('#\\r?\\n|\\r\\n?#', $conf['eol'], $bodyHtml); } /* if (isset($conf['body']['text']) && strlen($conf['body']['text']) > 0) { $bodyText = $conf['body']['text']; if (!is_null($filter)) { $bodyText = str_replace(' replacement=\'%replacement%\'', $tmp, $bodyText); $bodyText = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyText, $replacement, $vsprintfTimes); } else { $bodyText = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyText, $replacement, 1); $bodyText = preg_replace('#\\[base64.*\\[\\/base64\\]#ui', '', $bodyText); $bodyText = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyText, $replacement, $vsprintfTimes - 1); } $bodyText = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyText, $replacement, $vsprintfTimes); } * */ if (isset($conf['body']['text']) && strlen($conf['body']['text']) > 0) { $bodyText = $conf['body']['text']; if (!is_null($filter)) { $bodyText = str_replace(' replacement=\'%replacement%\'', $tmp, $bodyText); $bodyText = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyText, $replacement, $vsprintfTimes); } else { for ($vsprintfTime = 0; $vsprintfTime < $vsprintfTimes; $vsprintfTime++) { $bodyText = preg_replace('#\\[base64.*\\[/base64\\]#si', '', $bodyText); $bodyText = str_replace(' replacement=\'%replacement%\'', '', $bodyText); $bodyText = str_replace(' replacement=\'\'', '', $bodyText); $bodyText = EhrlichAndreas_Util_Vsprintf::vsprintf($bodyText, $replacement); } } } if (!is_null($bodyText) && !is_null($filter)) { $bodyText = call_user_func_array($filter, array($bodyText)); } if (!is_null($bodyText)) { $bodyText = preg_replace('#\\r?\\n|\\r\\n?#', $conf['eol'], $bodyText); } if ($conf['zfversion'] == 1) { //require_once 'Zend/Mail.php'; $message = new Zend_Mail($conf['charset']); $message->setHeaderEncoding($conf['encoding']); } elseif ($conf['zfversion'] == 2) { $classMailMessage = 'Zend\\Mail\\Message'; $message = new $classMailMessage(); $message->setEncoding($conf['charset']); $classMimeMessage = 'Zend\\Mime\\Message'; $mimeBody = new $classMimeMessage(); $message->setBody($mimeBody); $headers = $message->getHeaders(); $headers->setEncoding('ASCII'); } $message->setFrom($fromEmail, $fromName); if (!is_null($replytoEmail)) { $message->setReplyTo($replytoEmail, $replytoName); } foreach ($toEmail as $key => $email) { $name = $toName[$key]; $message->addTo($email, $name); } foreach ($ccEmail as $key => $email) { $name = $ccName[$key]; $message->addCc($email, $name); } foreach ($bccEmail as $key => $email) { $name = $bccName[$key]; $message->addBcc($email, $name); } if ($conf['zfversion'] == 1) { foreach ($header as $key => $value) { $message->addHeader($value['name'], $value['value'], $value['append']); } } elseif ($conf['zfversion'] == 2) { $headers = $message->getHeaders(); //$headers->setEncoding($conf['charset']); foreach ($header as $key => $value) { $classGenericMultiHeader = 'Zend\\Mail\\Header\\GenericMultiHeader'; $headerTmp = new $classGenericMultiHeader($value['name'], $value['value']); $headerTmp->setEncoding($conf['charset']); $headers->addHeader($headerTmp); } } if (!is_null($subject)) { $message->setSubject($subject); } if ($conf['zfversion'] == 1) { if (!is_null($bodyHtml)) { $message->setBodyHtml($bodyHtml, $conf['charset'], $conf['encoding']); } if (!is_null($bodyText)) { $message->setBodyText($bodyText, $conf['charset'], $conf['encoding']); } } elseif ($conf['zfversion'] == 2) { $htmlPart = null; $textPart = null; //$mimeMessage = null; if (!is_null($bodyText)) { $classMimePart = 'Zend\\Mime\\Part'; $textPart = new $classMimePart($bodyText); $textPart->type = 'text/plain'; $textPart->charset = $conf['charset']; $textPart->encoding = $conf['encoding']; $message->getBody()->addPart($textPart); $classGenericMultiHeader = 'Zend\\Mail\\Header\\GenericMultiHeader'; $headerTmp = new $classGenericMultiHeader('content-type', 'text/plain; charset=' . $conf['charset']); $headerTmp->setEncoding('ASCII'); $headers->removeHeader('content-type'); $headers->addHeader($headerTmp); } if (!is_null($bodyHtml)) { $classMimePart = 'Zend\\Mime\\Part'; $htmlPart = new $classMimePart($bodyHtml); $htmlPart->type = 'text/html'; $htmlPart->charset = $conf['charset']; $htmlPart->encoding = $conf['encoding']; $message->getBody()->addPart($htmlPart); $classGenericMultiHeader = 'Zend\\Mail\\Header\\GenericMultiHeader'; $headerTmp = new $classGenericMultiHeader('content-type', 'text/html; charset=' . $conf['charset']); $headerTmp->setEncoding('ASCII'); $headers->removeHeader('content-type'); $headers->addHeader($headerTmp); } /* if (!is_null($htmlPart) || !is_null($textPart)) { $mimeMessage = new Zend\Mime\Message(); } if (!is_null($htmlPart)) { $mimeMessage->addPart($htmlPart); } if (!is_null($textPart)) { $mimeMessage->addPart($textPart); } if (!is_null($mimeMessage)) { $alternativePart = new Zend\Mime\Part($mimeMessage->generateMessage()); $alternativePart->type = 'multipart/alternative'; $alternativePart->boundary = $mimeMessage->getMime()->boundary(); $alternativePart->charset = $conf['charset']; //$alternativePart->encoding = $conf['encoding']; $message->getBody()->addPart($alternativePart); } * */ } if ($conf['zfversion'] == 1) { if (isset($conf['attachment']) && strlen($conf['attachment']) > 0 && $conf['attachment']) { foreach ($attachment as $att) { $message->addAttachment($att); } } } elseif ($conf['zfversion'] == 2) { //TODO add attachements } /* * * TODO add attachements * if (count($config)>0&&count($attachment)>0) { foreach ($attachment as $att) { if ($att->filename) { $extension = strtolower(pathinfo(strval($att->filename),PATHINFO_EXTENSION)); if (isset(EhrlichAndreas_Util_Mime::$MIME_TYPE[$extension])) { $att->type = EhrlichAndreas_Util_Mime::$MIME_TYPE[$extension]; } } } } if (isset($conf['attachment'])&&strlen($conf['attachment'])>0&&$conf['attachment']) { foreach ($attachment as $att) { $mail->addAttachment($att); } } * */ if ($conf['zfversion'] == 1) { } elseif ($conf['zfversion'] == 2) { $headers = $message->getHeaders(); //$headers->setEncoding('ASCII'); $classGenericMultiHeader = 'Zend\\Mail\\Header\\GenericMultiHeader'; $headerTmp = new $classGenericMultiHeader('Content-Transfer-Encoding', $conf['encoding']); $headerTmp->setEncoding('ASCII'); $headers->addHeader($headerTmp); if (!is_null($bodyText) && !is_null($bodyHtml)) { $headers->removeHeader('Content-Transfer-Encoding'); $headers->removeHeader('content-type'); $boundary = $message->getBody()->getMime()->boundary(); $classGenericMultiHeader = 'Zend\\Mail\\Header\\GenericMultiHeader'; $headerTmp = new $classGenericMultiHeader('content-type', 'multipart/alternative; boundary="' . $boundary . '"'); $headerTmp->setEncoding('ASCII'); $headers->addHeader($headerTmp); } if (false && $message->getBody()->isMultiPart()) { $classGenericMultiHeader = 'Zend\\Mail\\Header\\GenericMultiHeader'; $headerTmp = new $classGenericMultiHeader('content-type', 'multipart/alternative; charset=' . $conf['charset']); $headerTmp->setEncoding('ASCII'); $headers->addHeader($headerTmp); } } /* * echo '<pre>'; var_dump ( $fromEmail, $fromName, $replytoEmail, $replytoName, $toEmail, $toName, $ccEmail, $ccName, $bccEmail, $bccName, $header, $subject, $bodyHtml, $bodyText ); //print_r($replacement); //die(); /* */ if ($conf['zfversion'] == 1) { $defaultTranslator = Zend_Validate_Abstract::getDefaultTranslator(); if (is_object($defaultTranslator)) { Zend_Validate_Abstract::setDefaultTranslator(null); $defaultTranslatorOptions = $defaultTranslator->getOptions(); $defaultTranslator->setOptions(array('logUntranslated' => false)); } } else { } $messageTransport = $conf['transport']; try { $messageTransport->send($message); $sendTmp = true; } catch (Exception $e) { $sendTmp = false; } $send = $send && $sendTmp; if ($conf['zfversion'] == 1) { if (is_object($defaultTranslator)) { $defaultTranslator->setOptions($defaultTranslatorOptions); Zend_Validate_Abstract::setDefaultTranslator($defaultTranslator); } } else { } } return $send; }
/** * Returns the default translation object * * @return Zend_Translate_Adapter|null */ public static function getDefaultTranslator($translator = null) { return Zend_Validate_Abstract::getDefaultTranslator(); }