3 * Display Fortune cookies
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
9 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__
).'/../../').'/');
10 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC
.'lib/plugins/');
11 require_once(DOKU_PLUGIN
.'syntax.php');
13 class syntax_plugin_xfortune
extends DokuWiki_Syntax_Plugin
{
19 'author' => 'Andreas Gohr',
20 'email' => 'andi@splitbrain.org',
21 'date' => '2008-01-25',
22 'name' => 'Fortune Plugin',
23 'desc' => 'Displays random fortune cookies using AJAX requests',
24 'url' => 'http://www.dokuwiki.org/plugin:gallery',
29 * What kind of syntax are we?
36 * What about paragraphs?
50 * Connect pattern to lexer
52 function connectTo($mode) {
53 $this->Lexer
->addSpecialPattern('\{\{xfortune>[^}]*\}\}',$mode,'plugin_xfortune');
60 function handle($match, $state, $pos, &$handler){
61 $match = substr($match,11,-2); //strip markup from start and end
66 list($cookie,$params) = explode('?',$match,2);
68 //xfortune cookie file
69 $data['cookie'] = cleanID($cookie);
71 //time interval for changing cookies
72 if(preg_match('/\b(\d+)\b/i',$params,$match)){
73 $data['time'] = $match[1];
77 //no hammering please!
78 if($data['time'] < 5) $data['time'] = 5;
86 function render($mode, &$renderer, $data) {
88 $renderer->doc
.= '<div id="plugin_xfortune">';
89 $renderer->doc
.= $this->_getCookie($data['cookie']);
90 $renderer->doc
.= '</div>';
91 $renderer->doc
.= $this->_script($data['cookie'],$data['time']);
97 function _script($cookie,$time){
98 $str = '<script type="text/javascript" language="javascript">';
99 $str .= 'var plugin_xfortune_time = '.($time*1000).';';
100 $str .= 'var plugin_xfortune_cookie = \''.$cookie."';";
101 $str .= "addEvent(window,'load',plugin_xfortune);";
107 * Returns one random cookie
109 * @author Andreas Gohr <andi@splitbrain.org>
111 function _getCookie($cookie){
112 $file = mediaFN($cookie);
113 if(!@file_exists
($file)) return 'ERROR: cookie file not found';
115 $dim = filesize($file);
116 if($dim < 2) return "ERROR: invalid cookie file $file";
117 mt_srand( (double) microtime() * 1000000);
118 $rnd = mt_rand(0,$dim);
120 $fd = fopen($file, 'r');
121 if (!$fd) return "ERROR: reading cookie file $file failed";
123 // jump to random place in file
132 $line = fgets($fd, 1024);
135 // start of file always starts a cookie
138 // ignore delimiter if exists
141 // part of the cookie
142 $text .= htmlspecialchars($line).'<br />';
149 // we had a cookie already, stop here
152 // no cookie yet, wrap around
160 // we had a cookie already, stop here
162 }elseif($seek == $dim -2){
163 // it's the end of file delimiter, wrap around
167 // start of the cookie
173 // part of the cookie?
175 $text .= htmlspecialchars($line).'<br />';
180 // if it is not valid UTF-8 assume it's latin1
181 if(!utf8_check($text)) return utf8_encode($text);
187 //Setup VIM: ex: et ts=4 enc=utf-8 :