Skip to content

Commit

Permalink
Merge pull request #48 from fruux/sabre-cs
Browse files Browse the repository at this point in the history
Integrate sabre/cs
  • Loading branch information
evert committed May 18, 2015
2 parents 4818eca + c0bca1e commit 4eeb0ba
Show file tree
Hide file tree
Showing 27 changed files with 272 additions and 274 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ tests/cov/
# Composer binaries
bin/phpunit
bin/phpcs
bin/php-cs-fixer
bin/sabre-cs-fixer

# Vim
.*.swp
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ before_script:

script:
- ./bin/phpunit --configuration tests/phpunit.xml
- ./bin/phpcs -p --standard=tests/phpcs/ruleset.xml lib/
- ./bin/sabre-cs-fixer fix lib/ --dry-run --diff
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ChangeLog
* #45: Don't send more data than what is promised in the HTTP content-length.
(@dratini0).
* #43: `getCredentials` returns null if incomplete. (@Hywan)
* #48: Now using php-cs-fixer to make our CS consistent (yay!)
* This includes fixes released in version 3.0.5.


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"require-dev" : {
"phpunit/phpunit" : "~4.3",
"squizlabs/php_codesniffer": "~1.5.3"
"sabre/cs" : "~0.0.1"
},
"suggest" : {
"ext-curl" : " to make http requests with the Client class"
Expand Down
34 changes: 17 additions & 17 deletions lib/Auth/AWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ class AWS extends AbstractAuth {
function init() {

$authHeader = $this->request->getHeader('Authorization');
$authHeader = explode(' ',$authHeader);
$authHeader = explode(' ', $authHeader);

if ($authHeader[0]!='AWS' || !isset($authHeader[1])) {
if ($authHeader[0] != 'AWS' || !isset($authHeader[1])) {
$this->errorCode = self::ERR_NOAWSHEADER;
return false;
}

list($this->accessKey,$this->signature) = explode(':',$authHeader[1]);
list($this->accessKey, $this->signature) = explode(':', $authHeader[1]);

return true;

Expand Down Expand Up @@ -91,9 +91,9 @@ function validate($secretKey) {
if ($contentMD5) {
// We need to validate the integrity of the request
$body = $this->request->getBody(true);
$this->request->setBody($body,true);
$this->request->setBody($body, true);

if ($contentMD5!=base64_encode(md5($body,true))) {
if ($contentMD5 != base64_encode(md5($body, true))) {
// content-md5 header did not match md5 signature of body
$this->errorCode = self::ERR_MD5CHECKSUMWRONG;
return false;
Expand Down Expand Up @@ -141,7 +141,7 @@ function validate($secretKey) {
*/
function requireLogin() {

$this->response->addHeader('WWW-Authenticate','AWS');
$this->response->addHeader('WWW-Authenticate', 'AWS');
$this->response->setStatus(401);

}
Expand Down Expand Up @@ -191,15 +191,15 @@ protected function getAmzHeaders() {
$amzHeaders = [];
$headers = $this->request->getHeaders();
foreach($headers as $headerName => $headerValue) {
if (strpos(strtolower($headerName),'x-amz-')===0) {
$amzHeaders[strtolower($headerName)] = str_replace( ["\r\n"], [' '],$headerValue[0]) . "\n";
if (strpos(strtolower($headerName), 'x-amz-') === 0) {
$amzHeaders[strtolower($headerName)] = str_replace(["\r\n"], [' '], $headerValue[0]) . "\n";
}
}
ksort($amzHeaders);

$headerStr = '';
foreach($amzHeaders as $h=>$v) {
$headerStr.=$h.':'.$v;
foreach($amzHeaders as $h => $v) {
$headerStr .= $h . ':' . $v;
}

return $headerStr;
Expand All @@ -219,14 +219,14 @@ private function hmacsha1($key, $message) {
return hash_hmac('sha1', $message, $key, true);
}

$blocksize=64;
if (strlen($key)>$blocksize) {
$key=pack('H*', sha1($key));
$blocksize = 64;
if (strlen($key) > $blocksize) {
$key = pack('H*', sha1($key));
}
$key=str_pad($key,$blocksize,chr(0x00));
$ipad=str_repeat(chr(0x36),$blocksize);
$opad=str_repeat(chr(0x5c),$blocksize);
$hmac = pack('H*',sha1(($key^$opad).pack('H*',sha1(($key^$ipad).$message))));
$key = str_pad($key, $blocksize, chr(0x00));
$ipad = str_repeat(chr(0x36), $blocksize);
$opad = str_repeat(chr(0x5c), $blocksize);
$hmac = pack('H*', sha1(($key ^ $opad) . pack('H*', sha1(($key ^ $ipad) . $message))));
return $hmac;

}
Expand Down
5 changes: 2 additions & 3 deletions lib/Auth/AbstractAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Sabre\HTTP\Auth;

use
Sabre\HTTP\RequestInterface,
Sabre\HTTP\ResponseInterface;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;

/**
* HTTP Authentication base class.
Expand Down
6 changes: 3 additions & 3 deletions lib/Auth/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ function getCredentials() {
return null;
}

if (strtolower(substr($auth,0,6))!=='basic ') {
if (strtolower(substr($auth, 0, 6)) !== 'basic ') {
return null;
}

$credentials = explode(':',base64_decode(substr($auth, 6)), 2);
$credentials = explode(':', base64_decode(substr($auth, 6)), 2);

if (2 !== count($credentials)) {
return null;
Expand All @@ -55,7 +55,7 @@ function getCredentials() {
*/
function requireLogin() {

$this->response->addHeader('WWW-Authenticate','Basic realm="' . $this->realm . '"');
$this->response->addHeader('WWW-Authenticate', 'Basic realm="' . $this->realm . '"');
$this->response->setStatus(401);

}
Expand Down
13 changes: 6 additions & 7 deletions lib/Auth/Digest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Sabre\HTTP\Auth;

use
Sabre\HTTP\RequestInterface,
Sabre\HTTP\ResponseInterface;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;

/**
* HTTP Digest Authentication handler
Expand Down Expand Up @@ -138,7 +137,7 @@ protected function validate() {

$A2 = $this->request->getMethod() . ':' . $this->digestParts['uri'];

if ($this->digestParts['qop']=='auth-int') {
if ($this->digestParts['qop'] == 'auth-int') {
// Making sure we support this qop value
if (!($this->qop & self::QOP_AUTHINT)) return false;
// We need to add an md5 of the entire request body to the A2 part of the hash
Expand All @@ -155,7 +154,7 @@ protected function validate() {

$validResponse = md5("{$this->A1}:{$this->digestParts['nonce']}:{$this->digestParts['nc']}:{$this->digestParts['cnonce']}:{$this->digestParts['qop']}:{$A2}");

return $this->digestParts['response']==$validResponse;
return $this->digestParts['response'] == $validResponse;


}
Expand All @@ -182,7 +181,7 @@ function requireLogin() {
break;
}

$this->response->addHeader('WWW-Authenticate','Digest realm="' . $this->realm . '",qop="'.$qop.'",nonce="' . $this->nonce . '",opaque="' . $this->opaque . '"');
$this->response->addHeader('WWW-Authenticate', 'Digest realm="' . $this->realm . '",qop="' . $qop . '",nonce="' . $this->nonce . '",opaque="' . $this->opaque . '"');
$this->response->setStatus(401);

}
Expand Down Expand Up @@ -215,7 +214,7 @@ function getDigest() {
protected function parseDigest($digest) {

// protect against missing data
$needed_parts = ['nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1];
$needed_parts = ['nonce' => 1, 'nc' => 1, 'cnonce' => 1, 'qop' => 1, 'username' => 1, 'uri' => 1, 'response' => 1];
$data = [];

preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $digest, $matches, PREG_SET_ORDER);
Expand Down
16 changes: 8 additions & 8 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function __construct() {

$this->curlSettings = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => false,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => false,
];

}
Expand All @@ -100,7 +100,7 @@ function send(RequestInterface $request) {

$response = $this->doRequest($request);

$code = (int) $response->getStatus();
$code = (int)$response->getStatus();

// We are doing in-PHP redirects, because curl's
// FOLLOW_LOCATION throws errors when PHP is configured with
Expand Down Expand Up @@ -428,7 +428,7 @@ protected function createCurlSettingsArray(RequestInterface $request) {
}

$nHeaders = [];
foreach($request->getHeaders() as $key=>$values) {
foreach($request->getHeaders() as $key => $values) {

foreach($values as $value) {
$nHeaders[] = $key . ': ' . $value;
Expand Down Expand Up @@ -483,8 +483,8 @@ protected function parseCurlResult($response, $curlHandle) {

if ($curlErrNo) {
return [
'status' => self::STATUS_CURLERROR,
'curl_errno' => $curlErrNo,
'status' => self::STATUS_CURLERROR,
'curl_errno' => $curlErrNo,
'curl_errmsg' => $curlErrMsg,
];
}
Expand All @@ -503,7 +503,7 @@ protected function parseCurlResult($response, $curlHandle) {
$headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n"));

// We only care about the last set of headers
$headerBlob = $headerBlob[count($headerBlob)-1];
$headerBlob = $headerBlob[count($headerBlob) - 1];

// Splitting headers
$headerBlob = explode("\r\n", $headerBlob);
Expand All @@ -513,7 +513,7 @@ protected function parseCurlResult($response, $curlHandle) {

foreach($headerBlob as $header) {
$parts = explode(':', $header, 2);
if (count($parts)==2) {
if (count($parts) == 2) {
$response->addHeader(trim($parts[0]), trim($parts[1]));
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ function setUrl($url) {
function getQueryParameters() {

$url = $this->getUrl();
if (($index = strpos($url,'?'))===false) {
if (($index = strpos($url, '?')) === false) {
return [];
} else {
parse_str(substr($url, $index+1), $queryParams);
parse_str(substr($url, $index + 1), $queryParams);
return $queryParams;
}

Expand Down Expand Up @@ -191,21 +191,21 @@ function getBaseUrl() {
function getPath() {

// Removing duplicated slashes.
$uri = str_replace('//','/',$this->getUrl());
$uri = str_replace('//', '/', $this->getUrl());

$uri = Uri\normalize($uri);
$baseUri = Uri\normalize($this->getBaseUrl());

if (strpos($uri,$baseUri)===0) {
if (strpos($uri, $baseUri) === 0) {

// We're not interested in the query part (everything after the ?).
list($uri) = explode('?', $uri);
return trim(URLUtil::decodePath(substr($uri,strlen($baseUri))),'/');
return trim(URLUtil::decodePath(substr($uri, strlen($baseUri))), '/');

}
// A special case, if the baseUri was accessed without a trailing
// slash, we'll accept it as well.
elseif ($uri.'/' === $baseUri) {
elseif ($uri . '/' === $baseUri) {

return '';

Expand Down Expand Up @@ -297,10 +297,10 @@ function __toString() {

$out = $this->getMethod() . ' ' . $this->getUrl() . ' HTTP/' . $this->getHTTPVersion() . "\r\n";

foreach($this->getHeaders() as $key=>$value) {
foreach($this->getHeaders() as $key => $value) {
foreach($value as $v) {
if ($key==='Authorization') {
list($v) = explode(' ', $v,2);
if ($key === 'Authorization') {
list($v) = explode(' ', $v, 2);
$v .= ' REDACTED';
}
$out .= $key . ": " . $v . "\r\n";
Expand Down
10 changes: 5 additions & 5 deletions lib/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function setStatus($status) {
if (ctype_digit($status) || is_int($status)) {

$statusCode = $status;
$statusText = isset(self::$statusCodes[$status])?self::$statusCodes[$status]:'Unknown';
$statusText = isset(self::$statusCodes[$status]) ? self::$statusCodes[$status] : 'Unknown';

} else {
list(
Expand All @@ -180,13 +180,13 @@ function setStatus($status) {
function __toString() {

$str = 'HTTP/' . $this->httpVersion . ' ' . $this->getStatus() . ' ' . $this->getStatusText() . "\r\n";
foreach($this->getHeaders() as $key=>$value) {
foreach($this->getHeaders() as $key => $value) {
foreach($value as $v) {
$str.= $key . ": " . $v . "\r\n";
$str .= $key . ": " . $v . "\r\n";
}
}
$str.="\r\n";
$str.=$this->getBodyAsString();
$str .= "\r\n";
$str .= $this->getBodyAsString();
return $str;

}
Expand Down
16 changes: 8 additions & 8 deletions lib/Sapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Sapi {
static function getRequest() {

$r = self::createFromServerArray($_SERVER);
$r->setBody(fopen('php://input','r'));
$r->setBody(fopen('php://input', 'r'));
$r->setPostData($_POST);
return $r;

Expand All @@ -56,9 +56,9 @@ static function getRequest() {
static function sendResponse(ResponseInterface $response) {

header('HTTP/' . $response->getHttpVersion() . ' ' . $response->getStatus() . ' ' . $response->getStatusText());
foreach($response->getHeaders() as $key=>$value) {
foreach($response->getHeaders() as $key => $value) {

foreach($value as $k=>$v) {
foreach($value as $k => $v) {
if ($k === 0) {
header($key . ': ' . $v);
} else {
Expand Down Expand Up @@ -102,12 +102,12 @@ static function createFromServerArray(array $serverArray) {
$protocol = 'http';
$hostName = 'localhost';

foreach($serverArray as $key=>$value) {
foreach($serverArray as $key => $value) {

switch($key) {

case 'SERVER_PROTOCOL' :
if ($value==='HTTP/1.0') {
if ($value === 'HTTP/1.0') {
$httpVersion = '1.0';
}
break;
Expand Down Expand Up @@ -151,17 +151,17 @@ static function createFromServerArray(array $serverArray) {
break;

case 'HTTPS' :
if (!empty($value) && $value!=='off') {
if (!empty($value) && $value !== 'off') {
$protocol = 'https';
}
break;

default :
if (substr($key,0,5)==='HTTP_') {
if (substr($key, 0, 5) === 'HTTP_') {
// It's a HTTP header

// Normalizing it to be prettier
$header = strtolower(substr($key,5));
$header = strtolower(substr($key, 5));

// Transforming dashes into spaces, and uppercasing
// every first letter.
Expand Down
Loading

0 comments on commit 4eeb0ba

Please sign in to comment.