16
16
17
17
class DiscourseAPI
18
18
{
19
- private $ _sessionKey = null ;
20
19
private $ _protocol = 'http ' ;
21
20
private $ _apiKey = null ;
22
- private $ _userName = null ;
23
21
private $ _dcHostname = null ;
24
22
25
- function __construct ($ dcHostname , $ apiKey = null , $ userName = null )
23
+ function __construct ($ dcHostname , $ apiKey = null )
26
24
{
27
25
$ this ->_dcHostname = $ dcHostname ;
28
26
$ this ->_apiKey = $ apiKey ;
29
- $ this ->_userName = $ userName ;
30
27
}
31
28
32
- private function _ensureSession ($ forceNew = false )
33
- {
34
- if (($ forceNew == false ) && ($ this ->_sessionKey != null )) {
35
- return true ;
36
- }
37
-
38
- $ url = sprintf (
39
- '%s://%s?api_key=%s&api_username=%s ' ,
40
- $ this ->_protocol , $ this ->_dcHostname , $ this ->_apiKey , $ this ->_userName
41
- );
42
-
43
- $ ch = curl_init ();
44
- curl_setopt ($ ch , CURLOPT_URL , $ url );
45
- curl_setopt ($ ch , CURLINFO_HEADER_OUT , true );
46
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
47
- curl_setopt ($ ch , CURLOPT_VERBOSE , 1 );
48
- curl_setopt ($ ch , CURLOPT_HEADER , 1 );
49
- $ out = curl_exec ($ ch );
50
-
51
- $ rc = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
52
-
53
- $ hs = curl_getinfo ($ ch , CURLINFO_HEADER_SIZE );
54
- $ out = substr ($ out , 0 , $ hs );
55
- preg_match ('/^Set-Cookie:\s*([^;]*)/mi ' , $ out , $ m );
56
- parse_str ($ m [1 ], $ cookies );
57
- $ this ->_sessionKey = $ cookies ['_forum_session ' ];
58
-
59
- curl_close ($ ch );
60
-
61
- return ($ rc == 200 );
62
- }
63
-
64
- private function _getRequest ($ reqString , $ paramArray = null )
29
+ private function _getRequest ($ reqString , $ paramArray = null , $ apiUser = 'system ' )
65
30
{
66
31
if ($ paramArray == null ) {
67
32
$ paramArray = array ();
68
33
}
69
34
$ paramArray ['api_key ' ] = $ this ->_apiKey ;
70
- $ paramArray ['api_username ' ] = $ this -> _userName ;
35
+ $ paramArray ['api_username ' ] = $ apiUser ;
71
36
$ ch = curl_init ();
72
37
$ url = sprintf (
73
38
'%s://%s%s?%s ' ,
@@ -76,22 +41,30 @@ private function _getRequest($reqString, $paramArray = null)
76
41
$ reqString ,
77
42
http_build_query ($ paramArray )
78
43
);
44
+
79
45
curl_setopt ($ ch , CURLOPT_URL , $ url );
80
- curl_setopt ($ ch , CURLOPT_COOKIE , '_forum_session= ' .$ this ->_sessionKey );
81
46
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
82
47
$ body = curl_exec ($ ch );
83
48
$ rc = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
84
49
curl_close ($ ch );
85
50
86
- return ($ rc == 200 ) ? $ body : false ;
51
+ $ resObj = new \stdClass ();
52
+ $ resObj ->http_code = $ rc ;
53
+ $ resObj ->apiresult = json_decode ($ body );
54
+ return $ resObj ;
55
+ }
56
+
57
+ private function _putRequest ($ reqString , $ paramArray , $ apiUser = 'system ' )
58
+ {
59
+ return $ this ->_putpostRequest ($ reqString , $ paramArray , $ apiUser , true );
87
60
}
88
61
89
- private function _putRequest ($ reqString , $ paramArray )
62
+ private function _postRequest ($ reqString , $ paramArray, $ apiUser = ' system ' )
90
63
{
91
- return $ this ->_postRequest ($ reqString , $ paramArray , true );
64
+ return $ this ->_putpostRequest ($ reqString , $ paramArray , $ apiUser , false );
92
65
}
93
66
94
- private function _postRequest ($ reqString , $ paramArray , $ putMethod = false )
67
+ private function _putpostRequest ($ reqString , $ paramArray, $ apiUser = ' system ' , $ putMethod = false )
95
68
{
96
69
$ ch = curl_init ();
97
70
$ url = sprintf (
@@ -100,39 +73,22 @@ private function _postRequest($reqString, $paramArray, $putMethod = false)
100
73
$ this ->_dcHostname ,
101
74
$ reqString ,
102
75
$ this ->_apiKey ,
103
- $ this -> _userName
76
+ $ apiUser
104
77
);
105
78
curl_setopt ($ ch , CURLOPT_URL , $ url );
106
79
curl_setopt ($ ch , CURLOPT_POSTFIELDS , http_build_query ($ paramArray ));
107
- curl_setopt ($ ch , CURLOPT_COOKIE , '_forum_session= ' .$ this ->_sessionKey );
108
80
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
109
81
if ($ putMethod ) {
110
82
curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , "PUT " );
111
83
}
112
84
$ body = curl_exec ($ ch );
113
-
114
85
$ rc = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
115
86
curl_close ($ ch );
116
- return ($ rc == 200 ) ? $ body : false ;
117
- }
118
87
119
- /**
120
- * changeSiteSetting
121
- *
122
- * @param string $name name of site setting to be modified
123
- * @param string $value value of site setting to be modified
124
- *
125
- * @return mixed json or HTTP return code
126
- */
127
-
128
- function changeSiteSetting ($ name , $ value )
129
- {
130
- $ this ->_ensureSession ();
131
- $ rc = $ this ->_putRequest (
132
- '/admin/site_settings/ ' .$ name ,
133
- array ('value ' => $ value )
134
- );
135
- return $ rc ;
88
+ $ resObj = new \stdClass ();
89
+ $ resObj ->http_code = $ rc ;
90
+ $ resObj ->apiresult = json_decode ($ body );
91
+ return $ resObj ;
136
92
}
137
93
138
94
/**
@@ -143,60 +99,114 @@ function changeSiteSetting($name, $value)
143
99
* @param string $emailAddress email address of new user
144
100
* @param string $password password of new user
145
101
*
146
- * @return mixed json or HTTP return code
102
+ * @return mixed HTTP return code and API return object
147
103
*/
148
104
149
105
function createUser ($ name , $ userName , $ emailAddress , $ password )
150
106
{
151
- $ json = $ this ->_getRequest ('/users/hp.json ' );
152
- if ($ json === false ) {
107
+ $ obj = $ this ->_getRequest ('/users/hp.json ' );
108
+ if ($ obj -> http_code != 200 ) {
153
109
return false ;
154
110
}
155
111
156
- $ obj = json_decode ($ json );
157
-
158
112
$ params = array (
159
113
'name ' => $ name ,
160
114
'username ' => $ userName ,
161
115
'email ' => $ emailAddress ,
162
116
'password ' => $ password ,
163
- 'challenge ' => strrev ($ obj ->challenge ),
164
- 'password_confirmation ' => $ obj ->value
117
+ 'challenge ' => strrev ($ obj ->apiresult -> challenge ),
118
+ 'password_confirmation ' => $ obj ->apiresult -> value
165
119
);
166
120
167
121
return $ this ->_postRequest ('/users ' , $ params );
168
122
}
169
123
170
- function createCategory ($ name , $ color , $ textColor = '000000 ' )
124
+ /**
125
+ * activateUser
126
+ *
127
+ * @param integer $userId id of user to activate
128
+ *
129
+ * @return mixed HTTP return code
130
+ */
131
+
132
+ function activateUser ($ userId )
133
+ {
134
+ return $ this ->_putRequest ("/admin/users/ {$ userId }/activate " , array ());
135
+ }
136
+
137
+ /**
138
+ * getUserByUsername
139
+ *
140
+ * @param string $userName username of user
141
+ *
142
+ * @return mixed HTTP return code and API return object
143
+ */
144
+
145
+ function getUserByUsername ($ userName )
146
+ {
147
+ return $ this ->_getRequest ("/users/ {$ userName }.json " );
148
+ }
149
+
150
+ /**
151
+ * createCategory
152
+ *
153
+ * @param string $categoryName name of new category
154
+ * @param string $color color code of new category (six hex chars, no #)
155
+ * @param string $textColor optional color code of text for new category
156
+ * @param string $userName optional user to create category as
157
+ *
158
+ * @return mixed HTTP return code and API return object
159
+ */
160
+
161
+ function createCategory ($ categoryName , $ color , $ textColor = '000000 ' , $ userName = 'system ' )
171
162
{
172
163
$ params = array (
173
- 'name ' => $ name ,
164
+ 'name ' => $ categoryName ,
174
165
'color ' => $ color ,
175
166
'text_color ' => $ textColor
176
167
);
177
- return $ this ->_postRequest ('/categories ' , $ params );
168
+ return $ this ->_postRequest ('/categories ' , $ params, $ userName );
178
169
}
179
170
180
- function createTopic ($ title , $ bodyText , $ category , $ replyTo = 0 )
171
+ /**
172
+ * createTopic
173
+ *
174
+ * @param string $topicTitle title of topic
175
+ * @param string $bodyText body text of topic post
176
+ * @param string $categoryName category to create topic in
177
+ * @param string $userName user to create topic as
178
+ * @param string $replyToId post id to reply as
179
+ *
180
+ * @return mixed HTTP return code and API return object
181
+ */
182
+
183
+ function createTopic ($ topicTitle , $ bodyText , $ categoryId , $ userName , $ replyToId = 0 )
181
184
{
182
185
$ params = array (
183
- 'title ' => $ title ,
186
+ 'title ' => $ topicTitle ,
184
187
'raw ' => $ bodyText ,
185
- 'category ' => $ category ,
188
+ 'category ' => $ categoryId ,
186
189
'archetype ' => 'regular ' ,
187
- 'reply_to_post_number ' => $ replyTo ,
190
+ 'reply_to_post_number ' => $ replyToId ,
188
191
);
189
- return $ this ->_postRequest ('/posts ' , $ params );
192
+ return $ this ->_postRequest ('/posts ' , $ params, $ userName );
190
193
}
191
194
192
- function createPost ($ bodyText , $ topicId )
195
+ /**
196
+ * createPost
197
+ *
198
+ * NOT WORKING YET
199
+ */
200
+
201
+ function createPost ($ bodyText , $ topicId , $ categoryId , $ userName )
193
202
{
194
203
$ params = array (
195
204
'raw ' => $ bodyText ,
196
205
'archetype ' => 'regular ' ,
206
+ 'category ' => $ categoryId ,
197
207
'topic_id ' => $ topicId
198
208
);
199
- return $ this ->_postRequest ('/posts ' , $ params );
209
+ return $ this ->_postRequest ('/posts ' , $ params, $ userName );
200
210
}
201
211
202
212
}
0 commit comments