Passed
Push — master ( 853f8f...c34860 )
by Alxarafe
25:12
created

Request::getAlphaNoHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/* Copyright (C) 2019       Alxarafe            <[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
namespace Alixar\Helpers;
18
19
/**
20
 * Provides secure and flexible support to the GET and POST request methods.
21
 *
22
 * @package Alxarafe\Helpers
23
 */
24
class Request
25
{
26
27
    // Filters
28
    const NO_CHECK = 0;             // 'none'=no check (only for param that should have very rich content)
29
    const NUMERIC = 1;              // 'int'=check it's numeric (integer or float)
30
    const NUMBER_COMMA = 2;         // 'intcomma'=check it's integer+comma ('1,2,3,4...')
31
    const ALPHA = 3;                // 'alpha'=check it's text and sign
32
    const LETTERS_ONLY = 4;         // 'aZ'=check it's a-z only
33
    const LETTERS_AND_NUMBERS = 5;  // 'aZ09'=check it's simple alpha string (recommended for keys)
34
    const AN_ARRAY = 6;             // 'array'=check it's array
35
    const SANITIZE = 7;             // 'san_alpha' = Use filter_var with FILTER_SANITIZE_STRING (do not use this for free text string)
36
    const NO_HTML = 8;              // 'nohtml', 'alphanohtml' = check there is no html content
37
    const ALPHA_NO_HTML = 9;              // 'nohtml', 'alphanohtml' = check there is no html content
38
    const CUSTOM = 10;               // 'custom' = custom filter specify $filter and $options)
39
40
    public static function get(string $variable, array $methods = [INPUT_GET, INPUT_POST], int $filter = self::NO_CHECK): string
41
    {
42
        $result = null;
43
        foreach ($methods as $method) {
44
            $result = filter_input($method, $variable);
45
            if (isset($result)) {
46
                break;
47
            }
48
        }
49
50
        if (!isset($result)) {
51
            return '';
52
        }
53
54
        switch ($filter) {
55
            case self::NO_CHECK : // 'none'=no check (only for param that should have very rich content)
56
                break;
57
            case self::NUMERIC : // 'int'=check it's numeric (integer or float)
58
                // Check param is a numeric value (integer but also float or hexadecimal)
59
                if (!is_numeric($result)) {
60
                    $result = '';
61
                }
62
                break;
63
            case self::NUMBER_COMMA: // 'intcomma'=check it's integer+comma ('1,2,3,4...')
64
                break;
65
            case self::ALPHA :// 'alpha'=check it's text and sign
66
                if (!is_array($result)) {
67
                    $result = trim($result);
68
                    // '"' is dangerous because param in url can close the href= or src= and add javascript functions.
69
                    // '../' is dangerous because it allows dir transversals
70
                    if (preg_match('/"/', $result)) {
71
                        $result = '';
72
                    } else {
73
                        if (preg_match('/\.\.\//', $result)) {
74
                            $result = '';
75
                        }
76
                    }
77
                }
78
                break;
79
            case self::LETTERS_ONLY:// 'aZ'=check it's a-z only
80
                if (!is_array($result)) {
81
                    $out = trim($result);
82
                    if (preg_match('/[^a-z]+/i', $result))
83
                        $result = '';
84
                }
85
                break;
86
            case self::LETTERS_AND_NUMBERS:// 'aZ09'=check it's simple alpha string (recommended for keys)
87
                if (!is_array($result)) {
88
                    $result = trim($result);
89
                    if (preg_match('/[^a-z0-9_\-\.]+/i', $result))
90
                        $result = '';
91
                }
92
                break;
93
            case self::AN_ARRAY :// 'array'=check it's array
94
                break;
95
            case self::SANITIZE :// 'san_alpha' = Use filter_var with FILTER_SANITIZE_STRING (do not use this for free text string)
96
                break;
97
            case self::NO_HTML :// 'nohtml = check there is no html content
98
                $result = dol_string_nohtmltag($result, 0);
99
                break;
100
            case self::ALPHA_NO_HTML :// 'alphanohtml' = check there is no html content
101
                if (!is_array($result)) {
102
                    $result = trim($result);
103
                    // '"' is dangerous because param in url can close the href= or src= and add javascript functions.
104
                    // '../' is dangerous because it allows dir transversals
105
                    if (preg_match('/"/', $result)) {
106
                        $result = '';
107
                    } else {
108
                        if (preg_match('/\.\.\//', $result)) {
109
                            $result = '';
110
                        }
111
                    }
112
                    $result = dol_string_nohtmltag($result);
113
                }
114
                break;
115
            case self::CUSTOM :// 'custom' = custom filter specify $filter and $options)
116
                break;
117
        }
118
119
        return $result;
120
    }
121
122
    public static function getAlpha(string $variable, array $methods = [INPUT_GET, INPUT_POST]): string
123
    {
124
        return self::get($variable, $methods, self::ALPHA);
125
    }
126
127
    public static function getAlphaNoHtml(string $variable, array $methods = [INPUT_GET, INPUT_POST]): string
128
    {
129
        return self::get($variable, $methods, self::ALPHA_NO_HTML);
130
    }
131
132
    public static function getAz(string $variable, array $methods = [INPUT_GET, INPUT_POST]): string
133
    {
134
        return self::get($variable, $methods, self::LETTERS_ONLY);
135
    }
136
137
    public static function getAz09(string $variable, array $methods = [INPUT_GET, INPUT_POST]): string
138
    {
139
        return self::get($variable, $methods, self::LETTERS_AND_NUMBERS);
140
    }
141
142
    public static function getNumber(string $variable, array $methods = [INPUT_GET, INPUT_POST]): string
143
    {
144
        return self::get($variable, $methods, self::NUMERIC);
145
    }
146
147
    /**
148
     *  Return value of a param into GET or POST supervariable.
149
     *  Use the property $user->default_values[path]['creatform'] and/or $user->default_values[path]['filters'] and/or $user->default_values[path]['sortorder']
150
     *  Note: The property $user->default_values is loaded by main.php when loading the user.
151
     *
152
     *  @param  string  $paramname   Name of parameter to found
153
     *  @param  string  $check	     Type of check
154
     *                               ''=no check (deprecated)
155
     *                               'none'=no check (only for param that should have very rich content)
156
     *                               'int'=check it's numeric (integer or float)
157
     *                               'intcomma'=check it's integer+comma ('1,2,3,4...')
158
     *                               'alpha'=check it's text and sign
159
     *                               'aZ'=check it's a-z only
160
     *                               'aZ09'=check it's simple alpha string (recommended for keys)
161
     *                               'array'=check it's array
162
     *                               'san_alpha'=Use filter_var with FILTER_SANITIZE_STRING (do not use this for free text string)
163
     *                               'nohtml', 'alphanohtml'=check there is no html content
164
     *                               'custom'= custom filter specify $filter and $options)
165
     *  @param	int		$method	     Type of method (0 = get then post, 1 = only get, 2 = only post, 3 = post then get, 4 = post then get then cookie)
166
     *  @param  int     $filter      Filter to apply when $check is set to 'custom'. (See http://php.net/manual/en/filter.filters.php for détails)
167
     *  @param  mixed   $options     Options to pass to filter_var when $check is set to 'custom'
168
     *  @param	string	$noreplace	 Force disable of replacement of __xxx__ strings.
169
     *  @return string|string[]      Value found (string or array), or '' if check fails
170
     */
171
    function GETPOST($paramname, $check = 'none', $method = 0, $filter = null, $options = null, $noreplace = 0)
172
    {
173
        global $mysoc, $user, $conf;
174
175
        Debug::addMessage('Deprecated', 'Using GETPOST of functions.lib.php instead of Request library');
176
177
        if (empty($paramname))
178
            return 'BadFirstParameterForGETPOST';
179
        if (empty($check)) {
180
            dol_syslog("Deprecated use of GETPOST, called with 1st param = " . $paramname . " and 2nd param is '', when calling page " . $_SERVER["PHP_SELF"], LOG_WARNING);
181
// Enable this line to know who call the GETPOST with '' $check parameter.
182
//var_dump(debug_backtrace()[0]);
183
        }
184
185
        if (empty($method))
186
            $out = isset($_GET[$paramname]) ? $_GET[$paramname] : (isset($_POST[$paramname]) ? $_POST[$paramname] : '');
187
        elseif ($method == 1)
188
            $out = isset($_GET[$paramname]) ? $_GET[$paramname] : '';
189
        elseif ($method == 2)
190
            $out = isset($_POST[$paramname]) ? $_POST[$paramname] : '';
191
        elseif ($method == 3)
192
            $out = isset($_POST[$paramname]) ? $_POST[$paramname] : (isset($_GET[$paramname]) ? $_GET[$paramname] : '');
193
        elseif ($method == 4)
194
            $out = isset($_POST[$paramname]) ? $_POST[$paramname] : (isset($_GET[$paramname]) ? $_GET[$paramname] : (isset($_COOKIE[$paramname]) ? $_COOKIE[$paramname] : ''));
195
        else
196
            return 'BadThirdParameterForGETPOST';
197
198
        if (empty($method) || $method == 3 || $method == 4) {
199
            $relativepathstring = $_SERVER["PHP_SELF"];
200
// Clean $relativepathstring
201
            if (constant('DOL_URL_ROOT'))
202
                $relativepathstring = preg_replace('/^' . preg_quote(constant('DOL_URL_ROOT'), '/') . '/', '', $relativepathstring);
203
            $relativepathstring = preg_replace('/^\//', '', $relativepathstring);
204
            $relativepathstring = preg_replace('/^custom\//', '', $relativepathstring);
205
//var_dump($relativepathstring);
206
//var_dump($user->default_values);
207
// Code for search criteria persistence.
208
// Retrieve values if restore_lastsearch_values
209
            if (!empty($_GET['restore_lastsearch_values'])) {        // Use $_GET here and not GETPOST
210
                if (!empty($_SESSION['lastsearch_values_' . $relativepathstring])) { // If there is saved values
211
                    $tmp = json_decode($_SESSION['lastsearch_values_' . $relativepathstring], true);
212
                    if (is_array($tmp)) {
213
                        foreach ($tmp as $key => $val) {
214
                            if ($key == $paramname) { // We are on the requested parameter
215
                                $out = $val;
216
                                break;
217
                            }
218
                        }
219
                    }
220
                }
221
// If there is saved contextpage, page or limit
222
                if ($paramname == 'contextpage' && !empty($_SESSION['lastsearch_contextpage_' . $relativepathstring])) {
223
                    $out = $_SESSION['lastsearch_contextpage_' . $relativepathstring];
224
                } elseif ($paramname == 'page' && !empty($_SESSION['lastsearch_page_' . $relativepathstring])) {
225
                    $out = $_SESSION['lastsearch_page_' . $relativepathstring];
226
                } elseif ($paramname == 'limit' && !empty($_SESSION['lastsearch_limit_' . $relativepathstring])) {
227
                    $out = $_SESSION['lastsearch_limit_' . $relativepathstring];
228
                }
229
            }
230
// Else, retreive default values if we are not doing a sort
231
            elseif (!isset($_GET['sortfield'])) { // If we did a click on a field to sort, we do no apply default values. Same if option MAIN_ENABLE_DEFAULT_VALUES is not set
232
                if (!empty($_GET['action']) && $_GET['action'] == 'create' && !isset($_GET[$paramname]) && !isset($_POST[$paramname])) {
233
// Search default value from $object->field
234
                    global $object;
235
                    if (is_object($object) && isset($object->fields[$paramname]['default'])) {
236
                        $out = $object->fields[$paramname]['default'];
237
                    }
238
                }
239
                if (!empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES)) {
240
                    if (!empty($_GET['action']) && $_GET['action'] == 'create' && !isset($_GET[$paramname]) && !isset($_POST[$paramname])) {
241
// Now search in setup to overwrite default values
242
                        if (!empty($user->default_values)) {  // $user->default_values defined from menu 'Setup - Default values'
243
                            if (isset($user->default_values[$relativepathstring]['createform'])) {
244
                                foreach ($user->default_values[$relativepathstring]['createform'] as $defkey => $defval) {
245
                                    $qualified = 0;
246
                                    if ($defkey != '_noquery_') {
247
                                        $tmpqueryarraytohave = explode('&', $defkey);
248
                                        $tmpqueryarraywehave = explode('&', dol_string_nohtmltag($_SERVER['QUERY_STRING']));
249
                                        $foundintru = 0;
250
                                        foreach ($tmpqueryarraytohave as $tmpquerytohave) {
251
                                            if (!in_array($tmpquerytohave, $tmpqueryarraywehave))
252
                                                $foundintru = 1;
253
                                        }
254
                                        if (!$foundintru)
255
                                            $qualified = 1;
256
//var_dump($defkey.'-'.$qualified);
257
                                    } else
258
                                        $qualified = 1;
259
260
                                    if ($qualified) {
261
//var_dump($user->default_values[$relativepathstring][$defkey]['createform']);
262
                                        if (isset($user->default_values[$relativepathstring]['createform'][$defkey][$paramname])) {
263
                                            $out = $user->default_values[$relativepathstring]['createform'][$defkey][$paramname];
264
                                            break;
265
                                        }
266
                                    }
267
                                }
268
                            }
269
                        }
270
                    }
271
// Management of default search_filters and sort order
272
//elseif (preg_match('/list.php$/', $_SERVER["PHP_SELF"]) && ! empty($paramname) && ! isset($_GET[$paramname]) && ! isset($_POST[$paramname]))
273
                    elseif (!empty($paramname) && !isset($_GET[$paramname]) && !isset($_POST[$paramname])) {
274
                        if (!empty($user->default_values)) {  // $user->default_values defined from menu 'Setup - Default values'
275
//var_dump($user->default_values[$relativepathstring]);
276
                            if ($paramname == 'sortfield' || $paramname == 'sortorder') {   // Sorted on which fields ? ASC or DESC ?
277
                                if (isset($user->default_values[$relativepathstring]['sortorder'])) { // Even if paramname is sortfield, data are stored into ['sortorder...']
278
                                    foreach ($user->default_values[$relativepathstring]['sortorder'] as $defkey => $defval) {
279
                                        $qualified = 0;
280
                                        if ($defkey != '_noquery_') {
281
                                            $tmpqueryarraytohave = explode('&', $defkey);
282
                                            $tmpqueryarraywehave = explode('&', dol_string_nohtmltag($_SERVER['QUERY_STRING']));
283
                                            $foundintru = 0;
284
                                            foreach ($tmpqueryarraytohave as $tmpquerytohave) {
285
                                                if (!in_array($tmpquerytohave, $tmpqueryarraywehave))
286
                                                    $foundintru = 1;
287
                                            }
288
                                            if (!$foundintru)
289
                                                $qualified = 1;
290
//var_dump($defkey.'-'.$qualified);
291
                                        } else
292
                                            $qualified = 1;
293
294
                                        if ($qualified) {
295
                                            $forbidden_chars_to_replace = array(" ", "'", "/", "\\", ":", "*", "?", "\"", "<", ">", "|", "[", "]", ";", "=");  // we accept _, -, . and ,
296
                                            foreach ($user->default_values[$relativepathstring]['sortorder'][$defkey] as $key => $val) {
297
                                                if ($out)
298
                                                    $out .= ', ';
299
                                                if ($paramname == 'sortfield') {
300
                                                    $out .= dol_string_nospecial($key, '', $forbidden_chars_to_replace);
301
                                                }
302
                                                if ($paramname == 'sortorder') {
303
                                                    $out .= dol_string_nospecial($val, '', $forbidden_chars_to_replace);
304
                                                }
305
                                            }
306
//break;	// No break for sortfield and sortorder so we can cumulate fields (is it realy usefull ?)
307
                                        }
308
                                    }
309
                                }
310
                            } elseif (isset($user->default_values[$relativepathstring]['filters'])) {
311
                                foreach ($user->default_values[$relativepathstring]['filters'] as $defkey => $defval) { // $defkey is a querystring like 'a=b&c=d', $defval is key of user
312
                                    $qualified = 0;
313
                                    if ($defkey != '_noquery_') {
314
                                        $tmpqueryarraytohave = explode('&', $defkey);
315
                                        $tmpqueryarraywehave = explode('&', dol_string_nohtmltag($_SERVER['QUERY_STRING']));
316
                                        $foundintru = 0;
317
                                        foreach ($tmpqueryarraytohave as $tmpquerytohave) {
318
                                            if (!in_array($tmpquerytohave, $tmpqueryarraywehave))
319
                                                $foundintru = 1;
320
                                        }
321
                                        if (!$foundintru)
322
                                            $qualified = 1;
323
//var_dump($defkey.'-'.$qualified);
324
                                    } else
325
                                        $qualified = 1;
326
327
                                    if ($qualified) {
328
                                        if (isset($_POST['sall']) || isset($_POST['search_all']) || isset($_GET['sall']) || isset($_GET['search_all'])) {
329
// We made a search from quick search menu, do we still use default filter ?
330
                                            if (empty($conf->global->MAIN_DISABLE_DEFAULT_FILTER_FOR_QUICK_SEARCH)) {
331
                                                $forbidden_chars_to_replace = array(" ", "'", "/", "\\", ":", "*", "?", "\"", "<", ">", "|", "[", "]", ";", "=");  // we accept _, -, . and ,
332
                                                $out = dol_string_nospecial($user->default_values[$relativepathstring]['filters'][$defkey][$paramname], '', $forbidden_chars_to_replace);
333
                                            }
334
                                        } else {
335
                                            $forbidden_chars_to_replace = array(" ", "'", "/", "\\", ":", "*", "?", "\"", "<", ">", "|", "[", "]", ";", "=");  // we accept _, -, . and ,
336
                                            $out = dol_string_nospecial($user->default_values[$relativepathstring]['filters'][$defkey][$paramname], '', $forbidden_chars_to_replace);
337
                                        }
338
                                        break;
339
                                    }
340
                                }
341
                            }
342
                        }
343
                    }
344
                }
345
            }
346
        }
347
348
// Substitution variables for GETPOST (used to get final url with variable parameters or final default value with variable paramaters)
349
// Example of variables: __DAY__, __MONTH__, __YEAR__, __MYCOMPANY_COUNTRY_ID__, __USER_ID__, ...
350
// We do this only if var is a GET. If it is a POST, may be we want to post the text with vars as the setup text.
351
        if (!is_array($out) && empty($_POST[$paramname]) && empty($noreplace)) {
352
            $maxloop = 20;
353
            $loopnb = 0;    // Protection against infinite loop
354
            while (preg_match('/__([A-Z0-9]+_?[A-Z0-9]+)__/i', $out, $reg) && ($loopnb < $maxloop)) {    // Detect '__ABCDEF__' as key 'ABCDEF' and '__ABC_DEF__' as key 'ABC_DEF'. Detection is also correct when 2 vars are side by side.
355
                $loopnb++;
356
                $newout = '';
357
358
                if ($reg[1] == 'DAY') {
359
                    $tmp = dol_getdate(dol_now(), true);
360
                    $newout = $tmp['mday'];
361
                } elseif ($reg[1] == 'MONTH') {
362
                    $tmp = dol_getdate(dol_now(), true);
363
                    $newout = $tmp['mon'];
364
                } elseif ($reg[1] == 'YEAR') {
365
                    $tmp = dol_getdate(dol_now(), true);
366
                    $newout = $tmp['year'];
367
                } elseif ($reg[1] == 'PREVIOUS_DAY') {
368
                    $tmp = dol_getdate(dol_now(), true);
369
                    $tmp2 = dol_get_prev_day($tmp['mday'], $tmp['mon'], $tmp['year']);
370
                    $newout = $tmp2['day'];
371
                } elseif ($reg[1] == 'PREVIOUS_MONTH') {
372
                    $tmp = dol_getdate(dol_now(), true);
373
                    $tmp2 = dol_get_prev_month($tmp['mon'], $tmp['year']);
374
                    $newout = $tmp2['month'];
375
                } elseif ($reg[1] == 'PREVIOUS_YEAR') {
376
                    $tmp = dol_getdate(dol_now(), true);
377
                    $newout = ($tmp['year'] - 1);
378
                } elseif ($reg[1] == 'NEXT_DAY') {
379
                    $tmp = dol_getdate(dol_now(), true);
380
                    $tmp2 = dol_get_next_day($tmp['mday'], $tmp['mon'], $tmp['year']);
381
                    $newout = $tmp2['day'];
382
                } elseif ($reg[1] == 'NEXT_MONTH') {
383
                    $tmp = dol_getdate(dol_now(), true);
384
                    $tmp2 = dol_get_next_month($tmp['mon'], $tmp['year']);
385
                    $newout = $tmp2['month'];
386
                } elseif ($reg[1] == 'NEXT_YEAR') {
387
                    $tmp = dol_getdate(dol_now(), true);
388
                    $newout = ($tmp['year'] + 1);
389
                } elseif ($reg[1] == 'MYCOMPANY_COUNTRY_ID' || $reg[1] == 'MYCOUNTRY_ID' || $reg[1] == 'MYCOUNTRYID') {
390
                    $newout = $mysoc->country_id;
391
                } elseif ($reg[1] == 'USER_ID' || $reg[1] == 'USERID') {
392
                    $newout = $user->id;
393
                } elseif ($reg[1] == 'USER_SUPERVISOR_ID' || $reg[1] == 'SUPERVISOR_ID' || $reg[1] == 'SUPERVISORID') {
394
                    $newout = $user->fk_user;
395
                } elseif ($reg[1] == 'ENTITY_ID' || $reg[1] == 'ENTITYID') {
396
                    $newout = $conf->entity;
397
                } else
398
                    $newout = '';     // Key not found, we replace with empty string
399
//var_dump('__'.$reg[1].'__ -> '.$newout);
400
                $out = preg_replace('/__' . preg_quote($reg[1], '/') . '__/', $newout, $out);
401
            }
402
        }
403
404
// Check is done after replacement
405
        switch ($check) {
406
            case 'none':
407
                break;
408
            case 'int':
409
                // Check param is a numeric value (integer but also float or hexadecimal)
410
                if (!is_numeric($out)) {
411
                    $out = '';
412
                }
413
                break;
414
            case 'intcomma':
415
                if (preg_match('/[^0-9,-]+/i', $out))
416
                    $out = '';
417
                break;
418
            case 'alpha':
419
                if (!is_array($out)) {
420
                    $out = trim($out);
421
// '"' is dangerous because param in url can close the href= or src= and add javascript functions.
422
// '../' is dangerous because it allows dir transversals
423
                    if (preg_match('/"/', $out))
424
                        $out = '';
425
                    else if (preg_match('/\.\.\//', $out))
426
                        $out = '';
427
                }
428
                break;
429
            case 'san_alpha':
430
                $out = filter_var($out, FILTER_SANITIZE_STRING);
431
                break;
432
            case 'aZ':
433
                if (!is_array($out)) {
434
                    $out = trim($out);
435
                    if (preg_match('/[^a-z]+/i', $out))
436
                        $out = '';
437
                }
438
                break;
439
            case 'aZ09':
440
                if (!is_array($out)) {
441
                    $out = trim($out);
442
                    if (preg_match('/[^a-z0-9_\-\.]+/i', $out))
443
                        $out = '';
444
                }
445
                break;
446
            case 'aZ09comma':  // great to sanitize sortfield or sortorder params that can be t.abc,t.def_gh
447
                if (!is_array($out)) {
448
                    $out = trim($out);
449
                    if (preg_match('/[^a-z0-9_\-\.,]+/i', $out))
450
                        $out = '';
451
                }
452
                break;
453
            case 'array':
454
                if (!is_array($out) || empty($out))
455
                    $out = array();
456
                break;
457
            case 'nohtml':  // Recommended for most scalar parameters
458
                $out = dol_string_nohtmltag($out, 0);
459
                break;
460
            case 'alphanohtml': // Recommended for search parameters
461
                if (!is_array($out)) {
462
                    $out = trim($out);
463
// '"' is dangerous because param in url can close the href= or src= and add javascript functions.
464
// '../' is dangerous because it allows dir transversals
465
                    if (preg_match('/"/', $out))
466
                        $out = '';
467
                    else if (preg_match('/\.\.\//', $out))
468
                        $out = '';
469
                    $out = dol_string_nohtmltag($out);
470
                }
471
                break;
472
            case 'custom':
473
                if (empty($filter))
474
                    return 'BadFourthParameterForGETPOST';
475
                $out = filter_var($out, $filter, $options);
476
                break;
477
        }
478
479
// Code for search criteria persistence.
480
// Save data into session if key start with 'search_' or is 'smonth', 'syear', 'month', 'year'
481
        if (empty($method) || $method == 3 || $method == 4) {
482
            if (preg_match('/^search_/', $paramname) || in_array($paramname, array('sortorder', 'sortfield'))) {
483
//var_dump($paramname.' - '.$out.' '.$user->default_values[$relativepathstring]['filters'][$paramname]);
484
// We save search key only if $out not empty that means:
485
// - posted value not empty, or
486
// - if posted value is empty and a default value exists that is not empty (it means we did a filter to an empty value when default was not).
487
488
                if ($out != '') {  // $out = '0' or 'abc', it is a search criteria to keep
489
                    $user->lastsearch_values_tmp[$relativepathstring][$paramname] = $out;
490
                }
491
            }
492
        }
493
494
        return $out;
495
    }
496
}
497