[evolvis-commits] r17233: merge bm:taramir51/patches/common-functionality

mirabilos at evolvis.org mirabilos at evolvis.org
Fri May 13 18:13:58 CEST 2011


Author: mirabilos
Date: 2011-05-13 18:13:57 +0200 (Fri, 13 May 2011)
New Revision: 17233

Modified:
   trunk/gforge_base/evolvisforge-5.1/src/common/include/utils.php
Log:
merge bm:taramir51/patches/common-functionality

Modified: trunk/gforge_base/evolvisforge-5.1/src/common/include/utils.php
===================================================================
--- trunk/gforge_base/evolvisforge-5.1/src/common/include/utils.php	2011-05-13 16:13:54 UTC (rev 17232)
+++ trunk/gforge_base/evolvisforge-5.1/src/common/include/utils.php	2011-05-13 16:13:57 UTC (rev 17233)
@@ -5,7 +5,7 @@
  * Copyright 1999-2001, VA Linux Systems, Inc.
  * Copyright 2009-2010, Roland Mas
  * Copyright 2009-2010, Franck Villaume - Capgemini
- * Copyright 2010, Thorsten Glaser <t.glaser at tarent.de>
+ * Copyright 2010-2011, Thorsten Glaser <t.glaser at tarent.de>
  * Copyright (C) 2010-2011 Alain Peyrat - Alcatel-Lucent
  *
  * This file is part of FusionForge.
@@ -312,7 +312,7 @@
  *	to either a jabber account or email account or both, depending on
  *	user preferences
  *
- *	@param	array	array of user_id's from the user table
+ *	@param	array	array of user_ids from the user table
  *	@param	string	subject of the message
  *	@param	string	the message body
  *	@param	string	a comma-separated list of email address
@@ -503,7 +503,7 @@
 }
 
 /**
- * util_make_links() - Turn URL's into HREF's.
+ * util_make_links() - Turn URLs into HREFs.
  *
  * @param		string	The URL
  * @returns The HREF'ed URL
@@ -568,10 +568,10 @@
 			<br /><input type="checkbox" name="'.$name.'" value="'.$i.'"';
 		for ($j=0; $j<$checked_count; $j++) {
 			if ($i == $checked_array[$j]) {
-				echo ' checked';
+				echo ' checked="checked"';
 			}
 		}
-		echo '> '.$options[$i];
+		echo '/> '.$options[$i];
 	}
 }
 
@@ -720,7 +720,7 @@
  *
  * @param	int		The result set ID
  * @param	string	The title of the result set
- * @param	bool	The option to turn URL's into links
+ * @param	bool	The option to turn URLs into links
  * @param	bool	The option to display headers
  * @param	array	The db field name -> label mapping
  * @param	array   Don't display these cols
@@ -1156,7 +1156,7 @@
 }
 
 /**
- * Create URL for user's profile page
+ * Create URL for users' profile pages
  * 
  * @param string $username
  * @param int $user_id
@@ -1284,6 +1284,109 @@
 	}
 }
 
+if (!function_exists('quoted_printable_encode')) {
+	/* from http://www.php.net/manual/en/function.quoted-printable-encode.php */
+	function quoted_printable_encode($input, $line_max=75) {
+		$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
+		$lines = preg_split("/(?:\r\n|\r|\n)/", $input);
+		/* this was wrong, fixed --mirabilos */
+		$linebreak = "\r\n";
+		/*
+		 * the linebreak also counts as characters in the
+		 * mime_qp_long_line rule of spam-assassin
+		 */
+		$line_max = $line_max - strlen($linebreak);
+		$escape = "=";
+		$output = "";
+		$cur_conv_line = "";
+		$length = 0;
+		$whitespace_pos = 0;
+		$addtl_chars = 0;
+
+		// iterate lines
+		for ($j=0; $j<count($lines); $j++) {
+			$line = $lines[$j];
+			$linlen = strlen($line);
+
+			// iterate chars
+			for ($i = 0; $i < $linlen; $i++) {
+				$c = substr($line, $i, 1);
+				$dec = ord($c);
+
+				$length++;
+
+				if ($dec == 32) {
+					// space occurring at end of line, need to encode
+					if (($i == ($linlen - 1))) {
+						$c = "=20";
+						$length += 2;
+					}
+
+					$addtl_chars = 0;
+					$whitespace_pos = $i;
+				} elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) {
+					$h2 = floor($dec/16); $h1 = floor($dec%16);
+					$c = $escape . $hex["$h2"] . $hex["$h1"];
+					$length += 2;
+					$addtl_chars += 2;
+				}
+
+				// length for wordwrap exceeded, get a newline into the text
+				if ($length >= $line_max) {
+					$cur_conv_line .= $c;
+
+					// read only up to the whitespace for the current line
+					$whitesp_diff = $i - $whitespace_pos + $addtl_chars;
+
+					/*
+					 * the text after the whitespace will have to
+					 * be read again ( + any additional characters
+					 * that came into existence as a result of the
+					 * encoding process after the whitespace)
+					 *
+					 * Also, do not start at 0, if there was *no*
+					 * whitespace in the whole line
+					 */
+					if (($i + $addtl_chars) > $whitesp_diff) {
+						$output .= substr($cur_conv_line, 0,
+						    (strlen($cur_conv_line) - $whitesp_diff)) .
+						    $linebreak;
+						$i = $i - $whitesp_diff + $addtl_chars;
+					} else {
+						/* emit continuation --mirabilos */
+						$output .= $cur_conv_line .
+						    '=' . $linebreak;
+					}
+
+					$cur_conv_line = "";
+					$length = 0;
+					$whitespace_pos = 0;
+				} else {
+					// length for wordwrap not reached, continue reading
+					$cur_conv_line .= $c;
+				}
+			} // end of for
+
+			$length = 0;
+			$whitespace_pos = 0;
+			$output .= $cur_conv_line;
+			$cur_conv_line = "";
+
+			if ($j<=count($lines)-1) {
+				$output .= $linebreak;
+			}
+		} // end for
+
+		return trim($output);
+	}
+}
+
+/*-
+ * I have absolutely no idea how to write a programming language, I just
+ * kept adding the next logical step on the way.
+ * -- Rasmus Lerdorf
+ */
+
 /* returns an integer from http://forge/foo/bar.php/123 or false */
 function util_path_info_last_numeric_component() {
 	if (!isset($_SERVER['PATH_INFO']))
@@ -1362,6 +1465,10 @@
 	return $postfile;
 }
 
+function util_ifsetor(&$val, $default = false) {
+	return (isset($val) ? $val : $default);
+}
+
 function util_randbytes($num=6) {
 	$f = fopen("/dev/urandom", "rb");
 	$b = fread($f, $num);
@@ -1409,6 +1516,89 @@
 	}
 }
 
+/* convert '\n' to <br /> or </p><p> */
+function util_pwrap($encoded_string) {
+	return str_replace("<p></p>", "",
+	    str_replace("<p><br />", "<p>",
+	    "<p>" . str_replace("<br /><br />", "</p><p>",
+	    implode("<br />",
+	    explode("\n",
+	    $encoded_string
+	    ))) . "</p>"));
+}
+function util_ttwrap($encoded_string) {
+	return str_replace("<p><tt></tt></p>", "",
+	    str_replace("<p><tt><br />", "<p><tt>",
+	    "<p><tt>" . str_replace("<br /><br />", "</tt></p><p><tt>",
+	    implode("<br />",
+	    explode("\n",
+	    $encoded_string
+	    ))) . "</tt></p>"));
+}
+
+/* takes a string and returns it HTML encoded, URIs made to hrefs */
+function util_uri_grabber($unencoded_string) {
+	/* escape all ^A and ^B as ^BX^B and ^BY^B, respectively */
+	$s = str_replace("\x01", "\x02X\x02", str_replace("\x02", "\x02Y\x02",
+	    $unencoded_string));
+	/* replace all URIs with ^AURI^A */
+	$s = preg_replace(
+	    '|([a-zA-Z][a-zA-Z0-9+.-]*:[#0-9a-zA-Z;/?:@&=+$._!~*\'()%-]+)|',
+	    "\x01\$1\x01", $s);
+	if (!$s)
+		return htmlentities($unencoded_string, ENT_QUOTES, "UTF-8");
+	/* encode the string */
+	$s = htmlentities($s, ENT_QUOTES, "UTF-8");
+	/* convert 「^Afoo^A」 to 「<a href="foo">foo</a>」 */
+	$s = preg_replace('|\x01([^\x01]+)\x01|',
+	    '<a href="$1">$1</a>', $s);
+	if (!$s)
+		return htmlentities($unencoded_string, ENT_QUOTES, "UTF-8");
+	/* convert ^BX^B and ^BY^B back to ^A and ^B, respectively */
+	$s = str_replace("\x02Y\x02", "\x02", str_replace("\x02X\x02", "\x01",
+	    $s));
+	/* return the final result */
+	return $s;
+}
+
+/* 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 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;
+}
+
 // Local Variables:
 // mode: php
 // c-file-style: "bsd"



More information about the evolvis-commits mailing list