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:55 queued 02:02
created

Strings::decrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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