1<?php
2/**
3 * Helper Component for the Userhomepage Plugin
4 *
5 * @author   Simon DELAGE <sdelage@gmail.com>
6 * @license: CC Attribution-Share Alike 3.0 Unported <http://creativecommons.org/licenses/by-sa/3.0/>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_userhomepage extends DokuWiki_Plugin {
13
14    // Returns the ID of current user's private namespace start page (even if it doesn't exist)
15    function getPrivateID() {
16        if ($this->getConf('group_by_name')) {
17            // private:s:simon or private:s:simon_delage
18            $this->private_ns = cleanID($this->getConf('users_namespace').':'.strtolower(substr($this->privateNamespace(), 0, 1)).':'. $this->privateNamespace());
19        } else {
20            // private:simon or private:simon_delage
21            $this->private_ns = cleanID($this->getConf('users_namespace').':'. $this->privateNamespace());
22        }
23        // ...:start.txt
24        return $this->private_page = $this->private_ns.':'.$this->privateStart();
25    }
26
27    // Returns the ID of any (or current) user's public page (even if it doesn't exist)
28    function getPublicID($userLogin=null) {
29        global $conf;
30
31        if ($userLogin == null) {
32            $userLogin = $_SERVER['REMOTE_USER'];
33        }
34        if (strpos($this->getConf('public_pages_ns'),':%NAME%:%START%') !== false) {
35            $target = str_replace('%NAME%', $userLogin, $this->getConf('public_pages_ns'));
36            $target = str_replace('%START%', $conf['start'], $target);
37        } else {
38            $target = $this->getConf('public_pages_ns').':'.$userLogin;
39        }
40        return $this->public_page = cleanID($target);
41    }
42
43    // Returns a link to current user's private namespace start page (even if it doesn't exist)
44    // If @param == "loggedinas", the link will be wraped in an <li> element
45    function getPrivateLink($param=null) {
46        global $INFO;
47        global $lang;
48        $pageId = $this->getPrivateID();
49        $classes = 'class="'.$this->getLinkClasses($pageId, "uhp_private").'"';
50        if ($param == "loggedinas") {
51            return '<li>'.$lang['loggedinas'].' <a href="'.wl($pageId).'" '.$classes.' rel="nofollow" title="'.$this->getLang('privatenamespace').'">'.$INFO['userinfo']['name'].' ('.$_SERVER['REMOTE_USER'].')</a></li>';
52        } elseif ($param != null) {
53            return '<a href="'.wl($pageId).'" '.$classes.' rel="nofollow" title="'.$this->getLang('privatenamespace').'">'.$param.'</a>';
54        } else {
55            return '<a href="'.wl($pageId).'" '.$classes.' rel="nofollow" title="'.$this->getLang('privatenamespace').'">'.$this->getLang('privatenamespace').'</a>';
56        }
57    }
58
59    // Returns a link to current user's public page (even if it doesn't exist)
60    // If @param == "loggedinas", the link will be wraped in an <li> element
61    function getPublicLink($param=null) {
62        global $INFO;
63        global $lang;
64        $pageId = $this->getPublicID();
65        $classes = 'class="'.$this->getLinkClasses($pageId, "uhp_public").'"';
66        if ($param == "loggedinas") {
67            return '<li>'.$lang['loggedinas'].' <a href="'.wl($pageId).'" '.$classes.' rel="nofollow" title="'.$this->publicString().'">'.$INFO['userinfo']['name'].' ('.$_SERVER['REMOTE_USER'].')</a></li>';
68        } elseif ($param != null) {
69            return '<a href="'.wl($pageId).'" '.$classes.' rel="nofollow" title="'.$this->publicString().'">'.$param.'</a>';
70        } else {
71            return '<a href="'.wl($pageId).'" '.$classes.' rel="nofollow" title="'.$this->publicString().'">'.$this->publicString().'</a>';
72        }
73    }
74
75    // Returns CSS classes to apply to a UHP link
76    // $pageID is target ID, $class is link's primary CSS class, $default adds default interwiki classes or not
77    function getLinkClasses($pageId = null, $class = null, $default = true) {
78        if ($default) {
79            $ret = $this->getConf('userlink_classes');
80        }
81        if ($class != null) {
82            $ret .= " ".$class;
83        }
84        if ($ret != null) { $ret .= " "; }
85        // Make sure "wikilink1" isn't there yet (because it was previously part of default 'userlink_classes' setting)
86        $ret = str_replace(" wikilink1", "", $ret);
87        if (page_exists($pageId)) {
88            $ret .= "wikilink1";
89        } else {
90            $ret .= "wikilink2";
91        }
92        return $ret;
93    }
94
95    // Returns a more or less complex 'Logged in as' string with link(s) to private and/or public page
96    function getComplexLoggedInAs() {
97        global $INFO;
98        global $lang;
99        // If user's private namespace and public page exist, return a 'Logged in as' string with both styled links)
100        if ((page_exists($this->getPrivateID())) && (page_exists($this->getPublicID()))) {
101            //return '<li>'.$lang['loggedinas'].' <a href="'.wl($this->getPrivateID()).'"  class="uhp_private" rel="nofollow" title="'.$this->getLang('privatenamespace').'">'.$INFO['userinfo']['name'].'</a> (<a href="'.wl($this->getPublicID()).'"  class="uhp_public" rel="nofollow" title="'.$this->publicString().'">'.$_SERVER['REMOTE_USER'].'</a>)</li>';
102            return '<li>'.$lang['loggedinas'].' '.$this->getPrivateLink($INFO['userinfo']['name']).' ('.$this->getPublicLink($_SERVER['REMOTE_USER']).')</li>';
103        // Else if only private namespace exists, return 'Logged in as' string with private namespace styled link
104        } elseif (page_exists($this->getPrivateID())) {
105            return $this->getPrivateLink("loggedinas");
106        // Else if only public page exists, return 'Logged in as' string with public page styled link
107        } elseif (page_exists($this->getPublicID())) {
108            return $this->getPublicLink("loggedinas");
109        // Else default back to standard string
110        } else {
111            return '<li class="user">'.$lang['loggedinas'].' '.userlink().'</li>';
112        }
113    }
114
115    // Returns a link to any user's public page (user login is required and page must exist)
116    // This is to provide proper "Last update by" link
117    function getAnyPublicLink($userLogin) {
118        global $lang;
119        if ($userLogin != null) {
120            $publicID = $this->getPublicID($userLogin);
121            $classes = 'class="'.$this->getLinkClasses($publicID, "uhp_public").'"';
122            $result = '<a href="'.wl($publicID).'" '.$classes.' rel="nofollow" title="'.$this->publicString().'">'.editorinfo($userLogin, true).'</a>';
123            return $result;
124        } else {
125            return false;
126        }
127    }
128
129    /**
130     * Print some info about the current page with a link to last editor's public page
131     *
132     * Based on core function tpl_pageinfo() by Andreas Gohr <andi@splitbrain.org>
133     *
134     * @param bool $ret return content instead of printing it
135     * @return bool|string
136     */
137    function getPageInfo($ret = false) {
138        global $conf;
139        global $lang;
140        global $INFO;
141        global $ID;
142
143        // return if we are not allowed to view the page
144        if(!auth_quickaclcheck($ID)) {
145            return false;
146        }
147
148        // prepare date and path
149        $fn = $INFO['filepath'];
150        if(!$conf['fullpath']) {
151            if($INFO['rev']) {
152                $fn = str_replace($conf['olddir'].'/', '', $fn);
153            } else {
154                $fn = str_replace($conf['datadir'].'/', '', $fn);
155            }
156        }
157        $fn   = utf8_decodeFN($fn);
158        $date = dformat($INFO['lastmod']);
159
160        // print it
161        if($INFO['exists']) {
162            $out = '';
163            $out .= '<bdi>'.$fn.'</bdi>';
164            $out .= ' · ';
165            $out .= $lang['lastmod'];
166            $out .= ' ';
167            $out .= $date;
168            if($INFO['editor']) {
169                $out .= ' '.$lang['by'].' ';
170                $out .= $this->getAnyPublicLink($INFO['editor']);
171            } else {
172                $out .= ' ('.$lang['external_edit'].')';
173            }
174            if($INFO['locked']) {
175                $out .= ' · ';
176                $out .= $lang['lockedby'];
177                $out .= ' ';
178                $out .= $this->getAnyPublicLink($INFO['locked']);
179            }
180            if($ret) {
181                return $out;
182            } else {
183                echo $out;
184                return true;
185            }
186        }
187        return false;
188    }
189
190	function getButton($type="private") {
191        global $INFO;
192        global $lang;
193		if ($type == "private") {
194			echo '<form class="button btn_show" method="post" action="doku.php?id='.$this->getPrivateID().'"><input class="button" type="submit" value="'.$this->getLang('privatenamespace').'"/></form>';
195        } elseif ($type == "public") {
196			echo '<form class="button btn_show" method="post" action="doku.php?id='.$this->getPublicID().'"><input class="button" type="submit" value="'.$this->publicString().'"/></form>';
197		}
198	}
199
200    // Returns an array containing id and language strings of Private NS Start Page and/or Public Page (depending on options, page existance isn't checked)
201    // $default adds default interwiki classes or not
202	function getElements($default) {
203        global $INFO;
204        $return = array();
205        // Don't return anything if no known user is logged in
206        if ($_SERVER['REMOTE_USER'] != null) {
207            // Add PRIVATE NAMESPACE START PAGE INFO IF NEEDED (is required by options)
208            if ($this->getConf('create_private_ns')) {
209                $return['private'] = array();
210                $return['private']['id'] = $this->getPrivateID();
211                $return['private']['title'] = $this->getLang('privatenamespace');
212                $return['private']['string'] = $INFO['userinfo']['name'];
213                $return['private']['classes'] = $this->getLinkClasses($this->getPrivateID(), null, $default);
214            }
215            // Add PUBLIC PAGE INFO IF NEEDED (is required by options)
216            if ($this->getConf('create_public_page')) {
217                $return['public'] = array();
218                $return['public']['id'] = $this->getPublicID();
219                $return['public']['title'] = $this->publicString();
220                $return['public']['string'] = $_SERVER['REMOTE_USER'];
221                $return['public']['classes'] = $this->getLinkClasses($this->getPublicID(), null, $default);
222            }
223        }
224        return $return;
225    }
226
227    function privateNamespace() {
228        if ($this->getConf('use_name_string')) {
229            global $INFO;
230            $raw_string = cleanID($INFO['userinfo']['name']);
231            // simon_delage
232            return $raw_string;
233        } else {
234            // simon
235            return strtolower($_SERVER['REMOTE_USER']);
236        }
237    }
238
239    function privateStart() {
240        if ( $this->getConf('use_start_page')) {
241            global $conf;
242            return cleanID($conf['start']);
243        } else {
244            return $this->privateNamespace();
245        }
246    }
247
248    function publicString() {
249        if (strpos($this->getConf('public_pages_ns'),':%NAME%:%START%') !== false) {
250            return $this->getLang('publicnamespace');
251        } else {
252            return $this->getLang('publicpage');
253        }
254    }
255
256}
257