-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPost.lua
115 lines (97 loc) · 3.78 KB
/
Post.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
--[[
CheckLogin function Check Login given in PublishSettings
and save to PublishSettings if successful
by Martin von Berg
]]
------------- Debug ----------------------
--require 'strict.lua'
-- local inspect = require 'inspect'
--local LrMobdebug = import 'LrMobdebug' -- Import LR/ZeroBrane debug module
--LrMobdebug.start()
----------------------------------------
local LrHttp = import( 'LrHttp' )
-- Check Login given in PublishSettings
function CheckLogin( publishSettings )
--LrMobdebug.on()
publishSettings.urlreadable = false
--Log('Debug: ' .. tostring(logDebug))
local ReturnTable = {}
publishSettings.hash = ''
local uid = publishSettings.loginName
local pwd = publishSettings.loginPassword
local hash = 'Basic ' .. encb64(uid .. ':' .. pwd)
local httphead = {
{field='Authorization', value=hash},
}
local url = publishSettings.siteURL .. "/wp-json/wp/v2/"
Log('CheckLogin url: ' .. url) -- Debugging
Log('CheckLogin hash-value: ' .. hash) -- Debugging
--local result, headers = LrHttp.get( url ) -- GET-Anfrage ohne Auth
local result, headers = LrHttp.post( url, '', {}, 'HEAD',2 ) -- GET-Anfrage ohne Auth
local headresp = inspect(headers)
if headers.status == 200 then
ReturnTable['warning'] = 'REST-API (GET) of site can be reached without PWD! Check OAuth!'
elseif headers.status == 401 then
result = JSON:decode(result)
local str = 'OK. GET-Req without Auth. blocked. Authorization required. ' -- .. result.message
ReturnTable['Authentification'] = str
elseif string.match(headresp, 'error') then
ReturnTable['error'] = 'Cannot reach Site. Check Site URL.'
Log('Site not found: ' .. headresp)
else
ReturnTable['error'] = 'Cannot Reach Site. Unknown Error'
Log('Site not reached: ' .. headers.status)
end
if headers.status == 200 or headers.status == 401 then
publishSettings.urlreadable = true
local result, headers = LrHttp.post( url, '', httphead )
if headers.status == 500 then
ReturnTable['error'] = 'Login failed! Check Username and Password.'
elseif headers.status == 404 then
ReturnTable['success'] = 'Sucess. Login-OK! with hash-Method: ' .. hash
publishSettings.hash = encb64(uid .. ':' .. pwd)
result = JSON:decode(result) -- Debugging
Log(result) -- Debugging
elseif headers.status == 401 then
result = JSON:decode(result)
ReturnTable['error'] = result.message
else
ReturnTable['error'] = 'Site reachable, but unknown error.'
end
-- Check Install of Plugin for communication via REST-API
url = publishSettings.siteURL .. "/wp-json/wp/v2/plugins"
local result, headers = LrHttp.get( url, httphead )
if headers.status == 200 then
local wppluginName = 'wpcat[_-]json[_-]rest'
local str = inspect(result) -- JSON-Rückgabe für ein Image in str umwandeln
local i = string.match(result, wppluginName)
local pluginstatus
local pluginversion
if i ~= nil then
result = JSON:decode(result)
for k = 1, #result do
local m,n = string.find(result[k]['textdomain'], wppluginName)
if m~= nil then
pluginstatus = result[k]['status']
pluginversion = result[k]['version']
end
end
if pluginstatus == 'active' then
ReturnTable['plugin'] = 'OK. Wordpress Plugin installed and active. Version: ' .. tostring(pluginversion)
publishSettings.wpplugin = true
else
ReturnTable['plugin'] = 'OK. Wordpress Plugin installed but not activated! Version: ' .. tostring(pluginversion)
publishSettings.wpplugin = false
end
else
ReturnTable['plugin'] = 'Plugin not installed. Many functions won\'t work!'
publishSettings.wpplugin = false
end
else
ReturnTable['plugin'] = 'Unknown Error while searching for Plugin'
publishSettings.wpplugin = false
end
end
Log('CheckLogin: End')
return ReturnTable, nil
end