Add files via upload

This commit is contained in:
Nikiroy78 2023-03-08 01:10:34 +03:00 committed by GitHub
commit dc0fdbf73d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 322 additions and 0 deletions

View File

@ -0,0 +1,7 @@
<?php
function apiOut ($out) {
return json_encode($out);
}
?>

View File

@ -0,0 +1,3 @@
<?php
$PATH_COMPONENTS = $_SERVER['DOCUMENT_ROOT']; // Путь до корня проекта
?>

View File

@ -0,0 +1,47 @@
<?php
include "config.php";
$db = new SQLite3($PATH_COMPONENTS . "/database.db");
function getRequestHistory () {
global $db;
$results = $db->query('SELECT date, hash, text FROM history;');
$list = array();
while ($row = $results->fetchArray()) {
// var_dump($row);
$list[] = '['. $row['date'] .']: ('. $row['hash'] .'): '. $row['text'];
}
return $list;
}
function saveHistory ($md5, $text) {
global $db;
$stmt = $db->prepare("INSERT INTO history (date, hash, text) VALUES (:time, :hash, :text);");
//if (!$stmt) print_r("fail: " . $db->lastErrorMsg());
$stmt->bindParam(':hash', $md5, SQLITE3_TEXT);
$stmt->bindParam(':text', $text, SQLITE3_TEXT);
$stmt->bindParam(':time', time(), SQLITE3_INTEGER);
$stmt->execute();
}
function saveResult ($md5, $text, $result) {
global $db;
$stmt = $db->prepare("INSERT INTO results (hash, result) VALUES (:md5Val, :result);");
//if (!$stmt) print_r("fail: " . $db->lastErrorMsg());
$stmt->bindParam(':md5Val', $md5, SQLITE3_TEXT);
$stmt->bindParam(':result', json_encode($result), SQLITE3_TEXT);
$stmt->execute();
saveHistory($md5, $text);
}
function getResultFromMd5 ($md5, $lang) {
return "NO";
}
?>

View File

View File

@ -0,0 +1,16 @@
<?php
error_reporting(E_ERROR | E_PARSE);
include "database-controller.php";
function getHistory () {
$result = getRequestHistory();
if (count($result) == 0) {
return "Записи отсутствуют";
}
else {
return "Запросы: \n". implode("\n", $result);
}
}
?>

7
api/get_history.php Normal file
View File

@ -0,0 +1,7 @@
<?php
include "components/get-history.php";
die(
getHistory()
);
?>

128
api/wrong_symbols.find.php Normal file
View File

@ -0,0 +1,128 @@
<?php
include "components/database-controller.php";
include "components/api-out.php";
error_reporting(E_ERROR | E_PARSE);
function toMd5 ($str) { // Возможность своей ренализации алгоритма md5 при необходимости
return md5($str);
}
function detectLang ($text) {
$ruSymbols = selectRuIndex($text);
$text = iconv("UTF-8", "windows-1251", $text);
$textArr = str_split($text, 1);
return (float)count($ruSymbols) / (float)count($textArr) >= 0.5 || count(selectRuIndex($text)) == strlen($text) ? "ru_RU" : "en_US";
}
function selectEnIndex ($text) {
$enSymbols = array();
// $textArr = explode(" ", $text);
// $textArr = str_split($text, 1);
$text = iconv("UTF-8", "windows-1251", $text);
$textArr = str_split($text, 1);
$enDict = str_split('qwertyuiopasdfghjklzxcvbnm', 1);
// print_r($textArr);
for ($i=0; $i <= count($textArr); $i++) {
// if (strpos('ййцукенгшщзхъфывапролджэячсмитьбюёё', mb_strtolower($text[$i])) === true) {
if (in_array(mb_strtolower($textArr[$i]), $enDict)) {
$enSymbols[] = $i;
}
}
return $enSymbols;
}
function selectRuIndex ($text) {
$enSymbols = selectEnIndex($text);
$result = array();
$text = iconv("UTF-8", "windows-1251", $text);
$textArr = str_split($text, 1);
for ($i=0; $i < count($textArr); $i++) {
if (!in_array($i, $enSymbols)) {
$result[] = $i;
}
}
return $result;
}
function wrong_symbol ($text, $lang) {
switch ($lang) {
case "ru_RU":
$cacheResult = getResultFromMd5(toMd5($text), "ru_RU");
if (
$cacheResult === "NO"
) {
// return selectRuIndex($text);
$enSymbols = selectEnIndex($text);
$text = iconv("UTF-8", "windows-1251", $text);
$textArr = str_split($text, 1);
$recheckSymbols = array();
$enDict = str_split('eopakxcyEOPAKXCBMHT', 1);
for ($i=0; $i <= count($textArr); $i++) {
if (in_array($textArr[$i], $enDict)) {
$recheckSymbols[] = $i;
}
}
saveResult(
toMd5($text),
$text,
$recheckSymbols
);
return $recheckSymbols;
}
else {
saveHistory(toMd5($text), $text);
return $cacheResult;
}
case "en_US":
$cacheResult = getResultFromMd5(toMd5($text), "en_US");
if (
$cacheResult === "NO"
) {
$ruSymbols = selectRuIndex($text);
$text = iconv("UTF-8", "windows-1251", $text);
$textArr = str_split($text, 1);
$recheckSymbols = array();
$ruDict = str_split(iconv("UTF-8", "windows-1251", "еоракхсуЕОРАКХСВМНТ"), 1);
for ($i=0; $i <= count($textArr); $i++) {
if (in_array($textArr[$i], $ruDict)) {
$recheckSymbols[] = $i;
}
}
saveResult(
toMd5($text),
$text,
$recheckSymbols
);
return $recheckSymbols;
}
else {
saveHistory(toMd5($text), $text);
return $cacheResult;
}
default : // Определим язык и применим рекурсию
return wrong_symbol($text, detectLang($text));
}
}
die(
apiOut(
wrong_symbol($_GET['text'], $_GET['lang'])
)
);
?>

BIN
database.db Normal file

Binary file not shown.

114
index.php Normal file
View File

@ -0,0 +1,114 @@
<!doctype html>
<html>
<head>
<title>Тестовое задание</title>
<meta charset="utf-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<style>
body {
margin-top : 2.5%;
}
</style>
</head>
<body>
<center><h1>Подсветка латинских символов</h1></center>
<form>
<div class="mb-3">
<label for="textArea" class="form-label">Введите текст здесь</label>
<textarea type="text" class="form-control" id="textArea"></textarea>
<hr>
<p id="outputArea"></p>
<hr>
</div>
<div class="mb-3">
<label for="textArea" class="form-label">История запросов</label>
<textarea type="text" class="form-control" id="historyArea" readonly><?php
include "api/components/get-history.php";
echo getHistory();
?></textarea>
</div>
<center><button type="button" class="btn btn-primary" onclick="formHandler();">Отправить</button></center>
</form>
<script>
var textInputed = document.getElementById('textArea').innerHTML;
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
var autoLightMode = false;
function syncHistroy () {
let request = new XMLHttpRequest();
request.open("GET", "/api/get_history.php", false);
request.send();
if (request.status == 200) {
return request.responseText;
}
}
const formHandler = () => {
let request = new XMLHttpRequest();
request.open("GET", `/api/wrong_symbols.find.php?text=${textInputed}`, false);
request.send();
console.log(request.status);
if (request.status == 200) {
let result = JSON.parse(request.responseText);
let index;
let bufferStr = textInputed;
let lightingLetters = [];
for (let i in result) {
index = result[i];
lightingLetters.push(`<strong>${bufferStr[index]}</strong>`);
}
for (let i in result) {
index = result[i];
bufferStr = replaceAll(bufferStr, bufferStr[index], "");
}
bufferStr = bufferStr.split("");
let lightingLettersId = 0;
let endlessStr = "";
for (let i in bufferStr) {
endlessStr += bufferStr[i];
if (lightingLettersId < lightingLetters.length) {
endlessStr += lightingLetters[lightingLettersId];
lightingLettersId++;
}
}
document.getElementById('outputArea').innerHTML = endlessStr;
document.getElementById('historyArea').innerHTML = syncHistroy();
if (!autoLightMode) autoLightMode = true;
}
}
function realTimeTyping () {
// console.log(123);
if (textInputed != document.getElementById('textArea').value) {
textInputed = document.getElementById('textArea').value;
if (!autoLightMode) {
document.getElementById('outputArea').innerHTML = textInputed;
}
else {
formHandler();
}
}
}
setInterval(realTimeTyping, 1);
</script>
</body>
</html>