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:40 queued 01:46
created

Strings   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 340
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 18
Bugs 1 Features 0
Metric Value
wmc 32
c 18
b 1
f 0
lcom 0
cbo 0
dl 0
loc 340
rs 9.6

11 Methods

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