| 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 |
||
| 158 | public function check($post = true, $area = '') |
||
| 159 | { |
||
| 160 | global $xoopsModule; |
||
| 161 | |||
| 162 | $this->_errors = array(); |
||
| 163 | |||
| 164 | // CHECK: stubs are not stored in session |
||
| 165 | if (empty($_SESSION['XOOPS_G_STUBS']) || !is_array($_SESSION['XOOPS_G_STUBS'])) { |
||
| 166 | $this->clear(); |
||
| 167 | $this->_errors[] = 'Invalid Session'; |
||
| 168 | |||
| 169 | return false; |
||
| 170 | } |
||
| 171 | |||
| 172 | // get key&val of the ticket from a user's query |
||
| 173 | if ($post) { |
||
| 174 | $ticket = empty($_POST['XOOPS_G_TICKET']) ? '' : $_POST['XOOPS_G_TICKET']; |
||
| 175 | } else { |
||
| 176 | $ticket = empty($_GET['XOOPS_G_TICKET']) ? '' : $_GET['XOOPS_G_TICKET']; |
||
| 177 | } |
||
| 178 | |||
| 179 | // CHECK: no tickets found |
||
| 180 | if (empty($ticket)) { |
||
| 181 | $this->clear(); |
||
| 182 | $this->_errors[] = 'Irregular post found'; |
||
| 183 | |||
| 184 | return false; |
||
| 185 | } |
||
| 186 | |||
| 187 | // gargage collection & find a right stub |
||
| 188 | $stubs_tmp = $_SESSION['XOOPS_G_STUBS']; |
||
| 189 | $_SESSION['XOOPS_G_STUBS'] = array(); |
||
| 190 | foreach ($stubs_tmp as $stub) { |
||
| 191 | // default lifetime 30min |
||
| 192 | if ($stub['expire'] >= time()) { |
||
| 193 | if (md5($stub['token'] . XOOPS_DB_PREFIX) === $ticket) { |
||
| 194 | $found_stub = $stub; |
||
| 195 | } else { |
||
| 196 | // store the other valid stubs into session |
||
| 197 | $_SESSION['XOOPS_G_STUBS'][] = $stub; |
||
| 198 | } |
||
| 199 | } else { |
||
| 200 | if (md5($stub['token'] . XOOPS_DB_PREFIX) === $ticket) { |
||
| 201 | // not CSRF but Time-Out |
||
| 202 | $timeout_flag = true; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | // CHECK: the right stub found or not |
||
| 208 | if (empty($found_stub)) { |
||
| 209 | $this->clear(); |
||
| 210 | if (empty($timeout_flag)) { |
||
| 211 | $this->_errors[] = 'Invalid Session'; |
||
| 212 | } else { |
||
| 213 | $this->_errors[] = 'Time out'; |
||
| 214 | } |
||
| 215 | |||
| 216 | return false; |
||
| 217 | } |
||
| 218 | |||
| 219 | // set area if necessary |
||
| 220 | // area as module's dirname |
||
| 221 | if (!$area && is_object(@$xoopsModule)) { |
||
| 222 | $area = $xoopsModule->getVar('dirname'); |
||
| 223 | } |
||
| 224 | |||
| 225 | // check area or referer |
||
| 226 | if (@$found_stub['area'] == $area) { |
||
| 227 | $area_check = true; |
||
| 228 | } |
||
| 229 | if (!empty($found_stub['referer']) && true === strpos(@$_SERVER['HTTP_REFERER'], $found_stub['referer'])) { |
||
| 230 | $referer_check = true; |
||
| 231 | } |
||
| 232 | |||
| 233 | // if ( empty( $area_check ) || empty( $referer_check ) ) { // restrict |
||
| 234 | if (empty($area_check) && empty($referer_check)) { // loose |
||
| 235 | $this->clear(); |
||
| 236 | $this->_errors[] = 'Invalid area or referer'; |
||
| 237 | |||
| 238 | return false; |
||
| 239 | } |
||
| 240 | |||
| 241 | // all green |
||
| 242 | return true; |
||
| 243 | } |
||
| 244 | |||
| 317 |