/
urlapprove.plugin.php
162 lines (142 loc) · 4.94 KB
/
urlapprove.plugin.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
<?php
class URLApprove extends Plugin
{
const DIRECT = 1;
const REDIRECT = 2;
private $fetch_real = false;
public function action_init()
{
$this->load_text_domain('urlapprove');
}
public function filter_plugin_config( $actions, $plugin_id )
{
if ( $plugin_id == $this->plugin_id() ) {
$actions['urlapprove'] = _t( 'Configure' );
}
return $actions;
}
public function action_plugin_ui( $plugin_id, $action )
{
if ( $plugin_id == $this->plugin_id() ) {
$ui = new FormUI( 'urlapprove' );
$ui->append( 'checkbox', 'redirect_default', 'urlapprove__redirect_default', _t( 'Redirect all comment author links by default', 'urlapprove' ) );
$ui->append( 'textarea', 'whitelist', 'urlapprove__whitelist', _t('Whitelist of domains that will not be redirected, one per line (does not auto-approve)') );
$ui->append( 'submit', 'submit', 'Submit' );
$ui->out();
}
}
public function action_admin_header()
{
$script = <<< SCRIPT
//Need to call this on ajax comment reload
\$(function(){ \$('.comments .item:has(.commenter_redirected)').addClass('redirected')});
SCRIPT;
$style = <<< STYLE
#comments .redirected .author { color: red; font-style: italic; }
STYLE;
Stack::add('admin_header_javascript', $script, 'urlapprove', array('jquery', 'admin'));
Stack::add('admin_stylesheet', array($style, 'screen'), 'urlapprove');
}
public function filter_comment_actions($actions, $comment)
{
if($comment->url == '') {
return $actions;
}
if($comment->info->redirecturl == URLApprove::REDIRECT || (!isset($comment->info->redirecturl) && Options::get('urlapprove__redirect_default') == true) ) {
$actions['direct'] = array('url' => 'javascript:itemManage.update(\'direct\','. $comment->id . ');', 'title' => _t('Use Direct link to author URL'), 'label' => _t('Direct Link'));
}
else {
$actions['redirect'] = array('url' => 'javascript:itemManage.update(\'redirect\','. $comment->id . ');', 'title' => _t('Use Redirected link to author URL'), 'label' => _t('Redirect Link'));
}
return $actions;
}
public function filter_admin_comments_action($status_msg, $action, $comments )
{
switch($action) {
case 'direct':
$value = URLApprove::DIRECT;
$status_msg = _t('Comment set to link directly.');
break;
case 'redirect':
$value = URLApprove::REDIRECT;
$status_msg = _t('Comment set to use redirector.');
break;
default: return $status_msg;
}
foreach($comments as $comment) {
$comment->info->redirecturl = $value;
$comment->info->commit();
}
return $status_msg;
}
public function action_comment_insert_before($comment)
{
if($comment->url != '') {
$lastcomment = Comments::get(array('url' => $comment->url, 'limit' => 1, 'orderby'=>'`date` DESC', 'fetch_fn'=>'get_row'));
if($lastcomment instanceof Comment) {
if(isset($lastcomment->info->redirecturl)) {
$comment->info->redirecturl = $lastcomment->info->redirecturl;
}
}
}
}
protected function get_hash($commentid)
{
return substr(md5($commentid . $_SERVER['REMOTE_ADDR'] . Options::get('GUID') . HabariDateTime::date_create()->yday), 0, 6);
}
private function get_whitelist()
{
static $whitelist = null;
if(is_null($whitelist)) {
$whitelist = explode("\n", Options::get('urlapprove__whitelist'));
$whitelist = array_map('trim', $whitelist);
$whitelist = Plugins::filter('urlapprove_whitelist', $whitelist);
}
return $whitelist;
}
public function filter_comment_url_out($value, $comment)
{
$whitelist = $this->get_whitelist();
if(count($whitelist) > 0 && $comment->url != '') {
$comment_url = InputFilter::parse_url($value);
$domain = $comment_url['host'];
if(in_array($domain, $whitelist)) {
return $value;
}
}
if(isset($comment->info->redirecturl)){
if($comment->info->redirecturl == URLApprove::REDIRECT) {
$value = URL::get('comment_url_redirect', array('id' => $comment->id, 'ccode' => $this->get_hash($comment->id)));
}
}
elseif(Options::get('urlapprove__redirect_default') == true) {
$value = URL::get('comment_url_redirect', array('id' => $comment->id, 'ccode' => $this->get_hash($comment->id)));
}
return $value;
}
public function filter_rewrite_rules($rules)
{
$rules[] = new RewriteRule(array(
'name' => 'comment_url_redirect',
'parse_regex' => '/^(?P<id>([0-9]+))\/(?P<ccode>([0-9a-f]+))\/redirect[\/]{0,1}$/i',
'build_str' => '{$id}/{$ccode}/redirect',
'handler' => 'FeedbackHandler',
'action' => 'comment_url_redirect',
'priority' => 7,
'is_active' => 1,
));
return $rules;
}
public function action_handler_comment_url_redirect($handler_vars)
{
$comment = Comment::get($handler_vars['id']);
$hash = $this->get_hash($handler_vars['id']);
if($hash == $handler_vars['ccode']) {
Utils::redirect($comment->url);
exit;
}
header('HTTP/1.1 410 Gone');
exit;
}
}
?>