akismet.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:6k
源码类别:

Ajax

开发平台:

Others

  1. # Akismet
  2. #
  3. # Author:: David Czarnecki
  4. # Copyright:: Copyright (c) 2005 - David Czarnecki
  5. # License:: BSD
  6. #
  7. # Heavily modified for Typo by Scott Laird
  8. #
  9. class Akismet
  10.   require 'net/http'
  11.   require 'uri'
  12.   require 'timeout'
  13.   
  14.   STANDARD_HEADERS = {
  15.     'User-Agent' => "Typo/#{TYPO_VERSION} | Akismet Ruby API/1.0",
  16.     'Content-Type' => 'application/x-www-form-urlencoded'
  17.   }
  18.   
  19.   # Instance variables
  20.   @apiKey
  21.   @blog
  22.   @verifiedKey
  23.   @proxyPort = nil
  24.   @proxyHost = nil
  25.   # Create a new instance of the Akismet class
  26.   #
  27.   # apiKey 
  28.   #   Your Akismet API key
  29.   # blog 
  30.   #   The blog associated with your api key
  31.   def initialize(apiKey, blog)
  32.     @apiKey = apiKey
  33.     @blog = blog
  34.     @verifiedKey = false
  35.   end
  36.   
  37.   # Set proxy information 
  38.   #
  39.   # proxyHost
  40.   #   Hostname for the proxy to use
  41.   # proxyPort
  42.   #   Port for the proxy
  43.   def setProxy(proxyHost, proxyPort) 
  44.     @proxyPort = proxyPort
  45.     @proxyHost = proxyHost
  46.   end
  47.     
  48.   # Call to check and verify your API key. You may then call the #hasVerifiedKey method to see if your key has been validated.
  49.   def verifyAPIKey()
  50.     http = Net::HTTP.new('rest.akismet.com', 80, @proxyHost, @proxyPort)
  51.     path = '/1.1/verify-key'
  52.     
  53.     data="key=#{@apiKey}&blog=#{@blog}"
  54.     
  55.     resp, data = http.post(path, data, STANDARD_HEADERS)
  56.     @verifiedKey = (data == "valid")
  57.   end
  58.  
  59.   # Returns <tt>true</tt> if the API key has been verified, <tt>false</tt> otherwise
  60.   def hasVerifiedKey()
  61.     return @verifiedKey
  62.   end
  63.   
  64.   # Internal call to Akismet. Prepares the data for posting to the Akismet service.
  65.   #
  66.   # akismet_function
  67.   #   The Akismet function that should be called
  68.   # user_ip (required)
  69.   #    IP address of the comment submitter.
  70.   # user_agent (required)
  71.   #    User agent information.
  72.   # referrer (note spelling)
  73.   #    The content of the HTTP_REFERER header should be sent here.
  74.   # permalink
  75.   #    The permanent location of the entry the comment was submitted to.
  76.   # comment_type
  77.   #    May be blank, comment, trackback, pingback, or a made up value like "registration".
  78.   # comment_author
  79.   #    Submitted name with the comment
  80.   # comment_author_email
  81.   #    Submitted email address
  82.   # comment_author_url
  83.   #    Commenter URL.
  84.   # comment_content
  85.   #    The content that was submitted.
  86.   # Other server enviroment variables
  87.   #    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.  
  88.   #options[:user_ip] = user_ip
  89.   #options[:user_agent] = user_agent
  90.   #options[:referrer] = referrer
  91.   #options[:permalink] = permalink
  92.   #options[:comment_type] = comment_type
  93.   #options[:comment_author] = comment_author
  94.   #options[:comment_author_email] = comment_author_email
  95.   #options[:comment_author_url] = comment_author_url
  96.   #options[:comment_content] = comment_content
  97.   
  98.   def callAkismet(akismet_function, options = {})
  99.     result = false
  100.     begin
  101.       Timeout.timeout(5) do
  102.         http = Net::HTTP.new("#{@apiKey}.rest.akismet.com", 80, @proxyHost, @proxyPort)
  103.         path = "/1.1/#{akismet_function}"
  104.     
  105.         options[:blog] = @blog
  106.         params=[]
  107.     
  108.         options.each_key do |key|
  109.           params.push "#{key}=#{CGI.escape(options[key].to_s)}"
  110.         end
  111.     
  112.         data = params.join('&')
  113.         resp, data = http.post(path, data, STANDARD_HEADERS)
  114.     
  115.         unless data == 'true' or data == 'false' or data == ''
  116.           STDERR.puts "AKISMET error: #{data}"
  117.         end
  118.         result = (data == "true" or data == '')
  119.       end
  120.     rescue => err
  121.       STDERR.puts "AKISMET exception: #{err}"
  122.     end
  123.     
  124.     return result
  125.   end
  126.   
  127.   protected :callAkismet
  128.   # 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.
  129.   #
  130.   # user_ip (required)
  131.   #    IP address of the comment submitter.
  132.   # user_agent (required)
  133.   #    User agent information.
  134.   # referrer (note spelling)
  135.   #    The content of the HTTP_REFERER header should be sent here.
  136.   # permalink
  137.   #    The permanent location of the entry the comment was submitted to.
  138.   # comment_type
  139.   #    May be blank, comment, trackback, pingback, or a made up value like "registration".
  140.   # comment_author
  141.   #    Submitted name with the comment
  142.   # comment_author_email
  143.   #    Submitted email address
  144.   # comment_author_url
  145.   #    Commenter URL.
  146.   # comment_content
  147.   #    The content that was submitted.
  148.   # Other server enviroment variables
  149.   #    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.
  150.   def commentCheck(options = {})
  151.     return callAkismet('comment-check', options)
  152.   end
  153.   
  154.   # This call is for submitting comments that weren't marked as spam but should have been. It takes identical arguments as comment check.
  155.   # The call parameters are the same as for the #commentCheck method.
  156.   def submitSpam(options = {})
  157.     callAkismet('submit-spam', options)
  158.   end
  159.   
  160.   # 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.
  161.   # The call parameters are the same as for the #commentCheck method.
  162.   def submitHam(options = {})
  163.     callAkismet('submit-ham', options)
  164.   end
  165. end