54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace DPTW;
|
||
|
|
||
|
class Dictionary{
|
||
|
|
||
|
public $dptw;
|
||
|
|
||
|
public function __construct($dptw){
|
||
|
|
||
|
$this->dptw = $dptw;
|
||
|
|
||
|
}
|
||
|
|
||
|
public function update(){
|
||
|
|
||
|
$urls = null;
|
||
|
$results = "";
|
||
|
$words = [];
|
||
|
$key = isset($_GET["key"]) ? $_GET["key"] : null;
|
||
|
|
||
|
try{
|
||
|
isset($_GET["urls"]) && ($urls = ($json = json_decode($urls = base64_decode($_GET["urls"]), true)) ? $json : [$urls]);
|
||
|
}catch(Exception $exception){};
|
||
|
|
||
|
!$urls && ($urls = is_array($urls = $this->dptw->settings("dictionary_urls")) ? $urls : [$urls]);
|
||
|
|
||
|
foreach($urls as $url)
|
||
|
$results .= file_get_contents($url);
|
||
|
|
||
|
$results = "<body" . preg_split('/<\/head>[\s]*<body/i', $results)[1];
|
||
|
|
||
|
foreach(["script", "style", "data", "object"] as $tag)
|
||
|
$results = preg_replace('/<' . $tag . '(?!(<\/' . $tag . '>)).+<\/' . $tag . '>/i', "", $results);
|
||
|
|
||
|
$results = preg_replace('/<[^<>]+>/', " ", $results);
|
||
|
|
||
|
foreach(preg_split('/\s+/', $results) as $word)
|
||
|
if(strlen($word) > 1 && !preg_match('/[^a-záéúíóàèùìòäëüïÖâêûîôÁÉÚÍÓÀÈÙÌÒÄËÜÏÖÂÊÛÎÔñçÑÇ]/i', $word) && !in_array($word, $words))
|
||
|
$words[] = $word;
|
||
|
|
||
|
$words = json_encode([
|
||
|
"urls" => $urls,
|
||
|
"words" => $words
|
||
|
], JSON_UNESCAPED_UNICODE);
|
||
|
|
||
|
$key && DPTW::save_file(__DIR__ . "/../Public/data/json/" . $key . ".json", $words);
|
||
|
|
||
|
header("content-type: application/json");
|
||
|
echo $words;
|
||
|
|
||
|
}
|
||
|
|
||
|
};
|