Elgg  Version master
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  $data[$form_name] = $vars;
59 
60  $this->session->set('sticky_forms', $data);
61  }
62 
74  public function clearStickyForm(string $form_name): void {
75  $data = $this->session->get('sticky_forms', []);
76  unset($data[$form_name]);
77 
78  $this->session->set('sticky_forms', $data);
79  }
80 
88  public function isStickyForm(string $form_name): bool {
89  $data = $this->session->get('sticky_forms', []);
90  return isset($data[$form_name]);
91  }
92 
103  public function getStickyValue(string $form_name, string $variable = '', $default = null, bool $filter_result = true) {
104  $data = $this->session->get('sticky_forms', []);
105  if (isset($data[$form_name][$variable])) {
106  $value = $data[$form_name][$variable];
107  if ($filter_result) {
108  // XSS filter result
110  }
111 
112  return $value;
113  }
114 
115  return $default;
116  }
117 
126  public function getStickyValues(string $form_name, bool $filter_result = true): array {
127  $data = $this->session->get('sticky_forms', []);
128  if (!isset($data[$form_name])) {
129  return [];
130  }
131 
132  $values = $data[$form_name];
133  if ($filter_result) {
134  foreach ($values as $key => $value) {
135  // XSS filter result
136  $values[$key] = elgg_sanitize_input($value);
137  }
138  }
139 
140  return $values;
141  }
142 }
$default
Definition: checkbox.php:31
clearStickyForm(string $form_name)
Remove form submission data from the session.
Definition: StickyForms.php:74
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(elgg_trigger_event_results('usersettings:save', 'user', $event_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
isStickyForm(string $form_name)
Does form submission data exist for this form?
Definition: StickyForms.php:88
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
_elgg_services()
Get the global service provider.
Definition: elgglib.php:347
$vars['head']
Definition: html.php:24
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