-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave-dictionary.php
More file actions
85 lines (68 loc) · 2.34 KB
/
save-dictionary.php
File metadata and controls
85 lines (68 loc) · 2.34 KB
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
<?php
// save-dictionary.php — UNIVERSAL VERSION
// -------------------------------------------------------
// 1. DETECT MODE: MODULE DEMO vs SITE INSTALL
// -------------------------------------------------------
$localDictionaryPath = __DIR__ . '/dictionary.json'; // módulo / demo
$rootDictionaryPath = $_SERVER['DOCUMENT_ROOT'] . '/assets/data/dictionary.json'; // site real
$isModuleDemo = false;
if (file_exists($localDictionaryPath)) {
// DEMO MODE
$dictionaryPath = $localDictionaryPath;
$isModuleDemo = true;
$backToEditor = "dictionary-editor.php";
$backHome = "index.php";
$downloadUrl = "dictionary.json";
} else {
// SITE MODE
$dictionaryPath = $rootDictionaryPath;
$isModuleDemo = false;
$backToEditor = "dictionary-editor.php";
$backHome = "/"; // ajuste se quiser outra home
$downloadUrl = "/assets/data/dictionary.json";
}
// -------------------------------------------------------
// 2. VALIDATE POST
// -------------------------------------------------------
if (!isset($_POST['terms'], $_POST['definitions'])) {
die("Invalid data.");
}
$terms = $_POST['terms'];
$definitions = $_POST['definitions'];
$newDict = [];
// -------------------------------------------------------
// 3. BUILD NEW DICTIONARY ARRAY
// -------------------------------------------------------
for ($i = 0; $i < count($terms); $i++) {
$term = trim($terms[$i]);
$def = trim($definitions[$i]);
if ($term !== '') {
$newDict[$term] = $def;
}
}
// Sort alphabetically
ksort($newDict, SORT_NATURAL | SORT_FLAG_CASE);
// Encode as pretty JSON
$json = json_encode($newDict, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// -------------------------------------------------------
// 4. SAVE
// -------------------------------------------------------
file_put_contents($dictionaryPath, $json);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dictionary Saved</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
button { padding: 8px 16px; cursor: pointer; }
code { background: #f4f4f4; padding: 2px 4px; border-radius: 4px; }
a { color: #0066cc; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h2>Dictionary Saved Successfully!</h2>
</body>
</html>