1<?php
2/**
3
4 */
5
6if(!defined('DOKU_INC')) die();
7if(!defined('DOKU_PLUGIN')) define( 'DOKU_PLUGIN','DOKU_INC' . '/lib/plugins/');
8
9class action_plugin_sectiontoggle extends DokuWiki_Action_Plugin {
10
11    /**
12     * Register its handlers with the DokuWiki's event controller
13     */
14    function register(Doku_Event_Handler $controller) {
15        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, '_jsinfo');
16        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'add_button_toggle', array());
17    }
18
19    /**
20     * @author Myron Turner <turnermm02@shaw.ca>
21     */
22    function _jsinfo(&$event, $param) {
23        global $JSINFO;
24        global $ACT;
25        global $conf, $ID;
26
27
28        $JSINFO['se_suspend']=0;
29        $NS_inc = implode("|",$this->normalize_names($this->getConf('incl_ns'),true));
30        if($NS_inc && !preg_match("/($NS_inc)[^:]/",$ID)) {
31         $JSINFO['se_suspend']=1;
32		  return;
33        }
34
35
36      $id_inc = implode("|",$this->normalize_names($this->getConf('incl_pg')));
37          $JSINFO['toc_xcl'] = $this->getConf('toc_toggle');    /* Determines whether or not to apply jQuery toc code to current page*/
38          if($id_inc && !preg_match('/^(' .$id_inc. ')$/',$ID)) {
39             $JSINFO['se_suspend']=1;
40                         $JSINFO['toc_xcl'] = 1;
41		     return;
42        }
43
44        $NS = implode("|",$this->normalize_names($this->getConf('xcl_ns'),true));
45        $JSINFO['se_suspend']=0;
46        if($NS && preg_match("/($NS)[^:]/",$ID)) {
47      	    $JSINFO['se_suspend']=1;
48		    return;
49        }
50
51        $id = implode("|",$this->normalize_names($this->getConf('xcl_pg')));
52	    if($id && preg_match('/^' . $id. '$/',$ID)) {
53      	    $JSINFO['se_suspend']=1;
54		   return;
55        }
56
57       $JSINFO['se_act'] = $ACT;
58       $JSINFO['se_template'] =  $conf['template'];
59       $JSINFO['se_actual_tpl'] =  $conf['template'];
60       if($JSINFO['se_template'] == 'bootstrap3' && !$ACT) {
61           $JSINFO['se_act'] = 'show';
62       }
63       $p = $this->getConf('platform');
64       $JSINFO['se_platform'] = $p[0];
65     //  msg( $JSINFO['se_platform']);
66       $headers = $this->getConf('headers');
67       $JSINFO['se_headers'] = $headers[1];
68       $xcl_headers = $this->getConf('xcl_headers');
69       $xcl_headers = str_replace('h',"",$xcl_headers);
70       $JSINFO['se_xcl_headers'] = $xcl_headers;
71       $JSINFO['se_type'] = $this->getConf('type');
72	   $JSINFO['h_ini_open'] = trim(strtolower($this->getConf('h_ini_open')));
73	   $JSINFO['h_ini_open'] = preg_replace('/\s+/','_',$JSINFO['h_ini_open']);
74       $alt_mobile = $this->getConf('mobile_alt');
75	   $JSINFO['no_ini'] = 0;
76       $JSINFO['start_open'] = $this->getConf('start_open');
77
78           $JSINFO['se_device'] = trim($this->device_type()) ;
79           $acl = auth_quickaclcheck($ID);
80           if($JSINFO['se_device'] == 'phone' || $acl < AUTH_EDIT) {
81               $JSINFO['start_open'] = 0;
82           }
83          // msg($JSINFO['se_device']);
84		      if($p != 'all')
85              {
86                  if($JSINFO['se_device'] == 'desktop' || $JSINFO['se_device'] == 'computer' || $JSINFO['se_device'] == 'tablet') return;
87              }
88
89
90           if($JSINFO['se_device'] == 'phone') {
91               $JSINFO['se_device'] = 'mobile';
92               if(empty($alt_mobile)) {
93                   $alt_mobile =  $JSINFO['se_template'];
94               }
95               else {
96                $alt_mobile  = $this->getConf('mobile_alt');
97                $alt_template_dir = tpl_incdir($alt_mobile);
98                if(file_exists($alt_template_dir)) {
99                    $conf['template'] = $alt_mobile;
100      	            $JSINFO['alt_tpl'] = $alt_mobile;
101                }
102               }
103           }
104
105       else {
106          $JSINFO['se_device'] = "";
107       }
108
109       $JSINFO['se_suspend'] = $this->getConf('suspend');
110	   if($conf['template'] != 'dokuwiki' && $JSINFO['se_type'] == 'none') { //	in template.ini?
111	       if($this->check_ini()) return; // if true template configured in template.ini
112	   }
113
114       if($conf['template'] != 'dokuwiki' && $JSINFO['se_type'] != 'none') {  //another template, using configuration mgr
115           $JSINFO['se_template'] = 'other';
116           if(trim($this->getConf('name')) == false) {
117               $JSINFO['se_name'] = '_empty_';
118           }
119           else {
120               $type = $this->getConf('type');
121               $name = trim($this->getConf('name'));
122               $JSINFO['se_name'] = $type == 'id' ? "#$name" : ".$name";
123           }
124       }
125       else {
126         $JSINFO['se_name'] = '_empty_';
127         if($conf['template'] != 'dokuwiki') {
128            $JSINFO['no_ini'] = "1";
129         }
130       }
131    }
132    function device_type() {
133        require_once 'Mobile_Detect.php';
134        $detect = new Mobile_Detect;
135        $deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
136
137        if($deviceType !== 'computer') {
138            if($deviceType =='tablet') {
139                if($this->getConf('tablet_alt')) return 'phone';
140				if($this->getConf('tablet_toggle')) return 'phone';
141            }
142
143            return  $deviceType;
144        }
145        return 'desktop';
146    }
147    function check_ini(){
148	    global $JSINFO;
149        global $conf;
150	    $tpl_ini =  DOKU_PLUGIN. 'sectiontoggle/templates.ini';
151		$tpl_ini_local =  DOKU_PLUGIN. 'sectiontoggle/templates.ini.local';
152	    if(file_exists($tpl_ini)) {
153		   $stored_templates = parse_ini_file($tpl_ini,true);
154           if(file_exists($tpl_ini_local)) {
155               $local_templates = parse_ini_file($tpl_ini_local,true);
156               if(!empty($local_templates)) {
157                   $stored_templates = array_merge($stored_templates,$local_templates);
158               }
159           }
160
161		   if(isset($stored_templates[$conf['template']])) {
162              // $JSINFO['se_tplname'] = $conf['template'];
163			   $type = trim($stored_templates[$conf['template']]['type']);
164			   $name = trim($stored_templates[$conf['template']]['name']);
165			   if(!$type || !$name) return false;
166			   $JSINFO['se_template'] = 'other';
167			   $JSINFO['se_type'] = $type;
168               $JSINFO['se_name'] = $type == 'id' ? "#$name" : ".$name";
169
170			  return true;
171		   }
172	    }
173      return false;
174	}
175
176    function normalize_names($str,$ns = false) {
177        $ar = array();
178        $str = preg_replace("/\s+/", "",$str);
179            $names = explode(',',$str);
180            for ($i = 0; $i < count($names); $i++) {
181                $names[$i] = preg_replace("/^\s?:\s?/", ":",$names[$i]);
182                $names[$i] = trim ($names[$i]);
183                if($names[$i] != ':') $names[$i] = trim ($names[$i],':');
184            if ($ns && !empty($names[$i])) $names[$i].= ":";
185
186            if($names[$i])
187            {
188                $ar[] = $names[$i];
189            }
190        }
191        return $ar;
192    }
193
194    public function add_button_toggle(Doku_Event $event) {
195        if($event->data['view'] != 'page') return;
196        if($this->getConf('show_section_toggle_button')) {
197            array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\sectiontoggle\MenuItemSectionToggle()]);
198        }
199    }
200}