function patterns_import_validate in Patterns 6
Same name and namespace in other branches
- 5 patterns.module \patterns_import_validate()
- 6.2 patterns.module \patterns_import_validate()
- 7.2 includes/forms/import.inc \patterns_import_validate()
- 7 includes/forms/import.inc \patterns_import_validate()
3 string references to 'patterns_import_validate'
- patterns_import_file in ./
patterns.module - Display the import pattern file form
- patterns_import_source in ./
patterns.module - Display the import pattern form
- patterns_import_url in ./
patterns.module - Display the import pattern url form
File
- ./
patterns.module, line 567 - Enables extremely simple adding/removing features to your site with minimal to no configuration
Code
function patterns_import_validate($form, &$form_state) {
$path = file_create_path(variable_get('patterns_save_xml', 'patterns'));
if (!file_check_directory($path, true)) {
$message = t("Destination folder doesn't exist: !path<br />", array(
'!path' => $path,
));
$link = l(t('status report'), 'admin/reports/status');
$message .= t('You might want to check the !link to ensure your files directory exists and is writable.', array(
'!link' => $link,
));
form_set_error('xmlfile', $message);
return;
}
$validators = array(
'file_validate_extensions' => array(
'xml',
),
);
if (isset($form_state['values']['xmlfile']) && ($file = file_save_upload('xmlfile', $validators))) {
$form_state['values']['xmlsource'] = file_get_contents($file->filepath);
$form_state['pattern_file'] = $file;
$form_state['pattern_file']->destination = $path;
}
else {
if (isset($form_state['values']['xmlfile'])) {
form_set_error('files[xmlfile]', t('Error uploading XML file.'));
return;
}
else {
if ($form_state['values']['xmlurl']) {
if (!ini_get('allow_url_fopen')) {
form_set_error('xmlsource', t('allow_url_fopen must be enabled in your php configuration in order to use this feature.'));
return;
}
if (strtolower(substr($form_state['values']['xmlurl'], -4)) != '.xml') {
form_set_error('xmlsource', t('Invalid file extension. Only "XML" file extension is allowed.'));
return;
}
if (!($form_state['values']['xmlsource'] = file_get_contents($form_state['values']['xmlurl']))) {
form_set_error('xmlurl', t('Failed to retreive the pattern specified: ' . $form_state['values']['xmlurl']) . '. Check your URL and try again.');
return;
}
$pattern = array(
'/\\.[^\\.]*$/',
'/[^a-zA-Z0-9_]/',
);
$replacement = array(
'',
'_',
);
$form_state['values']['xmlname'] = preg_replace($pattern, $replacement, basename($form_state['values']['xmlurl']));
}
}
}
if (strpos($form_state['values']['xmlsource'], '<?xml') !== 0) {
$form_state['values']['xmlsource'] = '<?xml version="1.0" encoding="ISO-8859-1"?>' . $form_state['values']['xmlsource'];
}
if ($form_state['values']['xmlname'] && preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['xmlname'])) {
form_set_error('xmlname', t('You can only include letters, numbers, and underscores in the pattern identifier.'));
}
else {
if ($form_state['values']['xmlname'] && preg_match('/^_/', $form_state['values']['xmlname'])) {
form_set_error('xmlname', t('You cannot start the pattern identifier with an underscore.'));
}
}
// @TODO validate XML and don't allow import if validation fails
$parse = drupal_xml_parser_create($form_state['values']['xmlsource']);
$success = xml_parse_into_struct($parse, $form_state['values']['xmlsource'], $vals, $index);
// Check that the xml was properly parsed and also that the
// root <pattern> tag and also an <info> tag were used.
if (!$success || !$vals || $vals[0]['tag'] != 'PATTERN' || $vals[1]['tag'] != 'INFO') {
form_set_error('xmlsource', t('Error parsing the XML, please check your syntax and try again.'));
}
}