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 ( 8e874c...1704c6 )
by w3l
01:43
created

Strings::encrypt()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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