3

I've recently experienced a problem where by users have entered control code characters into Long text fields. Specifically the ASCII codes \001, \007, \008, \021 (codes for Start Of Heading, Bell, Backspace, Device control 1) which are causing havoc in my XML exports since they are not allowed by the XML standard and prevent the XML file from being parsed.

Therefore I would like to cleanse the output as it goes through Views Data Export to prevent bad characters from outputting. As commented below, this sounds like a bug in VDE, however I'm wondering whether I can tackle this issue easily with my own views hooks rather than wait for a new version/patch?

2
  • Three questions: 1) Where your users input them? Node edit form, custom form, form created by ____ module? 2) Is there any particular reason to sanitize on input instead of output, as usual in Drupal? 3) What are you using to generate XML and why isn't it clearing forbidden characters on it's own? Sounds like a bug in XML generator module.
    – Mołot
    Aug 19, 2013 at 6:38
  • Hi, good questions: 1) Node form 2) Good point, I should concentrate on the output, which my co-worker also just reminded me of 3) Views Data Export. I'll update my question to relate to output instead of input.
    – DanH
    Aug 19, 2013 at 6:50

1 Answer 1

3

Views Data Export enables you to write your own .tpl files for fields. So, in a template for a problematic fields, you might use

preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $input);

to strip all potentially problematic characters. Of course cut list of chars for your specific need and if they are in any way meaningful to your users, consider replacing them with some mark instead of eradicating them entirely. It's a bit clumsy solution, but as far as I remember it works, ant it's fast to get up and running.

You can also use preprocess hook instead of tpl:

/**
 * Implements hook_preprocess_views_view_field().
 */
function mymodule_preprocess_views_view_field(&$vars) {
  if ($vars['view']->name == 'myview' && $vars['view']->plugin_name = 'views_data_export_xml') {
    $vars['output'] = preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $vars['output']);
  }
}
1
  • Thanks I'll give this a try. Since there's quite a large array of fields affected though I think I'll probably go for a views hook and check the field names from an array.
    – DanH
    Aug 19, 2013 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.