| Total Complexity | 58 |
| Total Lines | 293 |
| 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 |
||
| 26 | class XoopsGTicket |
||
| 27 | { |
||
| 28 | public $_errors = array(); |
||
| 29 | public $_latest_token = ''; |
||
| 30 | public $messages = array(); |
||
| 31 | |||
| 32 | public function __construct() |
||
| 33 | { |
||
| 34 | $xoops = Xoops::getInstance(); |
||
| 35 | $language = $xoops->getConfig('language'); |
||
| 36 | |||
| 37 | // language file |
||
| 38 | if ($language && !strstr($language, '/')) { |
||
| 39 | if (XoopsLoad::fileExists(dirname(__DIR__) . '/language/' . $language . '/gticket_messages.phtml')) { |
||
| 40 | include dirname(__DIR__) . '/language/' . $language . '/gticket_messages.phtml'; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | // default messages |
||
| 45 | if (empty($this->messages)) { |
||
| 46 | $this->messages = array( |
||
| 47 | 'err_general' => 'GTicket Error', 'err_nostubs' => 'No stubs found', |
||
| 48 | 'err_noticket' => 'No ticket found', 'err_nopair' => 'No valid ticket-stub pair found', |
||
| 49 | 'err_timeout' => 'Time out', 'err_areaorref' => 'Invalid area or referer', |
||
| 50 | 'fmt_prompt4repost' => 'error(s) found:<br><span style="background-color:red;font-weight:bold;color:white;">%s</span><br>Confirm it.<br>And do you want to post again?', |
||
| 51 | 'btn_repost' => 'repost', |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | // render form as plain html |
||
| 57 | public function getTicketHtml($salt = '', $timeout = 1800, $area = '') |
||
| 58 | { |
||
| 59 | return '<input type="hidden" name="XOOPS_G_TICKET" value="' . $this->issue($salt, $timeout, $area) . '" />'; |
||
| 60 | } |
||
| 61 | |||
| 62 | // returns an object of XoopsFormHidden including the ticket |
||
| 63 | public function getTicketXoopsForm($salt = '', $timeout = 1800, $area = '') |
||
| 64 | { |
||
| 65 | return new Xoops\Form\Hidden('XOOPS_G_TICKET', $this->issue($salt, $timeout, $area)); |
||
| 66 | } |
||
| 67 | |||
| 68 | // add a ticket as Hidden Element into XoopsForm |
||
| 69 | public function addTicketXoopsFormElement(&$form, $salt = '', $timeout = 1800, $area = '') |
||
| 72 | } |
||
| 73 | |||
| 74 | // returns an array for xoops_confirm() ; |
||
| 75 | public function getTicketArray($salt = '', $timeout = 1800, $area = '') |
||
| 76 | { |
||
| 77 | return array('XOOPS_G_TICKET' => $this->issue($salt, $timeout, $area)); |
||
| 78 | } |
||
| 79 | |||
| 80 | // return GET parameter string. |
||
| 81 | public function getTicketParamString($salt = '', $noamp = false, $timeout = 1800, $area = '') |
||
| 82 | { |
||
| 83 | return ($noamp ? '' : '&') . 'XOOPS_G_TICKET=' . $this->issue($salt, $timeout, $area); |
||
| 84 | } |
||
| 85 | |||
| 86 | // issue a ticket |
||
| 87 | public function issue($salt = '', $timeout = 1800, $area = '') |
||
| 88 | { |
||
| 89 | $xoops = Xoops::getInstance(); |
||
| 90 | |||
| 91 | // create a token |
||
| 92 | list($usec, $sec) = explode(" ", microtime()); |
||
| 93 | $appendix_salt = empty($_SERVER['PATH']) ? \XoopsBaseConfig::get('db-name') : $_SERVER['PATH']; |
||
| 94 | $token = crypt($salt . $usec . $appendix_salt . $sec); |
||
| 95 | $this->_latest_token = $token; |
||
| 96 | |||
| 97 | if (empty($_SESSION['XOOPS_G_STUBS'])) { |
||
| 98 | $_SESSION['XOOPS_G_STUBS'] = array(); |
||
| 99 | } |
||
| 100 | |||
| 101 | // limit max stubs 10 |
||
| 102 | if (sizeof($_SESSION['XOOPS_G_STUBS']) > 10) { |
||
| 103 | $_SESSION['XOOPS_G_STUBS'] = array_slice($_SESSION['XOOPS_G_STUBS'], -10); |
||
| 104 | } |
||
| 105 | |||
| 106 | // record referer if browser send it |
||
| 107 | $referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['REQUEST_URI']; |
||
| 108 | |||
| 109 | // area as module's dirname |
||
| 110 | if (!$area && $xoops->isModule()) { |
||
| 111 | $area = $xoops->module->getVar('dirname'); |
||
|
|
|||
| 112 | } |
||
| 113 | |||
| 114 | // store stub |
||
| 115 | $_SESSION['XOOPS_G_STUBS'][] = array( |
||
| 116 | 'expire' => time() + $timeout, 'referer' => $referer, 'area' => $area, 'token' => $token |
||
| 117 | ); |
||
| 118 | |||
| 119 | // paid md5ed token as a ticket |
||
| 120 | return md5($token . \XoopsBaseConfig::get('db-prefix')); |
||
| 121 | } |
||
| 122 | |||
| 123 | // check a ticket |
||
| 124 | public function check($post = true, $area = '', $allow_repost = true) |
||
| 125 | { |
||
| 126 | $xoops = Xoops::getInstance(); |
||
| 127 | $this->_errors = array(); |
||
| 128 | |||
| 129 | // CHECK: stubs are not stored in session |
||
| 130 | if (!is_array(@$_SESSION['XOOPS_G_STUBS'])) { |
||
| 131 | $this->_errors[] = $this->messages['err_nostubs']; |
||
| 132 | $_SESSION['XOOPS_G_STUBS'] = array(); |
||
| 133 | } |
||
| 134 | |||
| 135 | // get key&val of the ticket from a user's query |
||
| 136 | $ticket = $post ? @$_POST['XOOPS_G_TICKET'] : @$_GET['XOOPS_G_TICKET']; |
||
| 137 | |||
| 138 | // CHECK: no tickets found |
||
| 139 | if (empty($ticket)) { |
||
| 140 | $this->_errors[] = $this->messages['err_noticket']; |
||
| 141 | } |
||
| 142 | |||
| 143 | // gargage collection & find a right stub |
||
| 144 | $stubs_tmp = $_SESSION['XOOPS_G_STUBS']; |
||
| 145 | $_SESSION['XOOPS_G_STUBS'] = array(); |
||
| 146 | foreach ($stubs_tmp as $stub) { |
||
| 147 | // default lifetime 30min |
||
| 148 | if ($stub['expire'] >= time()) { |
||
| 149 | if (md5($stub['token'] . \XoopsBaseConfig::get('db-prefix')) === $ticket) { |
||
| 150 | $found_stub = $stub; |
||
| 151 | } else { |
||
| 152 | // store the other valid stubs into session |
||
| 153 | $_SESSION['XOOPS_G_STUBS'][] = $stub; |
||
| 154 | } |
||
| 155 | } else { |
||
| 156 | if (md5($stub['token'] . \XoopsBaseConfig::get('db-prefix')) === $ticket) { |
||
| 157 | // not CSRF but Time-Out |
||
| 158 | $timeout_flag = true; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | // CHECK: the right stub found or not |
||
| 164 | if (empty($found_stub)) { |
||
| 165 | if (empty($timeout_flag)) { |
||
| 166 | $this->_errors[] = $this->messages['err_nopair']; |
||
| 167 | } else { |
||
| 168 | $this->_errors[] = $this->messages['err_timeout']; |
||
| 169 | } |
||
| 170 | } else { |
||
| 171 | |||
| 172 | // set area if necessary |
||
| 173 | // area as module's dirname |
||
| 174 | if (!$area && $xoops->isModule()) { |
||
| 175 | $area = $xoops->module->getVar('dirname'); |
||
| 176 | } |
||
| 177 | |||
| 178 | // check area or referer |
||
| 179 | if (@$found_stub['area'] == $area) { |
||
| 180 | $area_check = true; |
||
| 181 | } |
||
| 182 | if (!empty($found_stub['referer']) && strstr(@$_SERVER['HTTP_REFERER'], $found_stub['referer'])) { |
||
| 183 | $referer_check = true; |
||
| 184 | } |
||
| 185 | |||
| 186 | if (empty($area_check) && empty($referer_check)) { // loose |
||
| 187 | $this->_errors[] = $this->messages['err_areaorref']; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | if (!empty($this->_errors)) { |
||
| 192 | if ($allow_repost) { |
||
| 193 | // repost form |
||
| 194 | $this->draw_repost_form($area); |
||
| 195 | exit; |
||
| 196 | } else { |
||
| 197 | // failed |
||
| 198 | $this->clear(); |
||
| 199 | return false; |
||
| 200 | } |
||
| 201 | } else { |
||
| 202 | // all green |
||
| 203 | return true; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | // draw form for repost |
||
| 208 | public function draw_repost_form($area = '') |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $key_name |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function extract_post_recursive($key_name, $tmp_array) |
||
| 276 | } |
||
| 277 | |||
| 278 | |||
| 279 | // clear all stubs |
||
| 280 | public function clear() |
||
| 281 | { |
||
| 282 | $_SESSION['XOOPS_G_STUBS'] = array(); |
||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | // Ticket Using |
||
| 287 | public function using() |
||
| 288 | { |
||
| 289 | if (!empty($_SESSION['XOOPS_G_STUBS'])) { |
||
| 290 | return true; |
||
| 291 | } else { |
||
| 292 | return false; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | |||
| 297 | // return errors |
||
| 298 | public function getErrors($ashtml = true) |
||
| 309 | } |
||
| 310 | |||
| 311 | public function errorHandler4FindOutput($errNo, $errStr, $errFile, $errLine) |
||
| 312 | { |
||
| 347 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.