Skip to content

Commit 2ac6b97

Browse files
author
=
committed
Merge branch 'develop'
2 parents cb0e3b1 + 40183d4 commit 2ac6b97

19 files changed

+476
-241
lines changed
Lines changed: 19 additions & 12 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',
@@ -84,9 +88,6 @@ public function __construct( $settings = array() ) {
8488

8589
//Remove trailing slash from log path
8690
$this->settings['path'] = rtrim($this->settings['path'], DIRECTORY_SEPARATOR);
87-
88-
//Open resource handle to log file
89-
$this->resource = fopen($this->settings['path'] . DIRECTORY_SEPARATOR . date($this->settings['name_format']), 'a');
9091
}
9192

9293
/**
@@ -96,20 +97,21 @@ public function __construct( $settings = array() ) {
9697
* @param int $level
9798
* @return void
9899
*/
99-
public function write( $object, $level ) {
100+
public function write($object, $level)
101+
{
100102
//Determine label
101103
$label = 'DEBUG';
102-
switch ( $level ) {
103-
case 0:
104+
switch ($level) {
105+
case \Slim\Log::FATAL:
104106
$label = 'FATAL';
105107
break;
106-
case 1:
108+
case \Slim\Log::ERROR:
107109
$label = 'ERROR';
108110
break;
109-
case 2:
111+
case \Slim\Log::WARN:
110112
$label = 'WARN';
111113
break;
112-
case 3:
114+
case \Slim\Log::INFO:
113115
$label = 'INFO';
114116
break;
115117
}
@@ -125,7 +127,12 @@ public function write( $object, $level ) {
125127
(string)$object
126128
), $this->settings['message_format']);
127129

130+
//Open resource handle to log file
131+
if (!$this->resource) {
132+
$this->resource = fopen($this->settings['path'] . DIRECTORY_SEPARATOR . date($this->settings['name_format']), 'a');
133+
}
134+
128135
//Output to resource
129136
fwrite($this->resource, $message . PHP_EOL);
130137
}
131-
}
138+
}

Middleware/CsrfGuard.php

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

34
/**
45
* CsrfGuard
56
*
67
* This middleware provides protection from CSRF attacks
7-
88
* USAGE
99
*
1010
* // Adding middleware
@@ -17,7 +17,8 @@
1717
* @author Mikhail Osher, https://github.com/miraage
1818
* @version 1.0
1919
*/
20-
class CsrfGuard extends Slim_Middleware {
20+
class CsrfGuard extends \Slim\Middleware
21+
{
2122
/**
2223
* Request key
2324
*
@@ -30,9 +31,10 @@ class CsrfGuard extends Slim_Middleware {
3031
*
3132
* @param string $key Request key
3233
*/
33-
public function __construct( $key = 'csrf_token' ) {
34+
public function __construct($key = 'csrf_token')
35+
{
3436
// Validate key (i won't use htmlspecialchars)
35-
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)) {
3638
throw new OutOfBoundsException('Invalid key' . $key);
3739
}
3840

@@ -42,7 +44,8 @@ public function __construct( $key = 'csrf_token' ) {
4244
/**
4345
* Call middleware
4446
*/
45-
public function call() {
47+
public function call()
48+
{
4649
// Attach as hook
4750
$this->app->hook('slim.before', array($this, 'check'));
4851

@@ -53,25 +56,30 @@ public function call() {
5356
/**
5457
* Check token
5558
*/
56-
public function check() {
59+
public function check()
60+
{
5761
// Create token
58-
$env = $this->app->environment();
59-
$token = sha1($env['REMOTE_ADDR'] . '|' . $env['USER_AGENT']);
62+
if (session_id() !== "") {
63+
if (!isset($_SESSION[$this->key])) {
64+
$_SESSION[$this->key] = sha1(serialize($_SERVER) . rand(0, 0xffffffff));
65+
}
66+
} else {
67+
throw new Exception( "Session are required to use CSRF Guard" );
68+
}
69+
$token = $_SESSION[$this->key];
6070

6171
// Validate
62-
if ( in_array($this->app->request()->getMethod(), array('POST', 'PUT', 'DELETE')) ) {
72+
if (in_array($this->app->request()->getMethod(), array('POST', 'PUT', 'DELETE'))) {
6373
$usertoken = $this->app->request()->post($this->key);
64-
if ( $token !== $usertoken ) {
74+
if ($token !== $usertoken) {
6575
$this->app->halt(400, 'Missing token');
6676
}
6777
}
6878

6979
// Assign to view
70-
$this->app->view()->setData(array(
80+
$this->app->view()->appendData(array(
7181
'csrf_key' => $this->key,
7282
'csrf_token' => $token,
7383
));
7484
}
7585
}
76-
77-
?>

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+
}

0 commit comments

Comments
 (0)