2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2010-01-03 08:15:52 +00:00
|
|
|
require_once "lib/recaptchalib.php";
|
|
|
|
require_once "lib/securimage/securimage.php";
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Input / Output Sanitising *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Make some data safe for printing into HTML
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval string
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Make sure some data is safe to be used in integer context
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval int
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
function int_escape($input) {
|
2012-01-11 20:08:27 +00:00
|
|
|
/*
|
|
|
|
Side note, Casting to an integer is FASTER than using intval.
|
|
|
|
http://hakre.wordpress.com/2010/05/13/php-casting-vs-intval/
|
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
return (int)$input;
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Make sure some data is safe to be used in URL context
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval string
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-05-23 03:44:15 +00:00
|
|
|
function url_escape($input) {
|
2012-04-18 04:56:11 +00:00
|
|
|
/*
|
|
|
|
Shish: I have a feeling that these three lines are important, possibly for searching for tags with slashes in them like fate/stay_night
|
|
|
|
green-ponies: indeed~
|
2012-04-18 06:20:42 +00:00
|
|
|
|
2012-04-18 04:56:11 +00:00
|
|
|
$input = str_replace('^', '^^', $input);
|
|
|
|
$input = str_replace('/', '^s', $input);
|
|
|
|
$input = str_replace('\\', '^b', $input);
|
|
|
|
|
2012-04-15 22:59:23 +00:00
|
|
|
/* The function idn_to_ascii is used to support Unicode domains / URLs as well.
|
2012-04-16 20:42:32 +00:00
|
|
|
See here for more: http://php.net/manual/en/function.filter-var.php
|
|
|
|
However, it is only supported by PHP version 5.3 and up
|
2012-04-18 06:20:42 +00:00
|
|
|
|
2012-04-16 20:42:32 +00:00
|
|
|
if (function_exists('idn_to_ascii')) {
|
|
|
|
return filter_var(idn_to_ascii($input), FILTER_SANITIZE_URL);
|
|
|
|
} else {
|
|
|
|
return filter_var($input, FILTER_SANITIZE_URL);
|
|
|
|
}
|
2012-04-18 06:20:42 +00:00
|
|
|
*/
|
|
|
|
if(is_null($input)) {
|
2012-03-09 18:14:14 +00:00
|
|
|
return "";
|
|
|
|
}
|
2009-01-18 14:58:32 +00:00
|
|
|
$input = str_replace('^', '^^', $input);
|
|
|
|
$input = str_replace('/', '^s', $input);
|
2010-04-23 02:31:10 +00:00
|
|
|
$input = str_replace('\\', '^b', $input);
|
2007-07-26 13:11:25 +00:00
|
|
|
$input = rawurlencode($input);
|
2012-04-18 06:20:42 +00:00
|
|
|
return $input;
|
2007-05-23 03:44:15 +00:00
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Make sure some data is safe to be used in SQL context
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval string
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
function sql_escape($input) {
|
|
|
|
global $database;
|
|
|
|
return $database->db->Quote($input);
|
|
|
|
}
|
|
|
|
|
2011-12-25 15:11:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn all manner of HTML / INI / JS / DB booleans into a PHP one
|
|
|
|
*
|
|
|
|
* @retval boolean
|
|
|
|
*/
|
|
|
|
function bool_escape($input) {
|
2012-04-15 23:28:27 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2012-04-16 20:42:32 +00:00
|
|
|
if (is_bool($input)) {
|
|
|
|
return $input;
|
2012-04-18 04:56:11 +00:00
|
|
|
} else if (is_numeric($input)) {
|
|
|
|
return ($input === 1);
|
2012-04-15 23:28:27 +00:00
|
|
|
} else {
|
|
|
|
$value = filter_var($input, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
|
|
|
if (!is_null($value)) {
|
|
|
|
return $value;
|
|
|
|
} else {
|
2012-04-18 04:56:11 +00:00
|
|
|
$input = strtolower( trim($input) );
|
2012-04-15 23:28:27 +00:00
|
|
|
return (
|
|
|
|
$input === "y" ||
|
|
|
|
$input === "yes" ||
|
|
|
|
$input === "t" ||
|
|
|
|
$input === "true" ||
|
|
|
|
$input === "on" ||
|
2012-04-18 05:17:44 +00:00
|
|
|
$input === "1"
|
2012-04-15 23:28:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2011-12-25 15:11:26 +00:00
|
|
|
}
|
|
|
|
|
2010-03-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2012-03-13 07:01:27 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Turn a human readable filesize into an integer, eg 1KB -> 1024
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval int
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
function parse_shorthand_int($limit) {
|
|
|
|
if(is_numeric($limit)) {
|
|
|
|
return (int)$limit;
|
|
|
|
}
|
|
|
|
|
2012-01-11 20:08:27 +00:00
|
|
|
if(preg_match('/^([\d\.]+)([gmk])?b?$/i', (string)$limit, $m)) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Turn an integer into a human readable filesize, eg 1024 -> 1KB
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval string
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
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 {
|
2012-01-11 20:08:27 +00:00
|
|
|
return (string)$int;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-24 19:12:05 +00:00
|
|
|
|
2009-07-27 23:54:47 +00:00
|
|
|
/**
|
|
|
|
* Turn a date into a time, a date, an "X minutes ago...", etc
|
|
|
|
*
|
|
|
|
* @retval string
|
|
|
|
*/
|
2009-11-10 03:49:56 +00:00
|
|
|
function autodate($date, $html=true) {
|
2011-12-31 14:12:05 +00:00
|
|
|
$cpu = date('c', strtotime($date));
|
2012-01-22 14:40:25 +00:00
|
|
|
$hum = date('F j, Y; H:i', strtotime($date));
|
2011-12-31 14:12:05 +00:00
|
|
|
return ($html ? "<time datetime='$cpu'>$hum</time>" : $hum);
|
2009-07-28 22:56:46 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 20:58:55 +00:00
|
|
|
/**
|
|
|
|
* Check if a given string is a valid date-time. ( Format: yyyy-mm-dd hh:mm:ss )
|
|
|
|
*
|
|
|
|
* @retval boolean
|
|
|
|
*/
|
2012-03-09 22:27:12 +00:00
|
|
|
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;
|
2012-01-16 20:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a given string is a valid date. ( Format: yyyy-mm-dd )
|
|
|
|
*
|
|
|
|
* @retval boolean
|
|
|
|
*/
|
2012-03-09 22:27:12 +00:00
|
|
|
function isValidDate($date) {
|
|
|
|
if (preg_match("/^(\d{4})-(\d{2})-(\d{2})$/", $date, $matches)) {
|
2012-01-16 20:58:55 +00:00
|
|
|
// checkdate wants (month, day, year)
|
2012-03-09 22:27:12 +00:00
|
|
|
if (checkdate($matches[2], $matches[3], $matches[1])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 20:58:55 +00:00
|
|
|
|
2012-03-09 22:27:12 +00:00
|
|
|
return false;
|
2012-01-16 20:58:55 +00:00
|
|
|
}
|
2009-07-28 22:56:46 +00:00
|
|
|
|
2012-02-09 22:51:22 +00:00
|
|
|
/**
|
|
|
|
* 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") ? ", <a href='".make_link("ip_ban/list", "ip=$ip&reason=$u_reason&end=$u_end#add")."'>Ban</a>" : "";
|
|
|
|
$ip = $user->can("view_ip") ? $ip.$ban : "";
|
|
|
|
return $ip;
|
|
|
|
}
|
|
|
|
|
2012-02-08 01:05:38 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2012-01-31 15:11:06 +00:00
|
|
|
$length = strlen($needle);
|
|
|
|
return (substr($haystack, 0, $length) === $needle);
|
|
|
|
}
|
|
|
|
|
2012-02-08 01:05:38 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2012-01-31 15:11:06 +00:00
|
|
|
$length = strlen($needle);
|
|
|
|
$start = $length * -1; //negative
|
|
|
|
return (substr($haystack, $start) === $needle);
|
|
|
|
}
|
|
|
|
|
2009-10-08 16:43:18 +00:00
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* HTML Generation *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Figure out the correct way to link to a page, taking into account
|
2010-01-12 15:01:34 +00:00
|
|
|
* things like the nice URLs setting.
|
|
|
|
*
|
|
|
|
* eg make_link("post/list") becomes "/v2/index.php?q=post/list"
|
2009-07-19 07:38:13 +00:00
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval string
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
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
|
|
|
|
2012-01-20 03:15:58 +00:00
|
|
|
if(NICE_URLS || $config->get_bool('nice_urls', false)) {
|
2009-07-01 12:17:09 +00:00
|
|
|
#$full = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"];
|
|
|
|
$full = $_SERVER["PHP_SELF"];
|
2012-06-21 08:07:52 +00:00
|
|
|
$base = str_replace(basename($_SERVER["SCRIPT_FILENAME"]), "", $full);
|
2008-12-27 10:17:53 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-02-01 16:41:18 +00:00
|
|
|
$base = "./".basename($_SERVER["SCRIPT_FILENAME"])."?q=";
|
2008-12-27 10:17:53 +00:00
|
|
|
}
|
2007-04-28 19:28:29 +00:00
|
|
|
|
|
|
|
if(is_null($query)) {
|
2012-01-11 20:08:27 +00:00
|
|
|
return str_replace("//", "/", $base.'/'.$page );
|
2007-04-28 19:28:29 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(strpos($base, "?")) {
|
2012-01-11 20:08:27 +00:00
|
|
|
return $base .'/'. $page .'&'. $query;
|
2007-04-28 19:28:29 +00:00
|
|
|
}
|
2010-02-09 09:05:30 +00:00
|
|
|
else if(strpos($query, "#") === 0) {
|
2012-01-11 20:08:27 +00:00
|
|
|
return $base .'/'. $page . $query;
|
2010-02-09 09:05:30 +00:00
|
|
|
}
|
2007-04-28 19:28:29 +00:00
|
|
|
else {
|
2012-01-11 20:08:27 +00:00
|
|
|
return $base .'/'. $page .'?'. $query;
|
2007-04-28 19:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-25 17:57:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Take the current URL and modify some paramaters
|
|
|
|
*
|
|
|
|
* @retval string
|
|
|
|
*/
|
|
|
|
function modify_current_url($changes) {
|
2012-01-01 17:13:50 +00:00
|
|
|
return modify_url($_SERVER['QUERY_STRING'], $changes);
|
|
|
|
}
|
|
|
|
|
|
|
|
function modify_url($url, $changes) {
|
2011-12-25 17:57:15 +00:00
|
|
|
// 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();
|
2012-01-01 17:13:50 +00:00
|
|
|
parse_str($url, $params);
|
2011-12-25 17:57:15 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-24 07:08:29 +00:00
|
|
|
/**
|
|
|
|
* Turn a relative link into an absolute one, including hostname
|
|
|
|
*
|
|
|
|
* @retval string
|
|
|
|
*/
|
2012-02-02 13:58:48 +00:00
|
|
|
function make_http(/*string*/ $link) {
|
2009-07-24 07:08:29 +00:00
|
|
|
if(strpos($link, "ttp://") > 0) return $link;
|
2010-01-18 08:29:35 +00:00
|
|
|
if(strlen($link) > 0 && $link[0] != '/') $link = get_base_href().'/'.$link;
|
2009-07-24 07:08:29 +00:00
|
|
|
$link = "http://".$_SERVER["HTTP_HOST"].$link;
|
|
|
|
$link = str_replace("/./", "/", $link);
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2010-09-22 11:56:19 +00:00
|
|
|
/**
|
|
|
|
* Make a form tag with relevant auth token and stuff
|
|
|
|
*
|
|
|
|
* @retval string
|
|
|
|
*/
|
2011-12-29 22:55:44 +00:00
|
|
|
function make_form($target, $method="POST", $multipart=False, $form_id="", $onsubmit="") {
|
2010-09-22 11:56:19 +00:00
|
|
|
global $user;
|
|
|
|
$auth = $user->get_auth_html();
|
2012-01-11 20:08:27 +00:00
|
|
|
$extra = empty($form_id) ? '' : 'id="'. $form_id .'"';
|
2010-09-22 11:56:19 +00:00
|
|
|
if($multipart) {
|
2010-09-22 12:20:08 +00:00
|
|
|
$extra .= " enctype='multipart/form-data'";
|
2010-09-22 11:56:19 +00:00
|
|
|
}
|
2011-12-29 22:55:44 +00:00
|
|
|
if($onsubmit) {
|
2012-01-11 20:08:27 +00:00
|
|
|
$extra .= ' onsubmit="'.$onsubmit.'"';
|
2011-12-29 22:55:44 +00:00
|
|
|
}
|
2012-01-11 20:08:27 +00:00
|
|
|
return '<form action="'.$target.'" method="'.$method.'" '.$extra.'>'.$auth;
|
2010-09-22 11:56:19 +00:00
|
|
|
}
|
|
|
|
|
2012-03-12 23:17:20 +00:00
|
|
|
function mtimefile($file) {
|
|
|
|
$data_href = get_base_href();
|
|
|
|
$mtime = filemtime($file);
|
|
|
|
return "$data_href/$file?$mtime";
|
|
|
|
}
|
|
|
|
|
2012-06-17 23:00:21 +00:00
|
|
|
function get_theme() {
|
|
|
|
global $config;
|
|
|
|
$theme = $config->get_string("theme", "default");
|
|
|
|
if(!file_exists("themes/$theme")) $theme = "default";
|
|
|
|
return $theme;
|
|
|
|
}
|
|
|
|
|
2012-05-22 11:46:56 +00:00
|
|
|
/*
|
|
|
|
* like glob, with support for matching very long patterns with braces
|
|
|
|
*/
|
2012-03-21 15:04:17 +00:00
|
|
|
function zglob($pattern) {
|
2012-05-22 11:46:56 +00:00
|
|
|
$results = array();
|
|
|
|
if(preg_match('/(.*)\{(.*)\}(.*)/', $pattern, $matches)) {
|
|
|
|
$braced = explode(",", $matches[2]);
|
|
|
|
foreach($braced as $b) {
|
|
|
|
$sub_pattern = $matches[1].$b.$matches[3];
|
|
|
|
$results = array_merge($results, zglob($sub_pattern));
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$r = glob($pattern);
|
|
|
|
if($r) return $r;
|
|
|
|
else return array();
|
|
|
|
}
|
2012-03-21 15:04:17 +00:00
|
|
|
}
|
2012-03-12 23:17:20 +00:00
|
|
|
|
2012-02-11 08:33:09 +00:00
|
|
|
|
2010-01-03 08:15:52 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* CAPTCHA abstraction *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
function captcha_get_html() {
|
|
|
|
global $config, $user;
|
2010-01-23 12:48:43 +00:00
|
|
|
|
|
|
|
if(DEBUG && ip_in_range($_SERVER['REMOTE_ADDR'], "127.0.0.0/8")) return "";
|
|
|
|
|
2010-01-03 08:15:52 +00:00
|
|
|
$captcha = "";
|
2011-12-21 02:28:39 +00:00
|
|
|
if($user->is_anonymous() && $config->get_bool("comment_captcha")) {
|
|
|
|
$rpk = $config->get_string("api_recaptcha_privkey");
|
2010-01-03 08:15:52 +00:00
|
|
|
if(!empty($rpk)) {
|
|
|
|
$captcha = recaptcha_get_html($rpk);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
session_start();
|
|
|
|
$securimg = new Securimage();
|
|
|
|
$base = get_base_href();
|
|
|
|
$captcha = "<br/><img src='$base/lib/securimage/securimage_show.php?sid=". md5(uniqid(time())) ."'>".
|
|
|
|
"<br/>CAPTCHA: <input type='text' name='code' value='' />";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $captcha;
|
|
|
|
}
|
|
|
|
|
|
|
|
function captcha_check() {
|
|
|
|
global $config, $user;
|
|
|
|
|
2010-01-23 12:48:43 +00:00
|
|
|
if(DEBUG && ip_in_range($_SERVER['REMOTE_ADDR'], "127.0.0.0/8")) return true;
|
|
|
|
|
2011-12-21 02:28:39 +00:00
|
|
|
if($user->is_anonymous() && $config->get_bool("comment_captcha")) {
|
|
|
|
$rpk = $config->get_string('api_recaptcha_pubkey');
|
2010-01-03 08:15:52 +00:00
|
|
|
if(!empty($rpk)) {
|
|
|
|
$resp = recaptcha_check_answer(
|
2012-03-09 22:27:12 +00:00
|
|
|
$rpk,
|
|
|
|
$_SERVER["REMOTE_ADDR"],
|
|
|
|
$_POST["recaptcha_challenge_field"],
|
|
|
|
$_POST["recaptcha_response_field"]
|
|
|
|
);
|
2010-01-03 08:15:52 +00:00
|
|
|
|
|
|
|
if(!$resp->is_valid) {
|
|
|
|
log_info("core", "Captcha failed (ReCaptcha): " . $resp->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
session_start();
|
|
|
|
$securimg = new Securimage();
|
|
|
|
if($securimg->check($_POST['code']) == false) {
|
|
|
|
log_info("core", "Captcha failed (Securimage)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-05-14 05:43:23 +00:00
|
|
|
/**
|
|
|
|
* Get MIME type for file
|
|
|
|
*
|
|
|
|
* The contents of this function are taken from the __getMimeType() function
|
|
|
|
* from the "Amazon S3 PHP class" which is Copyright (c) 2008, Donovan Schönknecht
|
|
|
|
* and released under the 'Simplified BSD License'.
|
|
|
|
*
|
|
|
|
* @internal Used to get mime types
|
|
|
|
* @param string &$file File path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getMimeType($file) {
|
|
|
|
$type = false;
|
|
|
|
// Fileinfo documentation says fileinfo_open() will use the
|
|
|
|
// MAGIC env var for the magic file
|
|
|
|
if (extension_loaded('fileinfo') && isset($_ENV['MAGIC']) &&
|
|
|
|
($finfo = finfo_open(FILEINFO_MIME, $_ENV['MAGIC'])) !== false)
|
|
|
|
{
|
|
|
|
if (($type = finfo_file($finfo, $file)) !== false)
|
|
|
|
{
|
|
|
|
// Remove the charset and grab the last content-type
|
|
|
|
$type = explode(' ', str_replace('; charset=', ';charset=', $type));
|
|
|
|
$type = array_pop($type);
|
|
|
|
$type = explode(';', $type);
|
|
|
|
$type = trim(array_shift($type));
|
|
|
|
}
|
|
|
|
finfo_close($finfo);
|
|
|
|
|
|
|
|
// If anyone is still using mime_content_type()
|
|
|
|
} elseif (function_exists('mime_content_type'))
|
|
|
|
$type = trim(mime_content_type($file));
|
|
|
|
|
|
|
|
if ($type !== false && strlen($type) > 0) return $type;
|
|
|
|
|
|
|
|
// Otherwise do it the old fashioned way
|
|
|
|
static $exts = array(
|
|
|
|
'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png',
|
|
|
|
'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'ico' => 'image/x-icon',
|
|
|
|
'swf' => 'application/x-shockwave-flash', 'pdf' => 'application/pdf',
|
|
|
|
'zip' => 'application/zip', 'gz' => 'application/x-gzip',
|
|
|
|
'tar' => 'application/x-tar', 'bz' => 'application/x-bzip',
|
|
|
|
'bz2' => 'application/x-bzip2', 'txt' => 'text/plain',
|
|
|
|
'asc' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html',
|
|
|
|
'css' => 'text/css', 'js' => 'text/javascript',
|
|
|
|
'xml' => 'text/xml', 'xsl' => 'application/xsl+xml',
|
|
|
|
'ogg' => 'application/ogg', 'mp3' => 'audio/mpeg', 'wav' => 'audio/x-wav',
|
|
|
|
'avi' => 'video/x-msvideo', 'mpg' => 'video/mpeg', 'mpeg' => 'video/mpeg',
|
|
|
|
'mov' => 'video/quicktime', 'flv' => 'video/x-flv', 'php' => 'text/x-php'
|
|
|
|
);
|
|
|
|
$ext = strtolower(pathInfo($file, PATHINFO_EXTENSION));
|
|
|
|
return isset($exts[$ext]) ? $exts[$ext] : 'application/octet-stream';
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-07-21 03:18:40 +00:00
|
|
|
* @private
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2009-07-21 03:18:40 +00:00
|
|
|
function _version_check() {
|
2012-02-01 16:51:38 +00:00
|
|
|
if(version_compare(PHP_VERSION, "5.2.6") == -1) {
|
2009-07-21 03:18:40 +00:00
|
|
|
print "
|
2012-02-01 16:51:38 +00:00
|
|
|
Currently SCore Engine doesn't support versions of PHP lower than 5.2.6 --
|
|
|
|
if your web host is running an older version, they are dangerously out of
|
|
|
|
date and you should plan on moving elsewhere.
|
2009-07-21 03:18:40 +00:00
|
|
|
";
|
2008-06-08 15:04:57 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-07-21 03:18:40 +00:00
|
|
|
* @private
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2012-06-17 19:05:16 +00:00
|
|
|
function is_cli() {
|
|
|
|
return (PHP_SAPI === 'cli');
|
2008-06-09 11:21:11 +00:00
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* $db is the connection object
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @private
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-07-12 07:26:50 +00:00
|
|
|
function _count_execs($db, $sql, $inputarray) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $_execs;
|
2012-01-31 12:16:47 +00:00
|
|
|
if((DEBUG_SQL === true) || (is_null(DEBUG_SQL) && @$_GET['DEBUG_SQL'])) {
|
2010-02-01 16:11:00 +00:00
|
|
|
$fp = @fopen("data/sql.log", "a");
|
2009-11-15 10:26:17 +00:00
|
|
|
if($fp) {
|
2012-01-12 20:46:34 +00:00
|
|
|
if(isset($inputarray) && is_array($inputarray)) {
|
2009-11-15 10:26:17 +00:00
|
|
|
fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)." -- ".join(", ", $inputarray)."\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)."\n");
|
|
|
|
}
|
|
|
|
fclose($fp);
|
2008-07-29 19:43:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-03-21 02:17:16 +00:00
|
|
|
# WARNING:
|
|
|
|
# SQL queries happen before the event system is fully initialised
|
|
|
|
# (eg, "select theme from config" happens before "load themes"),
|
|
|
|
# so using the event system to report an error will create some
|
|
|
|
# really weird looking bugs.
|
|
|
|
#
|
|
|
|
#log_error("core", "failed to open sql.log for appending");
|
2008-07-29 19:43:34 +00:00
|
|
|
}
|
2007-07-06 17:02:52 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Compare two Block objects, used to sort them before being displayed
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval int
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2009-07-21 06:36:12 +00:00
|
|
|
function blockcmp(Block $a, Block $b) {
|
2007-07-06 17:02:52 +00:00
|
|
|
if($a->position == $b->position) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ($a->position > $b->position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Figure out PHP's internal memory limit
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval int
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-07-06 17:02:52 +00:00
|
|
|
function get_memory_limit() {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
// thumbnail generation requires lots of memory
|
2012-02-13 00:48:07 +00:00
|
|
|
$default_limit = 8*1024*1024; // 8 MB of memory is PHP's default.
|
2007-07-06 17:02:52 +00:00
|
|
|
$shimmie_limit = parse_shorthand_int($config->get_int("thumb_mem_limit"));
|
2012-02-13 00:48:07 +00:00
|
|
|
|
2007-07-06 17:02:52 +00:00
|
|
|
if($shimmie_limit < 3*1024*1024) {
|
|
|
|
// we aren't going to fit, override
|
|
|
|
$shimmie_limit = $default_limit;
|
|
|
|
}
|
2012-02-13 00:48:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Get PHP's configured memory limit.
|
|
|
|
Note that this is set to -1 for NO memory limit.
|
|
|
|
|
|
|
|
http://ca2.php.net/manual/en/ini.core.php#ini.memory-limit
|
|
|
|
*/
|
2007-07-06 17:02:52 +00:00
|
|
|
$memory = parse_shorthand_int(ini_get("memory_limit"));
|
2012-02-13 00:48:07 +00:00
|
|
|
|
2012-03-09 22:27:12 +00:00
|
|
|
if($memory == -1) {
|
2012-02-13 00:48:07 +00:00
|
|
|
// No memory limit.
|
|
|
|
// Return the larger of the set limits.
|
2012-03-09 22:27:12 +00:00
|
|
|
return max($shimmie_limit, $default_limit);
|
|
|
|
}
|
|
|
|
else {
|
2012-02-13 00:48:07 +00:00
|
|
|
// PHP has a memory limit set.
|
|
|
|
if ($shimmie_limit > $memory) {
|
|
|
|
// Shimmie wants more memory than what PHP is currently set for.
|
2012-03-09 22:27:12 +00:00
|
|
|
|
2012-02-13 00:48:07 +00:00
|
|
|
// Attempt to set PHP's memory limit.
|
|
|
|
if ( ini_set("memory_limit", $shimmie_limit) === FALSE ) {
|
|
|
|
/* We can't change PHP's limit, oh well, return whatever its currently set to */
|
|
|
|
return $memory;
|
|
|
|
}
|
|
|
|
$memory = parse_shorthand_int(ini_get("memory_limit"));
|
|
|
|
}
|
2012-03-09 22:27:12 +00:00
|
|
|
|
2012-02-13 00:48:07 +00:00
|
|
|
// PHP's memory limit is more than Shimmie needs.
|
|
|
|
return $memory; // return the current setting
|
2007-07-06 17:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Get the currently active IP, masked to make it not change when the last
|
|
|
|
* octet or two change, for use in session cookies and such
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval string
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2012-02-02 13:58:48 +00:00
|
|
|
function get_session_ip(Config $config) {
|
2012-03-09 22:27:12 +00:00
|
|
|
$mask = $config->get_string("session_hash_mask", "255.255.0.0");
|
|
|
|
$addr = $_SERVER['REMOTE_ADDR'];
|
|
|
|
$addr = inet_ntop(inet_pton($addr) & inet_pton($mask));
|
|
|
|
return $addr;
|
2008-04-08 16:02:43 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 12:59:12 +00:00
|
|
|
/**
|
|
|
|
* similar to $_COOKIE[$name], but $name has the site-wide cookie
|
|
|
|
* prefix prepended to it, eg username -> shm_username, to prevent
|
|
|
|
* conflicts from multiple installs within one domain.
|
|
|
|
*/
|
2012-02-02 13:58:48 +00:00
|
|
|
function get_prefixed_cookie(/*string*/ $name) {
|
2009-10-08 12:59:12 +00:00
|
|
|
global $config;
|
2009-10-26 11:39:53 +00:00
|
|
|
$full_name = COOKIE_PREFIX."_".$name;
|
2009-10-08 12:59:12 +00:00
|
|
|
if(isset($_COOKIE[$full_name])) {
|
|
|
|
return $_COOKIE[$full_name];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The counterpart for get_prefixed_cookie, this works like php's
|
|
|
|
* setcookie method, but prepends the site-wide cookie prefix to
|
|
|
|
* the $name argument before doing anything.
|
|
|
|
*/
|
|
|
|
function set_prefixed_cookie($name, $value, $time, $path) {
|
|
|
|
global $config;
|
|
|
|
$full_name = $config->get_string('cookie_prefix','shm')."_".$name;
|
|
|
|
setcookie($full_name, $value, $time, $path);
|
|
|
|
}
|
|
|
|
|
2012-06-09 16:00:25 +00:00
|
|
|
/**
|
|
|
|
* Set (or extend) a flash-message cookie
|
2012-06-10 03:21:03 +00:00
|
|
|
*
|
|
|
|
* This can optionally be done at the same time as saving a log message with log_*()
|
|
|
|
*
|
|
|
|
* Generally one should flash a message in onPageRequest and log a message wherever
|
|
|
|
* the action actually takes place (eg onWhateverElse) - but much of the time, actions
|
|
|
|
* are taken from within onPageRequest...
|
2012-06-09 16:00:25 +00:00
|
|
|
*/
|
2012-06-10 00:05:03 +00:00
|
|
|
function flash_message(/*string*/ $text, /*string*/ $type="info") {
|
2012-06-09 16:00:25 +00:00
|
|
|
$current = get_prefixed_cookie("flash_message");
|
|
|
|
if($current) {
|
|
|
|
$text = $current . "\n" . $text;
|
|
|
|
}
|
|
|
|
# the message should be viewed pretty much immediately,
|
|
|
|
# so 60s timeout should be more than enough
|
|
|
|
set_prefixed_cookie("flash_message", $text, time()+60, "/");
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2012-01-20 03:29:29 +00:00
|
|
|
* Figure out the path to the shimmie install directory.
|
|
|
|
*
|
|
|
|
* eg if shimmie is visible at http://foo.com/gallery, this
|
|
|
|
* function should return /gallery
|
2009-07-19 07:38:13 +00:00
|
|
|
*
|
2007-08-05 22:03:43 +00:00
|
|
|
* PHP really, really sucks.
|
2009-07-19 07:38:13 +00:00
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval string
|
2007-08-05 22:03:43 +00:00
|
|
|
*/
|
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) {
|
2012-01-11 20:08:27 +00:00
|
|
|
if(substr($_SERVER[$var], -4) === '.php') {
|
2007-08-05 22:03:43 +00:00
|
|
|
$ok_var = $_SERVER[$var];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(!empty($ok_var));
|
|
|
|
$dir = dirname($ok_var);
|
2012-01-11 20:08:27 +00:00
|
|
|
if($dir === "/" || $dir === "\\") $dir = "";
|
2007-07-17 02:02:57 +00:00
|
|
|
return $dir;
|
2007-07-16 21:30:28 +00:00
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* A shorthand way to send a TextFormattingEvent and get the
|
|
|
|
* results
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval string
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2012-02-02 13:58:48 +00:00
|
|
|
function format_text(/*string*/ $string) {
|
2008-07-29 19:43:34 +00:00
|
|
|
$tfe = new TextFormattingEvent($string);
|
|
|
|
send_event($tfe);
|
|
|
|
return $tfe->formatted;
|
|
|
|
}
|
|
|
|
|
2012-02-02 13:58:48 +00:00
|
|
|
function warehouse_path(/*string*/ $base, /*string*/ $hash, /*bool*/ $create=true) {
|
2010-02-02 00:29:38 +00:00
|
|
|
$ab = substr($hash, 0, 2);
|
2011-03-23 11:19:34 +00:00
|
|
|
$cd = substr($hash, 2, 2);
|
2011-12-25 11:24:20 +00:00
|
|
|
if(WH_SPLITS == 2) {
|
2012-01-11 20:08:27 +00:00
|
|
|
$pa = $base.'/'.$ab.'/'.$cd.'/'.$hash;
|
2011-12-25 11:24:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-01-11 20:08:27 +00:00
|
|
|
$pa = $base.'/'.$ab.'/'.$hash;
|
2011-12-25 11:24:20 +00:00
|
|
|
}
|
2010-04-23 04:46:07 +00:00
|
|
|
if($create && !file_exists(dirname($pa))) mkdir(dirname($pa), 0755, true);
|
|
|
|
return $pa;
|
2010-02-02 00:29:38 +00:00
|
|
|
}
|
2008-04-08 21:56:37 +00:00
|
|
|
|
2012-03-30 17:21:35 +00:00
|
|
|
function data_path($filename) {
|
|
|
|
$filename = "data/" . $filename;
|
2012-03-30 19:28:09 +00:00
|
|
|
if(!file_exists(dirname($filename))) mkdir(dirname($filename), 0755, true);
|
2012-03-30 17:21:35 +00:00
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
2012-03-14 20:35:15 +00:00
|
|
|
function transload($url, $mfile) {
|
2012-03-09 21:06:03 +00:00
|
|
|
global $config;
|
|
|
|
|
|
|
|
if($config->get_string("transload_engine") == "curl" && function_exists("curl_init")) {
|
|
|
|
$ch = curl_init($url);
|
|
|
|
$fp = fopen($mfile, "w");
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_FILE, $fp);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_REFERER, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, "Shimmie-".VERSION);
|
|
|
|
|
|
|
|
curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($config->get_string("transload_engine") == "wget") {
|
|
|
|
$s_url = escapeshellarg($url);
|
|
|
|
$s_mfile = escapeshellarg($mfile);
|
|
|
|
system("wget $s_url --output-document=$s_mfile");
|
|
|
|
|
|
|
|
return file_exists($mfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($config->get_string("transload_engine") == "fopen") {
|
|
|
|
$fp = @fopen($url, "r");
|
|
|
|
if(!$fp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$data = "";
|
|
|
|
$length = 0;
|
|
|
|
while(!feof($fp) && $length <= $config->get_int('upload_size')) {
|
|
|
|
$data .= fread($fp, 8192);
|
|
|
|
$length = strlen($data);
|
|
|
|
}
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
$fp = fopen($mfile, "w");
|
|
|
|
fwrite($fp, $data);
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-17 23:45:32 +00:00
|
|
|
|
|
|
|
$_included = array();
|
|
|
|
/**
|
|
|
|
* Get the active contents of a .php file
|
|
|
|
*/
|
|
|
|
function manual_include($fname) {
|
2012-06-18 00:06:36 +00:00
|
|
|
if(!file_exists($fname)) return;
|
|
|
|
|
2012-06-17 23:45:32 +00:00
|
|
|
global $_included;
|
|
|
|
if(in_array($fname, $_included)) return;
|
|
|
|
$_included[] = $fname;
|
|
|
|
|
|
|
|
print "$fname\n";
|
|
|
|
|
|
|
|
$text = file_get_contents($fname);
|
2012-06-18 00:06:36 +00:00
|
|
|
|
|
|
|
// we want one continuous file
|
|
|
|
$text = str_replace('<'.'?php', '', $text);
|
|
|
|
$text = str_replace('?'.'>', '', $text);
|
|
|
|
|
2012-06-17 23:45:32 +00:00
|
|
|
// most requires are built-in, but we want /lib separately
|
|
|
|
$text = str_replace('require_', '// require_', $text);
|
|
|
|
$text = str_replace('// require_once "lib', 'require_once "lib', $text);
|
2012-06-18 00:06:36 +00:00
|
|
|
|
|
|
|
// @include_once is used for user-creatable config files
|
|
|
|
$text = preg_replace('/@include_once "(.*)";/e', "manual_include('$1')", $text);
|
|
|
|
|
|
|
|
// wibble the defines for HipHop's sake
|
2012-06-18 00:23:31 +00:00
|
|
|
$text = str_replace('function _d(', '// function _messed_d(', $text);
|
|
|
|
$text = preg_replace('/_d\("(.*)", (.*)\);/', 'if(!defined("$1")) define("$1", $2);', $text);
|
2012-06-17 23:45:32 +00:00
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-08 10:52:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Logging convenience *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2009-12-30 07:59:40 +00:00
|
|
|
define("SCORE_LOG_CRITICAL", 50);
|
|
|
|
define("SCORE_LOG_ERROR", 40);
|
|
|
|
define("SCORE_LOG_WARNING", 30);
|
|
|
|
define("SCORE_LOG_INFO", 20);
|
|
|
|
define("SCORE_LOG_DEBUG", 10);
|
|
|
|
define("SCORE_LOG_NOTSET", 0);
|
2009-05-08 10:52:29 +00:00
|
|
|
|
2009-07-21 03:18:40 +00:00
|
|
|
/**
|
|
|
|
* A shorthand way to send a LogEvent
|
2012-06-10 03:21:03 +00:00
|
|
|
*
|
|
|
|
* When parsing a user request, a flash message should give info to the user
|
|
|
|
* When taking action, a log event should be stored by the server
|
|
|
|
* Quite often, both of these happen at once, hence log_*() having $flash
|
|
|
|
*
|
|
|
|
* $flash = null (default) - log to server only, no flash message
|
|
|
|
* $flash = true - show the message to the user as well
|
|
|
|
* $flash = "some string" - log the message, flash the string
|
2009-07-21 03:18:40 +00:00
|
|
|
*/
|
2012-06-10 03:21:03 +00:00
|
|
|
function log_msg(/*string*/ $section, /*int*/ $priority, /*string*/ $message, $flash=null) {
|
2009-05-11 14:04:33 +00:00
|
|
|
send_event(new LogEvent($section, $priority, $message));
|
2012-06-17 19:05:16 +00:00
|
|
|
if(is_cli() && ($priority >= CLI_LOG_LEVEL)) {
|
|
|
|
print date("c")." $section: $message\n";
|
|
|
|
}
|
2012-06-10 03:21:03 +00:00
|
|
|
if($flash === True) {
|
|
|
|
flash_message($message);
|
|
|
|
}
|
|
|
|
else if(!is_null($flash)) {
|
|
|
|
flash_message($flash);
|
|
|
|
}
|
2009-05-08 10:52:29 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 01:05:38 +00:00
|
|
|
// More shorthand ways of logging
|
2012-06-10 03:21:03 +00:00
|
|
|
function log_debug( /*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_DEBUG, $message, $flash);}
|
|
|
|
function log_info( /*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_INFO, $message, $flash);}
|
|
|
|
function log_warning( /*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_WARNING, $message, $flash);}
|
|
|
|
function log_error( /*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_ERROR, $message, $flash);}
|
|
|
|
function log_critical(/*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_CRITICAL, $message, $flash);}
|
2009-11-15 10:26:17 +00:00
|
|
|
|
2009-05-08 10:52:29 +00:00
|
|
|
|
2007-05-01 13:30:05 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Things which should be in the core API *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Remove an item from an array
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval array
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-05-01 13:30:05 +00:00
|
|
|
function array_remove($array, $to_remove) {
|
|
|
|
$array = array_unique($array);
|
|
|
|
$a2 = array();
|
|
|
|
foreach($array as $existing) {
|
|
|
|
if($existing != $to_remove) {
|
|
|
|
$a2[] = $existing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $a2;
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2011-09-25 17:40:34 +00:00
|
|
|
* Adds an item to an array.
|
|
|
|
*
|
|
|
|
* Also removes duplicate values from the array.
|
2009-07-19 07:38:13 +00:00
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval array
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2009-01-04 19:18:37 +00:00
|
|
|
function array_add($array, $element) {
|
2011-09-25 17:40:34 +00:00
|
|
|
// Could we just use array_push() ?
|
|
|
|
// http://www.php.net/manual/en/function.array-push.php
|
2009-01-04 19:18:37 +00:00
|
|
|
$array[] = $element;
|
|
|
|
$array = array_unique($array);
|
|
|
|
return $array;
|
|
|
|
}
|
2007-07-06 04:47:01 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Return the unique elements of an array, case insensitively
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval array
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2008-05-20 03:06:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Figure out if an IP is in a specified range
|
|
|
|
*
|
|
|
|
* from http://uk.php.net/network
|
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* @retval bool
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2008-04-01 10:57:18 +00:00
|
|
|
function ip_in_range($IP, $CIDR) {
|
2009-09-14 20:19:13 +00:00
|
|
|
list ($net, $mask) = explode("/", $CIDR);
|
2008-04-01 10:57:18 +00:00
|
|
|
|
|
|
|
$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
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Delete an entire file heirachy
|
|
|
|
*
|
|
|
|
* from a patch by Christian Walde; only intended for use in the
|
|
|
|
* "extension manager" extension, but it seems to fit better here
|
|
|
|
*/
|
2008-04-06 17:43:03 +00:00
|
|
|
function deltree($f) {
|
2011-03-06 01:17:40 +00:00
|
|
|
//Because Windows (I know, bad excuse)
|
2012-03-09 22:27:12 +00:00
|
|
|
if(PHP_OS === 'WINNT') {
|
2011-03-06 01:17:40 +00:00
|
|
|
$real = realpath($f);
|
|
|
|
$path = realpath('./').'\\'.str_replace('/', '\\', $f);
|
2012-03-09 22:27:12 +00:00
|
|
|
if($path != $real) {
|
2011-03-06 01:17:40 +00:00
|
|
|
rmdir($path);
|
|
|
|
}
|
2012-03-09 22:27:12 +00:00
|
|
|
else {
|
2011-03-06 01:17:40 +00:00
|
|
|
foreach(glob($f.'/*') as $sf) {
|
|
|
|
if (is_dir($sf) && !is_link($sf)) {
|
|
|
|
deltree($sf);
|
2012-03-09 22:27:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-03-06 01:17:40 +00:00
|
|
|
unlink($sf);
|
|
|
|
}
|
|
|
|
}
|
2011-03-03 09:40:34 +00:00
|
|
|
rmdir($f);
|
|
|
|
}
|
2011-03-06 01:17:40 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (is_link($f)) {
|
2011-03-03 09:40:34 +00:00
|
|
|
unlink($f);
|
|
|
|
}
|
2011-03-06 01:17:40 +00:00
|
|
|
else if(is_dir($f)) {
|
|
|
|
foreach(glob($f.'/*') as $sf) {
|
|
|
|
if (is_dir($sf) && !is_link($sf)) {
|
|
|
|
deltree($sf);
|
2012-03-09 22:27:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-03-06 01:17:40 +00:00
|
|
|
unlink($sf);
|
|
|
|
}
|
2008-04-06 17:43:03 +00:00
|
|
|
}
|
2011-03-06 01:17:40 +00:00
|
|
|
rmdir($f);
|
2008-04-06 17:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Copy an entire file heirachy
|
|
|
|
*
|
|
|
|
* from a comment on http://uk.php.net/copy
|
|
|
|
*/
|
2008-04-06 17:43:03 +00:00
|
|
|
function full_copy($source, $target) {
|
|
|
|
if(is_dir($source)) {
|
|
|
|
@mkdir($target);
|
|
|
|
|
|
|
|
$d = dir($source);
|
|
|
|
|
|
|
|
while(FALSE !== ($entry = $d->read())) {
|
|
|
|
if($entry == '.' || $entry == '..') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-04 19:18:37 +00:00
|
|
|
$Entry = $source . '/' . $entry;
|
2008-04-06 17:43:03 +00:00
|
|
|
if(is_dir($Entry)) {
|
|
|
|
full_copy($Entry, $target . '/' . $entry);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
copy($Entry, $target . '/' . $entry);
|
|
|
|
}
|
|
|
|
$d->close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
copy($source, $target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-14 18:45:00 +00:00
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Event API *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-07-21 03:18:40 +00:00
|
|
|
/** @private */
|
2007-04-16 11:58:25 +00:00
|
|
|
$_event_listeners = array();
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Register an Extension
|
|
|
|
*/
|
2012-01-27 18:16:46 +00:00
|
|
|
function add_event_listener(Extension $extension, $pos=50, $events=array()) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $_event_listeners;
|
2012-03-30 15:41:25 +00:00
|
|
|
$pos *= 100;
|
2012-01-27 18:16:46 +00:00
|
|
|
foreach($events as $event) {
|
|
|
|
while(isset($_event_listeners[$event][$pos])) {
|
2012-03-30 15:41:25 +00:00
|
|
|
$pos += 1;
|
2012-01-27 18:16:46 +00:00
|
|
|
}
|
|
|
|
$_event_listeners[$event][$pos] = $extension;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-21 03:18:40 +00:00
|
|
|
/** @private */
|
2007-10-28 01:48:11 +00:00
|
|
|
$_event_count = 0;
|
2009-07-19 07:38:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an event to all registered Extensions
|
|
|
|
*/
|
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;
|
2012-01-27 18:16:46 +00:00
|
|
|
if(!isset($_event_listeners[get_class($event)])) return;
|
2012-02-08 12:07:01 +00:00
|
|
|
$method_name = "on".str_replace("Event", "", get_class($event));
|
2012-01-27 18:16:46 +00:00
|
|
|
|
2011-10-09 16:08:13 +00:00
|
|
|
ctx_log_start(get_class($event));
|
2012-01-27 18:16:46 +00:00
|
|
|
// SHIT: http://bugs.php.net/bug.php?id=35106
|
|
|
|
$my_event_listeners = $_event_listeners[get_class($event)];
|
2007-07-08 21:39:44 +00:00
|
|
|
ksort($my_event_listeners);
|
|
|
|
foreach($my_event_listeners as $listener) {
|
2011-10-09 16:08:13 +00:00
|
|
|
ctx_log_start(get_class($listener));
|
2012-02-08 12:07:01 +00:00
|
|
|
$listener->$method_name($event);
|
2011-10-09 16:08:13 +00:00
|
|
|
ctx_log_endok();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-10-28 01:48:11 +00:00
|
|
|
$_event_count++;
|
2011-10-09 16:08:13 +00:00
|
|
|
ctx_log_endok();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Debugging functions *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2012-01-20 05:45:09 +00:00
|
|
|
// SHIT by default this returns the time as a string. And it's not even a
|
|
|
|
// string representation of a number, it's two numbers separated by a space.
|
|
|
|
// What the fuck were the PHP developers smoking.
|
|
|
|
$_load_start = microtime(true);
|
2012-02-08 01:05:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Collects some debug information (execution time, memory usage, queries, etc)
|
|
|
|
* and formats it to stick in the footer of the page.
|
|
|
|
*
|
|
|
|
* @retval String of debug info to add to the page.
|
|
|
|
*/
|
2009-07-19 07:38:13 +00:00
|
|
|
function get_debug_info() {
|
2012-01-22 14:48:06 +00:00
|
|
|
global $config, $_event_count, $database, $_execs, $_load_start;
|
2009-07-19 07:38:13 +00:00
|
|
|
|
2012-03-11 04:33:24 +00:00
|
|
|
$i_mem = sprintf("%5.2f", ((memory_get_peak_usage(true)+512)/1024)/1024);
|
2012-01-30 11:23:58 +00:00
|
|
|
|
2012-02-22 13:33:35 +00:00
|
|
|
if($config->get_string("commit_hash", "unknown") == "unknown"){
|
2012-01-30 11:23:58 +00:00
|
|
|
$commit = "";
|
2012-03-09 22:27:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-01-30 11:23:58 +00:00
|
|
|
$commit = " (".$config->get_string("commit_hash").")";
|
|
|
|
}
|
2012-01-20 05:45:09 +00:00
|
|
|
$time = sprintf("%5.2f", microtime(true) - $_load_start);
|
2009-07-19 07:38:13 +00:00
|
|
|
$i_files = count(get_included_files());
|
|
|
|
$hits = $database->cache->get_hits();
|
|
|
|
$miss = $database->cache->get_misses();
|
2011-10-24 01:54:04 +00:00
|
|
|
|
2012-01-20 05:45:09 +00:00
|
|
|
$debug = "<br>Took $time seconds and {$i_mem}MB of RAM";
|
2009-07-19 07:38:13 +00:00
|
|
|
$debug .= "; Used $i_files files and $_execs queries";
|
|
|
|
$debug .= "; Sent $_event_count events";
|
|
|
|
$debug .= "; $hits cache hits and $miss misses";
|
2012-01-30 11:23:58 +00:00
|
|
|
$debug .= "; Shimmie version ". VERSION . $commit; // .", SCore Version ". SCORE_VERSION;
|
2009-07-19 07:38:13 +00:00
|
|
|
|
|
|
|
return $debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 19:28:29 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Request initialisation stuff *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2009-07-21 03:18:40 +00:00
|
|
|
/** @privatesection */
|
|
|
|
|
|
|
|
function _stripslashes_r($arr) {
|
2009-08-02 08:10:28 +00:00
|
|
|
return is_array($arr) ? array_map('_stripslashes_r', $arr) : stripslashes($arr);
|
2009-07-21 03:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _sanitise_environment() {
|
2012-02-01 15:07:03 +00:00
|
|
|
if(TIMEZONE) {
|
|
|
|
date_default_timezone_set(TIMEZONE);
|
|
|
|
}
|
|
|
|
|
2009-07-21 03:18:40 +00:00
|
|
|
if(DEBUG) {
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
}
|
|
|
|
|
2012-01-26 15:26:00 +00:00
|
|
|
assert_options(ASSERT_ACTIVE, 1);
|
|
|
|
assert_options(ASSERT_BAIL, 1);
|
|
|
|
|
2009-07-21 03:18:40 +00:00
|
|
|
ob_start();
|
|
|
|
|
|
|
|
if(get_magic_quotes_gpc()) {
|
|
|
|
$_GET = _stripslashes_r($_GET);
|
|
|
|
$_POST = _stripslashes_r($_POST);
|
|
|
|
$_COOKIE = _stripslashes_r($_COOKIE);
|
|
|
|
}
|
2012-02-16 16:10:31 +00:00
|
|
|
|
2012-06-17 19:05:16 +00:00
|
|
|
if(is_cli()) {
|
|
|
|
if(isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
|
die("CLI with remote addr? Confused, not taking the risk.");
|
|
|
|
}
|
2012-02-16 16:10:31 +00:00
|
|
|
$_SERVER['REMOTE_ADDR'] = "0.0.0.0";
|
|
|
|
$_SERVER['HTTP_HOST'] = "<cli command>";
|
|
|
|
}
|
2009-07-21 03:18:40 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 16:41:18 +00:00
|
|
|
function _get_themelet_files($_theme) {
|
2012-03-31 17:59:28 +00:00
|
|
|
if(file_exists('themes/'.$_theme.'/custompage.class.php')) $base_themelets[] = 'themes/'.$_theme.'/custompage.class.php';
|
|
|
|
$base_themelets[] = 'themes/'.$_theme.'/layout.class.php';
|
|
|
|
$base_themelets[] = 'themes/'.$_theme.'/themelet.class.php';
|
2012-02-01 15:07:03 +00:00
|
|
|
|
2012-05-22 11:46:56 +00:00
|
|
|
$ext_themelets = zglob("ext/{".ENABLED_EXTS."}/theme.php");
|
|
|
|
$custom_themelets = zglob('themes/'.$_theme.'/{'.ENABLED_EXTS.'}.theme.php');
|
2012-02-01 15:07:03 +00:00
|
|
|
|
2012-03-31 17:59:28 +00:00
|
|
|
return array_merge($base_themelets, $ext_themelets, $custom_themelets);
|
2012-02-01 15:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _load_extensions() {
|
|
|
|
global $_event_listeners;
|
|
|
|
|
|
|
|
ctx_log_start("Loading extensions");
|
|
|
|
|
2012-03-30 17:07:40 +00:00
|
|
|
if(COMPILE_ELS && file_exists("data/cache/event_listeners.php")) {
|
|
|
|
require_once("data/cache/event_listeners.php");
|
2012-02-01 15:07:03 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach(get_declared_classes() as $class) {
|
|
|
|
$rclass = new ReflectionClass($class);
|
|
|
|
if($rclass->isAbstract()) {
|
|
|
|
// don't do anything
|
|
|
|
}
|
2012-02-08 12:07:01 +00:00
|
|
|
elseif(is_subclass_of($class, "Extension")) {
|
2012-02-01 15:07:03 +00:00
|
|
|
$c = new $class();
|
|
|
|
$c->i_am($c);
|
|
|
|
$my_events = array();
|
|
|
|
foreach(get_class_methods($c) as $method) {
|
|
|
|
if(substr($method, 0, 2) == "on") {
|
|
|
|
$my_events[] = substr($method, 2) . "Event";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_event_listener($c, $c->get_priority(), $my_events);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(COMPILE_ELS) {
|
|
|
|
$p = "<"."?php\n";
|
|
|
|
|
|
|
|
foreach(get_declared_classes() as $class) {
|
|
|
|
$rclass = new ReflectionClass($class);
|
|
|
|
if($rclass->isAbstract()) {}
|
2012-02-08 12:07:01 +00:00
|
|
|
elseif(is_subclass_of($class, "Extension")) {
|
2012-02-01 15:07:03 +00:00
|
|
|
$p .= "\$$class = new $class(); ";
|
|
|
|
$p .= "\${$class}->i_am(\$$class);\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$p .= "\$_event_listeners = array(\n";
|
|
|
|
foreach($_event_listeners as $event => $listeners) {
|
|
|
|
$p .= "\t'$event' => array(\n";
|
|
|
|
foreach($listeners as $id => $listener) {
|
|
|
|
$p .= "\t\t$id => \$".get_class($listener).",\n";
|
|
|
|
}
|
|
|
|
$p .= "\t),\n";
|
|
|
|
}
|
|
|
|
$p .= ");\n";
|
|
|
|
|
|
|
|
$p .= "?".">";
|
2012-03-30 17:21:35 +00:00
|
|
|
file_put_contents(data_path("cache/event_listeners.php"), $p);
|
2012-02-01 15:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx_log_endok();
|
|
|
|
}
|
|
|
|
|
2012-02-08 01:05:38 +00:00
|
|
|
/**
|
|
|
|
* Used to display fatal errors to the web user.
|
|
|
|
*/
|
2012-02-01 15:07:03 +00:00
|
|
|
function _fatal_error(Exception $e) {
|
|
|
|
$version = VERSION;
|
|
|
|
$message = $e->getMessage();
|
2012-03-31 16:07:11 +00:00
|
|
|
|
2012-02-01 15:07:03 +00:00
|
|
|
//$trace = var_dump($e->getTrace());
|
2012-03-31 16:07:11 +00:00
|
|
|
|
|
|
|
//$hash = exec("git rev-parse HEAD");
|
|
|
|
//$h_hash = $hash ? "<p><b>Hash:</b> $hash" : "";
|
|
|
|
//'.$h_hash.'
|
|
|
|
|
2012-02-01 15:07:03 +00:00
|
|
|
header("HTTP/1.0 500 Internal Error");
|
|
|
|
echo '
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Internal error - SCore-'.$version.'</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Internal Error</h1>
|
2012-03-31 16:07:11 +00:00
|
|
|
<p><b>Message:</b> '.$message.'
|
|
|
|
<p><b>Version:</b> '.$version.'
|
2012-02-01 15:07:03 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
';
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-01-18 14:58:32 +00:00
|
|
|
* Turn ^^ into ^ and ^s into /
|
|
|
|
*
|
|
|
|
* Necessary because various servers and various clients
|
|
|
|
* think that / is special...
|
|
|
|
*/
|
|
|
|
function _decaret($str) {
|
|
|
|
$out = "";
|
2012-01-16 05:07:04 +00:00
|
|
|
$length = strlen($str);
|
|
|
|
for($i=0; $i<$length; $i++) {
|
2009-01-18 14:58:32 +00:00
|
|
|
if($str[$i] == "^") {
|
|
|
|
$i++;
|
|
|
|
if($str[$i] == "^") $out .= "^";
|
|
|
|
if($str[$i] == "s") $out .= "/";
|
2010-04-23 02:31:10 +00:00
|
|
|
if($str[$i] == "b") $out .= "\\";
|
2009-01-18 14:58:32 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out .= $str[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2009-05-11 14:04:33 +00:00
|
|
|
function _get_user() {
|
|
|
|
global $config, $database;
|
2007-04-16 11:58:25 +00:00
|
|
|
$user = null;
|
2009-10-08 12:59:12 +00:00
|
|
|
if(get_prefixed_cookie("user") && get_prefixed_cookie("session")) {
|
|
|
|
$tmp_user = User::by_session(get_prefixed_cookie("user"), get_prefixed_cookie("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)) {
|
2009-05-11 14:04:33 +00:00
|
|
|
$user = User::by_id($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));
|
2010-05-28 12:04:57 +00:00
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2009-08-09 12:11:54 +00:00
|
|
|
|
2009-09-19 19:13:37 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Code coverage *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2009-09-27 13:04:27 +00:00
|
|
|
function _start_coverage() {
|
2011-12-24 21:55:33 +00:00
|
|
|
if(function_exists("xdebug_start_code_coverage")) {
|
2009-09-19 19:13:37 +00:00
|
|
|
#xdebug_start_code_coverage(XDEBUG_CC_UNUSED|XDEBUG_CC_DEAD_CODE);
|
|
|
|
xdebug_start_code_coverage(XDEBUG_CC_UNUSED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-27 13:04:27 +00:00
|
|
|
function _end_coverage() {
|
2011-12-24 21:55:33 +00:00
|
|
|
if(function_exists("xdebug_get_code_coverage")) {
|
2011-10-04 13:03:53 +00:00
|
|
|
// Absolute path is necessary because working directory
|
|
|
|
// inside register_shutdown_function is unpredictable.
|
|
|
|
$absolute_path = dirname(dirname(__FILE__)) . "/data/coverage";
|
|
|
|
if(!file_exists($absolute_path)) mkdir($absolute_path);
|
2009-09-19 19:13:37 +00:00
|
|
|
$n = 0;
|
|
|
|
$t = time();
|
2011-10-04 13:03:53 +00:00
|
|
|
while(file_exists("$absolute_path/$t.$n.log")) $n++;
|
2012-03-11 00:36:57 +00:00
|
|
|
file_put_contents("$absolute_path/$t.$n.log", gzdeflate(serialize(xdebug_get_code_coverage())));
|
2009-09-19 19:13:37 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
?>
|