You are here

function filefield_source_clipboard_value in FileField Sources 7

A #filefield_value_callback function.

1 string reference to 'filefield_source_clipboard_value'
filefield_source_clipboard_info in sources/clipboard.inc
Implements hook_filefield_source_info().

File

sources/clipboard.inc, line 106
A FileField extension to allow transfer of files through the clipboard.

Code

function filefield_source_clipboard_value(&$element, &$item) {
  if (isset($item['filefield_clipboard']['contents']) && strlen($item['filefield_clipboard']['contents']) > 0) {

    // Check that the destination is writable.
    $temporary_directory = 'temporary://';
    if (!file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS)) {
      watchdog('file', 'The directory %directory is not writable, because it does not have the correct permissions set.', array(
        '%directory' => drupal_realpath($temporary_directory),
      ));
      drupal_set_message(t('The file could not be transferred because the temporary directory is not writable.'), 'error');
      return;
    }

    // Check that the destination is writable.
    $directory = $element['#upload_location'];
    $mode = variable_get('file_chmod_directory', 0775);

    // This first chmod check is for other systems such as S3, which don't work
    // with file_prepare_directory().
    if (!drupal_chmod($directory, $mode) && !file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
      watchdog('file', 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array(
        '%file' => $url,
        '%destination' => drupal_realpath($directory),
      ));
      drupal_set_message(t('The specified file %file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array(
        '%file' => $url,
      )), 'error');
      return;
    }

    // Split the file information in mimetype and base64 encoded binary.
    $base64_data = $item['filefield_clipboard']['contents'];
    $comma_position = strpos($base64_data, ',');
    $semicolon_position = strpos($base64_data, ';');
    $file_contents = base64_decode(substr($base64_data, $comma_position + 1));
    $mimetype = substr($base64_data, 5, $semicolon_position - 5);
    include_once './includes/file.mimetypes.inc';
    $mime_mapping = file_mimetype_mapping();
    $mime_key = array_search($mimetype, $mime_mapping['mimetypes']);
    $extension = array_search($mime_key, $mime_mapping['extensions']);
    $filename = trim($item['filefield_clipboard']['filename']);
    $filename = preg_replace('/\\.[a-z0-9]{3,4}$/', '', $filename);
    $filename = (empty($filename) ? 'paste_' . REQUEST_TIME : $filename) . '.' . $extension;
    $filepath = file_create_filename($filename, $temporary_directory);
    $copy_success = FALSE;
    if ($fp = @fopen($filepath, 'w')) {
      fwrite($fp, $file_contents);
      fclose($fp);
      $copy_success = TRUE;
    }
    if ($copy_success && ($file = filefield_sources_save_file($filepath, $element['#upload_validators'], $element['#upload_location']))) {
      $item = array_merge($item, (array) $file);
    }

    // Remove the temporary file generated from paste.
    if ($filepath !== $item['uri']) {
      @unlink($filepath);
    }
  }
}