-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins_softaculous.go
179 lines (143 loc) · 4.2 KB
/
plugins_softaculous.go
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
package directadmin
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/spf13/cast"
)
const (
SoftaculousProtocolHTTP = "1"
SoftaculousProtocolHTTPWWW = "2"
SoftaculousProtocolHTTPS = "3"
SoftaculousProtocolHTTPSWWW = "4"
SoftaculousScriptIDWordPress = 26
)
type SoftaculousScript struct {
AdminEmail string `url:"admin_email"`
AdminPassword string `url:"admin_pass"`
AdminUsername string `url:"admin_username"`
AutoUpgrade bool `url:"en_auto_upgrade"`
AutoUpgradePlugins bool `url:"auto_upgrade_plugins"`
AutoUpgradeThemes bool `url:"auto_upgrade_plugins"`
DatabaseName string `url:"softdb"`
DatabasePrefix string `url:"dbprefix"` // optional
Directory string `url:"softdirectory"`
Domain string `url:"softdomain"`
Language string `url:"language"`
NotifyOnInstall bool `url:"noemail"`
NotifyOnUpdate bool `url:"disable_notify_update"`
OverwriteExisting bool `url:"overwrite_existing"`
Protocol string `url:"softproto"`
SiteDescription string `url:"site_desc"`
SiteName string `json:"site_name"`
}
func (s *SoftaculousScript) Parse() (url.Values, error) {
if err := s.Validate(); err != nil {
return nil, err
}
values := url.Values{}
values.Add("admin_email", s.AdminEmail)
values.Add("admin_pass", s.AdminPassword)
values.Add("admin_username", s.AdminUsername)
if s.AutoUpgrade {
values.Add("en_auto_upgrade", "1")
}
if s.AutoUpgradePlugins {
values.Add("auto_upgrade_plugins", "1")
}
if s.AutoUpgradeThemes {
values.Add("auto_upgrade_themes", "1")
}
if s.DatabaseName != "" {
values.Add("softdb", s.DatabaseName)
if s.DatabasePrefix != "" {
values.Add("dbprefix", s.DatabasePrefix)
}
}
if s.Directory != "" {
values.Add("softdirectory", s.Directory)
}
values.Add("softdomain", s.Domain)
values.Add("language", s.Language)
if !s.NotifyOnInstall {
values.Add("noemail", "1")
}
if !s.NotifyOnUpdate {
values.Add("disable_notify_update", "1")
}
if s.OverwriteExisting {
values.Add("overwrite_existing", "1")
}
values.Add("softproto", s.Protocol)
values.Add("site_desc", s.SiteDescription)
values.Add("site_name", s.SiteName)
return values, nil
}
func (s *SoftaculousScript) Validate() error {
if s.AdminEmail == "" {
return errors.New("admin email is required")
}
if s.AdminPassword == "" {
return errors.New("admin password is required")
}
if s.AdminUsername == "" {
return errors.New("admin username is required")
}
if s.DatabasePrefix != "" && !strings.HasSuffix(s.DatabasePrefix, "_") {
return errors.New("database prefix missing trailing underscore")
}
if s.Language == "" {
return errors.New("language is required")
}
switch s.Protocol {
case SoftaculousProtocolHTTP, SoftaculousProtocolHTTPWWW, SoftaculousProtocolHTTPS, SoftaculousProtocolHTTPSWWW:
default:
return errors.New("invalid protocol")
}
if s.SiteDescription == "" {
return errors.New("site description is required")
}
if s.SiteName == "" {
return errors.New("site name is required")
}
return nil
}
func SoftaculousScriptWithDefaults() *SoftaculousScript {
return &SoftaculousScript{
Language: "en",
Protocol: SoftaculousProtocolHTTPS,
}
}
// SoftaculousInstallScript calls Softaculous's install script API endpoint.
//
// Docs: https://www.softaculous.com/docs/api/remote-api/#install-a-script
func (c *UserContext) SoftaculousInstallScript(script *SoftaculousScript, scriptID int) error {
response := struct {
Error map[string]string `json:"error"`
}{
Error: make(map[string]string),
}
if scriptID == 0 {
return errors.New("missing script id")
}
body, err := script.Parse()
if err != nil {
return err
}
body.Set("softsubmit", "1")
// Softaculous requires a genuine session ID
if c.sessionID == "" {
if err = c.CreateSession(); err != nil {
return fmt.Errorf("failed to create user session: %w", err)
}
}
if _, err = c.makeRequestOld(http.MethodPost, "PLUGINS/softaculous/index.raw?act=software&soft="+cast.ToString(scriptID)+"&multi_ver=1&api=json", body, &response); err != nil {
return err
}
if len(response.Error) > 0 {
return fmt.Errorf("failed to install script: %v", response.Error)
}
return nil
}