I have a very simple form function (see below). It just creates a text box and inserts that into a database.
When I use either basic or Mcrypt and #encrypt => TRUE on that field I get an error:
Error message
PDOException: in form_example_tutorial_2_submit() (line 97 of /home/greggles/workspace/d7/sites/all/modules/contrib/examples/form_example/form_example_tutorial.inc).
I think the problem is that Drupal's database API isn't able to handle some of the resulting characters from the encryption functions. If we add a uuencode and uudecode to the process then it works fine. I'm not sure if we should add this here or in the encryption_method plugins (e.g. mcrypt_rij_256.inc) but this seems like a good start: the problem only occurs when trying to save the data to the database so, at this point, it feels best to leave the encryption_method plugins "pure" while the fapi convenience tool gets the uuencoding.
Here's a simple module to demonstrate the problem:
function form_example_tutorial_2($form, &$form_state) {
$fields = db_select('e2')
->fields('e2')
->orderBy('id', 'DESC')
->range(0,1)
->execute()
->fetchAll();
if (!empty($fields[0]->text_value)) {
$text_value = $fields[0]->text_value;
}
else {
$text_value = convert_uuencode(encrypt('monkeypants'));
}
$form['description'] = array(
'#type' => 'item',
'#title' => t('A simple form with a submit button'),
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $text_value,
'#encrypt' => TRUE,
);
// Adds a simple submit button that refreshes the form and clears its
// contents. This is the default behavior for forms.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function form_example_tutorial_2_submit($form, &$form_state) {
if (!empty($form_state['values']['name'])) {
db_insert('e2')
->fields(array('text_value' => $form_state['values']['name']))
->execute();
}
}
To create the table:
create table e2 (id int auto_increment, text_value varchar(2550), primary key (id));
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | 1775734_encrypt_uuencode_6.patch | 4.32 KB | greggles |
| #3 | 1775734_encrypt_uuencode_2.patch | 2.38 KB | greggles |
| #1 | 1775734_encrypt_uuencode.patch | 1.43 KB | greggles |
Comments
Comment #1
gregglesComment #2
gregglesComment #3
gregglesOk, so I did a bit more review and see now:
1. base64 is way cooler than uuencoding
2. the MCRYPT_RIJNDAEL_256 has an option to base64encode, but that is not being used and missing from basic
So, the attached patch:
1. Tries to use the option when possible
2. Adds it as an option to the basic method
This seems to work pretty well for the MCRYPT_RIJNDAEL_256 encryption, but there's a bug somewhere in my logic for the basic encryption. If I set the site to use basic and then do
drupal_set_message(decrypt(encrypt('asdf', array('base64' => TRUE)), array('base64' => TRUE)));I get back something beyond just the asdf.Comment #6
greggles#3: 1775734_encrypt_uuencode_2.patch queued for re-testing.
Comment #7
gregglesComment #9
gregglesOK, maybe the basic method just doesn't need to base64 encode. I can no longer reproduce the issue I was initially having.
Comment #10
gregglesBetter title for what's happening.
Given that this now passes and nobody's commented that this seemed like a bad idea I committed it. http://drupalcode.org/project/encrypt.git/commit/4bab066
I also created a new beta release from the 7.x-2.x branch with notes on how to deal with upgrading if people used this sub-module.