1<?php
2if (!defined('DOKU_INC')) die();
3
4if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
5if(!defined('HTMLOK_WIKI_PATH'))define ('HTMLOK_WIKI_PATH', DOKU_INC . 'data/pages/');
6require_once(DOKU_PLUGIN . 'action.php');
7if (!defined('HTMLOK_ACCESS_DIR')) define('HTMLOK_ACCESS_DIR', realpath(dirname(__FILE__)) . '/conf/access');
8define ('CONFIG_FILE', DOKU_INC . 'conf/local.php');
9require_once(DOKU_INC . 'inc/cache.php');
10
11class action_plugin_htmlOKay extends DokuWiki_Action_Plugin
12{
13    var $saved_inf;
14    var $files;
15    var $db_msg = "";
16    var $do_dbg = false;
17    var $access_file;
18    var $namespace;
19    var $helper;
20
21    function register(Doku_Event_Handler $controller)
22    {
23        $controller->register_hook('HTMLOK_ACCESS_EVENT', 'BEFORE', $this, 'errors_top');
24        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'bypasss_cache');
25        $controller->register_hook('TEMPLATE_USERTOOLS_DISPLAY', 'BEFORE', $this, 'action_link', array('user'));
26        $controller->register_hook('TEMPLATE_HTMLOKAYTOOLS_DISPLAY', 'BEFORE', $this, 'action_link', array('ok'));
27        $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, 'modify_headers', array());
28        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'dw_started', array());
29        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'errors_top', array());
30    }
31
32     function dw_started(&$event, $param) {
33        $this->_init();
34     }
35
36   function modify_headers(&$event, $param) {
37        global $INFO, $ID;
38
39        $this->get_info();
40        $page = noNS($ID) . '.txt';
41
42        if(!$this->files) {
43            return;
44        }
45        if(!in_array('all', $this->files) && !in_array($page, $this->files)) {
46              return;
47        }
48        $event->data[] = 'Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT';
49        $event->data[] =  'Cache-control: no-cache,must-revalidate,no-store';
50
51    }
52
53    function bypasss_cache(&$event, $param)
54    {
55        global $INFO;
56            $cache =$event->data;
57
58          if (!isset($cache->page)) return;
59            if (isset($cache->mode) &&  $cache->mode == 'metadata') return;
60           $depends = p_get_metadata($cache->page, 'relation htmlokay');
61           $meta_file = metaFN($cache->page,'.meta');
62           if(isset($depends))    {
63               if(!empty($cache->depends['files'])) {
64                $cache->depends['files'][] = $meta_file;
65                //msg($cache->cache);
66               //msg('<pre>c = ' . print_r($event->data, 1) .'</pre>');
67             }
68            else $cache->depends['files'] = array($meta_file);
69        }
70    }
71
72    function get_info()
73    {
74        global $conf;
75        global $INFO;
76
77        $this->helper->get_info();
78
79       $namespace = $this->helper->get_namespace();
80
81        $this->namespace = $namespace;
82        $namespace = str_replace(':', '#', $namespace);
83        $access_file = $this->helper->get_access_file(HTMLOK_ACCESS_DIR, $namespace);
84        $this->set_dbg_msg("access file: $access_file");
85        if (file_exists($access_file))
86        {
87            $INFO['htmlOK_access_scope'] = $this->helper->get_access_scope($access_file);
88            $this->set_dbg_msg("file exists: access file: $access_file");
89            $this->saved_inf = $this->helper->get_saved_inf();
90            if (!$this->saved_inf)
91            {
92                return;
93            }
94
95            $this->files = $this->saved_inf['filespecs'];
96            if(!empty($INFO['filepath']))  {
97               $this->curent_file =  basename($INFO['filepath']);
98            }
99            else {
100               $this->curent_file = noNS($ID) . '.txt';
101            }
102            $this->access_file = $access_file;
103        }
104    }
105
106   function action_link(&$event, $param)
107    {
108          global $INFO;
109
110
111         if(!empty($INFO['userinfo'])) {
112              $name = "HTML Error Window";
113              if($param[0] == 'ok') {
114                  $htm_open = '<span>';
115                  $htm_close = '</span>';
116              }
117              else {
118                  $htm_open = '<li>';
119                  $htm_close = '</li>';
120              }
121              $event->data['items']['htmlokay'] = $htm_open .'<a href="javascript: htmlOK_ERRORS(0);jQuery(\'#htmlOKDBG_ERRORWIN\').toggle();void(0);"  rel="nofollow"   title="' .$name. '">'. $name.'</a>' . $htm_close;
122          }
123
124    }
125
126
127    function set_dbg_msg($msg) {
128       if(!$this->do_dbg) return;
129       $this->db_msg .= $msg . '<br />';
130    }
131
132
133    function _init()
134    {
135        global $INFO, $JSINFO;
136        $this->helper = $this->loadhelper('htmlOKay',1);
137        if(isset($_GET['htmlOKay_dbg'])) {
138           $this->do_dbg = true;
139        }
140        $this->get_info();
141
142
143        $JSINFO['path'] =  HTMLOK_WIKI_PATH;
144
145    }
146
147    function htmlOK_plugin_shutdown()
148    {
149        global $conf;
150        plugin_disable('htmlOK');
151    }
152
153    function setup_debug(&$event, $param) {
154             $this->format_error_window();
155             $this->debug();
156    }
157
158        function get_JSErrString($msg)
159    {
160
161        $msg = trim($msg);
162        static $msgs_inx = -1;
163
164        if (!isset($msg) || empty($msg)) return "";
165        $msgs_inx++;
166        ptln('htmlOK_ERRORS_HEADER[' . $msgs_inx . ']="' . $msg . '";');
167    }
168
169    function  errors_top(&$event,$params) {
170         global $INFO;
171         static $done = false;
172
173         if($done) return;
174         $done = true;
175
176         $this->setup_debug ($event,$params);
177         ptln( '<script language="javascript">' );
178         ptln(' //<![CDATA[');
179         ptln('var htmlOK_ERRORS_HEADER = new Array();');
180
181        $this->JS_ErrString .= $this->get_JSErrString("<b>---End User Info---</b>");
182        $this->JS_ErrString .= $this->get_JSErrString("hmtlOK_access_level: " .  $this->helper->get_access());
183        if ($INFO['htmlOK_client'])
184        {
185            $this->JS_ErrString .= $this->get_JSErrString("client:  " . $INFO['htmlOK_client']);
186        }
187        else
188        {
189            $this->JS_ErrString .= $this->get_JSErrString("client:  " . $INFO['client']);
190        }
191
192        $this->JS_ErrString .= $this->get_JSErrString("Scope: " . $INFO['htmlOK_access_scope']);
193        $this->JS_ErrString .= $this->get_JSErrString("<b>User Info:</b>");
194        if ($INFO['hmtlOK_access_level'] > 0)
195        {
196            $this->access_level = $INFO['hmtlOK_access_level'];
197        }
198        else
199        {
200            $this->access_level = $INFO['htmlOK_displayOnly'];
201        }
202          ptln('//]]>');
203          ptln( '</script>');
204   }
205    function format_error_window()
206    {
207        echo <<<ERRORWINDOW
208      <script language="javascript"><!--//--><![CDATA[//><!--
209       var htmlOK_ERRORS_ARRAY = new Array();
210       var htmlOK_ERRORS_merged = false;
211
212       function htmlOK_ERRORS(viz) {
213             var dom = document.getElementById("htmlOKDBG_ERRORWIN");
214             if(htmlOK_ERRORS_merged) {
215                 dom.innerHTML = htmlOK_ERRORS_merged;
216                 return;
217             }
218              for(i=0; i<htmlOK_ERRORS_HEADER.length;i++) {
219                  htmlOK_ERRORS_ARRAY.splice(0, 0, htmlOK_ERRORS_HEADER[i]);
220              }
221
222
223             var str = "";
224             for(i=0; i<htmlOK_ERRORS_ARRAY.length;i++) {
225                  if(htmlOK_ERRORS_ARRAY[i]) {
226                      str += (htmlOK_ERRORS_ARRAY[i] + "<br />");
227                  }
228             }
229             htmlOK_ERRORS_merged = str;
230             dom.innerHTML = str;
231             if(!viz) return;
232             dom.style.display=viz;
233       }
234       function show_htmlOKay_ERRORSLINK() {
235             var dom = document.getElementById("htmlOKDBG_ERRORWINLINK");
236             dom.style.display="block";
237       }
238       function display_htmlOKay_ERRORS(msg) {
239             var dom = document.getElementById("htmlOKDBG_ERRORWIN");
240             dom.innerHTML = msg;
241       }
242
243      //--><!]]></script>
244
245      <div id="htmlOKDBG_ERRORWINLINK"  style="display:none; padding-top:2em;">
246      <a href="javascript:htmlOK_ERRORS('block');">show errors</a>&nbsp;&nbsp;
247      <a href="javascript:htmlOK_ERRORS('none');">close errors</a>
248      </div>
249
250      <div id="htmlOKDBG_ERRORWIN" style="display:none; padding:1em; background-color:white;"></div>
251
252ERRORWINDOW;
253    }
254
255
256
257    function debug()
258    {
259        global $INFO;
260        global $ID;
261        global $conf;
262        global $NS;
263
264        if(!isset($INFO['userinfo']) ) return ; // && !$INFO['htmlOK_client']) return;
265        if(!$this->do_dbg) return;
266
267        echo <<<DBG_JS
268<script language="javascript"><!--//--><![CDATA[//><!--
269function show_htmlOKayDBG_DIV(mode) {
270   var dom = document.getElementById('htmlOKDBG_DIV');
271   dom.style.display = mode;
272}
273//--><!]]></script>
274<style type="text/css">
275#htmlOKDBG_DIV { position:relative; height: 12em; overflow:scroll; display: none; background-color:white; }
276</style>
277<a href="javascript:show_htmlOKayDBG_DIV('block');">show debug</a>&nbsp;&nbsp;<a href="javascript:show_htmlOKayDBG_DIV('none');">close debug</a>
278DBG_JS;
279
280        echo '<div>&nbsp;</div><div id="htmlOKDBG_DIV">';
281        echo $this->db_msg;
282        echo '<br />';
283
284        echo "Current File: $this->curent_file<br />";
285        echo "Access Directory: " . HTMLOK_ACCESS_DIR . "<br />Access File: $this->access_file <br /> \n";
286        echo "Client: " . $INFO['client'] . '<br />';
287        echo "HTML_OK: " . $conf['htmlok'] . "<br />";
288	    echo "Name Space: " . getNS($ID). " -->  $NS\n";
289
290        echo "\$INFO['htmlOK_client']:  " . $INFO['htmlOK_client'] . "&nbsp;&nbsp;&nbsp;--Writable: " . $INFO['writable'] . " &nbsp;&nbsp;&nbsp;--Editable: " . $INFO['editable'] . '<br />';
291        echo "Access level: {$INFO['hmtlOK_access_level']}<br />";
292
293        echo "<pre>";
294        echo "<br />\$INFO array:<br />";
295        $str = print_r($INFO,true);
296        echo htmlentities($str,ENT_QUOTES);
297
298        echo "<br />\$conf array:<br />";
299        $str = print_r($conf,true);
300        echo htmlentities($str,ENT_QUOTES);
301
302
303        echo "<br />Saved Info:";
304        print_r($this->saved_inf,true);
305
306        echo "</pre>";
307
308        echo "</div>";
309    }
310
311     function write_debug($what) {
312     return;
313     $handle = fopen("htmlok_php.txt", "a");
314     if(is_array($what)) $what = print_r($what,true);
315     fwrite($handle,"$what\n");
316     fclose($handle);
317  }
318}
319?>
320