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