5 * Contains \Drupal\filefield_sources\Plugin\FilefieldSource\Remote.
8 namespace Drupal\filefield_sources\Plugin\FilefieldSource;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\filefield_sources\FilefieldSourceInterface;
12 use Symfony\Component\Routing\Route;
13 use Drupal\Core\Field\WidgetInterface;
14 use Symfony\Component\HttpFoundation\JsonResponse;
15 use Drupal\Component\Utility\UrlHelper;
16 use Drupal\Core\Site\Settings;
17 use Drupal\Component\Utility\Unicode;
20 * A FileField source plugin to allow downloading a file from a remote server.
24 * name = @Translation("Remote URL textfield"),
25 * label = @Translation("Remote URL"),
26 * description = @Translation("Download a file from a remote server.")
29 class Remote implements FilefieldSourceInterface {
34 public static function value(array &$element, &$input, FormStateInterface $form_state) {
35 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) {
36 $field = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
37 $url = $input['filefield_remote']['url'];
39 // Check that the destination is writable.
40 $temporary_directory = 'temporary://';
41 if (!file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS)) {
42 \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)));
43 drupal_set_message(t('The file could not be transferred because the temporary directory is not writable.'), 'error');
47 // Check that the destination is writable.
48 $directory = $element['#upload_location'];
49 $mode = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
51 // This first chmod check is for other systems such as S3, which don't
52 // work with file_prepare_directory().
53 if (!drupal_chmod($directory, $mode) && !file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
54 \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)));
55 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');
59 // Check the headers to make sure it exists and is within the allowed
62 curl_setopt($ch, CURLOPT_URL, $url);
63 curl_setopt($ch, CURLOPT_HEADER, TRUE);
64 curl_setopt($ch, CURLOPT_NOBODY, TRUE);
65 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
66 curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(get_called_class(), 'parseHeader'));
67 // Causes a warning if PHP safe mode is on.
68 @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
70 $info = curl_getinfo($ch);
71 if ($info['http_code'] != 200) {
72 curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
73 $file_contents = curl_exec($ch);
74 $info = curl_getinfo($ch);
78 if ($info['http_code'] != 200) {
79 switch ($info['http_code']) {
81 $form_state->setError($element, t('The remote file could not be transferred because access to the file was denied.'));
85 $form_state->setError($element, t('The remote file could not be transferred because it was not found.'));
89 $form_state->setError($element, t('The remote file could not be transferred due to an HTTP error (@code).', array('@code' => $info['http_code'])));
94 // Update the $url variable to reflect any redirects.
96 $url_info = parse_url($url);
98 // Determine the proper filename by reading the filename given in the
99 // Content-Disposition header. If the server fails to send this header,
100 // fall back on the basename of the URL.
102 // We prefer to use the Content-Disposition header, because we can then
103 // use URLs like http://example.com/get_file/23 which would otherwise be
104 // rejected because the URL basename lacks an extension.
105 $filename = static::filename();
106 if (empty($filename)) {
107 $filename = rawurldecode(basename($url_info['path']));
110 $pathinfo = pathinfo($filename);
112 // Create the file extension from the MIME header if all else has failed.
113 if (empty($pathinfo['extension']) && $extension = static::mimeExtension()) {
114 $filename = $filename . '.' . $extension;
115 $pathinfo = pathinfo($filename);
118 $filename = filefield_sources_clean_filename($filename, $field->getSetting('file_extensions'));
119 $filepath = file_create_filename($filename, $temporary_directory);
121 if (empty($pathinfo['extension'])) {
122 $form_state->setError($element, t('The remote URL must be a file and have an extension.'));
126 // Perform basic extension check on the file before trying to transfer.
127 $extensions = $field->getSetting('file_extensions');
128 $regex = '/\.(' . preg_replace('/[ +]/', '|', preg_quote($extensions)) . ')$/i';
129 if (!empty($extensions) && !preg_match($regex, $filename)) {
130 $form_state->setError($element, t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions)));
134 // Check file size based off of header information.
135 if (!empty($element['#upload_validators']['file_validate_size'][0])) {
136 $max_size = $element['#upload_validators']['file_validate_size'][0];
137 $file_size = $info['download_content_length'];
138 if ($file_size > $max_size) {
139 $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))));
144 // Set progress bar information.
146 'key' => $element['#entity_type'] . '_' . $element['#bundle'] . '_' . $element['#field_name'] . '_' . $element['#delta'],
147 'filepath' => $filepath,
149 static::setTransferOptions($options);
151 $transfer_success = FALSE;
152 // If we've already downloaded the entire file because the
153 // header-retrieval failed, just ave the contents we have.
154 if (isset($file_contents)) {
155 if ($fp = @fopen($filepath, 'w')) {
156 fwrite($fp, $file_contents);
158 $transfer_success = TRUE;
161 // If we don't have the file contents, download the actual file.
164 curl_setopt($ch, CURLOPT_URL, $url);
165 curl_setopt($ch, CURLOPT_HEADER, FALSE);
166 curl_setopt($ch, CURLOPT_WRITEFUNCTION, array(get_called_class(), 'curlWrite'));
167 // Causes a warning if PHP safe mode is on.
168 @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
169 $transfer_success = curl_exec($ch);
172 if ($transfer_success && $file = filefield_sources_save_file($filepath, $element['#upload_validators'], $element['#upload_location'])) {
173 if (!in_array($file->id(), $input['fids'])) {
174 $input['fids'][] = $file->id();
178 // Delete the temporary file.
184 * Set a transfer key that can be retreived by the progress function.
186 protected static function setTransferOptions($options = NULL) {
187 static $current = FALSE;
188 if (isset($options)) {
195 * Get a transfer key that can be retrieved by the progress function.
197 protected static function getTransferOptions() {
198 return static::setTransferOptions();
202 * Save the file to disk. Also updates progress bar.
204 protected static function curlWrite(&$ch, $data) {
205 $progress_update = 0;
206 $options = static::getTransferOptions();
208 // Get the current progress and update the progress value.
209 // Only update every 64KB to reduce Drupal::cache()->set() calls.
210 // cURL usually writes in 16KB chunks.
211 if (curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD) / 65536 > $progress_update) {
214 'current' => curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD),
215 'total' => curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD),
217 // Set a cache so that we can retrieve this value from the progress bar.
218 $cid = 'filefield_transfer:' . session_id() . ':' . $options['key'];
219 if ($progress['current'] != $progress['total']) {
220 \Drupal::cache()->set($cid, $progress, time() + 300);
223 \Drupal::cache()->delete($cid);
228 if ($fp = @fopen($options['filepath'], 'a')) {
231 $data_length = strlen($data);
238 * Parse cURL header and record the filename specified in Content-Disposition.
240 protected static function parseHeader(&$ch, $header) {
241 if (preg_match('/Content-Disposition:.*?filename="(.+?)"/', $header, $matches)) {
242 // Content-Disposition: attachment; filename="FILE NAME HERE"
243 static::filename($matches[1]);
245 elseif (preg_match('/Content-Disposition:.*?filename=([^; ]+)/', $header, $matches)) {
246 // Content-Disposition: attachment; filename=file.ext
247 $uri = trim($matches[1]);
248 static::filename($uri);
250 elseif (preg_match('/Content-Type:[ ]*([a-z0-9_\-]+\/[a-z0-9_\-]+)/i', $header, $matches)) {
251 $mime_type = $matches[1];
252 static::mimeExtension($mime_type);
255 // This is required by cURL.
256 return strlen($header);
260 * Get/set the remote file extension in a static variable.
262 protected static function mimeExtension($curl_mime_type = NULL) {
263 static $extension = NULL;
264 $mimetype = Unicode::strtolower($curl_mime_type);
265 $result = \Drupal::service('file.mime_type.guesser.extension')->convertMimeTypeToMostCommonExtension($mimetype);
267 $extension = $result;
273 * Get/set the remote file name in a static variable.
275 protected static function filename($curl_filename = NULL) {
276 static $filename = NULL;
277 if (isset($curl_filename)) {
278 $filename = $curl_filename;
286 public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
288 $element['filefield_remote'] = array(
290 '#theme' => 'filefield_sources_element',
291 '#source_id' => 'remote',
292 // Required for proper theming.
293 '#filefield_source' => TRUE,
294 '#filefield_sources_hint_text' => FILEFIELD_SOURCE_REMOTE_HINT_TEXT,
297 $element['filefield_remote']['url'] = array(
298 '#type' => 'textfield',
299 '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
300 '#maxlength' => NULL,
303 $class = '\Drupal\file\Element\ManagedFile';
305 'callback' => [$class, 'uploadAjaxCallback'],
308 'element_parents' => implode('/', $element['#array_parents']),
311 'wrapper' => $element['upload_button']['#ajax']['wrapper'],
315 'path' => 'file/remote/progress/' . $element['#entity_type'] . '/' . $element['#bundle'] . '/' . $element['#field_name'] . '/' . $element['#delta'],
316 'message' => t('Starting transfer...'),
320 $element['filefield_remote']['transfer'] = [
321 '#name' => implode('_', $element['#parents']) . '_transfer',
323 '#value' => t('Transfer'),
324 '#validate' => array(),
325 '#submit' => ['filefield_sources_field_submit'],
326 '#limit_validation_errors' => [$element['#parents']],
327 '#ajax' => $ajax_settings,
334 * Theme the output of the remote element.
336 public static function element($variables) {
337 $element = $variables['element'];
339 $element['url']['#field_suffix'] = drupal_render($element['transfer']);
340 return '<div class="filefield-source filefield-source-remote clear-block">' . drupal_render($element['url']) . '</div>';
344 * Menu callback; progress.js callback to return upload progress.
346 public static function progress($entity_type, $bundle_name, $field_name, $delta) {
347 $key = $entity_type . '_' . $bundle_name . '_' . $field_name . '_' . $delta;
349 'message' => t('Starting transfer...'),
353 if ($cache = \Drupal::cache()->get('filefield_transfer:' . session_id() . ':' . $key)) {
354 $current = $cache->data['current'];
355 $total = $cache->data['total'];
356 $progress['message'] = t('Transferring... (@current of @total)', array('@current' => format_size($current), '@total' => format_size($total)));
357 $progress['percentage'] = round(100 * $current / $total);
360 return new JsonResponse($progress);
364 * Define routes for Remote source.
369 public static function routes() {
372 $routes['filefield_sources.remote'] = new Route(
373 '/file/remote/progress/{entity_type}/{bundle_name}/{field_name}/{delta}',
375 '_controller' => get_called_class() . '::progress',
386 * Implements hook_filefield_source_settings().
388 public static function settings(WidgetInterface $plugin) {
391 // Add settings to the FileField widget form.
392 if (!filefield_sources_curl_enabled()) {
393 drupal_set_message(t('<strong>Filefield sources:</strong> remote plugin will be disabled without php-curl extension.'), 'warning');