Skip to content

Commit

Permalink
Overall improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed Sep 9, 2017
1 parent e8ed0b7 commit 58a62fd
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 65 deletions.
58 changes: 47 additions & 11 deletions src/Database/ORM/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,29 @@ 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);
}

/**
* 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);
}
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
164 changes: 111 additions & 53 deletions src/Helpers/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

use Nova\Support\Facades\Config;
use Nova\Support\Facades\Request;
use Nova\Support\Arr;

use InvalidArgumentException;


/**
Expand All @@ -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;
}
}

0 comments on commit 58a62fd

Please sign in to comment.