diff options
Diffstat (limited to 'app/Core')
-rw-r--r-- | app/Core/Base.php | 8 | ||||
-rw-r--r-- | app/Core/Http/Client.php (renamed from app/Core/HttpClient.php) | 56 | ||||
-rw-r--r-- | app/Core/Http/Request.php (renamed from app/Core/Request.php) | 11 | ||||
-rw-r--r-- | app/Core/Http/Response.php (renamed from app/Core/Response.php) | 8 | ||||
-rw-r--r-- | app/Core/Http/Router.php (renamed from app/Core/Router.php) | 5 | ||||
-rw-r--r-- | app/Core/Security/Token.php (renamed from app/Core/Security.php) | 41 | ||||
-rw-r--r-- | app/Core/Session.php | 1 |
7 files changed, 68 insertions, 62 deletions
diff --git a/app/Core/Base.php b/app/Core/Base.php index d402fb37..11f4e31b 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -12,18 +12,20 @@ use Pimple\Container; * * @property \Kanboard\Core\Helper $helper * @property \Kanboard\Core\Mail\Client $emailClient - * @property \Kanboard\Core\HttpClient $httpClient * @property \Kanboard\Core\Paginator $paginator - * @property \Kanboard\Core\Request $request + * @property \Kanboard\Core\Http\Client $httpClient + * @property \Kanboard\Core\Http\Request $request + * @property \Kanboard\Core\Http\Router $router + * @property \Kanboard\Core\Http\Response $response * @property \Kanboard\Core\Session $session * @property \Kanboard\Core\Template $template * @property \Kanboard\Core\OAuth2 $oauth - * @property \Kanboard\Core\Router $router * @property \Kanboard\Core\Lexer $lexer * @property \Kanboard\Core\ObjectStorage\ObjectStorageInterface $objectStorage * @property \Kanboard\Core\Cache\Cache $memoryCache * @property \Kanboard\Core\Plugin\Hook $hook * @property \Kanboard\Core\Plugin\Loader $pluginLoader + * @property \Kanboard\Core\Security\Token $token * @property \Kanboard\Integration\BitbucketWebhook $bitbucketWebhook * @property \Kanboard\Integration\GithubWebhook $githubWebhook * @property \Kanboard\Integration\GitlabWebhook $gitlabWebhook diff --git a/app/Core/HttpClient.php b/app/Core/Http/Client.php index 7f4ea47a..c6bf36a6 100644 --- a/app/Core/HttpClient.php +++ b/app/Core/Http/Client.php @@ -1,14 +1,16 @@ <?php -namespace Kanboard\Core; +namespace Kanboard\Core\Http; + +use Kanboard\Core\Base; /** * HTTP client * - * @package core + * @package http * @author Frederic Guillot */ -class HttpClient extends Base +class Client extends Base { /** * HTTP connection timeout in seconds @@ -99,6 +101,36 @@ class HttpClient extends Base return ''; } + $stream = @fopen(trim($url), 'r', false, stream_context_create($this->getContext($method, $content, $headers))); + $response = ''; + + if (is_resource($stream)) { + $response = stream_get_contents($stream); + } else { + $this->logger->error('HttpClient: request failed'); + } + + if (DEBUG) { + $this->logger->debug('HttpClient: url='.$url); + $this->logger->debug('HttpClient: payload='.$content); + $this->logger->debug('HttpClient: metadata='.var_export(@stream_get_meta_data($stream), true)); + $this->logger->debug('HttpClient: response='.$response); + } + + return $response; + } + + /** + * Get stream context + * + * @access private + * @param string $method + * @param string $content + * @param string[] $headers + * @return array + */ + private function getContext($method, $content, array $headers) + { $default_headers = array( 'User-Agent: '.self::HTTP_USER_AGENT, 'Connection: close', @@ -126,22 +158,6 @@ class HttpClient extends Base $context['http']['request_fulluri'] = true; } - $stream = @fopen(trim($url), 'r', false, stream_context_create($context)); - $response = ''; - - if (is_resource($stream)) { - $response = stream_get_contents($stream); - } else { - $this->container['logger']->error('HttpClient: request failed'); - } - - if (DEBUG) { - $this->container['logger']->debug('HttpClient: url='.$url); - $this->container['logger']->debug('HttpClient: payload='.$content); - $this->container['logger']->debug('HttpClient: metadata='.var_export(@stream_get_meta_data($stream), true)); - $this->container['logger']->debug('HttpClient: response='.$response); - } - - return $response; + return $context; } } diff --git a/app/Core/Request.php b/app/Core/Http/Request.php index 5eda2d02..9f89a6e2 100644 --- a/app/Core/Request.php +++ b/app/Core/Http/Request.php @@ -1,14 +1,16 @@ <?php -namespace Kanboard\Core; +namespace Kanboard\Core\Http; + +use Kanboard\Core\Base; /** * Request class * - * @package core + * @package http * @author Frederic Guillot */ -class Request +class Request extends Base { /** * Get URL string parameter @@ -57,7 +59,8 @@ class Request */ public function getValues() { - if (! empty($_POST) && Security::validateCSRFFormToken($_POST)) { + if (! empty($_POST) && ! empty($_POST['csrf_token']) && $this->token->validateCSRFToken($_POST['csrf_token'])) { + unset($_POST['csrf_token']); return $_POST; } diff --git a/app/Core/Response.php b/app/Core/Http/Response.php index 528a6302..a793e58b 100644 --- a/app/Core/Response.php +++ b/app/Core/Http/Response.php @@ -1,14 +1,16 @@ <?php -namespace Kanboard\Core; +namespace Kanboard\Core\Http; + +use Kanboard\Core\Base; /** * Response class * - * @package core + * @package http * @author Frederic Guillot */ -class Response +class Response extends Base { /** * Send no cache headers diff --git a/app/Core/Router.php b/app/Core/Http/Router.php index 843f5139..0080b23a 100644 --- a/app/Core/Router.php +++ b/app/Core/Http/Router.php @@ -1,13 +1,14 @@ <?php -namespace Kanboard\Core; +namespace Kanboard\Core\Http; use RuntimeException; +use Kanboard\Core\Base; /** * Router class * - * @package core + * @package http * @author Frederic Guillot */ class Router extends Base diff --git a/app/Core/Security.php b/app/Core/Security/Token.php index 54207ee1..7aca08af 100644 --- a/app/Core/Security.php +++ b/app/Core/Security/Token.php @@ -1,14 +1,16 @@ <?php -namespace Kanboard\Core; +namespace Kanboard\Core\Security; + +use Kanboard\Core\Base; /** - * Security class + * Token Handler * - * @package core + * @package security * @author Frederic Guillot */ -class Security +class Token extends Base { /** * Generate a random token with different methods: openssl or /dev/urandom or fallback to uniqid() @@ -17,7 +19,7 @@ class Security * @access public * @return string Random token */ - public static function generateToken() + public static function getToken() { if (function_exists('openssl_random_pseudo_bytes')) { return bin2hex(\openssl_random_pseudo_bytes(30)); @@ -31,18 +33,16 @@ class Security /** * Generate and store a CSRF token in the current session * - * @static * @access public * @return string Random token */ - public static function getCSRFToken() + public function getCSRFToken() { - $nonce = self::generateToken(); - - if (empty($_SESSION['csrf_tokens'])) { + if (! isset($_SESSION['csrf_tokens'])) { $_SESSION['csrf_tokens'] = array(); } + $nonce = self::getToken(); $_SESSION['csrf_tokens'][$nonce] = true; return $nonce; @@ -51,12 +51,11 @@ class Security /** * Check if the token exists for the current session (a token can be used only one time) * - * @static * @access public * @param string $token CSRF token * @return bool */ - public static function validateCSRFToken($token) + public function validateCSRFToken($token) { if (isset($_SESSION['csrf_tokens'][$token])) { unset($_SESSION['csrf_tokens'][$token]); @@ -65,22 +64,4 @@ class Security return false; } - - /** - * Check if the token used in a form is correct and then remove the value - * - * @static - * @access public - * @param array $values Form values - * @return bool - */ - public static function validateCSRFFormToken(array &$values) - { - if (! empty($values['csrf_token']) && self::validateCSRFToken($values['csrf_token'])) { - unset($values['csrf_token']); - return true; - } - - return false; - } } diff --git a/app/Core/Session.php b/app/Core/Session.php index a93131c7..dd1e760e 100644 --- a/app/Core/Session.php +++ b/app/Core/Session.php @@ -3,6 +3,7 @@ namespace Kanboard\Core; use ArrayAccess; +use Kanboard\Core\Http\Request; /** * Session class |