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