db->Quote($input); } /** * Some functions require a callback function for escaping, * but we might not want to alter the data * * @retval string */ function no_escape($input) { return $input; } /** * Turn a human readable filesize into an integer, eg 1KB -> 1024 * * @retval int */ function parse_shorthand_int($limit) { if(is_numeric($limit)) { return (int)$limit; } if(preg_match('/^([\d\.]+)([gmk])?b?$/i', "$limit", $m)) { $value = $m[1]; if (isset($m[2])) { switch(strtolower($m[2])) { case 'g': $value *= 1024; # fallthrough case 'm': $value *= 1024; # fallthrough case 'k': $value *= 1024; break; default: $value = -1; } } return (int)$value; } else { return -1; } } /** * Turn an integer into a human readable filesize, eg 1024 -> 1KB * * @retval string */ function to_shorthand_int($int) { if($int >= pow(1024, 3)) { return sprintf("%.1fGB", $int / pow(1024, 3)); } else if($int >= pow(1024, 2)) { return sprintf("%.1fMB", $int / pow(1024, 2)); } else if($int >= 1024) { return sprintf("%.1fKB", $int / 1024); } else { return "$int"; } } /** * Turn a date into a time, a date, an "X minutes ago...", etc * * @retval string */ function autodate($date, $html=true) { global $config; $format = $config->get_string('autodate_format', "F j, Y"); $seconds = time() - strtotime($date); $ago = seconds_to_time($seconds) . " ago"; $on = "on " . date($format, strtotime($date)); if($config->get_bool('use_autodate', true)) { return ($html ? "$ago" : $ago); } else { return $on; } } /** * Turn a number of seconds into a vague human timespan, eg 142 -> "2 minutes" * * @retval string */ function seconds_to_time($seconds) { if($seconds<60) return $seconds . " second" . plural($seconds); $seconds = round($seconds/60); if($seconds<60) return $seconds . " minute" . plural($seconds); $seconds = round($seconds/60); if($seconds<24) return $seconds . " hour" . plural($seconds); $seconds = round($seconds/24); if($seconds<7) return $seconds . " day" . plural($seconds); $seconds = round($seconds/7); if($seconds<4) return $seconds . " week" . plural($seconds); $seconds = round($seconds/4); if($seconds<12) return $seconds . " month" . plural($seconds); $seconds = round($seconds/12); return $seconds . " year" . plural($seconds); } /** * Return a pluraliser if necessary * * @retval string */ function plural($num, $single_form="", $plural_form="s") { return ($num == 1) ? $single_form : $plural_form; } /** * Different databases have different ways to represent booleans; this * will try and standardise them */ function undb_bool($val) { if($val === true || $val == 'Y' || $val == 'y' || $val == 'T' || $val == 't' || $val === 1) return true; if($val === false || $val == 'N' || $val == 'n' || $val == 'F' || $val == 'f' || $val === 0) return false; } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ * HTML Generation * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /** * Figure out the correct way to link to a page, taking into account * things like the nice URLs setting. * * eg make_link("post/list") becomes "/v2/index.php?q=post/list" * * @retval string */ function make_link($page=null, $query=null) { global $config; if(is_null($page)) $page = $config->get_string('main_page'); if($config->get_bool('nice_urls', false)) { #$full = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"]; $full = $_SERVER["PHP_SELF"]; $base = str_replace("/index.php", "", $full); } else { $base = "./index.php?q="; } if(is_null($query)) { return str_replace("//", "/", "$base/$page"); } else { if(strpos($base, "?")) { return "$base/$page&$query"; } else if(strpos($query, "#") === 0) { return "$base/$page$query"; } else { return "$base/$page?$query"; } } } /** * Turn a relative link into an absolute one, including hostname * * @retval string */ function make_http($link) { if(strpos($link, "ttp://") > 0) return $link; if(strlen($link) > 0 && $link[0] != '/') $link = get_base_href().'/'.$link; $link = "http://".$_SERVER["HTTP_HOST"].$link; $link = str_replace("/./", "/", $link); return $link; } /** * Make a form tag with relevant auth token and stuff * * @retval string */ function make_form($target, $method="POST", $multipart=False) { global $user; $auth = $user->get_auth_html(); $extra = ""; if($multipart) { $extra .= " enctype='multipart/form-data'"; } return "