Skip to content

Commit 639c891

Browse files
committed
enabling regular login/password authentification
1 parent b0b3621 commit 639c891

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

lib/Redmine/Client.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class Client
3434
*/
3535
private $apikey;
3636

37+
/**
38+
* @var string or null
39+
*/
40+
private $pass;
41+
3742
/**
3843
* @var boolean
3944
*/
@@ -70,11 +75,15 @@ class Client
7075
/**
7176
* @param string $url
7277
* @param string $apikey
78+
* @param string $pass (string or null)
79+
*
80+
* Usage: apikey can be auth key or username. Password needs to be set if username is given.
7381
*/
74-
public function __construct($url, $apikey)
82+
public function __construct($url, $apikey, $pass = null)
7583
{
7684
$this->url = $url;
7785
$this->apikey = $apikey;
86+
$this->pass = $pass;
7887
}
7988

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

319328
$curl = curl_init();
320329
if (isset($this->apikey) && $this->useHttpAuth) {
321-
curl_setopt($curl, CURLOPT_USERPWD, $this->apikey.':'.rand(100000, 199999) );
322-
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
330+
if ($this->pass) {
331+
curl_setopt($curl, CURLOPT_USERPWD, $this->apikey.':'.$this->pass );
332+
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
333+
} else {
334+
curl_setopt($curl, CURLOPT_USERPWD, $this->apikey.':'.rand(100000, 199999) );
335+
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
336+
}
323337
}
324338
curl_setopt($curl, CURLOPT_URL, $this->url.$path);
325339
curl_setopt($curl, CURLOPT_VERBOSE, 0);
@@ -332,24 +346,22 @@ private function runRequest($path, $method = 'GET', $data = '')
332346
}
333347

334348
$tmp = parse_url($this->url.$path);
349+
$httpHeader = array();
335350
if ('xml' === substr($tmp['path'], -3)) {
336-
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
337-
'Content-Type: text/xml',
338-
'X-Redmine-API-Key: '.$this->apikey
339-
));
351+
$httpHeader[] = 'Content-Type: text/xml';
340352
}
341353
if ('json' === substr($tmp['path'], -4)) {
342-
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
343-
'Content-Type: application/json',
344-
'X-Redmine-API-Key: '.$this->apikey
345-
));
354+
$httpHeader[] = 'Content-Type: application/json';
346355
}
347-
348356
if ('/uploads.json' === $path || '/uploads.xml' === $path) {
349-
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
350-
'Content-Type: application/octet-stream',
351-
'X-Redmine-API-Key: '.$this->apikey
352-
));
357+
$httpHeader[] = 'Content-Type: application/octet-stream';
358+
}
359+
360+
if (!empty($httpHeader)) {
361+
if (!$this->pass) {
362+
$httpHeader[] = 'X-Redmine-API-Key: '.$this->apikey;
363+
}
364+
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeader);
353365
}
354366

355367
switch ($method) {

0 commit comments

Comments
 (0)