5 use Friendica\Core\L10n;
6 use Friendica\Core\Logger;
9 * All modules in Friendica should extend BaseModule, although not all modules
10 * need to extend all the methods described here
12 * The filename of the module in src/Module needs to match the class name
13 * exactly to make the module available.
15 * @author Hypolite Petovan <hypolite@mrpetovan.com>
17 abstract class BaseModule extends BaseObject
20 * @brief Initialization method common to both content() and post()
22 * Extend this method if you need to do any shared processing before both
25 public static function init(array $parameters = [])
30 * @brief Module GET method to display raw content from technical endpoints
32 * Extend this method if the module is supposed to return communication data,
33 * e.g. from protocol implementations.
35 public static function rawContent(array $parameters = [])
40 * @brief Module GET method to display any content
42 * Extend this method if the module is supposed to return any display
43 * through a GET request. It can be an HTML page through templating or a
44 * XML feed or a JSON output.
48 public static function content(array $parameters = [])
56 * @brief Module POST method to process submitted data
58 * Extend this method if the module is supposed to process POST requests.
59 * Doesn't display any content
61 public static function post(array $parameters = [])
66 * @brief Called after post()
70 public static function afterpost(array $parameters = [])
75 * Functions used to protect against Cross-Site Request Forgery
76 * 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.
77 * In this implementation, a security token is reusable (if the user submits a form, goes back and resubmits the form, maybe with small changes;
78 * or if the security token is used for ajax-calls that happen several times), but only valid for a certain amout of time (3hours).
79 * The "typename" seperates the security tokens of different types of forms. This could be relevant in the following case:
80 * A security token is used to protekt a link from CSRF (e.g. the "delete this profile"-link).
81 * If the new page contains by any chance external elements, then the used security token is exposed by the referrer.
82 * Actually, important actions should not be triggered by Links / GET-Requests at all, but somethimes they still are,
83 * 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).
85 public static function getFormSecurityToken($typename = '')
90 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $timestamp . $typename);
92 return $timestamp . '.' . $sec_hash;
95 public static function checkFormSecurityToken($typename = '', $formname = 'form_security_token')
99 if (!empty($_REQUEST[$formname])) {
100 /// @TODO Careful, not secured!
101 $hash = $_REQUEST[$formname];
104 if (!empty($_SERVER['HTTP_X_CSRF_TOKEN'])) {
105 /// @TODO Careful, not secured!
106 $hash = $_SERVER['HTTP_X_CSRF_TOKEN'];
113 $max_livetime = 10800; // 3 hours
117 $x = explode('.', $hash);
118 if (time() > (intval($x[0]) + $max_livetime)) {
122 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $x[0] . $typename);
124 return ($sec_hash == $x[1]);
127 public static function getFormSecurityStandardErrorMessage()
129 return 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;
132 public static function checkFormSecurityTokenRedirectOnError($err_redirect, $typename = '', $formname = 'form_security_token')
134 if (!self::checkFormSecurityToken($typename, $formname)) {
136 Logger::log('checkFormSecurityToken failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
137 Logger::log('checkFormSecurityToken failed: _REQUEST data: ' . print_r($_REQUEST, true), Logger::DATA);
138 notice(self::getFormSecurityStandardErrorMessage());
139 $a->internalRedirect($err_redirect);
143 public static function checkFormSecurityTokenForbiddenOnError($typename = '', $formname = 'form_security_token')
145 if (!self::checkFormSecurityToken($typename, $formname)) {
147 Logger::log('checkFormSecurityToken failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
148 Logger::log('checkFormSecurityToken failed: _REQUEST data: ' . print_r($_REQUEST, true), Logger::DATA);
150 throw new \Friendica\Network\HTTPException\ForbiddenException();