Mercurial > SimpleWebPresenter
changeset 5:18aafb1a8986
Better handling of errors and globals.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Fri, 20 May 2011 13:25:53 +0200 |
| parents | 74196528fc64 |
| children | 6c0162497d56 |
| files | common-functions.inc filters.inc http-response-status-codes.inc index.php |
| diffstat | 4 files changed, 336 insertions(+), 36 deletions(-) [+] |
line wrap: on
line diff
--- a/common-functions.inc Thu May 19 18:04:33 2011 +0200 +++ b/common-functions.inc Fri May 20 13:25:53 2011 +0200 @@ -1,4 +1,6 @@ <?php +include_once 'http-response-status-codes.inc'; + function loadFile($sFilename, $sCharset = 'UTF-8') { if (floatval(phpversion()) >= 4.3) { @@ -21,4 +23,32 @@ } return $sData; } -?> + +function errorPage($errorText, $errorCode=403) +{ + header(StatusCodes::httpHeaderFor($errorCode)); + print "<div id=\"page\"><h1>${errorText}</h1></div>"; + exit; +} + +function genUrl($keys=array()) { + $out="?"; + $first=1; + $URL_PARAMS=$GLOBALS['URL_PARAMS']; + $new_params=array(); + foreach($URL_PARAMS as $param) { + $new_params[$param]=$GLOBALS[$param]; + } + foreach($keys as $param => $val) { + $new_params[$param]=$val; + } + foreach($new_params as $param => $val) { + if($first) + $first=0; + else + $out.="&"; + $out.=urlencode($param).'='.urlencode($val); + } + return $out; +} +?> \ No newline at end of file
--- a/filters.inc Thu May 19 18:04:33 2011 +0200 +++ b/filters.inc Fri May 20 13:25:53 2011 +0200 @@ -1,26 +1,38 @@ <?php -function activeNav($input, $name, $lang) +function activeNav($input) { + $name=$GLOBALS['name']; + $lang=$GLOBALS['lang']; $pattern = "/<li id=\"${name}\"\s?([^>]*)>/is"; $replacement = "<li id=\"${name}\" class=\"active\" $1>"; $output= preg_replace($pattern, $replacement, $input); $pattern = '/<li id="([^"]+)"\s?([^>]*)>(.*?)<\/li>/is'; - $replacement = "<li id=\"\$1\" \$2><a href=\"?name=$1&lang=${lang}\">\$3</a></li>"; + $replacement = "<li id=\"\$1\" \$2><a href=\"".genUrl(array( "name" => "$1" ))."\">\$3</a></li>"; $output=preg_replace($pattern, $replacement, $output); return $output; } -function addLangBar($input, $lang) { +function addLangBar($input) { + $name=$GLOBALS['name']; + $lang=$GLOBALS['lang']; $languages = array("no","en"); $langbar='<ul id="language-select">'; foreach($languages as $l) { - $active=($l == $lang)?1:0; + $active=($l == $lang)?0:1; $langbar.= " - <li class=\"${l}\"> - <img src=\"http://dev.bfginvest.no/php/flag.php?lang=${l}&active=${active}\" width=\"20\" height=\"16\" alt=\"Norsk versjon - inaktiv\" title=\"Norsk\"/> + <li class=\"${l}\">"; + if ($active) + $langbar.='<a href="'.genUrl(array( lang => $l)).'">'; + + $langbar.= " + <img src=\"http://dev.bfginvest.no/php/flag.php?lang=${l}&active=${active}\" width=\"20\" height=\"16\" alt=\"Norsk versjon - inaktiv\" title=\"Norsk\"/>"; + if ($active) + $langbar.="</a>"; + + $langbar.= " </li> ";
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/http-response-status-codes.inc Fri May 20 13:25:53 2011 +0200 @@ -0,0 +1,256 @@ +<?php + +/** + +* StatusCodes provides named constants for + +* HTTP protocol status codes. Written for the + +* Recess Framework (http://www.recessframework.com/) + +* + +* @author Kris Jordan + +* @license MIT + +* @package recess.http + +*/ + +class StatusCodes { + +// [Informational 1xx] + +const HTTP_CONTINUE = 100; + +const HTTP_SWITCHING_PROTOCOLS = 101; + +// [Successful 2xx] + +const HTTP_OK = 200; + +const HTTP_CREATED = 201; + +const HTTP_ACCEPTED = 202; + +const HTTP_NONAUTHORITATIVE_INFORMATION = 203; + +const HTTP_NO_CONTENT = 204; + +const HTTP_RESET_CONTENT = 205; + +const HTTP_PARTIAL_CONTENT = 206; + +// [Redirection 3xx] + +const HTTP_MULTIPLE_CHOICES = 300; + +const HTTP_MOVED_PERMANENTLY = 301; + +const HTTP_FOUND = 302; + +const HTTP_SEE_OTHER = 303; + +const HTTP_NOT_MODIFIED = 304; + +const HTTP_USE_PROXY = 305; + +const HTTP_UNUSED= 306; + +const HTTP_TEMPORARY_REDIRECT = 307; + +// [Client Error 4xx] + +const errorCodesBeginAt = 400; + +const HTTP_BAD_REQUEST = 400; + +const HTTP_UNAUTHORIZED = 401; + +const HTTP_PAYMENT_REQUIRED = 402; + +const HTTP_FORBIDDEN = 403; + +const HTTP_NOT_FOUND = 404; + +const HTTP_METHOD_NOT_ALLOWED = 405; + +const HTTP_NOT_ACCEPTABLE = 406; + +const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407; + +const HTTP_REQUEST_TIMEOUT = 408; + +const HTTP_CONFLICT = 409; + +const HTTP_GONE = 410; + +const HTTP_LENGTH_REQUIRED = 411; + +const HTTP_PRECONDITION_FAILED = 412; + +const HTTP_REQUEST_ENTITY_TOO_LARGE = 413; + +const HTTP_REQUEST_URI_TOO_LONG = 414; + +const HTTP_UNSUPPORTED_MEDIA_TYPE = 415; + +const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416; + +const HTTP_EXPECTATION_FAILED = 417; + +// [Server Error 5xx] + +const HTTP_INTERNAL_SERVER_ERROR = 500; + +const HTTP_NOT_IMPLEMENTED = 501; + +const HTTP_BAD_GATEWAY = 502; + +const HTTP_SERVICE_UNAVAILABLE = 503; + +const HTTP_GATEWAY_TIMEOUT = 504; + +const HTTP_VERSION_NOT_SUPPORTED = 505; + + + +private static $messages = array( + +// [Informational 1xx] + +100=>'100 Continue', + +101=>'101 Switching Protocols', + +// [Successful 2xx] + +200=>'200 OK', + +201=>'201 Created', + +202=>'202 Accepted', + +203=>'203 Non-Authoritative Information', + +204=>'204 No Content', + +205=>'205 Reset Content', + +206=>'206 Partial Content', + +// [Redirection 3xx] + +300=>'300 Multiple Choices', + +301=>'301 Moved Permanently', + +302=>'302 Found', + +303=>'303 See Other', + +304=>'304 Not Modified', + +305=>'305 Use Proxy', + +306=>'306 (Unused)', + +307=>'307 Temporary Redirect', + +// [Client Error 4xx] + +400=>'400 Bad Request', + +401=>'401 Unauthorized', + +402=>'402 Payment Required', + +403=>'403 Forbidden', + +404=>'404 Not Found', + +405=>'405 Method Not Allowed', + +406=>'406 Not Acceptable', + +407=>'407 Proxy Authentication Required', + +408=>'408 Request Timeout', + +409=>'409 Conflict', + +410=>'410 Gone', + +411=>'411 Length Required', + +412=>'412 Precondition Failed', + +413=>'413 Request Entity Too Large', + +414=>'414 Request-URI Too Long', + +415=>'415 Unsupported Media Type', + +416=>'416 Requested Range Not Satisfiable', + +417=>'417 Expectation Failed', + +// [Server Error 5xx] + +500=>'500 Internal Server Error', + +501=>'501 Not Implemented', + +502=>'502 Bad Gateway', + +503=>'503 Service Unavailable', + +504=>'504 Gateway Timeout', + +505=>'505 HTTP Version Not Supported' + +); + + + +public static function httpHeaderFor($code) { + +return 'HTTP/1.1 ' . self::$messages[$code]; + +} + +public static function getMessageForCode($code) { + +return self::$messages[$code]; + + } + + +public static function isError($code) { + +return is_numeric($code) && $code >= self::HTTP_BAD_REQUEST; + + } + + +public static function canHaveBody($code) { + +return + +// True if not in 100s + +($code < self::HTTP_CONTINUE || $code >= self::HTTP_OK) + +&& // and not 204 NO CONTENT + +$code != self::HTTP_NO_CONTENT + +&& // and not 304 NOT MODIFIED + +$code != self::HTTP_NOT_MODIFIED; + + } +} + +?>
--- a/index.php Thu May 19 18:04:33 2011 +0200 +++ b/index.php Fri May 20 13:25:53 2011 +0200 @@ -4,12 +4,16 @@ include 'php/filters.inc'; include 'php/common-functions.inc'; +$URL_PARAMS=array('name','lang'); + +#Globals $name = $_GET['name']; $lang = $_GET['lang']; if(!$name) { $name="home"; } + if(!$lang) { $lang="no"; $langs=acceptedLanguages(); @@ -34,8 +38,12 @@ return $elem; } +$confFile="${lang}/${name}.xml"; +if (!file_exists($confFile)) { + errorPage("Resource not available"); +} $doc = new DOMDocument(); -$doc->load("${lang}/${name}.xml"); +$doc->load($confFile); $head=getElementByTagName($doc,"head"); $title=$head->getAttribute("title"); @@ -47,29 +55,33 @@ $body=getElementByTagName($doc,"body"); $files=$body->getElementsByTagName("file"); -print '<?xml version="1.0" encoding="UTF-8"?>'; -?> -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" + +$out= '<?xml version="1.0" encoding="UTF-8"?>'; + +$out.= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> - <title> -<?php -print "$title"; -?> + <title>'; + +$out.= "$title"; +$out.= ' </title> -<?php -print "$css"; -?> +'; +$out.= "$css"; +$out.= ' </head> <body> <div id="container"> +'; -<?php foreach ( $files as $file) { $src=$file->getAttribute("src"); $file_content=loadFile("${lang}/${src}"); -// print $file_content; + if(floatval($file_content)<0) { + errorPage("Resource not found '${lang}/${src}'"); + } + $filters=$file->getElementsByTagName("filter"); foreach($filters as $filter) { $func=$filter->getAttribute("function"); @@ -87,24 +99,14 @@ $callString.=");"; eval($callString); } - print $file_content; -} - -/* -$body_content=loadFile("${lang}/${body}"); -if(floatval($body_content)<0) { - header('HTTP/1.0 404 Not Found'); - $body_content='<div id="page"><h1>Resource not found</h1></div>'; + $out.= $file_content; } -if (!file_exists($lang)) { - $lang=no; - header('HTTP/1.0 404 Not Found'); - $body_content='<div id="page"><h1>Language is not available</h1></div>'; -} -*/ -?> - +$out.=' </div> </body> </html> +'; + +print $out; +?>
