]> git.mxchange.org Git - friendica.git/blob - src/BaseModule.php
Merge pull request #7828 from nupplaphil/task/move_enotify
[friendica.git] / src / BaseModule.php
1 <?php
2
3 namespace Friendica;
4
5 use Friendica\Core\L10n;
6 use Friendica\Core\Logger;
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(array $parameters = [])
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(array $parameters = [])
36         {
37                 // echo '';
38                 // exit;
39         }
40
41         /**
42          * @brief Module GET method to display any content
43          *
44          * Extend this method if the module is supposed to return any display
45          * through a GET request. It can be an HTML page through templating or a
46          * XML feed or a JSON output.
47          *
48          * @return string
49          */
50         public static function content(array $parameters = [])
51         {
52                 $o = '';
53
54                 return $o;
55         }
56
57         /**
58          * @brief Module POST method to process submitted data
59          *
60          * Extend this method if the module is supposed to process POST requests.
61          * Doesn't display any content
62          */
63         public static function post(array $parameters = [])
64         {
65                 // $a = self::getApp();
66                 // $a->internalRedirect('module');
67         }
68
69         /**
70          * @brief Called after post()
71          *
72          * Unknown purpose
73          */
74         public static function afterpost(array $parameters = [])
75         {
76         }
77
78         /*
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).
88          */
89         public static function getFormSecurityToken($typename = '')
90         {
91                 $a = \get_app();
92
93                 $timestamp = time();
94                 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $timestamp . $typename);
95
96                 return $timestamp . '.' . $sec_hash;
97         }
98
99         public static function checkFormSecurityToken($typename = '', $formname = 'form_security_token')
100         {
101                 $hash = null;
102
103                 if (!empty($_REQUEST[$formname])) {
104                         /// @TODO Careful, not secured!
105                         $hash = $_REQUEST[$formname];
106                 }
107
108                 if (!empty($_SERVER['HTTP_X_CSRF_TOKEN'])) {
109                         /// @TODO Careful, not secured!
110                         $hash = $_SERVER['HTTP_X_CSRF_TOKEN'];
111                 }
112
113                 if (empty($hash)) {
114                         return false;
115                 }
116
117                 $max_livetime = 10800; // 3 hours
118
119                 $a = \get_app();
120
121                 $x = explode('.', $hash);
122                 if (time() > (intval($x[0]) + $max_livetime)) {
123                         return false;
124                 }
125
126                 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $x[0] . $typename);
127
128                 return ($sec_hash == $x[1]);
129         }
130
131         public static function getFormSecurityStandardErrorMessage()
132         {
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;
134         }
135
136         public static function checkFormSecurityTokenRedirectOnError($err_redirect, $typename = '', $formname = 'form_security_token')
137         {
138                 if (!self::checkFormSecurityToken($typename, $formname)) {
139                         $a = \get_app();
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);
144                 }
145         }
146
147         public static function checkFormSecurityTokenForbiddenOnError($typename = '', $formname = 'form_security_token')
148         {
149                 if (!self::checkFormSecurityToken($typename, $formname)) {
150                         $a = \get_app();
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);
153
154                         throw new \Friendica\Network\HTTPException\ForbiddenException();
155                 }
156         }
157 }