escape($input); } /** * Turn all manner of HTML / INI / JS / DB booleans into a PHP one * * @param $input * @return bool */ function bool_escape($input) { /* Sometimes, I don't like PHP -- this, is one of those times... "a boolean FALSE is not considered a valid boolean value by this function." Yay for Got'chas! http://php.net/manual/en/filter.filters.validate.php */ if (is_bool($input)) { return $input; } else if (is_numeric($input)) { return ($input === 1); } else { $value = filter_var($input, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); if (!is_null($value)) { return $value; } else { $input = strtolower( trim($input) ); return ( $input === "y" || $input === "yes" || $input === "t" || $input === "true" || $input === "on" || $input === "1" ); } } } /** * Some functions require a callback function for escaping, * but we might not want to alter the data * * @param $input * @return string */ function no_escape($input) { return $input; } /** * @param string $name * @param array $attrs * @param array $children * @return string */ function xml_tag($name, $attrs=array(), $children=array()) { $xml = "<$name "; foreach($attrs as $k => $v) { $xv = str_replace(''', ''', htmlspecialchars($v, ENT_QUOTES)); $xml .= "$k=\"$xv\" "; } if(count($children) > 0) { $xml .= ">\n"; foreach($children as $child) { $xml .= xml_tag($child); } $xml .= "$name>\n"; } else { $xml .= "/>\n"; } return $xml; } // Original PHP code by Chirp Internet: www.chirp.com.au // Please acknowledge use of this code by including this header. function truncate($string, $limit, $break=" ", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) <= $limit) return $string; // is $break present between $limit and the end of the string? if(false !== ($breakpoint = strpos($string, $break, $limit))) { if($breakpoint < strlen($string) - 1) { $string = substr($string, 0, $breakpoint) . $pad; } } return $string; } /** * Turn a human readable filesize into an integer, eg 1KB -> 1024 * * @param $limit * @return 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])) { /** @noinspection PhpMissingBreakStatementInspection */ case 'g': $value *= 1024; // fall through /** @noinspection PhpMissingBreakStatementInspection */ case 'm': $value *= 1024; // fall through /** @noinspection PhpMissingBreakStatementInspection */ 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 * * @param $int * @return 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 * * @param $date * @param bool $html * @return 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 ) * * @param $dateTime * @return bool */ 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 ) * * @param $date * @return bool */ 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; } function validate_input($inputs) { $outputs = array(); foreach($inputs as $key => $validations) { $flags = explode(',', $validations); if(in_array('optional', $flags)) { if(!isset($_POST[$key])) { continue; } } if(!isset($_POST[$key])) { throw new InvalidInput("Input '$key' not set"); } $value = $_POST[$key]; if(in_array('user_id', $flags)) { $id = int_escape($value); if(in_array('exists', $flags)) { if(is_null(User::by_id($id))) { throw new InvalidInput("User #$id does not exist"); } } $outputs[$key] = $id; } else if(in_array('user_name', $flags)) { if(strlen($value) < 1) { throw new InvalidInput("Username must be at least 1 character"); } else if(!preg_match('/^[a-zA-Z0-9-_]+$/', $value)) { throw new InvalidInput( "Username contains invalid characters. Allowed characters are ". "letters, numbers, dash, and underscore"); } $outputs[$key] = $value; } else if(in_array('user_class', $flags)) { global $_shm_user_classes; if(!array_key_exists($value, $_shm_user_classes)) { throw new InvalidInput("Invalid user class: ".html_escape($value)); } $outputs[$key] = $value; } else if(in_array('email', $flags)) { $outputs[$key] = $value; } else if(in_array('password', $flags)) { $outputs[$key] = $value; } else { throw new InvalidInput("Unknown validation '$validations'"); } } return $outputs; } /** * 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 * * @param $ip * @param $ban_reason * @return 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; } /** * Checks if a given string contains another at the beginning. * * @param $haystack String to examine. * @param $needle String to look for. * @return 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. * @return 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" * * @param null|string $page * @param null|string $query * @return 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)) { $base = str_replace('/'.basename($_SERVER["SCRIPT_FILENAME"]), "", $_SERVER["PHP_SELF"]); } 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 parameters * * @param $changes * @return 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_query(); } 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 * * @param string $link * @return string */ function make_http(/*string*/ $link) { if(strpos($link, "://") > 0) { return $link; } if(strlen($link) > 0 && $link[0] != '/') { $link = get_base_href() . '/' . $link; } $protocol = is_https_enabled() ? "https://" : "http://"; $link = $protocol . $_SERVER["HTTP_HOST"] . $link; $link = str_replace("/./", "/", $link); return $link; } /** * Make a form tag with relevant auth token and stuff * * @param string $target * @param string $method * @param bool $multipart * @param string $form_id * @param string $onsubmit * * @return 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 '