forked from 1r0n3d3v3l0per/scribeflow-mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscribeflow-tinymce-deepl-openai-init.php
245 lines (219 loc) · 9.08 KB
/
scribeflow-tinymce-deepl-openai-init.php
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
<?php
// Creo la tabella dei token, se non esiste.
// I create the token table, if it does not exist.
$sql_create_table = "CREATE TABLE IF NOT EXISTS tokens (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
data_1 VARCHAR(255),
data_4 VARCHAR(255)
)";
mysqli_query($dbh, $sql_create_table);
// Recupero il token API dalla tabella dei token
// I retrieve the API token from the token table
$sql_fetch_token = "SELECT data_4 FROM tokens WHERE data_1 = 'tokens'";
$result = mysqli_query($dbh, $sql_fetch_token);
// Controllo se ci sono risultati
// Check if there are any results
if ($result && mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$token = $row['data_4'];
} else {
echo "No results";
}
// Visualizzo il modulo per inserire il token API
// I display the form to enter the API token
echo <<<HTML
<form action="process.php" method="POST">
<label for="api_token">API Token:</label>
<input type="text" name="api_token" id="api_token" required>
<input type="submit" value="Save">
</form>
HTML;
// Elaboro l'invio del modulo
// I process the form submission
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Sanitize and validate the user input
$api_token = mysqli_real_escape_string($dbh, $_POST["api_token"]);
$api_token = trim($api_token);
// Eseguo una validazione aggiuntiva, se necessario
// I perform additional validation, if necessary
// Memorizzo il token API nel database
// I store the API token in the database
$sql_store_token = "INSERT INTO tokens (data_1, data_4) VALUES ('tokens', '$api_token')";
mysqli_query($dbh, $sql_store_token);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.tiny.cloud/1/INSERT-API-KEY/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
tinymce.init({
selector: "textarea",
language: "it",
editor_deselector: "mceNoEditor",
plugins: "advlist anchor autolink autoresize autosave bbcode charmap code codesample directionality emoticons fullpage fullscreen help hr image imagetools importcss insertdatetime legacyoutput link lists media nonbreaking pagebreak paste preview print save searchreplace tabfocus table template textcolor textpattern toc visualblocks visualchars wordcount",
toolbar: "formatselect | bold italic strikethrough forecolor backcolor | emoticons | scribeflowdeepl | scribeflowopenai | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | removeformat | link image media insertfile",
relative_urls: false,
remove_script_host: false,
image_advtab: true,
height: 400,
max_height: 500,
extended_valid_elements: "i[class|src|style]",
// SCRIBEFLOW - CUSTOM TRANSLATION FUNCTION
setup: function (editor) {
var languages = [
{ code: "BG", name: "Bulgarian" },
{ code: "CS", name: "Czech" },
{ code: "DA", name: "Danish" },
{ code: "DE", name: "German" },
{ code: "EL", name: "Greek" },
{ code: "EN-GB", name: "English (UK)" },
{ code: "EN-US", name: "English (US)" },
{ code: "ES", name: "Spanish" },
{ code: "ET", name: "Estonian" },
{ code: "FI", name: "Finnish" },
{ code: "FR", name: "French" },
{ code: "HU", name: "Hungarian" },
{ code: "ID", name: "Indonesian" },
{ code: "IT", name: "Italian" },
{ code: "JA", name: "Japanese" },
{ code: "KO", name: "Korean" },
{ code: "LT", name: "Lithuanian" },
{ code: "LV", name: "Latvian" },
{ code: "NB", name: "Norwegian" },
{ code: "NL", name: "Dutch" },
{ code: "PL", name: "Polish" },
{ code: "PT-BR", name: "Portuguese" },
{ code: "PT-PT", name: "Portuguese (BR)" },
{ code: "RO", name: "Romanian" },
{ code: "RU", name: "Russian" },
{ code: "SK", name: "Slovak" },
{ code: "SL", name: "Slovenian" },
{ code: "SV", name: "Swedish" },
{ code: "TR", name: "Turkish" },
{ code: "UK", name: "Ukrainian" },
{ code: "ZH", name: "Chinese" }
];
var currentLanguage = 'it';
// Funzione per ottenere il testo di traduzione in base alla lingua corrente
// Function to get the translation text based on the current language
function getTranslationText() {
var translateTexts = {
bg: "Преведи с DeepL",
cs: "Přeložit pomocí DeepL",
da: "Oversæt med DeepL",
de: "Mit DeepL übersetzen",
el: "Μετάφραση με το DeepL",
"en-gb": "Translate with DeepL",
"en-us": "Translate with DeepL",
es: "Traducir con DeepL",
et: "Tõlgi DeepL-iga",
fi: "Käännä DeepL:llä",
fr: "Traduire avec DeepL",
hu: "Fordítás a DeepL segítségével",
id: "Terjemahkan dengan DeepL",
it: "Traduci con DeepL",
ja: "DeepLで翻訳する",
ko: "DeepL로 번역하기",
lt: "Versti su DeepL",
lv: "Tulkot ar DeepL",
nb: "Oversett med DeepL",
nl: "Vertalen met DeepL",
pl: "Tłumacz z DeepL",
"pt-br": "Traduzir com o DeepL",
"pt-pt": "Traduzir com o DeepL",
ro: "Tradu cu DeepL",
ru: "Перевести с DeepL",
sk: "Preložiť pomocou DeepL",
sl: "Prevedi z DeepL",
sv: "Översätt med DeepL",
tr: "DeepL ile Çevir",
uk: "Перекласти з DeepL",
zh: "使用 DeepL 翻译"
// Aggiungo altre lingue e testi di traduzione se necessario per toolbar
// Add more languages and translation texts if needed for toolbar
};
return translationTexts[currentLanguage] || "Translate with DeepL";
}
// Aggiorno il valore della lingua corrente quando l'utente cambia la lingua nell'editor
// I update the value of the current language when the user changes the language in the editor
editor.on("init", function () {
currentLanguage = editor.getParam("language") || "it";
});
editor.ui.registry.addMenuButton("scribeflowdeepl", {
text: getTranslationText(),
fetch: function (callback) {
var items = [];
for (var i = 0; i < languages.length; i++) {
(function (i) {
items.push({
type: "menuitem",
text: languages[i].name,
onAction: function () {
// Get selected text
var selectedText = editor.selection.getContent({
format: "text"
});
// Uso DeepL API per tradurre il testo selezionato
// I use DeepL API to translate the selected text
fetch("https://api-free.deepl.com/v2/translate", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: `text=${selectedText}&target_lang=${languages[i].code}&auth_key=<?php echo $token; ?>`
})
.then(response => response.json())
.then(data => {
// Sostituisco il testo selezionato con il testo tradotto
// I replace the selected text with the translated text
editor.selection.setContent(data.translations[0].text);
})
.catch(error => {
console.error("Error while translating text:", error);
});
}
});
})(i);
}
callback(items);
}
});
editor.ui.registry.addButton("scribeflow", {
text: "Genera con OpenAI",
onAction: function() {
var apiKey = "TOKEN_API_OPENAI";
var promptText = editor.getContent();
axios
.post(
"https://api.openai.com/v1/engines/text-davinci-003/completions",
{
prompt: promptText,
max_tokens: 500,
temperature: 1,
top_p: 1,
frequency_penalty: 0,
n: 1,
stop: null
},
{
headers: {
Authorization: "Bearer " + apiKey,
"Content-Type": "application/json"
}
}
)
.then(function(response) {
var generatedText = response.data.choices[0].text;
editor.selection.setContent(generatedText);
})
.catch(function(error) {
console.error("Errore nella generazione del testo:", error);
});
}
});
}
});
</script>
</head>