1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2014-2025
6: *
7: * This program is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU General Public
9: * License as published by the Free Software Foundation; either
10: * version 2 of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program; if not, write to the Free
19: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: *
21: */
22:
23: namespace SimpleID\Base;
24:
25: use Psr\Log\LogLevel;
26: use SimpleID\Module;
27: use SimpleID\ModuleManager;
28: use SimpleID\Auth\AuthManager;
29: use SimpleID\Store\StoreManager;
30: use SimpleID\Util\SecurityToken;
31: use SimpleID\Util\Events\UIBuildEvent;
32: use SimpleID\Util\UI\Template;
33:
34:
35: class UserModule extends Module {
36: static function init($f3) {
37: $f3->route('GET @user: /user/@uid', 'SimpleID\Base\UserModule->user');
38: }
39:
40: /**
41: * Returns the user's public page.
42: *
43: * @param \Base $f3
44: * @param array<string, mixed> $params
45: * @return void
46: */
47: function user($f3, $params) {
48: $web = \Web::instance();
49: $tpl = Template::instance();
50: $store = StoreManager::instance();
51: $mgr = ModuleManager::instance();
52:
53: $this->f3->set('title', $this->f3->get('intl.core.user.user_title'));
54: if (!isset($params['uid'])) {
55: $this->f3->status(400);
56: $this->f3->set('message', $this->f3->get('intl.common.missing_uid'));
57: } else {
58: $user = $store->loadUser($params['uid']);
59:
60: if ($user == NULL) {
61: $this->f3->status(404);
62: $this->f3->set('message', $this->f3->get('intl.common.user_not_found', $params['uid']));
63: } else {
64: header('Vary: Accept');
65:
66: $event = new RouteContentNegotiationEvent($this->f3->get('ALIAS'), $this->f3->get('REQUEST'), $this->f3->get('SERVER.HTTP_ACCEPT'));
67: $dispatcher = \Events::instance();
68: $dispatcher->dispatch($event);
69: if ($event->isPropagationStopped()) return;
70:
71: $xrds_location = $this->getCanonicalURL('@openid_user_xrds');
72: header('X-XRDS-Location: ' . $xrds_location);
73:
74: $this->f3->set('message', $this->f3->get('intl.core.user.user_page', $params['uid']));
75:
76: $this->f3->set('title', $user['uid']);
77: $this->f3->set('xrds', $xrds_location);
78: if ($user->hasLocalOpenIDIdentity()) {
79: $this->f3->set('local_id', $user["identity"]);
80: }
81:
82: $this->f3->set('head', 'openid_head.html');
83: }
84: }
85:
86: print $tpl->render('page.html');
87: }
88:
89: /**
90: * Returns a block containing user information.
91: *
92: * @param UIBuildEvent $event the event to collect the
93: * user information block
94: * @return void
95: */
96: function onProfileBlocks(UIBuildEvent $event) {
97: $auth = AuthManager:: instance();
98: $user = $auth->getUser();
99:
100: $html = '<p>' . $this->f3->get('intl.core.user.profile_label') . '</p>';
101:
102: $html .= "<table><tr><th>" . $this->f3->get('intl.common.name') . "</th><th>" . $this->f3->get('intl.common.value') . "</th></tr>";
103:
104: if (isset($user['userinfo'])) {
105: foreach ($user['userinfo'] as $member => $value) {
106: if (is_array($value)) {
107: foreach ($value as $submember => $subvalue) {
108: $html .= "<tr><td>" . $this->f3->clean($member) . " (" .$this->f3->clean($submember) . ")</td><td>" . $this->f3->clean($subvalue) . "</td></tr>";
109: }
110: } else {
111: $html .= "<tr><td>" . $this->f3->clean($member) . "</td><td>" . $this->f3->clean($value) . "</td></tr>";
112: }
113: }
114: }
115:
116: $html .= "</table>";
117:
118: $event->addBlock('userinfo', $html, -1, [
119: 'title' => $this->f3->get('intl.core.user.userinfo_title')
120: ]);
121: }
122: }
123:
124: ?>
125: