@@ -19,12 +19,16 @@ class DiscourseAPI
19
19
private $ _protocol = 'http ' ;
20
20
private $ _apiKey = null ;
21
21
private $ _dcHostname = null ;
22
+ private $ _httpAuthName = '' ;
23
+ private $ _httpAuthPass = '' ;
22
24
23
- function __construct ($ dcHostname , $ apiKey = null , $ protocol ='http ' )
25
+ function __construct ($ dcHostname , $ apiKey = null , $ protocol ='http ' , $ httpAuthName = '' , $ httpAuthPass = '' )
24
26
{
25
27
$ this ->_dcHostname = $ dcHostname ;
26
28
$ this ->_apiKey = $ apiKey ;
27
29
$ this ->_protocol =$ protocol ;
30
+ $ this ->_httpAuthName = $ httpAuthName ;
31
+ $ this ->_httpAuthPass = $ httpAuthPass ;
28
32
}
29
33
30
34
private function _getRequest ($ reqString , $ paramArray = null , $ apiUser = 'system ' )
@@ -42,6 +46,11 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'
42
46
$ reqString ,
43
47
http_build_query ($ paramArray )
44
48
);
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
+ }
45
54
46
55
curl_setopt ($ ch , CURLOPT_URL , $ url );
47
56
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
@@ -82,6 +91,12 @@ private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $
82
91
if ($ putMethod ) {
83
92
curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , "PUT " );
84
93
}
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
+
85
100
$ body = curl_exec ($ ch );
86
101
$ rc = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
87
102
curl_close ($ ch );
@@ -196,6 +211,19 @@ function activateUser($userId)
196
211
return $ this ->_putRequest ("/admin/users/ {$ userId }/activate " , array ());
197
212
}
198
213
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
+
199
227
/**
200
228
* getUsernameByEmail
201
229
*
@@ -287,6 +315,22 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
287
315
);
288
316
return $ this ->_postRequest ('/posts ' , $ params , $ userName );
289
317
}
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
+ }
290
334
291
335
/**
292
336
* createPost
@@ -336,5 +380,10 @@ function logoutByEmail($email)
336
380
$ params = array ('username_or_email ' => $ email );
337
381
return $ this ->_postRequest ('/admin/users/ ' . $ user_id . '/log_out ' , $ params );
338
382
}
383
+
384
+ function getUserinfoByName ($ username )
385
+ {
386
+ return $ this ->_getRequest ("/users/ {$ username }.json " );
387
+ }
339
388
}
340
389
0 commit comments