maio
[h2N7SspZmY.git] / lib / plugins / xfortune / syntax.php
blob1d19940fedd7925effe53b5311943e5f524f9709
1 <?php
2 /**
3 * Display Fortune cookies
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
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 {
14 /**
15 * return some info
17 function getInfo(){
18 return array(
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',
28 /**
29 * What kind of syntax are we?
31 function getType(){
32 return 'substition';
35 /**
36 * What about paragraphs?
38 function getPType(){
39 return 'block';
42 /**
43 * Where to sort in?
45 function getSort(){
46 return 302;
49 /**
50 * Connect pattern to lexer
52 function connectTo($mode) {
53 $this->Lexer->addSpecialPattern('\{\{xfortune>[^}]*\}\}',$mode,'plugin_xfortune');
57 /**
58 * Handle the match
60 function handle($match, $state, $pos, &$handler){
61 $match = substr($match,11,-2); //strip markup from start and end
63 $data = array();
65 //handle params
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];
74 }else{
75 $data['time'] = 30;
77 //no hammering please!
78 if($data['time'] < 5) $data['time'] = 5;
80 return $data;
83 /**
84 * Create output
86 function render($mode, &$renderer, $data) {
87 if($mode == 'xhtml'){
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']);
92 return true;
94 return false;
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);";
102 $str .= '</script>';
103 return $str;
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
124 fseek($fd, $rnd);
126 $text = '';
127 $line = '';
128 $cookie = false;
129 $test = 0;
130 while(true){
131 $seek = ftell($fd);
132 $line = fgets($fd, 1024);
134 if($seek == 0){
135 // start of file always starts a cookie
136 $cookie = true;
137 if($line == "%\n"){
138 // ignore delimiter if exists
139 continue;
140 }else{
141 // part of the cookie
142 $text .= htmlspecialchars($line).'<br />';
143 continue;
147 if(feof($fd)){
148 if($cookie){
149 // we had a cookie already, stop here
150 break;
151 }else{
152 // no cookie yet, wrap around
153 fseek($fd,0);
154 continue;
158 if($line == "%\n"){
159 if($cookie){
160 // we had a cookie already, stop here
161 break;
162 }elseif($seek == $dim -2){
163 // it's the end of file delimiter, wrap around
164 fseek($fd,0);
165 continue;
166 }else{
167 // start of the cookie
168 $cookie = true;
169 continue;
173 // part of the cookie?
174 if($cookie){
175 $text .= htmlspecialchars($line).'<br />';
178 fclose($fd);
180 // if it is not valid UTF-8 assume it's latin1
181 if(!utf8_check($text)) return utf8_encode($text);
183 return $text;
187 //Setup VIM: ex: et ts=4 enc=utf-8 :