/**
  * {@inheritdoc}
  */
 public static function value(array &$element, &$input, FormStateInterface $form_state)
 {
     if (!empty($input['filefield_flysystem']['filename'])) {
         $instance = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
         $filepath = $input['filefield_flysystem']['filename'];
         $directory = $element['#upload_location'];
         $mode = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
         if (!drupal_chmod($directory, $mode) && !file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
             \Drupal::logger('filefield_sources')->log(E_NOTICE, 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' => $filepath, '%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' => $filepath)), 'error');
             return;
         }
         // Clean up the file name extensions and transliterate.
         $original_filepath = $filepath;
         $new_filepath = filefield_sources_clean_filename($filepath, $instance->getSetting('file_extensions'));
         rename($filepath, $new_filepath);
         $filepath = $new_filepath;
         // Run all the normal validations, minus file size restrictions.
         $validators = $element['#upload_validators'];
         if (isset($validators['file_validate_size'])) {
             unset($validators['file_validate_size']);
         }
         // Serve files from source folder directly.
         if ($element['#filefield_sources_settings']['flysystem']['attach_mode'] == FILEFIELD_SOURCE_FLYSYSTEM_ATTACH_MODE_SERVEFROMFOLDER) {
             $directory = $filepath;
             if ($file = filefield_sources_flysystem_save_file_servefromattach($filepath, $validators, $directory)) {
                 if (!in_array($file->id(), $input['fids'])) {
                     $input['fids'][] = $file->id();
                 }
             }
         } else {
             if ($file = filefield_sources_save_file($filepath, $validators, $directory)) {
                 if (!in_array($file->id(), $input['fids'])) {
                     $input['fids'][] = $file->id();
                 }
                 // Delete the original file if "moving" the file instead of copying.
                 if ($element['#filefield_sources_settings']['flysystem']['attach_mode'] !== FILEFIELD_SOURCE_FLYSYSTEM_ATTACH_MODE_COPY) {
                     @unlink($filepath);
                 }
             }
         }
         // Restore the original file name if the file still exists.
         if (file_exists($filepath) && $filepath != $original_filepath) {
             rename($filepath, $original_filepath);
         }
         $input['filefield_flysystem']['filename'] = '';
     }
 }
 /**
  * {@inheritdoc}
  */
 public static function value(array &$element, &$input, FormStateInterface $form_state)
 {
     if (isset($input['filefield_clipboard']['contents']) && strlen($input['filefield_clipboard']['contents']) > 0) {
         // Check that the destination is writable.
         $temporary_directory = 'temporary://';
         if (!file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS)) {
             \Drupal::logger('filefield_sources')->log(E_NOTICE, '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 = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
         // 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)) {
             $url = $input['filefield_clipboard']['filename'];
             \Drupal::logger('filefield_sources')->log(E_NOTICE, '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 = $input['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);
         $extension = \Drupal::service('file.mime_type.guesser.extension')->convertMimeTypeToExtension($mimetype);
         $filename = trim($input['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']))) {
             if (!in_array($file->id(), $input['fids'])) {
                 $input['fids'][] = $file->id();
             }
         }
         // Remove the temporary file generated from paste.
         @unlink($filepath);
     }
 }
 /**
  * {@inheritdoc}
  */
 public static function value(array &$element, &$input, FormStateInterface $form_state)
 {
     if (isset($input['filefield_remote']['url']) && strlen($input['filefield_remote']['url']) > 0 && UrlHelper::isValid($input['filefield_remote']['url']) && $input['filefield_remote']['url'] != FILEFIELD_SOURCE_REMOTE_HINT_TEXT) {
         $field = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
         $url = $input['filefield_remote']['url'];
         // Check that the destination is writable.
         $temporary_directory = 'temporary://';
         if (!file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS)) {
             \Drupal::logger('filefield_sources')->log(E_NOTICE, '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 = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
         // 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)) {
             \Drupal::logger('filefield_sources')->log(E_NOTICE, '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;
         }
         // Check the headers to make sure it exists and is within the allowed
         // size.
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_HEADER, TRUE);
         curl_setopt($ch, CURLOPT_NOBODY, TRUE);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
         curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(get_called_class(), 'parseHeader'));
         // Causes a warning if PHP safe mode is on.
         @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
         curl_exec($ch);
         $info = curl_getinfo($ch);
         if ($info['http_code'] != 200) {
             curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
             $file_contents = curl_exec($ch);
             $info = curl_getinfo($ch);
         }
         curl_close($ch);
         if ($info['http_code'] != 200) {
             switch ($info['http_code']) {
                 case 403:
                     $form_state->setError($element, t('The remote file could not be transferred because access to the file was denied.'));
                     break;
                 case 404:
                     $form_state->setError($element, t('The remote file could not be transferred because it was not found.'));
                     break;
                 default:
                     $form_state->setError($element, t('The remote file could not be transferred due to an HTTP error (@code).', array('@code' => $info['http_code'])));
             }
             return;
         }
         // Update the $url variable to reflect any redirects.
         $url = $info['url'];
         $url_info = parse_url($url);
         // Determine the proper filename by reading the filename given in the
         // Content-Disposition header. If the server fails to send this header,
         // fall back on the basename of the URL.
         //
         // We prefer to use the Content-Disposition header, because we can then
         // use URLs like http://example.com/get_file/23 which would otherwise be
         // rejected because the URL basename lacks an extension.
         $filename = static::filename();
         if (empty($filename)) {
             $filename = rawurldecode(basename($url_info['path']));
         }
         $pathinfo = pathinfo($filename);
         // Create the file extension from the MIME header if all else has failed.
         if (empty($pathinfo['extension']) && ($extension = static::mimeExtension())) {
             $filename = $filename . '.' . $extension;
             $pathinfo = pathinfo($filename);
         }
         $filename = filefield_sources_clean_filename($filename, $field->getSetting('file_extensions'));
         $filepath = file_create_filename($filename, $temporary_directory);
         if (empty($pathinfo['extension'])) {
             $form_state->setError($element, t('The remote URL must be a file and have an extension.'));
             return;
         }
         // Perform basic extension check on the file before trying to transfer.
         $extensions = $field->getSetting('file_extensions');
         $regex = '/\\.(' . preg_replace('/[ +]/', '|', preg_quote($extensions)) . ')$/i';
         if (!empty($extensions) && !preg_match($regex, $filename)) {
             $form_state->setError($element, t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions)));
             return;
         }
         // Check file size based off of header information.
         if (!empty($element['#upload_validators']['file_validate_size'][0])) {
             $max_size = $element['#upload_validators']['file_validate_size'][0];
             $file_size = $info['download_content_length'];
             if ($file_size > $max_size) {
                 $form_state->setError($element, t('The remote file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($file_size), '%maxsize' => format_size($max_size))));
                 return;
             }
         }
         // Set progress bar information.
         $options = array('key' => $element['#entity_type'] . '_' . $element['#bundle'] . '_' . $element['#field_name'] . '_' . $element['#delta'], 'filepath' => $filepath);
         static::setTransferOptions($options);
         $transfer_success = FALSE;
         // If we've already downloaded the entire file because the
         // header-retrieval failed, just ave the contents we have.
         if (isset($file_contents)) {
             if ($fp = @fopen($filepath, 'w')) {
                 fwrite($fp, $file_contents);
                 fclose($fp);
                 $transfer_success = TRUE;
             }
         } else {
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, $url);
             curl_setopt($ch, CURLOPT_HEADER, FALSE);
             curl_setopt($ch, CURLOPT_WRITEFUNCTION, array(get_called_class(), 'curlWrite'));
             // Causes a warning if PHP safe mode is on.
             @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
             $transfer_success = curl_exec($ch);
             curl_close($ch);
         }
         if ($transfer_success && ($file = filefield_sources_save_file($filepath, $element['#upload_validators'], $element['#upload_location']))) {
             if (!in_array($file->id(), $input['fids'])) {
                 $input['fids'][] = $file->id();
             }
         }
         // Delete the temporary file.
         @unlink($filepath);
     }
 }