2019-03-27 09:21:05 -07:00
|
|
|
<?php
|
2015-10-26 23:16:56 -05:00
|
|
|
|
2022-08-29 17:58:52 +09:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-01-31 21:39:50 -06:00
|
|
|
/**
|
2021-07-19 21:32:33 +08:00
|
|
|
* This file is part of CodeIgniter 4 framework.
|
2016-01-31 21:39:50 -06:00
|
|
|
*
|
2020-10-24 16:38:41 +08:00
|
|
|
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
2016-01-31 21:39:50 -06:00
|
|
|
*
|
2021-07-19 21:32:33 +08:00
|
|
|
* For the full copyright and license information, please view
|
|
|
|
* the LICENSE file that was distributed with this source code.
|
2016-01-31 21:39:50 -06:00
|
|
|
*/
|
2019-03-07 19:20:33 -08:00
|
|
|
|
2019-03-27 09:21:05 -07:00
|
|
|
namespace CodeIgniter;
|
|
|
|
|
2024-03-21 12:54:40 +09:00
|
|
|
use CodeIgniter\HTTP\CLIRequest;
|
2020-10-04 00:27:56 +07:00
|
|
|
use CodeIgniter\HTTP\Exceptions\HTTPException;
|
2024-12-19 16:33:56 +08:00
|
|
|
use CodeIgniter\HTTP\Exceptions\RedirectException;
|
2024-03-21 12:54:40 +09:00
|
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
2016-02-25 09:40:41 -06:00
|
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
2018-11-07 18:45:53 +08:00
|
|
|
use CodeIgniter\Validation\Exceptions\ValidationException;
|
2022-07-12 09:00:19 +09:00
|
|
|
use CodeIgniter\Validation\ValidationInterface;
|
2023-06-24 09:20:24 +09:00
|
|
|
use Config\Validation;
|
2018-07-09 23:44:28 -05:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-10-26 23:16:56 -05:00
|
|
|
|
2015-12-18 23:42:54 -06:00
|
|
|
/**
|
|
|
|
* Class Controller
|
2023-09-06 18:13:24 +09:00
|
|
|
*
|
|
|
|
* @see \CodeIgniter\ControllerTest
|
2015-12-18 23:42:54 -06:00
|
|
|
*/
|
2015-10-26 23:16:56 -05:00
|
|
|
class Controller
|
|
|
|
{
|
2021-06-04 22:51:52 +08:00
|
|
|
/**
|
|
|
|
* Helpers that will be automatically loaded on class instantiation.
|
|
|
|
*
|
2024-03-21 12:54:40 +09:00
|
|
|
* @var list<string>
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
protected $helpers = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of the main Request object.
|
|
|
|
*
|
2024-03-21 12:54:40 +09:00
|
|
|
* @var CLIRequest|IncomingRequest
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of the main response object.
|
|
|
|
*
|
|
|
|
* @var ResponseInterface
|
|
|
|
*/
|
|
|
|
protected $response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of logger to use.
|
|
|
|
*
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should enforce HTTPS access for all methods in this controller.
|
|
|
|
*
|
2021-06-08 11:46:56 +08:00
|
|
|
* @var int Number of seconds to set HSTS header
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
protected $forceHTTPS = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Once validation has been run, will hold the Validation instance.
|
|
|
|
*
|
2023-11-09 17:39:32 +09:00
|
|
|
* @var ValidationInterface|null
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
protected $validator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2023-08-08 14:30:20 +08:00
|
|
|
* @return void
|
|
|
|
*
|
2024-12-19 16:33:56 +08:00
|
|
|
* @throws HTTPException|RedirectException
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
|
|
{
|
|
|
|
$this->request = $request;
|
|
|
|
$this->response = $response;
|
|
|
|
$this->logger = $logger;
|
|
|
|
|
2021-06-07 19:06:26 +08:00
|
|
|
if ($this->forceHTTPS > 0) {
|
2021-06-04 22:51:52 +08:00
|
|
|
$this->forceHTTPS($this->forceHTTPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Autoload helper files.
|
|
|
|
helper($this->helpers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A convenience method to use when you need to ensure that a single
|
|
|
|
* method is reached only via HTTPS. If it isn't, then a redirect
|
|
|
|
* will happen back to this method and HSTS header will be sent
|
|
|
|
* to have modern browsers transform requests automatically.
|
|
|
|
*
|
2021-06-08 11:46:56 +08:00
|
|
|
* @param int $duration The number of seconds this link should be
|
2021-06-08 12:11:23 +08:00
|
|
|
* considered secure for. Only with HSTS header.
|
|
|
|
* Default value is 1 year.
|
2021-06-04 22:51:52 +08:00
|
|
|
*
|
2023-08-08 14:30:20 +08:00
|
|
|
* @return void
|
|
|
|
*
|
2024-12-19 16:33:56 +08:00
|
|
|
* @throws HTTPException|RedirectException
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
2021-12-13 22:07:52 +07:00
|
|
|
protected function forceHTTPS(int $duration = 31_536_000)
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
|
|
|
force_https($duration, $this->request, $this->response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-03 11:55:04 +09:00
|
|
|
* How long to cache the current page for.
|
|
|
|
*
|
|
|
|
* @params int $time time to live in seconds.
|
2023-08-08 14:30:20 +08:00
|
|
|
*
|
|
|
|
* @return void
|
2021-06-04 22:51:52 +08:00
|
|
|
*/
|
|
|
|
protected function cachePage(int $time)
|
|
|
|
{
|
2024-02-28 14:08:29 +09:00
|
|
|
service('responsecache')->setTtl($time);
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-01 19:32:59 +09:00
|
|
|
* A shortcut to performing validation on Request data.
|
2021-06-04 22:51:52 +08:00
|
|
|
*
|
|
|
|
* @param array|string $rules
|
|
|
|
* @param array $messages An array of custom error messages
|
|
|
|
*/
|
|
|
|
protected function validate($rules, array $messages = []): bool
|
2022-02-01 19:32:59 +09:00
|
|
|
{
|
|
|
|
$this->setValidator($rules, $messages);
|
|
|
|
|
|
|
|
return $this->validator->withRequest($this->request)->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A shortcut to performing validation on any input data.
|
|
|
|
*
|
|
|
|
* @param array $data The data to validate
|
|
|
|
* @param array|string $rules
|
|
|
|
* @param array $messages An array of custom error messages
|
2022-02-15 14:51:06 +09:00
|
|
|
* @param string|null $dbGroup The database group to use
|
2022-02-01 19:32:59 +09:00
|
|
|
*/
|
2022-02-15 14:51:06 +09:00
|
|
|
protected function validateData(array $data, $rules, array $messages = [], ?string $dbGroup = null): bool
|
2022-02-01 19:32:59 +09:00
|
|
|
{
|
|
|
|
$this->setValidator($rules, $messages);
|
|
|
|
|
2022-02-15 14:51:06 +09:00
|
|
|
return $this->validator->run($data, null, $dbGroup);
|
2022-02-01 19:32:59 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array|string $rules
|
|
|
|
*/
|
|
|
|
private function setValidator($rules, array $messages): void
|
2021-06-04 22:51:52 +08:00
|
|
|
{
|
2024-02-28 14:08:29 +09:00
|
|
|
$this->validator = service('validation');
|
2021-06-04 22:51:52 +08:00
|
|
|
|
|
|
|
// If you replace the $rules array with the name of the group
|
2021-06-07 19:06:26 +08:00
|
|
|
if (is_string($rules)) {
|
2023-06-24 09:20:24 +09:00
|
|
|
$validation = config(Validation::class);
|
2021-06-04 22:51:52 +08:00
|
|
|
|
|
|
|
// If the rule wasn't found in the \Config\Validation, we
|
|
|
|
// should throw an exception so the developer can find it.
|
2021-06-28 22:10:44 +08:00
|
|
|
if (! isset($validation->{$rules})) {
|
2021-06-04 22:51:52 +08:00
|
|
|
throw ValidationException::forRuleNotFound($rules);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no error message is defined, use the error message in the Config\Validation file
|
2024-04-03 22:25:35 +07:00
|
|
|
if ($messages === []) {
|
2021-06-04 22:51:52 +08:00
|
|
|
$errorName = $rules . '_errors';
|
2021-06-28 22:10:44 +08:00
|
|
|
$messages = $validation->{$errorName} ?? [];
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 22:10:44 +08:00
|
|
|
$rules = $validation->{$rules};
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
|
|
|
|
2022-02-01 19:32:59 +09:00
|
|
|
$this->validator->setRules($rules, $messages);
|
2021-06-04 22:51:52 +08:00
|
|
|
}
|
2016-03-18 21:50:38 -05:00
|
|
|
}
|