Further modules included.
[yaffs-website] / web / modules / contrib / filefield_sources / src / Plugin / FilefieldSource / Remote.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\filefield_sources\Plugin\FilefieldSource\Remote.
6  */
7
8 namespace Drupal\filefield_sources\Plugin\FilefieldSource;
9
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;
18
19 /**
20  * A FileField source plugin to allow downloading a file from a remote server.
21  *
22  * @FilefieldSource(
23  *   id = "remote",
24  *   name = @Translation("Remote URL textfield"),
25  *   label = @Translation("Remote URL"),
26  *   description = @Translation("Download a file from a remote server.")
27  * )
28  */
29 class Remote implements FilefieldSourceInterface {
30
31   /**
32    * {@inheritdoc}
33    */
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'];
38
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');
44         return;
45       }
46
47       // Check that the destination is writable.
48       $directory = $element['#upload_location'];
49       $mode = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
50
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');
56         return;
57       }
58
59       // Check the headers to make sure it exists and is within the allowed
60       // size.
61       $ch = curl_init();
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);
69       curl_exec($ch);
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);
75       }
76       curl_close($ch);
77
78       if ($info['http_code'] != 200) {
79         switch ($info['http_code']) {
80           case 403:
81             $form_state->setError($element, t('The remote file could not be transferred because access to the file was denied.'));
82             break;
83
84           case 404:
85             $form_state->setError($element, t('The remote file could not be transferred because it was not found.'));
86             break;
87
88           default:
89             $form_state->setError($element, t('The remote file could not be transferred due to an HTTP error (@code).', array('@code' => $info['http_code'])));
90         }
91         return;
92       }
93
94       // Update the $url variable to reflect any redirects.
95       $url = $info['url'];
96       $url_info = parse_url($url);
97
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.
101       //
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']));
108       }
109
110       $pathinfo = pathinfo($filename);
111
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);
116       }
117
118       $filename = filefield_sources_clean_filename($filename, $field->getSetting('file_extensions'));
119       $filepath = file_create_filename($filename, $temporary_directory);
120
121       if (empty($pathinfo['extension'])) {
122         $form_state->setError($element, t('The remote URL must be a file and have an extension.'));
123         return;
124       }
125
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)));
131         return;
132       }
133
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))));
140           return;
141         }
142       }
143
144       // Set progress bar information.
145       $options = array(
146         'key' => $element['#entity_type'] . '_' . $element['#bundle'] . '_' . $element['#field_name'] . '_' . $element['#delta'],
147         'filepath' => $filepath,
148       );
149       static::setTransferOptions($options);
150
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);
157           fclose($fp);
158           $transfer_success = TRUE;
159         }
160       }
161       // If we don't have the file contents, download the actual file.
162       else {
163         $ch = curl_init();
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);
170         curl_close($ch);
171       }
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();
175         }
176       }
177
178       // Delete the temporary file.
179       @unlink($filepath);
180     }
181   }
182
183   /**
184    * Set a transfer key that can be retreived by the progress function.
185    */
186   protected static function setTransferOptions($options = NULL) {
187     static $current = FALSE;
188     if (isset($options)) {
189       $current = $options;
190     }
191     return $current;
192   }
193
194   /**
195    * Get a transfer key that can be retrieved by the progress function.
196    */
197   protected static function getTransferOptions() {
198     return static::setTransferOptions();
199   }
200
201   /**
202    * Save the file to disk. Also updates progress bar.
203    */
204   protected static function curlWrite(&$ch, $data) {
205     $progress_update = 0;
206     $options = static::getTransferOptions();
207
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) {
212       $progress_update++;
213       $progress = array(
214         'current' => curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD),
215         'total' => curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD),
216       );
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);
221       }
222       else {
223         \Drupal::cache()->delete($cid);
224       }
225     }
226
227     $data_length = 0;
228     if ($fp = @fopen($options['filepath'], 'a')) {
229       fwrite($fp, $data);
230       fclose($fp);
231       $data_length = strlen($data);
232     }
233
234     return $data_length;
235   }
236
237   /**
238    * Parse cURL header and record the filename specified in Content-Disposition.
239    */
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]);
244     }
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);
249     }
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);
253     }
254
255     // This is required by cURL.
256     return strlen($header);
257   }
258
259   /**
260    * Get/set the remote file extension in a static variable.
261    */
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);
266     if ($result) {
267       $extension = $result;
268     }
269     return $extension;
270   }
271
272   /**
273    * Get/set the remote file name in a static variable.
274    */
275   protected static function filename($curl_filename = NULL) {
276     static $filename = NULL;
277     if (isset($curl_filename)) {
278       $filename = $curl_filename;
279     }
280     return $filename;
281   }
282
283   /**
284    * {@inheritdoc}
285    */
286   public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
287
288     $element['filefield_remote'] = array(
289       '#weight' => 100.5,
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,
295     );
296
297     $element['filefield_remote']['url'] = array(
298       '#type' => 'textfield',
299       '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
300       '#maxlength' => NULL,
301     );
302
303     $class = '\Drupal\file\Element\ManagedFile';
304     $ajax_settings = [
305       'callback' => [$class, 'uploadAjaxCallback'],
306       'options' => [
307         'query' => [
308           'element_parents' => implode('/', $element['#array_parents']),
309         ],
310       ],
311       'wrapper' => $element['upload_button']['#ajax']['wrapper'],
312       'effect' => 'fade',
313       'progress' => [
314         'type' => 'bar',
315         'path' => 'file/remote/progress/' . $element['#entity_type'] . '/' . $element['#bundle'] . '/' . $element['#field_name'] . '/' . $element['#delta'],
316         'message' => t('Starting transfer...'),
317       ],
318     ];
319
320     $element['filefield_remote']['transfer'] = [
321       '#name' => implode('_', $element['#parents']) . '_transfer',
322       '#type' => 'submit',
323       '#value' => t('Transfer'),
324       '#validate' => array(),
325       '#submit' => ['filefield_sources_field_submit'],
326       '#limit_validation_errors' => [$element['#parents']],
327       '#ajax' => $ajax_settings,
328     ];
329
330     return $element;
331   }
332
333   /**
334    * Theme the output of the remote element.
335    */
336   public static function element($variables) {
337     $element = $variables['element'];
338
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>';
341   }
342
343   /**
344    * Menu callback; progress.js callback to return upload progress.
345    */
346   public static function progress($entity_type, $bundle_name, $field_name, $delta) {
347     $key = $entity_type . '_' . $bundle_name . '_' . $field_name . '_' . $delta;
348     $progress = array(
349       'message' => t('Starting transfer...'),
350       'percentage' => -1,
351     );
352
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);
358     }
359
360     return new JsonResponse($progress);
361   }
362
363   /**
364    * Define routes for Remote source.
365    *
366    * @return array
367    *   Array of routes.
368    */
369   public static function routes() {
370     $routes = array();
371
372     $routes['filefield_sources.remote'] = new Route(
373       '/file/remote/progress/{entity_type}/{bundle_name}/{field_name}/{delta}',
374       array(
375         '_controller' => get_called_class() . '::progress',
376       ),
377       array(
378         '_access' => 'TRUE',
379       )
380     );
381
382     return $routes;
383   }
384
385   /**
386    * Implements hook_filefield_source_settings().
387    */
388   public static function settings(WidgetInterface $plugin) {
389     $return = array();
390
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');
394     }
395
396     return $return;
397
398   }
399
400 }