-
Notifications
You must be signed in to change notification settings - Fork 1
/
BBCHelpers.php
338 lines (284 loc) · 9.75 KB
/
BBCHelpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
*
* @name ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause
*
* This software is a derived product, based on:
*
* Simple Machines Forum (SMF)
* copyright: 2011 Simple Machines (http://www.simplemachines.org)
* license: BSD, See included LICENSE.TXT for terms and conditions.
*
*
*/
/**
* Microsoft uses their own character set Code Page 1252 (CP1252), which is a
* superset of ISO 8859-1, defining several characters between DEC 128 and 159
* that are not normally displayable. This converts the popular ones that
* appear from a cut and paste from windows.
*
* @param string|false $string
* @return string $string
*/
function sanitizeMSCutPaste($string)
{
if (empty($string))
return $string;
// UTF-8 occurrences of MS special characters
$findchars_utf8 = array(
"\xe2\x80\x9a", // single low-9 quotation mark
"\xe2\x80\x9e", // double low-9 quotation mark
"\xe2\x80\xa6", // horizontal ellipsis
"\xe2\x80\x98", // left single curly quote
"\xe2\x80\x99", // right single curly quote
"\xe2\x80\x9c", // left double curly quote
"\xe2\x80\x9d", // right double curly quote
"\xe2\x80\x93", // en dash
"\xe2\x80\x94", // em dash
);
// safe replacements
$replacechars = array(
',', // ‚
',,', // „
'...', // …
"'", // ‘
"'", // ’
'"', // “
'"', // ”
'-', // –
'--', // —
);
$string = str_replace($findchars_utf8, $replacechars, $string);
return $string;
}
/**
* Parse smileys in the passed message.
*
* What it does:
* - The smiley parsing function which makes pretty faces appear :).
* - If custom smiley sets are turned off by smiley_enable, the default set of smileys will be used.
* - These are specifically not parsed in code tags [url=mailto:Dad@blah.com]
* - Caches the smileys from the database or array in memory.
* - Doesn't return anything, but rather modifies message directly.
*
* @param string $message
*/
function parsesmileys(&$message)
{
global $modSettings, $txt, $user_info;
static $smileyPregSearch = null, $smileyPregReplacements = array();
// No smiley set at all?!
if ($user_info['smiley_set'] == 'none' || trim($message) == '')
return;
// If smileyPregSearch hasn't been set, do it now.
if (empty($smileyPregSearch))
{
// Use the default smileys if it is disabled. (better for "portability" of smileys.)
if (empty($modSettings['smiley_enable']))
{
$smileysfrom = array('>:D', ':D', '::)', '>:(', ':))', ':)', ';)', ';D', ':(', ':o', '8)', ':P', '???', ':-[', ':-X', ':-*', ':\'(', ':-\\', '^-^', 'O0', 'C:-)', 'O:)');
$smileysto = array('evil.gif', 'cheesy.gif', 'rolleyes.gif', 'angry.gif', 'laugh.gif', 'smiley.gif', 'wink.gif', 'grin.gif', 'sad.gif', 'shocked.gif', 'cool.gif', 'tongue.gif', 'huh.gif', 'embarrassed.gif', 'lipsrsealed.gif', 'kiss.gif', 'cry.gif', 'undecided.gif', 'azn.gif', 'afro.gif', 'police.gif', 'angel.gif');
$smileysdescs = array('', $txt['icon_cheesy'], $txt['icon_rolleyes'], $txt['icon_angry'], $txt['icon_laugh'], $txt['icon_smiley'], $txt['icon_wink'], $txt['icon_grin'], $txt['icon_sad'], $txt['icon_shocked'], $txt['icon_cool'], $txt['icon_tongue'], $txt['icon_huh'], $txt['icon_embarrassed'], $txt['icon_lips'], $txt['icon_kiss'], $txt['icon_cry'], $txt['icon_undecided'], '', '', '', $txt['icon_angel']);
}
else
{
// Load the smileys in reverse order by length so they don't get parsed wrong.
if (($temp = cache_get_data('parsing_smileys', 480)) == null)
{
$smileysfrom = array();
$smileysto = array();
$smileysdescs = array();
// @todo there is no reason $db should be used before this
$db = database();
$db->fetchQueryCallback('
SELECT code, filename, description
FROM {db_prefix}smileys
ORDER BY LENGTH(code) DESC',
array(
),
function($row) use (&$smileysfrom, &$smileysto, &$smileysdescs)
{
$smileysfrom[] = $row['code'];
$smileysto[] = htmlspecialchars($row['filename']);
$smileysdescs[] = $row['description'];
}
);
cache_put_data('parsing_smileys', array($smileysfrom, $smileysto, $smileysdescs), 480);
}
else
list ($smileysfrom, $smileysto, $smileysdescs) = $temp;
}
// The non-breaking-space is a complex thing...
$non_breaking_space = '\x{A0}';
// This smiley regex makes sure it doesn't parse smileys within code tags (so [url=mailto:David@bla.com] doesn't parse the :D smiley)
$smileyPregReplacements = array();
$searchParts = array();
$smileys_path = htmlspecialchars($modSettings['smileys_url'] . '/' . $user_info['smiley_set'] . '/');
for ($i = 0, $n = count($smileysfrom); $i < $n; $i++)
{
$specialChars = htmlspecialchars($smileysfrom[$i], ENT_QUOTES);
$smileyCode = '<img src="' . $smileys_path . $smileysto[$i] . '" alt="' . strtr($specialChars, array(':' => ':', '(' => '(', ')' => ')', '$' => '$', '[' => '[')). '" title="' . strtr(htmlspecialchars($smileysdescs[$i]), array(':' => ':', '(' => '(', ')' => ')', '$' => '$', '[' => '[')) . '" class="smiley" />';
$smileyPregReplacements[$smileysfrom[$i]] = $smileyCode;
$searchParts[] = preg_quote($smileysfrom[$i], '~');
if ($smileysfrom[$i] != $specialChars)
{
$smileyPregReplacements[$specialChars] = $smileyCode;
$searchParts[] = preg_quote($specialChars, '~');
}
}
$smileyPregSearch = '~(?<=[>:\?\.\s' . $non_breaking_space . '[\]()*\\\;]|^)(' . implode('|', $searchParts) . ')(?=[^[:alpha:]0-9]|$)~';
}
// Replace away!
$message = preg_replace_callback($smileyPregSearch, function ($matches) use ($smileyPregReplacements)
{
return $smileyPregReplacements[$matches[0]];
}, $message);
}
/**
* Calculates all the possible permutations (orders) of an array.
*
* What it does:
* - should not be called on arrays bigger than 10 elements as this function is memory hungry
* - returns an array containing each permutation.
* - e.g. (1,2,3) returns (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1)
* - really a combinations without repetition N! function so 3! = 6 and 10! = 4098 combinations
* - Used by parse_bbc to allow bbc tag parameters to be in any order and still be
* parsed properly
*
* @param mixed[] $array index array of values
* @return mixed[] array representing all permutations of the supplied array
*/
function permute($array)
{
$orders = array($array);
$n = count($array);
$p = range(0, $n);
for ($i = 1; $i < $n; null)
{
$p[$i]--;
$j = $i % 2 != 0 ? $p[$i] : 0;
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $temp;
for ($i = 1; $p[$i] == 0; $i++)
$p[$i] = 1;
$orders[] = $array;
}
return $orders;
}
function pc_next_permutation($p, $size)
{
// If there is only 1, then there can only be 1 permutation... duh.
if ($size < 1)
{
return false;
}
// slide down the array looking for where we're smaller than the next guy
for ($i = $size - 1; isset($p[$i]) && $p[$i] >= $p[$i + 1]; --$i);
// if this doesn't occur, we've finished our permutations
// the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if ($i < 0)
{
return false;
}
// slide down the array looking for a bigger number than what we found before
for ($j = $size; $p[$j] <= $p[$i]; --$j);
// swap them
$tmp = $p[$i];
$p[$i] = $p[$j];
$p[$j] = $tmp;
// now reverse the elements in between by swapping the ends
for (++$i, $j = $size; $i < $j; ++$i, --$j)
{
$tmp = $p[$i];
$p[$i] = $p[$j];
$p[$j] = $tmp;
}
return $p;
}
function footnote_callback($matches)
{
global $fn_num, $fn_content, $fn_count;
$fn_num++;
$fn_content[] = '<div class="target" id="fn' . $fn_num . '_' . $fn_count . '"><sup>' . $fn_num . ' </sup>' . $matches[2] . '<a class="footnote_return" href="#ref' . $fn_num . '_' . $fn_count . '">↵</a></div>';
return '<a class="target" href="#fn' . $fn_num . '_' . $fn_count . '" id="ref' . $fn_num . '_' . $fn_count . '">[' . $fn_num . ']</a>';
}
/**
* Cut down version just so we can run test cases
*/
function htmlTime($timestamp)
{
if (empty($timestamp))
return '';
$timestamp = forum_time(true, $timestamp);
$time = date('Y-m-d H:i', $timestamp);
$stdtime = standardTime($timestamp, true, true);
// @todo maybe htmlspecialchars on the title attribute?
return '<time title="' . $stdtime . '" datetime="' . $time . '" data-timestamp="' . $timestamp . '">' . $stdtime . '</time>';
}
/**
* Cut down version just so we can run test cases
*/
function forum_time($use_user_offset = true, $timestamp = null)
{
if ($timestamp === null)
$timestamp = time();
elseif ($timestamp == 0)
return 0;
return $timestamp;
}
/**
* Cut down version just so we can run test cases
*/
function standardTime($log_time, $show_today = true, $offset_type = false)
{
$time = $log_time;
// Format any other characters..
return strftime('%B %d, %Y, %I:%M:%S %p', $time);
}
// This is just a mock so we don't break anything
function call_integration_hook($hook, $parameters = array())
{
return;
}
function cache_put_data($key, $value, $ttl = 120)
{
return;
}
function cache_get_data($key, $ttl = 120)
{
return;
}
// because shuffle doesn't have a shuffle_assoc()
function shuffle_assoc(&$array)
{
$keys = array_keys($array);
shuffle($keys);
foreach($keys as $key)
{
$new[$key] = $array[$key];
}
$array = $new;
return true;
}
function tabToHtmlTab($string)
{
return str_replace("\t", "<span class=\"tab\">\t</span>", $string);
}
function removeBr($string)
{
return str_replace('<br />', '', $string);
}
function addProtocol($string, $protocol = 'http')
{
if (substr_compare($string, 'http://', 0) !== 0
&& substr_compare($string, 'https://', 0) !== 0
//&& substr_compare($string, 'ftp://', 0) !== 0
&& substr_compare($string, 'mailto://', 0) !== 0
&& substr_compare($string, $protocol, 0) !== 0)
{
return $protocol . $string;
}
}