-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlinkback.php
261 lines (204 loc) · 7.82 KB
/
linkback.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* This script handles linkbacks and pingbacks
*
* @author Brian Moon <brian@moonspot.net>
* @copyright 1997-Present Brian Moon
* @package Wordcraft
* @license http://wordcraft.googlecode.com/files/license.txt
* @link http://wordcraft.googlecode.com/
*
*/
include_once "./include/common.php";
include_once "./include/database.php";
include_once "./include/url.php";
$success = false;
define("WC_LB_SUCCESS", "THANKS");
define("WC_LB_ERROR_NOTFOUND", 32);
define("WC_LB_ERROR_NOSOURCE", 16);
define("WC_LB_ERROR_NOLINK", 17);
define("WC_LB_ERROR_DUP", 48);
define("WC_LB_ERROR_DENIED", 49);
$pingback_error_strings = array(
32 => "Hmm, I don't know of a page on my site that has that URL.",
16 => "Well, I tried, but I could not find the URL that you claim links to me.",
17 => "I checked out that page. It does not look like it links to me.",
48 => "Yeah, I already know about that link. Persistent aren't we?",
49 => "I'm sorry Dave, I'm afraid I can't do that."
);
/**
* Check linkbacks for validity and updates the posts comments
*
* @param $remote The remote URL reported in the pingback
* @param $local The local URL reported in the pingback
* @param $title Optional - the title of the remote page
* @return integer
*
*/
function handle_linkback($remote, $local, $title="") {
global $WC, $WCDATA;
// verify the $local url is on this site
if(strpos($local, $WC["base_url"])===false){
return WC_LB_ERROR_NOTFOUND;
}
// check for regular urls
$q = parse_url($local, PHP_URL_QUERY);
if($q){
parse_str($q);
if(isset($post_id)){
$post = wc_db_get_post($post_id);
}
}
// check for sef urls if no $post
if(empty($post)){
$uri = str_replace($WC["base_url"]."/", "", $local);
if($uri){
$post = wc_db_get_post($uri);
}
}
// if no post found, return error
if(empty($post)){
return WC_LB_ERROR_NOTFOUND;
}
// check that we have not gotten this already
list($comments, $count) = wc_db_get_comments($post["post_id"]);
foreach($comments as $comment){
if($comment["url"] == $remote){
return WC_LB_ERROR_DUP;
}
}
// get the remote page to run some checks
$remote_page = @file_get_contents($remote);
if(empty($remote_page)){
return WC_LB_ERROR_NOSOURCE;
}
// replace any & with & to make the lookup easy
$remote_page = str_replace("&", "&", $remote_page);
// normalize spaces
$remote_page = preg_replace("!\s+!s", " ", $remote_page);
// do a simple check before we go to a lot of trouble
if(strpos($remote_page, $local)===false){
return WC_LB_ERROR_NOLINK;
}
// for deeper inspection, only look at the body of the page
if(!preg_match('!<body[^>]*>.+?</body>!is', $remote_page, $match)){
print_var($match);
return WC_LB_ERROR_NOSOURCE;
}
$remote_body = $match[0];
/**
* We are only going to acknowledge links in plain HTML
*/
// strip html comments
$remote_body = preg_replace('|<!--.+?-->|s', "", $remote_body);
// strip script tags
$remote_body = preg_replace('|<script[^>]*>.+?</script>|is', "", $remote_body);
// now pull out all the anchor tags
if(!preg_match_all('!<a\s+[^>]*>!i', $remote_page, $matches)){
return WC_LB_ERROR_NOLINK;
}
$found = false;
foreach($matches[0] as $anchor){
if(preg_match('!href="(.+?)"|href=\'(.+?)\'|href=([^ >]+)!', $anchor, $match)){
$url = max($match[1], $match[2], $match[3]);
$url = str_replace("&", "&", $url);
if($local==$url){
$remote_body = str_replace($anchor, "[wctag]", $remote_body);
$found = true;
break;
}
}
}
if(!$found){
return WC_LB_ERROR_NOLINK;
}
// ok, lets start making a comment
$title = str_replace(array("http://","https://"), "", $remote);
if(strlen($title) > 50){
$title = substr($title, 0, 47)."...";
}
$excerpt = "Linkback: $remote";
$comment = array(
"post_id" => $post["post_id"],
"name" => $title,
"url" => $remote,
"comment" => $excerpt,
"ip_address" => $_SERVER["REMOTE_ADDR"],
"status" => "APPROVED",
"linkback" => 1
);
$success = wc_db_save_comment($comment);
if($success){
$comment = wc_db_get_comment($success);
// email the comment
if(($WC["email_comment"]=="all") ||
($WC["email_comment"]=="spam" && $comment["status"]=="SPAM")){
$subject = "[".$WC["default_title"]."] Link to $post[subject]";
$body = "There is a new link back to your post \"$post[subject]\"\n";
$body.= wc_get_url("post", array($post["post_id"], $post["uri"]), false)."#comments\n\n";
$body.= "URL : $comment[url]\n";
$body.= "Delete: $WC[base_url]/admin/comment_moderate.php?mode=delete&comment_id=$comment[comment_id]\n";
if($comment["status"]!="SPAM"){
$body.= "Spam: $WC[base_url]/admin/comment_moderate.php?mode=spam&comment_id=$comment[comment_id]\n";
}
if($comment["status"]=="APPROVED"){
$body.= "Hide: $WC[base_url]/admin/comment_moderate.php?mode=hide&comment_id=$comment[comment_id]\n";
} else {
$body.= "Approve: $WC[base_url]/admin/comment_moderate.php?mode=approve&comment_id=$comment[comment_id]\n";
}
$author = wc_db_get_user($post["user_id"]);
mail($author["email"], $subject, $body, "From: $author[email]\r\nReply-To: $author[email]");
}
return WC_LB_SUCCESS;
} else {
return WC_LB_ERROR_DENIED;
}
}
// this is a pingback, look for xml data
$xml = file_get_contents('php://input');
if(!empty($xml)){
if(preg_match('!<methodName>\s*pingback.ping\s*</methodName>!s', $xml) &&
preg_match_all('!<param>\s*<value>\s*<string>\s*(.+?)\s*</string>\s*</value>\s*</param>!s', $xml, $match)){
if(count($match[1])==2){
$sourceURI = $match[1][0];
$targetURI = $match[1][1];
$success = handle_linkback($sourceURI, $targetURI);
if($success==WC_LB_SUCCESS){
$response = '<?xml version="1.0"?>';
$response.= '<methodResponse>';
$response.= '<params>';
$response.= '<param>';
$response.= '<value><string>You are made of awesome and win! Thanks for the link!</string></value>';
$response.= '</param>';
$response.= '</params>';
$response.= '</methodResponse>';
} else {
$response = '<?xml version="1.0"?>';
$response.= '<methodResponse>';
$response.= '<fault>';
$response.= '<value>';
$response.= '<struct>';
$response.= '<member>';
$response.= '<name>faultCode</name>';
$response.= '<value><int>'.$success.'</int></value>';
$response.= '</member>';
$response.= '<member>';
$response.= '<name>faultString</name>';
$response.= '<value><string>'.$pingback_error_strings[$success].'</string></value>';
$response.= '</member>';
$response.= '</struct>';
$response.= '</value>';
$response.= '</fault>';
$response.= '</methodResponse>';
}
header("Content-Type: text/xml");
echo $response;
exit();
}
}
}
if($success===false){
header('HTTP/1.x 400 Bad Request');
header("Status: 400 Bad Request");
}
?>