* Copyright 1999-2001, VA Linux Systems, Inc.
* Copyright 2009-2011, Roland Mas
* Copyright 2009-2011, Franck Villaume - Capgemini
- * Copyright (c) 2010, 2011
+ * Copyright (c) 2010, 2011, 2012
* Thorsten Glaser <t.glaser@tarent.de>
* Copyright 2010-2011, Alain Peyrat - Alcatel-Lucent
*
*
*/
function util_unconvert_htmlspecialchars($string) {
- if (strlen($string) < 1) {
- return '';
- } else {
- //$trans = get_html_translation_table(HTMLENTITIES, ENT_QUOTES);
- $trans = get_html_translation_table(HTML_ENTITIES);
- $trans = array_flip ($trans);
- $str = strtr ($string, $trans);
- return $str;
- }
+ return html_entity_decode($string, ENT_QUOTES, "UTF-8");
}
/**
if(empty($data)) {
return $data;
}
- $lines = explode("\n", $data);
+ $withPattern = 0;
+ for ($i = 0; $i < 5; $i++) {
+ $randPattern = rand(10000, 30000);
+ if (! preg_match("/$randPattern/", $data)) {
+ $withPattern = 1;
+ break;
+ }
+ }
+ if ($withPattern) {
+/*
+ while(preg_match('/<a [^>]*>[^<]*<\/a>/i', $data, $part)) {
+ $mem[] = $part[0];
+ $data = preg_replace('/<a [^>]*>[^<]*<\/a>/i', $randPattern, $data, 1);
+ }
+*/
+ $mem = array();
+ while(preg_match('/<a [^>]*>.*<\/a>/siU', $data, $part)) {
+ $mem[] = $part[0];
+ $data = preg_replace('/<a [^>]*>.*<\/a>/siU', $randPattern, $data, 1);
+ }
+ while(preg_match('/<img [^>]*\/>/siU', $data, $part)) {
+ $mem[] = $part[0];
+ $data = preg_replace('/<img [^>]*\/>/siU', $randPattern, $data, 1);
+ }
+ $data = str_replace('>', "\1", $data);
+ $data = preg_replace("#([ \t]|^)www\.#i"," http://www.",$data);
+ $data = preg_replace("#([[:alnum:]]+)://([^[:space:]<\1]*)([[:alnum:]\#?/&=])#i", "<a href=\"\\1://\\2\\3\" target=\"_new\">\\1://\\2\\3</a>", $data);
+ $data = preg_replace("#([[:space:]]|^)(([a-z0-9_]|\\-|\\.)+@([^[:space:]<\1]*)([[:alnum:]-]))#i", "\\1<a href=\"mailto:\\2\" target=\"_new\">\\2</a>", $data);
+ $data = str_replace("\1", '>', $data);
+ for ($i = 0; $i < count($mem); $i++) {
+ $data = preg_replace("/$randPattern/", $mem[$i], $data, 1);
+ }
+ return($data);
+ }
+
+ $lines = split("\n",$data);
$newText = "";
while ( list ($key, $line) = each ($lines)) {
+ // Do not scan lines if they already have hyperlinks.
+ // Avoid problem with text written with an WYSIWYG HTML editor.
+ if (eregi('<a ([^>]*)>.*</a>', $line, $linePart)) {
+ if (eregi('href="[^"]*"', $linePart[1])) {
+ $newText .= $line;
+ continue;
+ }
+ }
+
+ // Skip </img> tag also
+ if (eregi('<img ([^>]*)/>', $line, $linePart)) {
+ if (eregi('href="[^"]*"', $linePart[1])) {
+ $newText .= $line;
+ continue;
+ }
+ }
+
// When we come here, we usually have form input
// encoded in entities. Our aim is to NOT include
// angle brackets in the URL
// (RFC2396; http://www.w3.org/Addressing/URL/5.1_Wrappers.html)
$line = str_replace('>', "\1", $line);
- $line = preg_replace( "/([ \t]|^)www\./i", " http://www.", $line);
- $text = preg_replace( "/([[:alnum:]]+):\/\/([^[:space:]<\1]*)([[:alnum:]#?\/&=])/i",
+ $line = preg_replace("/([ \t]|^)www\./i", " http://www.", $line);
+ $line = preg_replace("/([[:alnum:]]+):\/\/([^[:space:]<\1]*)([[:alnum:]#?\/&=])/i",
"<a href=\"\\1://\\2\\3\" target=\"_new\">\\1://\\2\\3</a>", $line);
- $text = preg_replace(
+ $line = preg_replace(
"/([[:space:]]|^)(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))/i",
"\\1<a href=\"mailto:\\2\" target=\"_new\">\\2</a>",
- $text
+ $line
);
- $text = str_replace("\1", '>', $text);
- $newText .= $text;
+ $line = str_replace("\1", '>', $line);
+ $newText .= $line;
}
return $newText;
}
*
*/
function GraphIt($name_string, $value_string, $title) {
- GLOBAL $HTML;
+ global $HTML;
$counter = count($name_string);
return $prefix ;
}
+/**
+ * Return URL prefix (http:// or https://)
+ *
+ * @param string $prefix (optional) : 'http' or 'https' to force it
+ * @return string URL prefix
+ */
+function util_url_prefix($prefix = '') {
+ if ($prefix == 'http' || $prefix == 'https' ) {
+ return $prefix . '://';
+ }
+ else {
+ if (forge_get_config('use_ssl')) {
+ return "https://";
+ } else {
+ return "http://";
+ }
+ }
+}
+
/**
* Construct the base URL http[s]://forge_name[:port]
*
+ * @param string $prefix (optional) : 'http' or 'https' to force it
* @return string base URL
*/
-function util_make_base_url() {
- if (forge_get_config('use_ssl')) {
- $url = "https://" ;
- } else {
- $url = "http://" ;
- }
+function util_make_base_url($prefix = '') {
+ $url = util_url_prefix($prefix);
$url .= forge_get_config('web_host') ;
if (forge_get_config('https_port') && (forge_get_config('https_port') != 443)) {
$url .= ":".forge_get_config('https_port') ;
}
return $url;
}
+
/**
* Construct full URL from a relative path
*
- * @param string $path
+ * @param string $path (optional)
+ * @param string $prefix (optional) : 'http' or 'https' to force it
* @return string URL
*/
-function util_make_url($path = '') {
- $url = util_make_base_url().util_make_uri($path) ;
+function util_make_url($path = '', $prefix = '') {
+ $url = util_make_base_url($prefix).util_make_uri($path) ;
return $url;
}
* @return string
*/
function util_make_link_u ($username, $user_id,$text) {
- return '<a href="' . util_make_url_u ($username, $user_id) . '">' . $text . '</a>' ;
+ return '<a href="' . util_make_url_u ($username, $user_id) . '">' . $text . '</a>';
}
/**
function util_make_link_g ($groupname, $group_id,$text) {
$hook_params =array();
$hook_params['resource_type'] = 'group';
- $hook_params['group_name'] = $groupname;
+ $hook_params['group_name'] = $groupname;
$hook_params['group_id'] = $group_id;
$hook_params['link_text'] = $text;
- $hook_params['group_link'] = '';
- plugin_hook_by_reference('project_link_with_tooltip', $hook_params);
- if($hook_params['group_link'] != '') {
- return $hook_params['group_link'];
- }
+ $hook_params['group_link'] = '';
+ plugin_hook_by_reference('project_link_with_tooltip', $hook_params);
+ if($hook_params['group_link'] != '') {
+ return $hook_params['group_link'];
+ }
return '<a href="' . util_make_url_g ($groupname, $group_id) . '">' . $text . '</a>' ;
}
return $s;
}
+function util_html_encode($s) {
+ return htmlspecialchars($s, ENT_QUOTES, "UTF-8");
+}
+
/* secure a (possibly already HTML encoded) string */
function util_html_secure($s) {
- return htmlentities(html_entity_decode($s, ENT_QUOTES, "UTF-8"),
- ENT_QUOTES, "UTF-8");
+ return util_html_encode(util_unconvert_htmlspecialchars($s));
+}
+
+/* return integral value (ℕ₀) of passed string if it matches, or false */
+function util_nat0(&$s) {
+ if (!isset($s)) {
+ /* unset variable */
+ return false;
+ }
+ if (is_array($s)) {
+ if (count($s) == 1) {
+ /* one-element array */
+ return util_nat0($s[0]);
+ }
+ /* not one element, or element not at [0] */
+ return false;
+ }
+ if (!is_numeric($s)) {
+ /* not numeric */
+ return false;
+ }
+ $num = (int)$s;
+ if ($num >= 0) {
+ /* number element of ℕ₀ */
+ $text = (string)$num;
+ if ($text == $s) {
+ /* number matches its textual representation */
+ return ($num);
+ }
+ /* doesn't match, like 0123 or 1.2 or " 1" */
+ }
+ /* or negative */
+ return false;
}
/**
return $content_type;
}
+/**
+ * util_gethref() - Construct a hypertext reference
+ *
+ * @param string $baseurl
+ * (optional) base URL (absolute or relative);
+ * urlencoded, but not htmlencoded
+ * (default (falsy): PHP_SELF)
+ * @param array $args
+ * (optional) associative array of unencoded query parameters;
+ * false values are ignored
+ * @param bool $ashtml
+ * (optional) htmlencode the result?
+ * (default: true)
+ * @param string $sep
+ * (optional) argument separator ('&' or ';')
+ * (default: '&')
+ * @return string
+ * URL, possibly htmlencoded
+ */
+function util_gethref($baseurl=false, $args=array(), $ashtml=true, $sep='&') {
+ $rv = $baseurl ? $baseurl : getStringFromServer('PHP_SELF');
+ $pfx = '?';
+ foreach ($args as $k => $v) {
+ if ($v === false) {
+ continue;
+ }
+ $rv .= $pfx . urlencode($k) . '=' . urlencode($v);
+ $pfx = $sep;
+ }
+ return ($ashtml ? util_html_encode($rv) : $rv);
+}
+
// Local Variables:
// mode: php
// c-file-style: "bsd"
// End:
-
-?>