-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmb_api.lua
539 lines (422 loc) · 16.5 KB
/
mb_api.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
-- WORDPRESS
-- Remote access to a Wordpress installation
--[[
Requires installation of the "JSON API" plugin in Wordpress, AND the
installation of the "auth" controller into the "controller" folder inside it.
The JSON API plugin is here:
http://wordpress.org/extend/plugins/json-api/other_notes/
The "auth.php" file is here:
https://github.com/mattberg/wp-json-api-auth
Here's the how-to use the "auth", but hard to figure out!
https://github.com/mattberg/wp-json-api-auth/issues/2
USAGE:
local wordpress = require ("wordpress")
wordpress.url = "http://example.com/wordpress/" (note the ending slash!)
-- Get the 'nonce' for a given controller and method, required to do
-- anything. It's a form of authentication.
-- Default is get a nonce for controller=auth, method=generate_auth_cookie
wordpress.access(url, username, password, callbackUponSuccess)
-- When it is done, the following will be set:
wordpress.user.wp_user : a table of user info
wordpress.wp_cookie : the authentication cookie (a string)
]]
-------------------------------------------------
-- url library, used for decode/encode
-------------------------------------------------
local url = require("socket.url")
-------------------------------------------------
-- functions library
-------------------------------------------------
require ("funx")
local json = require( "json" )
---------------------------
local app = { user = {} }
app.wp_nonce = { nonce = "" }
app.wp_cookie = {}
app.url = ""
local function requestBody(params)
local e = {}
for k, v in pairs(params) do
table.insert(e, k .. "=" .. url.escape(v))
end
return table.concat(e, "&")
end
----------------
-- Get nonce ID in the way the WordPress JSON API wants it
--[[
local function get_nonce_id(controller, method)
controller = lower(controller);
method = lower(method);
return "mb_api-" .. controller .. "-" .. method
end
--]]
----------------
-- Get current user information
function app.validateUser()
local function networkListener( event )
if ( event.isError ) then
--print( "Network error!")
app.status = { status = "error", error = "Network error!", }
app.onError(app.status)
else
local data = json.decode(event.response)
app.result = data
app.callback(app.result)
end
end
local url = app.url
local postdata = "cookie="..app.wp_cookie
local params = { body = {}, }
params.body = postdata
network.request( url.."mb/user/validate_user", "POST", networkListener, params)
end
----------------
-- Get current user information
function app.getCurrentUserInfo()
local function networkListener( event )
if ( event.isError ) then
--print( "Network error!")
app.status = { status = "error", error = "Network error!", }
app.onError(app.status)
else
local data = json.decode(event.response)
app.result = data
app.callback(app.result)
end
end
local url = app.url
local postdata = "cookie="..app.wp_cookie
local params = { body = {}, }
params.body = postdata
network.request( url.."mb/user/get_currentuser_info", "POST", networkListener, params)
end
----------------
-- Get current user information
function app.registerUser()
local function networkListener( event )
if ( event.isError ) then
--print( "Network error!")
app.status = { status = "error", error = "Network error!", }
app.onError(app.status)
else
local data = json.decode(event.response)
app.result = data
app.callback(app.result)
end
end
local url = app.url
local postdata = "cookie="..app.wp_cookie
local params = { body = {}, }
app.params.cookie = app.wp_cookie
params.body = requestBody(app.params)
params.dev = "1" -- for developing
network.request( url.."mb/user/new_user", "POST", networkListener, params)
end
----------------
-- Get an authentication cookie so we can do more
-- Requires signing in with a username/password, set elsewhere
function app.generateAuthCookie()
--local mime = require("mime")
local function networkListener( event )
if ( event.isError ) then
app.status = { status = "error", error = "Network error!", }
app.onError(app.status)
print( "app.generateAuthCookie: Network error!")
else
--print ( "generateAuthCookie RESPONSE: " .. event.response )
local data = json.decode(event.response)
app.status = data
--funx.dump(data)
if (data.status ~= "error") then
app.wp_cookie = data.cookie
app.nextFunctionWithCookie()
else
app.onError(data)
end
end
end
local url = app.url
local username = app.username
local password = app.password
local postdata = "nonce="..app.wp_nonce.nonce
local postdata = postdata.."&username="..username
local postdata = postdata.."&password="..password
local params = { body = {}, }
params.body = postdata
network.request( url.."mb/auth/generate_auth_cookie", "POST", networkListener, params)
end
--[[
----------------
-- Get a post
-- Use either id or slug to identify the post
function app.getPost(id_method, id, callback)
local function networkListener( event )
if ( event.isError ) then
print( "getPost: Network error!")
if (callback) then
callback( { status = "network error", err = "getPost: Network error!"} )
end
else
--print ( "getPost: " .. event.response )
local data = json.decode(event.response)
app.result = data;
end
if (callback) then
callback()
end
end
local url = app.url
--local postdata = "controller=auth&method=generate_auth_cookie"
controller = "core_auth"
method = "get_post"
local postdata = "" --"controller="..controller.."&method="..method
if (id_method == "slug") then
postdata = postdata .. "post_slug="..id
else
postdata = postdata .. "post_id="..id
end
local params = {}
params.body = postdata
network.request( url.."mb/"..controller.."/"..method, "POST", networkListener, params)
end
----------------
-- Submit a comment
-- Use either id or slug to identify the post
function app.submitComment(event)
local function networkListener( event )
if ( event.isError ) then
print( "getPost: Network error!")
callback( { status = "network error", err = "submitComment: Network error!"} )
else
print ( "submitComment: " .. event.response )
local data = json.decode(event.response)
app.result = data;
if (callback) then
callback(app.result)
end
end
end
local url = app.url
--local postdata = "controller=auth&method=generate_auth_cookie"
local controller = "respond"
local method = "submit_comment"
local postdata = ""
postdata = postdata .. "&post_id=".. app.params.post_id
postdata = postdata .. "&name=".. app.params.name
postdata = postdata .. "&email=".. app.params.email
postdata = postdata .. "&content=".. app.params.content
postdata = postdata .. "&cookie="..app.wp_cookie
postdata = postdata .. "&controller="..controller
postdata = postdata .. "&method="..method
local params = {}
params.body = postdata
network.request( url.."mb/"..controller.."/"..method, "POST", networkListener, params)
end
--]]
----------------
-- Submit a post: possible values from WordPress. We should NOT use all of these!
--[[
$post = array(
'ID' => [ <post id> ] //Are you updating an existing post?
'menu_order' => [ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs.
'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments.
'ping_status' => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off
'pinged' => [ ? ] //?
'post_author' => [ <user ID> ] //The user ID number of the author.
'post_category' => [ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories
'post_content' => [ <the text of the post> ] //The full text of the post.
'post_date' => [ Y-m-d H:i:s ] //The time post was made.
'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT.
'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs.
'post_name' => [ <the name> ] // The name (slug) for your post
'post_parent' => [ <post ID> ] //Sets the parent of the new post.
'post_password' => [ ? ] //password for post?
'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] //Set the status of the new post.
'post_title' => [ <the title> ] //The title of your post.
'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //You may want to insert a regular post, page, link, a menu item or some custom post type
'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags.
'to_ping' => [ ? ] //?
'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies.
);
--]]
--[[
function app.submitPost(event)
local url = app.url
local function networkListener( event )
if ( event.isError ) then
print( "getPost: Network error!")
callback( { status = "network error", err = "submitPost: Network error!"} )
else
print ( "submitPost: " .. event.response )
local data = json.decode(event.response)
app.result = data;
if (callback) then
callback(app.result)
end
end
end
local function submitPostWithNonce(event)
if ( event.isError ) then
print( "Network error!")
else
print ( "RESPONSE to getNonce: " .. event.response )
local nonce = json.decode(event.response)
--local postdata = "controller=auth&method=generate_auth_cookie"
-- clean up params to be sure no missing values
app.params.post_date = app.params.post_date or ""
app.params.menu_order = app.params.menu_order or ""
app.params.comment_status = app.params.comment_status or ""
app.params.ping_status = app.params.ping_status or ""
app.params.pinged = app.params.pinged or ""
app.params.post_author = app.params.post_author or ""
app.params.post_category = app.params.post_category or ""
app.params.post_date_gmt = app.params.post_date_gmt or ""
app.params.post_excerpt = app.params.post_excerpt or ""
app.params.post_name = app.params.post_name or ""
app.params.post_parent = app.params.post_parent or ""
app.params.post_password = app.params.post_password or ""
app.params.post_status = app.params.post_status or ""
app.params.post_title = app.params.post_title or ""
app.params.post_type = app.params.post_type or ""
app.params.tags_input = app.params.tags_input or ""
app.params.to_ping = app.params.to_ping or ""
app.params.tax_input = app.params.tax_input or ""
--------------------
-- Set the POST data
local postdata = ""
postdata = postdata .. "&post_content=" .. funx.escape(app.params.post_content)
postdata = postdata .. "&post_date=" .. funx.escape(app.params.post_date)
postdata = postdata .. "&menu_order=" .. funx.escape(app.params.menu_order)
postdata = postdata .. "&comment_status=" .. funx.escape(app.params.comment_status)
postdata = postdata .. "&ping_status=" .. funx.escape(app.params.ping_status)
postdata = postdata .. "&pinged=" .. funx.escape(app.params.pinged)
postdata = postdata .. "&post_author=" .. funx.escape(app.params.post_author)
postdata = postdata .. "&post_category=" .. funx.escape(app.params.post_category)
postdata = postdata .. "&post_date_gmt=" .. funx.escape(app.params.post_date_gmt)
postdata = postdata .. "&post_excerpt=" .. funx.escape(app.params.post_excerpt)
postdata = postdata .. "&post_name=" .. funx.escape(app.params.post_name)
postdata = postdata .. "&post_parent=" .. funx.escape(app.params.post_parent)
postdata = postdata .. "&post_password=" .. funx.escape(app.params.post_password)
postdata = postdata .. "&post_status=" .. funx.escape(app.params.post_status)
postdata = postdata .. "&post_title=" .. funx.escape(app.params.post_title)
postdata = postdata .. "&post_type=" .. funx.escape(app.params.post_type)
postdata = postdata .. "&tags_input=" .. funx.escape(app.params.tags_input)
postdata = postdata .. "&to_ping=" .. funx.escape(app.params.to_ping)
postdata = postdata .. "&tax_input=" .. funx.escape(app.params.tax_input)
local controller = "posts_auth"
local method = "create_post"
postdata = postdata .. "&controller="..controller
postdata = postdata .. "&method="..method
postdata = postdata .. "&cookie="..app.wp_cookie
postdata = postdata .. "&nonce=".. nonce.nonce
--funx.dump(app.params)
local params = {}
params.body = postdata
--print ("URL: ",url.."mb/"..controller.."/"..method)
network.request( url.."mb/"..controller.."/"..method, "POST", networkListener, params)
end
end
-- Get a nonce to submit the post
controller = "posts_auth"
method = "create_post"
local postdata = "controller="..controller.."&method="..method
local params = {}
params.body = postdata
network.request( url.."mb/core_auth/get_nonce", "POST", submitPostWithNonce, params)
end
--]]
----------------
-- Get a nonce for future transactions
-- Default is get a nonce for controller=auth, method=generate_auth_cookie
function app.getNonce(controller, method, nextFunction)
--local mime = require("mime")
local function networkListener( event )
if ( event.isError ) then
app.status = { status = "error", error = "Network error!", }
app.onError(app.status)
else
--print ( "RESPONSE to getNonce: " .. event.response )
local data = json.decode(event.response)
-- Failure may generate text, but not a table, right?
if (type(data) == "table") then
app.wp_nonce = data;
if (app.wp_nonce and app.wp_nonce.nonce) then
app.generateAuthCookie()
else
-- ERROR
print ( "mb_api.lua: Error in response to getNonce request to ".. app.url .. " : " .. event.response )
app.onError(event.response)
end
else
-- ERROR
print ( "mb_api.lua: Error in response to getNonce request to ".. app.url .. " : " .. event.response )
app.onError(event.response)
end
end
end
local url = app.url
--local postdata = "controller=auth&method=generate_auth_cookie"
-- Get a nonce to generate the cookie
controller = "auth"
method = "generate_auth_cookie"
local postdata = "controller="..controller.."&method="..method
local params = {}
params.body = postdata
network.request( url.."mb/book/get_nonce", "POST", networkListener, params)
end
----------------------------------------
-- getUserInfo
-- A handy packaging of the getCurrentUserInfo.
-- It calls onSuccess or onFailure as appropropriate.
-- This is useful for "login".
function app.getUserInfo(url, username, password, onSuccess, onFailure)
local params = {}
local controller = "auth"
local method = "generate_auth_cookie"
local action = app.getCurrentUserInfo
local callback = onSuccess
local onerror = onError
app.access(url, username, password, controller, method, params, action, onSuccess, onFailure)
end
----------------
-- Verify the current user exists
-- A handy packaging of the validateUser.
-- It calls onSuccess or onFailure as appropropriate.
-- This is useful for "login".
function app.validate_user(url, username, password, onSuccess, onFailure)
local params = {}
local controller = "auth"
local method = "generate_auth_cookie"
local action = app.validateUser
local callback = onSuccess
local onerror = onError
app.access(url, username, password, controller, method, params, action, onSuccess, onFailure)
end
----------------
-- Create a new user
-- A handy packaging of registerUser.
-- It calls onSuccess or onFailure as appropropriate.
function app.register_user(url, username, password, params, onSuccess, onFailure)
local controller = "auth"
local method = "generate_auth_cookie"
local action = app.registerUser
local callback = onSuccess
local onerror = onError
app.access(url, username, password, controller, method, params, action, onSuccess, onFailure)
end
----------------------------------------
-- nextFunction is function to call after getting the cookie
-- callback is function to call when everything is finished
function app.access(url, username, password, controller, method, params, nextFunction, callback, onError)
app.url = url
app.username = username
app.password = password
app.params = params
app.nextFunctionWithCookie = nextFunction
app.callback = callback
app.onError = onError
controller = controller or "auth"
method = method or "generate_auth_cookie"
app.getNonce(controller, method, app.generateAuthCookie)
end
return app