2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2007-04-28 19:28:29 +00:00
|
|
|
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Input / Output Sanitising *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
function html_escape($input) {
|
|
|
|
return htmlentities($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
function int_escape($input) {
|
|
|
|
return (int)$input;
|
|
|
|
}
|
|
|
|
|
2007-05-23 03:44:15 +00:00
|
|
|
function url_escape($input) {
|
|
|
|
$input = rawurlencode($input);
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
function sql_escape($input) {
|
|
|
|
global $database;
|
|
|
|
return $database->db->Quote($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
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");
|
|
|
|
}
|
2007-04-24 19:12:05 +00:00
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
$tags = array_map("trim", $tags);
|
|
|
|
|
2007-06-26 11:47:30 +00:00
|
|
|
$tag_array = array();
|
2007-04-28 19:28:29 +00:00
|
|
|
foreach($tags as $tag) {
|
|
|
|
if(is_string($tag) && strlen($tag) > 0) {
|
|
|
|
$tag_array[] = $tag;
|
|
|
|
}
|
2007-04-24 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
if(count($tag_array) == 0) {
|
|
|
|
$tag_array = array("tagme");
|
2007-04-24 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
return $tag_array;
|
2007-04-24 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* HTML Generation *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
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 bbcode_to_html($text) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$text = trim($text);
|
|
|
|
$text = html_escape($text);
|
2007-04-26 22:27:17 +00:00
|
|
|
$text = preg_replace("/\[b\](.*?)\[\/b\]/s", "<b>\\1</b>", $text);
|
|
|
|
$text = preg_replace("/\[i\](.*?)\[\/i\]/s", "<i>\\1</i>", $text);
|
|
|
|
$text = preg_replace("/\[u\](.*?)\[\/u\]/s", "<u>\\1</u>", $text);
|
2007-05-17 03:49:23 +00:00
|
|
|
$text = preg_replace("/\[code\](.*?)\[\/code\]/s", "<pre>\\1</pre>", $text);
|
2007-05-03 15:19:02 +00:00
|
|
|
$text = preg_replace("/>>(\d+)/s",
|
|
|
|
"<a href='".make_link("post/view/\\1")."'>>>\\1</a>", $text);
|
2007-05-18 00:55:47 +00:00
|
|
|
$text = preg_replace("/\[url=((?:https?|ftp|irc):\/\/.*?)\](.*?)\[\/url\]/s", "<a href='\\1'>\\2</a>", $text);
|
|
|
|
$text = preg_replace("/\[url\]((?:https?|ftp|irc):\/\/.*?)\[\/url\]/s", "<a href='\\1'>\\1</a>", $text);
|
2007-04-26 22:27:17 +00:00
|
|
|
$text = preg_replace("/\[\[(.*?)\]\]/s",
|
|
|
|
"<a href='".make_link("wiki/\\1")."'>\\1</a>", $text);
|
2007-04-16 11:58:25 +00:00
|
|
|
$text = str_replace("\n", "\n<br>", $text);
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
function bbcode_to_text($text) {
|
2007-04-26 22:27:17 +00:00
|
|
|
$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);
|
2007-05-17 03:49:23 +00:00
|
|
|
$text = preg_replace("/\[code\](.*?)\[\/code\]/s", "\\1", $text);
|
2007-05-18 00:55:47 +00:00
|
|
|
$text = preg_replace("/\[url=(.*?)\](.*?)\[\/url\]/s", "\\2", $text);
|
|
|
|
$text = preg_replace("/\[url\](.*?)\[\/url\]/s", "\\1", $text);
|
2007-04-26 22:27:17 +00:00
|
|
|
$text = preg_replace("/\[\[(.*?)\]\]/s", "\\1", $text);
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
function build_thumb_html($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);
|
2007-05-01 12:41:09 +00:00
|
|
|
return "<a href='$h_view_link'><img title='$h_tip' alt='$h_tip'
|
|
|
|
width='{$tsize[0]}' height='{$tsize[1]}' src='$h_thumb_link' /></a>\n";
|
2007-04-28 19:28:29 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Input sanitising *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
function get_memory_limit() {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
// thumbnail generation requires lots of memory
|
|
|
|
$default_limit = 8*1024*1024;
|
2007-07-05 20:16:10 +00:00
|
|
|
$shimmie_limit = parse_shorthand_int($config->get_int("thumb_mem_limit"));
|
2007-04-28 19:28:29 +00:00
|
|
|
if($shimmie_limit < 3*1024*1024) {
|
|
|
|
// we aren't going to fit, override
|
|
|
|
$shimmie_limit = $default_limit;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-04-28 19:28:29 +00:00
|
|
|
|
|
|
|
ini_set("memory_limit", $shimmie_limit);
|
|
|
|
$memory = parse_shorthand_int(ini_get("memory_limit"));
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
// changing of memory limit is disabled / failed
|
|
|
|
if($memory == -1) {
|
|
|
|
$memory = $default_limit;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2007-05-17 03:59:48 +00:00
|
|
|
assert($memory > 0);
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
return $memory;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
2007-05-01 13:30:05 +00:00
|
|
|
* Misc *
|
2007-04-28 19:28:29 +00:00
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-04-25 18:33:59 +00:00
|
|
|
function get_thumbnail_size($orig_width, $orig_height) {
|
|
|
|
global $config;
|
2007-05-03 15:19:02 +00:00
|
|
|
|
|
|
|
if($orig_width == 0) $orig_width = 192;
|
|
|
|
if($orig_height == 0) $orig_height = 192;
|
|
|
|
|
2007-04-25 18:33:59 +00:00
|
|
|
$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;
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
if($scale > 1 && $config->get_bool('thumb_upscale')) {
|
|
|
|
return array($orig_width, $orig_height);
|
|
|
|
}
|
|
|
|
else {
|
2007-04-25 18:33:59 +00:00
|
|
|
return array($orig_width*$scale, $orig_height*$scale);
|
2007-04-28 19:28:29 +00:00
|
|
|
}
|
2007-04-26 22:27:17 +00:00
|
|
|
}
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
# $db is the connection object
|
|
|
|
function CountExecs($db, $sql, $inputarray) {
|
|
|
|
global $_execs;
|
2007-07-04 01:21:32 +00:00
|
|
|
# $fp = fopen("sql.log", "a");
|
|
|
|
# fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)."\n");
|
|
|
|
# fclose($fp);
|
2007-04-16 11:58:25 +00:00
|
|
|
if (!is_array($inputarray)) $_execs++;
|
|
|
|
# handle 2-dimensional input arrays
|
|
|
|
else if (is_array(reset($inputarray))) $_execs += sizeof($inputarray);
|
|
|
|
else $_execs++;
|
|
|
|
# in PHP4.4 and PHP5, we need to return a value by reference
|
|
|
|
$null = null; return $null;
|
|
|
|
}
|
|
|
|
|
2007-06-30 01:19:11 +00:00
|
|
|
function get_theme_object($file, $class) {
|
|
|
|
global $config;
|
|
|
|
$theme = $config->get_string("theme", "default");
|
|
|
|
if(file_exists("themes/$theme/$file.theme.php")) {
|
|
|
|
require_once "themes/$theme/$file.theme.php";
|
|
|
|
return new $class();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
require_once "ext/$file/theme.php";
|
|
|
|
return new $class();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_debug_info() {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if($config->get_bool('debug_enabled')) {
|
|
|
|
if(function_exists('memory_get_usage')) {
|
|
|
|
$i_mem = sprintf("%5.2f", ((memory_get_usage()+512)/1024)/1024);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$i_mem = "???";
|
|
|
|
}
|
|
|
|
if(function_exists('getrusage')) {
|
|
|
|
$ru = getrusage();
|
|
|
|
$i_utime = sprintf("%5.2f", ($ru["ru_utime.tv_sec"]*1e6+$ru["ru_utime.tv_usec"])/1000000);
|
|
|
|
$i_stime = sprintf("%5.2f", ($ru["ru_stime.tv_sec"]*1e6+$ru["ru_stime.tv_usec"])/1000000);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$i_utime = "???";
|
|
|
|
$i_stime = "???";
|
|
|
|
}
|
|
|
|
$i_files = count(get_included_files());
|
|
|
|
global $_execs;
|
|
|
|
$debug = "<br>Took $i_utime + $i_stime seconds and {$i_mem}MB of RAM";
|
|
|
|
$debug .= "; Used $i_files files and $_execs queries";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$debug = "";
|
|
|
|
}
|
|
|
|
return $debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
function blockcmp($a, $b) {
|
|
|
|
if($a->position == $b->position) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ($a->position > $b->position);
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-05-01 13:30:05 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Things which should be in the core API *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
function array_remove($array, $to_remove) {
|
|
|
|
$array = array_unique($array);
|
|
|
|
$a2 = array();
|
|
|
|
foreach($array as $existing) {
|
|
|
|
if($existing != $to_remove) {
|
|
|
|
$a2[] = $existing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $a2;
|
|
|
|
}
|
|
|
|
|
2007-07-06 04:47:01 +00:00
|
|
|
function array_add($array, $element) {
|
|
|
|
$array[] = $element;
|
|
|
|
$array = array_unique($array);
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2007-05-01 13:30:05 +00:00
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Event API *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
$_event_listeners = array();
|
|
|
|
|
|
|
|
function add_event_listener($block, $pos=50) {
|
|
|
|
global $_event_listeners;
|
|
|
|
while(isset($_event_listeners[$pos])) {
|
|
|
|
$pos++;
|
|
|
|
}
|
|
|
|
$_event_listeners[$pos] = $block;
|
|
|
|
}
|
|
|
|
|
|
|
|
function send_event($event) {
|
|
|
|
global $_event_listeners;
|
2007-06-26 11:58:47 +00:00
|
|
|
ksort($_event_listeners);
|
2007-04-16 11:58:25 +00:00
|
|
|
foreach($_event_listeners as $listener) {
|
|
|
|
$listener->receive_event($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Request initialisation stuff *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
function _get_query_parts() {
|
|
|
|
if(isset($_GET["q"])) {
|
|
|
|
$path = $_GET["q"];
|
|
|
|
}
|
|
|
|
else if(isset($_SERVER["PATH_INFO"])) {
|
|
|
|
$path = $_SERVER["PATH_INFO"];
|
|
|
|
}
|
|
|
|
else {
|
2007-05-03 15:19:02 +00:00
|
|
|
$path = "";
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while(strlen($path) > 0 && $path[0] == '/') {
|
|
|
|
$path = substr($path, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return split('/', $path);
|
|
|
|
}
|
2007-04-28 19:28:29 +00:00
|
|
|
|
2007-06-30 01:19:11 +00:00
|
|
|
function get_page_request($page_object) {
|
2007-05-03 15:19:02 +00:00
|
|
|
global $config;
|
2007-04-16 11:58:25 +00:00
|
|
|
$args = _get_query_parts();
|
|
|
|
|
2007-05-03 15:19:02 +00:00
|
|
|
if(count($args) == 0 || strlen($args[0]) == 0) {
|
|
|
|
$page = $config->get_string('front_page', 'index');
|
2007-04-16 11:58:25 +00:00
|
|
|
$args = array();
|
|
|
|
}
|
|
|
|
else if(count($args) == 1) {
|
2007-05-03 15:19:02 +00:00
|
|
|
$page = $args[0];
|
2007-04-16 11:58:25 +00:00
|
|
|
$args = array();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$page = $args[0];
|
|
|
|
$args = array_slice($args, 1);
|
|
|
|
}
|
|
|
|
|
2007-06-30 01:19:11 +00:00
|
|
|
return new PageRequestEvent($page, $args, $page_object);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_user() {
|
|
|
|
global $database;
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$user = null;
|
|
|
|
if(isset($_COOKIE["shm_user"]) && isset($_COOKIE["shm_session"])) {
|
|
|
|
$tmp_user = $database->get_user_session($_COOKIE["shm_user"], $_COOKIE["shm_session"]);
|
|
|
|
if(!is_null($tmp_user) && $tmp_user->is_enabled()) {
|
|
|
|
$user = $tmp_user;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if(is_null($user)) {
|
2007-05-17 03:49:23 +00:00
|
|
|
$user = $database->get_user($config->get_int("anon_id", 0));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-05-17 03:49:23 +00:00
|
|
|
assert(!is_null($user));
|
2007-04-16 11:58:25 +00:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|