~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/Text/Filter/bbcode.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
require_once 'Horde.php';
 
4
 
 
5
/**
 
6
 * The Text_Filter_bbcode:: class finds bbcode-style markup (see below) in a
 
7
 * block of text and turns it into HTML.
 
8
 *
 
9
 * Parameters:
 
10
 * <pre>
 
11
 * entities -- If true before replacing bbcode with HTML tags, any HTML
 
12
 *             entities will be replaced.
 
13
 * </pre>
 
14
 *
 
15
 * Supported bbcode:
 
16
 * <pre>
 
17
 *     [b]Bold Text[/b]
 
18
 *     [i]Italics Text[/i]
 
19
 *     [u]Underlined Text[/u]
 
20
 *     [quote]Quoted Text[/quote]
 
21
 *     [center]Centered Text[/center]
 
22
 *
 
23
 *     List of items
 
24
 *     [list]
 
25
 *     [*] Item one
 
26
 *     [*] Item two
 
27
 *     [/list]
 
28
 *
 
29
 *     Numbered list
 
30
 *     [numlist]
 
31
 *     [*] Item one
 
32
 *     [*] Item two
 
33
 *     [/numlist]
 
34
 *
 
35
 *     [url]http://www.horde.org[/url] -> Link to the address using the
 
36
 *         address itself for the text.  You can specify the protocol: http or
 
37
 *         https and the port.
 
38
 *     [url]www.horde.org[/url] -> Link to the address using the address
 
39
 *         itself for the text.  You can specify the port.  The protocol is by
 
40
 *         default http.
 
41
 *     [url=http://www.horde.org]Link to Horde[/url] -> Link to the address
 
42
 *         using "Link to Horde" for the text.  You can specify the protocol:
 
43
 *         http or https and the port.
 
44
 *     [url=www.horde.org]Link to Horde[/url] -> Link to the address using
 
45
 *         "Link to Horde" for the text.  You can specify the port.  The
 
46
 *         protocol is by default http
 
47
 *     [email]cpedrinaci@yahoo.es[/email] -> sets a mailto link.
 
48
 *     [email=cpedrinaci@yahoo.es]Mail to Carlos[/email] -> Sets a mailto link
 
49
 *         and the text is "Mail to Carlos".
 
50
 * </pre>
 
51
 *
 
52
 * $Horde: framework/Text_Filter/Filter/bbcode.php,v 1.8.10.2 2005/01/11 20:44:48 chuck Exp $
 
53
 *
 
54
 * Copyright 2003-2005 Carlos Pedrinaci <cpedrinaci@yahoo.es>
 
55
 *
 
56
 * Email validation based on Chuck Hagenbuch's
 
57
 * Mail_RFC822::isValidInetAddress.
 
58
 *
 
59
 * See the enclosed file COPYING for license information (LGPL). If you did
 
60
 * not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 
61
 *
 
62
 * @author  Carlos Pedrinaci <cpedrinaci@yahoo.es>
 
63
 * @package Horde_Text
 
64
 */
 
65
class Text_Filter_bbcode extends Text_Filter {
 
66
 
 
67
    /**
 
68
     * Filter parameters.
 
69
     *
 
70
     * @var array $_params
 
71
     */
 
72
    var $_params = array('entities' => false);
 
73
 
 
74
    /**
 
75
     * Executes any code necessary before applying the filter patterns.
 
76
     *
 
77
     * @param string $text  The text before the filtering.
 
78
     *
 
79
     * @return string  The modified text.
 
80
     */
 
81
    function preProcess($text)
 
82
    {
 
83
        if ($this->_params['entities']) {
 
84
            $text = @htmlspecialchars($text);
 
85
        }
 
86
 
 
87
        return $text;
 
88
    }
 
89
 
 
90
    /**
 
91
     * Returns a hash with replace patterns.
 
92
     *
 
93
     * @return array  Patterns hash.
 
94
     */
 
95
    function getPatterns()
 
96
    {
 
97
        $replace = array(
 
98
            '[i]' => '<i>', '[/i]' => '</i>',
 
99
            '[u]' => '<u>', '[/u]' => '</u>',
 
100
            '[b]' => '<b>', '[/b]' => '</b>',
 
101
            '[center]' => '<center>', '[/center]' => '</center>',
 
102
            '[quote]' => '<blockquote>', '[/quote]' => '</blockquote>',
 
103
            '[list]' => '<ul>', '[/list]' => '</ul>',
 
104
            '[numlist]' => '<ol>', '[/numlist]' => '</ol>',
 
105
            '[*]' => '<li>');
 
106
 
 
107
        /* When checking URLs we validate part of them, but it is up
 
108
         * to the user to write them correctly (in particular the
 
109
         * query string). Concerning mails we use the regular
 
110
         * expression in Mail_RFC822's isValidInetAddress() function,
 
111
         * slightly modified. */
 
112
        $regexp = array(
 
113
            "#\[url\]((http|https)://([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\[/url\]#U" =>
 
114
            Horde::link("$1", "$1") . "$1</a>",
 
115
 
 
116
            "#\[url\=((http|https)://([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\]([^<>]+)\[/url\]#U" =>
 
117
            Horde::link("$1", "$1") . "$9</a>",
 
118
 
 
119
            "#\[url\](([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\[/url\]#U" =>
 
120
            Horde::link("http://$1", "http://$1") . "$1</a>",
 
121
 
 
122
            "#\[url\=(([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\]([^<>]+)\[/url\]#U" =>
 
123
            Horde::link("http://$1", "http://$1") . "$8</a>",
 
124
 
 
125
            "#\[email\](([*+!.&\#$|\'\\%\/0-9a-zA-Z^_`{}=?~:-]+)@(([0-9a-zA-Z-]+\.)+[0-9a-zA-Z]{2,4}))\[/email\]#U" =>
 
126
            Horde::link("mailto:$1", "mailto:$1") . "$1</a>",
 
127
 
 
128
            "#\[email\=(([*+!.&\#$|\'\\%\/0-9a-zA-Z^_`{}=?~:-]+)@(([0-9a-zA-Z-]+\.)+[0-9a-zA-Z]{2,4}))\]([^<>]+)\[/email\]#U" =>
 
129
            Horde::link("mailto:$1", "mailto:$1") . "$5</a>"
 
130
        );
 
131
 
 
132
        return array('replace' => $replace, 'regexp' => $regexp);
 
133
    }
 
134
 
 
135
}