Security   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 122
rs 10
wmc 18

8 Methods

Rating   Name   Duplication   Size   Complexity  
A escapeQuotes() 0 3 1
A secureHtml() 0 3 2
A __construct() 0 11 1
A strip_php_tags() 0 9 3
A var_export54() 0 3 1
A strip_tags() 0 16 4
A password_hash() 0 7 4
A simpleHash() 0 7 2
1
<?php
2
3
namespace Ffcms\Core\Helper;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Any;
7
use Ffcms\Core\Helper\Type\Arr;
8
use Ffcms\Core\Helper\Type\Obj;
9
use Ffcms\Core\Helper\Type\Str;
10
11
/**
12
 * Class Security. Basic framework security entry point
13
 * @package Ffcms\Core\Helper
14
 */
15
class Security
16
{
17
    protected $purifier;
18
19
    /**
20
     * Security constructor. Construct html purifier instance.
21
     */
22
    public function __construct()
23
    {
24
        $config = \HTMLPurifier_Config::createDefault();
25
        $config->set('Cache.SerializerPath', root . '/Private/Cache/HTMLPurifier/');
0 ignored issues
show
Bug introduced by
The constant Ffcms\Core\Helper\root was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
26
        $config->set('HTML.Allowed', 'p,b,strong,em,a[href],i,span,ul,ol,li,blockquote,h2,h3,pre,code,img[src|alt|width|height]');
27
        //$config->set('URI.Base', 'http://www.example.com');
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
        //$config->set('URI.MakeAbsolute', true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
        $config->set('AutoFormat.AutoParagraph', false);
30
        $config->set('HTML.TargetBlank', true);
31
32
        $this->purifier = new \HTMLPurifier($config);
33
    }
34
35
    /**
36
     * Secure html code
37
     * @param string|array $data
38
     * @return string
39
     */
40
    public function secureHtml($data): ?string
41
    {
42
        return (Any::isArray($data) ? $this->purifier->purifyArray($data) : $this->purifier->purify($data));
0 ignored issues
show
Bug Best Practice introduced by
The expression return Ffcms\Core\Helper...purifier->purify($data) could return the type string[] which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
It seems like $data can also be of type array; however, parameter $html of HTMLPurifier::purify() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        return (Any::isArray($data) ? $this->purifier->purifyArray($data) : $this->purifier->purify(/** @scrutinizer ignore-type */ $data));
Loading history...
Bug introduced by
It seems like $data can also be of type string; however, parameter $array_of_html of HTMLPurifier::purifyArray() does only seem to accept string[], maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        return (Any::isArray($data) ? $this->purifier->purifyArray(/** @scrutinizer ignore-type */ $data) : $this->purifier->purify($data));
Loading history...
43
    }
44
45
    /**
46
     * String html tags and escape quotes
47
     * @param string|array $html
48
     * @param boolean $escapeQuotes
49
     * @return string|array|null
50
     */
51
    public function strip_tags($html, $escapeQuotes = true)
52
    {
53
        // recursive usage
54
        if (Any::isArray($html)) {
55
            foreach ($html as $key=>$value) {
56
                $html[$key] = $this->strip_tags($value, $escapeQuotes);
57
            }
58
            return $html;
59
        }
60
61
        $text = strip_tags($html);
0 ignored issues
show
Bug introduced by
It seems like $html can also be of type array; however, parameter $str of strip_tags() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        $text = strip_tags(/** @scrutinizer ignore-type */ $html);
Loading history...
62
        if ($escapeQuotes) {
63
            $text = $this->escapeQuotes($text);
64
        }
65
66
        return $text;
67
    }
68
69
    /**
70
     * Strip php tags and notations in string.
71
     * @param array|string $data
72
     * @return array|null|string
73
     */
74
    public function strip_php_tags($data)
75
    {
76
        if (is_array($data)) {
77
            foreach ($data as $key=>$value) {
78
                $data[$key] = $this->strip_php_tags($value);
79
            }
80
            return $data;
81
        }
82
        return addslashes(htmlspecialchars(strip_tags($data)));
83
    }
84
85
    /**
86
     * Alternative var_export function for php >= 5.4 syntax
87
     * @deprecated
88
     * @param $var
89
     * @param null $indent
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $indent is correct as it would always require null to be passed?
Loading history...
90
     * @return mixed|string
91
     */
92
    public function var_export54($var, $indent = null, $guessTypes = false)
93
    {
94
        return Arr::exportVar($var, $indent, $guessTypes);
95
    }
96
97
    /**
98
     * Escape quotes
99
     * @param string $html
100
     * @return string
101
     */
102
    public function escapeQuotes($html)
103
    {
104
        return Str::ireplace(["\"", "'", "&quot;"], '', $html);
105
    }
106
107
    /**
108
     * Crypt password secure with Blow fish crypt algo (defined in salt)
109
     * Blow fish crypt example: crypt('somedata', '$2a$07$usesomesillystringfor$'), where $2a$07$ - definition of algo,
110
     * usesomesillystringfor - is salt (must be 21 or more chars), $ - end caret. Output: $2a$07$usesomesillystringfor.sUeCOxyFvckc3xgq1Kzqq90gLrrIVjq
111
     * @param string $password
112
     * @param string|null $salt
113
     * @return string
114
     * @deprecated
115
     */
116
    public static function password_hash($password, $salt = null)
117
    {
118
        if ($salt === null || !Any::isStr($salt) || Str::length($salt) < 1) {
119
            $salt = App::$Properties->get('passwordSalt');
120
        }
121
122
        return crypt($password, $salt);
123
    }
124
125
    /**
126
     * Generate simple hash of 8 chars (32bit) for string. This method is NOT SECURE for crypt reason!
127
     * @param string $string
128
     * @return string|null
129
     */
130
    public static function simpleHash($string): ?string
131
    {
132
        if (!Any::isLine($string)) {
133
            return null;
134
        }
135
136
        return dechex(crc32($string));
137
    }
138
}
139