xref: /plugin/bez/action/base.php (revision 2448d85a375c0af214036e69a0d89425ac1d328a)
1<?php
2
3use \dokuwiki\plugin\bez;
4
5class action_plugin_bez_base extends DokuWiki_Action_Plugin {
6
7    /** @var  bez\mdl\Model */
8    protected $model;
9
10    /** @var  bez\meta\Tpl */
11    protected $tpl;
12
13    /**
14     * See init.php:getBaseURL
15     *
16     * @return string
17     */
18    protected function basedir() {
19        if(!empty($conf['basedir'])){
20            $dir = $conf['basedir'];
21        }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
22            $dir = dirname($_SERVER['SCRIPT_NAME']);
23        }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
24            $dir = dirname($_SERVER['PHP_SELF']);
25        }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
26            $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
27                                 $_SERVER['SCRIPT_FILENAME']);
28            $dir = dirname('/'.$dir);
29        }else{
30            $dir = '.'; //probably wrong
31        }
32
33        $dir = str_replace('\\','/',$dir);             // bugfix for weird WIN behaviour
34        $dir = preg_replace('#//+#','/',"/$dir/");     // ensure leading and trailing slashes
35
36        //handle script in lib/exe dir
37        $dir = preg_replace('!lib/exe/$!','',$dir);
38
39        //handle script in lib/plugins dir
40        $dir = preg_replace('!lib/plugins/.*$!','',$dir);
41
42        return $dir;
43    }
44
45    /**
46     * See init.php:getBaseURL
47     *
48     * @return string
49     */
50    protected function baseurl() {
51        //split hostheader into host and port
52        if(isset($_SERVER['HTTP_HOST'])){
53            $parsed_host = parse_url('http://'.$_SERVER['HTTP_HOST']);
54            $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
55            $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
56        }elseif(isset($_SERVER['SERVER_NAME'])){
57            $parsed_host = parse_url('http://'.$_SERVER['SERVER_NAME']);
58            $host = isset($parsed_host['host']) ? $parsed_host['host'] : null;
59            $port = isset($parsed_host['port']) ? $parsed_host['port'] : null;
60        }else{
61            $host = php_uname('n');
62            $port = '';
63        }
64
65        if(is_null($port)){
66            $port = '';
67        }
68
69        if(!is_ssl()){
70            $proto = 'http://';
71            if ($port == '80') {
72                $port = '';
73            }
74        }else{
75            $proto = 'https://';
76            if ($port == '443') {
77                $port = '';
78            }
79        }
80
81        if($port !== '') $port = ':'.$port;
82
83        return $proto.$host.$port;
84    }
85
86    public function loadConfig() {
87        global $conf;
88
89        $update_config = false;
90        if (empty($conf['basedir']) || empty($conf['baseurl'])) {
91            $update_config = true;
92
93            include DOKU_PLUGIN . 'config/settings/config.class.php';
94            $datafile = DOKU_PLUGIN . 'config/settings/config.metadata.php';
95            $configuration = new configuration($datafile);
96        }
97
98        if (empty($conf['basedir'])) {
99            $basedir = $this->basedir();
100            $configuration->setting['basedir']->update($basedir);
101            $conf['basedir'] = $basedir;
102        }
103
104        if (empty($conf['baseurl'])) {
105            $baseurl = $this->baseurl();
106            $configuration->setting['baseurl']->update($baseurl);
107            $conf['baseurl'] = $baseurl;
108        }
109
110        if ($update_config) {
111            $configuration->save_settings('config');
112        }
113        parent::loadConfig();
114    }
115
116
117    public function getPluginName() {
118        return 'bez';
119    }
120
121    public function register(Doku_Event_Handler $controller) {
122    }
123
124    public function getGlobalConf($key='') {
125        global $conf;
126        if ($key == '') {
127            return $conf;
128        }
129        return $conf[$key];
130    }
131
132    public function get_model() {
133        return $this->model;
134    }
135
136    public function get_client() {
137        global $INFO;
138        return $INFO['client'];
139    }
140
141    public function get_tpl() {
142        return $this->tpl;
143    }
144
145    public function get_level() {
146        return $this->model->get_level();
147    }
148
149    public function bez_tpl_include($tpl_file='', $return=false) {
150        $file = DOKU_PLUGIN . "bez/tpl/$tpl_file.php";
151        if (!file_exists($file)) {
152            throw new Exception("$file doesn't exist");
153        }
154
155        $tpl = $this->tpl;
156        if ($return) ob_start();
157        include $file;
158        if ($return) return ob_get_clean();
159
160    }
161
162    public function createObjects($skip_acl=false) {
163        global $auth;
164        global $INFO;
165
166        if ($skip_acl) {
167            $client = false;
168        } else {
169            $client = $INFO['client'];
170        }
171
172        $this->model = new bez\mdl\Model($auth, $client, $this, $skip_acl);
173        $this->tpl = new bez\meta\Tpl($this);
174    }
175
176    public function id() {
177        $args = func_get_args();
178
179        if (count($args) === 0) {
180            return $_GET['id'];
181        }
182
183        $elms = array();
184        foreach ($args as $arg) {
185            if (is_array($arg)) {
186                foreach ($arg as $k => $v) {
187                    $elms[] = $k;
188                    $elms[] = $v;
189                }
190            } else {
191                $elms[] = $arg;
192            }
193        }
194        array_unshift($elms, 'bez');
195
196
197        if ($this->getGlobalConf('lang') != '') {
198            array_unshift($elms, $this->getGlobalConf('lang'));
199        }
200
201        $elms = array_map(function ($elm) {
202            return str_replace(':', '', $elm);
203        }, $elms);
204        return implode(':', $elms);
205    }
206
207    public function url() {
208        global $conf;
209
210        $args = func_get_args();
211        if (count($args) > 0) {
212            $id = call_user_func_array(array($this, 'id'), $args);
213            if ($conf['userewrite'] == '1') {
214                return DOKU_URL . $id;
215            } elseif ($conf['userewrite'] == '2') {
216                return DOKU_URL . 'doku.php/' . $id;
217            } else {
218                return DOKU_URL . 'doku.php?id=' . $id;
219            }
220
221        }
222    }
223}