Guest User

Untitled

a guest
Jan 23rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. # Akismet
  2. #
  3. # Author:: David Czarnecki
  4. # Copyright:: Copyright (c) 2005 - David Czarnecki
  5. # License:: BSD
  6. class Akismet
  7.  
  8. require 'net/HTTP'
  9. require 'uri'
  10.  
  11. STANDARD_HEADERS = {
  12. 'User-Agent' => 'Akismet Ruby API/1.0',
  13. 'Content-Type' => 'application/x-www-form-urlencoded'
  14. }
  15.  
  16. # Instance variables
  17. @apiKey
  18. @blog
  19. @verifiedKey
  20. @proxyPort = nil
  21. @proxyHost = nil
  22.  
  23. # Create a new instance of the Akismet class
  24. #
  25. # apiKey
  26. # Your Akismet API key
  27. # blog
  28. # The blog associated with your api key
  29. def initialize(apiKey, blog)
  30. @apiKey = apiKey
  31. @blog = blog
  32. @verifiedKey = false
  33. end
  34.  
  35. # Set proxy information
  36. #
  37. # proxyHost
  38. # Hostname for the proxy to use
  39. # proxyPort
  40. # Port for the proxy
  41. def setProxy(proxyHost, proxyPort)
  42. @proxyPort = proxyPort
  43. @proxyHost = proxyHost
  44. end
  45.  
  46. # Call to check and verify your API key. You may then call the #hasVerifiedKey method to see if your key has been validated.
  47. def verifyAPIKey()
  48. http = Net::HTTP.new('rest.akismet.com', 80, @proxyHost, @proxyPort)
  49. path = '/1.1/verify-key'
  50.  
  51. data="key=#{@apiKey}&blog=#{@blog}"
  52.  
  53. resp, data = http.post(path, data, STANDARD_HEADERS)
  54. @verifiedKey = (data == "valid")
  55. end
  56.  
  57. # Returns <tt>true</tt> if the API key has been verified, <tt>false</tt> otherwise
  58. def hasVerifiedKey()
  59. return @verifiedKey
  60. end
  61.  
  62. # Internal call to Akismet. Prepares the data for posting to the Akismet service.
  63. #
  64. # akismet_function
  65. # The Akismet function that should be called
  66. # user_ip (required)
  67. # IP address of the comment submitter.
  68. # user_agent (required)
  69. # User agent information.
  70. # referrer (note spelling)
  71. # The content of the HTTP_REFERER header should be sent here.
  72. # permalink
  73. # The permanent location of the entry the comment was submitted to.
  74. # comment_type
  75. # May be blank, comment, trackback, pingback, or a made up value like "registration".
  76. # comment_author
  77. # Submitted name with the comment
  78. # comment_author_email
  79. # Submitted email address
  80. # comment_author_url
  81. # Commenter URL.
  82. # comment_content
  83. # The content that was submitted.
  84. # Other server enviroment variables
  85. # 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.
  86. def callAkismet(akismet_function, user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
  87. http = Net::HTTP.new("#{@apiKey}.rest.akismet.com", 80, @proxyHost, @proxyPort)
  88. path = "/1.1/#{akismet_function}"
  89.  
  90. data = "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}"
  91. if (other != nil)
  92. other.each_pair {|key, value| data.concat("&#{key}=#{value}")}
  93. end
  94. data = URI.escape(data)
  95.  
  96. resp, data = http.post(path, data, STANDARD_HEADERS)
  97.  
  98. return (data != "false")
  99. end
  100.  
  101. protected :callAkismet
  102.  
  103. # 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.
  104. #
  105. # user_ip (required)
  106. # IP address of the comment submitter.
  107. # user_agent (required)
  108. # User agent information.
  109. # referrer (note spelling)
  110. # The content of the HTTP_REFERER header should be sent here.
  111. # permalink
  112. # The permanent location of the entry the comment was submitted to.
  113. # comment_type
  114. # May be blank, comment, trackback, pingback, or a made up value like "registration".
  115. # comment_author
  116. # Submitted name with the comment
  117. # comment_author_email
  118. # Submitted email address
  119. # comment_author_url
  120. # Commenter URL.
  121. # comment_content
  122. # The content that was submitted.
  123. # Other server enviroment variables
  124. # 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.
  125. def commentCheck(user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
  126. return callAkismet('comment-check', user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
  127. end
  128.  
  129. # This call is for submitting comments that weren't marked as spam but should have been. It takes identical arguments as comment check.
  130. # The call parameters are the same as for the #commentCheck method.
  131. def submitSpam(user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
  132. callAkismet('submit-spam', user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
  133. end
  134.  
  135. # 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.
  136. # The call parameters are the same as for the #commentCheck method.
  137. def submitHam(user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
  138. callAkismet('submit-ham', user_ip, user_agent, referrer, permalink, comment_type, comment_author, comment_author_email, comment_author_url, comment_content, other)
  139. end
  140. end
Add Comment
Please, Sign In to add comment