source: extensions/AdditionalPages/admin/parse_bbcode.php @ 3417

Last change on this file since 3417 was 3292, checked in by patdenice, 15 years ago

New extension added:
Additional pages (2.0.b)

File size: 5.8 KB
RevLine 
[3292]1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5global $lang, $conf;
6
7include(get_language_filepath('plugin.lang.php', AP_PATH));
8
9function ap_CheckTags($str){
10        //array of known tags
11        $known = array('p','b','i','u','s','center','right','ol','ul','li','quote', 'img','url','email','color', 'size');
12        //storage stack
13        $tags = array();
14       
15        for ($pos = 0; $pos<strlen($str); $pos++)
16        {
17                if ($str{$pos} == '[')
18                        {
19                        $end_pos = strpos($str, ']', $pos);
20                        $tag = substr($str, ++$pos, $end_pos-$pos);
21                        //deals with tags which contains arguments (ie quote)
22                        if ( ($equal_pos = strpos($tag, '=', 0)) !== FALSE)
23                                $tag = substr($tag, 0, $equal_pos);
24                        //check whether we have a defined tag or not.
25                        if (in_array(strtolower($tag),$known) || in_array(strtolower(substr($tag,1)),$known))
26                                {
27                        //closing tag
28                        if ($tag{0} == '/')
29                                {
30                                        //cleaned tag
31                                        $tag = substr($tag, 1);         
32                                        $before_tag = substr($str, 0, $pos-1);
33                                        $after_tag = substr($str, $end_pos+1); 
34                                        //pop stack
35                                        while (($temp = array_pop($tags)))
36                                                        {
37                                                        if ($temp != $tag)
38                                                                $before_tag.='[/'.$temp.']';
39                                                        else 
40                                                                {
41                                                                $before_tag.='[/'.$tag.']';
42                                                                break;
43                                                                }
44
45                                                        }
46                                        $end_pos += strlen($before_tag)+strlen($after_tag)-strlen($str);
47                                        $str = $before_tag.$after_tag;
48                                }
49                        else 
50                                { // push stack
51                                array_push($tags,$tag);
52                                }
53                        }
54                $pos = $end_pos;       
55                }
56        }
57        // empty stack and closing tags
58        while ($temp = array_pop($tags))
59                {               
60                $str.='[/'.$temp.']';
61                }
62
63        return $str;
64}
65
66function ap_parse_bbcode($comment)
67{
68        $comment = nl2br($comment);
69        $comment = ap_CheckTags($comment);
70                       
71        $patterns = array();
72        $replacements = array();
73       
74        //Paragraph
75        $patterns[] = '#\[p\](.*?)\[/p\]#is';
76        $replacements[] = '<p>\\1</p>';
77    // Bold
78        $patterns[] = '#\[b\](.*?)\[/b\]#is';
79        $replacements[] = '<strong>\\1</strong>';
80        //Italic
81        $patterns[] = '#\[i\](.*?)\[/i\]#is';
82        $replacements[] = '<em>\\1</em>';
83        //Underline     
84        $patterns[] = '#\[u\](.*?)\[\/u\]#is';
85        $replacements[] = '<u>\\1</u>';
86        //Strikethrough
87        $patterns[] = '#\[s\](.*?)\[/s\]#is';
88        $replacements[] = '<del>\\1</del>';
89        //Center
90        $patterns[] = '#\[center\](.*?)\[/center\]#is';
91        $replacements[] = '</p><div align="center"><p>\\1</p></div><p>';
92        //Right
93        $patterns[] = '#\[right\](.*?)\[/right\]#is';
94        $replacements[] = '</p><div align="right"><p>\\1</p></div><p>';
95        //Olist
96        $patterns[] = '#\[ol\](.*?)\[/ol\]#is';
97        $replacements[] = '<ol>\\1</ol>';
98        //Ulist
99        $patterns[] = '#\[ul\](.*?)\[/ul\]#is';
100        $replacements[] = '<ul>\\1</ul>';
101        //List
102        $patterns[] = '#\[li\](.*?)\[/li\]#is';
103        $replacements[] = '<li>\\1</li>';
104        // Quotes
105        $patterns[] = "#\[quote\](.*?)\[/quote\]#is";
106        $replacements[] = '</p><blockquote><span style="font-size: 11px; line-height: normal">\\1</span></blockquote><p>';
107        //Quotes with "user"
108        $patterns[] = "#\[quote=&quot;(.*?)&quot;\](.*?)\[/quote\]#is";
109        $replacements[] = '</p><blockquote><span style="font-size: 11px; line-height: normal"><b>\\1 : </b><br/>\\2</span></blockquote><p>';
110        //Quotes with user
111        $patterns[] = "#\[quote=(.*?)\](.*?)\[/quote\]#is";
112        $replacements[] = '</p><blockquote><span style="font-size: 11px; line-height: normal"><b>\\1 : </b><br/>\\2</span></blockquote><p>';
113        //Images
114        $patterns[] = "#\[img\](.*?)\[/img\]#si";
115        $replacements[] = "<img src='\\1' alt='' />";
116        //[url]xxxx://www.zzzz.yyy[/url]
117        $patterns[] = "#\[url\]([\w]+?://[^ \"\n\r\t<]*?)\[/url\]#is"; 
118        $replacements[] = '<a href="\\1" target="_blank">\\1</a>'; 
119        //[url]www.zzzzz.yyy[/url]
120        $patterns[] = "#\[url\]((www|ftp)\.[^ \"\n\r\t<]*?)\[/url\]#is"; 
121        $replacements[] = '<a href="http://\\1" target="_blank">\\1</a>'; 
122        //[url=xxxx://www.zzzzz.yyy]ZzZzZ[/url] /*No I ain't sleeping yet*/
123        $patterns[] = "#\[url=([\w]+?://[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is"; 
124        $replacements[] = '<a href="\\1" target="_blank">\\2</a>'; 
125        // [url=www.zzzzz.yyy]zZzZz[/url] /*But I'm thinking about*/
126        $patterns[] = "#\[url=((www|ftp)\.[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is"; 
127        $replacements[] = '<a href="http://\\1" target="_blank">\\2</a>'; 
128        // [url="www.zzzzz.yyy"]zZzZz[/url]   /* It's nearly 2 am now */
129        $patterns[] = "#\[url=&quot;((www|ftp)\.[^ \n\r\t<]*?)&quot;\](.*?)\[/url\]#is";
130        $replacements[] = '<a href="http://\\1" target="_blank">\\3</a>';
131        //[url="http://www.zzzzz.yyy"]zZzZz[/url] /*I really dislike commenting code*/
132        $patterns[] = "#\[url=&quot;([\w]+?://[^ \n\r\t<]*?)&quot;\](.*?)\[/url\]#is";
133        $replacements[] = '<a href="\\1" target="_blank">\\2</a>';
134        //[email]samvure@gmail.com[/email]
135        $patterns[] = "#\[email\]([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#is";
136        $replacements[] = '<a href="mailto:\\1">\\1</a>';
137        //Size
138        $patterns[] = "#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si";
139        $replacements[] = '<span style="font-size: \\1px; line-height: normal">\\2</span>';
140        //Colours
141        $patterns[] = "#\[color=(\#[0-9A-F]{6}|[a-z]+)\](.*?)\[/color\]#si";
142        $replacements[] = '<span style="color: \\1">\\2</span>';
143       
144        $comment = preg_replace($patterns, $replacements, $comment);
145               
146        return $comment;
147}
148
149// Traitement des pages
150$result= pwg_query('SELECT id, text FROM ' . ADD_PAGES_TABLE);
151while ($row = mysql_fetch_assoc($result)) {
152        $text = ap_parse_bbcode($row['text']);
153        pwg_query('UPDATE ' . ADD_PAGES_TABLE . ' SET text="' . addslashes($text) . '" WHERE id=' . $row['id'] . ' LIMIT 1');
154}
155
156// Mise à jour de la configuration
157$ap_conf = explode ("," , $conf['additional_pages']);
158if (isset($ap_conf[5])) {
159        $ap_conf[5] = '';
160        pwg_query('UPDATE ' . CONFIG_TABLE . ' SET value="' . implode ("," , $ap_conf) . '" WHERE param="additional_pages" LIMIT 1');
161}
162
163redirect(str_replace('&amp;', '&', get_admin_plugin_menu_link(AP_PATH . 'admin/admin.php')), l10n('ap_convert_bbcode_ok'), 3);
164
165?>
Note: See TracBrowser for help on using the repository browser.