return true;
}
+function util_strip_unprintable(&$data) {
+ if (is_array($data)) {
+ foreach ($data as $key => &$value) {
+ util_strip_unprintable($value);
+ }
+ }
+ else {
+ $data = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $data);
+ }
+ return $data;
+}
+
/**
* removeCRLF() - remove any Carriage Return-Line Feed from a string.
* That function is useful to remove the possibility of a CRLF Injection when sending mail
* @returns true on success/false on error
*
*/
-function validate_email($address) {
- if ( preg_match( "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\.\/0-9=?A-Z^_`a-z{|}~]+$/", $address) ) {
- return true;
- } else {
- return false;
+function validate_email ($address) {
+ if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
+ if ( filter_var($address, FILTER_VALIDATE_EMAIL) ) {
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+ else {
+ if ( preg_match( "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\.\/0-9=?A-Z^_`a-z{|}~]+$/", $address) ) {
+ return true;
+ } else {
+ return false;
+ }
}
}
* @param array labels strings associated to each 2^10 or
* 10^3(base10==true) multiple of base units
*/
-function human_readable_bytes($bytes, $base10=false, $round=0, $labels=array(' bytes', ' KB', ' MB', ' GB')) {
- if ($bytes <= 0 || !is_array($labels) || (count($labels) <= 0)) {
- return null;
- }
- $step = $base10 ? 3 : 10;
- $base = $base10 ? 10 : 2;
+function human_readable_bytes ($bytes, $base10=false, $round=0, $labels=array()) {
+ if ($bytes == 0) {
+ return "0";
+ }
+ if ($bytes < 0) {
+ return "-" . human_readable_bytes(-$bytes, $base10, $round);
+ }
+ if ($base10) {
+ $labels = array(_('bytes'), _('kB'), _('MB'), _('GB'), _('TB'));
+ $step = 3;
+ $base = 10;
+ } else {
+ $labels = array(_('bytes'), _('KiB'), _('MiB'), _('GiB'), _('TiB'));
+ $step = 10;
+ $base = 2;
+ }
$log = (int)(log10($bytes)/log10($base));
krsort($labels);
foreach ($labels as $p=>$lab) {
if ($log < $pow) {
continue;
}
- if ($lab == " MB" or $lab == " GB") {
+ if ($lab != _("bytes") and $lab != _("kB") and $lab != _("KiB")) {
$round = 2;
}
- $text = round($bytes/pow($base,$pow),$round).$lab;
+ $text = round($bytes/pow($base,$pow),$round) . " " . $lab;
break;
}
return $text;
$url = "http://" ;
}
$url .= forge_get_config('web_host') ;
- if (forge_get_config('https_port') != 443) {
+ if (forge_get_config('https_port') && (forge_get_config('https_port') != 443)) {
$url .= ":".forge_get_config('https_port') ;
}
return $url;
function util_get_compressed_file_extension() {
$m = forge_get_config('compression_method');
- if (preg_match ('/^gzip /', $m)) {
+ if (preg_match ('/^gzip\b/', $m)) {
return '.gz';
- } elseif (preg_match ('/^bzip2 /', $m)) {
+ } elseif (preg_match ('/^bzip2\b/', $m)) {
return '.bzip2';
- } elseif (preg_match ('/^lzma /', $m)) {
+ } elseif (preg_match ('/^lzma\b/', $m)) {
return '.lzma';
- } elseif (preg_match ('/^xz /', $m)) {
+ } elseif (preg_match ('/^xz\b/', $m)) {
return '.xz';
- } elseif (preg_match ('/^cat /', $m)) {
+ } elseif (preg_match ('/^cat\b/', $m)) {
return '';
} else {
return '.compressed';
ENT_QUOTES, "UTF-8");
}
+/**
+ * TODO: Enter description here ...
+ * @param unknown_type $script
+ * @param unknown_type $default_content_type
+ * @return Ambiguous
+ */
+function util_negociate_alternate_content_types($script, $default_content_type) {
+
+ // Invoke plugins' hooks 'script_accepted_types' to discover which alternate content types they would accept for /users/...
+ $hook_params = array();
+ $hook_params['script'] = $script;
+ $hook_params['accepted_types'] = array();
+ plugin_hook_by_reference('script_accepted_types', $hook_params);
+ if (count($hook_params['accepted_types'])) {
+ // By default, text/html is accepted
+ $accepted_types = array($default_content_type);
+ $new_accepted_types = $hook_params['accepted_types'];
+ $accepted_types = array_merge($accepted_types, $new_accepted_types);
+
+ // PEAR::HTTP (for negotiateMimeType())
+ require_once('HTTP.php');
+
+ // negociate accepted content-type depending on the preferred ones declared by client
+ $http=new HTTP();
+ $content_type = $http->negotiateMimeType($accepted_types, false);
+ } else {
+ $content_type = $default_content_type;
+ }
+ return $content_type;
+}
+
// Local Variables:
// mode: php
// c-file-style: "bsd"