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