Skip to content

Commit 19a6dfa

Browse files
author
=
committed
Merge branch 'develop-psr2' into develop
2 parents f29103d + ed2eb48 commit 19a6dfa

18 files changed

+285
-193
lines changed
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@
4343
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
4444
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4545
*/
46-
class TimestampLogFileWriter {
46+
namespace Slim\Extras\Log;
47+
48+
class DateTimeFileWriter
49+
{
4750
/**
4851
* @var resource
4952
*/
@@ -74,7 +77,8 @@ class TimestampLogFileWriter {
7477
* @param array $settings
7578
* @return void
7679
*/
77-
public function __construct( $settings = array() ) {
80+
public function __construct($settings = array())
81+
{
7882
//Merge user settings
7983
$this->settings = array_merge(array(
8084
'path' => './logs',
@@ -93,20 +97,21 @@ public function __construct( $settings = array() ) {
9397
* @param int $level
9498
* @return void
9599
*/
96-
public function write( $object, $level ) {
100+
public function write($object, $level)
101+
{
97102
//Determine label
98103
$label = 'DEBUG';
99-
switch ( $level ) {
100-
case 0:
104+
switch ($level) {
105+
case \Slim\Log::FATAL:
101106
$label = 'FATAL';
102107
break;
103-
case 1:
108+
case \Slim\Log::ERROR:
104109
$label = 'ERROR';
105110
break;
106-
case 2:
111+
case \Slim\Log::WARN:
107112
$label = 'WARN';
108113
break;
109-
case 3:
114+
case \Slim\Log::INFO:
110115
$label = 'INFO';
111116
break;
112117
}
@@ -123,11 +128,11 @@ public function write( $object, $level ) {
123128
), $this->settings['message_format']);
124129

125130
//Open resource handle to log file
126-
if (! $this->resource) {
131+
if (!$this->resource) {
127132
$this->resource = fopen($this->settings['path'] . DIRECTORY_SEPARATOR . date($this->settings['name_format']), 'a');
128133
}
129134

130135
//Output to resource
131136
fwrite($this->resource, $message . PHP_EOL);
132137
}
133-
}
138+
}

Middleware/CsrfGuard.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace Slim\Extras\Middleware;
23

34
/**
45
* CsrfGuard
@@ -16,7 +17,8 @@
1617
* @author Mikhail Osher, https://github.com/miraage
1718
* @version 1.0
1819
*/
19-
class CsrfGuard extends Slim_Middleware {
20+
class CsrfGuard extends \Slim\Middleware
21+
{
2022
/**
2123
* Request key
2224
*
@@ -29,9 +31,10 @@ class CsrfGuard extends Slim_Middleware {
2931
*
3032
* @param string $key Request key
3133
*/
32-
public function __construct( $key = 'csrf_token' ) {
34+
public function __construct($key = 'csrf_token')
35+
{
3336
// Validate key (i won't use htmlspecialchars)
34-
if ( !is_string($key) || empty($key) || preg_match('/[^a-zA-Z0-9\-\_]/', $key) ) {
37+
if (!is_string($key) || empty($key) || preg_match('/[^a-zA-Z0-9\-\_]/', $key)) {
3538
throw new OutOfBoundsException('Invalid key' . $key);
3639
}
3740

@@ -41,7 +44,8 @@ public function __construct( $key = 'csrf_token' ) {
4144
/**
4245
* Call middleware
4346
*/
44-
public function call() {
47+
public function call()
48+
{
4549
// Attach as hook
4650
$this->app->hook('slim.before', array($this, 'check'));
4751

@@ -52,21 +56,22 @@ public function call() {
5256
/**
5357
* Check token
5458
*/
55-
public function check() {
59+
public function check()
60+
{
5661
// Create token
57-
if ( session_id() !== "" ){
58-
if ( ! isset( $_SESSION[ $this->key ] ) ){
59-
$_SESSION[ $this->key ] = sha1( serialize( $_SERVER ) . rand( 0, 0xffffffff ) );
62+
if (session_id() !== "") {
63+
if (!isset($_SESSION[$this->key])) {
64+
$_SESSION[$this->key] = sha1(serialize($_SERVER) . rand(0, 0xffffffff));
6065
}
6166
} else {
6267
throw new Exception( "Session are required to use CSRF Guard" );
6368
}
64-
$token = $_SESSION[ $this->key ];
69+
$token = $_SESSION[$this->key];
6570

6671
// Validate
67-
if ( in_array($this->app->request()->getMethod(), array('POST', 'PUT', 'DELETE')) ) {
72+
if (in_array($this->app->request()->getMethod(), array('POST', 'PUT', 'DELETE'))) {
6873
$usertoken = $this->app->request()->post($this->key);
69-
if ( $token !== $usertoken ) {
74+
if ($token !== $usertoken) {
7075
$this->app->halt(400, 'Missing token');
7176
}
7277
}
@@ -77,4 +82,4 @@ public function check() {
7782
'csrf_token' => $token,
7883
));
7984
}
80-
}
85+
}

Middleware/HttpBasicAuth.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
3636
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3737
*/
38-
class HttpBasicAuth extends Slim_Middleware {
38+
namespace Slim\Extras\Middleware;
39+
40+
class HttpBasicAuth extends \Slim\Middleware
41+
{
3942
/**
4043
* @var string
4144
*/
@@ -57,9 +60,9 @@ class HttpBasicAuth extends Slim_Middleware {
5760
* @param string $username The HTTP Authentication username
5861
* @param string $password The HTTP Authentication password
5962
* @param string $realm The HTTP Authentication realm
60-
* @return void
6163
*/
62-
public function __construct( $username, $password, $realm = 'Protected Area' ) {
64+
public function __construct($username, $password, $realm = 'Protected Area')
65+
{
6366
$this->username = $username;
6467
$this->password = $password;
6568
$this->realm = $realm;
@@ -71,19 +74,18 @@ public function __construct( $username, $password, $realm = 'Protected Area' ) {
7174
* This method will check the HTTP request headers for previous authentication. If
7275
* the request has already authenticated, the next middleware is called. Otherwise,
7376
* a 401 Authentication Required response is returned to the client.
74-
*
75-
* @return void
7677
*/
77-
public function call() {
78+
public function call()
79+
{
7880
$req = $this->app->request();
7981
$res = $this->app->response();
8082
$authUser = $req->headers('PHP_AUTH_USER');
8183
$authPass = $req->headers('PHP_AUTH_PW');
82-
if ( $authUser && $authPass && $authUser === $this->username && $authPass === $this->password ) {
84+
if ($authUser && $authPass && $authUser === $this->username && $authPass === $this->password) {
8385
$this->next->call();
8486
} else {
8587
$res->status(401);
8688
$res->header('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realm));
8789
}
8890
}
89-
}
91+
}

Middleware/HttpDigestAuth.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
3939
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4040
*/
41-
class HttpDigestAuth extends Slim_Middleware {
41+
namespace Slim\Extras\Middleware;
42+
43+
class HttpDigestAuth extends \Slim\Middleware
44+
{
4245
/**
4346
* @var string
4447
*/
@@ -62,7 +65,8 @@ class HttpDigestAuth extends Slim_Middleware {
6265
* @param string $realm The HTTP Authentication realm
6366
* @return void
6467
*/
65-
public function __construct( $username, $password, $realm = 'Protected Area' ) {
68+
public function __construct($username, $password, $realm = 'Protected Area')
69+
{
6670
$this->username = $username;
6771
$this->password = $password;
6872
$this->realm = $realm;
@@ -77,14 +81,15 @@ public function __construct( $username, $password, $realm = 'Protected Area' ) {
7781
*
7882
* @return void
7983
*/
80-
public function call() {
84+
public function call()
85+
{
8186
//Check header and header username
82-
if ( empty($_SERVER['PHP_AUTH_DIGEST']) ) {
87+
if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
8388
$this->fail();
8489
return;
8590
} else {
8691
$data = $this->parseHttpDigest($_SERVER['PHP_AUTH_DIGEST']);
87-
if ( !$data || $data['username'] !== $this->username ) {
92+
if (!$data || $data['username'] !== $this->username) {
8893
$this->fail();
8994
return;
9095
}
@@ -94,7 +99,7 @@ public function call() {
9499
$A1 = md5($data['username'] . ':' . $this->realm . ':' . $this->password);
95100
$A2 = md5($_SERVER['REQUEST_METHOD'] . ':' . $data['uri']);
96101
$validResponse = md5($A1 . ':' . $data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' . $A2);
97-
if ( $data['response'] !== $validResponse ) {
102+
if ($data['response'] !== $validResponse) {
98103
$this->fail();
99104
return;
100105
}
@@ -108,25 +113,44 @@ public function call() {
108113
*
109114
* @return void
110115
*/
111-
protected function fail() {
116+
protected function fail()
117+
{
112118
$this->app->response()->status(401);
113-
$this->app->response()->header('WWW-Authenticate', sprintf('Digest realm="%s",qop="auth",nonce="%s",opaque="%s"', $this->realm, uniqid(), md5($this->realm)));
119+
$this->app->response()->header(
120+
'WWW-Authenticate',
121+
sprintf(
122+
'Digest realm="%s",qop="auth",nonce="%s",opaque="%s"',
123+
$this->realm,
124+
uniqid(),
125+
md5($this->realm)
126+
)
127+
);
114128
}
115129

116130
/**
117131
* Parse HTTP Digest Authentication header
118132
*
119133
* @return array|false
120134
*/
121-
protected function parseHttpDigest( $headerValue ) {
122-
$needed_parts = array('nonce' => 1, 'nc' => 1, 'cnonce' => 1, 'qop' => 1, 'username' => 1, 'uri' => 1, 'response' => 1);
135+
protected function parseHttpDigest($headerValue)
136+
{
137+
$needed_parts = array(
138+
'nonce' => 1,
139+
'nc' => 1,
140+
'cnonce' => 1,
141+
'qop' => 1,
142+
'username' => 1,
143+
'uri' => 1,
144+
'response' => 1
145+
);
123146
$data = array();
124147
$keys = implode('|', array_keys($needed_parts));
125148
preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $headerValue, $matches, PREG_SET_ORDER);
126-
foreach ( $matches as $m ) {
149+
foreach ($matches as $m) {
127150
$data[$m[1]] = $m[3] ? $m[3] : $m[4];
128151
unset($needed_parts[$m[1]]);
129152
}
153+
130154
return $needed_parts ? false : $data;
131155
}
132-
}
156+
}

Middleware/StrongAuth.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
3636
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3737
*/
38-
class StrongAuth extends Slim_Middleware {
38+
namespace Slim\Extras\Middleware;
39+
40+
class StrongAuth extends \Slim\Middleware
41+
{
3942
/**
4043
* @var string
4144
*/
@@ -65,7 +68,8 @@ class StrongAuth extends Slim_Middleware {
6568
* @param array $config Configuration for Strong and Login Details
6669
* @return void
6770
*/
68-
public function __construct(array $config = array()) {
71+
public function __construct(array $config = array())
72+
{
6973
$this->config = array_merge($this->settings, $config);
7074
}
7175

@@ -74,34 +78,34 @@ public function __construct(array $config = array()) {
7478
*
7579
* @return void
7680
*/
77-
public function call() {
81+
public function call()
82+
{
7883
$app = $this->app;
7984
$config = $this->config;
80-
8185
$req = $this->app->request();
8286

8387
// Authentication Initialised
8488
$auth = Strong::factory($this->config);
8589
switch ($this->config['auth_type']) {
8690
case 'form':
8791
$this->formauth($auth, $req);
88-
break;
92+
break;
8993
default:
9094
$this->httpauth($auth, $req);
91-
break;
95+
break;
9296
}
9397
}
9498

9599
/**
96100
* Form based authentication
97-
*
101+
*
98102
* @param Strong $auth
99103
* @param object $req
100104
*/
101-
private function formauth(Strong $auth, $req) {
105+
private function formauth(Strong $auth, $req)
106+
{
102107
$app = $this->app;
103108
$config = $this->config;
104-
105109
$this->app->hook('slim.before.router', function () use ($app, $auth, $req, $config) {
106110
$secured_urls = isset($config['security.urls']) ? $config['security.urls'] : array();
107111
foreach ($secured_urls as $surl) {
@@ -120,30 +124,31 @@ private function formauth(Strong $auth, $req) {
120124
}
121125
}
122126
});
123-
127+
124128
$this->next->call();
125129
}
126130

127131
/**
128132
* HTTPAuth based authentication
129-
*
133+
*
130134
* This method will check the HTTP request headers for previous authentication. If
131135
* the request has already authenticated, the next middleware is called. Otherwise,
132136
* a 401 Authentication Required response is returned to the client.
133-
*
134-
* @param Strong $auth
137+
*
138+
* @param Strong $auth
135139
* @param object $req
136140
*/
137-
private function httpauth(Strong $auth, $req) {
141+
private function httpauth(Strong $auth, $req)
142+
{
138143
$res = $this->app->response();
139144
$authUser = $req->headers('PHP_AUTH_USER');
140145
$authPass = $req->headers('PHP_AUTH_PW');
141146

142-
if ( $authUser && $authPass && $auth->login($authUser, $authPass) ) {
147+
if ($authUser && $authPass && $auth->login($authUser, $authPass)) {
143148
$this->next->call();
144149
} else {
145150
$res->status(401);
146151
$res->header('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realm));
147152
}
148153
}
149-
}
154+
}

0 commit comments

Comments
 (0)