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 — master ( 74479f...058a43 )
by w3l
03:44 queued 01:50
created

Strings::decrypt()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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