Skip to content

Commit

Permalink
Turning off HTTP basic auth, better json decode
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Chmarzynski committed Jul 15, 2013
1 parent 3937669 commit 0678822
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions lib/Redmine/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class Client
*/
private $checkSslCertificate = false;

/**
*
* Flag to determine authentication method
*
* @var boolean
*/
private $useHttpAuth = true;

/**
* @var array APIs
*/
Expand Down Expand Up @@ -157,8 +165,34 @@ public function get($path)
if (false === $json = $this->runRequest($path, 'GET')) {
return false;
}
return $this->decode($json);
}

/**
* Decodes json response
* @param unknown $json
*/
public function decode($string)
{

$decoded = json_decode( $string, true );

if (null == $decoded) {
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
if (json_last_error() == JSON_ERROR_NONE) {
return $string;
} else {
return $json_errors[json_last_error()];
}
} else {
return $decoded;
}

return json_decode($json, true);
}

/**
Expand Down Expand Up @@ -202,6 +236,11 @@ public function setCheckSslCertificate($check = false)
$this->checkSslCertificate = $check;
}

public function setUseHttpAuth($use = true)
{
$this->useHttpAuth = $use;
}

/**
* Set the port of the connection
* @param int $port
Expand Down Expand Up @@ -252,7 +291,7 @@ private function runRequest($path, $method = 'GET', $data = '')
$this->getPort($this->url.$path);

$curl = curl_init();
if (isset($this->apikey)) {
if (isset($this->apikey) && $this->useHttpAuth) {
curl_setopt($curl, CURLOPT_USERPWD, $this->apikey.':'.rand(100000, 199999) );
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
Expand All @@ -269,17 +308,20 @@ private function runRequest($path, $method = 'GET', $data = '')
if ('xml' === substr($tmp['path'], -3)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml',
'X-Redmine-API-Key: '.$this->apikey
));
}
if ('json' === substr($tmp['path'], -4)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Redmine-API-Key: '.$this->apikey
));
}

if ('/uploads.json' === $path || '/uploads.xml' === $path) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/octet-stream',
'X-Redmine-API-Key: '.$this->apikey
));
}

Expand Down

0 comments on commit 0678822

Please sign in to comment.