1<?php
2if (!defined('DOKU_INC'))
3{
4    die();
5}
6
7class action_plugin_tplmod extends DokuWiki_Action_Plugin {
8    private $html_bg_color, $act_blocking, $ui_priority_metafn, $xcl_sbar;
9    function register(Doku_Event_Handler $controller) {
10        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'dwstarted');
11        $controller->register_hook('TEMPLATE_SITETOOLS_DISPLAY', 'BEFORE', $this, 'action_link', array('site'));
12        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton', array());
13		$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_act');
14        $controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'BEFORE', $this, 'handle_profile_form');
15        $controller->register_hook('FORM_UPDATEPROFILE_OUTPUT', 'BEFORE', $this, 'handle_profile_form');
16        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this,'_ajax_call');
17
18    }
19    function __construct() {
20	    $this->ui_priority_metafn = metaFN(':tplmod:ui_lang', '.ser');
21        if(!file_exists($this->ui_priority_metafn)) {
22           io_saveFile($this->ui_priority_metafn, serialize(array()));
23        }
24
25         $ini = parse_ini_file( tpl_incdir() . 'style.ini');
26         if(isset($ini['__background_alt__']))
27         {
28         $this->html_bg_color=$ini['__background_alt__'];
29         }
30    }
31
32	function _ajax_call(Doku_Event $event, $param) {
33	    if ($event->data != 'tplmod_ui_lang' ) {
34			return;
35		}
36        global $INPUT;
37        $event->stopPropagation();
38        $event->preventDefault();
39        $ar = unserialize(file_get_contents($this->ui_priority_metafn));
40        $ln = $INPUT->str('tplmod_val');
41        $client = $INPUT->str('tplmod_client');
42        $ar[$client] = $ln;
43         $retv = file_put_contents($this->ui_priority_metafn,serialize($ar));
44         if($retv === false) {
45             echo $this->ui_priority_metafn;
46         }
47         else echo "done";
48         return;
49	}
50
51    function handle_profile_form(Doku_Event $event, $param) {
52		 $language = trim($this->getConf('deflang'));
53		 if(!isset($language)|| empty($language)) {
54			 return;
55		 }
56         $language = explode( ',',$language);
57
58		 $client =   $_SERVER['REMOTE_USER'];
59         $ar = unserialize(file_get_contents($this->ui_priority_metafn));
60         if(isset($ar[$client])) {
61             $lan = $ar[$client];
62         }
63
64         $_form =  "\n" . '</div></form><br /><form name="tplmodform" action="#"><div class="no">';
65         $_form.= '<fieldset ><legend>' . $this->getLang('uprofile_title') .'</legend>';
66
67         $num_langs = 0;
68         foreach($language as $ln) {
69               $checked = "";
70               list($name,$val) = preg_split("/\s+/",$ln);
71               $_form .= '<label><span>' .$name .'</span> ';
72               $val = strtolower(trim($val));
73               if($lan == $val) {
74                   $checked = 'checked';
75               }
76
77               $_form .='<input type = "radio" value = "' . $val . '" name= "tplmod_selector" ' . $checked .'>&nbsp;&nbsp;&nbsp;</label>';
78			    if( $num_langs > 0 &&  $num_langs % 3 == 0) {
79				    $_form .= "<br />\n";
80				 }
81				 if($num_langs == 0) {
82					  $num_langs++;
83				 }
84			    $num_langs++;
85         }
86         $_form.= '<br /><label><span><b>User Name: </b></span> ';
87         $_form.= '<input type="textbox" name="tplmod_client" disabled value="' .  $client .'"/></label>';
88         $_form.= '<br /><br /><input type="button" value="Save" class="button" ' . "onclick='tplmod_setui_lang(this.form.tplmod_selector.value,this.form.tplmod_client.value,this.form.tplmod_selector);' />&nbsp;";
89         $_form.= '<input type="reset" value="Reset" class="button" />';
90         $_form.= '</fieldset>';
91         if(is_a($event->data,\dokuwiki\Form\Form::class)) {
92            $pos = $event->data->findPositionByAttribute('type','reset');
93            $pos+=2;
94            $event->data->addHTML($_form,$pos);
95         }
96         else {
97            $pos = $event->data->findElementByAttribute('type', 'reset');
98         $event->data->insertElement($pos+2, $_form);
99         }
100
101    }
102
103    function dwstarted(DOKU_EVENT $event, $param) {
104            global $INPUT, $JSINFO, $conf,$ID,$USERINFO;
105
106            if(file_exists($this->ui_priority_metafn)) {
107               $client = $_SERVER['REMOTE_USER'];
108               $ar = unserialize(file_get_contents($this->ui_priority_metafn));
109               if(isset($ar[$client])) {
110                  $ln = $ar[$client];
111                  init_lang($ln);
112	        	  $conf['lang']= $ln;
113               }
114            }
115
116            $JSINFO['tmplft_template'] = $conf['template'];
117            $JSINFO['tmplftacl'] = auth_quickaclcheck( $ID);
118            $acl_levels = array('NONE'=>0,'READ'=>1,'EDIT'=>2,'CREATE'=>4,'UPLOAD'=>8,'DELETE'=>16);
119            $JSINFO['tmplft_aclgen'] = $acl_levels[$this->getConf('acl_all')];
120            $background_color = $this->getConf('background_color');
121            $background_color = trim($background_color);
122            if(!empty($background_color)) {
123                if($background_color == 'default') $background_color = $this->html_bg_color;
124                 $JSINFO['tmplft_bgcolor'] = $background_color;
125            }
126
127            /* Suppress sidebar */
128            $xcludes = $this->getConf('xcl_sidebar');
129            if($xcludes) {
130               // msg($xcludes);
131            $xcludes = preg_replace("/\s+/","",$xcludes);
132            $xcludes = trim($xcludes,',');
133            $xcludes = str_replace(',','|',$xcludes);
134                if(preg_match('/('.$xcludes.')/',$ID,$matches)) {
135                $conf['sidebar'] = 0;
136                    //msg(print_r($matches,1));
137                }
138            }
139
140           $this->tools();
141          $ips = $this->getConf('ips');
142          $ips = trim($ips);
143          if(!empty($ips)) {
144             $remote_addr = $INPUT->server->str('REMOTE_ADDR');
145             $ips = explode(',',$ips);
146           }
147           else {
148               $ips = array();
149               $remote_addr = "";
150           }
151           $which = $this->getConf('rotatewhich');
152           $dateorip = $this->getConf('dateorip');
153
154           if($which == 'BOTH') {
155               $this->logos($ips,$remote_addr,$dateorip);
156               $this->tags($ips,$remote_addr,$dateorip);
157           }
158           else if($which == 'LOGO') {
159               $this->logos($ips,$remote_addr,$dateorip);
160           }
161           else if($which == 'TAG') {
162               $this->tags($ips,$remote_addr,$dateorip);
163           }
164           if($this->getConf('rotate_title')) {
165              $this->wiki_names($ips,$remote_addr,$dateorip);
166           }
167
168           $profile = $this->getConf('profile');
169           $restricted_group = $this->getConf('restricted_group');
170           $restricted = false;
171           if(isset($USERINFO) && isset($restricted_group)) {
172               $groups = $USERINFO['grps'];
173                if(in_array($restricted_group,$groups)) {
174                    $restricted = true;
175                }
176           }
177
178           if( $restricted && !empty($profile) || !$restricted_group && !empty($profile)) {
179               $JSINFO['tmplft_profile'] = '1';
180           }
181           else {
182               $JSINFO['tmplft_profile'] = "";
183           }
184          $search = $this->getConf('search');
185           if(!empty($search)) {
186               $JSINFO['tmplft_search'] = '1';
187           }
188          else $JSINFO['tmplft_search'] = "";
189		  $this->act_blocking = $this->getConf('blocking');
190
191           /*   debuging
192                if($JSINFO['tmplftacl'] == '255')  { msg('<pre>' . print_r($JSINFO,1) .'</pre>');}
193           */
194
195  }
196
197   function logos($ips,$remote_addr, $dateorip) {
198         global $JSINFO;
199
200         if($dateorip == 'NEITHER') return;
201         $logos = $this->getConf('logos');
202         if(empty($logos)) return;
203         $logos = explode(',',$logos);
204
205         if($dateorip == 'DAY') {
206             $nday = date('w');
207
208             if(isset($logos[$nday])) {
209                 $slot = $nday;
210             }
211             else $slot = 0;
212             list($logo,$width) = preg_split("/\s+/", trim($logos[$slot]));
213             $JSINFO['tmplft_logo'] = trim($logo);
214             $JSINFO['tmplft_logo_width'] = trim($width);
215              return;
216             }
217
218             for($i=0; $i<count($ips); $i++) {
219                 $addr = trim($ips[$i]);
220                if($remote_addr == $addr) {
221                    if(!empty($logos[$i])) {
222                        list($logo,$width) = preg_split("/\s+/", trim($logos[$i]));
223                        $JSINFO['tmplft_logo'] = trim($logo);
224                       $JSINFO['tmplft_logo_width'] = trim($width);
225                    }
226                else {
227                    list($logo,$width) = preg_split("/\s+/", trim($logos[0]));
228                    $JSINFO['tmplft_logo'] = trim($logo);
229                    $JSINFO['tmplft_logo_width'] = trim($width);
230                }
231               }
232           }
233       }
234
235    function wiki_names($ips,$remote_addr, $dateorip) {
236         global $JSINFO;
237          if($dateorip == 'NEITHER') return;
238
239         $names = $this->getConf('wiki_names');
240         $names = explode(',',$names);
241
242         if($dateorip == 'DAY') {
243             $nday = date('w');
244             if(isset($names[$nday])) {
245                 $slot = $nday;
246             }
247             else $slot = 0;
248             $JSINFO['tmplft_title'] = trim($names[$slot]);
249              return;
250             }
251
252         for($i=0; $i<count($ips); $i++) {
253             $addr = trim($ips[$i]);
254             if($remote_addr == $addr) {
255                 if(!empty($names[$i])) {
256                     $JSINFO['tmplft_title'] = trim($names[$i]);
257                }
258                else $JSINFO['tmplft_title'] = trim($names[0]);
259           }
260       }
261   }
262
263    function tags($ips,$remote_addr, $dateorip) {
264         global $JSINFO;
265
266          $opt = $this->getConf('tag_date_format');
267          if($opt) {
268          $JSINFO['tmplft_tag'] = date($opt);
269          return;
270          }
271
272         if($dateorip == 'NEITHER') return;
273         $tags = $this->getConf('taglines');
274         if(empty($tags)) return;
275         $tags = explode(',',$tags);
276
277         if($dateorip == 'DAY') {
278             $nday = date('w');
279             if(isset($tags[$nday])) {
280                 $slot = $nday;
281             }
282             else $slot = 0;
283             $JSINFO['tmplft_tag'] = trim($tags[$slot]);
284              return;
285             }
286
287         for($i=0; $i<count($ips); $i++) {
288             $addr = trim($ips[$i]);
289             if($remote_addr == $addr) {
290                 if(!empty($tags[$i])) {
291                     $JSINFO['tmplft_tag'] = trim($tags[$i]);
292                }
293                else $JSINFO['tmplft_tag'] = trim($tags[0]);
294           }
295       }
296   }
297
298    function tools() {
299             global $JSINFO, $INPUT;
300
301            $sitetools = $this->getConf('sitetools') ;
302            if(!empty($sitetools)) {
303                $pat = array('/Changes/', '/Manager/','/Sitemap/', '/\s/' );
304                $repl = array("","","index","");
305                $sitetools=strtolower(preg_replace($pat, $repl,$sitetools));
306                 $JSINFO['tmplft_sitetools']  = "$sitetools";
307            }
308            else $JSINFO['tmplft_sitetools'] = "";
309
310            $pagetools = $this->getConf('pagetools') ;
311            if(!empty($pagetools)) {
312                $pat = array('/Old/','/\s/ ', '/Backlinks/');
313                $repl = array("","","backlink");
314                $pagetools=strtolower(preg_replace($pat, $repl,$pagetools));
315                if(strpos($pagetools,'all') !== false) {
316                   $pagetools_conf = 'edit,revisions,backlink,subscribe,revert';
317                   $pagetools  = '\w+';
318                }
319                $JSINFO['tmplft_pagetools'] = $pagetools;
320            }
321            else $JSINFO['tmplft_pagetools'] = "";
322
323             $JSINFO['tmplft_ptools_xcl'] =  "";
324			 if(strpos($pagetools,'\w+') !== false) {
325                 $xcl = $this->getConf('ptools_xcl');
326                 $xcl = preg_replace("/\s/","",$xcl);
327			 }
328			 else $xcl = 'NONE';
329             if(!empty($xcl)) $JSINFO['tmplft_ptools_xcl'] = $xcl;
330
331            $mobile_pt = explode(',',$JSINFO['tmplft_pagetools']);
332            $mobile_st = explode(',',$JSINFO['tmplft_sitetools']);
333            $mobile_ar = array_merge($mobile_pt,$mobile_st);
334            $mobile_ar = array_unique($mobile_ar);
335            $JSINFO['tmplft_mobile'] = implode('|',$mobile_ar);
336            if(isset($pagetools_conf)) {  // $pagetools is set to \w+, i.e. all,
337                $pt_conf = explode(',',$pagetools_conf);
338                if(isset($JSINFO['tmplft_ptools_xcl'] )) {
339                    $pt_conf = array_diff($pt_conf, explode(',',$JSINFO['tmplft_ptools_xcl'] ));
340                 }
341                $actions_ar = array_merge($pt_conf,$mobile_st);
342                $actions_ar = array_unique($actions_ar);
343                $JSINFO['tmplft_actions'] = implode(',',$actions_ar);
344                $JSINFO['tmplft_mobile'] = implode('|',$actions_ar);
345            }
346            else $JSINFO['tmplft_actions'] = implode(',',$mobile_ar);
347
348             if($this->getConf('search')) {
349                    $JSINFO['tmplft_actions'] .= ',search';
350                }
351              if($this->getConf('profile')) {
352                    $JSINFO['tmplft_actions'] .= ',profile';
353                }
354               if(preg_match('/revisions|recent/',$JSINFO['tmplft_actions'])) {
355                   $JSINFO['tmplft_actions'] .= ',diff';
356               }
357            }
358
359    function action_link(&$event, $param)  {
360         global  $ACT,$conf;
361         if(!$conf['sidebar']) return;
362         $sbar = $this->getConf('toggle_sidebar');
363         if($ACT != 'show' || !$sbar) return;
364         $name = $this->getLang('toggle_name');
365         $event->data['items']['tplmod'] = '<li><a href="javascript:tplmod_toggle_aside();void(0);"  class="tplmodtoggle" rel="nofollow"   title="' .$name. '">'. $name.'</a></li>';
366    }
367
368    public function addsvgbutton(Doku_Event $event) {
369        global  $ACT,$conf;
370        if(!$conf['sidebar']) return;
371        if($event->data['view'] != 'site') return;
372        $sbar = $this->getConf('toggle_sidebar');
373        if($ACT != 'show' || !$sbar) return;
374        $btn = $this->getLang('toggle_name');
375        if(!$btn) $btn = 'Sidebar toggle';
376        array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\tplmod\MenuItem($btn)]);
377   }
378    public function handle_act(Doku_Event $event) {
379	   global $JSINFO;
380
381	   if(!$this->act_blocking){
382		  return;
383	   }
384
385	   if(empty($JSINFO['tmplftacl'])) {
386		   $JSINFO['tmplftacl']=0;
387	   }
388	   $acl = (($JSINFO['tmplftacl'] >= 0)  && $JSINFO['tmplftacl'] <= $JSINFO['tmplft_aclgen']) ? true: false;
389       if(!$acl) return;
390       $act = act_clean($event->data);
391       if($act == 'logout' || $act == 'login') return;
392	   if(isset($JSINFO['tmplft_ptools_xcl']) && !empty($JSINFO['tmplft_ptools_xcl'])) {
393		   if(strpos($JSINFO['tmplft_ptools_xcl'],$act) !== false) {	 // if excluded allow
394			   return 1;
395		   }
396	   }
397
398	   if(strpos($JSINFO['tmplft_actions'],$act) === false) { // if allowed action, allow
399		   return 1;
400	   }
401
402         if($act == 'revert') {
403               $event->data = 'login';
404              return 1;
405         }
406	    $event->data = 'show';
407        return 1;
408   }
409
410}
411