I planned to sign my mails with php-mail-signature (https://github.com/louisameline/php-mail-signature), but I can't find a method in the Nette-Mail Message class (https://api.nette.org/2.4/Nette.Mail.Message.html) to set the DKIM-value.
Since I thought that would be more or less basic functionality, I'm wondering if it really isn't possible to use DKIM with Nette Mail.
Is it possible, and if so, how do I do it?
Problem is, php-mail-signature is too much low-level. It returns signed headers as string. You need to parse $signature->get_signed_headers() output and call $message->setHeader() for each of them. If you're looking for some magic method $message->setDkimSignature(), you won't find it. But you can always inherit from Message class and write your own.
This is only non-tested example:
<?php
use mail_signature;
use Nette\Mail\Message;
final class DkimSignedMessage extends Message
{
/**
* #var mail_signature
*/
private $signature;
public function __construct(mail_signature $signature)
{
$this->signature = $signature;
}
public function generateMessage(): string
{
$message = $this->build();
$signedHeaders = $this->signature->get_signed_headers(
$message->getTo(),
$message->getSubject(),
$message->getBody(),
implode("\r\n", $message->getHeaders())
);
foreach (explode("\r\n", trim($signedHeaders)) as $header) {
[$name, $value] = explode(': ', $header);
$message->setHeader($name, trim($value))
}
return $message->getEncodedMessage();
}
}
Related
I am newbie with filemaker. I am trying to set search function but something wrong and it returns No records match the request even if it is present there. Here is code
public function get_row($table, $search='')
{
$layout_object = $this->fm->getLayout($table);
if (FileMaker::isError($layout_object)) {
return array();
}
$request = $this->fm->newFindCommand($table);
if ($search)
{
$request->addFindCriterion($search['key'], 'hh#kkk.nn'); // hardcoded.
}
$result = $request->execute();
if (FileMaker::isError($result)) {
echo $result->getErrorString();
}
//.....Result: No records match the request
}
what I am doing wrong?
You need to escape the # symbol as it's a special character in Find mode to match any one character, so try this:
$request->addFindCriterion($search['key'], 'hh\#kkk.nn'); // hardcoded.
I want to handle application feedback regarding, in this case, form validation.
To do this I check for model validation in controller, using
// VALIDATE
if ($this->Event->validates($this->data))
{
// SAVE
$this->Event->create();
if ($this->Event->saveAll($this->data, array('validate' => false)))
{
$this->Session->setFlash('Evenimentul a fost salvat!', 'flash_admin_success');
$this->redirect(array('action' => 'index', 'admin' => true));
} else {
$this->Session->setFlash('Evenimentul nu a putut fi salvat. Va rugam sa incercati din nou!', 'flash_admin_error');
}
////////
$errors = 'O EROARE';
$this->set(compact('errors'));
}
else
{
// GET ERRORS to display it nicely :)
$errors = $this->Event->invalidFields();
$flash = '';
foreach($errors as $error)
{
$flash .= $error."<br />";
}
$this->Session->setFlash($flash, 'flash_admin_error');
}
I know that there is a way to get rid of form field errors using 'error' => false, but i want to set this for the entire application, thus for all fields in all forms.
It has to be there a way of setting that fot the object itself, and I would be gratefull if someone would tell me.
Thaks a lot!
Edit: This doesn't really disable error output, but will hide the error: go to webroot/css/cake.generic.css add display:none to selector div.error-message. That's the simplest way to achieve what you want that I can think of.
Though it may seem like a bit of an extreme approach to override a single property, you can achieve this by extend the core FormHelper. This will allow you to make Anh Pham's original suggestion the default for all FormHelper instances:
// app/views/helpers/app_form.php
App::import('Helper', 'Time');
class AppFormHelper extends FormHelper {
public $_inputDefaults = array('error' => false);
}
Now to use this as-is in CakePHP 1.3, you would have to use "AppForm" throughout your application to refer to this helper from now on (ie. $this->AppForm->input()). CakePHP 2.0 introduces helper aliasing to overcome this, but for now one has to resort to a bit of trickery to continue using "Form" instead. One blog post I found shows how to backport the functionality and another manages allow the helper to do it itself. I personally use the following without any problems:
// app/views/app.php
class AppView extends View {
function &_loadHelpers(&$loaded, $helpers, $parent = null) {
$return = parent::_loadHelpers($loaded, $helpers, $parent);
# rename App helpers (ie. AppHtml -> Html)
foreach ($return as $helperName => $helper) {
if (substr($helperName, 0, 3) === 'App') {
$newHelperName = substr($helperName, 3);
$return[$newHelperName] = $return[$helperName];
}
}
# done
return $return;
}
}
To use the new created classes above, just add the following to your AppController:
// app/app_controller.php
class AppController extends Controller {
public $helpers = array(/*...*/, 'AppForm');
public $view = array('App');
}
I have a many to many relation between Product and Properties. I'm using embedRelation() in my Product form to edit a Product and it's Properties. Properties includes images which causes my issue. Every time I save the form the updated_at column is updated for file properties even when no file is uploaded.
Therefore, I want to exclude empty properties when saving my form.
I'm using Symfony 1.4 and Doctrine 1.2.
I'm thinking something like this in my ProductForm.class.php, but I need some input on how to make this work.
Thanks
class ProductForm extends BaseProductForm
{
public function configure()
{
unset($this['created_at'], $this['updated_at'], $this['id'], $this['slug']);
$this->embedRelation('ProductProperties');
}
public function saveEmbeddedForms($con = null, $forms = null)
{
if (null === $forms)
{
$properties = $this->getValue('ProductProperties');
$forms = $this->embeddedForms;
foreach($properties as $p)
{
// If property value is empty, unset from $forms['ProductProperties']
}
}
}
}
I ended up avoiding Symfony's forms and saving models instead of saving forms. It can be easier when playing with embedded forms. http://arialdomartini.wordpress.com/2011/04/01/how-to-kill-symfony%E2%80%99s-forms-and-live-well/
Solved it by checking if posted value is a file, and if both filename and value_delete is null I unset from the array. It might not be best practice, but it works for now.
Solution based on http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms
class ProductPropertyValidatorSchema extends sfValidatorSchema
{
protected function configure($options = array(), $messages = array())
{
// N0thing to configure
}
protected function doClean($values)
{
$errorSchema = new sfValidatorErrorSchema($this);
foreach($values as $key => $value)
{
$errorSchemaLocal = new sfValidatorErrorSchema($this);
if(array_key_exists('value_delete', $values))
{
if(!$value && !$values['value_delete'])
{
unset($values[$key]);
}
}
// Some error for this embedded-form
if (count($errorSchemaLocal))
{
$errorSchema->addError($errorSchemaLocal, (string) $key);
}
}
// Throws the error for the main form
if (count($errorSchema))
{
throw new sfValidatorErrorSchema($this, $errorSchema);
}
return $values;
}
}
I get a really anoying error when I try to edit an entry from a table, in tutorial they always use getTable()->find(), but I need to verify that the person logged in is the owner of that entry here what I did:
In the action:
public function executeEdit(sfWebRequest $request)
{
$id = $request->getParameter('id');
$userid = $this->getUser()->getGuardUser()->getId();
$ad = Doctrine_Core::getTable('BambinbazarArticles')->getMyAd($id, $userid);
$this->forward404Unless($ad, sprintf('Object bambinbazar_articles does not exist (%s).', $request->getParameter('id')));
$this->form = new BambinbazarArticlesForm($ad);
}
In the model:
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid);
return $q->execute();
}
I tried it with and without the ->execute(), did doctrine clean, cleared cache, rebuilded model,
Always get the same error 'The "%s" form only accepts a "%s" object.
If I use the Doctrine_Core::getTable('BambinbazarArticles')->find() it work, but of course, i need more than that..
I am becoming crazy over this.
execute() can return multiple rows; effectively you're getting a recordset back, rather than the individual object that your form is expecting. Try fetching a single object, using, e.g.:
return $q->execute()->getFirst();
or
return $q->fetchOne();
Its probably because your query is returning a Doctrine_Collection, not the actual Doctrine_Record youre expecting. Instead of execute use fetchOne.
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid)
->limit(1);
return $q->fetchOne();
}
I'm new to Zend Framework, I have a question is that if I have two Zend_Form_Element_Text in a form, and I want make either of them to be filled by the user.
For example, phone number and mobile number. People only need enter one of them to continue.
How am I going to do this? Thanks
Hi i think you can do the following.
If you're initalizing your form in an Controller Action try this:
/**
* IndexAction
*
* #return void
*/
public function indexAction() {
// initalize your form
$form = new MyForm();
// get post data
$post = $this->_request->getPost();
// check if phone number is empty
if (! empty($post['phone_number'])) {
// remove validator from mobile phone element
$mobile = $form->getElement('mobile');
$mobile->removeValidator('notEmpty');
$mobile->setRequired(false);
}
if ($form->isValid($this->getRequest()->getPost())) {
$input = $form->getValues();
// do something with the input
print_r($input, true);
}
}