<?php

// Site-wide constants

// Separator between general navigation items
$NAV_SEP = " •\n";

// Separator between hierarchical navigation items
$NAV_SEP_HIER = " <strong>»</strong>\n";

// Timezone
date_default_timezone_set('Europe/Zurich');


class Application {

  // general properties
  var $site = "dr Beat(li)";
  var $metaauthor = "Beat &quot;2b&quot; Bolli";
  var $author = "<a href=\"/php/redir.php?d=bb\">Beat &quot;2b&quot; Bolli</a>";
  var $title = '(none)';
  var $language = 'de';

  // style sheet
  var $style = '/bb.css';

  var $doctype = '';
  var $htmltag = '';

  var $head_attr = '';          // can specify additional <head> attributes here
  var $js_includes = Array();   // can define JavaScript include files
  var $head_add = '';		// can specify additional <head> content here
  var $body_attr = '';          // can specify additional <body> attributes here
  var $body_class = '';         // can specify the <div id=body> class here

  var $template_h1 = '<h1>%s</h1>';

  var $menu;			// menu/navigation bar to show
  var $submenu;			// page internal menu/navigation, shown after the title
  var $sidebar = false;		// sidebar true/false

  var $logos;			// Array of logos

  function Application($title, $menu=null, $submenu=null)  {
    $this->doctype = "<!DOCTYPE html>\n\n";
    $this->htmltag = "<html lang=\"{$this->language}\">\n\n";
    $this->title = $title;
    $this->menu = $menu;
    if ($this->menu && $this->title)
      $this->menu->inactivate($this->title);
    $this->submenu = $submenu;
    $this->sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : false;

    $this->logos = Array();
    $this->addLogo('<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/"><img src="/pic/somerights.png" width="88" height="31" alt="Some rights reserved" /></a>');
  }

  function addLogo($logo) {
    $this->logos[] = $logo;
  }

  function delLogo($ident) {
    foreach ($this->logos as $i => $logo)
      if (strpos($logo, $ident) !== false) {
        unset($this->logos[$i]);
	return;
      }
  }

  function html_logos() {
    return implode("\n", $this->logos);
  }

  function html() {
    echo $this->doctype;
    echo $this->htmltag;
  }

  function head() {
    echo "<head{$this->head_attr}>
<meta charset=utf-8>
<meta name=\"author\" content=\"{$this->metaauthor}\" />
<meta name=\"ICBM\" content=\"47.0452, 07.2715\" />
<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,maximum-scale=2\" />
<link rel=\"me\" href=\"https://drbeat.li/\" />
<link rel=\"me\" href=\"https://micro.blog/bbolli\" />
<link rel=\"contact\" href=\"/php/contact.php\" title=\"Contact address\" />
<link rel=\"publickey\" href=\"/pubkey.php\" title=\"GnuPG public key\" />
<link rel=\"author\" href=\"/about.php\" title=\"&Uuml;ber mich...\" />
<link rel=\"shortcut icon\" href=\"/favicon.ico\" />
<link rel=\"apple-touch-icon\" sizes=\"57x57\" href=\"/pic/family/beat-57.jpg\" />
<link rel=\"apple-touch-icon\" sizes=\"72x72\" href=\"/pic/family/beat-72.jpg\" />
<link rel=\"apple-touch-icon\" sizes=\"114x114\" href=\"/pic/family/beat-114.jpg\" />
<link rel=\"apple-touch-icon\" sizes=\"144x144\" href=\"/pic/family/beat-144.jpg\" />
<link rel=\"openid.server\" href=\"https://drbeat.li/id/\" />
<link rel=\"openid.delegate\" href=\"https://drbeat.li/id/index.php\" />\n";
    if ($this->style)
      if (date('m-d') == '04-09')
        echo "<!-- today is CSS naked day! -->\n";
      else
        echo "<link rel=\"stylesheet\" href=\"{$this->style}\" type=\"text/css\" title=\"2b style\" />
<link media=\"handheld, only screen and (max-device-width: 480px)\" href=\"/bbmobile.css\" type=\"text/css\" rel=\"stylesheet\" />\n";
    if ($this->menu)
      echo $this->menu->html_links();
    foreach ($this->js_includes as $js)
      echo "<script type=\"text/javascript\" src=\"$js\"></script>\n";
    if ($this->head_add)
      echo $this->head_add, "\n";
    echo "<title>{$this->site} - {$this->title}</title>
</head>

<body{$this->body_attr}><div id=\"body\" class=\"{$this->body_class}\">
";
  }

  function body($header1='') {

    echo "<div id=\"header\">\n";

    $ra = $_SERVER['REMOTE_ADDR'];
    if (strpos($ra, ':'))
        echo "<p style='float: right; font-size: small;'>connecting with IPv6 from {$ra} </p>\n\n";

    if ($this->menu)
      echo $this->menu->html_nav();

    printf($this->template_h1, $header1 ? $header1 : $this->title);
    echo "\n</div>\n\n";

    if ($this->submenu)
      echo $this->submenu->html_nav();

    echo "<div id=\"main\">\n";

    if ($this->sidebar)
      echo "\n<div id=\"content\">\n";

    include "noie.inc.php";
  }

  function header($header1='') {
    $this->html();
    $this->head();
    $this->body($header1);
  }

  function footer($noref=0) {

    $nav = $this->menu ? $this->menu->html_nav() : '';
    $subnav = $this->submenu ? $this->submenu->html_nav() : '';
    $logos = $this->html_logos();
    $uri = htmlspecialchars($_SERVER['REQUEST_URI']);
    $year = date('Y');

    if ($this->sidebar) {
      echo "</div> <!-- #content -->\n\n<div id=\"sidebar\">\n\n";
      $this->doSidebar();
      echo "</div> <!-- #sidebar -->\n\n";
    }

    echo "</div> <!-- #main -->

<div id=\"footer\">
$subnav$nav
<div id=\"epilogue\">\n";
    if (!$noref)
      echo "<!-- a href=\"/php/access.php?uri={$uri}&amp;dns=1\">accesses</a>
<a href=\"/php/referrer.php?uri={$uri}\">referrers</a -->\n";
    echo "&copy; 2005-{$year} {$this->author}
$logos
</div>\n\n</div>\n\n</div>\n\n</body>\n\n</html>\n";

  }	// footer()

  function plus_1() {
    echo "

<!-- g:plusone size='small' href='{Permalink}'></g:plusone -->
<script type='text/javascript'>
  (function() {
    var po = document.createElement('script');
    po.type = 'text/javascript';
    po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(po, s);
  })();
</script>

";
  }

  function doSidebar() {
    foreach (glob("/home/bb/public_html/sidebar.d/*") as $sideitem) {
      if (substr($sideitem, -1) != '-' && substr($sideitem, -9) != 'index.php') {
        include $sideitem;
        echo "\n";
      }
    }
  }

  function not_found() {
    include '404.php';
    exit;
  }

}	// class Application


include 'album/album.inc.php';


class MenuItem {

  var $rel;
  var $name;
  var $url;

  function MenuItem($rel, $name, $url) {
    $this->rel = $rel;
    $this->name = $name;
    $this->url = $url;
  }

  function href() {
    return $this->name ?
      $this->url ?
        $this->rel ?
	  ($this->rel == 'index') ?
	    "<a rel=\"{$this->rel}\" accesskey=\"0\" href=\"{$this->url}\">{$this->name}</a>" :
	    "<a rel=\"{$this->rel}\" href=\"{$this->url}\">{$this->name}</a>" :
	  "<a href=\"{$this->url}\">{$this->name}</a>" :
	$this->name :
      '';
  }

  function linkrel() {
    return $this->rel ?
      "<link rel=\"{$this->rel}\" href=\"{$this->url}\"" .
      ($this->name ? " title=\"{$this->name}\"" : '') . " />\n" : '';
  }

}	// class MenuItem


class Menu {	// the whole menu

  var $items;
  var $sep;

  function Menu($sep=false) {
    global $NAV_SEP;
    $this->items = array();
    $this->sep = ($sep === false) ? $NAV_SEP : $sep;
  }

  function addItem($item) {
    $this->items[] = $item;
  }

  function html_nav($prefix="") {
    $items = array($prefix);
    foreach ($this->items as $item)
      $items[] = $item->href();
    return "<p class=\"nav\">\n" . implode($this->sep, array_filter($items)) . "\n</p>\n";
  }

  function html_links() {
    $links = '';
    foreach ($this->items as $item)
      $links .= $item->linkrel();
    return $links;
  }

  function inactivate($name) {
    foreach ($this->items as $i => $item)
      if (strpos($name, $item->name) === 0)
	$this->items[$i]->rel = $this->items[$i]->url = '';
  }

}	// class Menu

?>