/
string_api.php
312 lines (263 loc) · 10.6 KB
/
string_api.php
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
<?php
# Mantis - a php based bugtracking system
# Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 - 2003 Mantis Team - mantisbt-dev@lists.sourceforge.net
# This program is distributed under the terms and conditions of the GPL
# See the README and LICENSE files for details
# --------------------------------------------------------
# $Id: string_api.php,v 1.30 2003-02-22 08:26:07 jfitzell Exp $
# --------------------------------------------------------
$t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR;
require_once( $t_core_dir . 'bug_api.php' );
require_once( $t_core_dir . 'user_pref_api.php' );
###########################################################################
# String Processing API
###########################################################################
# --------------------
# Prepare a string for display to HTML
function string_display( $p_string ) {
$p_string = string_strip_hrefs( $p_string );
$p_string = htmlentities( $p_string );
$p_string = string_restore_valid_html_tags( $p_string );
$p_string = nl2br( $p_string );
return $p_string;
}
# --------------------
# Prepare a string for display to HTML and add href anchors for URLs, emails,
# bug references, and cvs references
function string_display_links( $p_string ) {
$p_string = string_display( $p_string );
$p_string = string_insert_hrefs( $p_string );
$p_string = string_process_bug_link( $p_string );
$p_string = string_process_cvs_link( $p_string );
return $p_string;
}
# --------------------
# Prepare a string for plain text display in email
function string_email( $p_string ) {
$p_string = string_strip_hrefs( $p_string );
return $p_string;
}
# --------------------
# Prepare a string for plain text display in email and add URLs for bug
# links and cvs links
function string_email_links( $p_string ) {
$p_string = string_email( $p_string );
$p_string = string_process_bug_link( $p_string, false );
$p_string = string_process_cvs_link( $p_string, false );
return $p_string;
}
# --------------------
# Process a string for display in a textarea box
function string_textarea( $p_string ) {
$p_string = htmlentities( $p_string );
return $p_string;
}
# --------------------
# Process a string for display in a text box
function string_attribute( $p_string ) {
$p_string = htmlentities( $p_string );
return $p_string;
}
# --------------------
# Process a string for inclusion in a URL as a GET parameter
function string_url( $p_string ) {
$p_string = htmlentities( urlencode( $p_string ) );
return $p_string;
}
# --------------------
# process the $p_string and convert filenames in the format
# cvs:filename.ext or cvs:filename.ext:n.nn to a html link
# if $p_include_anchor is true, include an <a href="..."> tag,
# otherwise, just insert the URL as text
function string_process_cvs_link( $p_string, $p_include_anchor=true ) {
$t_cvs_web = config_get( 'cvs_web' );
if ( $p_include_anchor ) {
$t_replace_with = '[CVS] <a href="'.$t_cvs_web.'\\1?rev=\\4" target="_new">\\1</a>\\5';
} else {
$t_replace_with = '[CVS] '.$t_cvs_web.'\\1?rev=\\4\\5';
}
return preg_replace( '/cvs:([^\.\s:,\?!]+(\.[^\.\s:,\?!]+)*)(:)?(\d\.[\d\.]+)?([\W\s])?/i',
$t_replace_with,
$p_string );
}
# --------------------
# process the $p_string and create links to bugs if warranted
# Uses the bug_link_tag config variable to determine the bug link tag
# eg. #45 or bug:76
# default is the # symbol. You may substitue any pattern you want.
# if $p_include_anchor is true, include an <a href="..."> tag,
# otherwise, just insert the URL as text
# The symbol must be at the beginning of the string or preceeded by whitespace
function string_process_bug_link( $p_string, $p_include_anchor=true ) {
$t_tag = config_get( 'bug_link_tag' );
$t_path = config_get( 'path' );
preg_match_all('/(^|.+?)(?:(?<=^|\s)' . preg_quote($t_tag) . '(\d+)|$)/s',
$p_string, $t_matches, PREG_SET_ORDER );
$t_result = '';
if ( $p_include_anchor ) {
foreach ( $t_matches as $t_match ) {
$t_result .= $t_match[1];
if ( isset( $t_match[2] ) ) {
$t_bug_id = $t_match[2];
if ( bug_exists( $t_bug_id ) ) {
$t_result .= string_get_bug_view_link( $t_bug_id );
} else {
$t_result .= $t_tag . $t_bug_id;
}
}
}
} else {
foreach ( $t_matches as $t_match ) {
$t_result .= $t_match[1];
if ( isset( $t_match[2] ) ) {
$t_bug_id = $t_match[2];
# We might as well create the link here even if the bug
# doesn't exist. In the case above we don't want to do
# the summary lookup on a non-existant bug. But here, we
# can create the link and by the time it is clicked on, the
# bug may exist.
$t_result .= string_get_bug_view_url( $t_bug_id );
}
}
}
return $t_result;
}
#===================================
# Tag Processing
#===================================
# --------------------
# Detect URLs and email addresses in the string and replace them with href anchors
function string_insert_hrefs( $p_string ) {
if ( ! config_get( 'html_make_links' ) ) {
return $p_string;
}
$p_string = eregi_replace( "([[:alnum:]]+)://([^[:space:]<]*)([[:alnum:]#?/&=])",
"<a href=\"\\1://\\2\\3\">\\1://\\2\\3</a>",
$p_string);
$p_string = eregi_replace( "(([a-z0-9_]|\\-|\\.)+@([^[:space:]<]*)([[:alnum:]-]))",
"<a href=\"mailto:\\1\" target=\"_new\">\\1</a>",
$p_string);
return $p_string;
}
# --------------------
# Detect href anchors in the string and replace them with URLs and email addresses
function string_strip_hrefs( $p_string ) {
$p_string = eregi_replace( "<a href=\"mailto:(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))\" target=\"_new\">(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))</a>",
"\\1",
$p_string);
$p_string = eregi_replace( "<a href=\"([[:alnum:]]+://[^[:space:]]*)([[:alnum:]#?/&=])\">([^<]*)</a>",
"\\1",
$p_string);
return $p_string;
}
# --------------------
# This function looks for text with htmlentities
# like <b> and converts is into corresponding
# html <b> based on the configuration presets
function string_restore_valid_html_tags( $p_string ) {
$t_html_valid_tags = config_get( 'html_valid_tags' );
if ( OFF === $t_html_valid_tags ||
is_blank( $t_html_valid_tags ) ) {
return $p_string;
}
$tags = explode( ',', $t_html_valid_tags );
foreach ( $tags as $tag ) {
if ( !is_blank( $tag ) ) {
$tag = trim( $tag );
$p_string = eregi_replace( "<($tag)[[:space:]]*>", "<\\1>", $p_string );
$p_string = eregi_replace( "<\/($tag)[[:space:]]*>", "</\\1>", $p_string );
$p_string = eregi_replace( "<($tag)[[:space:]]*\/>", "<\\1 />", $p_string );
}
}
return $p_string;
}
#===================================
# Advanced/Simple page selection
#===================================
# --------------------
# return the name of a bug page for the user
# account for the user preference and site override
#
# $p_action should be something like 'view', 'update', or 'report'
# If $p_user_id is null or not specified, use the current user
function string_get_bug_page( $p_action, $p_user_id=null ) {
if ( null === $p_user_id ) {
if ( auth_is_user_authenticated() ) {
$p_user_id = auth_get_current_user_id();
}
}
switch ( config_get( 'show_' . $p_action ) ) {
case BOTH:
if ( null !== $p_user_id &&
ON == user_pref_get_pref( $p_user_id, 'advanced_' . $p_action ) ) {
return 'bug_' . $p_action . '_advanced_page.php';
} else {
return 'bug_' . $p_action . '_page.php';
}
break;
case SIMPLE_ONLY:
return 'bug_' . $p_action . '_page.php';
break;
case ADVANCED_ONLY:
return 'bug_' . $p_action . '_advanced_page.php';
break;
}
}
# --------------------
# return an href anchor that links to a bug VIEW page for the given bug
# account for the user preference and site override
function string_get_bug_view_link( $p_bug_id, $p_user_id=null ) {
$t_summary = string_attribute( bug_get_field( $p_bug_id, 'summary' ) );
return '<a href="' . string_get_bug_view_url( $p_bug_id, $p_user_id ) . '" title="' . $t_summary . '">' . bug_format_id( $p_bug_id ) . '</a>';
}
# --------------------
# return the name and GET parameters of a bug VIEW page for the given bug
# account for the user preference and site override
function string_get_bug_view_url( $p_bug_id, $p_user_id=null ) {
return string_get_bug_view_page( $p_user_id ) . '?bug_id=' . bug_format_id( $p_bug_id );
}
# --------------------
# return the name of a bug VIEW page for the user
# account for the user preference and site override
function string_get_bug_view_page( $p_user_id=null ) {
return string_get_bug_page( 'view', $p_user_id );
}
# --------------------
# return an href anchor that links to a bug UPDATE page for the given bug
# account for the user preference and site override
function string_get_bug_update_link( $p_bug_id, $p_user_id=null ) {
$t_summary = string_attribute( bug_get_field( $p_bug_id, 'summary' ) );
return '<a href="' . string_get_bug_update_url( $p_bug_id, $p_user_id ) . '" title="' . $t_summary . '">' . bug_format_id( $p_bug_id ) . '</a>';
}
# --------------------
# return the name and GET parameters of a bug UPDATE page for the given bug
# account for the user preference and site override
function string_get_bug_update_url( $p_bug_id, $p_user_id=null ) {
return string_get_bug_update_page( $p_user_id ) . '?bug_id=' . bug_format_id( $p_bug_id );
}
# --------------------
# return the name of a bug UPDATE page for the user
# account for the user preference and site override
function string_get_bug_update_page( $p_user_id=null ) {
return string_get_bug_page( 'update', $p_user_id );
}
# --------------------
# return an href anchor that links to a bug REPORT page for the given bug
# account for the user preference and site override
function string_get_bug_report_link( $p_user_id=null ) {
return '<a href="' . string_get_bug_report_url( $p_user_id ) . '">' . lang_get( 'report_bug_link' ) . '</a>';
}
# --------------------
# return the name and GET parameters of a bug REPORT page for the given bug
# account for the user preference and site override
function string_get_bug_report_url( $p_user_id=null ) {
return string_get_bug_report_page( $p_user_id );
}
# --------------------
# return the name of a bug REPORT page for the user
# account for the user preference and site override
function string_get_bug_report_page( $p_user_id=null ) {
return string_get_bug_page( 'report', $p_user_id );
}
?>