Skip to content

Commit

Permalink
Merge pull request pkp#9918 from touhidurabir/i9895_main
Browse files Browse the repository at this point in the history
pkp#9895 app key and encryption service provider attached to container
  • Loading branch information
touhidurabir authored Jul 1, 2024
2 parents b66865f + 4d85c0f commit 203067f
Show file tree
Hide file tree
Showing 19 changed files with 870 additions and 205 deletions.
46 changes: 46 additions & 0 deletions classes/cliTool/CommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* @file classes/cliTool/CommandInterface.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @trait CommandInterface
*
* @brief Class to provide IO interface on CLI
*/

namespace PKP\cliTool;

use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\OutputStyle;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\StreamOutput;

class CommandInterface
{
use InteractsWithIO;

public function __construct()
{
$output = new OutputStyle(
new StringInput(''),
new StreamOutput(fopen('php://stdout', 'w'))
);

$this->setOutput($output);
}

public function errorBlock(array $messages = [], ?string $title = null): void
{
$this->getOutput()->block(
$messages,
$title,
'fg=white;bg=red',
' ',
true
);
}
}
1 change: 1 addition & 0 deletions classes/cliTool/UpgradeTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use APP\core\Application;
use APP\install\Upgrade;
use PKP\core\PKPAppKey;
use PKP\site\VersionCheck;

Application::upgrade();
Expand Down
67 changes: 67 additions & 0 deletions classes/cliTool/traits/HasCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* @file classes/cliTool/traits/HasCommandInterface.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @trait HasCommandInterface
*
* @brief A helper trait for CLI tools that provide functionality to read/write on CLI interface
*/

namespace PKP\cliTool\traits;

use PKP\cliTool\CommandInterface;
use Symfony\Component\Console\Helper\Helper;

trait HasCommandInterface
{
/**
* CLI interface, this object should extends InteractsWithIO
*/
protected ?CommandInterface $commandInterface = null;

/**
* Set the command interface
*/
public function setCommandInterface(?CommandInterface $commandInterface = null): self
{
$this->commandInterface = $commandInterface ?? new CommandInterface;

return $this;
}

/**
* Get the command interface
*/
public function getCommandInterface(): CommandInterface
{
return $this->commandInterface;
}

/**
* Print given options in a pretty way.
*/
protected function printCommandList(array $options, bool $shouldTranslate = true): void
{
$width = (int)collect(array_keys($options))
->map(fn($command) => Helper::width($command))
->sort()
->last() + 2;

foreach ($options as $commandName => $description) {
$spacingWidth = $width - Helper::width($commandName);
$this->getCommandInterface()->line(
sprintf(
' <info>%s</info>%s%s',
$commandName,
str_repeat(' ', $spacingWidth),
$shouldTranslate ? __($description) : $description
)
);
}
}
}
76 changes: 76 additions & 0 deletions classes/cliTool/traits/HasParameterList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* @file classes/cliTool/traits/HasParameterList.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @trait HasParameterList
*
* @brief A helper trait manage the params and flags on CLI interface
*/

namespace PKP\cliTool\traits;

trait HasParameterList
{
/**
* Parameters and arguments from CLI
*/
protected ?array $parameterList = null;

/**
* Save the parameter list passed on CLI
*
* @param array $items Array with parameters and arguments passed on CLI
*/
public function setParameterList(array $items): static
{
$parameters = [];

foreach ($items as $param) {
if (strpos($param, '=')) {
[$key, $value] = explode('=', ltrim($param, '-'));
$parameters[$key] = $value;

continue;
}

$parameters[] = $param;
}

$this->parameterList = $parameters;

return $this;
}

/**
* Get the parameter list passed on CLI
*/
public function getParameterList(): ?array
{
return $this->parameterList;
}

/**
* Get the value of a specific parameter
*/
protected function getParameterValue(string $parameter, mixed $default = null): mixed
{
if (!isset($this->getParameterList()[$parameter])) {
return $default;
}

return $this->getParameterList()[$parameter];
}

/**
* Determined if the given flag set on CLI
*/
protected function hasFlagSet(string $flag): bool
{
return in_array($flag, $this->getParameterList());
}
}
6 changes: 6 additions & 0 deletions classes/config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ class Config
* ]
*/
public const SENSITIVE_DATA = [
'general' => [
'app_key',
],
'database' => [
'password',
],
'email' => [
'smtp_password',
],
'security' => [
'api_key_secret',
],
];

/**
Expand Down
35 changes: 18 additions & 17 deletions classes/config/ConfigParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,23 @@
/**
* @file classes/config/ConfigParser.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2014-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ConfigParser
*
* @ingroup config
*
* @brief Class for parsing and modifying php.ini style configuration files.
*/

namespace PKP\config;

class ConfigParser
{
/** @var string Contents of the config file currently being parsed */
public $content;

/**
* Constructor.
* Contents of the config file currently being parsed
*/
public function __construct()
{
}
protected string $content;

/**
* Read a configuration file into a multidimensional array.
Expand All @@ -36,7 +29,7 @@ public function __construct()
*
* @return bool|array the configuration data (same format as http://php.net/parse_ini_file)
*/
public static function &readConfig($file)
public static function &readConfig(string $file): bool|array
{
$configData = [];
$currentSection = false;
Expand Down Expand Up @@ -129,7 +122,7 @@ public static function &readConfig($file)
*
* @return bool true if file could be read, false otherwise
*/
public function updateConfig($file, $params)
public function updateConfig(string $file, array $params): bool
{
if (!file_exists($file) || !is_readable($file)) {
return false;
Expand Down Expand Up @@ -189,7 +182,7 @@ public function updateConfig($file, $params)
*
* @return bool file write is successful
*/
public function writeConfig($file)
public function writeConfig(string $file): bool
{
if (!(file_exists($file) && is_writable($file))
&& !(!file_exists($file) && is_dir(dirname($file)) && is_writable(dirname($file)))) {
Expand All @@ -209,11 +202,19 @@ public function writeConfig($file)

/**
* Return the contents of the current config file.
*
* @return string
*/
public function getFileContents()
public function getFileContents(): string
{
return $this->content;
}

/**
* Set the content of file
*/
public function setFileContent(string $content): static
{
$this->content = $content;

return $this;
}
}
16 changes: 5 additions & 11 deletions classes/core/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,11 @@ public function initSession(): void

(new \Illuminate\Pipeline\Pipeline(PKPContainer::getInstance()))
->send($illuminateRequest)
->through(
app()->has('encrypter')
? [
\PKP\middleware\PKPEncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\PKP\middleware\PKPAuthenticateSession::class,
] : [
\Illuminate\Session\Middleware\StartSession::class,
\PKP\middleware\PKPAuthenticateSession::class,
]
)
->through([
\PKP\middleware\PKPEncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\PKP\middleware\PKPAuthenticateSession::class,
])
->via('handle')
->then(function (\Illuminate\Http\Request $request) {
return app()->get(\Illuminate\Http\Response::class);
Expand Down
Loading

0 comments on commit 203067f

Please sign in to comment.