Import Ruty

This commit is contained in:
2024-03-11 00:58:34 +01:00
parent 34a31bb184
commit 985f1ab418
618 changed files with 225414 additions and 0 deletions
@@ -0,0 +1,212 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Spellchecking backend implementation for afterthedeadline services |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
+-----------------------------------------------------------------------+
*/
/**
* Spellchecking backend implementation to work with an After the Deadline service
* See http://www.afterthedeadline.com/ for more information
*
* @package Framework
* @subpackage Utils
*/
class rcube_spellchecker_atd extends rcube_spellchecker_engine
{
const SERVICE_HOST = 'service.afterthedeadline.com';
const SERVICE_PORT = 80;
private $matches = [];
private $content;
private $langhosts = [
'fr' => 'fr.',
'de' => 'de.',
'pt' => 'pt.',
'es' => 'es.',
];
/**
* Return a list of languages supported by this backend
*
* @see rcube_spellchecker_engine::languages()
*/
function languages()
{
$langs = array_values($this->langhosts);
$langs[] = 'en';
return $langs;
}
/**
* Set content and check spelling
*
* @see rcube_spellchecker_engine::check()
*/
function check($text)
{
$this->content = $text;
// spell check uri is configured
$rcube = rcube::get_instance();
$url = $rcube->config->get('spellcheck_uri');
$key = $rcube->config->get('spellcheck_atd_key');
if ($url) {
$a_uri = parse_url($url);
$ssl = ($a_uri['scheme'] == 'https' || $a_uri['scheme'] == 'ssl');
$port = !empty($a_uri['port']) ? $a_uri['port'] : ($ssl ? 443 : 80);
$host = ($ssl ? 'ssl://' : '') . $a_uri['host'];
$path = $a_uri['path'] . (!empty($a_uri['query']) ? '?'.$a_uri['query'] : '') . $this->lang;
}
else {
$host = self::SERVICE_HOST;
$port = self::SERVICE_PORT;
$path = '/checkDocument';
// prefix host for other languages than 'en'
$lang = substr($this->lang, 0, 2);
if (!empty($this->langhosts[$lang])) {
$host = $this->langhosts[$lang] . $host;
}
}
$postdata = 'data=' . urlencode($text);
if (!empty($key)) {
$postdata .= '&key=' . urlencode($key);
}
$response = $headers = '';
$in_header = true;
if ($fp = fsockopen($host, $port, $errno, $errstr, 30)) {
$out = "POST $path HTTP/1.0\r\n";
$out .= "Host: " . str_replace('ssl://', '', $host) . "\r\n";
$out .= "Content-Length: " . strlen($postdata) . "\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= $postdata;
fwrite($fp, $out);
while (!feof($fp)) {
if ($in_header) {
$line = fgets($fp, 512);
$headers .= $line;
if (trim($line) == '') {
$in_header = false;
}
}
else {
$response .= fgets($fp, 1024);
}
}
fclose($fp);
}
// parse HTTP response headers
if (preg_match('!^HTTP/1.\d (\d+)(.+)!', $headers, $m)) {
$http_status = $m[1];
if ($http_status != '200') {
$this->error = 'HTTP ' . $m[1] . $m[2];
}
}
if (!$response) {
$this->error = "Empty result from spelling engine";
}
try {
$result = new SimpleXMLElement($response);
}
catch (Exception $e) {
$this->error = "Unexpected response from server: " . $response;
return [];
}
$matches = [];
foreach ($result->error as $error) {
if (strval($error->type) == 'spelling') {
$word = strval($error->string);
// skip exceptions
if ($this->dictionary->is_exception($word)) {
continue;
}
$prefix = strval($error->precontext);
$start = $prefix ? mb_strpos($text, $prefix) : 0;
$pos = mb_strpos($text, $word, $start);
$len = mb_strlen($word);
$num = 0;
$match = [$word, $pos, $len, null, []];
foreach ($error->suggestions->option as $option) {
$match[4][] = strval($option);
if (++$num == self::MAX_SUGGESTIONS) {
break;
}
}
$matches[] = $match;
}
}
$this->matches = $matches;
return $matches;
}
/**
* Returns suggestions for the specified word
*
* @see rcube_spellchecker_engine::get_words()
*/
function get_suggestions($word)
{
$matches = $word ? $this->check($word) : $this->matches;
if (!empty($matches[0][4])) {
return $matches[0][4];
}
return [];
}
/**
* Returns misspelled words
*
* @see rcube_spellchecker_engine::get_suggestions()
*/
function get_words($text = null)
{
if ($text) {
$matches = $this->check($text);
}
else {
$matches = $this->matches;
$text = $this->content;
}
$result = [];
foreach ($matches as $m) {
$result[] = mb_substr($text, $m[1], $m[2], RCUBE_CHARSET);
}
return $result;
}
}
@@ -0,0 +1,186 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Spellchecking backend implementation to work with Enchant |
+-----------------------------------------------------------------------+
| Author: Aleksander Machniak <machniak@kolabsys.com> |
+-----------------------------------------------------------------------+
*/
/**
* Spellchecking backend implementation to work with Pspell
*
* @package Framework
* @subpackage Utils
*/
class rcube_spellchecker_enchant extends rcube_spellchecker_engine
{
private $enchant_broker;
private $enchant_dictionary;
private $matches = [];
/**
* Return a list of languages supported by this backend
*
* @see rcube_spellchecker_engine::languages()
*/
function languages()
{
$this->init();
if (!$this->enchant_broker) {
return [];
}
$langs = [];
if ($dicts = enchant_broker_list_dicts($this->enchant_broker)) {
foreach ($dicts as $dict) {
$langs[] = preg_replace('/-.*$/', '', $dict['lang_tag']);
}
}
return array_unique($langs);
}
/**
* Initializes Enchant dictionary
*/
private function init()
{
if (!$this->enchant_broker) {
if (!extension_loaded('enchant')) {
$this->error = "Enchant extension not available";
return;
}
$this->enchant_broker = enchant_broker_init();
}
if (!enchant_broker_dict_exists($this->enchant_broker, $this->lang)) {
$this->error = "Unable to load dictionary for selected language using Enchant";
return;
}
$this->enchant_dictionary = enchant_broker_request_dict($this->enchant_broker, $this->lang);
}
/**
* Set content and check spelling
*
* @see rcube_spellchecker_engine::check()
*/
function check($text)
{
$this->init();
if (!$this->enchant_dictionary) {
return [];
}
// tokenize
$text = preg_split($this->separator, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
$diff = 0;
$matches = [];
foreach ($text as $w) {
$word = trim($w[0]);
$pos = $w[1] - $diff;
$len = mb_strlen($word);
if ($this->dictionary->is_exception($word)) {
// skip exceptions
}
else if (!enchant_dict_check($this->enchant_dictionary, $word)) {
$suggestions = enchant_dict_suggest($this->enchant_dictionary, $word);
if (is_array($suggestions) && count($suggestions) > self::MAX_SUGGESTIONS) {
$suggestions = array_slice($suggestions, 0, self::MAX_SUGGESTIONS);
}
$matches[] = [$word, $pos, $len, null, $suggestions];
}
$diff += (strlen($word) - $len);
}
$this->matches = $matches;
return $matches;
}
/**
* Returns suggestions for the specified word
*
* @see rcube_spellchecker_engine::get_words()
*/
function get_suggestions($word)
{
$this->init();
if (!$this->enchant_dictionary) {
return [];
}
$suggestions = enchant_dict_suggest($this->enchant_dictionary, $word);
if (is_array($suggestions) && count($suggestions) > self::MAX_SUGGESTIONS) {
$suggestions = array_slice($suggestions, 0, self::MAX_SUGGESTIONS);
}
return is_array($suggestions) ? $suggestions : [];
}
/**
* Returns misspelled words
*
* @see rcube_spellchecker_engine::get_suggestions()
*/
function get_words($text = null)
{
$result = [];
if ($text) {
// init spellchecker
$this->init();
if (!$this->enchant_dictionary) {
return [];
}
// With Enchant we don't need to get suggestions to return misspelled words
$text = preg_split($this->separator, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
foreach ($text as $w) {
$word = trim($w[0]);
// skip exceptions
if ($this->dictionary->is_exception($word)) {
continue;
}
if (!enchant_dict_check($this->enchant_dictionary, $word)) {
$result[] = $word;
}
}
return $result;
}
foreach ($this->matches as $m) {
$result[] = $m[0];
}
return $result;
}
}
@@ -0,0 +1,91 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Interface class for a spell-checking backend |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
+-----------------------------------------------------------------------+
*/
/**
* Interface class for a spell-checking backend
*
* @package Framework
* @subpackage Utils
*/
abstract class rcube_spellchecker_engine
{
const MAX_SUGGESTIONS = 10;
protected $lang;
protected $error;
protected $dictionary;
protected $options = [];
protected $separator = '/[\s\r\n\t\(\)\/\[\]{}<>\\"]+|[:;?!,\.](?=\W|$)/';
/**
* Default constructor
*/
public function __construct($dict, $lang, $options = [])
{
$this->dictionary = $dict;
$this->lang = $lang;
$this->options = $options;
}
/**
* Return a list of languages supported by this backend
*
* @return array Indexed list of language codes
*/
abstract function languages();
/**
* Set content and check spelling
*
* @param string $text Text content for spellchecking
*
* @return bool True when no misspelling found, otherwise false
*/
abstract function check($text);
/**
* Returns suggestions for the specified word
*
* @param string $word The word
*
* @return array Suggestions list
*/
abstract function get_suggestions($word);
/**
* Returns misspelled words
*
* @param string $text The content for spellchecking. If empty content
* used for check() method will be used.
*
* @return array List of misspelled words
*/
abstract function get_words($text = null);
/**
* Returns error message
*
* @return string Error message
*/
public function error()
{
return $this->error;
}
}
@@ -0,0 +1,170 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Spellchecking backend implementation to work with Googiespell |
+-----------------------------------------------------------------------+
| Author: Aleksander Machniak <machniak@kolabsys.com> |
| Author: Thomas Bruederli <roundcube@gmail.com> |
+-----------------------------------------------------------------------+
*/
/**
* Spellchecking backend implementation to work with a Googiespell service
*
* @package Framework
* @subpackage Utils
*/
class rcube_spellchecker_googie extends rcube_spellchecker_engine
{
const GOOGIE_HOST = 'https://spell.roundcube.net';
private $matches = [];
private $content;
/**
* Return a list of languages supported by this backend
*
* @see rcube_spellchecker_engine::languages()
*/
function languages()
{
return [
'am','ar','ar','bg','br','ca','cs','cy','da',
'de_CH','de_DE','el','en_GB','en_US',
'eo','es','et','eu','fa','fi','fr_FR','ga','gl','gl',
'he','hr','hu','hy','is','it','ku','lt','lv','nl',
'pl','pt_BR','pt_PT','ro','ru',
'sk','sl','sv','uk'
];
}
/**
* Set content and check spelling
*
* @see rcube_spellchecker_engine::check()
*/
function check($text)
{
$this->content = $text;
$matches = [];
if (empty($text)) {
return $this->matches = $matches;
}
$rcube = rcube::get_instance();
$client = $rcube->get_http_client();
// spell check uri is configured
$url = $rcube->config->get('spellcheck_uri');
if (!$url) {
$url = self::GOOGIE_HOST . '/tbproxy/spell?lang=';
}
$url .= $this->lang;
$url .= sprintf('&key=%06d', !empty($_SESSION['user_id']) ? $_SESSION['user_id'] : 0);
$gtext = '<?xml version="1.0" encoding="utf-8" ?>'
.'<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1">'
.'<text>' . htmlspecialchars($text, ENT_QUOTES, RCUBE_CHARSET) . '</text>'
.'</spellrequest>';
try {
$response = $client->post($url, [
'connect_timeout' => 5, // seconds
'headers' => [
'User-Agent' => "Roundcube Webmail/" . RCUBE_VERSION . " (Googiespell Wrapper)",
'Content-type' => 'text/xml'
],
'body' => $gtext
]
);
}
catch (Exception $e) {
// Do nothing, the error set below should be logged by the caller
}
if (empty($response)) {
$this->error = $e ? $e->getMessage() : "Spelling engine failure";
}
else if ($response->getStatusCode() != 200) {
$this->error = 'HTTP ' . $response->getReasonPhrase();
}
else {
$response_body = $response->getBody();
if (preg_match('/<spellresult error="([^"]+)"/', $response_body, $m) && $m[1]) {
$this->error = "Error code $m[1] returned";
$this->error .= preg_match('/<errortext>([^<]+)/', $response_body, $m) ? ": " . html_entity_decode($m[1]) : '';
}
preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $response_body, $matches, PREG_SET_ORDER);
// skip exceptions (if appropriate options are enabled)
foreach ($matches as $idx => $m) {
$word = mb_substr($text, $m[1], $m[2], RCUBE_CHARSET);
// skip exceptions
if ($this->dictionary->is_exception($word)) {
unset($matches[$idx]);
}
}
}
return $this->matches = $matches;
}
/**
* Returns suggestions for the specified word
*
* @see rcube_spellchecker_engine::get_words()
*/
function get_suggestions($word)
{
$matches = $word ? $this->check($word) : $this->matches;
if (!empty($matches[0][4])) {
$suggestions = explode("\t", $matches[0][4]);
if (count($suggestions) > self::MAX_SUGGESTIONS) {
$suggestions = array_slice($suggestions, 0, self::MAX_SUGGESTIONS);
}
return $suggestions;
}
return [];
}
/**
* Returns misspelled words
*
* @see rcube_spellchecker_engine::get_suggestions()
*/
function get_words($text = null)
{
if ($text) {
$matches = $this->check($text);
}
else {
$matches = $this->matches;
$text = $this->content;
}
$result = [];
foreach ($matches as $m) {
$result[] = mb_substr($text, $m[1], $m[2], RCUBE_CHARSET);
}
return $result;
}
}
@@ -0,0 +1,191 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Spellchecking backend implementation to work with Pspell |
+-----------------------------------------------------------------------+
| Author: Aleksander Machniak <machniak@kolabsys.com> |
| Author: Thomas Bruederli <roundcube@gmail.com> |
+-----------------------------------------------------------------------+
*/
/**
* Spellchecking backend implementation to work with Pspell
*
* @package Framework
* @subpackage Utils
*/
class rcube_spellchecker_pspell extends rcube_spellchecker_engine
{
private $plink;
private $matches = [];
/**
* Return a list of languages supported by this backend
*
* @see rcube_spellchecker_engine::languages()
*/
function languages()
{
$defaults = ['en'];
$langs = [];
// get aspell dictionaries
exec('aspell dump dicts', $dicts);
if (!empty($dicts)) {
$seen = [];
foreach ($dicts as $lang) {
$lang = preg_replace('/-.*$/', '', $lang);
$langc = strlen($lang) == 2 ? $lang.'_'.strtoupper($lang) : $lang;
if (empty($seen[$langc])) {
$langs[] = $lang;
$seen[$langc] = true;
}
}
$langs = array_unique($langs);
}
else {
$langs = $defaults;
}
return $langs;
}
/**
* Initializes PSpell dictionary
*/
private function init()
{
if (!$this->plink) {
if (!extension_loaded('pspell')) {
$this->error = "Pspell extension not available";
return;
}
$this->plink = pspell_new($this->lang, '', '', RCUBE_CHARSET, PSPELL_FAST);
}
if (!$this->plink) {
$this->error = "Unable to load Pspell engine for selected language";
}
}
/**
* Set content and check spelling
*
* @see rcube_spellchecker_engine::check()
*/
function check($text)
{
$this->init();
if (!$this->plink) {
return [];
}
// tokenize
$text = preg_split($this->separator, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
$diff = 0;
$matches = [];
foreach ($text as $w) {
$word = trim($w[0]);
$pos = $w[1] - $diff;
$len = mb_strlen($word);
if ($this->dictionary->is_exception($word)) {
// skip exceptions
}
else if (!pspell_check($this->plink, $word)) {
$suggestions = pspell_suggest($this->plink, $word);
if (count($suggestions) > self::MAX_SUGGESTIONS) {
$suggestions = array_slice($suggestions, 0, self::MAX_SUGGESTIONS);
}
$matches[] = [$word, $pos, $len, null, $suggestions];
}
$diff += (strlen($word) - $len);
}
return $this->matches = $matches;
}
/**
* Returns suggestions for the specified word
*
* @see rcube_spellchecker_engine::get_words()
*/
function get_suggestions($word)
{
$this->init();
if (!$this->plink) {
return [];
}
$suggestions = pspell_suggest($this->plink, $word);
if (count($suggestions) > self::MAX_SUGGESTIONS) {
$suggestions = array_slice($suggestions, 0, self::MAX_SUGGESTIONS);
}
return $suggestions ?: [];
}
/**
* Returns misspelled words
*
* @see rcube_spellchecker_engine::get_suggestions()
*/
function get_words($text = null)
{
$result = [];
if ($text) {
// init spellchecker
$this->init();
if (!$this->plink) {
return [];
}
// With PSpell we don't need to get suggestions to return misspelled words
$text = preg_split($this->separator, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
foreach ($text as $w) {
$word = trim($w[0]);
// skip exceptions
if ($this->dictionary->is_exception($word)) {
continue;
}
if (!pspell_check($this->plink, $word)) {
$result[] = $word;
}
}
return $result;
}
foreach ($this->matches as $m) {
$result[] = $m[0];
}
return $result;
}
}