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