Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / system / src / Form / FileSystemForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Datetime\DateFormatterInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\StreamWrapper\PrivateStream;
9 use Drupal\Core\StreamWrapper\PublicStream;
10 use Drupal\Core\Form\ConfigFormBase;
11 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
12 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Configure file system settings for this site.
17  */
18 class FileSystemForm extends ConfigFormBase {
19
20   /**
21    * The date formatter service.
22    *
23    * @var \Drupal\Core\Datetime\DateFormatterInterface
24    */
25   protected $dateFormatter;
26
27   /**
28    * The stream wrapper manager.
29    *
30    * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
31    */
32   protected $streamWrapperManager;
33
34   /**
35    * Constructs a FileSystemForm object.
36    *
37    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
38    *   The factory for configuration objects.
39    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
40    *   The date formatter service.
41    * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
42    *   The stream wrapper manager.
43    */
44   public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, StreamWrapperManagerInterface $stream_wrapper_manager) {
45     parent::__construct($config_factory);
46     $this->dateFormatter = $date_formatter;
47     $this->streamWrapperManager = $stream_wrapper_manager;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public static function create(ContainerInterface $container) {
54     return new static (
55       $container->get('config.factory'),
56       $container->get('date.formatter'),
57       $container->get('stream_wrapper_manager')
58     );
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getFormId() {
65     return 'system_file_system_settings';
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function getEditableConfigNames() {
72     return ['system.file'];
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function buildForm(array $form, FormStateInterface $form_state) {
79     $config = $this->config('system.file');
80     $form['file_public_path'] = [
81       '#type' => 'item',
82       '#title' => t('Public file system path'),
83       '#markup' => PublicStream::basePath(),
84       '#description' => t('A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web. This must be changed in settings.php'),
85     ];
86
87     $form['file_public_base_url'] = [
88       '#type' => 'item',
89       '#title' => t('Public file base URL'),
90       '#markup' => PublicStream::baseUrl(),
91       '#description' => t('The base URL that will be used for public file URLs. This can be changed in settings.php'),
92     ];
93
94     $form['file_private_path'] = [
95       '#type' => 'item',
96       '#title' => t('Private file system path'),
97       '#markup' => (PrivateStream::basePath() ? PrivateStream::basePath() : t('Not set')),
98       '#description' => t('An existing local file system path for storing private files. It should be writable by Drupal and not accessible over the web. This must be changed in settings.php'),
99     ];
100
101     $form['file_temporary_path'] = [
102       '#type' => 'textfield',
103       '#title' => t('Temporary directory'),
104       '#default_value' => $config->get('path.temporary'),
105       '#maxlength' => 255,
106       '#description' => t('A local file system path where temporary files will be stored. This directory should not be accessible over the web.'),
107       '#after_build' => ['system_check_directory'],
108     ];
109     // Any visible, writeable wrapper can potentially be used for the files
110     // directory, including a remote file system that integrates with a CDN.
111     $options = $this->streamWrapperManager->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE);
112
113     if (!empty($options)) {
114       $form['file_default_scheme'] = [
115         '#type' => 'radios',
116         '#title' => t('Default download method'),
117         '#default_value' => $config->get('default_scheme'),
118         '#options' => $options,
119         '#description' => t('This setting is used as the preferred download method. The use of public files is more efficient, but does not provide any access control.'),
120       ];
121     }
122
123     $intervals = [0, 21600, 43200, 86400, 604800, 2419200, 7776000];
124     $period = array_combine($intervals, array_map([$this->dateFormatter, 'formatInterval'], $intervals));
125     $period[0] = t('Never');
126     $form['temporary_maximum_age'] = [
127       '#type' => 'select',
128       '#title' => t('Delete orphaned files after'),
129       '#default_value' => $config->get('temporary_maximum_age'),
130       '#options' => $period,
131       '#description' => t('Orphaned files are not referenced from any content but remain in the file system and may appear in administrative listings. <strong>Warning:</strong> If enabled, orphaned files will be permanently deleted and may not be recoverable.'),
132     ];
133
134     return parent::buildForm($form, $form_state);
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function submitForm(array &$form, FormStateInterface $form_state) {
141     $config = $this->config('system.file')
142       ->set('path.temporary', $form_state->getValue('file_temporary_path'))
143       ->set('temporary_maximum_age', $form_state->getValue('temporary_maximum_age'));
144
145     if ($form_state->hasValue('file_default_scheme')) {
146       $config->set('default_scheme', $form_state->getValue('file_default_scheme'));
147     }
148     $config->save();
149
150     parent::submitForm($form, $form_state);
151   }
152
153 }