1<?php
2
3/**
4
5 * DokuWiki Plugin footer (Action Component)
6
7 *
8
9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10
11 *
12
13 * Original: from Plugin headerfooter, author Li Zheng <lzpublic@qq.com>
14
15 * Modified by Juergen H-J-Schuemmer@Web.de
16
17 * Only the footer component is supported in this plugin because the header functionality breaks the section edit mode
18
19 */
20
21
22// must be run within Dokuwiki
23
24if(!defined('DOKU_INC')) die();
25
26
27class action_plugin_footer extends DokuWiki_Action_Plugin {
28
29   public function register(Doku_Event_Handler $controller) {
30
31
32      $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'AFTER', $this, 'handle_parser_wikitext_preprocess');
33
34     // aus Seite "https://github.com/MrBertie/pagequery/commit/6cae014dc7cc779c0be8d0a660af42407b414806":
35
36     $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, '_purgecache');
37
38   }
39
40   public function handle_parser_wikitext_preprocess(Doku_Event &$event, $param) {
41
42       global $INFO;
43
44       global $ID;
45
46       global $conf;
47
48
49       //what does this mean???
50
51       if ($INFO['id'] != '') return; // Jede Seite wird zweimal ausgeführt. Wenn die ID leer ist, ist es der echte Text, andernfalls ist es das Menü.
52
53
54       //helper array needed for parsePageTemplate
55
56       //so that replacement like shown here is possible: https://www.dokuwiki.org/namespace_templates#replacement_patterns
57
58       $data = array(
59
60           'id'       => $ID, // the id of the page to be created
61
62           'tpl'      => '', // the text used as template
63
64       );
65
66
67      // Auslesen der Konfiguration für das Präfix der Vorlage-Dateien:
68
69      $pre_nsp = $this->getConf('prefix_namespace');
70
71      if ($pre_nsp != '') {
72
73         $pre_nsp = '/'.$pre_nsp.'_';
74
75      } else {
76
77         $pre_nsp = '/_';   // Defaultwert 1 Unterstrich für Namespace
78
79      };
80
81      $pre_sub = $this->getConf('prefix_subnamespace');
82
83      if ($pre_sub != '') {
84
85         $pre_sub = '/'.$pre_sub.'_';
86
87      } else {
88
89         $pre_sub = '/__';  // Defaultwert 2 Unterstriche für Sub-Namespace
90
91      };
92
93
94       $footerpath = '';
95
96      $templatename = 'footer.txt';   // Name der Vorlage
97
98       $path = dirname(wikiFN($ID));
99
100       if (@file_exists($path.$pre_nsp.$templatename)) {
101
102           $footerpath = $path.$pre_nsp.$templatename;
103
104       } else {
105
106           // search upper namespaces for templates
107
108           $len = strlen(rtrim($conf['datadir'], '/'));
109
110           while (strlen($path) >= $len) {
111
112               if (@file_exists($path.$pre_sub.$templatename)) {
113
114                   $footerpath = $path.$pre_sub.$templatename;
115
116                   break;
117
118               }
119
120               $path = substr($path, 0, strrpos($path, '/'));
121
122           }
123
124       }
125
126
127       if (!empty($footerpath)) {
128
129         $content = $event->data;
130
131         if(strpos($content,"~~NOFOOTER~~") == false) {
132
133            // Prüfung. ob der Befehl "~~NOFOOTER~~" im Quelltext enthalten ist
134
135            $footer = file_get_contents($footerpath);
136
137            if ($footer !== false) {
138
139               $data['tpl'] = cleanText($footer);
140
141               $footer = parsePageTemplate($data);
142
143               if ($this->getConf('separation') == 'paragraph') {
144
145                  // Wenn Absätze zum Teilen verwendet werden
146
147                  $footer = rtrim($footer, " \r\n\\") . "\n\n";
148
149               }
150
151               $event->data .= $footer;
152
153            }
154
155            /*
156
157            // Code übernommen von Seite "https://www.dokuwiki.org/devel:event_handlers_code#caching":
158
159            $event->preventDefault();  // stop dokuwiki carrying out its own checks
160
161            $event->stopPropagation(); // avoid other handlers of this event, changing our decision here
162
163            $event->result = false;    // don't use the cached version
164
165            */
166
167         } else {
168
169            $event->data = str_replace('~~NOFOOTER~~','',$content);
170
171            // Befehl "~~NOFOOTER~~" soll nicht angezeigt werden
172
173         }
174
175      }
176
177   }
178
179
180   // Codeschnipsel aus Seite "https://github.com/MrBertie/pagequery/commit/6cae014dc7cc779c0be8d0a660af42407b414806":
181
182   /**
183
184    * Check for pages changes and eventually purge cache.
185
186    *
187
188    * @author Samuele Tognini <samuele@samuele.netsons.org>
189
190    *
191
192    * @param Doku_Event $event
193
194    * @param mixed     $param not defined
195
196    */
197
198   function _purgecache(&$event, $param) {
199
200       global $ID;
201
202       global $conf;
203
204       /** @var cache_parser $cache */
205
206       $cache = &$event->data;
207
208
209       if(!isset($cache->page)) return;
210
211       //purge only xhtml cache
212
213       if($cache->mode != "xhtml") return;
214
215       //Check if it is an pagequery page
216
217       if(!p_get_metadata($ID, 'pagequery')) return;
218
219       $aclcache = $this->getConf('aclcache');
220
221       if($conf['useacl']) {
222
223           $newkey = false;
224
225           if($aclcache == 'user') {
226
227               //Cache per user
228
229               if($_SERVER['REMOTE_USER']) $newkey = $_SERVER['REMOTE_USER'];
230
231           } else if($aclcache == 'groups') {
232
233               //Cache per groups
234
235               global $INFO;
236
237               if($INFO['userinfo']['grps']) $newkey = implode('#', $INFO['userinfo']['grps']);
238
239           }
240
241           if($newkey) {
242
243               $cache->key .= "#".$newkey;
244
245               $cache->cache = getCacheName($cache->key, $cache->ext);
246
247           }
248
249       }
250
251       //Check if a page is more recent than purgefile.
252
253       if(@filemtime($cache->cache) < @filemtime($conf['cachedir'].'/purgefile')) {
254
255           $event->preventDefault();
256
257           $event->stopPropagation();
258
259           $event->result = false;
260
261       }
262   }
263}