1<?php
2/**
3 * Action Plugin for authsmf20.
4 *
5 * @package SMF DokuWiki
6 * @file action.php
7 * @author digger <digger@mysmf.net>
8 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @version 1.0 beta1
10 */
11
12if (!defined('DOKU_INC')) {
13    die();
14}
15
16/**
17 * Action class for authsmf20 plugin.
18 */
19class action_plugin_authsmf20 extends DokuWiki_Plugin
20{
21
22    /**
23     * Registers a callback function for a given event.
24     *
25     * @param Doku_Event_Handler $controller
26     */
27    public function register(Doku_Event_Handler $controller)
28    {
29        $controller->register_hook('COMMON_USER_LINK', 'AFTER', $this, 'hookUserLink');
30    }
31
32
33    /**
34     * Adds a link to SMF member profile for user's name.
35     *
36     * @param Doku_Event $event
37     */
38    public function hookUserLink(&$event)
39    {
40        global $auth, $conf;
41        $userlink = '<a href="%s" class="interwiki iw_user" rel="nofollow" target="_blank">%s</a>';
42
43        if (empty($event->data['name'])) {
44            $event->data['name'] = $event->data['username'];
45        }
46
47        if ($conf['showuseras'] !== 'username_link' && $conf['showuseras'] !== 'username') {
48            return;
49            // TODO: Add other variants: loginname, email, email_link
50        }
51
52        $data = $auth->getUserData($event->data['username']);
53
54        if (!empty($data['smf_user_id'])) {
55            $event->data['userlink'] = sprintf($userlink, $data['smf_user_profile'], $data['smf_user_realname']);
56            $event->data['name'] = $data['smf_user_realname'];
57        } else {
58            $event->data['userlink'] = sprintf($userlink, '#', $event->data['name']);
59        }
60
61        if ($conf['showuseras'] == 'username') {
62            $event->data['userlink'] = $event->data['name'];
63        }
64
65        $event->data['userlink'] = $this->renderProfileLink($data);
66    }
67
68    /*
69	 * Render all availiable information as a XHTML link to user's profile
70	 * @param  $userinfo   array of all nessesary data for creating a link
71	 * @param  $popup  display popup at top of a link ('top'), bottom ('bottom') or don't display at all ('none')
72	 * @return string  XHTML markup for link to user's profile
73	*/
74    public function renderProfileLink($userinfo, $popup = 'bottom')
75    {
76        if ($userinfo['smf_user_profile']) {
77            // Build basic link
78            $result = '<a href="' . $userinfo['smf_user_profile'] . '" class="userlink' .
79                ($userinfo['smf_user_gender'] ? ' gender-' . hsc($userinfo['smf_user_gender']) : ' gender-unknown') . '">' .
80                hsc($userinfo['smf_user_realname']);
81
82            // Test if we have some data to show
83            //$fields = array_map('trim', explode(",", $this->getConf('fields')));
84            $fields = array('smf_personal_text', 'smf_user_usertitle');
85
86            $is_fields = false;
87            foreach ($fields as $field) {
88                if ($userinfo[$field]) {
89                    $is_fields = true;
90                }
91            }
92
93            // If we should display popup and have some data for it...
94            if (($popup == 'top' or $popup == 'bottom') and
95                ($userinfo['smf_user_avatar'] or $is_fields)) {
96                $result .= '<span class="userlink-popup ' . $popup . '">';
97                if ($userinfo['smf_user_avatar']) {
98                    $result .= '<img src="' . $userinfo['smf_user_avatar'] . '" alt="' . hsc($userinfo['smf_user_realname']) . '" />';
99                }
100                $result .= '<span><strong>' . hsc($userinfo['smf_user_realname']) . '</strong>';
101
102                foreach ($fields as $field) {
103                    if ($userinfo[$field]) {
104                        $result .= '<span class="userlink-' . hsc($field) . '">' . hsc($userinfo[$field]) . '</span>';
105                    }
106                }
107                $result .= '</span></span></a>';
108
109            } else {
110                $result .= '</a>';
111            }
112            return $result;
113        } else {
114            return hsc($userinfo['smf_user_realname']);
115        }
116    }
117
118}
119