| Total Complexity | 64 |
| Total Lines | 377 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like XoopsGTicket often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XoopsGTicket, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class XoopsGTicket |
||
| 11 | { |
||
| 12 | public $_errors = []; |
||
| 13 | public $_latest_token = ''; |
||
| 14 | public $messages = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * XoopsGTicket constructor. |
||
| 18 | */ |
||
| 19 | public function __construct() |
||
| 41 | ]; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | // render form as plain html |
||
| 46 | /** |
||
| 47 | * @param string $salt |
||
| 48 | * @param int $timeout |
||
| 49 | * @param string $area |
||
| 50 | * |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function getTicketHtml($salt = '', $timeout = 1800, $area = '') |
||
| 54 | { |
||
| 55 | return '<input type="hidden" name="XOOPS_G_TICKET" value="' . $this->issue($salt, $timeout, $area) . '" />'; |
||
| 56 | } |
||
| 57 | |||
| 58 | // returns an object of XoopsFormHidden including theh ticket |
||
| 59 | /** |
||
| 60 | * @param string $salt |
||
| 61 | * @param int $timeout |
||
| 62 | * @param string $area |
||
| 63 | * |
||
| 64 | * @return XoopsFormHidden |
||
| 65 | */ |
||
| 66 | public function getTicketXoopsForm($salt = '', $timeout = 1800, $area = '') |
||
| 67 | { |
||
| 68 | return new XoopsFormHidden('XOOPS_G_TICKET', $this->issue($salt, $timeout, $area)); |
||
| 69 | } |
||
| 70 | |||
| 71 | // add a ticket as Hidden Element into XoopsForm |
||
| 72 | /** |
||
| 73 | * @param $form |
||
| 74 | * @param string $salt |
||
| 75 | * @param int $timeout |
||
| 76 | * @param string $area |
||
| 77 | */ |
||
| 78 | public function addTicketXoopsFormElement(&$form, $salt = '', $timeout = 1800, $area = '') |
||
| 81 | } |
||
| 82 | |||
| 83 | // returns an array for xoops_confirm() ; |
||
| 84 | /** |
||
| 85 | * @param string $salt |
||
| 86 | * @param int $timeout |
||
| 87 | * @param string $area |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | public function getTicketArray($salt = '', $timeout = 1800, $area = '') |
||
| 94 | } |
||
| 95 | |||
| 96 | // return GET parameter string. |
||
| 97 | /** |
||
| 98 | * @param string $salt |
||
| 99 | * @param bool $noamp |
||
| 100 | * @param int $timeout |
||
| 101 | * @param string $area |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getTicketParamString($salt = '', $noamp = false, $timeout = 1800, $area = '') |
||
| 108 | } |
||
| 109 | |||
| 110 | // issue a ticket |
||
| 111 | /** |
||
| 112 | * @param string $salt |
||
| 113 | * @param int $timeout |
||
| 114 | * @param string $area |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function issue($salt = '', $timeout = 1800, $area = '') |
||
| 119 | { |
||
| 120 | global $xoopsModule; |
||
| 121 | |||
| 122 | if ('' === $salt) { |
||
| 123 | $salt = '$2y$07$' . str_replace('+', '.', base64_encode(random_bytes(16))); |
||
| 124 | } |
||
| 125 | |||
| 126 | // create a token |
||
| 127 | list($usec, $sec) = explode(' ', microtime()); |
||
| 128 | $appendix_salt = empty($_SERVER['PATH']) ? XOOPS_DB_NAME : $_SERVER['PATH']; |
||
| 129 | $token = crypt($salt . $usec . $appendix_salt . $sec, $salt); |
||
| 130 | $this->_latest_token = $token; |
||
| 131 | |||
| 132 | if (empty($_SESSION['XOOPS_G_STUBS'])) { |
||
| 133 | $_SESSION['XOOPS_G_STUBS'] = []; |
||
| 134 | } |
||
| 135 | |||
| 136 | // limit max stubs 10 |
||
| 137 | if (count($_SESSION['XOOPS_G_STUBS']) > 10) { |
||
| 138 | $_SESSION['XOOPS_G_STUBS'] = array_slice($_SESSION['XOOPS_G_STUBS'], -10); |
||
| 139 | } |
||
| 140 | |||
| 141 | // record referer if browser send it |
||
| 142 | $referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['REQUEST_URI']; |
||
| 143 | |||
| 144 | // area as module's dirname |
||
| 145 | if (!$area && isset($xoopsModule) && is_object($xoopsModule)) { |
||
| 146 | $area = $xoopsModule->getVar('dirname'); |
||
| 147 | } |
||
| 148 | |||
| 149 | // store stub |
||
| 150 | $_SESSION['XOOPS_G_STUBS'][] = [ |
||
| 151 | 'expire' => time() + $timeout, |
||
| 152 | 'referer' => $referer, |
||
| 153 | 'area' => $area, |
||
| 154 | 'token' => $token |
||
| 155 | ]; |
||
| 156 | |||
| 157 | // paid md5ed token as a ticket |
||
| 158 | return md5($token . XOOPS_DB_PREFIX); |
||
| 159 | } |
||
| 160 | |||
| 161 | // check a ticket |
||
| 162 | /** |
||
| 163 | * @param bool $post |
||
| 164 | * @param string $area |
||
| 165 | * @param bool $allow_repost |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function check($post = true, $area = '', $allow_repost = true) |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | // draw form for repost |
||
| 266 | /** |
||
| 267 | * @param string $area |
||
| 268 | */ |
||
| 269 | public function draw_repost_form($area = '') |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param $key_name |
||
| 310 | * @param $tmp_array |
||
| 311 | * |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | public function extract_post_recursive($key_name, $tmp_array) |
||
| 330 | } |
||
| 331 | |||
| 332 | // clear all stubs |
||
| 333 | public function clear() |
||
| 334 | { |
||
| 335 | $_SESSION['XOOPS_G_STUBS'] = []; |
||
| 336 | } |
||
| 337 | |||
| 338 | // Ticket Using |
||
| 339 | /** |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public function using() |
||
| 343 | { |
||
| 344 | if (!empty($_SESSION['XOOPS_G_STUBS'])) { |
||
| 345 | return true; |
||
| 346 | } else { |
||
| 347 | return false; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | // return errors |
||
| 352 | /** |
||
| 353 | * @param bool $ashtml |
||
| 354 | * |
||
| 355 | * @return array|string |
||
| 356 | */ |
||
| 357 | public function getErrors($ashtml = true) |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param $errNo |
||
| 373 | * @param $errStr |
||
| 374 | * @param $errFile |
||
| 375 | * @param $errLine |
||
| 376 | * @return null |
||
| 377 | */ |
||
| 378 | public function errorHandler4FindOutput($errNo, $errStr, $errFile, $errLine) |
||
| 417 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.