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