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 }
$default
Definition: checkbox.php:30
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
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
elgg_sanitize_input($input)
Filter input from a given string based on registered events.
Definition: input.php:77
__construct(protected\ElggSession $session)
Constructor.
Definition: StickyForms.php:18
Elgg Session Management.
Definition: ElggSession.php:19
$value
Definition: generic.php:51
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
isStickyForm(string $form_name)
Does form submission data exist for this form?
Definition: StickyForms.php:83
getStickyValues(string $form_name, bool $filter_result=true)
Get all submission data cached for a form.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:29
Stick forms service.
Definition: StickyForms.php:11
$vars
Definition: theme.php:5
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
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