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