Further modules included.
[yaffs-website] / web / modules / contrib / filefield_sources / src / Plugin / FilefieldSource / Clipboard.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\filefield_sources\Plugin\FilefieldSource\Clipboard.
6  */
7
8 namespace Drupal\filefield_sources\Plugin\FilefieldSource;
9
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\filefield_sources\FilefieldSourceInterface;
12 use Drupal\Core\Site\Settings;
13
14 /**
15  * A FileField source plugin to allow transfer of files through the clipboard.
16  *
17  * @FilefieldSource(
18  *   id = "clipboard",
19  *   name = @Translation("Paste from clipboard (<a href=""http://drupal.org/node/1775902"">limited browser support</a>)"),
20  *   label = @Translation("Clipboard"),
21  *   description = @Translation("Allow users to paste a file directly from the clipboard."),
22  *   weight = 1
23  * )
24  */
25 class Clipboard implements FilefieldSourceInterface {
26
27   /**
28    * {@inheritdoc}
29    */
30   public static function value(array &$element, &$input, FormStateInterface $form_state) {
31     if (isset($input['filefield_clipboard']['contents']) && strlen($input['filefield_clipboard']['contents']) > 0) {
32       // Check that the destination is writable.
33       $temporary_directory = 'temporary://';
34       if (!file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS)) {
35         \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)));
36         drupal_set_message(t('The file could not be transferred because the temporary directory is not writable.'), 'error');
37         return;
38       }
39       // Check that the destination is writable.
40       $directory = $element['#upload_location'];
41       $mode = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
42
43       // This first chmod check is for other systems such as S3, which don't
44       // work with file_prepare_directory().
45       if (!drupal_chmod($directory, $mode) && !file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
46         $url = $input['filefield_clipboard']['filename'];
47         \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)));
48         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');
49         return;
50       }
51
52       // Split the file information in mimetype and base64 encoded binary.
53       $base64_data = $input['filefield_clipboard']['contents'];
54       $comma_position = strpos($base64_data, ',');
55       $semicolon_position = strpos($base64_data, ';');
56       $file_contents = base64_decode(substr($base64_data, $comma_position + 1));
57       $mimetype = substr($base64_data, 5, $semicolon_position - 5);
58
59       $extension = \Drupal::service('file.mime_type.guesser.extension')->convertMimeTypeToExtension($mimetype);
60
61       $filename = trim($input['filefield_clipboard']['filename']);
62       $filename = preg_replace('/\.[a-z0-9]{3,4}$/', '', $filename);
63       $filename = (empty($filename) ? 'paste_' . REQUEST_TIME : $filename) . '.' . $extension;
64       $filepath = file_create_filename($filename, $temporary_directory);
65
66       $copy_success = FALSE;
67       if ($fp = @fopen($filepath, 'w')) {
68         fwrite($fp, $file_contents);
69         fclose($fp);
70         $copy_success = TRUE;
71       }
72
73       if ($copy_success && $file = filefield_sources_save_file($filepath, $element['#upload_validators'], $element['#upload_location'])) {
74         if (!in_array($file->id(), $input['fids'])) {
75           $input['fids'][] = $file->id();
76         }
77       }
78
79       // Remove the temporary file generated from paste.
80       @unlink($filepath);
81     }
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
88     $element['filefield_clipboard'] = array(
89       '#weight' => 100.5,
90       '#theme' => 'filefield_sources_element',
91       '#source_id' => 'clipboard',
92       // Required for proper theming.
93       '#filefield_source' => TRUE,
94       '#filefield_sources_hint_text' => t('Enter filename then paste.'),
95       '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
96     );
97
98     $element['filefield_clipboard']['capture'] = array(
99       '#type' => 'item',
100       '#markup' => '<div class="filefield-source-clipboard-capture" contenteditable="true"><span class="hint">example_filename.png</span></div> <span class="hint">' . t('ctrl + v') . '</span>',
101       '#description' => t('Enter a file name and paste an image from the clipboard. This feature only works in <a href="http://drupal.org/node/1775902">limited browsers</a>.'),
102     );
103
104     $element['filefield_clipboard']['filename'] = array(
105       '#type' => 'hidden',
106       '#attributes' => array('class' => array('filefield-source-clipboard-filename')),
107     );
108     $element['filefield_clipboard']['contents'] = array(
109       '#type' => 'hidden',
110       '#attributes' => array('class' => array('filefield-source-clipboard-contents')),
111     );
112
113     $class = '\Drupal\file\Element\ManagedFile';
114     $ajax_settings = [
115       'callback' => [$class, 'uploadAjaxCallback'],
116       'options' => [
117         'query' => [
118           'element_parents' => implode('/', $element['#array_parents']),
119         ],
120       ],
121       'wrapper' => $element['upload_button']['#ajax']['wrapper'],
122       'effect' => 'fade',
123       'progress' => [
124         'type' => 'throbber',
125         'message' => t('Transfering file...'),
126       ],
127     ];
128
129     $element['filefield_clipboard']['upload'] = [
130       '#name' => implode('_', $element['#parents']) . '_clipboard_upload_button',
131       '#type' => 'submit',
132       '#value' => t('Upload'),
133       '#attributes' => ['class' => ['js-hide']],
134       '#validate' => [],
135       '#submit' => ['filefield_sources_field_submit'],
136       '#limit_validation_errors' => [$element['#parents']],
137       '#ajax' => $ajax_settings,
138     ];
139
140     return $element;
141   }
142
143   /**
144    * Theme the output of the clipboard element.
145    */
146   public static function element($variables) {
147     $element = $variables['element'];
148
149     return '<div class="filefield-source filefield-source-clipboard clear-block">' . drupal_render_children($element) . '</div>';
150   }
151
152 }