db->Quote($input); } /** * Turn all manner of HTML / INI / JS / DB booleans into a PHP one * * @retval boolean */ function bool_escape($input) { $input = strtolower($input); return ( $input === "y" || $input === "yes" || $input === "t" || $input === "true" || $input === "on" || $input === 1 || $input === true ); } /** * 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', (string)$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 (string)$int; } } /** * Turn a date into a time, a date, an "X minutes ago...", etc * * @retval string */ function autodate($date, $html=true) { $cpu = date('c', strtotime($date)); $hum = date('F j, Y; H:i', strtotime($date)); return ($html ? "" : $hum); } /** * Check if a given string is a valid date-time. ( Format: yyyy-mm-dd hh:mm:ss ) * * @retval boolean */ function isValidDateTime($dateTime) { if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) { if (checkdate($matches[2], $matches[3], $matches[1])) { return true; } } return false; } /** * Check if a given string is a valid date. ( Format: yyyy-mm-dd ) * * @retval boolean */ function isValidDate($date) { if (preg_match("/^(\d{4})-(\d{2})-(\d{2})$/", $date, $matches)) { // checkdate wants (month, day, year) if (checkdate($matches[2], $matches[3], $matches[1])) { return true; } } return false; } /** * Return a pluraliser if necessary * * @retval string */ function plural($num, $single_form="", $plural_form="s") { return ($num == 1) ? $single_form : $plural_form; } /** * Give a HTML string which shows an IP (if the user is allowed to see IPs), * and a link to ban that IP (if the user is allowed to ban IPs) * * FIXME: also check that IP ban ext is installed * * @retval string */ function show_ip($ip, $ban_reason) { global $user; $u_reason = url_escape($ban_reason); $u_end = url_escape("+1 week"); $ban = $user->can("ban_ip") ? ", Ban" : ""; $ip = $user->can("view_ip") ? $ip.$ban : ""; return $ip; } /** * Turn an IP address into a colour, for easily spotting samefags * * NOTE: this should only be shown to admins, as it can be reversed * to get an original IP address */ function ip2color($ip) { $b = explode(".", $ip); #return sprintf("#%02x%02x%02x", $b[0]/2, $b[1]/2, $b[2]/2); return sprintf("hsl(%d, %d%%, %d%%)", $b[3]*360/256, $b[2]*100/256, $b[1]*100/256/2); } /** * 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; } /** * Checks if a given string contains another at the beginning. * * @param $haystack String to examine. * @param $needle String to look for. * @retval bool */ function startsWith(/*string*/ $haystack, /*string*/ $needle) { $length = strlen($needle); return (substr($haystack, 0, $length) === $needle); } /** * Checks if a given string contains another at the end. * * @param $haystack String to examine. * @param $needle String to look for. * @retval bool */ function endsWith(/*string*/ $haystack, /*string*/ $needle) { $length = strlen($needle); $start = $length * -1; //negative return (substr($haystack, $start) === $needle); } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ * 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(NICE_URLS || $config->get_bool('nice_urls', false)) { #$full = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"]; $full = $_SERVER["PHP_SELF"]; $base = str_replace("/".basename($_SERVER["SCRIPT_FILENAME"]), "", $full); } else { $base = "./".basename($_SERVER["SCRIPT_FILENAME"])."?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; } } } /** * Take the current URL and modify some paramaters * * @retval string */ function modify_current_url($changes) { return modify_url($_SERVER['QUERY_STRING'], $changes); } function modify_url($url, $changes) { // SHIT: PHP is officially the worst web API ever because it does not // have a built-in function to do this. // SHIT: parse_str is magically retarded; not only is it a useless name, it also // didn't return the parsed array, preferring to overwrite global variables with // whatever data the user supplied. Thankfully, 4.0.3 added an extra option to // give it an array to use... $params = array(); parse_str($url, $params); if(isset($changes['q'])) { $base = $changes['q']; unset($changes['q']); } else { $base = $_GET['q']; } if(isset($params['q'])) { unset($params['q']); } foreach($changes as $k => $v) { if(is_null($v) and isset($params[$k])) unset($params[$k]); $params[$k] = $v; } return make_link($base, http_build_query($params)); } /** * Turn a relative link into an absolute one, including hostname * * @retval string */ function make_http(/*string*/ $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 * (Added optional Form ID field for helping jquery.) * * @retval string */ function make_form($target, $method="POST", $multipart=False, $form_id="", $onsubmit="") { global $user; $auth = $user->get_auth_html(); $extra = empty($form_id) ? '' : 'id="'. $form_id .'"'; if($multipart) { $extra .= " enctype='multipart/form-data'"; } if($onsubmit) { $extra .= ' onsubmit="'.$onsubmit.'"'; } return '