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
|
|
|
$colorsBuild[] = $colorsBase[$i]; |
189
|
|
|
|
190
|
|
|
if (count($colorsBuild) >= $strlenText) { |
191
|
|
|
continue 2; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($i < count($colorsBase)-1) { |
195
|
|
|
$colorsBuild[] = self::colorBlend($colorsBase[$i], $colorsBase[$i+1]); |
196
|
|
|
|
197
|
|
|
if (count($colorsBuild) >= $strlenText) { |
198
|
|
|
continue 2; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
$colorsBase = $colorsBuild; |
203
|
|
|
$colorsBuild = array(); |
204
|
|
|
} |
205
|
|
|
} elseif ($strlenText <= 7) { |
206
|
|
|
$colorsBuild = $colorsBase; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$arrayText = str_split($text); |
210
|
|
|
$returnText = ""; |
211
|
|
|
for ($i = 0, $size = count($arrayText); $i < $size; $i++) { |
212
|
|
|
$returnText .= '<span style="color: #'.self::rgbhex($colorsBuild[$i]).';">'.$arrayText[$i].'</span>'; |
213
|
|
|
} |
214
|
|
|
return $returnText; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the symbol from a list of keyboard-keys... |
219
|
|
|
* |
220
|
|
|
* @used-by Holt45::kbdShortcut() |
221
|
|
|
* |
222
|
|
|
* @param string $inputKey Text |
223
|
|
|
* @param string $inputOperatingSystem default|auto|win|mac|linux |
224
|
|
|
* @return null|string HTML Entity (decimal) |
225
|
|
|
*/ |
226
|
|
|
public static function kbdSymbol($inputKey, $inputOperatingSystem = "default") |
227
|
|
|
{ |
228
|
|
|
$inputKey = mb_strtolower($inputKey); |
229
|
|
|
|
230
|
|
|
if ($inputOperatingSystem == "auto") { |
231
|
|
|
$inputOperatingSystem = "default"; |
232
|
|
|
|
233
|
|
|
$getClientOS = self::getClientOperatingSystem(); |
234
|
|
|
|
235
|
|
|
if ($getClientOS == "linux" || |
236
|
|
|
$getClientOS == "mac" || |
237
|
|
|
$getClientOS == "windows") { |
238
|
|
|
$inputOperatingSystem = $getClientOS; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$arrayConvert = array( |
243
|
|
|
"return" => "enter", |
244
|
|
|
"control" => "ctrl", |
245
|
|
|
"escape" => "esc", |
246
|
|
|
"caps lock" => "caps-lock", |
247
|
|
|
"page up" => "page-up", |
248
|
|
|
"page down" => "page-down", |
249
|
|
|
"arrow left" => "arrow-left", |
250
|
|
|
"left" => "arrow-left", |
251
|
|
|
"arrow up" => "arrow-up", |
252
|
|
|
"up" => "arrow-up", |
253
|
|
|
"arrow right" => "arrow-right", |
254
|
|
|
"right" => "arrow-right", |
255
|
|
|
"arrow down" => "arrow-down", |
256
|
|
|
"down" => "arrow-down" |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
/* Convert input */ |
260
|
|
|
if (array_key_exists($inputKey, $arrayConvert)) { |
261
|
|
|
$inputKey = $arrayConvert[$inputKey]; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$arrayKeySymbols = array( |
265
|
|
|
"shift" => array("default" => "⇧"), |
266
|
|
|
"opt" => array("default" => "⌥"), |
267
|
|
|
"enter" => array("default" => "⏎", "mac" => "⌤"), |
268
|
|
|
"alt" => array("default" => "⎇", "mac" => "⌥"), |
269
|
|
|
"delete" => array("default" => "⌫"), |
270
|
|
|
"ctrl" => array("default" => "✲", "windows" => "✲", "linux" => "⎈", "mac" => "^"), |
271
|
|
|
"esc" => array("default" => "⎋"), |
272
|
|
|
"command" => array("default" => "⌘"), |
273
|
|
|
"tab" => array("default" => "↹", "mac" => "⇥"), |
274
|
|
|
"caps-lock" => array("default" => "A", "mac" => "⇪"), |
275
|
|
|
"page-up" => array("default" => "▲", "mac" => "⇞"), |
276
|
|
|
"page-down" => array("default" => "▼", "mac" => "⇟"), |
277
|
|
|
"arrow-left" => array("default" => "←"), |
278
|
|
|
"arrow-up" => array("default" => "↑"), |
279
|
|
|
"arrow-right" => array("default" => "→"), |
280
|
|
|
"arrow-down" => array("default" => "↓"), |
281
|
|
|
// Sun |
282
|
|
|
"compose" => array("default" => "⎄"), |
283
|
|
|
"meta" => array("default" => "◆") |
284
|
|
|
); |
285
|
|
|
|
286
|
|
|
if (array_key_exists($inputKey, $arrayKeySymbols)) { |
287
|
|
|
return ((array_key_exists($inputOperatingSystem, $arrayKeySymbols[$inputKey])) ? |
288
|
|
|
$arrayKeySymbols[$inputKey][$inputOperatingSystem] : |
289
|
|
|
$arrayKeySymbols[$inputKey]["default"]); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return null; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Show fancy buttons for keyboard-shortcuts. |
297
|
|
|
* |
298
|
|
|
* @uses Holt45::kbdSymbol() |
299
|
|
|
* |
300
|
|
|
* @param array $inputArrayKeys |
301
|
|
|
* @param string $inputOperatingSystem |
302
|
|
|
* @param string $inputKbdClass |
303
|
|
|
* @param string $inputKbdSymbolClass |
304
|
|
|
* @param string $inputJoinGlue Glue |
305
|
|
|
* @return string String of html |
306
|
|
|
*/ |
307
|
|
|
public static function kbdShortcut( |
308
|
|
|
$inputArrayKeys, |
309
|
|
|
$inputOperatingSystem = "default", |
310
|
|
|
$inputKbdClass = "holt45-kbd", |
311
|
|
|
$inputKbdSymbolClass = "holt45-kbd__symbol", |
312
|
|
|
$inputJoinGlue = " + " |
313
|
|
|
) { |
314
|
|
|
$returnArray = array(); |
315
|
|
|
|
316
|
|
|
foreach ($inputArrayKeys as $key) { |
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
|
|
|
return implode($inputJoinGlue, $returnArray); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Attempting to keep CSS on one line with scoped style. |
333
|
|
|
* |
334
|
|
|
* NOTICE: This is a rough estimate, font type, design, etc affects the results. |
335
|
|
|
* |
336
|
|
|
* @param string $text |
337
|
|
|
* @param string $cssSelector |
338
|
|
|
* @param int $fontSizePx |
339
|
|
|
* @param int $minPageWidthPx |
340
|
|
|
* @return null|string Scoped style |
341
|
|
|
*/ |
342
|
|
|
public static function cssOneLineText($text, $cssSelector = "h1", $fontSizePx = 36, $minPageWidthPx = 320) |
343
|
|
|
{ |
344
|
|
|
$countText = strlen($text)+1; |
345
|
|
|
|
346
|
|
|
$fontWidth = ($fontSizePx / 2); |
347
|
|
|
|
348
|
|
|
if ($minPageWidthPx < $countText) { |
349
|
|
|
$minPageWidthPx = $countText; |
350
|
|
|
} |
351
|
|
|
if (($countText * $fontWidth) > $minPageWidthPx) { |
352
|
|
|
$cssNewFontSizePx = round(($minPageWidthPx / $countText) * 2, 2); |
353
|
|
|
$cssNewFontSizeVw = round((100/$countText) * 2, 2); |
354
|
|
|
|
355
|
|
|
$cssBreakpointPx = round($countText * $fontWidth); |
356
|
|
|
|
357
|
|
|
return '<style scoped>'.PHP_EOL. |
358
|
|
|
'@media (max-width: '.$cssBreakpointPx.'px) {'.PHP_EOL. |
359
|
|
|
$cssSelector.' {'.PHP_EOL. |
360
|
|
|
'font-size: '.$cssNewFontSizePx.'px; font-size: '.$cssNewFontSizeVw.'vw;'.PHP_EOL. |
361
|
|
|
'}'.PHP_EOL. |
362
|
|
|
'}'.PHP_EOL. |
363
|
|
|
'</style>'; |
364
|
|
|
} |
365
|
|
|
return null; |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|