From 71eebcefb57763451d3cc2749d2ed9e181887157 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Sun, 8 Nov 2009 23:35:59 +0200 Subject: Lots of fixes, better url parsing --- App.php | 61 +++++++++++++++++++++++++++++++++++----------------------- Controller.php | 22 ++++++++++++--------- Db.php | 5 ++++- 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/App.php b/App.php index 963396d..803aa27 100644 --- a/App.php +++ b/App.php @@ -20,10 +20,12 @@ class App @list($controller, $param, $action) = App::url(); + // No action given, read default one if (empty($param)) { - $controller = self::conf("defaults.action"); + $action = self::conf("defaults.action"); } + // No controller given, read default one if (!$controller) { $controller = self::conf("defaults.controller"); } @@ -56,21 +58,33 @@ class App } - static function param() - { - $url = App::url(); - return $url[1]; - } - - // Requests should always be in the form: controller/action/parameters.type - // Strip type info since it's not needed at this point + /** + * Requests should always be in the form: controller/action/parameters.type + * Strip type info since it's not needed at this point + */ static function url($index = null) { - $req = ltrim($_SERVER['REQUEST_URI'], "/"); + $url = parse_url($_SERVER['REQUEST_URI']); + if ($index == "query") { + return $url['query']; + } + $req = ltrim($url['path'], "/"); $array = explode("/", preg_replace('/\.\w+$/', '', $req)); return (is_numeric($index)) ? $array[$index] : $array; } + /** + * + */ + static function param() + { + $url = App::url(); + return $url[1]; + } + + /** + * Return configuration data from ini file + */ static function conf($key = null) { $cpath = realpath("../system/config.ini"); @@ -89,7 +103,7 @@ class App $type = ltrim(strrchr($last, "."), "."); $contentTypes = array("html", "rss", "xml", "tpl", "pdf", "jpg"); - + if (in_array($type, $contentTypes)) { return $type; } @@ -110,14 +124,14 @@ class App // Send error to client self::json($msg); // And log it locally - self::log($msg); + self::log($msg); } static function json($msg) { $json = json_encode($msg); header("Content-Type: application/json"); - header("Content-Length: " . strlen($json)); + header("Content-Length: " . mb_strlen($json)); print $json; } @@ -130,15 +144,9 @@ class App $msg = print_r($msg, true); } - $c = self::conf("app.error_log"); - - if (!$c) { - return false; - } - - $file = realpath(__FILE__."/../../../../data/$c"); - - if (!$file) { + $file = self::conf("app.error_log"); + + if (!is_file($file)) { return false; } @@ -175,8 +183,14 @@ class App header("Location: $url"); } + /** + * Determine locale from USER_AGENT + */ static function locale() { + if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + return false; + } // Set language to whatever the browser is set to list($loc, $lang) = explode("-", $_SERVER['HTTP_ACCEPT_LANGUAGE']); return sprintf("%s_%s", $loc, strtoupper($lang)); @@ -274,8 +288,7 @@ class App { if (!isset($_SERVER['PHP_AUTH_USER'])) { - $header = sprintf('WWW-Authenticate: Basic realm="%s"', $realm) - header($header); + header(sprintf('WWW-Authenticate: Basic realm="%s"', $realm)); header("HTTP/1.0 401 Unauthorized"); return false; } else { diff --git a/Controller.php b/Controller.php index 43b0efe..5432673 100644 --- a/Controller.php +++ b/Controller.php @@ -14,15 +14,19 @@ class Controller const OrderBy = ""; const HasMany = ""; + const TableName = ""; const ManyToMany = ""; const ForeignKey = ""; - const TableName = ""; const TableSelect = ""; function __construct($id = null) { // Child classes should always have the same name as their tables - $this->table = strtolower(get_class($this)); + $this->class = get_class($this); + $this->table = eval("return {$this->class}::TableName;"); + if (!$this->table) { + $this->table = strtolower($this->class); + } $this->result = null; if ($id) { return $this->get($id); @@ -88,10 +92,10 @@ class Controller // $this->schema = $schema[$this->table]; // Ugly hack until PHP 5.3 - $i_sort = eval("return {$this->table}::OrderBy;"); - $i_fk = eval("return {$this->table}::ForeignKey;"); - $i_mtm = eval("return {$this->table}::ManyToMany;"); - $i_select = eval("return {$this->table}::TableSelect;"); + $i_sort = eval("return {$this->class}::OrderBy;"); + $i_fk = eval("return {$this->class}::ForeignKey;"); + $i_mtm = eval("return {$this->class}::ManyToMany;"); + $i_select = eval("return {$this->class}::TableSelect;"); // $orderBy = ($sort) ? $sort : @@ -122,7 +126,7 @@ class Controller $row = $result[$i]; $this->data[$i] = $row; $this->find_parent($row, $i); - $this->find_children($row, $i); +// $this->find_children($row, $i); } return $this; @@ -136,7 +140,7 @@ class Controller private function find_children($row, $i) { $id = $row['id']; // ID of the parent - $fk = explode(",", eval("return $this->table::HasMany;")); + $fk = explode(",", eval("return $this->class::HasMany;")); if (empty($fk[0])) { return false; @@ -175,7 +179,7 @@ class Controller private function find_parent($row, $i) { $select = "*"; - $fk = explode(",", eval("return {$this->table}::ForeignKey;")); + $fk = explode(",", eval("return {$this->class}::ForeignKey;")); // No parents defined if (empty($fk[0])) { diff --git a/Db.php b/Db.php index f961614..d57af10 100644 --- a/Db.php +++ b/Db.php @@ -84,7 +84,7 @@ class Db } // Select statements need the query results - if (preg_match('/^select/i', $sql)) { + if (preg_match('/^SELECT/i', $sql)) { return $stmt; } @@ -104,6 +104,9 @@ class Db } + /** + * + */ public static function fetch($sql, $data = null) { $stmt = self::query($sql, $data); -- cgit v1.2.3