/** * Check request reCAPTCHA validity * This method return `true` or `false` after validation, and set error in * helper. If `true` error is set to null, otherwise `'incorrect-captcha-sol'` * * Example: * {{{ * class YourController extends \lithium\action\Controller { * public function index() { * if ($this->request->data) { * if (!Recaptcha::check($this->request)) { * return; * } * } * } * } * }}} * @param object $request Pass request object to check method * @return boolean * @throws ConfigException * @throws RuntimeException */ public static function check(\lithium\net\http\Request $request) { $config = Libraries::get('li3_recaptcha', 'keys'); if (!$config['private']) { throw new ConfigException('To use reCAPTCHA you must get API key from' . 'https://www.google.com/recaptcha/admin/create'); } if (!$request->env('SERVER_ADDR')) { throw new RuntimeException('For security reasons, you must pass the remote ip to reCAPTCHA'); } $data = array('privatekey' => $config['private'], 'remoteip' => $request->env('SERVER_ADDR'), 'challenge' => null, 'response' => null); if ($request->data) { $data['challenge'] = $request->data['recaptcha_challenge_field']; $data['response'] = $request->data['recaptcha_response_field']; } if (!$data['challenge'] || !$data['response']) { RecaptchaHelper::$error = 'incorrect-captcha-sol'; return false; } $service = Connections::get('recaptcha'); $serviceRespond = explode("\n", $service->post('/recaptcha/api/verify', $data)); if ($serviceRespond[0] == 'true') { RecaptchaHelper::$error = null; return true; } else { RecaptchaHelper::$error = 'incorrect-captcha-sol'; return false; } }
public function testConstruct() { $request = new Request(array('host' => 'localhost', 'port' => 443, 'headers' => array('Header' => 'Value'), 'body' => array('Part 1'), 'params' => array('param' => 'value'))); $expected = 'localhost'; $result = $request->host; $this->assertEqual($expected, $result); $expected = 443; $result = $request->port; $this->assertEqual($expected, $result); $expected = 'GET'; $result = $request->method; $this->assertEqual($expected, $result); $expected = 'HTTP/1.1'; $result = $request->protocol; $this->assertEqual($expected, $result); $expected = '1.1'; $result = $request->version; $this->assertEqual($expected, $result); $expected = '/'; $result = $request->path; $this->assertEqual($expected, $result); $expected = array('param' => 'value'); $result = $request->params; $this->assertEqual($expected, $result); $expected = array('Host: localhost:443', 'Connection: Close', 'User-Agent: Mozilla/5.0 (Lithium)', 'Header: Value'); $result = $request->headers(); $this->assertEqual($expected, $result); $expected = array(); $result = $request->cookies; $this->assertEqual($expected, $result); $expected = 'Part 1'; $result = $request->body(); $this->assertEqual($expected, $result); }
/** * Overrides `lithium\net\http\Request::to()` to provide the correct options for generating * URLs. For information about this method, see the parent implementation. * * @see lithium\net\http\Request::to() * @param string $format The format to convert to. * @param array $options Override options. * @return mixed The return value type depends on `$format`. */ public function to($format, array $options = array()) { $defaults = array('scheme' => $this->env('HTTPS') ? 'https' : 'http', 'host' => $this->env('HTTP_HOST'), 'path' => $this->_base . $this->url, 'query' => $this->query); return parent::to($format, $options + $defaults); }
public function testToContextWithAuth() { $request = new Request(array('auth' => 'Basic', 'username' => 'Aladdin', 'password' => 'open sesame')); $expected = array('http' => array('method' => 'GET', 'header' => array('Host: localhost', 'Connection: Close', 'User-Agent: Mozilla/5.0', 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='), 'content' => '', 'protocol_version' => '1.1', 'ignore_errors' => true)); $this->assertEqual($expected, $request->to('context')); }
/** * Overrides `lithium\net\http\Request::to()` to provide the correct options for generating * URLs. For information about this method, see the parent implementation. * * @see lithium\net\http\Request::to() * @param string $format The format to convert to. * @param array $options Override options. * @return mixed The return value type depends on `$format`. */ public function to($format, array $options = array()) { $defaults = array('path' => $this->env('base') . '/' . $this->url); return parent::to($format, $options + $defaults); }
/** * Attempts to parse a request object and determine its execution details. * * @see lithium\net\http\Request * @see lithium\net\http\Request::$params * @see lithium\net\http\Route::$_handler * @param \lithium\net\http\Request $request A request object containing the details of * the request to be routed. * @param array $options Used to determine the operation of the method, and override certain * values in the `Request` object: * - `'url'` _string_: If present, will be used to match in place of the `$url` * property of `$request`. * @return object|boolean If this route matches `$request`, returns the request with * execution details attached to it (inside `Request::$params`). Alternatively when * a route handler function was used, returns the result of its invocation. Returns * `false` if the route never matched. */ public function parse($request, array $options = array()) { $defaults = array('url' => $request->url); $options += $defaults; $url = '/' . trim($options['url'], '/'); $pattern = $this->_pattern; if (!preg_match($pattern, $url, $match)) { return false; } foreach ($this->_meta as $key => $compare) { $value = $request->get($key); if (!($compare == $value || is_array($compare) && in_array($value, $compare))) { return false; } } foreach ($this->_config['modifiers'] as $key => $modifier) { if (isset($match[$key])) { $match[$key] = $modifier($match[$key]); } } $result = array_intersect_key($match + array('args' => array()), $this->_keys); foreach ($result as $key => $value) { if ($value === '') { unset($result[$key]); } } $result += $this->_params + $this->_defaults; $request->params = $result + (array) $request->params; $request->persist = array_unique(array_merge($request->persist, $this->_persist)); if ($this->_handler) { $handler = $this->_handler; return $handler($request); } return $request; }
public function testKeepDefinedContentTypeHeaderWhenTypeIsSet() { $request = new Request(array('method' => 'POST', 'type' => 'json', 'headers' => array('Content-Type' => 'text/x-test'))); $expected = 'Content-Type: text/x-test'; $result = $request->headers(); $message = "Expected value `{$expected}` not found in result."; $this->assertTrue(in_array($expected, $result), $message); $expected = '#Content-Type: text/x-test#'; $result = $request->to('string'); $this->assertPattern($expected, $result); }
public function testQueryParamsConstructed() { $url = "http://localhost/path/one.php?param=1¶m=2"; $config = parse_url($url); $request = new Request($config); $expected = "?param=1¶m=2"; $result = $request->queryString(); $this->assertEqual($expected, $result); $expected = "?param=1¶m=2¶m3=3"; $result = $request->queryString(array('param3' => 3)); $this->assertEqual($expected, $result); }
public function body($data = null, $options = array()) { $defaults = array('encode' => false); return parent::body($data, $options + $defaults); }