forked from Moidea/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.php
143 lines (124 loc) · 3.92 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
<?php
/**
* GoLinks Plugin
*
* @copyright Copyright (c) 2011 DEFE (http://defe.me)
* @license GNU General Public License 2.0
*
*/
class GoLinks_Action extends Typecho_Widget implements Widget_Interface_Do
{
private $db;
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
$this->db = Typecho_Db::get();
}
/**
* 添加新的链接转换
*
*/
public function add(){
$key = $this->request->key;
$key = $key ? $key : Typecho_Common::randString(8);
$target = $this->request->target;
if($target === "" || $target === "http://"){
$this->widget('Widget_Notice')->set(_t('请输入目标链接。'), NULL, 'error');
}
//判断key是否被占用
elseif($this->getTarget($key)){ ;
$this->widget('Widget_Notice')->set(_t('该key已被使用,请更换key值。'), NULL, 'error');
} else {
$links=array(
'key' => $key,
'target' => $this->request->target,
'count' => 0
);
$insertId = $this->db->query($this->db->insert('table.golinks')->rows($links));
}
}
/**
* 修改链接
*
*/
public function edit(){
$target = $this->request->url;
$id = $this->request->id;
if(trim($target) == "" || $target == "http://"){
$this->response->throwJson('error');
}else{
if($id){
$this->db->query($this->db->update('table.golinks')->rows(array('target' => $target))
->where('id = ?', $id));
$this->response->throwJson('success');
}}
}
/**
*删除链接转换
*
* @param int $id
*/
public function del($id){
$this->db->query($this->db->delete('table.golinks')
->where('id = ?', $id));
}
/**
* 链接重定向
*
*/
public function golink()
{
$key = $this->request->key;
$target = $this->getTarget($key);
if( $target){
//增加统计
$count = $this->db->fetchObject($this->db->select('count')
->from('table.golinks')
->where('key = ?', $key))->count;
$count = $count+1;
$this->db->query($this->db->update('table.golinks')
->rows(array('count' => $count))
->where('key = ?', $key));
//设置nofollow属性
$this->response->setHeader('X-Robots-Tag','noindex, nofollow');
//301重定向
$this->response->redirect($target,301);
}else{
throw new Typecho_Widget_Exception(_t('您访问的网页不存在'), 404);
}
}
/**
* 获取目标链接
*
* @param string $key
* @return void
*/
public function getTarget($key){
$target = $this->db->fetchRow($this->db->select('target')
->from('table.golinks')
->where(' key = ?' , $key));
if($target['target']){
return $target['target'];
}else{
return FALSE;
}
}
/**
* 重设自定义链接
*/
public function resetLink(){
$link = $this->request->link;
Helper::removeRoute('go');
Helper::addRoute('go', $link, 'GoLinks_Action', 'golink');
$this->response->throwJson('success');
}
public function action(){
$this->widget('Widget_User')->pass('administrator');
$this->on($this->request->is('add'))->add();
$this->on($this->request->is('edit'))->edit();
$this->on($this->request->is('del'))->del($this->request->del);
$this->on($this->request->is('resetLink'))->resetLink();
$this->response->goBack();
}
}
?>