-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwoocommerce.go
91 lines (78 loc) · 2.04 KB
/
woocommerce.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
package gocommerce
import (
"context"
"errors"
"os"
"path/filepath"
"regexp"
)
type WooCommerce struct {
basePlatform
}
var wpLookupRgx = map[string]string{
"user": `define\(\s*['"]DB_USER['"]\s*,\s*['"](\S+?)['"]\s*\);`,
"pass": `define\(\s*['"]DB_PASSWORD['"]\s*,\s*['"]([^']{0,64})['"]\s*\);`,
"host": `define\(\s*['"]DB_HOST['"]\s*,\s*['"](\S+?)['"]\s*\);`,
"db": `define\(\s*['"]DB_NAME['"]\s*,\s*['"](\S+?)['"]\s*\);`,
"prefix": `$?table_prefix\s*=\s*['"]([^']*?)['"]\s*;`,
}
func (w *WooCommerce) ParseConfig(cfgPath string) (*StoreConfig, error) {
data, err := os.ReadFile(cfgPath)
if err != nil {
return nil, err
}
matches := map[string]string{}
port := 3306
for k, v := range wpLookupRgx {
m := regexp.MustCompile(v).FindStringSubmatch(string(data))
if len(m) != 2 {
continue
}
matches[k] = m[1]
}
if h, p, e := parseHostPort(matches["host"]); p > 0 && e == nil {
matches["host"] = h
port = p
}
return &StoreConfig{
DB: &DBConfig{
Host: matches["host"],
User: matches["user"],
Pass: matches["pass"],
Name: matches["db"],
Prefix: matches["prefix"],
Port: port,
},
}, nil
}
func (w *WooCommerce) BaseURLs(ctx context.Context, docroot string) ([]string, error) {
cfg, err := w.ParseConfig(filepath.Join(docroot, w.ConfigPath()))
if err != nil {
return nil, err
}
db, err := ConnectDB(ctx, *cfg.DB)
if err != nil {
return nil, err
}
prefix, err := cfg.DB.SafePrefix()
if err != nil {
return nil, err
}
var url string
if err = db.QueryRow(`select option_value from ` + prefix + `options where option_name = 'home'`).Scan(&url); err != nil {
return nil, err
}
return []string{url}, nil
}
func (w *WooCommerce) Version(docroot string) (string, error) {
re := regexp.MustCompile(`\$wp_version\s*=\s*'([^']+)';`)
data, err := os.ReadFile(filepath.Join(docroot, "wp-includes", "version.php"))
if err != nil {
return "", err
}
match := re.FindStringSubmatch(string(data))
if len(match) < 2 {
return "", errors.New("no version found")
}
return match[1], nil
}