| Total Complexity | 47 |
| Total Lines | 324 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TReCaptcha 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 TReCaptcha, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | class TReCaptcha extends \Prado\Web\UI\WebControls\TWebControl implements \Prado\Web\UI\IValidatable |
||
| 60 | { |
||
| 61 | private $_isValid = true; |
||
| 62 | |||
| 63 | const ChallengeFieldName = 'recaptcha_challenge_field'; |
||
| 64 | const ResponseFieldName = 'recaptcha_response_field'; |
||
| 65 | const RECAPTCHA_API_SERVER = "http://www.google.com/recaptcha/api"; |
||
| 66 | const RECAPTCHA_API_SECURE_SERVER = "https://www.google.com/recaptcha/api"; |
||
| 67 | const RECAPTCHA_VERIFY_SERVER = "www.google.com"; |
||
| 68 | const RECAPTCHA_JS = 'http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'; |
||
| 69 | |||
| 70 | public function getTagName() |
||
| 71 | { |
||
| 72 | return 'span'; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns true if this control validated successfully. |
||
| 77 | * Defaults to true. |
||
| 78 | * @return bool wether this control validated successfully. |
||
| 79 | */ |
||
| 80 | public function getIsValid() |
||
| 81 | { |
||
| 82 | return $this->_isValid; |
||
| 83 | } |
||
| 84 | /** |
||
| 85 | * @param bool wether this control is valid. |
||
|
|
|||
| 86 | */ |
||
| 87 | public function setIsValid($value) |
||
| 88 | { |
||
| 89 | $this->_isValid = TPropertyValue::ensureBoolean($value); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getValidationPropertyValue() |
||
| 93 | { |
||
| 94 | return $this->Request[$this->getChallengeFieldName()]; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function getPublicKey() |
||
| 98 | { |
||
| 99 | return $this->getViewState('PublicKey'); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function setPublicKey($value) |
||
| 103 | { |
||
| 104 | return $this->setViewState('PublicKey', TPropertyValue::ensureString($value)); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function getPrivateKey() |
||
| 108 | { |
||
| 109 | return $this->getViewState('PrivateKey'); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function setPrivateKey($value) |
||
| 113 | { |
||
| 114 | return $this->setViewState('PrivateKey', TPropertyValue::ensureString($value)); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getThemeName() |
||
| 120 | } |
||
| 121 | |||
| 122 | public function setThemeName($value) |
||
| 125 | } |
||
| 126 | |||
| 127 | public function getCustomTranslations() |
||
| 130 | } |
||
| 131 | |||
| 132 | public function setCustomTranslations($value) |
||
| 133 | { |
||
| 134 | return $this->setViewState('CustomTranslations', TPropertyValue::ensureArray($value)); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function getLanguage() |
||
| 138 | { |
||
| 139 | return $this->getViewState('Language'); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function setLanguage($value) |
||
| 143 | { |
||
| 144 | return $this->setViewState('Language', TPropertyValue::ensureString($value)); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getCallbackScript() |
||
| 148 | { |
||
| 149 | return $this->getViewState('CallbackScript'); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function setCallbackScript($value) |
||
| 153 | { |
||
| 154 | return $this->setViewState('CallbackScript', TPropertyValue::ensureString($value)); |
||
| 155 | } |
||
| 156 | |||
| 157 | protected function getChallengeFieldName() |
||
| 158 | { |
||
| 159 | return /*$this->ClientID.'_'.*/self::ChallengeFieldName; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function getResponseFieldName() |
||
| 163 | { |
||
| 164 | return /*$this->ClientID.'_'.*/self::ResponseFieldName; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function getClientSideOptions() |
||
| 168 | { |
||
| 169 | $options = []; |
||
| 170 | if ($theme = $this->getThemeName()) |
||
| 171 | $options['theme'] = $theme; |
||
| 172 | if ($lang = $this->getLanguage()) |
||
| 173 | $options['lang'] = $lang; |
||
| 174 | if ($trans = $this->getCustomTranslations()) |
||
| 175 | $options['custom_translations'] = $trans; |
||
| 176 | return $options; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function validate() |
||
| 180 | { |
||
| 181 | if (! |
||
| 182 | ( |
||
| 183 | ($challenge = @$_POST[$this->getChallengeFieldName()]) |
||
| 184 | and |
||
| 185 | ($response = @$_POST[$this->getResponseFieldName()]) |
||
| 186 | ) |
||
| 187 | ) |
||
| 188 | return false; |
||
| 189 | |||
| 190 | return $this->recaptcha_check_answer( |
||
| 191 | $this->getPrivateKey(), |
||
| 192 | $_SERVER["REMOTE_ADDR"], |
||
| 193 | $challenge, |
||
| 194 | $response |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Checks for API keys |
||
| 200 | * @param mixed event parameter |
||
| 201 | */ |
||
| 202 | public function onPreRender($param) |
||
| 203 | { |
||
| 204 | parent::onPreRender($param); |
||
| 205 | |||
| 206 | if("" == $this->getPublicKey()) |
||
| 207 | throw new TConfigurationException('recaptcha_publickey_unknown'); |
||
| 208 | if("" == $this->getPrivateKey()) |
||
| 209 | throw new TConfigurationException('recaptcha_privatekey_unknown'); |
||
| 210 | |||
| 211 | // need to register captcha fields so they will be sent back also in callbacks |
||
| 212 | $page = $this->getPage(); |
||
| 213 | $page->registerRequiresPostData($this->getChallengeFieldName()); |
||
| 214 | $page->registerRequiresPostData($this->getResponseFieldName()); |
||
| 215 | } |
||
| 216 | |||
| 217 | protected function addAttributesToRender($writer) |
||
| 218 | { |
||
| 219 | parent::addAttributesToRender($writer); |
||
| 220 | $writer->addAttribute('id', $this->getClientID()); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function regenerateToken() |
||
| 224 | { |
||
| 225 | // if we're in a callback, then schedule re-rendering of the control |
||
| 226 | // if not, don't do anything, because a new challenge will be rendered anyway |
||
| 227 | if ($this->Page->IsCallback) |
||
| 228 | $this->Page->CallbackClient->jQuery($this->getClientID() . ' #recaptcha_reload', 'click'); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function renderContents($writer) |
||
| 232 | { |
||
| 233 | $readyscript = 'jQuery(document).trigger(' . TJavaScript::quoteString('captchaready:' . $this->getClientID()) . ')'; |
||
| 234 | $cs = $this->Page->ClientScript; |
||
| 235 | $id = $this->getClientID(); |
||
| 236 | $divid = $id . '_1_recaptchadiv'; |
||
| 237 | $writer->write('<div id="' . htmlspecialchars($divid) . '">'); |
||
| 238 | |||
| 239 | if (!$this->Page->IsCallback) |
||
| 240 | { |
||
| 241 | $writer->write(TJavaScript::renderScriptBlock( |
||
| 242 | 'var RecaptchaOptions = ' . TJavaScript::jsonEncode($this->getClientSideOptions()) . ';' |
||
| 243 | )); |
||
| 244 | |||
| 245 | $html = $this->recaptcha_get_html($this->getPublicKey()); |
||
| 246 | /* |
||
| 247 | reCAPTCHA currently does not support multiple validations per page |
||
| 248 | $html = str_replace( |
||
| 249 | array(self::ChallengeFieldName,self::ResponseFieldName), |
||
| 250 | array($this->getChallengeFieldName(),$this->getResponseFieldName()), |
||
| 251 | $html |
||
| 252 | ); |
||
| 253 | */ |
||
| 254 | $writer->write($html); |
||
| 255 | |||
| 256 | $cs->registerEndScript('ReCaptcha::EventScript', 'jQuery(document).ready(function() { ' . $readyscript . '; } );'); |
||
| 257 | } |
||
| 258 | else |
||
| 259 | { |
||
| 260 | $options = $this->getClientSideOptions(); |
||
| 261 | $options['callback'] = new TJavaScriptLiteral('function() { ' . $readyscript . '; ' . $this->getCallbackScript() . '; }'); |
||
| 262 | $cs->registerScriptFile('ReCaptcha::AjaxScript', self::RECAPTCHA_JS); |
||
| 263 | $cs->registerEndScript('ReCaptcha::CreateScript::' . $id, implode(' ', [ |
||
| 264 | 'if (!jQuery(' . TJavaScript::quoteString('#' . $this->getResponseFieldName()) . '))', |
||
| 265 | '{', |
||
| 266 | 'Recaptcha.destroy();', |
||
| 267 | 'Recaptcha.create(', |
||
| 268 | TJavaScript::quoteString($this->getPublicKey()) . ', ', |
||
| 269 | TJavaScript::quoteString($divid) . ', ', |
||
| 270 | TJavaScript::encode($options), |
||
| 271 | ');', |
||
| 272 | '}', |
||
| 273 | ])); |
||
| 274 | } |
||
| 275 | |||
| 276 | $writer->write('</div>'); |
||
| 277 | } |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Gets the challenge HTML (javascript and non-javascript version). |
||
| 282 | * This is called from the browser, and the resulting reCAPTCHA HTML widget |
||
| 283 | * is embedded within the HTML form it was called from. |
||
| 284 | * @param string $pubkey A public key for reCAPTCHA |
||
| 285 | * @param string $error The error given by reCAPTCHA (optional, default is null) |
||
| 286 | * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false) |
||
| 287 | * @return string - The HTML to be embedded in the user's form. |
||
| 288 | */ |
||
| 289 | private function recaptcha_get_html($pubkey, $error = null, $use_ssl = false) |
||
| 299 | <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> |
||
| 300 | <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/> |
||
| 301 | </noscript>'; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Encodes the given data into a query string format |
||
| 306 | * @param $data - array of string elements to be encoded |
||
| 307 | * @return string - encoded request |
||
| 308 | */ |
||
| 309 | private function recaptcha_qsencode ($data) { |
||
| 310 | $req = ""; |
||
| 311 | foreach ($data as $key => $value) |
||
| 312 | $req .= $key . '=' . urlencode(stripslashes($value)) . '&'; |
||
| 313 | |||
| 314 | // Cut the last '&' |
||
| 315 | $req = substr($req, 0, strlen($req) - 1); |
||
| 316 | return $req; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Submits an HTTP POST to a reCAPTCHA server |
||
| 321 | * @param string $host |
||
| 322 | * @param string $path |
||
| 323 | * @param array $data |
||
| 324 | * @param int port |
||
| 325 | * @return array response |
||
| 326 | */ |
||
| 327 | private function recaptcha_http_post($host, $path, $data, $port = 80) |
||
| 328 | { |
||
| 329 | $req = $this->recaptcha_qsencode($data); |
||
| 330 | |||
| 331 | $http_request = "POST $path HTTP/1.0\r\n"; |
||
| 332 | $http_request .= "Host: $host\r\n"; |
||
| 333 | $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; |
||
| 334 | $http_request .= "Content-Length: " . strlen($req) . "\r\n"; |
||
| 335 | $http_request .= "User-Agent: reCAPTCHA/PHP\r\n"; |
||
| 336 | $http_request .= "\r\n"; |
||
| 337 | $http_request .= $req; |
||
| 338 | |||
| 339 | $response = ''; |
||
| 340 | if(false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10))) |
||
| 341 | die('Could not open socket'); |
||
| 342 | |||
| 343 | fwrite($fs, $http_request); |
||
| 344 | |||
| 345 | while (!feof($fs)) |
||
| 346 | $response .= fgets($fs, 1160); // One TCP-IP packet |
||
| 347 | fclose($fs); |
||
| 348 | $response = explode("\r\n\r\n", $response, 2); |
||
| 349 | |||
| 350 | return $response; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Calls an HTTP POST function to verify if the user's guess was correct |
||
| 355 | * @param string $privkey |
||
| 356 | * @param string $remoteip |
||
| 357 | * @param string $challenge |
||
| 358 | * @param string $response |
||
| 359 | * @param array $extra_params an array of extra variables to post to the server |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | private function recaptcha_check_answer($privkey, $remoteip, $challenge, $response, $extra_params = []) |
||
| 383 | } |
||
| 384 | } |
||
| 385 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths