-
Notifications
You must be signed in to change notification settings - Fork 0
/
translateMessage.php
74 lines (59 loc) · 2.36 KB
/
translateMessage.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
<?php
/**
* Apache License 2.0
* @author Trapenok Victor, [email protected], 89244269357
* I will be glad to new orders for the development of anything.
*
* Skype:Levhav
* 89244269357
*
* https://github.com/Levhav/Star.Comet-Chat
*
*/
/**
* File transfer messages
*
* List of supported languages
* https://tech.yandex.ru/translate/doc/dg/concepts/langs-docpage/
*/
include './config.php';
include './common.php';
$user_id = getUserIdOrDie();
$message_id = (int)$_POST['message_id'];
$language = $_POST['language'];
if(strlen($language) != 2 && !preg_match("#^[a-z][a-z]$#", $language))
{
die("Invalid request data");
}
$result = mysqli_query(StarCometChat::conf()->getDB(), "SELECT * FROM `messages` where id = ".$message_id." and ( from_user_id= ".$user_id." or to_user_id = ".$user_id." ) ");
if(mysqli_errno(StarCometChat::conf()->getDB()) != 0)
{
die("Error code:".mysqli_errno(StarCometChat::conf()->getDB())." ".mysqli_error(StarCometChat::conf()->getDB())."");
}
else if(!mysqli_num_rows($result))
{
die("No access");
}
$msg = mysqli_fetch_assoc($result);
$result = mysqli_query(StarCometChat::conf()->getDB(), "SELECT text, language FROM `messages_translate` where message_id = ".$message_id." and language = '".$language."' ");
if(mysqli_errno(StarCometChat::conf()->getDB()) != 0)
{
die("Error code:".mysqli_errno(StarCometChat::conf()->getDB())." ".mysqli_error(StarCometChat::conf()->getDB())."");
}
else if(mysqli_num_rows($result))
{
$msg = mysqli_fetch_assoc($result);
echo json_encode(array("text" => $msg['text'], "language" => $msg['language'], "translate" => false));
exit();
}
$messageText = preg_replace("/\[\[img=([A-z0-9\._]+)\]\]/usi", "", $msg['message']);
$translate = @file_get_contents("https://translate.yandex.net/api/v1.5/tr.json/translate?key=".getConfArray("yandex_translate_key")."&text=".urlencode($messageText)."&lang=".$language."&options=1");
if(!$translate)
{
die("Translation error");
}
$data = json_decode($translate, true);
mysqli_query(StarCometChat::conf()->getDB(), "INSERT INTO `messages_translate` (`id`, `message_id`, `language`, `text`)"
. " VALUES (NULL, '".$message_id."', '".$language."', '". mysqli_escape_string(StarCometChat::conf()->getDB(), $data['text'][0])."')");
echo json_encode(array("text" => $data['text'][0], "language" => $data['detected']['lang']));