Elgg  Version master
StickyForms.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Forms;
4 
11 class StickyForms {
12 
18  public function __construct(protected \ElggSession $session) {
19  }
20 
32  public function makeStickyForm(string $form_name, array $ignored_field_names = []): void {
33  $this->clearStickyForm($form_name);
34 
35  $default_ignored_field_names = [
36  '__elgg_ts', // never store CSRF tokens
37  '__elgg_token', // never store CSRF tokens
38  '_elgg_sticky_form_name', // from sticky form support
39  '_elgg_sticky_ignored_fields', // from sticky form support
40  '_route', // added by router
41  ];
42  $ignored_field_names = array_merge($default_ignored_field_names, $ignored_field_names);
43 
44  $data = $this->session->get('sticky_forms', []);
45  $req = _elgg_services()->request;
46 
47  // will go through XSS filtering in elgg_get_sticky_value()
48  $vars = array_merge($req->query->all(), $req->request->all());
49  foreach ($ignored_field_names as $key) {
50  unset($vars[$key]);
51  }
52 
53  $data[$form_name] = $vars;
54 
55  $this->session->set('sticky_forms', $data);
56  }
57 
69  public function clearStickyForm(string $form_name): void {
70  $data = $this->session->get('sticky_forms', []);
71  unset($data[$form_name]);
72 
73  $this->session->set('sticky_forms', $data);
74  }
75 
83  public function isStickyForm(string $form_name): bool {
84  $data = $this->session->get('sticky_forms', []);
85  return isset($data[$form_name]);
86  }
87 
98  public function getStickyValue(string $form_name, string $variable = '', $default = null, bool $filter_result = true) {
99  $data = $this->session->get('sticky_forms', []);
100  if (isset($data[$form_name][$variable])) {
101  $value = $data[$form_name][$variable];
102  if ($filter_result) {
103  // XSS filter result
105  }
106 
107  return $value;
108  }
109 
110  return $default;
111  }
112 
121  public function getStickyValues(string $form_name, bool $filter_result = true): array {
122  $data = $this->session->get('sticky_forms', []);
123  if (!isset($data[$form_name])) {
124  return [];
125  }
126 
127  $values = $data[$form_name];
128  if ($filter_result) {
129  foreach ($values as $key => $value) {
130  // XSS filter result
131  $values[$key] = elgg_sanitize_input($value);
132  }
133  }
134 
135  return $values;
136  }
137 }
$vars
Definition: theme.php:5
if(! $entity instanceof \ElggUser) $data
Definition: attributes.php:13
Elgg Session Management.
Definition: ElggSession.php:19
Stick forms service.
Definition: StickyForms.php:11
isStickyForm(string $form_name)
Does form submission data exist for this form?
Definition: StickyForms.php:83
makeStickyForm(string $form_name, array $ignored_field_names=[])
Save form submission data (all GET and POST vars) into a session cache.
Definition: StickyForms.php:32
__construct(protected \ElggSession $session)
Constructor.
Definition: StickyForms.php:18
clearStickyForm(string $form_name)
Remove form submission data from the session.
Definition: StickyForms.php:69
getStickyValue(string $form_name, string $variable='', $default=null, bool $filter_result=true)
Get a specific value from cached form submission data.
Definition: StickyForms.php:98
getStickyValues(string $form_name, bool $filter_result=true)
Get all submission data cached for a form.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_sanitize_input($input)
Filter input from a given string based on registered events.
Definition: input.php:77
$value
Definition: generic.php:51
$default
Definition: checkbox.php:30
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:29
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
Definition: summary.php:44