Skip to content

Commit 6f2823b

Browse files
committed
refs kbsali#2 allow to turn on ssl certificate check and to set the port number
1 parent bd7fd93 commit 6f2823b

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ $client->api('issue')->create(array(
7474

7575
see `example.php`
7676

77-
### Contributors
77+
### Thanks!
7878

7979
- Thanks to [Thomas Spycher](http://tspycher.com/2011/03/using-the-redmine-api-with-php/) for the 1st version of the class.
8080
- Thanks to [Thibault Duplessis aka. ornicar](https://github.com/ornicar) for the php-github-api library, great source of inspiration!

example.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
$client = new Redmine\Client('http://redmine.example.com', '1234567890abcdfgh');
66

7+
// ----------------------------
8+
// [OPTIONAL] if you want to check
9+
// the servers' SSL certificate on Curl call
10+
$client->setCheckSslCertificate(true);
11+
12+
// ----------------------------
13+
// [OPTIONAL] set the port
14+
// (it will try to guess it from the url)
15+
$client->setPort(8080);
16+
717
// ----------------------------
818
// Trackers
919
$client->api('tracker')->all();

lib/Redmine/Client.php

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class Client
1717
'https' => 443,
1818
);
1919

20+
/**
21+
* @var int
22+
*/
23+
private $port;
24+
2025
/**
2126
* @var string
2227
*/
@@ -27,6 +32,11 @@ class Client
2732
*/
2833
private $apikey;
2934

35+
/**
36+
* @var boolean
37+
*/
38+
private $checkSslCertificate = false;
39+
3040
/**
3141
* @param string $url
3242
* @param string $apikey
@@ -166,6 +176,53 @@ public function delete($path)
166176
return $this->runRequest($path, 'DELETE');
167177
}
168178

179+
/**
180+
* Turns on/off ssl certificate check
181+
* @param boolean $check
182+
*/
183+
public function setCheckSslCertificate($check = false)
184+
{
185+
$this->checkSslCertificate = $check;
186+
}
187+
188+
/**
189+
* Set the port of the connection
190+
* @param int $port
191+
*/
192+
public function setPort($port = null)
193+
{
194+
if (null !== $port) {
195+
$this->port = (int) $port;
196+
}
197+
}
198+
199+
/**
200+
* Returns the port of the current connection,
201+
* if not set, it will try to guess the port
202+
* from the given $urlPath
203+
* @param string $urlPath the url called
204+
* @return int
205+
*/
206+
public function getPort($urlPath = null)
207+
{
208+
if (null === $urlPath) {
209+
return $this->port;
210+
}
211+
if (null !== $this->port) {
212+
return $this->port;
213+
}
214+
$tmp = parse_url($urlPath);
215+
216+
if (isset($tmp['port'])) {
217+
$this->setPort($tmp['port']);
218+
219+
return $this->port;
220+
}
221+
$this->setPort(self::$defaultPorts[$tmp['scheme']]);
222+
223+
return $this->port;
224+
}
225+
169226
/**
170227
* @param string $path
171228
* @param string $method
@@ -175,13 +232,7 @@ public function delete($path)
175232
*/
176233
private function runRequest($path, $method = 'GET', $data = '')
177234
{
178-
$tmp = parse_url($this->url.$path);
179-
180-
if (isset($tmp['port'])) {
181-
$port = $tmp['port'];
182-
} else {
183-
$port = self::$defaultPorts[$tmp['scheme']];
184-
}
235+
$this->getPort($this->url.$path);
185236

186237
$curl = curl_init();
187238
if (isset($this->apikey)) {
@@ -192,11 +243,12 @@ private function runRequest($path, $method = 'GET', $data = '')
192243
curl_setopt($curl, CURLOPT_VERBOSE, 0);
193244
curl_setopt($curl, CURLOPT_HEADER, 0);
194245
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
195-
curl_setopt($curl, CURLOPT_PORT , $port);
196-
if (80 !== $port) {
197-
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
246+
curl_setopt($curl, CURLOPT_PORT , $this->port);
247+
if (80 !== $this->port) {
248+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->checkSslCertificate);
198249
}
199250

251+
$tmp = parse_url($this->url.$path);
200252
if ('xml' === substr($tmp['path'], -3)) {
201253
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
202254
'Content-Type: text/xml',

0 commit comments

Comments
 (0)