-
Notifications
You must be signed in to change notification settings - Fork 0
/
locales-file.go
389 lines (344 loc) · 10.6 KB
/
locales-file.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
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
// Copyright (c) 2023 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package locales
import (
"html"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-corelibs/slices"
"github.com/go-corelibs/words"
"github.com/go-corelibs/x-text/language"
"github.com/go-corelibs/x-text/message"
"github.com/go-enjin/be/pkg/context"
"github.com/go-enjin/be/pkg/editor"
"github.com/go-enjin/be/pkg/feature"
"github.com/go-enjin/be/pkg/forms"
"github.com/go-enjin/be/pkg/log"
"github.com/go-enjin/be/pkg/maps"
"github.com/go-enjin/be/pkg/request/argv"
"github.com/go-enjin/be/pkg/userbase"
)
func (f *CFeature) PrepareRenderFileEditor(w http.ResponseWriter, r *http.Request) (pg feature.Page, ctx context.Context, info *editor.File, eid string, mountPoints feature.MountPoints, handled bool) {
eid = userbase.GetCurrentEID(r)
printer := message.GetPrinter(r)
var err error
fsid := chi.URLParam(r, "fsid")
code := chi.URLParam(r, "code")
if fsid == "" {
f.Enjin.ServeRedirect(f.GetEditorPath(), w, r)
handled = true
return
}
info = editor.ParseFile(fsid, code)
info.Code = code
info.File = ""
info.HasDraft = f.HasDraftLocales(fsid, code)
if locked, lockedBy, ee := f.IsLocaleLocked(fsid, code); ee == nil && locked {
info.LockedBy = lockedBy
if info.Locked = lockedBy != eid; info.Locked {
info.ReadOnly = true
}
} else if err = f.LockLocale(eid, fsid, code); err != nil {
fsidCode := fsid
if code != "" {
fsidCode += "/" + code
}
f.Editor.Site().PushErrorNotice(eid, true, printer.Sprintf(`error locking %[1]s locale for editing: %[2]s`, fsidCode, err.Error()))
handled = true
return
} else {
info.LockedBy = eid
}
var mountedPoints feature.MountedPoints
if mountedPoints = f.FindMountedPoints(fsid); mountedPoints == nil {
f.Enjin.ServeRedirect(f.GetEditorPath(), w, r)
handled = true
return
}
//var mountPoints feature.MountPoints
if countMountedPoints := len(mountedPoints); countMountedPoints > 1 {
if handled = code == ""; handled {
f.RenderFileBrowser(w, r)
return
}
if v, present := mountedPoints[code]; present {
mountPoints = v
} else {
handled = true
f.Enjin.ServeRedirect(f.GetEditorPath()+"/"+fsid, w, r)
return
}
} else if countMountedPoints == 1 {
if v, present := mountedPoints[code]; present {
mountPoints = v
} else {
handled = true
f.Enjin.ServeRedirect(f.GetEditorPath()+"/"+fsid, w, r)
return
}
}
if handled = len(mountPoints) == 0; handled {
log.DebugRF(r, "mount points not found for: fsid=%v, code=%v", fsid, code)
f.Enjin.ServeRedirect(f.GetEditorPath()+"/"+fsid, w, r)
return
}
if pg, ctx, err = f.SelfEditor().PrepareEditPage("file-editor", f.EditorType, r); err != nil {
handled = true
log.ErrorRF(r, "error preparing %v editor page: %v", f.Tag(), err)
f.Enjin.ServeNotFound(w, r)
return
}
if mountPoints.HasRWFS() {
info.Actions = append(info.Actions,
editor.MakeCommitFileAction(printer),
editor.MakeCancelFileAction(printer),
)
} else {
info.ReadOnly = true
}
f.UpdateFileInfo(info, r)
ctx.SetSpecific("LocaleInfo", info)
ctx.SetSpecific("EditFSID", info.FSID)
if code != "" {
ctx.SetSpecific("EditCode", code)
}
ctx.SetSpecific("EditPath", info.Path)
//ctx.SetSpecific("EditFile", info.File)
//ctx.SetSpecific("FileInfo", info)
ctx.SetSpecific("LocaleSystems", f.ListLocales(r))
return
}
func (f *CFeature) RenderFileEditor(w http.ResponseWriter, r *http.Request) {
var pg feature.Page
var ctx context.Context
var handled bool
//var eid string
var mountPoints feature.MountPoints
var info *editor.File
eid := userbase.GetCurrentEID(r)
fsid := chi.URLParam(r, "fsid")
code := chi.URLParam(r, "code")
printer := message.GetPrinter(r)
if pg, ctx, info, _, mountPoints, handled = f.PrepareRenderFileEditor(w, r); handled {
return
}
reqArgv := argv.Get(r)
ctx.SetSpecific(argv.RequestConsumedKey, true)
if reqArgv.NumPerPage == -1 {
reqArgv.NumPerPage = 10
}
if reqArgv.PageNumber == -1 {
reqArgv.PageNumber = 0
}
if ld, ee := f.ReadDraftLocales(fsid, code, mountPoints, !info.HasDraft); ee == nil {
var searchQuery string
if len(reqArgv.Argv) > 0 {
if len(reqArgv.Argv[0]) > 0 {
searchQuery = reqArgv.Argv[0][0]
}
}
if searchQuery != "" {
wc := words.Default()
order := append([]string{}, ld.Order...)
for _, shasum := range ld.Order {
if msgs, ok := ld.Data[shasum]; ok {
var found bool
for _, msg := range msgs {
var checking []string
checking = append(checking, msg.ID)
if msg.Translation.Select == nil {
if msg.Translation.String != "" {
checking = append(checking, msg.Translation.String)
}
} else {
for _, text := range msg.Translation.Select.Cases {
checking = append(checking, text)
}
}
for _, ph := range msg.Placeholders {
checking = append(checking, ph.ID)
}
for _, check := range checking {
score, _ := wc.Search(searchQuery, check)
if found = score > 0; found {
break
}
}
if found {
break
}
}
if found {
continue
}
delete(ld.Data, shasum)
order = slices.Prune(order, shasum)
}
}
ld.Order = order
}
totalMsgs := len(ld.Order)
var totalPages, startItem, endItem int
if reqArgv.NumPerPage >= totalMsgs {
totalPages = 1
startItem = 1
endItem = totalMsgs
} else {
totalPages = totalMsgs / reqArgv.NumPerPage
if remainder := totalMsgs % reqArgv.NumPerPage; remainder > 0 {
totalPages += 1
}
// there are more translations than the number per page requested
var order []string
var start, end int
if start = reqArgv.NumPerPage * reqArgv.PageNumber; start >= totalMsgs {
// redirect to the last page number
reqArgv.PageNumber = totalPages - 1
f.Enjin.ServeRedirect(reqArgv.String(), w, r)
return
}
if end = start + reqArgv.NumPerPage; end >= totalMsgs {
// the end point is past the total number of translations
order = ld.Order[start:]
startItem = start + 1
endItem = totalMsgs
} else {
// the start and end are within the given range
order = ld.Order[start:end]
startItem = start + 1
endItem = end
}
data := make(map[string]map[language.Tag]*LocaleMessage)
for _, shasum := range order {
data[shasum] = ld.Data[shasum]
}
ld = &LocaleData{
FSID: ld.FSID,
Code: ld.Code,
Data: data,
Order: order,
}
}
ctx.SetSpecific("Pagination", &argv.Pagination{
BasePath: r.URL.Path,
PageNumber: reqArgv.PageNumber + 1,
NumPerPage: reqArgv.NumPerPage,
PageIndex: reqArgv.PageNumber,
LastIndex: totalPages - 1,
TotalItems: totalMsgs,
TotalPages: totalPages,
StartItem: startItem,
EndItem: endItem,
SearchQuery: searchQuery,
})
ctx.SetSpecific("LocalesData", ld)
if table, eee := f.MakeTable(ld); eee == nil {
ctx.SetSpecific("LocalesTable", table)
} else {
log.ErrorF("error making locales table: %allMountedPoints", eee)
}
}
pg.SetTitle(printer.Sprintf("Edit: %[1]s", info.Name))
r = feature.AddUserNotices(r, f.Editor.Site().PullNotices(eid)...)
f.SelfEditor().ServePreparedEditPage(pg, ctx, w, r)
}
func (f *CFeature) ReceiveFileEditorChanges(w http.ResponseWriter, r *http.Request) {
var err error
var pg feature.Page
var ctx context.Context
var info *editor.File
var handled bool
var eid string
if pg, ctx, info, eid, _, handled = f.PrepareRenderFileEditor(w, r); handled {
return
}
printer := message.GetPrinter(r)
if info.Tilde != "" {
// deny anything posted to .~stuff
f.Enjin.ServeRedirect(f.SelfEditor().GetEditorPath()+"/"+info.EditFilePath(), w, r)
return
}
nonceValue := r.PostFormValue("nonce")
nonceValue = forms.StrictSanitize(nonceValue)
if !f.Enjin.VerifyNonce("file-editor-form", nonceValue) {
f.Editor.Site().PushErrorNotice(eid, true, printer.Sprintf("Form expired before submitting, please try again."))
f.Enjin.ServeRedirect(f.SelfEditor().GetEditorPath()+"/"+info.EditFilePath(), w, r)
return
}
//fields := context.GetFields(r)
action, _ := feature.ParseEditorOpKey(r.PostFormValue("submit"))
formCtx := map[string]interface{}{}
for _, k := range maps.SortedKeys(r.Form) {
v := r.Form[k]
for i := 0; i < len(v); i++ {
escaped := html.EscapeString(v[i])
sanitized := forms.StrictSanitize(escaped)
cleaned := html.UnescapeString(sanitized)
v[i] = cleaned
}
switch len(v) {
case 0: // nop
case 1:
_ = maps.Set(formCtx, k, v[0])
case 2:
if v[0] == v[1] {
_ = maps.Set(formCtx, k, v[0])
} else {
_ = maps.Set(formCtx, k, v)
}
default:
_ = maps.Set(formCtx, k, v)
}
}
if modified := f.SelfEditor().PrepareEditableFile(r, info); modified != nil {
info = modified
}
if op, ok := f.FileOperations[action]; ok {
if !f.Enjin.ValidateUserRequest(op.Action, w, r) {
log.WarnRF(r, "user denied: %v", op.Action)
f.Editor.Site().PushErrorNotice(eid, true, printer.Sprintf("Permission to perform the operation has been denied."))
f.Enjin.ServeRedirect(f.SelfEditor().GetEditorPath()+"/"+info.EditFilePath(), w, r)
return
}
if op.Confirm != "" {
if _, confirmed := formCtx[op.Confirm]; !confirmed {
f.Editor.Site().PushErrorNotice(eid, true, printer.Sprintf("Unconfirmed operation, please confirm before submitting changes."))
f.Enjin.ServeRedirect(f.SelfEditor().GetEditorPath()+"/"+info.EditFilePath(), w, r)
return
}
}
if op.Validate != nil {
if err = op.Validate(r, pg, ctx, formCtx, info, eid); err != nil {
f.Editor.Site().PushErrorNotice(eid, true, err.Error())
f.Enjin.ServeRedirect(f.SelfEditor().GetEditorPath()+"/"+info.EditFilePath(), w, r)
return
}
}
if op.Operation != nil {
if redirect := op.Operation(r, pg, ctx, formCtx, info, eid); redirect != "" {
f.Enjin.ServeRedirect(redirect, w, r)
return
}
}
} else {
f.Editor.Site().PushErrorNotice(eid, true, printer.Sprintf("Unknown operation, please try again."))
}
if v, ok := formCtx["return"].(string); ok {
if v == "directory" {
f.Enjin.ServeRedirect(f.SelfEditor().GetEditorPath()+"/"+info.EditDirectoryPath(), w, r)
return
}
}
f.Enjin.ServeRedirect(f.SelfEditor().GetEditorPath()+"/"+info.EditFilePath(), w, r)
}