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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
<?php
/**
* StudipCoreFormat.php - simple Stud.IP text markup parser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Elmar Ludwig
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
class StudipCoreFormat extends TextFormat
{
/**
* list of global Stud.IP markup rules
*/
private static $studip_rules = [
// heading level 1-4
'heading' => [
'start' => '^(!{1,4})([^\n]+)\n*',
'callback' => 'StudipCoreFormat::markupHeading'
],
// horizontal rule
'hrule' => [
'start' => '^--(\d?)$',
'callback' => 'StudipCoreFormat::markupHorizontalRule'
],
// list and table
'list' => [
'start' => '(^[=-]+ [^\n]+\n?)+',
'callback' => 'StudipCoreFormat::markupList'
],
'table' => [
'start' => '(^\|[^\n]*\|[^\n]*\n?)+',
'callback' => 'StudipCoreFormat::markupTable'
],
// block indent
'indent' => [
'start' => '(^ [^\n]+\n?)+',
'callback' => 'StudipCoreFormat::markupIndent'
],
// basic text formatting
'bold' => [
'start' => '\*\*',
'end' => '\*\*',
'callback' => 'StudipCoreFormat::markupText'
],
'italics' => [
'start' => '%%',
'end' => '%%',
'callback' => 'StudipCoreFormat::markupText'
],
'underline' => [
'start' => '__',
'end' => '__',
'callback' => 'StudipCoreFormat::markupText'
],
'verb' => [
'start' => '##',
'end' => '##',
'callback' => 'StudipCoreFormat::markupText'
],
'big' => [
'start' => '(\+\+)+',
'end' => '(\+\+)+',
'callback' => 'StudipCoreFormat::markupTextSize'
],
'small' => [
'start' => '(--)+',
'end' => '(--)+',
'callback' => 'StudipCoreFormat::markupTextSize'
],
'super' => [
'start' => '>>',
'end' => '>>',
'callback' => 'StudipCoreFormat::markupText'
],
'sub' => [
'start' => '<<',
'end' => '<<',
'callback' => 'StudipCoreFormat::markupText'
],
'strike' => [
'start' => '\{-',
'end' => '-\}',
'callback' => 'StudipCoreFormat::markupText'
],
// basic text formatting (simple form)
'simple_bold' => [
'start' => '(?<=\s|^)\*(\S+)\*(?=\s|$)',
'callback' => 'StudipCoreFormat::markupTextSimple'
],
'simple_italics' => [
'start' => '(?<=\s|^)%(\S+)%(?=\s|$)',
'callback' => 'StudipCoreFormat::markupTextSimple'
],
'simple_underline' => [
'start' => '(?<=\s|^)_(\S+)_(?=\s|$)',
'callback' => 'StudipCoreFormat::markupTextSimple'
],
'simple_verb' => [
'start' => '(?<=\s|^)#(\S+)#(?=\s|$)',
'callback' => 'StudipCoreFormat::markupTextSimple'
],
'simple_big' => [
'start' => '(?<=\s|^)\+(\S+)\+(?=\s|$)',
'callback' => 'StudipCoreFormat::markupTextSimple'
],
'simple_small' => [
'start' => '(?<=\s|^)-(\S+)-(?=\s|$)',
'callback' => 'StudipCoreFormat::markupTextSimple'
],
'simple_super' => [
'start' => '(?<=\s|^)>(\S+)>(?=\s|$)',
'callback' => 'StudipCoreFormat::markupTextSimple'
],
'simple_sub' => [
'start' => '(?<=\s|^)<(\S+)<(?=\s|$)',
'callback' => 'StudipCoreFormat::markupTextSimple'
],
// preformatted text, quote, nop and code
'pre' => [
'start' => '\[pre\]',
'end' => '\[\/pre\]',
'callback' => 'StudipCoreFormat::markupPreformat'
],
'quote' => [
'start' => '\[quote(=.*?)?\]',
'end' => '\[\/quote\]\s*',
'callback' => 'StudipCoreFormat::markupQuote'
],
'nop' => [
'start' => '\[nop\](.*?)\[\/nop\]',
'callback' => 'StudipCoreFormat::markupNoFormat'
],
'code' => [
'start' => '\[code(=.*?)?\](.*?)\[\/code\]',
'callback' => 'StudipCoreFormat::markupCode'
],
'media' => [
'start' => '\[(img|flash|audio|video)(.*?)\](.*?)(?=\s|$)',
'callback' => 'StudipCoreFormat::markupMedia'
],
'emails' => [
'start' => '(?xi:
# ensure block is preceded by whitspace, line start or closing
# tag
(?<=\s|^|\>)
# capture displayed text
(?:\[([^\n\f\]]+?)\])?
# capture actual email address
([\w.!#%+\-]+@([[:alnum:]\-.]+\.[[:alnum:]\-.]{2,}))
# ensure block is succeeded by white space or line end
(?=\s|$)
)',
'callback' => 'StudipCoreFormat::markupEmails'
],
'htmlAnchor' => [
# avoid replacing html links by studip's links-markup
# match <a ...="..."> ... </a>
'start' => '(?xi: <\s* a (?:\s(?:\s* \w+ \s*=\s* "[^"]*" )*)? \s*>)',
'end' => '(?xi: <\s* \/\s* a \s*>)',
'callback' => 'StudipCoreFormat::htmlAnchor'
],
'htmlImg' => [
# avoid replacing img links by studip's links-markup
# match <img ...="..." />
'start' => '(?xi:
# match tag start: <img
<\s*img
# match optional attributes
(\s(\s*
\w+ # attribute name
(\s*=\s*
("[^\"]*"|\'[^\']*\'|[^\s]*) # attribute value
)?
)+)?
# match tag end: \/>
\s*\/?\s*>
)',
'callback' => 'StudipCoreFormat::htmlImg'
],
'links' => [
// markup: [text]url
//
// To set URLs apart from normal text, scheme and host are
// obligatory. URLs are based on, but allow more than RFC3986 in
// order to simplify the regular expression.
//
// http://tools.ietf.org/html/rfc3986
'start' => '(?xi:
# capture 1: displayed text
(?:\[( [^\n\f\]]+ )\])?
# capture 2: URL
\b(
# scheme
[a-z][a-z0-9\+\-\.]*:\/\/
# user:password@host:port\/path?query#fragment
[a-zA-Z0-9\x{00a0}-\x{d7ff}\x{f900}-\x{fdcf}\x{fdf0}-\x{ffef}_\-\.~!$&\'\(\)\[\]*+,;=%:@\/?#]+
)
)',
'callback' => 'StudipCoreFormat::markupLinks'
],
'tex' => [
'start' => '\[tex\]',
'end' => '\[\/tex\]',
'callback' => 'StudipCoreFormat::markupTexFormat'
],
];
/**
* Returns the list of global Stud.IP markup rules as an array.
* Each entry has the following attributes: 'start', 'end' and
* 'callback'. The rule name is used as the entry's array key.
*
* @return array list of all markup rules
*/
public static function getStudipMarkups()
{
return self::$studip_rules;
}
/**
* Adds a new markup rule to the global Stud.IP markup set. This can
* also be used to replace an existing markup rule. The end regular
* expression is optional (i.e. may be NULL) to indicate that this
* rule has an empty content model. The callback is called whenever
* the rule matches and is passed the following arguments:
*
* - $markup the markup parser object
* - $matches match results of preg_match for $start
* - $contents (parsed) contents of this markup rule
*
* Sometimes you may want your rule to apply before another specific rule
* will apply. For this case the parameter $before defines a rulename of
* existing markup, before which your rule should apply.
*
* @param string $name name of this rule
* @param string $start start regular expression
* @param string $end end regular expression (optional)
* @param callback $callback function generating output of this rule
* @param string $before mark before which rule this rule should be appended
*/
public static function addStudipMarkup($name, $start, $end, $callback, $before = null)
{
$inserted = false;
foreach (self::$studip_rules as $rule_name => $rule) {
if ($rule_name === $before) {
self::$studip_rules[$name] = compact('start', 'end', 'callback');
$inserted = true;
}
if ($inserted) {
unset(self::$studip_rules[$rule_name]);
self::$studip_rules[$rule_name] = $rule;
}
}
if (!$inserted) {
self::$studip_rules[$name] = $end
? compact('start', 'end', 'callback')
: compact('start', 'callback');
}
}
/**
* Returns a single markup-rule if it exists.
* @return array: array('start' => "...", 'end' => "...", 'callback' => "...")
*/
public static function getStudipMarkup($name) {
return self::$studip_rules[$name];
}
/**
* Removes a markup rule from the global Stud.IP markup set.
*
* @param string $name name of the rule
*/
public static function removeStudipMarkup($name)
{
unset(self::$studip_rules[$name]);
}
/**
* Initializes a new StudipCoreFormat instance.
*/
public function __construct()
{
parent::__construct(self::getStudipMarkups());
}
/**
* Stud.IP markup for headings
*/
protected static function markupHeading($markup, $matches)
{
$level = max(1, 5 - mb_strlen($matches[1]));
$text = $markup->format($matches[2]);
return sprintf('<h%d class="content">%s</h%d>', $level, $text, $level);
}
/**
* Stud.IP markup for horizontal rule
*/
protected static function markupHorizontalRule($markup, $matches)
{
return sprintf('<hr class="content"%s>',
$matches[1] ? ' style="height: '.((int) $matches[1]).'px"' : ""
);
}
/**
* Basic text formatting: bold, italics, underline, sup, sub etc.
*/
protected static function markupText($markup, $matches, $contents)
{
static $tag = [
'**' => 'strong',
'%%' => 'em',
'__' => 'u',
'##' => 'tt',
'>>' => 'sup',
'<<' => 'sub',
'{-' => 's'
];
$key = $matches[0];
return sprintf('<%s>%s</%s>', $tag[$key], $contents, $tag[$key]);
}
/**
* Text size formatting: small and small
*/
protected static function markupTextSize($markup, $matches, $contents)
{
static $tag = [
'++' => 'big',
'--' => 'small',
];
$key = $matches[1];
$level = mb_strlen($matches[0]) / 2;
$open = str_repeat('<' . $tag[$key] . '>', $level);
$close = str_repeat('</' . $tag[$key] . '>', $level);
return $open . $contents . $close;
}
/**
* Basic text formatting: bold, italics, underline etc. (simple form)
*/
protected static function markupTextSimple($markup, $matches)
{
static $tag = [
'*' => 'strong',
'%' => 'em',
'_' => 'u',
'#' => 'tt',
'+' => 'big',
'-' => 'small',
'>' => 'sup',
'<' => 'sub'
];
$key = $matches[0][0];
$text = str_replace($key, ' ', $matches[1]);
return sprintf('<%s>%s</%s>', $tag[$key], $markup->quote($text), $tag[$key]);
}
/**
* Stud.IP markup for lists (may be nested)
*/
protected static function markupList($markup, $matches)
{
$rows = explode("\n", rtrim($matches[0]));
$indent = 0;
$result = '';
foreach ($rows as $row) {
list($level, $text) = explode(' ', $row, 2);
$level = mb_strlen($level);
if ($indent < $level) {
for (; $indent < $level; ++$indent) {
$type = $row[$indent] == '=' ? 'ol' : 'ul';
$result .= sprintf('<%s><li>', $type);
$types[] = $type;
}
} else {
for (; $indent > $level; --$indent) {
$result .= sprintf('</li></%s>', array_pop($types));
}
$result .= '</li><li>';
}
$result .= $markup->format($text);
}
for (; $indent > 0; --$indent) {
$result .= sprintf('</li></%s>', array_pop($types));
}
return $result;
}
/**
* Stud.IP markup for tables
*/
protected static function markupTable($markup, $matches)
{
$rows = explode("\n", rtrim($matches[0]));
$result = '<table class="content">';
foreach ($rows as $row) {
$cells = explode('|', trim(trim($row), '|'));
$result .= '<tr>';
foreach ($cells as $cell) {
$result .= '<td>';
$result .= $markup->format(trim($cell));
$result .= '</td>';
}
$result .= '</tr>';
}
$result .= '</table>';
return $result;
}
/**
* Stud.IP markup for indented paragraphs
*/
protected static function markupIndent($markup, $matches)
{
$text = preg_replace('/^ +/', '', $matches[0]);
$text = preg_replace('/^ /m', '', $text);
return sprintf('<div class="indent">%s</div>', $markup->format($text));
}
/**
* Stud.IP markup for preformatted text
*/
protected static function markupPreformat($markup, $matches, $contents)
{
return sprintf('<pre>%s</pre>', trim($contents));
}
/**
* Stud.IP markup for quoted text
*/
protected static function markupQuote($markup, $matches, $contents)
{
if (mb_strlen($matches[1]) > 1) {
$title = sprintf(_('%s hat geschrieben:'), $markup->format(mb_substr($matches[1], 1)));
return sprintf('<blockquote><div class="author">%s</div>%s</blockquote>',
$title, trim($contents));
} else {
return sprintf('<blockquote>%s</blockquote>',
trim($contents));
}
}
/**
* Stud.IP markup for unformatted text
*/
protected static function markupNoFormat($markup, $matches)
{
return $markup->quote($matches[1]);
}
/**
* Stud.IP markup for (PHP) source code
*/
protected static function markupCode($markup, $matches)
{
$codetype = "";
if (mb_strlen($matches[1])) {
$codetype = " ".decodeHTML(trim(mb_substr($matches[1], 1)), ENT_QUOTES);
}
$code = decodeHTML(trim($matches[2]), ENT_QUOTES);
return sprintf('<pre class="usercode %1$s"><code class="%1$s">%2$s</code></pre>',
htmlReady($codetype),
htmlReady($code));
}
/**
* Stud.IP markup for email-adresses
*/
protected static function markupEmails($markup, $matches)
{
$link_text = $matches[1] ?: $matches[2];
$email = $matches[2];
$domain = $matches[3];
$intern = $domain === $_SERVER['HTTP_HOST'];
return sprintf('<a href="mailto:%1$s">%2$s</a>',
$email,
$link_text
);
}
/**
* Stud.IP markup for images, audio, video and flash-films
*/
protected static function markupMedia($markup, $matches)
{
$tag = $matches[1];
$params = explode(":",$matches[2]);
$url = $matches[3];
$whitespace = $matches[4] ?? null;
$title = null;
$link = null;
$position = null;
$width = null;
$virtual_url = null;
foreach ($params as $key => $param) {
if ($param) {
if (is_numeric($param)) {
$width = $param;
} elseif(in_array($param, words("left center right"))) {
$position = $param;
} elseif($key === 0 && $param[0] === "=") {
$title = mb_substr($param, 1);
} elseif($key < count($params) - 1) {
$virtual_url = $param.":".$params[$key + 1];
if (isURL($virtual_url)) {
$link = $virtual_url;
}
}
}
}
$format_strings = [
'img' => '<img src="%s" style="%s" title="%s" alt="%s">',
'audio' => '<audio src="%s" style="%s" title="%s" alt="%s" controls></audio>',
'video' => '<video src="%s" style="%s" title="%s" alt="%s" controls></video>'
];
$url = TransformInternalLinks($url);
$pu = @parse_url($url);
$intern = false;
if (
in_array($pu['scheme'], ['http', 'https'])
&& (
$pu['host'] === $_SERVER['HTTP_HOST']
|| (isset($pu['port']) && $pu['host'] . ':' . $pu['port'] === $_SERVER['HTTP_HOST'])
)
&& mb_strpos($pu['path'], $GLOBALS['CANONICAL_RELATIVE_PATH_STUDIP']) === 0
) {
$intern = true;
$checkpath = urldecode(mb_substr($pu['path'], mb_strlen($GLOBALS['CANONICAL_RELATIVE_PATH_STUDIP'])));
if (mb_strpos($checkpath, '../') === false) {
list($pu['first_target']) = explode('/', $checkpath);
} else {
$pu['first_target'] = false;
}
}
$LOAD_EXTERNAL_MEDIA = Config::GetInstance()->getValue('LOAD_EXTERNAL_MEDIA');
if ($intern
&& !in_array($pu['first_target'], ['sendfile.php','download','assets','pictures'])
&& !($pu['first_target'] === 'dispatch.php' && mb_strpos($pu['path'], 'dispatch.php/document/download') !== false))
{
return $matches[0];
} elseif ((!$LOAD_EXTERNAL_MEDIA || $LOAD_EXTERNAL_MEDIA === 'deny') && !$intern) {
return $matches[0];
}
//Mediaproxy?
if (!$intern && $LOAD_EXTERNAL_MEDIA === "proxy" && Seminar_Session::is_current_session_authenticated()) {
$media_url = $GLOBALS['ABSOLUTE_URI_STUDIP'] . 'dispatch.php/media_proxy?url=' . urlencode(decodeHTML(idna_link($url)));
} else {
$media_url = idna_link($url);
}
if ($tag === "flash") {
$width = $width ? $width : 200;
$height = round($width * 0.75);
$flash_config = $width > 200 ? $GLOBALS['FLASHPLAYER_DEFAULT_CONFIG_MAX'] : $GLOBALS['FLASHPLAYER_DEFAULT_CONFIG_MIN'];
$media = '<object type="application/x-shockwave-flash" id="FlashPlayer" data="'.Assets::url().'flash/player_flv.swf" width="'.$width.'" height="'.$height.'">
<param name="movie" value="'.Assets::url().'flash/player_flv.swf">
<param name="allowFullScreen" value="true">
<param name="FlashVars" value="flv='.urlencode(decodeHTML($media_url)).'&startimage='.$link.$flash_config.'">
<embed src="'.Assets::url().'flash/player_flv.swf" movie="$media_url" type="application/x-shockwave-flash" FlashVars="flv='.urlencode(decodeHTML($media_url)).'&startimage='.$link.$flash_config.'">
</object>';
} else {
$media = sprintf($format_strings[$tag],
$media_url,
isset($width) ? "width: ".$width."px;" : "",
$title,
$title
);
}
if ($tag === 'audio') {
$random_id = 'audio-' . mb_substr(md5(uniqid('audio', true)), -8);
$media = str_replace('<audio ', '<audio id="' . $random_id . '" onerror="STUDIP.Audio.handle(this);" ', $media);
}
if ($link && $tag === "img") {
$media = sprintf('<a href="%s"%s>%s</a>',
$link,
!isLinkIntern($link) ? ' target="_blank" rel="noreferrer noopener"' : "",
$media
);
}
if ($position) {
$media = '<div style="text-align: '.$position.'">'.$media.'</div>';
}
$media .= $whitespace;
return $media;
}
/**
* Stud.IP markup for hyperlinks (intern, extern).
* Has lower priority than [code], [img], etc
*/
protected static function markupLinks($markup, $matches)
{
if ($markup->isInsideOf('htmlAnchor') || $markup->isInsideOf('htmlImg')) {
return $matches[0];
}
$url = $matches[2];
$title = $matches[1] ?: $url;
// Remove closing bracket if not part of url
$postfix = '';
if ($title === $url
&& preg_match('/\)[\.,]?$/', $url, $match)
&& substr_count($url, '(') !== substr_count($url, ')'))
{
$title = $url = mb_substr($url, 0, -mb_strlen($match[0]));
$postfix = $match[0];
}
$intern = isLinkIntern($url);
$url = TransformInternalLinks($url);
$linkmarkup = clone $markup;
$linkmarkup->removeMarkup('links');
$linkmarkup->removeMarkup('wiki-links');
if (strpos($url, 'mailto:') === 0) {
return sprintf(
'<a href="%1$s">%2$s</a>%3$s',
$url,
$linkmarkup->format($title),
$postfix
);
} else {
return sprintf(
'<a class="%s" href="%s"%s>%s</a>%s',
$intern ? 'link-intern' : 'link-extern',
$url,
$intern ? '' : ' target="_blank" rel="noreferrer noopener"',
$linkmarkup->format($title),
$postfix
);
}
}
protected static function htmlAnchor($markup, $matches, $contents)
{
return $matches[0] . $contents . '</a>';
}
protected static function htmlImg($markup, $matches, $contents)
{
return $matches[0];
}
/**
* Stud.IP markup for tex tags
*/
protected static function markupTexformat($markup, $matches, $contents)
{
return sprintf('<span class="math-tex">\\(%s\\)</span>', trim($contents));
}
}
|