Skip to content

Commit

Permalink
fix some PSR-2 compliance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweissman committed Aug 19, 2017
1 parent 98f575d commit 2b174f6
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 113 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v4.1.10-alpha
- Add support for PHP7 runtime errors to be handled in the same way as Exceptions
- Implement NotFoundExceptionHandler and pass through all NotFoundExceptions to this handler.
- Fix some PSR-2 compliance issues

## v4.1.9-alpha
- Fixes #780, and more efficient way to collect ids in Unique::getPaginatedQuery
Expand Down
29 changes: 15 additions & 14 deletions app/sprinkles/account/src/Util/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public static function getHashType($password)
{
// If the password in the db is 65 characters long, we have an sha1-hashed password.
if (strlen($password) == 65) {
return "sha1";
} else if (substr($password, 0, 7) == "$2y$12$") {
return "legacy";
} else {
return "modern";
return 'sha1';
} elseif (substr($password, 0, 7) == '$2y$12$') {
return 'legacy';
}

return 'modern';
}

/**
Expand Down Expand Up @@ -60,27 +60,28 @@ public static function hash($password)
*/
public static function verify($password, $hash)
{
if (static::getHashType($hash) == "sha1") {
if (static::getHashType($hash) == 'sha1') {
// Legacy UserCake passwords
$salt = substr($hash, 0, 25); // Extract the salt from the hash
$hashInput = $salt . sha1($salt . $password);
if ($hashInput == $hash) {
return true;
} else {
return false;
}
} else if (static::getHashType($hash) == "legacy") {

return false;

} elseif (static::getHashType($hash) == 'legacy') {
// Homegrown implementation (assuming that current install has been using a cost parameter of 12)
// Used for manual implementation of bcrypt.
$cost = '12';
if (substr($hash, 0, 60) == crypt($password, '$2y$' . $cost . '$' . substr($hash, 60))) {
return true;
} else {
return false;
}
} else {
// Modern implementation
return password_verify($password, $hash);

return false;
}

// Modern implementation
return password_verify($password, $hash);
}
}
10 changes: 6 additions & 4 deletions app/sprinkles/admin/src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1177,20 +1177,22 @@ public function updateField($request, $response, $args)
// Special checks and transformations for certain fields
if ($fieldName == 'flag_enabled') {
// Check that we are not disabling the master account
if (($user->id == $config['reserved_user_ids.master']) &&
if (
($user->id == $config['reserved_user_ids.master']) &&
($fieldValue == '0')
) {
$e = new BadRequestException();
$e->addUserMessage('DISABLE_MASTER');
throw $e;
} else if (($user->id == $currentUser->id) &&
} elseif (
($user->id == $currentUser->id) &&
($fieldValue == '0')
) {
$e = new BadRequestException();
$e->addUserMessage('DISABLE_SELF');
throw $e;
}
} else if ($fieldName == 'password') {
} elseif ($fieldName == 'password') {
$fieldValue = Password::hash($fieldValue);
}

Expand Down Expand Up @@ -1222,7 +1224,7 @@ public function updateField($request, $response, $args)
'user_name' => $user->user_name
]);
}
} else if ($fieldName == 'flag_verified') {
} elseif ($fieldName == 'flag_verified') {
$ms->addMessageTranslated('success', 'MANUALLY_ACTIVATED', [
'user_name' => $user->user_name
]);
Expand Down
76 changes: 6 additions & 70 deletions app/sprinkles/core/src/Error/Renderer/WhoopsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\VarDumper\Cloner\AbstractCloner;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use UnexpectedValueException;
use UserFrosting\Sprinkle\Core\Util\Util;
use Whoops\Exception\Formatter;
use Whoops\Exception\Inspector;
use Whoops\Handler\PlainTextHandler;
Expand Down Expand Up @@ -55,7 +56,7 @@ class WhoopsRenderer extends ErrorRenderer
/**
* @var string
*/
private $pageTitle = "Whoops! There was an error.";
private $pageTitle = 'Whoops! There was an error.';

/**
* @var array[]
Expand Down Expand Up @@ -128,7 +129,7 @@ public function __construct($request, $response, $exception, $displayErrorDetail
}

// Add the default, local resource search path:
$this->searchPaths[] = \UserFrosting\VENDOR_DIR . "/filp/whoops/src/Whoops/Resources";
$this->searchPaths[] = \UserFrosting\VENDOR_DIR . '/filp/whoops/src/Whoops/Resources';

// blacklist php provided auth based values
$this->blacklist('_SERVER', 'PHP_AUTH_PW');
Expand Down Expand Up @@ -215,7 +216,7 @@ public function render()

// Nicely format the session object
$session = isset($_SESSION) ? $this->masked($_SESSION, '_SESSION') : [];
$session = ['session' => $this->prettyPrint($session)];
$session = ['session' => Util::prettyPrintArray($session)];

// List of variables that will be passed to the layout template.
$vars = [
Expand Down Expand Up @@ -692,8 +693,8 @@ protected function getResource($resource)
*
* We intentionally dont rely on $GLOBALS as it depends on 'auto_globals_jit' php.ini setting.
*
* @param $superGlobal array One of the superglobal arrays
* @param $superGlobalName string the name of the superglobal array, e.g. '_GET'
* @param array $superGlobal One of the superglobal arrays
* @param string $superGlobalName the name of the superglobal array, e.g. '_GET'
* @return array $values without sensitive data
*/
private function masked(array $superGlobal, $superGlobalName)
Expand All @@ -708,69 +709,4 @@ private function masked(array $superGlobal, $superGlobalName)
}
return $values;
}

/**
* Nicely format an array for printing.
* See https://stackoverflow.com/a/9776726/2970321
*
* @param array
* @return string
*/
private function prettyPrint($arr)
{
$json = json_encode($arr);
$result = '';
$level = 0;
$in_quotes = false;
$in_escape = false;
$ends_line_level = NULL;
$json_length = strlen( $json );

for( $i = 0; $i < $json_length; $i++ ) {
$char = $json[$i];
$new_line_level = NULL;
$post = "";
if( $ends_line_level !== NULL ) {
$new_line_level = $ends_line_level;
$ends_line_level = NULL;
}
if ( $in_escape ) {
$in_escape = false;
} else if( $char === '"' ) {
$in_quotes = !$in_quotes;
} else if( ! $in_quotes ) {
switch( $char ) {
case '}': case ']':
$level--;
$ends_line_level = NULL;
$new_line_level = $level;
break;

case '{': case '[':
$level++;
case ',':
$ends_line_level = $level;
break;

case ':':
$post = " ";
break;

case " ": case "\t": case "\n": case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = NULL;
break;
}
} else if ( $char === '\\' ) {
$in_escape = true;
}
if( $new_line_level !== NULL ) {
$result .= "<br>".str_repeat( "&nbsp;", $new_line_level );
}
$result .= $char.$post;
}

return $result;
}
}
36 changes: 12 additions & 24 deletions app/sprinkles/core/src/ServicesProvider/ServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,11 @@ public function register(ContainerInterface $container)
$container['alerts'] = function ($c) {
$config = $c->config;

if ($config['alert.storage'] == 'cache')
{
if ($config['alert.storage'] == 'cache') {
return new CacheAlertStream($config['alert.key'], $c->translator, $c->cache, $c->config);
}
else if ($config['alert.storage'] == 'session')
{
} elseif ($config['alert.storage'] == 'session') {
return new SessionAlertStream($config['alert.key'], $c->translator, $c->session);
}
else
{
} else {
throw new \Exception("Bad alert storage handler type '{$config['alert.storage']}' specified in configuration file.");
}
};
Expand Down Expand Up @@ -160,21 +155,14 @@ public function register(ContainerInterface $container)

$config = $c->config;

if ($config['cache.driver'] == 'file')
{
if ($config['cache.driver'] == 'file') {
$path = $c->locator->findResource('cache://', true, true);
$cacheStore = new TaggableFileStore($path);
}
else if ($config['cache.driver'] == 'memcached')
{
} elseif ($config['cache.driver'] == 'memcached') {
$cacheStore = new MemcachedStore($config['cache.memcached']);
}
else if ($config['cache.driver'] == 'redis')
{
} elseif ($config['cache.driver'] == 'redis') {
$cacheStore = new RedisStore($config['cache.redis']);
}
else
{
} else {
throw new \Exception("Bad cache store type '{$config['cache.driver']}' specified in configuration file.");
}

Expand Down Expand Up @@ -415,8 +403,8 @@ public function register(ContainerInterface $container)
$config = $c->config;

// Make sure the locale config is a valid string
if (!is_string($config['site.locales.default']) || $config['site.locales.default'] == "") {
throw new \UnexpectedValueException("The locale config is not a valid string.");
if (!is_string($config['site.locales.default']) || $config['site.locales.default'] == '') {
throw new \UnexpectedValueException('The locale config is not a valid string.');
}

// Load the base locale file(s) as specified in the configuration
Expand Down Expand Up @@ -521,7 +509,7 @@ public function register(ContainerInterface $container)
if ($config['session.handler'] == 'file') {
$fs = new FileSystem;
$handler = new FileSessionHandler($fs, $c->locator->findResource('session://'), $config['session.minutes']);
} else if ($config['session.handler'] == 'database') {
} elseif ($config['session.handler'] == 'database') {
$connection = $c->db->connection();
// Table must exist, otherwise an exception will be thrown
$handler = new DatabaseSessionHandler($connection, $config['session.database.table'], $config['session.minutes']);
Expand Down Expand Up @@ -612,8 +600,8 @@ public function register(ContainerInterface $container)

// Register the Slim extension with Twig
$slimExtension = new TwigExtension(
$c['router'],
$c['request']->getUri()
$c->router,
$c->request->getUri()
);
$view->addExtension($slimExtension);

Expand Down
Loading

0 comments on commit 2b174f6

Please sign in to comment.