Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion lib/DiscourseAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ class DiscourseAPI
private $_protocol = 'http';
private $_apiKey = null;
private $_dcHostname = null;
private $_httpAuthName = '';
private $_httpAuthPass = '';

function __construct($dcHostname, $apiKey = null, $protocol='http')
function __construct($dcHostname, $apiKey = null, $protocol='http', $httpAuthName='', $httpAuthPass='')
{
$this->_dcHostname = $dcHostname;
$this->_apiKey = $apiKey;
$this->_protocol=$protocol;
$this->_httpAuthName = $httpAuthName;
$this->_httpAuthPass = $httpAuthPass;
}

private function _getRequest($reqString, $paramArray = null, $apiUser = 'system')
Expand All @@ -42,6 +46,11 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'
$reqString,
http_build_query($paramArray)
);

if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand Down Expand Up @@ -82,6 +91,12 @@ private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $
if ($putMethod) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
}

if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}

$body = curl_exec($ch);
$rc = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Expand Down Expand Up @@ -196,6 +211,19 @@ function activateUser($userId)
return $this->_putRequest("/admin/users/{$userId}/activate", array());
}

/**
* suspendUser
*
* @param integer $userId id of user to suspend
*
* @return mixed HTTP return code
*/

function suspendUser($userId)
{
return $this->_putRequest("/admin/users/{$userId}/suspend", array());
}

/**
* getUsernameByEmail
*
Expand Down Expand Up @@ -287,6 +315,22 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
);
return $this->_postRequest('/posts', $params, $userName);
}

/**
* watchTopic
*
* watch Topic. If username is given, API-Key must be
* general API key. Otherwise it will fail.
* If no username is given, topic will be watched with
* the system API username
*/
function watchTopic($topicId, $userName = 'system')
{
$params = array(
'notification_level' => '3'
);
return $this->_postRequest("/t/{$topicId}/notifications.json", $params, $userName);
}

/**
* createPost
Expand Down Expand Up @@ -336,5 +380,10 @@ function logoutByEmail($email)
$params = array('username_or_email' => $email);
return $this->_postRequest('/admin/users/' . $user_id . '/log_out', $params);
}

function getUserinfoByName($username)
{
return $this->_getRequest("/users/{$username}.json");
}
}