3 * @copyright Copyright (C) 2020, Friendica
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\Core\Logger;
27 * All modules in Friendica should extend BaseModule, although not all modules
28 * need to extend all the methods described here
30 * The filename of the module in src/Module needs to match the class name
31 * exactly to make the module available.
33 * @author Hypolite Petovan <hypolite@mrpetovan.com>
35 abstract class BaseModule
38 * Initialization method common to both content() and post()
40 * Extend this method if you need to do any shared processing before both
43 public static function init(array $parameters = [])
48 * Module GET method to display raw content from technical endpoints
50 * Extend this method if the module is supposed to return communication data,
51 * e.g. from protocol implementations.
53 public static function rawContent(array $parameters = [])
60 * Module GET method to display any content
62 * Extend this method if the module is supposed to return any display
63 * through a GET request. It can be an HTML page through templating or a
64 * XML feed or a JSON output.
68 public static function content(array $parameters = [])
76 * Module POST method to process submitted data
78 * Extend this method if the module is supposed to process POST requests.
79 * Doesn't display any content
81 public static function post(array $parameters = [])
83 // DI::baseurl()->redirect('module');
91 public static function afterpost(array $parameters = [])
96 * Functions used to protect against Cross-Site Request Forgery
97 * 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.
98 * In this implementation, a security token is reusable (if the user submits a form, goes back and resubmits the form, maybe with small changes;
99 * or if the security token is used for ajax-calls that happen several times), but only valid for a certain amount of time (3hours).
100 * The "typename" separates the security tokens of different types of forms. This could be relevant in the following case:
101 * A security token is used to protect a link from CSRF (e.g. the "delete this profile"-link).
102 * If the new page contains by any chance external elements, then the used security token is exposed by the referrer.
103 * Actually, important actions should not be triggered by Links / GET-Requests at all, but sometimes they still are,
104 * 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).
106 public static function getFormSecurityToken($typename = '')
111 $sec_hash = hash('whirlpool', ($a->user['guid'] ?? '') . ($a->user['prvkey'] ?? '') . session_id() . $timestamp . $typename);
113 return $timestamp . '.' . $sec_hash;
116 public static function checkFormSecurityToken($typename = '', $formname = 'form_security_token')
120 if (!empty($_REQUEST[$formname])) {
121 /// @TODO Careful, not secured!
122 $hash = $_REQUEST[$formname];
125 if (!empty($_SERVER['HTTP_X_CSRF_TOKEN'])) {
126 /// @TODO Careful, not secured!
127 $hash = $_SERVER['HTTP_X_CSRF_TOKEN'];
134 $max_livetime = 10800; // 3 hours
138 $x = explode('.', $hash);
139 if (time() > (intval($x[0]) + $max_livetime)) {
143 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $x[0] . $typename);
145 return ($sec_hash == $x[1]);
148 public static function getFormSecurityStandardErrorMessage()
150 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;
153 public static function checkFormSecurityTokenRedirectOnError($err_redirect, $typename = '', $formname = 'form_security_token')
155 if (!self::checkFormSecurityToken($typename, $formname)) {
157 Logger::log('checkFormSecurityToken failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
158 Logger::log('checkFormSecurityToken failed: _REQUEST data: ' . print_r($_REQUEST, true), Logger::DATA);
159 notice(self::getFormSecurityStandardErrorMessage());
160 DI::baseUrl()->redirect($err_redirect);
164 public static function checkFormSecurityTokenForbiddenOnError($typename = '', $formname = 'form_security_token')
166 if (!self::checkFormSecurityToken($typename, $formname)) {
168 Logger::log('checkFormSecurityToken failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
169 Logger::log('checkFormSecurityToken failed: _REQUEST data: ' . print_r($_REQUEST, true), Logger::DATA);
171 throw new \Friendica\Network\HTTPException\ForbiddenException();