forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.php
175 lines (152 loc) · 3.9 KB
/
Action.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
<?php
/**
* 自动生成缩略名操作类
*
* @package AutoSlug
* @author shingchi
* @version 2.1.0
*/
class AutoSlug_Action extends Typecho_Widget implements Widget_Interface_Do
{
/**
* 插件配置
*
* @access private
* @var Typecho_Config
*/
private $_config;
/**
* 构造方法
*
* @access public
* @var void
*/
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
/* 获取插件配置 */
$this->_config = parent::widget('Widget_Options')->plugin('AutoSlug');
}
/**
* 转换为英文或拼音
*
* @access public
* @return void
*/
public function transform()
{
$word = $this->request->filter('strip_tags', 'trim', 'xss')->q;
if (empty($word)) {
return;
}
$result = call_user_func(array($this, $this->_config->mode), $word);
$result = preg_replace('/[[:punct:]]/', '', $result);
$result = str_replace(array(' ', ' '), '-', strtolower(trim($result)));
$message = array('result' => $result);
$this->response->throwJson($message);
}
/**
* 百度翻译
*
* @access public
* @param string $word 待翻译的字符串
* @return string
*/
public function baidu($word)
{
$url = 'http://api.fanyi.baidu.com/api/trans/vip/translate';
$salt = rand(10000, 99999);
$signStr = $this->_config->bdAppid . $word . $salt . $this->_config->bdKey;
$sign = md5($signStr);
$data = array(
'q' => $word,
'from' => 'zh',
'to' => 'en',
'appid' => $this->_config->bdAppid,
'salt' => $salt,
'sign' => $sign
);
$result = $this->translate($url, $data);
if (isset($result['error_code'])) {
return;
}
return $result['trans_result'][0]['dst'];
}
/**
* 有道翻译
*
* @access public
* @param string $word 待翻译的字符串
* @return string
*/
public function youdao($word)
{
$url = 'http://fanyi.youdao.com/openapi.do';
$data = array(
'keyfrom' => $this->_config->ydFrom,
'key' => $this->_config->ydKey,
'type' => 'data',
'doctype' => 'json',
'version' => '1.1',
'q' => $word
);
$result = $this->translate($url, $data);
if ($result['errorCode'] > 0) {
return;
}
return $result['translation'][0];
}
/**
* 谷歌翻译
*
* @access public
* @param string $word 待翻译的字符串
* @return string
*/
public function google($word)
{
require_once 'GoogleTranslate.php';
$trans = new GoogleTranslate();
$result = $trans->translate('zh-CN', 'en', $word);
return $result;
}
/**
* 发送翻译API请求
*
* @access public
* @param string $url 请求地址
* @param array $data 请求参数
* @return array
*/
public function translate($url, $data)
{
$client = Typecho_Http_Client::get();
$client->setData($data)->setTimeout(50)->send($url);
if (200 === $client->getResponseStatus()) {
return Json::decode($client->getResponseBody(), true);
}
}
/**
* 转换成拼音
*
* @access public
* @param string $word 待转换的字符串
* @return string
*/
public function pinyin($word)
{
require_once 'Pinyin.php';
$pinyin = new Pinyin();
return $pinyin->stringToPinyin($word);
}
/**
* 绑定动作
*
* @access public
* @return void
*/
public function action()
{
$this->on($this->request->isAjax())->transform();
}
}