1<?php
2/*
3 * sidebar.php
4 *
5 * taken from:
6 * <http://wiki.jalakai.co.uk/dokuwiki/doku.php/start>
7 */
8
9// recursive function to establish best sidebar file to be used
10function getSidebarFN($ns, $file) {
11
12  // check for wiki page = $ns:$file (or $file where no namespace)
13  $nsFile = ($ns) ? "$ns:$file" : $file;
14  if (file_exists(wikiFN($nsFile))) return wikiFN($nsFile);
15
16  // remove deepest namespace level and call function recursively
17
18  // no namespace left, exit with no file found
19  if (!$ns) return '';
20
21  $i = strrpos($ns, ":");
22  $ns = ($i) ? substr($ns, 0, $i) : false;
23  return getSidebarFN($ns, $file);
24}
25
26/**
27 * Returns xhtml for a virtual local sidebar file
28 * based on instructions from a master file
29 *
30 * Uses and creates a cachefile
31 *
32 * @author Jan Decaluwe <jan@jandecaluwe.com>
33 * by adapting the p_cached_xhtml function in dokuwiki
34 *
35 */
36function p_sidebar_cached_xhtml($localFile, $masterFile){
37  global $conf;
38  $cache  = getCacheName($localFile.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.xhtml');
39  $purge  = $conf['cachedir'].'/purgefile';
40  // check if cache can be used
41  $cachetime = @filemtime($cache); // 0 if not exists
42
43  if( @file_exists($masterFile)                                       // does the source exist
44      && $cachetime > @filemtime($masterFile)                         // cache is fresh
45      && ((time() - $cachetime) < $conf['cachetime'])                 // and is cachefile young enough
46      && !isset($_REQUEST['purge'])                                   // no purge param was set
47      && ($cachetime > @filemtime($purge))                            // and newer than the purgefile
48      && ($cachetime > @filemtime(DOKU_INC.'conf/dokuwiki.php'))      // newer than the config file
49      && ($cachetime > @filemtime(DOKU_INC.'conf/local.php'))         // newer than the local config file
50      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/xhtml.php'))   // newer than the renderer
51      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
52      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
53  {
54    //well then use the cache
55    $parsed = io_readfile($cache);
56    $parsed .= "\n<!-- cachefile $cache used -->\n";
57  }else{
58    $parsed = p_render('xhtml', p_cached_instructions($masterFile),$info); //try to use cached instructions
59
60    if($info['cache']){
61      io_saveFile($cache,$parsed); //save cachefile
62      $parsed .= "\n<!-- no cachefile used, but created -->\n";
63    }else{
64      @unlink($cache); //try to delete cachefile
65      $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
66    }
67  }
68
69  return $parsed;
70}
71
72function html_sidebar($cssid) {
73  global $ID;
74  global $ACT;
75  global $IDX;
76
77  //if ($ACT != 'show') return '';
78
79  // determine master sidebar file
80  $masterFile = getSidebarFN(getNS($ID), 'sidebar');
81
82  // open sidebar <div>
83  if (!isset($cssid)) $cssid = 'sidebar';
84  echo('<div id="' . $cssid . "\">\n");
85
86  // determine what to display
87  if ($masterFile) {
88    // virtual hidden local sidebar filename
89    $fn = wikiFN($ID.'_sidebar');
90    $localFile = dirname($fn).'/_'.basename($fn);
91    print p_sidebar_cached_xhtml($localFile, $masterFile);
92  }
93  else {
94    html_index($IDX);
95  }
96
97  // close sidebar <div>
98  echo("</div>\n");
99}
100
101?>
102