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) {
|
2007-07-06 05:57:24 +00:00
|
|
|
return htmlentities($input, ENT_QUOTES, "UTF-8");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function int_escape($input) {
|
|
|
|
return (int)$input;
|
|
|
|
}
|
|
|
|
|
2007-05-23 03:44:15 +00:00
|
|
|
function url_escape($input) {
|
2007-07-26 13:11:25 +00:00
|
|
|
$input = str_replace('/', '//', $input);
|
|
|
|
$input = rawurlencode($input);
|
|
|
|
$input = str_replace('%2F', '/', $input);
|
|
|
|
return $input;
|
2007-05-23 03:44:15 +00:00
|
|
|
}
|
|
|
|
|
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-24 19:12:05 +00:00
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* HTML Generation *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2007-07-26 13:19:39 +00:00
|
|
|
function make_link($page=null, $query=null) {
|
2007-04-28 19:28:29 +00:00
|
|
|
global $config;
|
2007-07-26 13:19:39 +00:00
|
|
|
|
2007-07-28 19:59:17 +00:00
|
|
|
if(is_null($page)) $page = $config->get_string('main_page');
|
2007-07-26 13:19:39 +00:00
|
|
|
|
2008-12-27 10:17:53 +00:00
|
|
|
if($config->get_bool('nice_urls', false)) {
|
|
|
|
$full = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"];
|
|
|
|
$base = str_replace("/index.php", "", $full);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$base = "./index.php?q=";
|
|
|
|
}
|
2007-04-28 19:28:29 +00:00
|
|
|
|
|
|
|
if(is_null($query)) {
|
|
|
|
return "$base/$page";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(strpos($base, "?")) {
|
|
|
|
return "$base/$page&$query";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "$base/$page?$query";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-06-08 15:04:57 +00:00
|
|
|
function version_check() {
|
|
|
|
if(version_compare(PHP_VERSION, "5.0.0") == -1) {
|
|
|
|
print <<<EOD
|
2009-01-03 23:16:18 +00:00
|
|
|
Currently SCore Engine doesn't support versions of PHP lower than 5.0.0 --
|
|
|
|
PHP4 and earlier are officially dead according to their creators,
|
|
|
|
please tell your host to upgrade.
|
2008-06-08 15:04:57 +00:00
|
|
|
EOD;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-09 11:21:11 +00:00
|
|
|
function check_cli() {
|
|
|
|
if(isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
|
print "This script is to be run from the command line only.";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$_SERVER['REMOTE_ADDR'] = "127.0.0.1";
|
|
|
|
}
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
# $db is the connection object
|
2007-07-12 07:26:50 +00:00
|
|
|
function _count_execs($db, $sql, $inputarray) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $_execs;
|
2007-07-06 17:02:52 +00:00
|
|
|
if(DEBUG) {
|
|
|
|
$fp = fopen("sql.log", "a");
|
2008-07-29 19:43:34 +00:00
|
|
|
if(is_array($inputarray)) {
|
|
|
|
fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)." -- ".join(", ", $inputarray)."\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)."\n");
|
|
|
|
}
|
2007-07-06 17:02:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-07 14:20:31 +00:00
|
|
|
function get_theme_object(Extension $class, $fatal=true) {
|
2008-09-06 16:59:02 +00:00
|
|
|
$base = get_class($class);
|
|
|
|
if(class_exists("Custom{$base}Theme")) {
|
|
|
|
$class = "Custom{$base}Theme";
|
2007-06-30 01:19:11 +00:00
|
|
|
return new $class();
|
|
|
|
}
|
2008-11-07 14:20:31 +00:00
|
|
|
elseif ($fatal || class_exists("{$base}Theme")) {
|
2008-09-06 16:59:02 +00:00
|
|
|
$class = "{$base}Theme";
|
2007-06-30 01:19:11 +00:00
|
|
|
return new $class();
|
2008-11-07 14:20:31 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2007-06-30 01:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-06 17:02:52 +00:00
|
|
|
function blockcmp($a, $b) {
|
|
|
|
if($a->position == $b->position) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ($a->position > $b->position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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_mem_limit"));
|
|
|
|
if($shimmie_limit < 3*1024*1024) {
|
|
|
|
// we aren't going to fit, override
|
|
|
|
$shimmie_limit = $default_limit;
|
|
|
|
}
|
2009-01-04 13:53:14 +00:00
|
|
|
|
2007-07-06 17:02:52 +00:00
|
|
|
ini_set("memory_limit", $shimmie_limit);
|
|
|
|
$memory = parse_shorthand_int(ini_get("memory_limit"));
|
|
|
|
|
|
|
|
// changing of memory limit is disabled / failed
|
|
|
|
if($memory == -1) {
|
2009-01-04 13:53:14 +00:00
|
|
|
$memory = $default_limit;
|
2007-07-06 17:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert($memory > 0);
|
|
|
|
|
|
|
|
return $memory;
|
|
|
|
}
|
|
|
|
|
2008-04-08 16:02:43 +00:00
|
|
|
function get_session_ip() {
|
|
|
|
global $config;
|
|
|
|
|
2008-08-22 08:19:15 +00:00
|
|
|
$mask = $config->get_string("session_hash_mask", "255.255.0.0");
|
2008-04-08 16:02:43 +00:00
|
|
|
$addr = $_SERVER['REMOTE_ADDR'];
|
|
|
|
$addr = inet_ntop(inet_pton($addr) & inet_pton($mask));
|
|
|
|
|
|
|
|
return $addr;
|
|
|
|
}
|
|
|
|
|
2007-08-05 22:03:43 +00:00
|
|
|
/*
|
|
|
|
* PHP really, really sucks.
|
|
|
|
*/
|
2007-07-16 21:30:28 +00:00
|
|
|
function get_base_href() {
|
2007-08-05 22:03:43 +00:00
|
|
|
$possible_vars = array('SCRIPT_NAME', 'PHP_SELF', 'PATH_INFO', 'ORIG_PATH_INFO');
|
|
|
|
$ok_var = null;
|
|
|
|
foreach($possible_vars as $var) {
|
|
|
|
if(substr($_SERVER[$var], -4) == '.php') {
|
|
|
|
$ok_var = $_SERVER[$var];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(!empty($ok_var));
|
|
|
|
$dir = dirname($ok_var);
|
2008-01-02 21:46:14 +00:00
|
|
|
if($dir == "/" || $dir == "\\") $dir = "";
|
2007-07-17 02:02:57 +00:00
|
|
|
return $dir;
|
2007-07-16 21:30:28 +00:00
|
|
|
}
|
|
|
|
|
2008-07-29 19:43:34 +00:00
|
|
|
function format_text($string) {
|
|
|
|
$tfe = new TextFormattingEvent($string);
|
|
|
|
send_event($tfe);
|
|
|
|
return $tfe->formatted;
|
|
|
|
}
|
|
|
|
|
2008-04-08 21:56:37 +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;
|
|
|
|
}
|
|
|
|
|
2008-02-17 09:25:49 +00:00
|
|
|
function array_contains($array, $target) {
|
|
|
|
foreach($array as $element) {
|
|
|
|
if($target == $element) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-05-20 03:06:44 +00:00
|
|
|
// case insensetive uniqueness
|
|
|
|
function array_iunique($array) {
|
|
|
|
$ok = array();
|
|
|
|
foreach($array as $element) {
|
|
|
|
$found = false;
|
|
|
|
foreach($ok as $existing) {
|
|
|
|
if(strtolower($element) == strtolower($existing)) {
|
|
|
|
$found = true; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!$found) {
|
|
|
|
$ok[] = $element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
|
2008-04-01 10:57:18 +00:00
|
|
|
// from http://uk.php.net/network
|
|
|
|
function ip_in_range($IP, $CIDR) {
|
|
|
|
list ($net, $mask) = split ("/", $CIDR);
|
|
|
|
|
|
|
|
$ip_net = ip2long ($net);
|
|
|
|
$ip_mask = ~((1 << (32 - $mask)) - 1);
|
|
|
|
|
|
|
|
$ip_ip = ip2long ($IP);
|
|
|
|
|
|
|
|
$ip_ip_net = $ip_ip & $ip_mask;
|
|
|
|
|
|
|
|
return ($ip_ip_net == $ip_net);
|
|
|
|
}
|
2007-05-01 13:30:05 +00:00
|
|
|
|
2008-04-06 17:43:03 +00:00
|
|
|
// from a patch by Christian Walde; only intended for use in the
|
|
|
|
// "extension manager" extension, but it seems to fit better here
|
|
|
|
function deltree($f) {
|
|
|
|
if (is_link($f)) {
|
|
|
|
unlink($f);
|
|
|
|
}
|
|
|
|
else if(is_dir($f)) {
|
|
|
|
foreach(glob($f.'/*') as $sf) {
|
|
|
|
if (is_dir($sf) && !is_link($sf)) {
|
|
|
|
deltree($sf);
|
|
|
|
} else {
|
|
|
|
unlink($sf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir($f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// from a comment on http://uk.php.net/copy
|
|
|
|
function full_copy($source, $target) {
|
|
|
|
if(is_dir($source)) {
|
|
|
|
@mkdir($target);
|
|
|
|
|
|
|
|
$d = dir($source);
|
|
|
|
|
|
|
|
while(FALSE !== ($entry = $d->read())) {
|
|
|
|
if($entry == '.' || $entry == '..') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$Entry = $source . '/' . $entry;
|
|
|
|
if(is_dir($Entry)) {
|
|
|
|
full_copy($Entry, $target . '/' . $entry);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
copy($Entry, $target . '/' . $entry);
|
|
|
|
}
|
|
|
|
$d->close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
copy($source, $target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-08 15:04:57 +00:00
|
|
|
function stripslashes_r($arr) {
|
|
|
|
return is_array($arr) ? array_map('stripslashes_r', $arr) : stripslashes($arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sanitise_environment() {
|
|
|
|
if(DEBUG) {
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
assert_options(ASSERT_ACTIVE, 1);
|
|
|
|
assert_options(ASSERT_BAIL, 1);
|
|
|
|
}
|
|
|
|
|
2008-07-21 11:04:05 +00:00
|
|
|
ob_start();
|
|
|
|
|
2008-06-08 15:04:57 +00:00
|
|
|
if(get_magic_quotes_gpc()) {
|
|
|
|
$_GET = stripslashes_r($_GET);
|
|
|
|
$_POST = stripslashes_r($_POST);
|
|
|
|
$_COOKIE = stripslashes_r($_COOKIE);
|
|
|
|
}
|
|
|
|
}
|
2008-04-06 17:43:03 +00:00
|
|
|
|
2008-06-14 18:45:00 +00:00
|
|
|
function weighted_random($weights) {
|
|
|
|
$total = 0;
|
|
|
|
foreach($weights as $k => $w) {
|
|
|
|
$total += $w;
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = mt_rand(0, $total);
|
|
|
|
foreach($weights as $k => $w) {
|
|
|
|
$r -= $w;
|
|
|
|
if($r <= 0) {
|
|
|
|
return $k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Event API *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
$_event_listeners = array();
|
|
|
|
|
2009-01-03 21:06:36 +00:00
|
|
|
function add_event_listener(Extension $extension, $pos=50) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $_event_listeners;
|
|
|
|
while(isset($_event_listeners[$pos])) {
|
|
|
|
$pos++;
|
|
|
|
}
|
2007-07-12 07:22:26 +00:00
|
|
|
$_event_listeners[$pos] = $extension;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2007-10-28 01:48:11 +00:00
|
|
|
$_event_count = 0;
|
2009-01-03 21:06:36 +00:00
|
|
|
function send_event(Event $event) {
|
2007-10-28 01:48:11 +00:00
|
|
|
global $_event_listeners, $_event_count;
|
2007-07-08 21:58:07 +00:00
|
|
|
$my_event_listeners = $_event_listeners; // http://bugs.php.net/bug.php?id=35106
|
2007-07-08 21:39:44 +00:00
|
|
|
ksort($my_event_listeners);
|
|
|
|
foreach($my_event_listeners as $listener) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$listener->receive_event($event);
|
|
|
|
}
|
2007-10-28 01:48:11 +00:00
|
|
|
$_event_count++;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-08-08 06:24:25 +00:00
|
|
|
$path = str_replace('/', '%%', $path);
|
|
|
|
$path = str_replace('%%%%', '/', $path);
|
|
|
|
$parts = split('%%', $path);
|
2007-07-26 13:11:25 +00:00
|
|
|
|
|
|
|
return $parts;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-04-28 19:28:29 +00:00
|
|
|
|
2008-04-04 12:07:38 +00:00
|
|
|
function _get_page_request($context) {
|
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) {
|
2008-09-06 17:48:03 +00:00
|
|
|
$args = split('/', $context->config->get_string('front_page'));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2008-09-06 17:48:03 +00:00
|
|
|
return new PageRequestEvent($context, $args);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-23 12:05:24 +00:00
|
|
|
function _get_user($config, $database) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$user = null;
|
|
|
|
if(isset($_COOKIE["shm_user"]) && isset($_COOKIE["shm_session"])) {
|
2008-08-23 12:05:24 +00:00
|
|
|
$tmp_user = User::by_session($config, $database, $_COOKIE["shm_user"], $_COOKIE["shm_session"]);
|
2008-04-02 20:07:52 +00:00
|
|
|
if(!is_null($tmp_user)) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$user = $tmp_user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_null($user)) {
|
2008-08-23 12:05:24 +00:00
|
|
|
$user = User::by_id($config, $database, $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;
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|