This repository has been archived by the owner. It is now read-only.
forked from b2evolution/b2evolution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_escapecode.plugin.php
More file actions
233 lines (194 loc) · 6.58 KB
/
_escapecode.plugin.php
File metadata and controls
233 lines (194 loc) · 6.58 KB
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
<?php
/**
* This file implements the Escape code plugin for b2evolution
*
* b2evolution - {@link http://b2evolution.net/}
* Released under GNU GPL License - {@link http://b2evolution.net/about/gnu-gpl-license}
* @copyright (c)2003-2016 by Francois Planque - {@link http://fplanque.com/}
*
* @package plugins
*/
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
/**
* @package plugins
*/
class escapecode_plugin extends Plugin
{
var $code = 'escape_code';
var $name = 'Escape code';
var $priority = 8;
var $group = 'rendering';
var $short_desc;
var $long_desc;
var $version = '6.7.9';
var $number_of_installs = 1;
/**
* Init
*/
function PluginInit( & $params )
{
$this->short_desc = T_('Escapes html tags in code blocks');
$this->long_desc = T_('Escapes tags in blocks marked with <code> [codeblock] [codespan] or ``` (Markdown)');
}
/**
* Define here default collection/blog settings that are to be made available in the backoffice.
*
* @param array Associative array of parameters.
* @return array See {@link Plugin::GetDefaultSettings()}.
*/
function get_coll_setting_definitions( & $params )
{
$default_params = array_merge( $params, array(
'default_post_rendering' => 'stealth',
'default_comment_rendering' => 'stealth'
) );
return parent::get_coll_setting_definitions( $default_params );
}
/**
* Filters out the custom tag that would not validate, PLUS escapes the actual code.
*
* @param mixed $params
*/
function FilterItemContents( & $params )
{
if( $params['object_type'] == 'Item' && ! empty( $params['object'] ) )
{
$Item = & $params['object'];
if( $Item->get_type_setting( 'allow_html' ) )
{ // Do escape html entities only when html is allowed for content:
$content = & $params['content'];
$content = $this->escape_code( $content );
}
}
return true;
}
/**
* Event handler: Called before at the beginning, if a comment form gets sent (and received).
*/
function CommentFormSent( & $params )
{
$ItemCache = & get_ItemCache();
$comment_Item = & $ItemCache->get_by_ID( $params['comment_item_ID'], false );
if( !$comment_Item )
{ // Incorrect item
return false;
}
$item_Blog = & $comment_Item->get_Blog();
$apply_rendering = $this->get_coll_setting( 'coll_apply_comment_rendering', $item_Blog );
if( $item_Blog->get_setting( 'allow_html_comment' ) && $this->is_renderer_enabled( $apply_rendering, $params['renderers'] ) )
{ // Do escape html entities only when html is allowed for content and plugin is enabled
$content = & $params['comment'];
$content = $this->escape_code( $content );
}
}
/**
* Event handler: Called before at the beginning, if a message of thread form gets sent (and received).
*/
function MessageThreadFormSent( & $params )
{
global $Settings;
$apply_rendering = $this->get_msg_setting( 'msg_apply_rendering' );
if( $Settings->get( 'allow_html_message' ) && $this->is_renderer_enabled( $apply_rendering, $params['renderers'] ) )
{ // Do escape html entities only when html is allowed for content and plugin is enabled
$content = & $params['content'];
$content = $this->escape_code( $content );
}
}
/**
* Event handler: Called before at the beginning, if an email form gets sent (and received).
*/
function EmailFormSent( & $params )
{
$apply_rendering = $this->get_email_setting( 'email_apply_rendering' );
if( $this->is_renderer_enabled( $apply_rendering, $params['renderers'] ) )
{ // Do escape html entities only when html is allowed for content and plugin is enabled
$content = & $params['content'];
$content = $this->escape_code( $content );
}
}
/**
* Perform rendering
*
* @see Plugin::RenderItemAsHtml()
*/
function RenderItemAsHtml( & $params )
{
/* Initialize this function only in order to detect this plugin as renderer */
return true;
}
/**
* Escape html entities inside <code> tag
*
* @param string Content
* @param string Function name for callback
* @return string Escaped content
*/
function escape_code( $content, $callback_function = 'escape_code_callback' )
{
if( strpos( $content, '[codeblock' ) !== false || strpos( $content, '<codeblock' ) !== false )
{ // Do escape the html entities in code blocks:
$content = preg_replace_callback( '#([<\[]codeblock[^>\]]*[>\]])([\s\S]+?)([<\[]/codeblock[>\]])#is', array( $this, $callback_function ), $content );
}
if( strpos( $content, '[codespan' ) !== false || strpos( $content, '<codespan' ) !== false )
{ // Do escape the html entities in code spans:
$content = preg_replace_callback( '#([<\[]codespan[>\]])([\s\S]+?)([<\[]/codespan[>\]])#is', array( $this, $callback_function ), $content );
}
if( strpos( $content, '<code' ) !== false )
{ // At least one tag <code> exists in the content, Do escape the html entities:
$content = preg_replace_callback( '#(<code[^>]*>)([\s\S]+?)(</code>)#is', array( $this, $callback_function ), $content );
}
if( strpos( $content, '`' ) !== false )
{ // String of codespan from markdown, Do escape the html entities:
$content = preg_replace_callback( '#(`)([^`\n]+)(`)#i', array( $this, $callback_function ), $content );
}
if( strpos( $content, '```' ) !== false )
{ // String of codeblock from markdown, Do escape the html entities:
$content = preg_replace_callback( '#(```)([\s\S]+?)(```)#is', array( $this, $callback_function ), $content );
}
return $content;
}
/**
* Escape html entities inside <code> tag
*
* @param string Code content
* @return string Escaped code content
*/
function escape_code_callback( $code_content )
{
// Start tag
$escaped_content = $code_content[1];
// Escape two chars to escape html tags inside <code>
$escaped_content .= str_replace( array( '<', '>' ), array( '<', '>' ), $code_content[2] );
// End tag
$escaped_content .= $code_content[3];
return $escaped_content;
}
/**
* Unescape html entities inside <code> tag
*
* @param string Code content
* @return string Unescaped code content
*/
function unescape_code_callback( $code_content )
{
// Start tag
$escaped_content = $code_content[1];
// Escape two chars to escape html tags inside <code>
$escaped_content .= str_replace( array( '<', '>' ), array( '<', '>' ), $code_content[2] );
// End tag
$escaped_content .= $code_content[3];
return $escaped_content;
}
/**
* Formats post contents ready for editing
*
* @param mixed $params
*/
function UnfilterItemContents( & $params )
{
$content = & $params['content'];
$content = $this->escape_code( $content, 'unescape_code_callback' );
return true;
}
}
?>