1<?php
2  /**
3  * Plugin Timer: Displays content at given time. (After next cache update)
4  * Format: <TIMER [P ]starttime=[endtime|-]> Text to show </TIMER>
5  * Time format must be parseable with
6  * {@LINK http://www.php.net/manual/en/function.strtotime.php php strtotime}
7  * Valid timestamp formats are descibed {@LINK http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html here}
8  *
9  * Examples: <TIMER P 07:00:00=10:00:00>Good morning</TIMER> Shows debug information (P option)
10  *           <TIMER 2005-09-24 00:00:00=2005-09-25 23:59:00> Have a nice weekend</TIMER>
11  *
12  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
13  * @author     Otto Vainio <otto@valjakko.net>
14  */
15
16  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
17  if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
18  require_once(DOKU_PLUGIN.'syntax.php');
19
20  /**
21  * All DokuWiki plugins to extend the parser/rendering mechanism
22  * need to inherit from this class
23  */
24  class syntax_plugin_timer extends DokuWiki_Syntax_Plugin {
25
26
27    /**
28    * What kind of syntax are we?
29    */
30    function getType(){
31      return 'substition';
32    }
33
34    function getSort(){
35      return 358;
36    }
37    function connectTo($mode) {
38      $this->Lexer->addEntryPattern('<timer.*?>(?=.*?\x3C/timer\x3E)',$mode,'plugin_timer');
39    }
40    function postConnect() {
41      $this->Lexer->addExitPattern('</timer>','plugin_timer');
42    }
43
44
45    /**
46    * Handle the match
47    */
48    function handle($match, $state, $pos, Doku_Handler $handler){
49      global $conf;
50      switch ($state) {
51        case DOKU_LEXER_ENTER :
52          $str = substr($match, 7, -1);
53          //$conf['dformat']
54          $prt=0;
55          if (substr($str,0,1)=="P") {
56            $prt=1;
57          }
58          $str=substr($str,1);
59          list($starttime, $endtime) = preg_split("/=/u", $str, 2);
60          return array($state, array($prt,$starttime,$endtime));
61        case DOKU_LEXER_UNMATCHED :  return array($state, $match);
62        case DOKU_LEXER_EXIT :       return array($state, '');
63      }
64      return array();
65    }
66
67    /**
68    * Create output
69    */
70    function render($mode,Doku_Renderer $renderer, $data) {
71      global $st;
72      global $et;
73      global $conf;
74      global $prt;
75      if($mode == 'xhtml'){
76        list($state, $match) = $data;
77        switch ($state) {
78        case DOKU_LEXER_ENTER :
79          list($prts,$starttime,$endtime) = $match;
80          $err = "";
81          if (($timestamp = strtotime($starttime)) === -1) {
82            // If time false do not show.
83            $sts = mktime()+10000;
84            $err= "Starttime ($starttime) is invalid";
85          } else {
86            $sts = $timestamp;
87          }
88          if ($endtime=="-") {
89            $ets = strtotime("+1 days");
90          } else {
91            if (($timestamp = strtotime($endtime)) === -1) {
92              // If time false do not show.
93              $ets = mktime()-10000;
94              $err .= " Endtime ($endtime) is invalid";
95            } else {
96              $ets = $timestamp;
97            }
98          }
99          $prt = $prts;
100          $st = $sts;
101          $et = $ets;
102          $renderer->doc .= $err;
103          break;
104        case DOKU_LEXER_UNMATCHED :
105          $now = mktime();
106          if (($st<$now) && ($et>$now)) {
107            if ($prt>0) {
108              $renderer->doc .= " From:" . date($conf['dformat'],$st);
109              $renderer->doc .= " Now:<b>" . date($conf['dformat']) . "</b>";
110              $renderer->doc .= " To:" . date($conf['dformat'],$et);
111            }
112              $renderer->doc .= p_render('xhtml',p_get_instructions($match),$info);
113          } else {
114            if ($prt>0) {
115              if ($now<$st) {
116                $renderer->doc .= " Now:<b>" . date($conf['dformat']) . "</b>";
117                $renderer->doc .= " From:" . date($conf['dformat'],$st);
118                $renderer->doc .= " To:" . date($conf['dformat'],$et);
119              } else {
120                $renderer->doc .= " From:" . date($conf['dformat'],$st);
121                $renderer->doc .= " To:" . date($conf['dformat'],$et);
122                $renderer->doc .= " Now:<b>" . date($conf['dformat']) . "</b>";
123              }
124
125            }
126          }
127          // Disable cache if start or end time within now and next cache refresh time
128          if (($now>$st-$conf['cachetime'] && $now<$st) || ($now>$et-$conf['cachetime'] && $now<$et)) {
129            $renderer->nocache();
130          }
131          $renderer->doc .= $cac;
132          break;
133        case DOKU_LEXER_EXIT :
134          break;
135        }
136        return true;
137      }
138      return false;
139    }
140  }
141?>
142