-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from saylordotorg/feat/new-discourse-api-2020
Feat/new discourse api 2020
- Loading branch information
Showing
4 changed files
with
105 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
<?php | ||
|
||
/** | ||
* Discourse API client for PHP | ||
* | ||
* This is the Discourse API client for PHP | ||
* This is a very experimental API implementation. | ||
* | ||
* @category DiscourseAPI | ||
* @package DiscourseAPI | ||
* @author Original author DiscourseHosting <[email protected]> | ||
* @copyright 2013, DiscourseHosting.com | ||
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2 | ||
* @link https://github.com/discoursehosting/discourse-api-php | ||
*/ | ||
* Discourse API client for PHP | ||
* | ||
* This is the Discourse API client for PHP | ||
* This is a very experimental API implementation. | ||
* | ||
* @category DiscourseAPI | ||
* @package DiscourseAPI | ||
* @author Original author DiscourseHosting <[email protected]> | ||
* @copyright 2013, DiscourseHosting.com | ||
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2 | ||
* @link https://github.com/discoursehosting/discourse-api-php | ||
*/ | ||
|
||
class DiscourseAPI | ||
{ | ||
|
@@ -22,11 +22,11 @@ class DiscourseAPI | |
private $_httpAuthName = ''; | ||
private $_httpAuthPass = ''; | ||
|
||
function __construct($dcHostname, $apiKey = null, $protocol='http', $httpAuthName='', $httpAuthPass='') | ||
function __construct($dcHostname, $apiKey = null, $protocol = 'http', $httpAuthName = '', $httpAuthPass = '') | ||
{ | ||
$this->_dcHostname = $dcHostname; | ||
$this->_apiKey = $apiKey; | ||
$this->_protocol=$protocol; | ||
$this->_protocol = $protocol; | ||
$this->_httpAuthName = $httpAuthName; | ||
$this->_httpAuthPass = $httpAuthPass; | ||
} | ||
|
@@ -74,18 +74,21 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system' | |
if ($paramArray == null) { | ||
$paramArray = array(); | ||
} | ||
$paramArray['api_key'] = $this->_apiKey; | ||
$paramArray['api_username'] = $apiUser; | ||
$ch = curl_init(); | ||
$url = sprintf( | ||
'%s://%s%s?%s', | ||
$this->_protocol, | ||
$this->_dcHostname, | ||
$reqString, | ||
$this->_protocol, | ||
$this->_dcHostname, | ||
$reqString, | ||
http_build_query($paramArray) | ||
); | ||
|
||
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) { | ||
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | ||
"Api-Key: " . $this->_apiKey, | ||
"Api-Username: $apiUser" | ||
]); | ||
|
||
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) { | ||
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass); | ||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | ||
} | ||
|
@@ -109,28 +112,34 @@ private function _putRequest($reqString, $paramArray, $apiUser = 'system') | |
|
||
private function _postRequest($reqString, $paramArray, $apiUser = 'system') | ||
{ | ||
|
||
return $this->_putpostRequest($reqString, $paramArray, $apiUser, false); | ||
} | ||
|
||
private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $putMethod = false) | ||
{ | ||
|
||
$ch = curl_init(); | ||
$url = sprintf( | ||
'%s://%s%s?api_key=%s&api_username=%s', | ||
$this->_protocol, | ||
$this->_dcHostname, | ||
$reqString, | ||
$this->_apiKey, | ||
$apiUser | ||
'%s://%s%s', | ||
$this->_protocol, | ||
$this->_dcHostname, | ||
$reqString | ||
); | ||
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | ||
"Api-Key: " . $this->_apiKey, | ||
"Api-Username: $apiUser" | ||
]); | ||
|
||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($paramArray)); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
if ($putMethod) { | ||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | ||
} | ||
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) { | ||
|
||
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) { | ||
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass); | ||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | ||
} | ||
|
@@ -161,8 +170,8 @@ function group($groupname, $usernames = array()) | |
return false; | ||
} | ||
|
||
foreach($obj->apiresult as $group) { | ||
if($group->name === $groupname) { | ||
foreach ($obj->apiresult as $group) { | ||
if ($group->name === $groupname) { | ||
$groupId = $group->id; | ||
break; | ||
} | ||
|
@@ -176,7 +185,7 @@ function group($groupname, $usernames = array()) | |
) | ||
); | ||
|
||
if($groupId) { | ||
if ($groupId) { | ||
return $this->_putRequest('/admin/groups/' . $groupId, $params); | ||
} else { | ||
return $this->_postRequest('/admin/groups', $params); | ||
|
@@ -291,19 +300,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 | ||
*/ | ||
/** | ||
* 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 | ||
* | ||
|
@@ -314,19 +323,20 @@ function suspendUser($userId) | |
|
||
function getUsernameByEmail($email) | ||
{ | ||
$users = $this->_getRequest('/admin/users/list/active.json', | ||
[ 'filter' => $email, 'show_emails' => 'true' ] | ||
$users = $this->_getRequest( | ||
'/admin/users/list/active.json', | ||
['filter' => $email, 'show_emails' => 'true'] | ||
); | ||
foreach($users->apiresult as $user) { | ||
if($user->email === $email) { | ||
foreach ($users->apiresult as $user) { | ||
if ($user->email === $email) { | ||
return $user->username; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
/** | ||
* getUserByUsername | ||
* | ||
* @param string $userName username of user | ||
|
@@ -338,18 +348,18 @@ function getUserByUsername($userName) | |
{ | ||
return $this->_getRequest("/users/{$userName}.json"); | ||
} | ||
|
||
/** | ||
* getUserByExternalID | ||
* | ||
* @param string $externalID external id of sso user | ||
* | ||
* @return mixed HTTP return code and API return object | ||
*/ | ||
function getUserByExternalID($externalID) | ||
{ | ||
return $this->_getRequest("/users/by-external/{$externalID}.json"); | ||
} | ||
* getUserByExternalID | ||
* | ||
* @param string $externalID external id of sso user | ||
* | ||
* @return mixed HTTP return code and API return object | ||
*/ | ||
function getUserByExternalID($externalID) | ||
{ | ||
return $this->_getRequest("/users/by-external/{$externalID}.json"); | ||
} | ||
|
||
/** | ||
* createCategory | ||
|
@@ -372,6 +382,18 @@ function createCategory($categoryName, $color, $textColor = '000000', $userName | |
return $this->_postRequest('/categories', $params, $userName); | ||
} | ||
|
||
/** | ||
* getTopic | ||
* | ||
* @param string $topicId id of topic | ||
* | ||
* @return mixed HTTP return code and API return object | ||
*/ | ||
function getTopic($topicID) | ||
{ | ||
return $this->_getRequest('/t/' . $topicID . '.json'); | ||
} | ||
|
||
/** | ||
* createTopic | ||
* | ||
|
@@ -383,8 +405,7 @@ function createCategory($categoryName, $color, $textColor = '000000', $userName | |
* | ||
* @return mixed HTTP return code and API return object | ||
*/ | ||
|
||
function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId = 0) | ||
function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId = 0) | ||
{ | ||
$params = array( | ||
'title' => $topicTitle, | ||
|
@@ -395,7 +416,7 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId | |
); | ||
return $this->_postRequest('/posts', $params, $userName); | ||
} | ||
|
||
/** | ||
* watchTopic | ||
* | ||
|
@@ -404,26 +425,27 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId | |
* If no username is given, topic will be watched with | ||
* the system API username | ||
*/ | ||
function watchTopic($topicId, $userName = 'system') | ||
{ | ||
function watchTopic($topicId, $userName = 'system') | ||
{ | ||
$params = array( | ||
'notification_level' => '3' | ||
'notification_level' => '3' | ||
); | ||
return $this->_postRequest("/t/{$topicId}/notifications.json", $params, $userName); | ||
} | ||
} | ||
|
||
/** | ||
* createPost | ||
* | ||
* NOT WORKING YET | ||
* @param string $bodyText body text of topic post | ||
* @param string $topicId topic id - must me a string not array | ||
* @param string $userName user to create topic as | ||
* | ||
* @return mixed HTTP return code and API return object | ||
*/ | ||
|
||
function createPost($bodyText, $topicId, $categoryId, $userName) | ||
function createPost($bodyText, $topicId, $userName) | ||
{ | ||
$params = array( | ||
'raw' => $bodyText, | ||
'archetype' => 'regular', | ||
'category' => $categoryId, | ||
'topic_id' => $topicId | ||
); | ||
return $this->_postRequest('/posts', $params, $userName); | ||
|
@@ -435,15 +457,15 @@ function inviteUser($email, $topicId, $userName = 'system') | |
'email' => $email, | ||
'topic_id' => $topicId | ||
); | ||
return $this->_postRequest('/t/'.intval($topicId).'/invite.json', $params, $userName); | ||
return $this->_postRequest('/t/' . intval($topicId) . '/invite.json', $params, $userName); | ||
} | ||
|
||
function changeSiteSetting($siteSetting, $value) | ||
{ | ||
$params = array($siteSetting => $value); | ||
return $this->_putRequest('/admin/site_settings/' . $siteSetting, $params); | ||
} | ||
|
||
function getIDByEmail($email) | ||
{ | ||
$username = $this->getUsernameByEmail($email); | ||
|
@@ -460,10 +482,9 @@ function logoutByEmail($email) | |
$params = array('username_or_email' => $email); | ||
return $this->_postRequest('/admin/users/' . $user_id . '/log_out', $params); | ||
} | ||
function getUserinfoByName($username) | ||
|
||
function getUserinfoByName($username) | ||
{ | ||
return $this->_getRequest("/users/{$username}.json"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters