db->Quote($input);
}
function make_link($page, $query=null) {
	global $config;
	$base = $config->get_string('base_href');
	if(is_null($query)) {
		return "$base/$page";
	}
	else {
		if(strpos($base, "?")) {
			return "$base/$page&$query";
		}
		else {
			return "$base/$page?$query";
		}
	}
}
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;
	}
}
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";
	}
}
function get_memory_limit() {
	global $config;
	// thumbnail generation requires lots of memory
	$default_limit = 8*1024*1024;
	$shimmie_limit = parse_shorthand_int($config->get_int("thumb_gd_mem_limit"));
	if($shimmie_limit < 3*1024*1024) {
		// we aren't going to fit, override
		$shimmie_limit = $default_limit;
	}
	
	ini_set("memory_limit", $shimmie_limit);
	$memory = parse_shorthand_int(ini_get("memory_limit"));
	// changing of memory limit is disabled / failed
	if($memory == -1) {
		$memory = $default_limit; 
	}
	return $memory;
}
function bbcode2html($text) {
	$text = trim($text);
	$text = html_escape($text);
	$text = preg_replace("/\[b\](.*?)\[\/b\]/s", "\\1", $text);
	$text = preg_replace("/\[i\](.*?)\[\/i\]/s", "\\1", $text);
	$text = preg_replace("/\[u\](.*?)\[\/u\]/s", "\\1", $text);
	$text = preg_replace("/\[\[(.*?)\]\]/s", 
		"\\1", $text);
	$text = str_replace("\n", "\n
", $text);
	return $text;
}
function strip_bbcode($text) {
	$text = trim($text);
	$text = html_escape($text);
	$text = preg_replace("/\[b\](.*?)\[\/b\]/s", "\\1", $text);
	$text = preg_replace("/\[i\](.*?)\[\/i\]/s", "\\1", $text);
	$text = preg_replace("/\[u\](.*?)\[\/u\]/s", "\\1", $text);
	$text = preg_replace("/\[\[(.*?)\]\]/s", "\\1", $text);
	return $text;
}
function tag_explode($tags) {
	if(is_string($tags)) {
		$tags = explode(' ', $tags);
	}
	else if(is_array($tags)) {
		// do nothing
	}
	else {
		die("tag_explode only takes strings or arrays");
	}
	$tags = array_map("trim", $tags);
	foreach($tags as $tag) {
		if(is_string($tag) && strlen($tag) > 0) {
			$tag_array[] = $tag;
		}
	}
	if(count($tag_array) == 0) {
		$tag_array = array("tagme");
	}
	return $tag_array;
}
function sql_quote($text) {
	return '"'.sql_escape($text).'"';
}
function get_thumbnail_size($orig_width, $orig_height) {
	global $config;
	$max_width  = $config->get_int('thumb_width');
	$max_height = $config->get_int('thumb_height');
	$xscale = ($max_height / $orig_height);
	$yscale = ($max_width / $orig_width);
	$scale = ($xscale < $yscale) ? $xscale : $yscale;
//	if($scale >= 1) {
//		return array($orig_width, $orig_height);
//	}
//	else {
		return array($orig_width*$scale, $orig_height*$scale);
//	}
}
function build_thumb($image, $query=null) {
	global $config;
	$h_view_link = make_link("post/view/{$image->id}", $query);
	$h_tip = html_escape($image->get_tooltip());
	$h_thumb_link = $image->get_thumb_link();
	$tsize = get_thumbnail_size($image->width, $image->height);
	return "