GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — dev ( 139983...49690f )
by w3l
02:01
created

Strings::decrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * Strings.php
4
 */
5
namespace w3l\Holt45;
6
7
/**
8
 * Handle strings(convert, encode, replace, etc).
9
 */
10
trait Strings
11
{
12
    /**
13
     * Encrypt string
14
     *
15
     * NOTICE: the code in general, this method in particular, comes with absolutely no warranty. If security is important for you, then 
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
16
     * use a purpose built package. https://github.com/defuse/php-encryption seems like a good candidate.
17
     * @deprecated Do not trust a two-line encryption-method.
18
     *
19
     * @param string $string String to encrypt
20
     * @param string $key Key to encrypt/decrypt.
21
     * @return string Encrypted string
22
     */
23
    public static function encrypt($string, $key) {
24
25
        if (!extension_loaded('mcrypt')) {
26
            throw new Exception('mcrypt not loaded');
27
        }
28
29
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_DEV_URANDOM);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $iv. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
30
        
31
        $encryptedString = $iv.mcrypt_encrypt(MCRYPT_RIJNDAEL_256, hash("sha256", $key, true), $string, MCRYPT_MODE_CBC, $iv);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
32
     
33
        return base64_encode($encryptedString);
34
    }
35
    
36
    /**
37
     * Decrypt string
38
     *
39
     * NOTICE: the code in general, this method in particular, comes with absolutely no warranty. If security is important for you, then 
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
40
     * use a purpose built package. https://github.com/defuse/php-encryption seems like a good candidate.
41
     * @deprecated Do not trust a two-line decryption-method.
42
     *
43
     * @param string $string String to decrypt
44
     * @param string $key Key to encrypt/decrypt.
45
     * @return string Decrypted string
46
     */
47
    public static function decrypt($string, $key) {
48
        $encryptedString = base64_decode($string);
49
       
50
        $iv = substr($encryptedString, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $iv. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
51
        
52
        $decryptedString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, hash("sha256", $key, true), substr($encryptedString, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB)), MCRYPT_MODE_CBC, $iv);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 197 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
53
        
54
        return $decryptedString;
55
    }
56
57
    /**
58
     * Obfuscate string (url-safe and somewhat hard to guess).
59
     *
60
     * @param string $input The text that should be obfuscated
61
     * @return string Obfuscated string
62
     */
63
    public static function obfuscateString($input)
64
    {
65
        return bin2hex(base64_encode(strrev($input)));
66
    }
67
68
    /**
69
     * Deobfuscate string
70
     *
71
     * @param string $input Obfuscated string
72
     * @return string Deobfuscated string
73
     */
74
    public static function deobfuscateString($input)
75
    {
76
        return strrev(base64_decode(hex2bin($input)));
77
    }
78
79
    /**
80
     * Convert <textarea> to [textarea].
81
     *
82
     * @param string $html
83
     * @return string
84
     */
85
    public static function textareaEncode($html)
86
    {
87
        return preg_replace("/<textarea(.*?)>(.*?)<\/textarea>/is", "[textarea$1]$2[/textarea]", $html);
88
    }
89
90
    /**
91
     * Convert [textarea] to <textarea>.
92
     *
93
     * @param string $html
94
     * @return string
95
     */
96
    public static function textareaDecode($html)
97
    {
98
        return preg_replace("/\[textarea(.*?)\](.*?)\[\/textarea\]/is", "<textarea$1>$2</textarea>", $html);
99
    }
100
101
    /**
102
     * To replace "Hallo [@var] world" with $value.
103
     *
104
     * Example:
105
     * ```php
106
     * replace_string($string, array("val1" => "foo", "val2" => "bar"))
107
     * ```
108
     *
109
     * @param string $langString String containing placeholder.
110
     * @param array $dynamicContent key->value array.
111
     * @return string String with placeholder replaced.
112
     */
113
    public static function replaceString($langString, $dynamicContent = array())
114
    {
115
        foreach ($dynamicContent as $k => $v) {
116
            $langString = str_replace("[@".$k."]", $v, $langString);
117
        }
118
        return $langString;
119
    }
120
121
    /**
122
     * Creates rainbow-colored text.
123
     *
124
     * @uses Holt45::colorBlend()
125
     * @uses Holt45::rgbhex()
126
     *
127
     * @param string $text Text wanted coloured.
128
     * @return string String with span-tags with color.
129
     */
130
    public static function rainbowText($text)
131
    {
132
        $colorsBase = array(
133
        array(255, 0, 0),
134
        array(255, 102, 0),
135
        array(255, 238, 0),
136
        array(0, 255, 0),
137
        array(0, 153, 255),
138
        array(68, 0, 255),
139
        array(153, 0, 255)
140
        );
141
142
        $colorsBuild = array();
143
144
        $strlenText = strlen($text);
145
146
        if ($strlenText > 7) {
147
            while (count($colorsBuild) < $strlenText) {
148
                for ($i = 0, $size = count($colorsBase); $i < $size; $i++) {
149
150
                    $colorsBuild[] = $colorsBase[$i];
151
152
                    if (count($colorsBuild) >= $strlenText) {
153
                        continue 2;
154
                    }
155
156
                    if ($i < count($colorsBase)-1) {
157
158
                        $colorsBuild[] = self::colorBlend($colorsBase[$i], $colorsBase[$i+1]);
159
160
                        if (count($colorsBuild) >= $strlenText) {
161
                            continue 2;
162
                        }
163
                    }
164
                }
165
                $colorsBase = $colorsBuild;
166
                $colorsBuild = array();
167
            }
168
        } elseif ($strlenText <= 7) {
169
            $colorsBuild = $colorsBase;
170
        }
171
172
        $arrayText = str_split($text);
173
        $returnText = "";
174
        for ($i = 0, $size = count($arrayText); $i < $size; $i++) {
175
            $returnText .= '<span style="color: #'.self::rgbhex($colorsBuild[$i]).';">'.$arrayText[$i].'</span>';
176
        }
177
        return $returnText;
178
    }
179
    
180
    /**
181
     * Get the symbol from a list of keyboard-keys...
182
     *
183
     * @used-by Holt45::kbdShortcut()
184
     *
185
     * @param string $inputKey Text
186
     * @param string $inputOperatingSystem default|auto|win|mac|linux
187
     * @return null|string HTML Entity (decimal)
188
     */
189
    public static function kbdSymbol($inputKey, $inputOperatingSystem = "default")
190
    {
191
        $inputKey = mb_strtolower($inputKey);
192
        
193
        if ($inputOperatingSystem == "auto") {
194
            
195
            $inputOperatingSystem = "default";
196
            
197
            $getClientOperatingSystem = self::getClientOperatingSystem();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $getClientOperatingSystem exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
198
            
199
            if ($getClientOperatingSystem == "linux" ||
200
                $getClientOperatingSystem == "mac" ||
201
                $getClientOperatingSystem == "windows") {
202
                   $inputOperatingSystem = $getClientOperatingSystem;
203
            }
204
        }
205
        
206
        $arrayConvert = array(
207
        "return" => "enter",
208
        "control" => "ctrl",
209
        "escape" => "esc",
210
        "caps lock" => "caps-lock",
211
        "page up" => "page-up",
212
        "page down" => "page-down",
213
        "arrow left" => "arrow-left",
214
        "left" => "arrow-left",
215
        "arrow up" => "arrow-up",
216
        "up" => "arrow-up",
217
        "arrow right" => "arrow-right",
218
        "right" => "arrow-right",
219
        "arrow down" => "arrow-down",
220
        "down" => "arrow-down"
221
        );
222
        
223
        /* Convert input */
224
        if (array_key_exists($inputKey, $arrayConvert)) {
225
            $inputKey = $arrayConvert[$inputKey];
226
        }
227
228
        $arrayKeySymbols = array(
229
        "shift" => array("default" => "&#8679;"),
230
        "opt" => array("default" => "&#8997;"),
231
        "enter" => array("default" => "&#9166;", "mac" => "&#8996;"),
232
        "alt" => array("default" => "&#9095;", "mac" => "&#8997;"),
233
        "delete" => array("default" => "&#9003;"),
234
        "ctrl" => array("default" => "&#10034;", "windows" => "&#10034;", "linux" => "&#9096;", "mac" => "&#00094;"),
235
        "esc" => array("default" => "&#9099;"),
236
        "command" => array("default" => "&#8984;"),
237
        "tab" => array("default" => "&#8633;", "mac" => "&#8677;"),
238
        "caps-lock" => array("default" => "&#65;", "mac" => "&#8682;"),
239
        "page-up" => array("default" => "&#9650;", "mac" => "&#8670;"),
240
        "page-down" => array("default" => "&#9660;", "mac" => "&#8671;"),
241
        "arrow-left" => array("default" => "&#8592;"),
242
        "arrow-up" => array("default" => "&#8593;"),
243
        "arrow-right" => array("default" => "&#8594;"),
244
        "arrow-down" => array("default" => "&#8595;"),
245
        // Sun
246
        "compose" => array("default" => "&#9092;"),
247
        "meta" => array("default" => "&#9670")
248
        );
249
        
250
        if (array_key_exists($inputKey, $arrayKeySymbols)) {
251
            
252
            return ((array_key_exists($inputOperatingSystem, $arrayKeySymbols[$inputKey])) ?
253
                                      $arrayKeySymbols[$inputKey][$inputOperatingSystem] :
254
                                      $arrayKeySymbols[$inputKey]["default"]);
255
        }
256
        
257
        return null;
258
    }
259
260
    /**
261
     * Show fancy buttons for keyboard-shortcuts.
262
     *
263
     * @uses Holt45::kbdSymbol()
264
     *
265
     * @param array $inputArrayKeys
266
     * @param string $inputOperatingSystem
267
     * @param string $inputKbdClass
268
     * @param string $inputKbdSymbolClass
269
     * @param string $inputJoinGlue Glue
270
     * @return string String of html
271
     */
272
    public static function kbdShortcut(
273
        $inputArrayKeys,
274
        $inputOperatingSystem = "default",
275
        $inputKbdClass = "holt45-kbd",
276
        $inputKbdSymbolClass = "holt45-kbd__symbol",
277
        $inputJoinGlue = " + "
278
    ) {
279
        $returnArray = array();
280
281
        foreach ($inputArrayKeys as $key) {
282
            
283
            $kbdSymbol = self::kbdSymbol($key, $inputOperatingSystem);
284
            
285
            $kbdSymbolHtml = "";
286
            
287
            if ($kbdSymbol !== null) {
288
                $kbdSymbolHtml = '<span class="'.$inputKbdSymbolClass.'">'.$kbdSymbol.'</span>';
289
            }
290
            
291
            $returnArray[] = '<kbd class="'.$inputKbdClass.'">'.$kbdSymbolHtml.$key.'</kbd>';
292
            
293
        }
294
295
        return implode($inputJoinGlue, $returnArray);
296
    }
297
298
    /**
299
     * Attempting to keep CSS on one line with scoped style.
300
     *
301
     * NOTICE: This is a rough estimate, font type, design, etc affects the results.
302
     *
303
     * @param string $text
304
     * @param string $cssSelector
305
     * @param int $fontSizePx
306
     * @param int $minPageWidthPx
307
     * @return null|string Scoped style
308
     */
309
    public static function cssOneLineText($text, $cssSelector = "h1", $fontSizePx = 36, $minPageWidthPx = 320)
310
    {
311
        $countText = strlen($text)+1;
312
313
        $fontWidth = ($fontSizePx / 2);
314
315
        if ($minPageWidthPx < $countText) {
316
            $minPageWidthPx = $countText;
317
        }
318
        if (($countText * $fontWidth) > $minPageWidthPx) {
319
320
            $cssNewFontSizePx = round(($minPageWidthPx / $countText) * 2, 2);
321
            $cssNewFontSizeVw = round((100/$countText) * 2, 2);
322
323
            $cssBreakpointPx = round($countText * $fontWidth);
324
325
            return '<style scoped>@media (max-width: '.$cssBreakpointPx.'px) { '.$cssSelector.' { font-size: '.$cssNewFontSizePx.'px; font-size: '.$cssNewFontSizeVw.'vw; } }</style>';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 183 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
326
        }
327
        return null;
328
    }
329
}
330