Mercurial > SimpleWebPresenter
changeset 0:d2da64705bce
Refactored everything in the CMS into this repos.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Tue, 10 May 2011 14:01:34 +0200 |
| parents | |
| children | d91abe5f6214 |
| files | accept-language.inc cache_check.inc index.php |
| diffstat | 3 files changed, 127 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/accept-language.inc Tue May 10 14:01:34 2011 +0200 @@ -0,0 +1,24 @@ +<?php +function acceptedLanguages() { + $langs = array(); + + if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + // break up string into pieces (languages and q factors) + preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); + + if (count($lang_parse[1])) { + // create a list like "en" => 0.8 + $langs = array_combine($lang_parse[1], $lang_parse[4]); + + // set default to 1 for any without q factor + foreach ($langs as $lang => $val) { + if ($val === '') $langs[$lang] = 1; + } + + // sort list based on value + arsort($langs, SORT_NUMERIC); + } + return $langs; + } +} +?> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cache_check.inc Tue May 10 14:01:34 2011 +0200 @@ -0,0 +1,12 @@ +<?php +$if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); + +$mtime = filemtime($SCRIPT_FILENAME); +$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; + +if ($if_modified_since == $gmdate_mod) { + header("HTTP/1.0 304 Not Modified"); + exit; +} +header("Last-Modified: $gmdate_mod"); +?> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/index.php Tue May 10 14:01:34 2011 +0200 @@ -0,0 +1,91 @@ +<?php +include 'php/cache_check.inc'; +include 'php/accept-language.inc'; + +function loadFile($sFilename, $sCharset = 'UTF-8') +{ + if (floatval(phpversion()) >= 4.3) { + if (!file_exists($sFilename)) return -3; + $sData = file_get_contents($sFilename); + } else { + if (!file_exists($sFilename)) return -3; + $rHandle = fopen($sFilename, 'r'); + if (!$rHandle) return -2; + + $sData = ''; + while(!feof($rHandle)) + $sData .= fread($rHandle, filesize($sFilename)); + fclose($rHandle); + } + if ($sEncoding = mb_detect_encoding($sData, 'auto', true) != $sCharset) { + if ($sEncoding != 1) { + $sData = mb_convert_encoding($sData, $sCharset, $sEncoding); + } + } + return $sData; +} + + +$name = $_GET['name']; +$lang = $_GET['lang']; + +if(!$name) { + $name="home"; +} +if(!$lang) { + $lang="no"; + $langs=acceptedLanguages(); + foreach ($langs as $l => $val) { + if (file_exists($l)) { + $lang=$l; + break; + } + } +} + +$body=loadFile("${lang}/${name}.html"); +if(floatval($body)<0) { + header('HTTP/1.0 404 Not Found'); + $body='<div id="page"><h1>Resource not found</h1></div>'; +} +if (!file_exists($lang)) { + $lang=no; + header('HTTP/1.0 404 Not Found'); + $body='<div id="page"><h1>Language is not available</h1></div>'; +} + +print '<?xml version="1.0" encoding="UTF-8"?>'; +?> +<!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>BFG Consult</title> + <link type="text/css" href="/css/reset.css" rel="stylesheet" media="screen" /> + <link type="text/css" href="/css/bfggroup.css" rel="stylesheet" media="screen" /> + </head> + <body> + <div id="container"> + +<?php +$header=loadFile("${lang}/header.html"); +$footer=loadFile("${lang}/footer.html"); + +$pattern = "/<li id=\"${name}\"\s?([^>]*)>/is"; +$replacement = "<li id=\"${name}\" class=\"active\" $1>"; +$header= preg_replace($pattern, $replacement, $header); + +$pattern = '/<li id="([^"]+)"\s?([^>]*)>(.*?)<\/li>/is'; +$replacement = "<li id=\"\$1\" \$2><a href=\"?name=$1&lang=${lang}\">\$3</a></li>"; +$header=preg_replace($pattern, $replacement, $header); + +print $header; +print $body; +print $footer; + + +?> + + </div> + </body> +</html>
