| Conditions | 20 |
| Paths | 365 |
| Total Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 130 | public function check($post = true, $area = '') |
||
| 131 | { |
||
| 132 | global $xoopsModule; |
||
| 133 | |||
| 134 | $this->_errors = array(); |
||
| 135 | |||
| 136 | // CHECK: stubs are not stored in session |
||
| 137 | if (empty($_SESSION['XOOPS_G_STUBS']) || !is_array($_SESSION['XOOPS_G_STUBS'])) { |
||
| 138 | $this->clear(); |
||
| 139 | $this->_errors[] = 'Invalid Session'; |
||
| 140 | |||
| 141 | return false; |
||
| 142 | } |
||
| 143 | |||
| 144 | // get key&val of the ticket from a user's query |
||
| 145 | if ($post) { |
||
| 146 | $ticket = empty($_POST['XOOPS_G_TICKET']) ? '' : $_POST['XOOPS_G_TICKET']; |
||
| 147 | } else { |
||
| 148 | $ticket = empty($_GET['XOOPS_G_TICKET']) ? '' : $_GET['XOOPS_G_TICKET']; |
||
| 149 | } |
||
| 150 | |||
| 151 | // CHECK: no tickets found |
||
| 152 | if (empty($ticket)) { |
||
| 153 | $this->clear(); |
||
| 154 | $this->_errors[] = 'Irregular post found'; |
||
| 155 | |||
| 156 | return false; |
||
| 157 | } |
||
| 158 | |||
| 159 | // gargage collection & find a right stub |
||
| 160 | $stubs_tmp = $_SESSION['XOOPS_G_STUBS']; |
||
| 161 | $_SESSION['XOOPS_G_STUBS'] = array(); |
||
| 162 | foreach ($stubs_tmp as $stub) { |
||
| 163 | // default lifetime 30min |
||
| 164 | if ($stub['expire'] >= time()) { |
||
| 165 | if (md5($stub['token'] . XOOPS_DB_PREFIX) === $ticket) { |
||
| 166 | $found_stub = $stub; |
||
| 167 | } else { |
||
| 168 | // store the other valid stubs into session |
||
| 169 | $_SESSION['XOOPS_G_STUBS'][] = $stub; |
||
| 170 | } |
||
| 171 | } else { |
||
| 172 | if (md5($stub['token'] . XOOPS_DB_PREFIX) === $ticket) { |
||
| 173 | // not CSRF but Time-Out |
||
| 174 | $timeout_flag = true; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | // CHECK: the right stub found or not |
||
| 180 | if (empty($found_stub)) { |
||
| 181 | $this->clear(); |
||
| 182 | if (empty($timeout_flag)) { |
||
| 183 | $this->_errors[] = 'Invalid Session'; |
||
| 184 | } else { |
||
| 185 | $this->_errors[] = 'Time out'; |
||
| 186 | } |
||
| 187 | |||
| 188 | return false; |
||
| 189 | } |
||
| 190 | |||
| 191 | // set area if necessary |
||
| 192 | // area as module's dirname |
||
| 193 | if (!$area && is_object(@$xoopsModule)) { |
||
| 194 | $area = $xoopsModule->getVar('dirname'); |
||
| 195 | } |
||
| 196 | |||
| 197 | // check area or referer |
||
| 198 | if (@$found_stub['area'] == $area) { |
||
| 199 | $area_check = true; |
||
| 200 | } |
||
| 201 | if (!empty($found_stub['referer']) && true === strpos(@$_SERVER['HTTP_REFERER'], $found_stub['referer'])) { |
||
| 202 | $referer_check = true; |
||
| 203 | } |
||
| 204 | |||
| 205 | // if ( empty( $area_check ) || empty( $referer_check ) ) { // restrict |
||
| 206 | if (empty($area_check) && empty($referer_check)) { // loose |
||
| 207 | $this->clear(); |
||
| 208 | $this->_errors[] = 'Invalid area or referer'; |
||
| 209 | |||
| 210 | return false; |
||
| 211 | } |
||
| 212 | |||
| 213 | // all green |
||
| 214 | return true; |
||
| 215 | } |
||
| 216 | |||
| 287 |