00001 <?php
00029 error_reporting(E_ALL);
00030
00032 $SITE_NAME = 'My Website';
00034 $CONTENT_DIR = 'content';
00036 $CSS = null;
00038 $HEADER = null;
00040 $FOOTER = null;
00042 $NEWSFEED = null;
00044 $TPL_FILE = null;
00046 $BOXES = array();
00047
00048 @include 'website-cfg.php';
00049
00050 $TEMPLATE = <<<TPL
00051 <!DOCTYPE html>
00052 <html>
00053 <head>
00054 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
00055 <title>%%page-title%% | %%site-name%%</title>
00056 <link href="%%self%%/sitemap" rel="contents" type="text/html" title="Sitemap">
00057 <style type="text/css">
00058 body { Margin: 0; }
00059 .header {
00060 Background-color: #e0e0e0; Color: #000000;
00061 Border-bottom: 1px outset #e0e0e0;
00062 Padding: 0.5em 1em;
00063 }
00064 .header h1 { Margin: 0; }
00065 .header ul { Float: right; Margin: 0; Padding: 0; List-style: none; }
00066 .header li { Float: left; Padding: 0.5em; }
00067 .nav { Width: 20%; Float: right; Clear: right; Color: #000000; }
00068 .nav ul {
00069 List-style: none; Margin: 1em; Padding: 0;
00070 Background-color: #e0e0e0;
00071 Border: 2px groove white;
00072 }
00073 .nav li { Border: 1px outset #e0e0e0; Padding: 0.5em 1em; Text-align: center; }
00074 li.active { Font-weight: bold; }
00075 .header a, .nav a { Display: block; Color: #0000ff; Text-decoration: none; }
00076 .section { Width: 79%; Float: left; }
00077 #content { Padding: 5%; }
00078 #skip-nav { Display: none; }
00079 .footer { Padding: 0.5em 1em; Clear: both; Border-top: 3px outset #e0e0e0; }
00080 img { Border: none; }
00081 </style>
00082 %%meta%%
00083 </head>
00084
00085 <body>
00086 <div class="header">
00087 %%header%%
00088 %%nav%%
00089 </div>
00090
00091 <div class="section" role="main">
00092 <div id="content">%%content%%</div>
00093 </div>
00094
00095 <div class="nav" role="navigation">
00096 %%peers%%
00097 %%subpages%%
00098 <ul>
00099 <li><a href="%%self%%" rel="home">Home</a></li>
00100 <li><a href="%%self%%/sitemap">Sitemap</a></li>
00101 </ul>
00102 </div>
00103
00104 <div class="footer">
00105 %%footer%%
00106 </div>
00107 </body>
00108 </html>
00109 TPL;
00110
00111 if (!empty($TPL_FILE) && is_file($TPL_FILE) && is_readable($TPL_FILE)) {
00112 $tpl_text = file_get_contents($TPL_FILE);
00113 } else {
00114 $tpl_text = $TEMPLATE;
00115 }
00116
00117 $app_dir = dirname($_SERVER['SCRIPT_NAME']);
00118 if ($app_dir == '/') $app_dir = '';
00119 $tpl_vars = array(
00120 'self' => $_SERVER['SCRIPT_NAME'],
00121 'app-dir' => $app_dir,
00122 'site-name' => $SITE_NAME);
00123
00124 $settings = array(
00125 'tpl-vars' => $tpl_vars,
00126 'template' => $tpl_text,
00127 'default_view' => 'page',
00128 'boxes' => array_merge(
00129 array('nav', 'peers', 'header', 'footer', 'meta'),
00130 $BOXES));
00131
00132 if ($CSS === null) {
00133 $CSS = array();
00134 } else if (!is_array($CSS)) {
00135 $CSS = array($CSS);
00136 }
00137 if ($NEWSFEED === null) {
00138 $NEWSFEED = array();
00139 } else if (!is_array($NEWSFEED)) {
00140 $NEWSFEED = array($NEWSFEED);
00141 }
00142
00143 function page_view($path, $tpl_vars) {
00144 global $CSS, $NEWSFEED, $CONTENT_DIR;
00145
00146 $pages = page_list($CONTENT_DIR);
00147
00148 if (count($path) == 0) $path[0] = $pages[0];
00149
00150 $pathname = implode(DIRECTORY_SEPARATOR, $path);
00151 $filename = $CONTENT_DIR . DIRECTORY_SEPARATOR . "$pathname.html";
00152
00153 $tpl_vars['page-title'] = $pathname;
00154 if (is_file($filename) && is_readable($filename)) {
00155 ob_start();
00156 include $filename;
00157 $content = ob_get_clean();
00158 } else {
00159 header("HTTP/1.1 404 Not Found");
00160 $content = "Sorry, can't read " . $pathname;
00161 }
00162 $tpl_vars['content'] = $content;
00163
00164 return $tpl_vars;
00165 }
00166
00167 function sitemap_view($path, $tpl_vars) {
00168 $tpl_vars['content'] = html_sitemap();
00169 $tpl_vars['page-title'] = 'Sitemap';
00170 return $tpl_vars;
00171 }
00172
00176
00178 function nav_box($location, $tpl_vars) {
00179 global $CONTENT_DIR;
00180
00181 $pages = page_list($CONTENT_DIR);
00182
00183 if (count($location['data']) > 0) {
00184 $active = $location['data'][0];
00185 } else if (count($pages) > 0) {
00186 $active = $pages[0];
00187 } else {
00188 $active = '';
00189 }
00190
00191 return page_list2html($pages, $active);
00192 }
00193
00195 function peers_box($location, $tpl_vars) {
00196 global $CONTENT_DIR;
00197
00198 if (count($location['data']) == 0 || count($location['data']) == 1) {
00199
00200 return '';
00201 } else {
00202 $active = array_pop($location['data']);
00203 }
00204
00205 $page_dir = implode(
00206 DIRECTORY_SEPARATOR,
00207 array_merge(array($CONTENT_DIR), $location['data']));
00208
00209 if (is_dir($page_dir) && is_readable($page_dir)) {
00210 return page_list2html(
00211 page_list($page_dir), $active, $location['data']);
00212 }
00213 }
00214
00216 function header_box($location, $tpl_vars) {
00217 global $HEADER;
00218
00219 if (!empty($HEADER) && is_file($HEADER) && is_readable($HEADER)) {
00220 $header_text = file_get_contents($HEADER);
00221 } else {
00222 $header_text = '<h1>%%page-title%% | %%site-name%%</h1>';
00223 }
00224
00225 return render($header_text, $tpl_vars);
00226 }
00227
00229 function footer_box($location, $tpl_vars) {
00230 global $FOOTER;
00231
00232 if (!empty($FOOTER) && is_file($FOOTER) && is_readable($FOOTER)) {
00233 return render(file_get_contents($FOOTER), $tpl_vars);
00234 } else {
00235 return '';
00236 }
00237 }
00238
00240 function meta_box($location, $tpl_vars) {
00241 global $CSS, $NEWSFEED;
00242
00243 $output = '';
00244
00245 $tpl = '<link rel="stylesheet" type="text/css" href="%s">' . "\n";
00246 foreach ($CSS as $style) {
00247 $output .= sprintf($tpl, $style);
00248 }
00249
00250 $tpl = '<link rel="alternate"'
00251 . ' type="application/rss+xml" href="%s">' . "\n";
00252 foreach ($NEWSFEED as $feed) {
00253 $output .= sprintf($tpl, $feed);
00254 }
00255
00256 return $output;
00257 }
00258
00260
00262 function page_list($dir) {
00263 static $pages = array();
00264
00265 if (isset($pages[$dir])) return $pages[$dir];
00266
00267 $cwd = getcwd();
00268 if (!@chdir($dir)) return array();
00269 $files = glob('*.html');
00270 chdir($cwd);
00271
00272 $pages[$dir] = array();
00273 foreach ($files as $filename) {
00274 $pages[$dir][] = substr($filename, 0, -5);
00275 }
00276
00277 return $pages[$dir];
00278 }
00279
00281 function page_list2html($pages, $active, $location = array()) {
00282 static $link_tpl1 = "<li><a href=\"%s/page:%s\">%s</a></li>\n";
00283 static $link_tpl2 =
00284 "<li class=\"active\"><a href=\"%s/page:%s\">%s</a></li>";
00285
00286 $output = "<ul>\n";
00287 foreach ($pages as $page) {
00288 $path = implode(':', array_merge($location, array($page)));
00289 $tpl = ($page == $active) ? $link_tpl2 : $link_tpl1;
00290 $output .=
00291 sprintf($tpl, $_SERVER['SCRIPT_NAME'], $path, $page);
00292 }
00293 $output .= "</ul>\n";
00294
00295 return $output;
00296 }
00297
00299 function html_sitemap($location = array()) {
00300 global $CONTENT_DIR;
00301 static $link_tpl1 = "<a href=\"%s/page:%s\">%s</a>\n";
00302
00303 $page_path = array_merge(array($CONTENT_DIR), $location);
00304 $page_dir = implode(DIRECTORY_SEPARATOR, $page_path);
00305 $pages = page_list($page_dir);
00306
00307 $output = '';
00308 if (count($pages) > 0) {
00309 $output = "<ul>\n";
00310 foreach ($pages as $page) {
00311 $output .= "<li>\n";
00312
00313 $new_location = array_merge($location, array($page));
00314 $path = implode(':', $new_location);
00315 $output .= sprintf($link_tpl1,
00316 $_SERVER['SCRIPT_NAME'], $path, $page);
00317 $output .= html_sitemap($new_location);
00318
00319 $output .= "</li>\n";
00320 }
00321 $output .= "</ul>\n";
00322 }
00323 return $output;
00324 }
00325
00327
00329 function website($settings) {
00330 if (isset($settings['tpl-vars']) && is_array($settings['tpl-vars'])) {
00331 $T = $settings['tpl-vars'];
00332 } else {
00333 $T = array();
00334 }
00335
00336 $location = parse_location($settings['default_view']);
00337
00338 $T = $location['view']($location['data'], $T);
00339
00340 if (isset($settings['boxes']) && is_array($settings['boxes'])) {
00341 foreach ($settings['boxes'] as $box_name) {
00342 $box_func = $box_name . '_box';
00343 if (function_exists($box_func)) {
00344 $T[$box_name] = $box_func($location, $T);
00345 } else {
00346 $T[$box_name] = "(missing content: $box_name)";
00347 }
00348 }
00349 }
00350
00351 print render($settings['template'], $T);
00352 }
00353
00355 function parse_location($default_view) {
00356 $request = parse_url($_SERVER['REQUEST_URI']);
00357 $url_split = explode('/', $request['path']);
00358 $location = end($url_split);
00359 if ($location == basename($_SERVER['SCRIPT_NAME'])) {
00360 $location_data = array($default_view);
00361 } else if (empty($location)) {
00362 $location_data = array($default_view);
00363 } else {
00364 $location_data =
00365 array_map('urldecode', explode(':', $location));
00366 }
00367 $view_function = $location_data[0] . '_view';
00368
00369 if (function_exists($view_function)) {
00370 return array(
00371 'view' => $view_function,
00372 'data' => array_slice($location_data, 1));
00373 } else {
00374 return array('view' => 'error_page', 'data' => array());
00375 }
00376 }
00377
00379 function to_template_var($str) {
00380 return "%%$str%%";
00381 }
00382
00384 function render($template, $tpl_vars) {
00385 $replaced = str_replace(
00386 array_map('to_template_var', array_keys($tpl_vars)),
00387 array_values($tpl_vars),
00388 $template);
00389
00390 return preg_replace('/%%[^%]*%%/', '', $replaced);
00391 }
00392
00394 function error_page($tpl_vars) {
00395 if (isset($tpl_vars['error']))
00396 $content = $tpl_vars['error'];
00397 else
00398 $content = 'Page not found';
00399
00400 $tpl_vars['page_title'] = 'Error';
00401 $tpl_vars['content'] = $content;
00402 return $tpl_vars;
00403 }
00404
00405 website($settings);
00406 ?>