* 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");
}
/**
}
/**
- * Construct the base URL http[s]://forge_name[:port]
+ * Return URL prefix (http:// or https://)
*
- * @return string base URL
+ * @return string URL prefix
*/
-function util_make_base_url() {
+function util_url_prefix() {
if (forge_get_config('use_ssl')) {
- $url = "https://" ;
+ return "https://";
} else {
- $url = "http://" ;
+ return "http://";
}
+}
+
+/**
+ * Construct the base URL http[s]://forge_name[:port]
+ *
+ * @return string base URL
+ */
+function util_make_base_url() {
+ $url = util_url_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
*
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);
+}
+
+/**
+ * util_sanitise_multiline_submission() – Convert text to ASCII CR-LF
+ *
+ * @param string $text
+ * input string to sanitise
+ * @return string
+ * sanitised string: CR, LF or CR-LF converted to CR-LF
+ */
+function util_sanitise_multiline_submission($text) {
+ /* convert all CR-LF into LF */
+ $text = preg_replace("/\015+\012+/m", "\012", $text);
+ /* convert all CR or LF into CR-LF */
+ $text = preg_replace("/[\012\015]/m", "\015\012", $text);
+
+ return $text;
+}
+
+/**
+ * util_create_file_with_contents() — Securely create (or replace) a file with given contents
+ *
+ * @param string $path Path of the file to be created
+ * @param string $contents Contents of the file
+ *
+ * @return boolean FALSE on error
+ */
+function util_create_file_with_contents($path, $contents) {
+ if (file_exists($path) && !unlink($path)) {
+ return false;
+ }
+ $handle = fopen($path, "x+");
+ if ($handle == false) {
+ return false;
+ }
+ fwrite($handle, $contents);
+ fclose($handle);
+ return true;
+}
+
+/**
+ * Create a directory in the system temp directory with a hard-to-predict name.
+ * Does not have the guarantees of the actual BSD libc function or Python tempfile function.
+ * @param string $suffix Append to the new directory's name
+ * @param string $prefix Prepend to the new directory's name
+ * @return string The path of the new directory.
+ *
+ * Mostly taken from https://gist.github.com/1407245 as a "temporary"
+ * workaround to https://bugs.php.net/bug.php?id=49211
+ */
+function util_mkdtemp($suffix = '', $prefix = 'tmp') {
+ $tempdir = sys_get_temp_dir();
+ for ($i=0; $i<5; $i++) {
+ $id = strtr(base64_encode(util_randbytes(6)), '+/', '-_');
+ $path = "{$tempdir}/{$prefix}{$id}{$suffix}";
+ if (mkdir($path, 0700)) {
+ return $path;
+ }
+ }
+ return false;
+}
+
+/**
+ * Run a function with only the permissions of a given Unix user
+ * Function can be an anonymous
+ * Optional arguments in an array
+ * @param string Unix user name
+ * @param function function to run (possibly anonymous)
+ * @param array parameters
+ * @return boolean true on success, false on error
+ */
+function util_sudo_effective_user($username, $function, $params=array()) {
+ $saved_egid = posix_getegid();
+ $saved_euid = posix_geteuid();
+
+ $userinfo = posix_getpwnam($username);
+ if ($userinfo === False) {
+ return False;
+ }
+ if (posix_setegid($userinfo['gid']) &&
+ ($saved_euid != 0 || posix_initgroups($username, $userinfo['gid'])) &&
+ posix_seteuid($userinfo['uid'])) {
+ $function($params);
+ }
+
+ posix_setegid($saved_egid);
+ posix_seteuid($saved_euid);
+ if ($saved_euid == 0)
+ posix_initgroups("root", 0);
+}
+
// Local Variables:
// mode: php
// c-file-style: "bsd"
// End:
-
-?>