root/trunk/scripts/bbcode.php

Revision 1771, 8.5 KB (checked in by driehle, 7 weeks ago)

typo

Line 
1<?php
2
3require_once JLOG_BASEPATH.'/scripts/stringparser_bbcode.class.php';
4
5// ZeilenumbrÃŒche verschiedener Betriebsysteme vereinheitlichen
6function convertlinebreaks ($text) {
7  return preg_replace ("/\015\012|\015|\012/", "\n", $text);
8}
9
10// Alles bis auf Neuezeile-Zeichen entfernen
11function bbcode_stripcontents ($text) {
12  return preg_replace ("/[^\n]/", '', $text);
13}
14
15// Sonderzeichen behandeln
16function special_character($text) {
17  return str_replace("&amp;#", "&#", $text);
18}
19
20function do_bbcode_url ($action, $attributes, $content, $params, $node_object) {
21  // get URL by parameters
22  $url = isset($attributes['default']) ? $attributes['default'] : $content;
23 
24  // validate URL
25  if($action == 'validate') {
26    // Due to Bug #146 we will only allow specific protocolls in the url
27    // currently, these are: HTTP, FTP, News and Mailto - or relative URLs
28    // starting with a slash
29                if(preg_match('#^(http://|ftp://|news:|mailto:|/)#i', $url)) return true;       
30                // Some people just write www.example.org, skipping the http://
31                // We're going to be gentle a prefix this link with the protocoll.
32                // However, example.org (without www) will not be recognized
33                elseif(substr($url, 0, 4) == 'www.') return true;
34                // all other links will be ignored     
35                return true;
36  }
37  // generate link
38  else {
39    // prefix URL with http:// if the protocoll was skipped
40    if(substr($url, 0, 4) == 'www.') {
41      $url = 'http://' . $url;
42    }
43    // in case a relative url is given without a link text, we display
44    // the full URI as link text, not just the relative path
45    if(!isset($attributes['default']) AND substr($url, 0, 1) == '/') {
46      $content = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') 
47               . $_SERVER['HTTP_HOST'] 
48               . $url;
49    }
50    // build link
51    return '<a href="' . htmlspecialchars($url) . '">' . $content . '</a>';
52  }
53}
54
55// Funktion zum Einbinden von Bildern
56function do_bbcode_img ($action, $attributes, $content, $params, $node_object) {
57    if ($action == 'validate') {
58        if (isset($attributes['caption'])) {
59            $node_object->setFlag('paragraph_type', BBCODE_PARAGRAPH_BLOCK_ELEMENT);
60            if ($node_object->_parent->type() == STRINGPARSER_NODE_ROOT OR
61                in_array($node_object->_parent->_codeInfo['content_type'], array('block', 'list', 'listitem'))) {
62                return true;
63            }
64            else return false;
65        }
66        else return true;
67    }
68                $title = empty($attributes["title"]) ? "" : " title='".htmlspecialchars($attributes["title"])."'";
69
70    if (isset($attributes['class']) AND isset($attributes['caption'])) $class = " class='img ".htmlspecialchars($attributes['class'])."'";
71    elseif (isset($attributes['class'])) $class = " class='".htmlspecialchars($attributes['class'])."'";
72
73    if (strpos($content, "http://") === 0) return "<img src='".htmlspecialchars($content)."'".$class." alt='".htmlspecialchars($attributes['alt'])."'".$title." />";
74    else {
75        list($img_width, $img_height, $img_type, $img_attr) = @getimagesize(JLOG_BASEPATH.'/img'.DIRECTORY_SEPARATOR.htmlspecialchars($content));
76        $img = "<img src='".JLOG_PATH."/img/".htmlspecialchars($content)."'".$class." alt='".htmlspecialchars($attributes['alt'])."' style='width: ".$img_width."px;'".$title." />";
77    }
78
79     if(isset($attributes['caption'])) {
80        return "\n<dl".$class." style='width: ".$img_width."px;'>\n <dt>".$img."</dt>\n  <dd>".htmlspecialchars($attributes['caption'])."</dd>\n</dl>\n";
81     }
82     else return $img;
83}
84
85// Funktion zum Einbinden von HTML Code, welcher vom Browser interpretiert wird
86function do_bbcode_html($action, $attributes, $content, $params, $node_object) {
87    if ($action == 'validate') return true;
88    return $content;
89}
90
91$bbcode = new StringParser_BBCode ();
92$bbcode->addFilter (STRINGPARSER_FILTER_PRE, 'convertlinebreaks');
93$bbcode->addFilter (STRINGPARSER_FILTER_POST, 'special_character');
94
95$bbcode->addParser (array ('block', 'inline', 'link', 'listitem'), 'htmlspecialchars');
96$bbcode->addParser (array ('block', 'inline', 'link', 'listitem'), 'nl2br');
97$bbcode->addParser ('list', 'bbcode_stripcontents');
98$bbcode->setRootParagraphHandling (true);
99
100$bbcode->addCode ('b', 'simple_replace', null, array ('start_tag' => '<strong>', 'end_tag' => '</strong>'),
101                  'inline', array ('listitem', 'block', 'inline', 'link'), array ());
102
103$bbcode->addCode ('i', 'simple_replace', null, array ('start_tag' => '<em>', 'end_tag' => '</em>'),
104                  'inline', array ('listitem', 'block', 'inline', 'link'), array ());
105
106$bbcode->addCode ('headline', 'simple_replace', null, array('start_tag' => '<h3>', 'end_tag' => '</h3>'),
107                        'block', array('block'), array('inline', 'link'));
108
109$bbcode->addCode ('quote', 'simple_replace', null, array('start_tag' => '<blockquote>', 'end_tag' => '</blockquote>'),
110                        'block', array('block', 'listitem'), array('inline', 'link'));
111
112$bbcode->addCode ('url', 'usecontent?', 'do_bbcode_url', array ('usecontent_param' => 'default'),
113                  'link', array ('listitem', 'block', 'inline'), array ('link'));
114
115$bbcode->addCode ('img', 'usecontent', 'do_bbcode_img', array (),
116                  'image', array ('listitem', 'block', 'inline', 'link'), array ());
117
118$bbcode->addCode ('html', 'usecontent', 'do_bbcode_html', array (),
119                  'html', array ('listitem', 'block', 'inline', 'link'), array ('image'));
120
121$bbcode->addCode ('list', 'simple_replace', null, array ('start_tag' => '<ul>', 'end_tag' => '</ul>'),
122                  'list', array ('block', 'listitem'), array ());
123
124$bbcode->addCode ('olist', 'simple_replace', null, array ('start_tag' => '<ol>', 'end_tag' => '</ol>'),
125                  'list', array ('block', 'listitem'), array ());
126
127$bbcode->addCode ('*', 'simple_replace', null, array ('start_tag' => '<li>', 'end_tag' => '</li>'),
128                  'listitem', array ('list', 'olist' ), array ());
129
130$bbcode->setCodeFlag ('*', 'closetag', BBCODE_CLOSETAG_OPTIONAL);
131$bbcode->setCodeFlag ('*', 'paragraphs', false);
132$bbcode->setCodeFlag ('list', 'paragraph_type', BBCODE_PARAGRAPH_BLOCK_ELEMENT);
133$bbcode->setCodeFlag ('list', 'opentag.before.newline', BBCODE_NEWLINE_DROP);
134$bbcode->setCodeFlag ('list', 'closetag.after.newline', BBCODE_NEWLINE_DROP);
135$bbcode->setCodeFlag ('olist', 'paragraph_type', BBCODE_PARAGRAPH_BLOCK_ELEMENT);
136$bbcode->setCodeFlag ('olist', 'opentag.before.newline', BBCODE_NEWLINE_DROP);
137$bbcode->setCodeFlag ('olist', 'closetag.before.newline', BBCODE_NEWLINE_DROP);
138$bbcode->setCodeFlag ('headline', 'paragraph_type', BBCODE_PARAGRAPH_BLOCK_ELEMENT);
139$bbcode->setCodeFlag ('headline', 'opentag.before.newline', BBCODE_NEWLINE_DROP);
140$bbcode->setCodeFlag ('headline', 'closetag.after.newline', BBCODE_NEWLINE_DROP);
141$bbcode->setCodeFlag ('html', 'opentag.before.newline', BBCODE_NEWLINE_DROP);
142$bbcode->setCodeFlag ('html', 'closetag.after.newline', BBCODE_NEWLINE_DROP);
143$bbcode->setCodeFlag ('quote', 'paragraph_type', BBCODE_PARAGRAPH_BLOCK_ELEMENT);
144$bbcode->setCodeFlag ('quote', 'paragraphs', true);
145$bbcode->setCodeFlag ('quote', 'opentag.before.newline', BBCODE_NEWLINE_DROP);
146$bbcode->setCodeFlag ('quote', 'closetag.after.newline', BBCODE_NEWLINE_DROP);
147
148// BBCode for comments
149$bbcomments = new StringParser_BBCode ();
150$bbcomments->addFilter (STRINGPARSER_FILTER_PRE, 'convertlinebreaks');
151$bbcomments->addFilter (STRINGPARSER_FILTER_POST, 'special_character');
152
153$bbcomments->addParser (array ('block', 'inline', 'link'), 'htmlspecialchars');
154$bbcomments->addParser (array ('block', 'inline', 'link'), 'nl2br');
155$bbcomments->setRootParagraphHandling (true);
156
157$bbcomments->addCode ('b', 'simple_replace', null, array ('start_tag' => '<strong>', 'end_tag' => '</strong>'),
158                  'inline', array ('block', 'inline', 'link'), array ());
159$bbcomments->addCode ('i', 'simple_replace', null, array ('start_tag' => '<em>', 'end_tag' => '</em>'),
160                  'inline', array ('block', 'inline', 'link'), array ());
161$bbcomments->addCode ('url', 'usecontent?', 'do_bbcode_url', array ('usecontent_param' => 'default'),
162                  'link', array ('block', 'inline'), array ('link'));
163$bbcomments->addCode ('quote', 'simple_replace', null, array('start_tag' => '<blockquote>', 'end_tag' => '</blockquote>'),
164                        'block', array('block'), array('inline', 'link'));
165   
166$bbcomments->setCodeFlag ('quote', 'paragraph_type', BBCODE_PARAGRAPH_BLOCK_ELEMENT);
167$bbcomments->setCodeFlag ('quote', 'paragraphs', true);
168$bbcomments->setCodeFlag ('quote', 'opentag.before.newline', BBCODE_NEWLINE_DROP);
169$bbcomments->setCodeFlag ('quote', 'closetag.after.newline', BBCODE_NEWLINE_DROP);
170
171// eof
Note: See TracBrowser for help on using the browser.