Skip to content

Commit 8e96f26

Browse files
authored
Update DiscourseAPI.php
Add possibility to define HTTP-Auth credentials Add getUserinfoByName, watchTopic and others
1 parent ddd1c43 commit 8e96f26

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

lib/DiscourseAPI.php

+50-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ class DiscourseAPI
1919
private $_protocol = 'http';
2020
private $_apiKey = null;
2121
private $_dcHostname = null;
22+
private $_httpAuthName = '';
23+
private $_httpAuthPass = '';
2224

23-
function __construct($dcHostname, $apiKey = null, $protocol='http')
25+
function __construct($dcHostname, $apiKey = null, $protocol='http', $httpAuthName='', $httpAuthPass='')
2426
{
2527
$this->_dcHostname = $dcHostname;
2628
$this->_apiKey = $apiKey;
2729
$this->_protocol=$protocol;
30+
$this->_httpAuthName = $httpAuthName;
31+
$this->_httpAuthPass = $httpAuthPass;
2832
}
2933

3034
private function _getRequest($reqString, $paramArray = null, $apiUser = 'system')
@@ -42,6 +46,11 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'
4246
$reqString,
4347
http_build_query($paramArray)
4448
);
49+
50+
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
51+
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass);
52+
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
53+
}
4554

4655
curl_setopt($ch, CURLOPT_URL, $url);
4756
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@@ -82,6 +91,12 @@ private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $
8291
if ($putMethod) {
8392
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
8493
}
94+
95+
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
96+
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass);
97+
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
98+
}
99+
85100
$body = curl_exec($ch);
86101
$rc = curl_getinfo($ch, CURLINFO_HTTP_CODE);
87102
curl_close($ch);
@@ -196,6 +211,19 @@ function activateUser($userId)
196211
return $this->_putRequest("/admin/users/{$userId}/activate", array());
197212
}
198213

214+
/**
215+
* suspendUser
216+
*
217+
* @param integer $userId id of user to suspend
218+
*
219+
* @return mixed HTTP return code
220+
*/
221+
222+
function suspendUser($userId)
223+
{
224+
return $this->_putRequest("/admin/users/{$userId}/suspend", array());
225+
}
226+
199227
/**
200228
* getUsernameByEmail
201229
*
@@ -287,6 +315,22 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
287315
);
288316
return $this->_postRequest('/posts', $params, $userName);
289317
}
318+
319+
/**
320+
* watchTopic
321+
*
322+
* watch Topic. If username is given, API-Key must be
323+
* general API key. Otherwise it will fail.
324+
* If no username is given, topic will be watched with
325+
* the system API username
326+
*/
327+
function watchTopic($topicId, $userName = 'system')
328+
{
329+
$params = array(
330+
'notification_level' => '3'
331+
);
332+
return $this->_postRequest("/t/{$topicId}/notifications.json", $params, $userName);
333+
}
290334

291335
/**
292336
* createPost
@@ -336,5 +380,10 @@ function logoutByEmail($email)
336380
$params = array('username_or_email' => $email);
337381
return $this->_postRequest('/admin/users/' . $user_id . '/log_out', $params);
338382
}
383+
384+
function getUserinfoByName($username)
385+
{
386+
return $this->_getRequest("/users/{$username}.json");
387+
}
339388
}
340389

0 commit comments

Comments
 (0)