From 58a62fdc01ef675ed07a2815f30bbbc19482fcbf Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Sat, 9 Sep 2017 16:46:54 +0300 Subject: [PATCH] Overall improvements --- src/Database/ORM/Builder.php | 58 +++++++++--- src/Foundation/Application.php | 2 +- src/Helpers/ReCaptcha.php | 164 ++++++++++++++++++++++----------- 3 files changed, 159 insertions(+), 65 deletions(-) diff --git a/src/Database/ORM/Builder.php b/src/Database/ORM/Builder.php index e502e7a1..4401e6ef 100644 --- a/src/Database/ORM/Builder.php +++ b/src/Database/ORM/Builder.php @@ -81,7 +81,9 @@ public function find($id, $columns = array('*')) return $this->findMany($id, $columns); } - $this->query->where($this->model->getQualifiedKeyName(), '=', $id); + $keyName = $this->model->getQualifiedKeyName(); + + $this->query->where($keyName, '=', $id); return $this->first($columns); } @@ -89,15 +91,19 @@ public function find($id, $columns = array('*')) /** * Find a model by its primary key. * - * @param array $id + * @param array $ids * @param array $columns * @return \Nova\Database\ORM\Model|Collection|static */ - public function findMany($id, $columns = array('*')) + public function findMany($ids, $columns = array('*')) { - if (empty($id)) return $this->model->newCollection(); + if (empty($ids)) { + return $this->model->newCollection(); + } + + $keyName = $this->model->getQualifiedKeyName(); - $this->query->whereIn($this->model->getQualifiedKeyName(), $id); + $this->query->whereIn($keyName, $ids); return $this->get($columns); } @@ -109,13 +115,17 @@ public function findMany($id, $columns = array('*')) * @param array $columns * @return \Nova\Database\ORM\Model|static * - * @throws \Database\ORM\ModelNotFoundException + * @throws \Nova\Database\ORM\ModelNotFoundException */ public function findOrFail($id, $columns = array('*')) { - if (! is_null($model = $this->find($id, $columns))) return $model; + if (! is_null($model = $this->find($id, $columns))) { + return $model; + } + + $className = get_class($this->model); - throw (new ModelNotFoundException)->setModel(get_class($this->model)); + throw (new ModelNotFoundException)->setModel($className); } /** @@ -135,13 +145,39 @@ public function first($columns = array('*')) * @param array $columns * @return \Nova\Database\ORM\Model|static * - * @throws \Database\ORM\ModelNotFoundException + * @throws \Nova\Database\ORM\ModelNotFoundException */ public function firstOrFail($columns = array('*')) { - if (! is_null($model = $this->first($columns))) return $model; + if (! is_null($model = $this->first($columns))) { + return $model; + } + + $className = get_class($this->model); + + throw (new ModelNotFoundException)->setModel($className); + } + + /** + * Execute the query and get the first result or call a callback. + * + * @param \Closure|array $columns + * @param \Closure|null $callback + * @return \Nova\Database\ORM\Model|static|mixed + */ + public function firstOr($columns = array('*'), Closure $callback = null) + { + if ($columns instanceof Closure) { + $callback = $columns; + + $columns = array('*'); + } + + if (! is_null($model = $this->first($columns))) { + return $model; + } - throw (new ModelNotFoundException)->setModel(get_class($this->model)); + return call_user_func($callback); } /** diff --git a/src/Foundation/Application.php b/src/Foundation/Application.php index 73288eab..784684a0 100644 --- a/src/Foundation/Application.php +++ b/src/Foundation/Application.php @@ -37,7 +37,7 @@ class Application extends Container implements HttpKernelInterface, TerminableIn * * @var string */ - const VERSION = '3.77.41'; + const VERSION = '3.77.42'; /** * Indicates if the application has "booted". diff --git a/src/Helpers/ReCaptcha.php b/src/Helpers/ReCaptcha.php index fd675322..96433975 100644 --- a/src/Helpers/ReCaptcha.php +++ b/src/Helpers/ReCaptcha.php @@ -10,6 +10,9 @@ use Nova\Support\Facades\Config; use Nova\Support\Facades\Request; +use Nova\Support\Arr; + +use InvalidArgumentException; /** @@ -18,100 +21,155 @@ class ReCaptcha { /** - * Constant holding the Googe API url. + * Whether or not the verification is active. + * + * @var bool */ - const GOOGLEHOST = 'https://www.google.com/recaptcha/api/siteverify'; + protected $active = true; /** - * Array holding the configuration. + * The site key. + * + * @var string */ - protected $config; - - - public function __construct() - { - $this->config = Config::get('reCaptcha', array()); - } + protected $siteKey; /** - * Get the Status + * The secret key. * - * @return string + * @var string */ - protected function isActive() - { - return array_get($this->config, 'active' , false); - } + protected $secret; /** - * Get the Site Key + * Constant holding the Googe API url. + */ + const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; + + + /** + * Create a new ReCaptcha instance. * - * @return string + * @param array|null $config + * + * @return void + * @throws \InvalidArgumentException */ - protected function getSiteKey() + public function __construct($config = null) { - return array_get($this->config, 'siteKey' , null); + if (is_null($config)) { + $config = Config::get('reCaptcha', array()); + } else if (! is_array($config)) { + throw new InvalidArgumentException('The [config] argument should be an array or null'); + } + + $this->active = $active = (bool) Arr::get($config, 'active' , false); + + if ($active) { + $this->siteKey = Arr::get($config, 'siteKey'); + $this->secret = Arr::get($config, 'secret'); + } } /** - * Get the Secret + * Compare given answer against the generated session. * - * @return string + * @param string|null $response + * @param string|null $remoteIp + * @return boolean */ - protected function getSecretkey() + public static check($response = null, $remoteIp = null) { - return array_get($this->config, 'secret' , null); + $instance = new static(); + + return $instance->verify($response, $remoteIp); } /** * Compare given answer against the generated session. * - * @param string $response + * @param string|null $response + * @param string|null $remoteIp * @return boolean */ - protected function check($response = null) + public function verify($response = null, $remoteIp = null) { - if (! $this->isActive()) return true; - - // Get the recaptcha response value. - $response = $response ?: Request::input('g-recaptcha-response', ''); - - // Build the query string. - $query = http_build_query(array( - 'secret' => $this->getSecretKey(), - 'response' => $response, - 'remoteip' => Request::ip() - )); + if (! $this->isActive()) { + return true; + } - // Calculate the (complete) request URL. - $url = static::GOOGLEHOST .'?' .$query; + // Build the request parameters. + $parameters = array( + 'secret' => $this->getSecret(), + 'response' => $response ?: Request::input('g-recaptcha-response', ''), + 'remoteip' => $remoteIp ?: Request::ip(), + ); - // Perform the request to Google server. - $result = file_get_contents($url); + // Submit the POST request. + $response = $this->submit($parameters); // Evaluate the Google server response. - if ($result !== false) { - $data = json_decode($result, true); + $result = json_decode($response, true); - if (is_array($data)) { - return ($data['success'] === true); - } + if (json_last_error() !== JSON_ERROR_NONE) { + return false; + } else if (is_array($result) && isset($result['success'])) { + return $result['success']; } return false; } /** - * Magic Method for handling dynamic functions. + * Submit the POST request with the specified parameters. * - * @param string $method - * @param array $params - * @return void|mixed + * @param array $parameters + * @return mixed */ - public static function __callStatic($method, $params) + protected function submit(array $parameters) { - $instance = new static(); + $options = array( + 'http' => array( + 'header' => "Content-type: application/x-www-form-urlencoded\r\n", + 'method' => 'POST', + 'content' => http_build_query($parameters, '', '&'), + // Force the peer validation to use www.google.com + 'peer_name' => 'www.google.com', + ), + ); + + $context = stream_context_create($options); + + return file_get_contents(static::SITE_VERIFY_URL, false, $context); + } + + /** + * Get the Status + * + * @return string + */ + public static function isActive() + { + return $this->active; + } - return call_user_func_array(array($instance, $method), $params); + /** + * Get the Site Key + * + * @return string + */ + public function getSiteKey() + { + return $this->siteKey; + } + + /** + * Get the Secret + * + * @return string + */ + public function getSecret() + { + return $this->secret; } }