You are here

function salesforce_api_update_wsdl_form_validate in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 7.2 salesforce_api/salesforce_api.admin.inc \salesforce_api_update_wsdl_form_validate()

Validate the WSDL file upload - check upload error code and file extension and do the actual upload processing so that we can invalidate the form on any errors. This function SHOULD use file_save_upload(), but unfortunately, that function calls file_create_path() which calls file_check_location(), which fails on any destination dir that isn't in the default file upload dir. And since the enterprise.wsdl.xml file needs to go into a sub-dir in the module directory, that won't do. Instead of uploading to the default dir, then moving, just upload it straight to the destination.

File

salesforce_api/salesforce_api.admin.inc, line 993
Contains the admin page callbacks for the Salesforce module, including forms for general settings and fieldmap administration.

Code

function salesforce_api_update_wsdl_form_validate($form, &$form_state) {
  $source = 'salesforce_api_wsdl_file';

  // Find the dir the file should go into
  if (!($dir = $form_state['values']['wsdl'])) {
    $dir = drupal_get_path('module', 'salesforce_api') . '/wsdl';
  }
  $dir = rtrim($dir, '/');

  // Upload file path
  $file = $dir . '/enterprise.wsdl.xml';

  // Make sure the directory is writeable
  if (!file_check_directory($dir)) {
    form_set_error($source, t('Server directory %directory is not writeable. Please check directory permissions or contact a site admin to correct this.', array(
      '%directory' => $dir,
    )));
    return;
  }
  if (!isset($_FILES['files']) && $_FILES['files']['name'][$source] || !is_uploaded_file($_FILES['files']['tmp_name'][$source])) {
    form_set_error($source, t('Error uploading file. Please try again.'));
    return;
  }

  // Check for file upload errors and return FALSE if a
  // lower level system error occurred.
  switch ($_FILES['files']['error'][$source]) {

    // @see http://php.net/manual/en/features.file-upload.errors.php
    case UPLOAD_ERR_OK:
      break;
    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
      drupal_set_message(t('The file %file could not be saved, because it exceeds %maxsize, the maximum allowed size for uploads.', array(
        '%file' => $source,
        '%maxsize' => format_size(file_upload_max_size()),
      )), 'error');
      return 0;
    case UPLOAD_ERR_PARTIAL:
    case UPLOAD_ERR_NO_FILE:
      drupal_set_message(t('The file %file could not be saved, because the upload did not complete.', array(
        '%file' => $source,
      )), 'error');
      return 0;

    // Unknown error
    default:
      drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array(
        '%file' => $source,
      )), 'error');
      return 0;
  }

  // Check the file extension
  $path_parts = pathinfo($_FILES['files']['name'][$source]);
  if (!in_array(strtolower($path_parts['extension']), array(
    'xml',
    'wsdl',
  ))) {
    form_set_error($source, t('File upload error: invalid file extension. Please upload a file with an XML or WSDL  extension.'));
  }

  // Try to move the uploaded file into the right place
  if (!move_uploaded_file($_FILES['files']['tmp_name'][$source], $file)) {
    form_set_error($source, t('File upload error. Could not move uploaded file.'));
    watchdog('file', 'Upload error. Could not move uploaded file to destination %destination.', array(
      '%destination' => $dir,
    ));
  }
}