Skip to content

Commit

Permalink
enabling regular login/password authentification
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKling committed Feb 28, 2014
1 parent b0b3621 commit 639c891
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions lib/Redmine/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Client
*/
private $apikey;

/**
* @var string or null
*/
private $pass;

/**
* @var boolean
*/
Expand Down Expand Up @@ -70,11 +75,15 @@ class Client
/**
* @param string $url
* @param string $apikey
* @param string $pass (string or null)
*
* Usage: apikey can be auth key or username. Password needs to be set if username is given.
*/
public function __construct($url, $apikey)
public function __construct($url, $apikey, $pass = null)
{
$this->url = $url;
$this->apikey = $apikey;
$this->pass = $pass;
}

/**
Expand Down Expand Up @@ -318,8 +327,13 @@ private function runRequest($path, $method = 'GET', $data = '')

$curl = curl_init();
if (isset($this->apikey) && $this->useHttpAuth) {
curl_setopt($curl, CURLOPT_USERPWD, $this->apikey.':'.rand(100000, 199999) );
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
if ($this->pass) {
curl_setopt($curl, CURLOPT_USERPWD, $this->apikey.':'.$this->pass );
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
} else {
curl_setopt($curl, CURLOPT_USERPWD, $this->apikey.':'.rand(100000, 199999) );
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
}
curl_setopt($curl, CURLOPT_URL, $this->url.$path);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
Expand All @@ -332,24 +346,22 @@ private function runRequest($path, $method = 'GET', $data = '')
}

$tmp = parse_url($this->url.$path);
$httpHeader = array();
if ('xml' === substr($tmp['path'], -3)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml',
'X-Redmine-API-Key: '.$this->apikey
));
$httpHeader[] = 'Content-Type: text/xml';
}
if ('json' === substr($tmp['path'], -4)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Redmine-API-Key: '.$this->apikey
));
$httpHeader[] = 'Content-Type: application/json';
}

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

if (!empty($httpHeader)) {
if (!$this->pass) {
$httpHeader[] = 'X-Redmine-API-Key: '.$this->apikey;
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeader);
}

switch ($method) {
Expand Down

0 comments on commit 639c891

Please sign in to comment.