Passed
Push — master ( eea0d4...9faae1 )
by Tony
09:44
created

Clean::alphaDash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Clean.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2019 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace LibreNMS\Util;
27
28
use HTMLPurifier;
29
use HTMLPurifier_Config;
30
use LibreNMS\Config;
31
32
class Clean
33
{
34
    /**
35
     * Sanitize file name by removing all invalid characters.
36
     * Does not make the string safe for javascript or sql!
37
     *
38
     * @param string $file
39
     * @return string|string[]|null
40
     */
41
    public static function fileName($file)
42
    {
43
        return preg_replace('/[^a-zA-Z0-9\-._]/', '', $file);
44
    }
45
46
    /**
47
     * Sanitize string to only contain alpha, numeric, dashes, and underscores
48
     *
49
     * @param string $string
50
     * @return string
51
     */
52
    public static function alphaDash($string)
53
    {
54
        return preg_replace('/[^a-zA-Z0-9\-_]/', '', $string);
55
    }
56
57
    /**
58
     * Sanitize string to only contain alpha, numeric, dashes, underscores, and spaces
59
     *
60
     * @param string $string
61
     * @return string
62
     */
63
    public static function alphaDashSpace($string)
64
    {
65
        return preg_replace('/[^a-zA-Z0-9\-_ ]/', '', $string);
66
    }
67
68
    /**
69
     * Clean a string for display in an html page.
70
     * For use in non-blade pages
71
     *
72
     * @param $value
73
     * @param array $purifier_config (key, value pair)
74
     * @return string
75
     */
76
    public static function html($value, $purifier_config = [])
77
    {
78
        /** @var HTMLPurifier $purifier */
79
        static $purifier;
80
81
        // If $purifier_config is non-empty then we don't want
82
        // to convert html tags and allow these to be controlled
83
        // by purifier instead.
84
        if (empty($purifier_config)) {
85
            $value = htmlentities($value);
86
        }
87
88
        if (!isset($purifier)) {
89
            // initialize HTML Purifier here since this is the only user
90
            $p_config = HTMLPurifier_Config::createDefault();
91
            $p_config->set('Cache.SerializerPath', Config::get('temp_dir', '/tmp'));
92
            foreach ($purifier_config as $k => $v) {
93
                $p_config->set($k, $v);
94
            }
95
            $purifier = new HTMLPurifier($p_config);
96
        }
97
98
        return $purifier->purify(stripslashes($value));
99
    }
100
}
101