diff options
-rw-r--r-- | App.php | 135 | ||||
-rw-r--r-- | Controller.php | 22 |
2 files changed, 75 insertions, 82 deletions
@@ -1,35 +1,33 @@ <?php - - /** - * main/App.php - * @author Filipp Lepalaan <filipp@mekanisti.fi> - * @copyright 2009 Filipp Lepalaan - */ - +//// +// main/App.php +// @author Filipp Lepalaan <filipp@mekanisti.fi> +// @copyright (c) 2009 Filipp Lepalaan class App { - /** - * Fire up the application - */ + //// + // Fire up the application static public function init() { + self::log($_SERVER); // Set custom error handler - set_error_handler("App::error_handler"); + set_error_handler('App::error_handler'); // Set correct timezone - date_default_timezone_set(self::conf("app.timezone")); + date_default_timezone_set(self::conf('app.timezone')); @list($controller, $param, $action) = App::url(); // No action given, read default one if (empty($param)) { - $action = self::conf("defaults.action"); + $action = self::conf('defaults.action'); } // No controller given, read default one if (!$controller) { - $controller = self::conf("defaults.controller"); + $controller = self::conf('defaults.controller'); } + // Fire up the output buffer ob_start(); // Dispatch correct controller @@ -58,18 +56,17 @@ class App } - /** - * 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) { $url = parse_url($_SERVER['REQUEST_URI']); - if ($index == "query") { + if ($index == 'query') { return $url['query']; } - $req = ltrim($url['path'], "/"); - $array = explode("/", preg_replace('/\.\w+$/', '', $req)); + $req = ltrim($url['path'], '/'); + $array = explode('/', preg_replace('/\.\w+$/', '', $req)); return (is_numeric($index)) ? $array[$index] : $array; } @@ -82,33 +79,35 @@ class App return $url[1]; } - /** - * Return configuration data from ini file - */ + //// + // Return configuration data from ini file static function conf($key = null) { - $cpath = realpath("../system/config.ini"); + $cpath = realpath('../system/config.ini'); $config = parse_ini_file($cpath, true); $config = $config['development']; + if ($key && ! $config[$key]) { return self::error("No such config key: $key"); } + return ($key) ? $config[$key] : $config; + } static function type() { - $tokens = explode("/", $_SERVER['REQUEST_URI']); + $tokens = explode('/', $_SERVER['REQUEST_URI']); $last = array_pop($tokens); - $type = ltrim(strrchr($last, "."), "."); + $type = ltrim(strrchr($last, '.'), '.'); - $contentTypes = array("html", "rss", "xml", "tpl", "pdf", "jpg"); + $contentTypes = array('html', 'rss', 'xml', 'tpl', 'pdf', 'jpg'); if (in_array($type, $contentTypes)) { return $type; } - return "html"; + return 'html'; } @@ -128,41 +127,37 @@ class App static function json($msg) { $json = json_encode($msg); - header("Content-Type: application/json"); - header("Content-Length: " . mb_strlen($json)); + header('Content-Type: application/json'); + header('Content-Length: ' . mb_strlen($json)); print $json; } - /** - * Log an error to our own logging system - */ + //// + // Log an error to our own logging system static function log($msg) { if (is_array($msg)) { $msg = print_r($msg, true); } - $file = self::conf("app.error_log"); + $file = self::conf('app.error_log'); if (!is_file($file)) { return false; } $msg = trim($msg); - $fh = fopen($file, "a+"); + $fh = fopen($file, 'a+'); fwrite($fh, trim($msg) . "\n"); fclose($fh); } - /** - * Set our own PHP error handler - */ + //// + // Set our own PHP error handler static function error_handler($errno, $errstr, $errfile, $errline) { - $str = sprintf( - "%s\t%s\t%s\t%s\n", - date("d.m H:i:s"), + $str = sprintf("%s\t%s\t%s\t%s\n", date('d.m H:i:s'), basename($errfile), $errline, $errstr ); @@ -170,38 +165,36 @@ class App } - /** - * Do a proper HTTP redirect - * @param string [$where] URL to redirect to - * @return void - */ + //// + // Do a proper HTTP redirect + // @param string [$where] URL to redirect to + // @return void static function redirect($url = null) { if (!$url) { // Is it smart to redirect back to the page which redirected here? $url = $_SERVER['HTTP_REFERER']; } - header("HTTP/1.1 303 See Other"); - header("Location: $url"); + header('HTTP/1.1 303 See Other'); + header('Location: ' . $url); } - /** - * Determine locale from USER_AGENT - */ + //// + // 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)); + list($loc, $lang) = explode('-', $_SERVER['HTTP_ACCEPT_LANGUAGE']); + return sprintf('%s_%s', $loc, strtoupper($lang)); } public function delete($table, $where) { if (empty($where)) { - exit(App::error("Delete without parameters")); + exit(App::error('Delete without parameters')); } list($key, $value) = each($where); @@ -212,13 +205,12 @@ class App } - /** - * Insert something in the database - */ + //// + // Insert something in the database static function insert($table, $data) { if (empty($data)) { - exit(self::error("Empty insert")); + exit(self::error('Empty insert')); } $cols = array(); @@ -229,8 +221,8 @@ class App $vals[] = ":{$k}"; } - $cols = implode(",", $cols); - $vals = implode(",", $vals); + $cols = implode(',', $cols); + $vals = implode(',', $vals); $sql = "INSERT INTO `$table` ($cols) VALUES ($vals)"; self::log($sql); @@ -239,7 +231,7 @@ class App } // Move this back to Controller once PHP 5.3 is out (get_called_class()) - static function select($table, $where = 1, $what = "*", $order_by = "") + static function select($table, $where = 1, $what = '*', $order_by = '') { $out = array(); @@ -280,27 +272,28 @@ class App } - /** - * Prompt for HTTP authentication - * @param string [$callback] Function that makes the actual authentication - * @param string [$realm] Realm name - * @return mixed false if cancelled or output of $function - */ - static function auth($callback, $realm = "Default") + //// + // Prompt for HTTP authentication + // @param string [$callback] Function that makes the actual authentication + // @param string [$realm] Realm name + // @return mixed false if cancelled or output of $function + static function auth($callback, $realm = 'Default') { if (!isset($_SERVER['PHP_AUTH_USER'])) { header(sprintf('WWW-Authenticate: Basic realm="%s"', $realm)); - header("HTTP/1.0 401 Unauthorized"); + header('HTTP/1.0 401 Unauthorized'); return false; } else { return call_user_func($callback, $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); } } + //// + // Output a JavaScript fragment public function js($string) { - header("Content-Type: text/javascript"); + header('Content-Type: text/javascript'); print '<script type="text/javascript" charset="utf-8"> ' . $string . ' </script>'; diff --git a/Controller.php b/Controller.php index dbe5e06..2534397 100644 --- a/Controller.php +++ b/Controller.php @@ -207,7 +207,7 @@ class Controller foreach ($fk as $parent) { - $fkey = "id"; + $fkey = 'id'; $lkey = "{$parent}_id"; /* if ($this->schema['foreignKey'][$parent]) @@ -245,15 +245,15 @@ class Controller /** * Insert this thing in the DB and return inserted - * thing + * Thing */ public function insert($data) { if (empty($data)) { - return App::error("Nothing to insert"); + return App::error('Cannot insert emptiness'); } - $insert = ""; + $insert = ''; $values = array(); foreach($data as $k => $v) @@ -262,8 +262,8 @@ class Controller $values[":{$k}"] = $v; } - $insert = rtrim($insert, ", "); - $val = implode(", ", array_keys($values)); + $insert = rtrim($insert, ', '); + $val = implode(', ', array_keys($values)); $sql = "INSERT INTO `{$this->table}` ({$insert}) VALUES ({$val})"; return DB::query($sql, $values); @@ -271,12 +271,12 @@ class Controller } /** - * Delete this thing + * Delete This Thing */ - protected function delete($where, $limit = "") + protected function delete($where, $limit = '') { if (empty($where)) { - return App::error("Delete without arguments"); + return App::error('Cannot delete without arguments'); } list($key, $value) = each($where); @@ -293,14 +293,14 @@ class Controller } /** - * Update this thing + * Update this Thing * We keep this in the Controller since it might know * more about the topmost class */ protected function update($data, $where = null) { if (!is_array($data)) { - return App::error("Update with empty parameters"); + return App::error('Cannot update without parameters'); } if (empty($where)) { |