]> git.mxchange.org Git - friendica.git/blob - src/BaseModule.php
Renaming functions + moving functions from security to Model/Item and BaseModule...
[friendica.git] / src / BaseModule.php
1 <?php
2
3 namespace Friendica;
4
5 use Friendica\Core\L10n;
6 use Friendica\Core\System;
7
8 /**
9  * All modules in Friendica should extend BaseModule, although not all modules
10  * need to extend all the methods described here
11  *
12  * The filename of the module in src/Module needs to match the class name
13  * exactly to make the module available.
14  *
15  * @author Hypolite Petovan <hypolite@mrpetovan.com>
16  */
17 abstract class BaseModule extends BaseObject
18 {
19         /**
20          * @brief Initialization method common to both content() and post()
21          *
22          * Extend this method if you need to do any shared processing before both
23          * content() or post()
24          */
25         public static function init()
26         {
27         }
28
29         /**
30          * @brief Module GET method to display raw content from technical endpoints
31          *
32          * Extend this method if the module is supposed to return communication data,
33          * e.g. from protocol implementations.
34          */
35         public static function rawContent()
36         {
37         }
38
39         /**
40          * @brief Module GET method to display any content
41          *
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.
45          *
46          * @return string
47          */
48         public static function content()
49         {
50                 $o = '';
51
52                 return $o;
53         }
54
55         /**
56          * @brief Module POST method to process submitted data
57          *
58          * Extend this method if the module is supposed to process POST requests.
59          * Doesn't display any content
60          */
61         public static function post()
62         {
63                 // goaway('module');
64         }
65
66         /**
67          * @brief Called after post()
68          *
69          * Unknown purpose
70          */
71         public static function afterpost()
72         {
73
74         }
75
76         /*
77          * Functions used to protect against Cross-Site Request Forgery
78          * 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.
79          * In this implementation, a security token is reusable (if the user submits a form, goes back and resubmits the form, maybe with small changes;
80          * or if the security token is used for ajax-calls that happen several times), but only valid for a certain amout of time (3hours).
81          * The "typename" seperates the security tokens of different types of forms. This could be relevant in the following case:
82          *    A security token is used to protekt a link from CSRF (e.g. the "delete this profile"-link).
83          *    If the new page contains by any chance external elements, then the used security token is exposed by the referrer.
84          *    Actually, important actions should not be triggered by Links / GET-Requests at all, but somethimes they still are,
85          *    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).
86          */
87         public static function getFormSecurityToken($typename = '')
88         {
89                 $a = get_app();
90
91                 $timestamp = time();
92                 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $timestamp . $typename);
93
94                 return $timestamp . '.' . $sec_hash;
95         }
96
97         public static function checkFormSecurityToken($typename = '', $formname = 'form_security_token')
98         {
99                 $hash = null;
100
101                 if (!empty($_REQUEST[$formname])) {
102                         /// @TODO Careful, not secured!
103                         $hash = $_REQUEST[$formname];
104                 }
105
106                 if (!empty($_SERVER['HTTP_X_CSRF_TOKEN'])) {
107                         /// @TODO Careful, not secured!
108                         $hash = $_SERVER['HTTP_X_CSRF_TOKEN'];
109                 }
110
111                 if (empty($hash)) {
112                         return false;
113                 }
114
115                 $max_livetime = 10800; // 3 hours
116
117                 $a = get_app();
118
119                 $x = explode('.', $hash);
120                 if (time() > (IntVal($x[0]) + $max_livetime)) {
121                         return false;
122                 }
123
124                 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $x[0] . $typename);
125
126                 return ($sec_hash == $x[1]);
127         }
128
129         public static function getFormSecurityStandardErrorMessage()
130         {
131                 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         }
133
134         public static function checkFormSecurityTokenRedirectOnError($err_redirect, $typename = '', $formname = 'form_security_token')
135         {
136                 if (!self::checkFormSecurityToken($typename, $formname)) {
137                         $a = get_app();
138                         logger('check_form_security_token failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
139                         logger('check_form_security_token failed: _REQUEST data: ' . print_r($_REQUEST, true), LOGGER_DATA);
140                         notice(check_form_security_std_err_msg());
141                         goaway(System::baseUrl() . $err_redirect);
142                 }
143         }
144
145         public static function checkFormSecurityTokenForbiddenOnError($typename = '', $formname = 'form_security_token')
146         {
147                 if (!self::checkFormSecurityToken($typename, $formname)) {
148                         $a = get_app();
149                         logger('check_form_security_token failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
150                         logger('check_form_security_token failed: _REQUEST data: ' . print_r($_REQUEST, true), LOGGER_DATA);
151                         header('HTTP/1.1 403 Forbidden');
152                         killme();
153                 }
154         }
155 }