lots of tidying and removal of duplicate code

This commit is contained in:
Shish 2012-06-18 00:00:21 +01:00
parent 082e6fa31b
commit 5519c3a320
4 changed files with 42 additions and 53 deletions

View File

@ -30,7 +30,32 @@ class PageRequestEvent extends Event {
var $arg_count; var $arg_count;
var $part_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->args = $args;
$this->arg_count = count($args); $this->arg_count = count($args);
} }
@ -149,16 +174,18 @@ class CommandEvent extends Event {
$this->args = array_slice($opts, 1); $this->args = array_slice($opts, 1);
} }
else { else {
print "\nUsage: php index.php [flags] [command]\n\n"; print "\n";
print "Usage: php {$args[0]} [flags] [command]\n";
print "\n";
print "Flags:\n"; print "Flags:\n";
print " -u [username]\n"; print " -u [username]\n";
print " Log in as the specified user\n"; print " Log in as the specified user\n";
print " -q / -v\n"; print " -q / -v\n";
print " Be quieter / more verbose\n"; print " Be quieter / more verbose\n";
print " (scale is debug / info / warning / error / critical)\n"; print " Scale is debug - info - warning - error - critical\n";
print " default is to show warnings or above\n"; print " Default is to show warnings and above\n";
print " \n"; print " \n";
print "\nCurrently know commands:\n"; print "Currently known commands:\n";
} }
} }
} }

View File

@ -386,6 +386,13 @@ function mtimefile($file) {
return "$data_href/$file?$mtime"; 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 * like glob, with support for matching very long patterns with braces
*/ */
@ -1247,47 +1254,6 @@ function _decaret($str) {
return $out; 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() { function _get_user() {
global $config, $database; global $config, $database;
$user = null; $user = null;

View File

@ -76,8 +76,7 @@ class AdminPage extends Extension {
} }
if($event->cmd == "get-page") { if($event->cmd == "get-page") {
global $page; global $page;
$_GET['q'] = $event->args[0]; send_event(new PageRequestEvent($event->args[0]));
send_event(_get_page_request());
$page->display(); $page->display();
} }
} }

View File

@ -82,9 +82,7 @@ try {
// load the theme parts // load the theme parts
ctx_log_start("Loading themelets"); ctx_log_start("Loading themelets");
$_theme = $config->get_string("theme", "default"); foreach(_get_themelet_files(get_theme()) as $themelet) {
if(!file_exists("themes/$_theme")) $_theme = "default";
foreach(_get_themelet_files($_theme) as $themelet) {
require_once $themelet; require_once $themelet;
} }
ctx_log_endok(); ctx_log_endok();
@ -96,11 +94,10 @@ try {
$user = _get_user(); $user = _get_user();
send_event(new InitExtEvent()); send_event(new InitExtEvent());
if(!is_cli()) { // web request if(!is_cli()) { // web request
send_event(_get_page_request()); send_event(new PageRequestEvent(@$_GET["q"]));
$page->display(); $page->display();
} }
else { // command line request else { // command line request
global $argv;
send_event(new CommandEvent($argv)); send_event(new CommandEvent($argv));
} }