forked from ryenski/radiant-comments
-
Notifications
You must be signed in to change notification settings - Fork 2
/
akismet.rb
134 lines (118 loc) · 6 KB
/
akismet.rb
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
# Akismet
#
# Author:: David Czarnecki
# Copyright:: Copyright (c) 2005 - David Czarnecki
# License:: BSD
# Modified by Dieter Komendera, Sparkling Studios:
# append blog= to data string (Akismet said it is required)
# changed require 'net/HTTP' to require 'net/http' (to work for me unter GNU/Linux)
# Akismet Key - 4ebc7e35c512
class Akismet
require 'net/http'
require 'uri'
STANDARD_HEADERS = {
'User-Agent' => 'Akismet Ruby API/1.0',
'Content-Type' => 'application/x-www-form-urlencoded'
}
# Create a new instance of the Akismet class
#
# apiKey
# Your Akismet API key
# blog
# The blog associated with your api key
def initialize(apiKey, blog)
@apiKey = apiKey
@blog = blog
end
# Set proxy information
#
# proxyHost
# Hostname for the proxy to use
# proxyPort
# Port for the proxy
def setProxy(proxyHost, proxyPort)
@proxyPort = proxyPort
@proxyHost = proxyHost
end
def valid?
(!@apiKey.blank? && !@blog.blank? && verifyAPIKey)
end
# Call to check and verify your API key.
def verifyAPIKey
http = Net::HTTP.new('rest.akismet.com', 80, @proxyHost, @proxyPort)
path = '/1.1/verify-key'
data="key=#{@apiKey}&blog=#{@blog}"
resp, data = http.post(path, data, STANDARD_HEADERS)
(data == "valid")
end
# Internal call to Akismet. Prepares the data for posting to the Akismet service.
#
# akismet_function
# The Akismet function that should be called
# user_ip (required)
# IP address of the comment submitter.
# user_agent (required)
# User agent information.
# referrer (note spelling)
# The content of the HTTP_REFERER header should be sent here.
# permalink
# The permanent location of the entry the comment was submitted to.
# comment_type
# May be blank, comment, trackback, pingback, or a made up value like "registration".
# comment_author
# Submitted name with the comment
# comment_author_email
# Submitted email address
# comment_author_url
# Commenter URL.
# comment_content
# The content that was submitted.
# Other server enviroment variables
# In PHP there is an array of enviroment variables called $_SERVER which contains information about the web server itself as well as a key/value for every HTTP header sent with the request. This data is highly useful to Akismet as how the submited content interacts with the server can be very telling, so please include as much information as possible.
def callAkismet(akismet_function, user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
http = Net::HTTP.new("#{@apiKey}.rest.akismet.com", 80, @proxyHost, @proxyPort)
path = "/1.1/#{akismet_function}"
data = "blog=#{@blog}&user_ip=#{user_ip}&user_agent=#{user_agent}&referrer=#{referrer}&permalink=#{permalink}&comment_type=#{comment_type}&comment_author=#{comment_author}&comment_author_email=#{comment_author_email}&comment_author_url=#{comment_author_url}&comment_content=#{comment_content}"
if (other != nil)
other.each_pair {|key, value| data.concat("&#{key}=#{value}")}
end
resp, data = http.post(path, data, STANDARD_HEADERS)
return (data != "false")
end
protected :callAkismet
# This is basically the core of everything. This call takes a number of arguments and characteristics about the submitted content and then returns a thumbs up or thumbs down. Almost everything is optional, but performance can drop dramatically if you exclude certain elements.
#
# user_ip (required)
# IP address of the comment submitter.
# user_agent (required)
# User agent information.
# referrer (note spelling)
# The content of the HTTP_REFERER header should be sent here.
# permalink
# The permanent location of the entry the comment was submitted to.
# comment_type
# May be blank, comment, trackback, pingback, or a made up value like "registration".
# comment_author
# Submitted name with the comment
# comment_author_email
# Submitted email address
# comment_author_url
# Commenter URL.
# comment_content
# The content that was submitted.
# Other server enviroment variables
# In PHP there is an array of enviroment variables called $_SERVER which contains information about the web server itself as well as a key/value for every HTTP header sent with the request. This data is highly useful to Akismet as how the submited content interacts with the server can be very telling, so please include as much information as possible.
def commentCheck(user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
return callAkismet('comment-check', user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
end
# This call is for submitting comments that weren't marked as spam but should have been. It takes identical arguments as comment check.
# The call parameters are the same as for the #commentCheck method.
def submitSpam(user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
callAkismet('submit-spam', user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
end
# This call is intended for the marking of false positives, things that were incorrectly marked as spam. It takes identical arguments as comment check and submit spam.
# The call parameters are the same as for the #commentCheck method.
def submitHam(user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
callAkismet('submit-ham', user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
end
end