This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/util.inc.php

870 lines
20 KiB
PHP
Raw Normal View History

<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* 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
*/
function html_escape($input) {
return htmlentities($input, ENT_QUOTES, "UTF-8");
}
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
*/
function int_escape($input) {
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
*/
function url_escape($input) {
2009-01-18 14:58:32 +00:00
$input = str_replace('^', '^^', $input);
$input = str_replace('/', '^s', $input);
$input = rawurlencode($input);
return $input;
}
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
*/
function sql_escape($input) {
global $database;
return $database->db->Quote($input);
}
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
*/
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;
}
}
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
*/
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";
}
}
/**
* Turn a date into a time, a date, an "X minutes ago...", etc
*
* @retval string
*/
function autodate($date) {
2009-08-18 22:06:15 +00:00
global $config;
if($config->get_bool('use_autodate', true)) {
$diff = time() - strtotime($date);
if ($diff<60) return $diff . " second" . plural($diff) . " ago"; $diff = round($diff/60);
if ($diff<60) return $diff . " minute" . plural($diff) . " ago"; $diff = round($diff/60);
if ($diff<24) return $diff . " hour" . plural($diff) . " ago"; $diff = round($diff/24);
if ($diff<7) return $diff . " day" . plural($diff) . " ago"; $diff = round($diff/7);
if ($diff<4) return $diff . " week" . plural($diff) . " ago";
}
2009-09-15 17:29:22 +00:00
$format = $config->get_string('autodate_format', "F j, Y");
return "on " . date($format, strtotime($date));
2009-07-28 22:56:46 +00:00
}
/**
* Return a pluraliser if necessary
*
* @retval string
*/
function plural($num, $single_form="", $plural_form="s") {
return ($num == 1) ? $single_form : $plural_form;
}
2009-10-08 16:43:18 +00:00
/**
* Different databases have different ways to represent booleans; this
* will try and standardise them
*/
function undb_bool($val) {
if($val === true || $val == 'Y' || $val == 'y' || $val == 'T' || $val == 't' || $val === 1) return true;
if($val === false || $val == 'N' || $val == 'n' || $val == 'F' || $val == 'f' || $val === 0) return false;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* HTML Generation *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-07-19 07:38:13 +00:00
/**
* Figure out the correct way to link to a page, taking into account
* things like the nice URLs setting
*
2009-07-21 03:18:40 +00:00
* @retval string
2009-07-19 07:38:13 +00:00
*/
function make_link($page=null, $query=null) {
global $config;
if(is_null($page)) $page = $config->get_string('main_page');
2008-12-27 10:17:53 +00:00
if($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"];
2008-12-27 10:17:53 +00:00
$base = str_replace("/index.php", "", $full);
}
else {
$base = "./index.php?q=";
}
if(is_null($query)) {
2009-06-29 22:15:25 +00:00
return str_replace("//", "/", "$base/$page");
}
else {
if(strpos($base, "?")) {
return "$base/$page&$query";
}
else {
return "$base/$page?$query";
}
}
}
2009-07-24 07:08:29 +00:00
/**
* Turn a relative link into an absolute one, including hostname
*
* @retval string
*/
function make_http($link) {
if(strpos($link, "ttp://") > 0) return $link;
if($link[0] != '/') $link = get_base_href().'/'.$link;
$link = "http://".$_SERVER["HTTP_HOST"].$link;
$link = str_replace("/./", "/", $link);
return $link;
}
2009-07-21 03:18:40 +00:00
/**
* Make a link to a static file in the current theme's
* directory
*/
function theme_file($filepath) {
global $config;
$theme = $config->get_string("theme","default");
return make_link("themes/$theme/$filepath");
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Misc *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
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() {
if(version_compare(PHP_VERSION, "5.0.0") == -1) {
2009-07-21 03:18:40 +00:00
print "
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.
2009-07-21 03:18:40 +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
*/
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";
}
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
*/
function _count_execs($db, $sql, $inputarray) {
global $_execs;
if(DEBUG) {
$fp = fopen("sql.log", "a");
if(is_array($inputarray)) {
fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)." -- ".join(", ", $inputarray)."\n");
}
else {
fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)."\n");
}
fclose($fp);
}
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
/**
* Find the theme object for a given extension
*/
function get_theme_object(Extension $class, $fatal=true) {
$base = get_class($class);
if(class_exists("Custom{$base}Theme")) {
$class = "Custom{$base}Theme";
return new $class();
}
elseif ($fatal || class_exists("{$base}Theme")) {
$class = "{$base}Theme";
return new $class();
} else {
return false;
}
}
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
*/
function blockcmp(Block $a, Block $b) {
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
*/
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;
}
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;
}
assert($memory > 0);
return $memory;
}
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
*/
function get_session_ip($config) {
$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;
}
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.
*/
function get_prefixed_cookie($name) {
global $config;
$full_name = $config->get_string('cookie_prefix','shm')."_".$name;
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);
}
2009-07-19 07:38:13 +00:00
/**
* Figure out the path to the shimmie install root.
*
* PHP really, really sucks.
2009-07-19 07:38:13 +00:00
*
2009-07-21 03:18:40 +00:00
* @retval string
*/
function get_base_href() {
$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);
if($dir == "/" || $dir == "\\") $dir = "";
return $dir;
}
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
*/
function format_text($string) {
$tfe = new TextFormattingEvent($string);
send_event($tfe);
return $tfe->formatted;
}
2009-05-08 10:52:29 +00:00
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Logging convenience *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-07-19 16:21:49 +00:00
if(!defined("LOG_CRITICAL")) define("LOG_CRITICAL", 50);
if(!defined("LOG_ERROR")) define("LOG_ERROR", 40);
if(!defined("LOG_WARNING")) define("LOG_WARNING", 30);
if(!defined("LOG_INFO")) define("LOG_INFO", 20);
if(!defined("LOG_DEBUG")) define("LOG_DEBUG", 10);
if(!defined("LOG_NOTSET")) define("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
*/
2009-05-08 10:52:29 +00:00
function log_msg($section, $priority, $message) {
send_event(new LogEvent($section, $priority, $message));
2009-05-08 10:52:29 +00:00
}
2009-07-21 03:18:40 +00:00
/**
* A shorthand way to send a LogEvent
*/
2009-05-08 10:52:29 +00:00
function log_info($section, $message) {
log_msg($section, LOG_INFO, $message);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* 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
*/
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
/**
* Add an item to an array
*
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) {
$array[] = $element;
$array = array_unique($array);
return $array;
}
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
*/
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
*/
function ip_in_range($IP, $CIDR) {
2009-09-14 20:19:13 +00:00
list ($net, $mask) = explode("/", $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);
}
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
*/
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);
}
}
2009-07-19 07:38:13 +00:00
/**
* Copy an entire file heirachy
*
* 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;
}
2009-01-04 19:18:37 +00:00
$Entry = $source . '/' . $entry;
if(is_dir($Entry)) {
full_copy($Entry, $target . '/' . $entry);
continue;
}
copy($Entry, $target . '/' . $entry);
}
$d->close();
}
else {
copy($source, $target);
}
}
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
*/
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;
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Event API *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-07-21 03:18:40 +00:00
/** @private */
$_event_listeners = array();
2009-07-19 07:38:13 +00:00
/**
* Register an Extension
*/
2009-01-03 21:06:36 +00:00
function add_event_listener(Extension $extension, $pos=50) {
global $_event_listeners;
while(isset($_event_listeners[$pos])) {
$pos++;
}
$_event_listeners[$pos] = $extension;
}
2009-07-21 03:18:40 +00:00
/** @private */
$_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) {
global $_event_listeners, $_event_count;
$my_event_listeners = $_event_listeners; // http://bugs.php.net/bug.php?id=35106
ksort($my_event_listeners);
foreach($my_event_listeners as $listener) {
$listener->receive_event($event);
}
$_event_count++;
}
2009-07-19 07:38:13 +00:00
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Debugging functions *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function get_debug_info() {
global $config, $_event_count;
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;
global $database;
$hits = $database->cache->get_hits();
$miss = $database->cache->get_misses();
$debug = "<br>Took $i_utime + $i_stime seconds and {$i_mem}MB of RAM";
$debug .= "; Used $i_files files and $_execs queries";
$debug .= "; Sent $_event_count events";
$debug .= "; $hits cache hits and $miss misses";
return $debug;
}
// print_obj ($object, $title, $return)
function print_obj($object,$title="Object Information", $return=false) {
global $user;
if(DEBUG && isset($_GET['debug']) && $user->is_admin()) {
$pr = print_r($object,true);
$count = substr_count($pr,"\n")<=25?substr_count($pr,"\n"):25;
$pr = "<textarea rows='".$count."' cols='80'>$pr</textarea>";
if($return) {
return $pr;
} else {
global $page;
$page->add_block(new Block($title,$pr,"main",1000));
return true;
}
}
}
// preset tests.
// Prints the contents of $event->args, even though they are clearly visible in
// the URL bar.
function print_url_args() {
global $event;
print_obj($event->args,"URL Arguments");
}
// Prints all the POST data.
function print_POST() {
print_obj($_POST,"\$_POST");
}
// Prints GET, though this is also visible in the url ( url?var&var&var)
function print_GET() {
print_obj($_GET,"\$_GET");
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* 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() {
if(DEBUG) {
error_reporting(E_ALL);
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_BAIL, 1);
}
ob_start();
if(get_magic_quotes_gpc()) {
$_GET = _stripslashes_r($_GET);
$_POST = _stripslashes_r($_POST);
$_COOKIE = _stripslashes_r($_COOKIE);
}
}
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 = "";
for($i=0; $i<strlen($str); $i++) {
if($str[$i] == "^") {
$i++;
if($str[$i] == "^") $out .= "^";
if($str[$i] == "s") $out .= "/";
}
else {
$out .= $str[$i];
}
}
return $out;
}
function _get_query_parts() {
if(isset($_GET["q"])) {
$path = $_GET["q"];
}
else if(isset($_SERVER["PATH_INFO"])) {
$path = $_SERVER["PATH_INFO"];
}
else {
$path = "";
}
2009-01-04 19:18:37 +00:00
while(strlen($path) > 0 && $path[0] == '/') {
$path = substr($path, 1);
}
2009-09-14 20:19:13 +00:00
$parts = explode('/', $path);
2009-01-18 14:58:32 +00:00
if(strpos($path, "^") === FALSE) {
return $parts;
}
else {
$unescaped = array();
foreach($parts as $part) {
$unescaped[] = _decaret($part);
}
return $unescaped;
}
}
function _get_page_request() {
global $config;
$args = _get_query_parts();
if(count($args) == 0 || strlen($args[0]) == 0) {
$args = split('/', $config->get_string('front_page'));
}
2009-01-04 19:18:37 +00:00
return new PageRequestEvent($args);
}
function _get_user() {
global $config, $database;
$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"));
if(!is_null($tmp_user)) {
$user = $tmp_user;
}
}
if(is_null($user)) {
$user = User::by_id($config->get_int("anon_id", 0));
}
assert(!is_null($user));
return $user;
}
2009-08-09 12:11:54 +00:00
$_cache_hash = null;
$_cache_memcache = false;
$_cache_filename = null;
function _cache_active() {
2009-10-08 12:59:12 +00:00
return (
(CACHE_MEMCACHE || CACHE_DIR) &&
$_SERVER["REQUEST_METHOD"] == "GET" &&
!get_prefixed_cookie("session") &&
!get_prefixed_cookie("nocache")
);
2009-08-09 12:11:54 +00:00
}
2009-08-01 01:31:49 +00:00
function _start_cache() {
2009-08-09 12:11:54 +00:00
global $_cache_hash, $_cache_memcache, $_cache_filename;
if(_cache_active()) {
$_cache_hash = md5($_SERVER["QUERY_STRING"]);
if(CACHE_MEMCACHE) {
$_cache_memcache = new Memcache;
$_cache_memcache->pconnect('localhost', 11211);
$zdata = $_cache_memcache->get($hash);
if($zdata) {
header("Content-type: text/html");
$data = @gzuncompress($zdata);
if($data) {
print $data;
exit;
}
}
2009-08-01 01:31:49 +00:00
}
2009-08-09 12:11:54 +00:00
if(CACHE_DIR) {
$ab = substr($_cache_hash, 0, 2);
$cd = substr($_cache_hash, 2, 2);
$_cache_filename = "data/$ab/$cd/$_cache_hash";
2009-08-01 01:31:49 +00:00
2009-08-09 12:11:54 +00:00
if(!file_exists(dirname($_cache_filename))) {
mkdir(dirname($_cache_filename), 0750, true);
}
if(file_exists($_cache_filename) && (filemtime($cachename) > time() - 3600)) {
$gmdate_mod = gmdate('D, d M Y H:i:s', filemtime($_cache_filename)) . ' GMT';
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]);
if($if_modified_since == $gmdate_mod) {
header("HTTP/1.0 304 Not Modified");
header("Content-type: text/html");
exit;
}
}
else {
2009-08-01 01:31:49 +00:00
header("Content-type: text/html");
2009-08-09 12:11:54 +00:00
header("Last-Modified: $gmdate_mod");
2009-08-10 12:33:08 +00:00
$zdata = @file_get_contents($_cache_filename);
if(CACHE_MEMCACHE) {
$_cache_memcache->set($_cache_hash, $zdata, 0, 600);
}
$data = @gzuncompress($zdata);
2009-08-09 12:11:54 +00:00
if($data) {
print $data;
exit;
}
2009-08-01 01:31:49 +00:00
}
}
2009-08-09 12:11:54 +00:00
ob_start();
2009-08-01 01:31:49 +00:00
}
}
}
function _end_cache() {
2009-08-09 12:11:54 +00:00
global $_cache_hash, $_cache_memcache, $_cache_filename;
if(_cache_active()) {
$data = gzcompress(ob_get_contents(), 9);
if(CACHE_MEMCACHE) {
$_cache_memcache->set($_cache_hash, $data, 0, 600);
}
if(CACHE_DIR) {
file_put_contents($_cache_filename, $data);
}
2009-08-01 01:31:49 +00:00
}
}
2009-09-19 19:13:37 +00:00
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Code coverage *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function _start_coverage() {
2009-09-19 19:13:37 +00:00
if(function_exists("xdebug_start_code_coverage")) {
#xdebug_start_code_coverage(XDEBUG_CC_UNUSED|XDEBUG_CC_DEAD_CODE);
xdebug_start_code_coverage(XDEBUG_CC_UNUSED);
}
}
function _end_coverage() {
2009-09-19 19:13:37 +00:00
if(function_exists("xdebug_get_code_coverage")) {
if(!file_exists("data/coverage")) mkdir("data/coverage");
$n = 0;
$t = time();
while(file_exists("data/coverage/$t.$n.log")) $n++;
file_put_contents("data/coverage/$t.$n.log", serialize(xdebug_get_code_coverage()));
}
}
?>