Elgg  Version 5.1
StickyForms.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Forms;
4 
11 class StickyForms {
12 
16  protected $session;
17 
23  public function __construct(\ElggSession $session) {
24  $this->session = $session;
25  }
26 
38  public function makeStickyForm(string $form_name, array $ignored_field_names = []): void {
39  $this->clearStickyForm($form_name);
40 
41  $default_ignored_field_names = [
42  '__elgg_ts', // never store CSRF tokens
43  '__elgg_token', // never store CSRF tokens
44  '_elgg_sticky_form_name', // from sticky form support
45  '_elgg_sticky_ignored_fields', // from sticky form support
46  '_route', // added by router
47  ];
48  $ignored_field_names = array_merge($default_ignored_field_names, $ignored_field_names);
49 
50  $data = $this->session->get('sticky_forms', []);
51  $req = _elgg_services()->request;
52 
53  // will go through XSS filtering in elgg_get_sticky_value()
54  $vars = array_merge($req->query->all(), $req->request->all());
55  foreach ($ignored_field_names as $key) {
56  unset($vars[$key]);
57  }
58 
59  $data[$form_name] = $vars;
60 
61  $this->session->set('sticky_forms', $data);
62  }
63 
75  public function clearStickyForm(string $form_name): void {
76  $data = $this->session->get('sticky_forms', []);
77  unset($data[$form_name]);
78 
79  $this->session->set('sticky_forms', $data);
80  }
81 
89  public function isStickyForm(string $form_name): bool {
90  $data = $this->session->get('sticky_forms', []);
91  return isset($data[$form_name]);
92  }
93 
104  public function getStickyValue(string $form_name, string $variable = '', $default = null, bool $filter_result = true) {
105  $data = $this->session->get('sticky_forms', []);
106  if (isset($data[$form_name][$variable])) {
107  $value = $data[$form_name][$variable];
108  if ($filter_result) {
109  // XSS filter result
111  }
112 
113  return $value;
114  }
115 
116  return $default;
117  }
118 
127  public function getStickyValues(string $form_name, bool $filter_result = true): array {
128  $data = $this->session->get('sticky_forms', []);
129  if (!isset($data[$form_name])) {
130  return [];
131  }
132 
133  $values = $data[$form_name];
134  if ($filter_result) {
135  foreach ($values as $key => $value) {
136  // XSS filter result
137  $values[$key] = elgg_sanitize_input($value);
138  }
139  }
140 
141  return $values;
142  }
143 }
$default
Definition: checkbox.php:30
clearStickyForm(string $form_name)
Remove form submission data from the session.
Definition: StickyForms.php:75
getStickyValue(string $form_name, string $variable= '', $default=null, bool $filter_result=true)
Get a specific value from cached form submission data.
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
__construct(\ElggSession $session)
Constructor.
Definition: StickyForms.php:23
elgg_sanitize_input($input)
Filter input from a given string based on registered events.
Definition: input.php:77
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:89
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
Stick forms service.
Definition: StickyForms.php:11
$vars
Definition: theme.php:5
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
makeStickyForm(string $form_name, array $ignored_field_names=[])
Save form submission data (all GET and POST vars) into a session cache.
Definition: StickyForms.php:38