Merge branch 'master' of github.com:shish/shimmie2

This commit is contained in:
Shish 2012-06-22 19:14:15 +01:00
commit 7ed7f4bbb8
10 changed files with 204 additions and 168 deletions

View File

@ -30,7 +30,32 @@ class PageRequestEvent extends Event {
var $arg_count;
var $part_count;
public function __construct($args) {
public function __construct($path) {
global $config;
// trim starting slashes
while(strlen($path) > 0 && $path[0] == '/') {
$path = substr($path, 1);
}
// if path is not specified, use the default front page
if(strlen($path) === 0) {
$path = $config->get_string('front_page');
}
// break the path into parts
$args = explode('/', $path);
// voodoo so that an arg can contain a slash; is
// this still needed?
if(strpos($path, "^") !== FALSE) {
$unescaped = array();
foreach($parts as $part) {
$unescaped[] = _decaret($part);
}
$args = $unescaped;
}
$this->args = $args;
$this->arg_count = count($args);
}
@ -110,6 +135,62 @@ class PageRequestEvent extends Event {
}
/**
* Sent when index.php is called from the command line
*/
class CommandEvent extends Event {
public $cmd = "help";
public $args = array();
public function __construct(/*array(string)*/ $args) {
global $user;
$opts = array();
$log_level = SCORE_LOG_WARNING;
for($i=1; $i<count($args); $i++) {
switch($args[$i]) {
case '-u':
$user = User::by_name($args[++$i]);
if(is_null($user)) {
die("Unknown user");
}
break;
case '-q':
$log_level += 10;
break;
case '-v':
$log_level -= 10;
break;
default:
$opts[] = $args[$i];
break;
}
}
define("CLI_LOG_LEVEL", $log_level);
if(count($opts) > 0) {
$this->cmd = $opts[0];
$this->args = array_slice($opts, 1);
}
else {
print "\n";
print "Usage: php {$args[0]} [flags] [command]\n";
print "\n";
print "Flags:\n";
print " -u [username]\n";
print " Log in as the specified user\n";
print " -q / -v\n";
print " Be quieter / more verbose\n";
print " Scale is debug - info - warning - error - critical\n";
print " Default is to show warnings and above\n";
print " \n";
print "Currently known commands:\n";
}
}
}
/**
* A signal that some text needs formatting, the event carries
* both the text and the result

View File

@ -25,8 +25,6 @@ _d("DEBUG", false); // boolean print various debugging details
_d("DEBUG_SQL", false); // boolean dump SQL queries to data/sql.log
_d("COVERAGE", false); // boolean activate xdebug coverage monitor
_d("CONTEXT", null); // string file to log performance data into
_d("CACHE_MEMCACHE", false); // boolean store complete rendered pages in memcache
_d("CACHE_DIR", false); // boolean store complete rendered pages on disk
_d("CACHE_HTTP", false); // boolean output explicit HTTP caching headers
_d("COOKIE_PREFIX", 'shm'); // string if you run multiple galleries with non-shared logins, give them different prefixes
_d("SPEED_HAX", false); // boolean do some questionable things in the name of performance

View File

@ -155,7 +155,5 @@ new UserClass("admin", "base", array(
"protected" => True,
));
if(file_exists("data/config/user-classes.conf.php")) {
require_once("data/config/user-classes.conf.php");
}
@include_once "data/config/user-classes.conf.php";
?>

View File

@ -285,7 +285,7 @@ function make_link($page=null, $query=null) {
if(NICE_URLS || $config->get_bool('nice_urls', false)) {
#$full = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"];
$full = $_SERVER["PHP_SELF"];
$base = str_replace("/".basename($_SERVER["SCRIPT_FILENAME"]), "", $full);
$base = str_replace(basename($_SERVER["SCRIPT_FILENAME"]), "", $full);
}
else {
$base = "./".basename($_SERVER["SCRIPT_FILENAME"])."?q=";
@ -386,6 +386,13 @@ function mtimefile($file) {
return "$data_href/$file?$mtime";
}
function get_theme() {
global $config;
$theme = $config->get_string("theme", "default");
if(!file_exists("themes/$theme")) $theme = "default";
return $theme;
}
/*
* like glob, with support for matching very long patterns with braces
*/
@ -541,12 +548,8 @@ date and you should plan on moving elsewhere.
/**
* @private
*/
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";
function is_cli() {
return (PHP_SAPI === 'cli');
}
/**
@ -812,6 +815,41 @@ function transload($url, $mfile) {
return false;
}
$_included = array();
/**
* Get the active contents of a .php file
*/
function manual_include($fname) {
if(!file_exists($fname)) return;
global $_included;
if(in_array($fname, $_included)) return;
$_included[] = $fname;
print "$fname\n";
$text = file_get_contents($fname);
// we want one continuous file
$text = str_replace('<'.'?php', '', $text);
$text = str_replace('?'.'>', '', $text);
// 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);
// @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
$text = str_replace('function _d(', '// function _messed_d(', $text);
$text = preg_replace('/_d\("(.*)", (.*)\);/', 'if(!defined("$1")) define("$1", $2);', $text);
return $text;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Logging convenience *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@ -836,6 +874,9 @@ define("SCORE_LOG_NOTSET", 0);
*/
function log_msg(/*string*/ $section, /*int*/ $priority, /*string*/ $message, $flash=null) {
send_event(new LogEvent($section, $priority, $message));
if(is_cli() && ($priority >= CLI_LOG_LEVEL)) {
print date("c")." $section: $message\n";
}
if($flash === True) {
flash_message($message);
}
@ -1120,13 +1161,12 @@ function _sanitise_environment() {
$_COOKIE = _stripslashes_r($_COOKIE);
}
if(php_sapi_name() === "cli") {
global $argc, $argv;
if(is_cli()) {
if(isset($_SERVER['REMOTE_ADDR'])) {
die("CLI with remote addr? Confused, not taking the risk.");
}
$_SERVER['REMOTE_ADDR'] = "0.0.0.0";
$_SERVER['HTTP_HOST'] = "<cli command>";
if($argc > 1) {
$_GET['q'] = $argv[1];
}
}
}
@ -1249,46 +1289,6 @@ function _decaret($str) {
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 = "";
}
while(strlen($path) > 0 && $path[0] == '/') {
$path = substr($path, 1);
}
$parts = explode('/', $path);
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(empty($args) || strlen($args[0]) === 0) {
$args = explode('/', $config->get_string('front_page'));
}
return new PageRequestEvent($args);
}
function _get_user() {
global $config, $database;
$user = null;
@ -1307,100 +1307,6 @@ function _get_user() {
}
$_cache_memcache = false;
$_cache_key = null;
$_cache_filename = null;
function _cache_active() {
return (
(CACHE_MEMCACHE || CACHE_DIR) &&
$_SERVER["REQUEST_METHOD"] == "GET" &&
!get_prefixed_cookie("session") &&
!get_prefixed_cookie("nocache")
);
}
function _cache_log($text) {
$fp = @fopen("data/cache.log", "a");
if($fp) {
fputs($fp, $text);
fclose($fp);
}
}
function _start_cache() {
global $_cache_memcache, $_cache_key, $_cache_filename;
if(_cache_active()) {
if(CACHE_MEMCACHE) {
$_cache_memcache = new Memcache;
$_cache_memcache->pconnect('localhost', 11211);
$_cache_key = "uri:".$_SERVER["REQUEST_URI"];
$data = $_cache_memcache->get($_cache_key);
if(DEBUG) {
$stat = $zdata ? "hit" : "miss";
_cache_log(time() . " " . sprintf(" %-4s ", $stat) . $_cache_key . "\n");
}
if($data) {
header("Content-type: text/html");
print $data;
exit;
}
}
if(CACHE_DIR) {
$_cache_hash = md5($_SERVER["QUERY_STRING"]);
$ab = substr($_cache_hash, 0, 2);
$cd = substr($_cache_hash, 2, 2);
$_cache_filename = data_path("http_cache/$ab/$cd/$_cache_hash");
@chmod(data_path('http_cache'), 750);
if(file_exists($_cache_filename) && (filemtime($_cache_filename) > 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 {
header("Content-type: text/html");
header('Last-Modified: '.$gmdate_mod);
$zdata = @file_get_contents($_cache_filename);
if(CACHE_MEMCACHE) {
$_cache_memcache->set($_cache_hash, $zdata, 0, 600);
}
$data = @gzuncompress($zdata);
if($data) {
print $data;
exit;
}
}
}
ob_start();
}
}
}
function _end_cache() {
global $_cache_memcache, $_cache_key, $_cache_filename;
if(_cache_active()) {
$data = ob_get_contents();
if(CACHE_MEMCACHE) {
$_cache_memcache->set($_cache_key, $data, 0, 600);
}
if(CACHE_DIR) {
$zdata = gzcompress($data, 2);
file_put_contents($_cache_filename, $zdata);
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Code coverage *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

View File

@ -69,6 +69,18 @@ class AdminPage extends Extension {
}
}
public function onCommand(CommandEvent $event) {
if($event->cmd == "help") {
print " get-page [query string]\n";
print " eg 'get-page post/list'\n\n";
}
if($event->cmd == "get-page") {
global $page;
send_event(new PageRequestEvent($event->args[0]));
$page->display();
}
}
public function onAdminBuilding(AdminBuildingEvent $event) {
$this->theme->display_page();
$this->theme->display_form();

View File

@ -27,6 +27,18 @@ class BulkAdd extends Extension {
}
}
public function onCommand(CommandEvent $event) {
if($event->cmd == "help") {
print " bulk-add [directory]\n";
print " Import this directory\n\n";
}
if($event->cmd == "bulk-add") {
if(count($event->args) == 1) {
$this->add_dir($event->args[0]);
}
}
}
public function onAdminBuilding(AdminBuildingEvent $event) {
$this->theme->display_admin_block();
}
@ -75,10 +87,16 @@ class BulkAdd extends Extension {
}
else {
$pathinfo = pathinfo($fullpath);
$tags = $subdir;
$tags = str_replace("/", " ", $tags);
$tags = str_replace("__", " ", $tags);
$tags = trim($tags);
$matches = array();
if(preg_match("/\d+ - (.*)\.([a-zA-Z]+)/", $pathinfo["basename"], $matches)) {
$tags = $matches[1];
}
else {
$tags = $subdir;
$tags = str_replace("/", " ", $tags);
$tags = str_replace("__", " ", $tags);
$tags = trim($tags);
}
$list .= "<br>".html_escape("$shortpath (".str_replace(" ", ", ", $tags).")... ");
try{
$this->add_image($fullpath, $pathinfo["basename"], $tags);

View File

@ -128,6 +128,17 @@ class ExtManager extends Extension {
}
}
public function onCommand(CommandEvent $event) {
if($event->cmd == "help") {
print " disable-all-ext\n";
print " disable all extensions\n\n";
}
if($event->cmd == "disable-all-ext") {
$this->write_config(array());
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
global $user;
if($user->can("manage_extension_list")) {
@ -167,12 +178,23 @@ class ExtManager extends Extension {
}
}
$this->write_config($extras);
}
private function write_config($extras) {
file_put_contents(
"data/config/extensions.conf.php",
'<'.'?php'."\n".
'define("EXTRA_EXTS", "'.implode(",", $extras).'");'."\n".
'?'.">"
);
// when the list of active extensions changes, we can be
// pretty sure that the list of who reacts to what will
// change too
if(file_exists("data/cache/event_listeners.php")) {
unlink("data/cache/event_listeners.php");
}
}
}
?>

View File

@ -221,7 +221,7 @@ class IPBan extends Extension {
exit;
}
}
log_error("ipban", "block() called but no bans matched");
log_error("ipban", "block($remote) called but no bans matched");
exit;
}
// }}}
@ -261,8 +261,8 @@ class IPBan extends Extension {
if($cached) return $cached;
$bans = $this->get_active_bans();
$ips = array("0.0.0.0" => false);
$nets = array("0.0.0.0/32" => false);
$ips = array(); # "0.0.0.0" => false);
$nets = array(); # "0.0.0.0/32" => false);
foreach($bans as $row) {
if(strstr($row['ip'], '/')) {
$nets[$row['ip']] = true;

View File

@ -62,7 +62,6 @@ if(COVERAGE) {
}
_version_check();
_sanitise_environment();
_start_cache();
try {
// load base files
@ -82,9 +81,7 @@ try {
// load the theme parts
ctx_log_start("Loading themelets");
$_theme = $config->get_string("theme", "default");
if(!file_exists("themes/$_theme")) $_theme = "default";
foreach(_get_themelet_files($_theme) as $themelet) {
foreach(_get_themelet_files(get_theme()) as $themelet) {
require_once $themelet;
}
ctx_log_endok();
@ -95,13 +92,17 @@ try {
$page = class_exists("CustomPage") ? new CustomPage() : new Page();
$user = _get_user();
send_event(new InitExtEvent());
send_event(_get_page_request());
$page->display();
if(!is_cli()) { // web request
send_event(new PageRequestEvent(@$_GET["q"]));
$page->display();
}
else { // command line request
send_event(new CommandEvent($argv));
}
$database->db->commit();
// saving cache data and profiling data to disk can happen later
if(function_exists("fastcgi_finish_request")) fastcgi_finish_request();
_end_cache();
$database->db->commit();
ctx_log_endok();
}
catch(Exception $e) {

View File

@ -40,7 +40,7 @@ $(document).ready(function() {
try {
var sidebar_hidden = ($.cookie("ui-sidebar-hidden") || $.cookie("sidebar-hidden") || "").split("|");
for(var i in sidebar_hidden) {
if(sidebar_hidden[i].length > 0) {
if(sidebar_hidden.hasOwnProperty(i) && sidebar_hidden[i].length > 0) {
$(sidebar_hidden[i]+" .blockbody").hide();
}
};