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
Pull Request — master (#42)
by w3l
03:56 queued 01:38
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
     * @throws Exception if extension mcrypt is not loaded.
20
     *
21
     * @param string $string String to encrypt
22
     * @param string $key Key to encrypt/decrypt.
23
     * @return string Encrypted string
24
     */
25
    public static function encrypt($string, $key) {
26
27
        if (!extension_loaded('mcrypt')) {
28
            throw new Exception('mcrypt not loaded');
29
        }
30
31
        $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...
32
        
33
        $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...
34
     
35
        return base64_encode($encryptedString);
36
    }
37
    
38
    /**
39
     * Decrypt string
40
     *
41
     * 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...
42
     * use a purpose built package. https://github.com/defuse/php-encryption seems like a good candidate.
43
     * @deprecated Do not trust a two-line decryption-method.
44
     *
45
     * @param string $string String to decrypt
46
     * @param string $key Key to encrypt/decrypt.
47
     * @return string Decrypted string
48
     */
49
    public static function decrypt($string, $key) {
50
        $encryptedString = base64_decode($string);
51
       
52
        $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...
53
        
54
        $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...
55
        
56
        return $decryptedString;
57
    }
58
59
    /**
60
     * Obfuscate string (url-safe and somewhat hard to guess).
61
     *
62
     * @param string $input The text that should be obfuscated
63
     * @return string Obfuscated string
64
     */
65
    public static function obfuscateString($input)
66
    {
67
        return bin2hex(base64_encode(strrev($input)));
68
    }
69
70
    /**
71
     * Deobfuscate string
72
     *
73
     * @param string $input Obfuscated string
74
     * @return string Deobfuscated string
75
     */
76
    public static function deobfuscateString($input)
77
    {
78
        return strrev(base64_decode(hex2bin($input)));
79
    }
80
81
    /**
82
     * Convert <textarea> to [textarea].
83
     *
84
     * @param string $html
85
     * @return string
86
     */
87
    public static function textareaEncode($html)
88
    {
89
        return preg_replace("/<textarea(.*?)>(.*?)<\/textarea>/is", "[textarea$1]$2[/textarea]", $html);
90
    }
91
92
    /**
93
     * Convert [textarea] to <textarea>.
94
     *
95
     * @param string $html
96
     * @return string
97
     */
98
    public static function textareaDecode($html)
99
    {
100
        return preg_replace("/\[textarea(.*?)\](.*?)\[\/textarea\]/is", "<textarea$1>$2</textarea>", $html);
101
    }
102
103
    /**
104
     * To replace "Hallo [@var] world" with $value.
105
     *
106
     * Example:
107
     * ```php
108
     * replace_string($string, array("val1" => "foo", "val2" => "bar"))
109
     * ```
110
     *
111
     * @param string $langString String containing placeholder.
112
     * @param array $dynamicContent key->value array.
113
     * @return string String with placeholder replaced.
114
     */
115
    public static function replaceString($langString, $dynamicContent = array())
116
    {
117
        foreach ($dynamicContent as $k => $v) {
118
            $langString = str_replace("[@".$k."]", $v, $langString);
119
        }
120
        return $langString;
121
    }
122
123
    /**
124
     * Creates rainbow-colored text.
125
     *
126
     * @uses Holt45::colorBlend()
127
     * @uses Holt45::rgbhex()
128
     *
129
     * @param string $text Text wanted coloured.
130
     * @return string String with span-tags with color.
131
     */
132
    public static function rainbowText($text)
133
    {
134
        $colorsBase = array(
135
        array(255, 0, 0),
136
        array(255, 102, 0),
137
        array(255, 238, 0),
138
        array(0, 255, 0),
139
        array(0, 153, 255),
140
        array(68, 0, 255),
141
        array(153, 0, 255)
142
        );
143
144
        $colorsBuild = array();
145
146
        $strlenText = strlen($text);
147
148
        if ($strlenText > 7) {
149
            while (count($colorsBuild) < $strlenText) {
150
                for ($i = 0, $size = count($colorsBase); $i < $size; $i++) {
151
152
                    $colorsBuild[] = $colorsBase[$i];
153
154
                    if (count($colorsBuild) >= $strlenText) {
155
                        continue 2;
156
                    }
157
158
                    if ($i < count($colorsBase)-1) {
159
160
                        $colorsBuild[] = self::colorBlend($colorsBase[$i], $colorsBase[$i+1]);
161
162
                        if (count($colorsBuild) >= $strlenText) {
163
                            continue 2;
164
                        }
165
                    }
166
                }
167
                $colorsBase = $colorsBuild;
168
                $colorsBuild = array();
169
            }
170
        } elseif ($strlenText <= 7) {
171
            $colorsBuild = $colorsBase;
172
        }
173
174
        $arrayText = str_split($text);
175
        $returnText = "";
176
        for ($i = 0, $size = count($arrayText); $i < $size; $i++) {
177
            $returnText .= '<span style="color: #'.self::rgbhex($colorsBuild[$i]).';">'.$arrayText[$i].'</span>';
178
        }
179
        return $returnText;
180
    }
181
    
182
    /**
183
     * Get the symbol from a list of keyboard-keys...
184
     *
185
     * @used-by Holt45::kbdShortcut()
186
     *
187
     * @param string $inputKey Text
188
     * @param string $inputOperatingSystem default|auto|win|mac|linux
189
     * @return null|string HTML Entity (decimal)
190
     */
191
    public static function kbdSymbol($inputKey, $inputOperatingSystem = "default")
192
    {
193
        $inputKey = mb_strtolower($inputKey);
194
        
195
        if ($inputOperatingSystem == "auto") {
196
            
197
            $inputOperatingSystem = "default";
198
            
199
            $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...
200
            
201
            if ($getClientOperatingSystem == "linux" ||
202
                $getClientOperatingSystem == "mac" ||
203
                $getClientOperatingSystem == "windows") {
204
                   $inputOperatingSystem = $getClientOperatingSystem;
205
            }
206
        }
207
        
208
        $arrayConvert = array(
209
        "return" => "enter",
210
        "control" => "ctrl",
211
        "escape" => "esc",
212
        "caps lock" => "caps-lock",
213
        "page up" => "page-up",
214
        "page down" => "page-down",
215
        "arrow left" => "arrow-left",
216
        "left" => "arrow-left",
217
        "arrow up" => "arrow-up",
218
        "up" => "arrow-up",
219
        "arrow right" => "arrow-right",
220
        "right" => "arrow-right",
221
        "arrow down" => "arrow-down",
222
        "down" => "arrow-down"
223
        );
224
        
225
        /* Convert input */
226
        if (array_key_exists($inputKey, $arrayConvert)) {
227
            $inputKey = $arrayConvert[$inputKey];
228
        }
229
230
        $arrayKeySymbols = array(
231
        "shift" => array("default" => "&#8679;"),
232
        "opt" => array("default" => "&#8997;"),
233
        "enter" => array("default" => "&#9166;", "mac" => "&#8996;"),
234
        "alt" => array("default" => "&#9095;", "mac" => "&#8997;"),
235
        "delete" => array("default" => "&#9003;"),
236
        "ctrl" => array("default" => "&#10034;", "windows" => "&#10034;", "linux" => "&#9096;", "mac" => "&#00094;"),
237
        "esc" => array("default" => "&#9099;"),
238
        "command" => array("default" => "&#8984;"),
239
        "tab" => array("default" => "&#8633;", "mac" => "&#8677;"),
240
        "caps-lock" => array("default" => "&#65;", "mac" => "&#8682;"),
241
        "page-up" => array("default" => "&#9650;", "mac" => "&#8670;"),
242
        "page-down" => array("default" => "&#9660;", "mac" => "&#8671;"),
243
        "arrow-left" => array("default" => "&#8592;"),
244
        "arrow-up" => array("default" => "&#8593;"),
245
        "arrow-right" => array("default" => "&#8594;"),
246
        "arrow-down" => array("default" => "&#8595;"),
247
        // Sun
248
        "compose" => array("default" => "&#9092;"),
249
        "meta" => array("default" => "&#9670")
250
        );
251
        
252
        if (array_key_exists($inputKey, $arrayKeySymbols)) {
253
            
254
            return ((array_key_exists($inputOperatingSystem, $arrayKeySymbols[$inputKey])) ?
255
                                      $arrayKeySymbols[$inputKey][$inputOperatingSystem] :
256
                                      $arrayKeySymbols[$inputKey]["default"]);
257
        }
258
        
259
        return null;
260
    }
261
262
    /**
263
     * Show fancy buttons for keyboard-shortcuts.
264
     *
265
     * @uses Holt45::kbdSymbol()
266
     *
267
     * @param array $inputArrayKeys
268
     * @param string $inputOperatingSystem
269
     * @param string $inputKbdClass
270
     * @param string $inputKbdSymbolClass
271
     * @param string $inputJoinGlue Glue
272
     * @return string String of html
273
     */
274
    public static function kbdShortcut(
275
        $inputArrayKeys,
276
        $inputOperatingSystem = "default",
277
        $inputKbdClass = "holt45-kbd",
278
        $inputKbdSymbolClass = "holt45-kbd__symbol",
279
        $inputJoinGlue = " + "
280
    ) {
281
        $returnArray = array();
282
283
        foreach ($inputArrayKeys as $key) {
284
            
285
            $kbdSymbol = self::kbdSymbol($key, $inputOperatingSystem);
286
            
287
            $kbdSymbolHtml = "";
288
            
289
            if ($kbdSymbol !== null) {
290
                $kbdSymbolHtml = '<span class="'.$inputKbdSymbolClass.'">'.$kbdSymbol.'</span>';
291
            }
292
            
293
            $returnArray[] = '<kbd class="'.$inputKbdClass.'">'.$kbdSymbolHtml.$key.'</kbd>';
294
            
295
        }
296
297
        return implode($inputJoinGlue, $returnArray);
298
    }
299
300
    /**
301
     * Attempting to keep CSS on one line with scoped style.
302
     *
303
     * NOTICE: This is a rough estimate, font type, design, etc affects the results.
304
     *
305
     * @param string $text
306
     * @param string $cssSelector
307
     * @param int $fontSizePx
308
     * @param int $minPageWidthPx
309
     * @return null|string Scoped style
310
     */
311
    public static function cssOneLineText($text, $cssSelector = "h1", $fontSizePx = 36, $minPageWidthPx = 320)
312
    {
313
        $countText = strlen($text)+1;
314
315
        $fontWidth = ($fontSizePx / 2);
316
317
        if ($minPageWidthPx < $countText) {
318
            $minPageWidthPx = $countText;
319
        }
320
        if (($countText * $fontWidth) > $minPageWidthPx) {
321
322
            $cssNewFontSizePx = round(($minPageWidthPx / $countText) * 2, 2);
323
            $cssNewFontSizeVw = round((100/$countText) * 2, 2);
324
325
            $cssBreakpointPx = round($countText * $fontWidth);
326
327
            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...
328
        }
329
        return null;
330
    }
331
}
332