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	function getButton($type="private") {
130        global $INFO;
131        global $lang;
132		if ($type == "private") {
133			echo '<form class="button btn_show" method="post" action="doku.php?id='.$this->getPrivateID().'"><input class="button" type="submit" value="'.$this->getLang('privatenamespace').'"/></form>';
134        } elseif ($type == "public") {
135			echo '<form class="button btn_show" method="post" action="doku.php?id='.$this->getPublicID().'"><input class="button" type="submit" value="'.$this->publicString().'"/></form>';
136		}
137	}
138
139    // 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)
140    // $default adds default interwiki classes or not
141	function getElements($default) {
142        global $INFO;
143        $return = array();
144        // Don't return anything if no known user is logged in
145        if ($_SERVER['REMOTE_USER'] != null) {
146            // Add PRIVATE NAMESPACE START PAGE INFO IF NEEDED (is required by options)
147            if ($this->getConf('create_private_ns')) {
148                $return['private'] = array();
149                $return['private']['id'] = $this->getPrivateID();
150                $return['private']['title'] = $this->getLang('privatenamespace');
151                $return['private']['string'] = $INFO['userinfo']['name'];
152                $return['private']['classes'] = $this->getLinkClasses($this->getPrivateID(), null, $default);
153            }
154            // Add PUBLIC PAGE INFO IF NEEDED (is required by options)
155            if ($this->getConf('create_public_page')) {
156                $return['public'] = array();
157                $return['public']['id'] = $this->getPublicID();
158                $return['public']['title'] = $this->publicString();
159                $return['public']['string'] = $_SERVER['REMOTE_USER'];
160                $return['public']['classes'] = $this->getLinkClasses($this->getPublicID(), null, $default);
161            }
162        }
163        return $return;
164    }
165
166    function privateNamespace() {
167        if ($this->getConf('use_name_string')) {
168            global $INFO;
169            $raw_string = cleanID($INFO['userinfo']['name']);
170            // simon_delage
171            return $raw_string;
172        } else {
173            // simon
174            return strtolower($_SERVER['REMOTE_USER']);
175        }
176    }
177
178    function privateStart() {
179        if ( $this->getConf('use_start_page')) {
180            global $conf;
181            return cleanID($conf['start']);
182        } else {
183            return $this->privateNamespace();
184        }
185    }
186
187    function publicString() {
188        if (strpos($this->getConf('public_pages_ns'),':%NAME%:%START%') !== false) {
189            return $this->getLang('publicnamespace');
190        } else {
191            return $this->getLang('publicpage');
192        }
193    }
194
195}
196