3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
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
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 use Friendica\Capabilities\ICanHandleRequests;
25 use Friendica\Core\Logger;
26 use Friendica\Model\User;
29 * All modules in Friendica should extend BaseModule, although not all modules
30 * need to extend all the methods described here
32 * The filename of the module in src/Module needs to match the class name
33 * exactly to make the module available.
35 * @author Hypolite Petovan <hypolite@mrpetovan.com>
37 abstract class BaseModule implements ICanHandleRequests
40 protected $parameters = [];
42 public function __construct(array $parameters = [])
44 $this->parameters = $parameters;
50 public function init()
57 public function rawContent()
66 public function content(): string
74 public function delete()
81 public function patch()
88 public function post()
90 // DI::baseurl()->redirect('module');
100 /** Gets the name of the current class */
101 public function getClassName(): string
103 return static::class;
107 * Functions used to protect against Cross-Site Request Forgery
108 * The security token has to base on at least one value that an attacker can't know - here it's the session ID and the private key.
109 * In this implementation, a security token is reusable (if the user submits a form, goes back and resubmits the form, maybe with small changes;
110 * or if the security token is used for ajax-calls that happen several times), but only valid for a certain amount of time (3hours).
111 * The "typename" separates the security tokens of different types of forms. This could be relevant in the following case:
112 * A security token is used to protect a link from CSRF (e.g. the "delete this profile"-link).
113 * If the new page contains by any chance external elements, then the used security token is exposed by the referrer.
114 * Actually, important actions should not be triggered by Links / GET-Requests at all, but sometimes they still are,
115 * so this mechanism brings in some damage control (the attacker would be able to forge a request to a form of this type, but not to forms of other types).
117 public static function getFormSecurityToken($typename = '')
119 $user = User::getById(DI::app()->getLoggedInUserId(), ['guid', 'prvkey']);
121 $sec_hash = hash('whirlpool', ($user['guid'] ?? '') . ($user['prvkey'] ?? '') . session_id() . $timestamp . $typename);
123 return $timestamp . '.' . $sec_hash;
126 public static function checkFormSecurityToken($typename = '', $formname = 'form_security_token')
130 if (!empty($_REQUEST[$formname])) {
131 /// @TODO Careful, not secured!
132 $hash = $_REQUEST[$formname];
135 if (!empty($_SERVER['HTTP_X_CSRF_TOKEN'])) {
136 /// @TODO Careful, not secured!
137 $hash = $_SERVER['HTTP_X_CSRF_TOKEN'];
144 $max_livetime = 10800; // 3 hours
146 $user = User::getById(DI::app()->getLoggedInUserId(), ['guid', 'prvkey']);
148 $x = explode('.', $hash);
149 if (time() > (intval($x[0]) + $max_livetime)) {
153 $sec_hash = hash('whirlpool', ($user['guid'] ?? '') . ($user['prvkey'] ?? '') . session_id() . $x[0] . $typename);
155 return ($sec_hash == $x[1]);
158 public static function getFormSecurityStandardErrorMessage()
160 return DI::l10n()->t("The form security token was not correct. This probably happened because the form has been opened for too long \x28>3 hours\x29 before submitting it.") . EOL;
163 public static function checkFormSecurityTokenRedirectOnError($err_redirect, $typename = '', $formname = 'form_security_token')
165 if (!self::checkFormSecurityToken($typename, $formname)) {
166 Logger::notice('checkFormSecurityToken failed: user ' . DI::app()->getLoggedInUserNickname() . ' - form element ' . $typename);
167 Logger::debug('checkFormSecurityToken failed', ['request' => $_REQUEST]);
168 notice(self::getFormSecurityStandardErrorMessage());
169 DI::baseUrl()->redirect($err_redirect);
173 public static function checkFormSecurityTokenForbiddenOnError($typename = '', $formname = 'form_security_token')
175 if (!self::checkFormSecurityToken($typename, $formname)) {
176 Logger::notice('checkFormSecurityToken failed: user ' . DI::app()->getLoggedInUserNickname() . ' - form element ' . $typename);
177 Logger::debug('checkFormSecurityToken failed', ['request' => $_REQUEST]);
179 throw new \Friendica\Network\HTTPException\ForbiddenException();
183 protected static function getContactFilterTabs(string $baseUrl, string $current, bool $displayCommonTab)
187 'label' => DI::l10n()->t('All contacts'),
188 'url' => $baseUrl . '/contacts',
189 'sel' => !$current || $current == 'all' ? 'active' : '',
192 'label' => DI::l10n()->t('Followers'),
193 'url' => $baseUrl . '/contacts/followers',
194 'sel' => $current == 'followers' ? 'active' : '',
197 'label' => DI::l10n()->t('Following'),
198 'url' => $baseUrl . '/contacts/following',
199 'sel' => $current == 'following' ? 'active' : '',
202 'label' => DI::l10n()->t('Mutual friends'),
203 'url' => $baseUrl . '/contacts/mutuals',
204 'sel' => $current == 'mutuals' ? 'active' : '',
208 if ($displayCommonTab) {
210 'label' => DI::l10n()->t('Common'),
211 'url' => $baseUrl . '/contacts/common',
212 'sel' => $current == 'common' ? 'active' : '',