5 use Friendica\Core\L10n;
6 use Friendica\Core\Logger;
7 use Friendica\Core\System;
10 * All modules in Friendica should extend BaseModule, although not all modules
11 * need to extend all the methods described here
13 * The filename of the module in src/Module needs to match the class name
14 * exactly to make the module available.
16 * @author Hypolite Petovan <hypolite@mrpetovan.com>
18 abstract class BaseModule extends BaseObject
21 * @brief Initialization method common to both content() and post()
23 * Extend this method if you need to do any shared processing before both
26 public static function init()
31 * @brief Module GET method to display raw content from technical endpoints
33 * Extend this method if the module is supposed to return communication data,
34 * e.g. from protocol implementations.
36 public static function rawContent()
41 * @brief Module GET method to display any content
43 * Extend this method if the module is supposed to return any display
44 * through a GET request. It can be an HTML page through templating or a
45 * XML feed or a JSON output.
49 public static function content()
57 * @brief Module POST method to process submitted data
59 * Extend this method if the module is supposed to process POST requests.
60 * Doesn't display any content
62 public static function post()
64 // $a = self::getApp();
65 // $a->internalRedirect('module');
69 * @brief Called after post()
73 public static function afterpost()
79 * Functions used to protect against Cross-Site Request Forgery
80 * 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.
81 * In this implementation, a security token is reusable (if the user submits a form, goes back and resubmits the form, maybe with small changes;
82 * or if the security token is used for ajax-calls that happen several times), but only valid for a certain amout of time (3hours).
83 * The "typename" seperates the security tokens of different types of forms. This could be relevant in the following case:
84 * A security token is used to protekt a link from CSRF (e.g. the "delete this profile"-link).
85 * If the new page contains by any chance external elements, then the used security token is exposed by the referrer.
86 * Actually, important actions should not be triggered by Links / GET-Requests at all, but somethimes they still are,
87 * 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).
89 public static function getFormSecurityToken($typename = '')
94 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $timestamp . $typename);
96 return $timestamp . '.' . $sec_hash;
99 public static function checkFormSecurityToken($typename = '', $formname = 'form_security_token')
103 if (!empty($_REQUEST[$formname])) {
104 /// @TODO Careful, not secured!
105 $hash = $_REQUEST[$formname];
108 if (!empty($_SERVER['HTTP_X_CSRF_TOKEN'])) {
109 /// @TODO Careful, not secured!
110 $hash = $_SERVER['HTTP_X_CSRF_TOKEN'];
117 $max_livetime = 10800; // 3 hours
121 $x = explode('.', $hash);
122 if (time() > (IntVal($x[0]) + $max_livetime)) {
126 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $x[0] . $typename);
128 return ($sec_hash == $x[1]);
131 public static function getFormSecurityStandardErrorMessage()
133 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;
136 public static function checkFormSecurityTokenRedirectOnError($err_redirect, $typename = '', $formname = 'form_security_token')
138 if (!self::checkFormSecurityToken($typename, $formname)) {
140 Logger::log('checkFormSecurityToken failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
141 Logger::log('checkFormSecurityToken failed: _REQUEST data: ' . print_r($_REQUEST, true), Logger::DATA);
142 notice(self::getFormSecurityStandardErrorMessage());
143 $a->internalRedirect($err_redirect);
147 public static function checkFormSecurityTokenForbiddenOnError($typename = '', $formname = 'form_security_token')
149 if (!self::checkFormSecurityToken($typename, $formname)) {
151 Logger::log('checkFormSecurityToken failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
152 Logger::log('checkFormSecurityToken failed: _REQUEST data: ' . print_r($_REQUEST, true), Logger::DATA);
154 System::httpExit(403);