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 ( ee7491...f5f2be )
by w3l
05:16 queued 03:28
created

Strings::kbdShortcut()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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