diff options
Diffstat (limited to 'vendor/plugins')
19 files changed, 655 insertions, 70 deletions
diff --git a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb index fb6a08979..d5c0e89c6 100644 --- a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb +++ b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb @@ -30,14 +30,19 @@ module ActsAsXapian class NoXapianRubyBindingsError < StandardError end - # XXX global class intializers here get loaded more than once, don't know why. Protect them. - if not $acts_as_xapian_class_var_init - @@db = nil - @@db_path = nil - @@writable_db = nil - @@init_values = [] + @@db = nil + @@db_path = nil + @@writable_db = nil + @@init_values = [] + + # There used to be a problem with this module being loaded more than once. + # Keep a check here, so we can tell if the problem recurs. + if $acts_as_xapian_class_var_init + raise "The acts_as_xapian module has already been loaded" + else $acts_as_xapian_class_var_init = true end + def ActsAsXapian.db @@db end @@ -111,14 +116,17 @@ module ActsAsXapian raise NoXapianRubyBindingsError.new("Xapian Ruby bindings not installed") unless ActsAsXapian.bindings_available raise "acts_as_xapian hasn't been called in any models" if @@init_values.empty? - # if DB is not nil, then we're already initialised, so don't do it again - # XXX we need to reopen the database each time, so Xapian gets changes to it. - # Hopefully in later version of Xapian it will autodetect this, and this can - # be commented back in again. - # return unless @@db.nil? - prepare_environment + # We need to reopen the database each time, so Xapian gets changes to it. + # Calling reopen() does not always pick up changes for reasons that I can + # only speculate about at the moment. (It is easy to reproduce this by + # changing the code below to use reopen() rather than open() followed by + # close(), and running rake spec.) + if !@@db.nil? + @@db.close + end + # basic Xapian objects begin @@db = Xapian::Database.new(@@db_path) @@ -138,6 +146,16 @@ module ActsAsXapian @@query_parser.stemming_strategy = Xapian::QueryParser::STEM_SOME @@query_parser.database = @@db @@query_parser.default_op = Xapian::Query::OP_AND + begin + @@query_parser.set_max_wildcard_expansion(1000) + rescue NoMethodError + # The set_max_wildcard_expansion method was introduced in Xapian 1.2.7, + # so may legitimately not be available. + # + # Large installations of Alaveteli should consider + # upgrading, because uncontrolled wildcard expansion + # can crash the whole server: see http://trac.xapian.org/ticket/350 + end @@stopper = Xapian::SimpleStopper.new @@stopper.add("and") @@ -218,7 +236,8 @@ module ActsAsXapian full_path = @@db_path + suffix # for indexing - @@writable_db = Xapian::flint_open(full_path, Xapian::DB_CREATE_OR_OPEN) + @@writable_db = Xapian::WritableDatabase.new(full_path, Xapian::DB_CREATE_OR_OPEN) + @@enquire = Xapian::Enquire.new(@@writable_db) @@term_generator = Xapian::TermGenerator.new() @@term_generator.set_flags(Xapian::TermGenerator::FLAG_SPELLING, 0) @@term_generator.database = @@writable_db @@ -247,6 +266,8 @@ module ActsAsXapian end end + MSET_MAX_TRIES = 5 + MSET_MAX_DELAY = 5 # Set self.query before calling this def initialize_query(options) #raise options.to_yaml @@ -266,7 +287,7 @@ module ActsAsXapian ActsAsXapian.enquire.sort_by_relevance! else value = ActsAsXapian.values_by_prefix[sort_by_prefix] - raise "couldn't find prefix '" + sort_by_prefix + "'" if value.nil? + raise "couldn't find prefix '" + sort_by_prefix.to_s + "'" if value.nil? ActsAsXapian.enquire.sort_by_value_then_relevance!(value, sort_by_ascending) end if collapse_by_prefix.nil? @@ -277,7 +298,28 @@ module ActsAsXapian ActsAsXapian.enquire.collapse_key = value end - self.matches = ActsAsXapian.enquire.mset(offset, limit, 100) + tries = 0 + delay = 1 + begin + self.matches = ActsAsXapian.enquire.mset(offset, limit, 100) + rescue IOError => e + if e.message =~ /DatabaseModifiedError: / + # This should be a transient error, so back off and try again, up to a point + if tries > MSET_MAX_TRIES + raise "Received DatabaseModifiedError from Xapian even after retrying #{MSET_MAX_TRIES} times" + else + sleep delay + end + tries += 1 + delay *= 2 + delay = MSET_MAX_DELAY if delay > MSET_MAX_DELAY + + ActsAsXapian.db.reopen() + retry + else + raise + end + end self.cached_results = nil } end @@ -391,7 +433,7 @@ module ActsAsXapian # User]. Can take a single model class, or you can express the model # class names in strings if you like. # query_string - user inputed query string, with syntax much like Google Search - def initialize(model_classes, query_string, options = {}) + def initialize(model_classes, query_string, options = {}, user_query = nil) # Check parameters, convert to actual array of model classes new_model_classes = [] model_classes = [model_classes] if model_classes.class != Array @@ -410,10 +452,13 @@ module ActsAsXapian # Construct query which only finds things from specified models model_query = Xapian::Query.new(Xapian::Query::OP_OR, model_classes.map{|mc| "M" + mc.to_s}) - user_query = ActsAsXapian.query_parser.parse_query(self.query_string, - Xapian::QueryParser::FLAG_BOOLEAN | Xapian::QueryParser::FLAG_PHRASE | - Xapian::QueryParser::FLAG_LOVEHATE | Xapian::QueryParser::FLAG_WILDCARD | - Xapian::QueryParser::FLAG_SPELLING_CORRECTION) + if user_query.nil? + user_query = ActsAsXapian.query_parser.parse_query( + self.query_string, + Xapian::QueryParser::FLAG_BOOLEAN | Xapian::QueryParser::FLAG_PHRASE | + Xapian::QueryParser::FLAG_LOVEHATE | + Xapian::QueryParser::FLAG_SPELLING_CORRECTION) + end self.query = Xapian::Query.new(Xapian::Query::OP_AND, model_query, user_query) # Call base class constructor @@ -525,7 +570,6 @@ module ActsAsXapian return if model_classes.size == 0 ActsAsXapian.writable_init - # Abort if full rebuild is going on new_path = ActsAsXapian.db_path + ".new" if File.exist?(new_path) @@ -544,10 +588,11 @@ module ActsAsXapian # was updated a second time by another process. In that case # ActsAsXapianJob.delete_all in xapian_mark_needs_index below # might have removed the first job record while we are working on it. - STDOUT.puts("job with #{id} vanished under foot") if verbose + #STDERR.puts("job with #{id} vanished under foot") if verbose next end STDOUT.puts("ActsAsXapian.update_index #{job.action} #{job.model} #{job.model_id.to_s} #{Time.now.to_s}") if verbose + begin if job.action == 'update' # XXX Index functions may reference other models, so we could eager load here too? @@ -566,47 +611,58 @@ module ActsAsXapian job.action = 'destroy' retry end - job.destroy - if flush ActsAsXapian.writable_db.flush end + job.destroy end rescue => detail # print any error, and carry on so other things are indexed STDERR.puts(detail.backtrace.join("\n") + "\nFAILED ActsAsXapian.update_index job #{id} #{$!} " + (job.nil? ? "" : "model " + job.model + " id " + job.model_id.to_s)) end end - # We close the database when we're finished to remove the lock file. Since writable_init # reopens it and recreates the environment every time we don't need to do further cleanup + ActsAsXapian.writable_db.flush ActsAsXapian.writable_db.close end + def ActsAsXapian._is_xapian_db(path) + is_db = File.exist?(File.join(path, "iamflint")) || File.exist?(File.join(path, "iamchert")) + return is_db + end + # You must specify *all* the models here, this totally rebuilds the Xapian # database. You'll want any readers to reopen the database after this. # # Incremental update_index calls above are suspended while this rebuild # happens (i.e. while the .new database is there) - any index update jobs # are left in the database, and will run after the rebuild has finished. + def ActsAsXapian.rebuild_index(model_classes, verbose = false, terms = true, values = true, texts = true, safe_rebuild = true) #raise "when rebuilding all, please call as first and only thing done in process / task" if not ActsAsXapian.writable_db.nil? - prepare_environment - - # Delete any existing .new database, and open a new one + + update_existing = !(terms == true && values == true && texts == true) + # Delete any existing .new database, and open a new one which is a copy of the current one new_path = ActsAsXapian.db_path + ".new" + old_path = ActsAsXapian.db_path if File.exist?(new_path) - raise "found existing " + new_path + " which is not Xapian flint database, please delete for me" if not File.exist?(File.join(new_path, "iamflint")) + raise "found existing " + new_path + " which is not Xapian flint database, please delete for me" if not ActsAsXapian._is_xapian_db(new_path) FileUtils.rm_r(new_path) end - + if update_existing + FileUtils.cp_r(old_path, new_path) + end + ActsAsXapian.writable_init + ActsAsXapian.writable_db.close # just to make an empty one to read # Index everything if safe_rebuild _rebuild_index_safely(model_classes, verbose, terms, values, texts) else + @@db_path = ActsAsXapian.db_path + ".new" + ActsAsXapian.writable_init # Save time by running the indexing in one go and in-process - ActsAsXapian.writable_init(".new") for model_class in model_classes STDOUT.puts("ActsAsXapian.rebuild_index: Rebuilding #{model_class.to_s}") if verbose model_class.find(:all).each do |model| @@ -614,16 +670,15 @@ module ActsAsXapian model.xapian_index(terms, values, texts) end end - # make sure everything is written and close ActsAsXapian.writable_db.flush ActsAsXapian.writable_db.close end # Rename into place - old_path = ActsAsXapian.db_path - temp_path = ActsAsXapian.db_path + ".tmp" + temp_path = old_path + ".tmp" if File.exist?(temp_path) - raise "temporary database found " + temp_path + " which is not Xapian flint database, please delete for me" if not File.exist?(File.join(temp_path, "iamflint")) + @@db_path = old_path + raise "temporary database found " + temp_path + " which is not Xapian flint database, please delete for me" if not ActsAsXapian._is_xapian_db(temp_path) FileUtils.rm_r(temp_path) end if File.exist?(old_path) @@ -633,12 +688,16 @@ module ActsAsXapian # Delete old database if File.exist?(temp_path) - raise "old database now at " + temp_path + " is not Xapian flint database, please delete for me" if not File.exist?(File.join(temp_path, "iamflint")) + if not ActsAsXapian._is_xapian_db(temp_path) + @@db_path = old_path + raise "old database now at " + temp_path + " is not Xapian flint database, please delete for me" + end FileUtils.rm_r(temp_path) end # You'll want to restart your FastCGI or Mongrel processes after this, # so they get the new db + @@db_path = old_path end def ActsAsXapian._rebuild_index_safely(model_classes, verbose, terms, values, texts) @@ -658,18 +717,18 @@ module ActsAsXapian # database connection doesn't survive a fork, rebuild it ActiveRecord::Base.connection.reconnect! else + # fully reopen the database each time (with a new object) # (so doc ids and so on aren't preserved across the fork) - ActsAsXapian.writable_init(".new") + @@db_path = ActsAsXapian.db_path + ".new" + ActsAsXapian.writable_init STDOUT.puts("ActsAsXapian.rebuild_index: New batch. #{model_class.to_s} from #{i} to #{i + batch_size} of #{model_class_count} pid #{Process.pid.to_s}") if verbose model_class.find(:all, :limit => batch_size, :offset => i, :order => :id).each do |model| STDOUT.puts("ActsAsXapian.rebuild_index #{model_class} #{model.id}") if verbose model.xapian_index(terms, values, texts) end - # make sure everything is written ActsAsXapian.writable_db.flush - # close database - ActsAsXapian.writable_db.close + ActsAsXapian.writable_db.close # database connection won't survive a fork, so shut it down ActiveRecord::Base.connection.disconnect! # brutal exit, so other shutdown code not run (for speed and safety) @@ -739,7 +798,7 @@ module ActsAsXapian # Store record in the Xapian database def xapian_index(terms = true, values = true, texts = true) - # if we have a conditional function for indexing, call it and destory object if failed + # if we have a conditional function for indexing, call it and destroy object if failed if self.class.xapian_options.include?(:if) if_value = xapian_value(self.class.xapian_options[:if], :boolean) if not if_value @@ -748,13 +807,6 @@ module ActsAsXapian end end - if self.class.to_s == "PublicBody" and self.url_name == "tgq" - -#require 'ruby-debug' -#debugger - end - # otherwise (re)write the Xapian record for the object - ActsAsXapian.readable_init existing_query = Xapian::Query.new("I" + self.xapian_document_term) ActsAsXapian.enquire.query = existing_query match = ActsAsXapian.enquire.mset(0,1,1).matches[0] @@ -767,8 +819,8 @@ module ActsAsXapian doc.add_term("M" + self.class.to_s) doc.add_term("I" + doc.data) end - ActsAsXapian.term_generator.document = doc - # work out what to index. XXX for now, this is only selective on "terms". + # work out what to index + # 1. Which terms to index? We allow the user to specify particular ones terms_to_index = [] drop_all_terms = false if terms and self.xapian_options[:terms] @@ -782,16 +834,18 @@ module ActsAsXapian drop_all_terms = true end end + # 2. Texts to index? Currently, it's all or nothing texts_to_index = [] if texts and self.xapian_options[:texts] texts_to_index = self.xapian_options[:texts] end + # 3. Values to index? Currently, it's all or nothing values_to_index = [] if values and self.xapian_options[:values] values_to_index = self.xapian_options[:values] end - # clear any existing values that we might want to replace + # clear any existing data that we might want to replace if drop_all_terms && texts # as an optimisation, if we're reindexing all of both, we remove everything doc.clear_terms @@ -801,17 +855,17 @@ module ActsAsXapian term_prefixes_to_index = terms_to_index.map {|x| x[1]} for existing_term in doc.terms first_letter = existing_term.term[0...1] - if !"MI".include?(first_letter) - if first_letter.match("^[A-Z]+") && terms_to_index.include?(first_letter) - doc.remove_term(existing_term.term) + if !"MI".include?(first_letter) # it's not one of the reserved value + if first_letter.match("^[A-Z]+") # it's a "value" (rather than indexed text) + if term_prefixes_to_index.include?(first_letter) # it's a value that we've been asked to index + doc.remove_term(existing_term.term) + end elsif texts - doc.remove_term(existing_term.term) + doc.remove_term(existing_term.term) # it's text and we've been asked to reindex it end end end end - # for now, we always clear values - doc.clear_values for term in terms_to_index value = xapian_value(term[0]) @@ -823,15 +877,20 @@ module ActsAsXapian doc.add_term(term[1] + value) end end - # values - for value in values_to_index - doc.add_value(value[1], xapian_value(value[0], value[3])) + + if values + doc.clear_values + for value in values_to_index + doc.add_value(value[1], xapian_value(value[0], value[3])) + end end - # texts - for text in texts_to_index - ActsAsXapian.term_generator.increase_termpos # stop phrases spanning different text fields - # XXX the "1" here is a weight that could be varied for a boost function - ActsAsXapian.term_generator.index_text(xapian_value(text, nil, true), 1) + if texts + ActsAsXapian.term_generator.document = doc + for text in texts_to_index + ActsAsXapian.term_generator.increase_termpos # stop phrases spanning different text fields + # XXX the "1" here is a weight that could be varied for a boost function + ActsAsXapian.term_generator.index_text(xapian_value(text, nil, true), 1) + end end ActsAsXapian.writable_db.replace_document("I" + doc.data, doc) diff --git a/vendor/plugins/acts_as_xapian/lib/tasks/xapian.rake b/vendor/plugins/acts_as_xapian/lib/tasks/xapian.rake index d18cd07d5..c1986ce1e 100644 --- a/vendor/plugins/acts_as_xapian/lib/tasks/xapian.rake +++ b/vendor/plugins/acts_as_xapian/lib/tasks/xapian.rake @@ -2,7 +2,6 @@ require 'rubygems' require 'rake' require 'rake/testtask' require 'active_record' -require File.dirname(__FILE__) + '/../acts_as_xapian.rb' namespace :xapian do # Parameters - specify "flush=true" to save changes to the Xapian database @@ -30,12 +29,23 @@ namespace :xapian do desc 'Completely rebuilds Xapian search index (must specify all models)' task :rebuild_index => :environment do + def coerce_arg(arg, default) + if arg == "false" + return false + elsif arg == "true" + return true + elsif arg.nil? + return default + else + return arg + end + end raise "specify ALL your models with models=\"ModelName1 ModelName2\" as parameter" if ENV['models'].nil? ActsAsXapian.rebuild_index(ENV['models'].split(" ").map{|m| m.constantize}, - ENV['verbose'] ? true : false, - ENV['terms'] == "false" ? false : ENV['terms'], - ENV['values'] == "false" ? false : ENV['values'], - ENV['texts'] == "false" ? false : true) + coerce_arg(ENV['verbose'], false), + coerce_arg(ENV['terms'], true), + coerce_arg(ENV['values'], true), + coerce_arg(ENV['texts'], true)) end # Parameters - are models, query, offset, limit, sort_by_prefix, diff --git a/vendor/plugins/exception_notification/README b/vendor/plugins/exception_notification/README new file mode 100644 index 000000000..d5e343630 --- /dev/null +++ b/vendor/plugins/exception_notification/README @@ -0,0 +1,144 @@ += Exception Notifier Plugin for Rails + +The Exception Notifier plugin provides a mailer object and a default set of +templates for sending email notifications when errors occur in a Rails +application. The plugin is configurable, allowing programmers to specify: + +* the sender address of the email +* the recipient addresses +* the text used to prefix the subject line + +The email includes information about the current request, session, and +environment, and also gives a backtrace of the exception. + +== Usage + +First, include the ExceptionNotifiable mixin in whichever controller you want +to generate error emails (typically ApplicationController): + + class ApplicationController < ActionController::Base + include ExceptionNotification::Notifiable + ... + end + +Then, specify the email recipients in your environment: + + ExceptionNotification::Notifier.exception_recipients = %w(joe@schmoe.com bill@schmoe.com) + +And that's it! The defaults take care of the rest. + +== Configuration + +You can tweak other values to your liking, as well. In your environment file, +just set any or all of the following values: + + # defaults to exception.notifier@default.com + ExceptionNotification::Notifier.sender_address = + %("Application Error" <app.error@myapp.com>) + + # defaults to "[ERROR] " + ExceptionNotification::Notifier.email_prefix = "[APP] " + +Even if you have mixed into ApplicationController you can skip notification in +some controllers by + + class MyController < ApplicationController + skip_exception_notifications + end + +== Deprecated local_request? overriding + +Email notifications will only occur when the IP address is determined not to +be local. You can specify certain addresses to always be local so that you'll +get a detailed error instead of the generic error page. You do this in your +controller (or even per-controller): + + consider_local "64.72.18.143", "14.17.21.25" + +You can specify subnet masks as well, so that all matching addresses are +considered local: + + consider_local "64.72.18.143/24" + +The address "127.0.0.1" is always considered local. If you want to completely +reset the list of all addresses (for instance, if you wanted "127.0.0.1" to +NOT be considered local), you can simply do, somewhere in your controller: + + local_addresses.clear + +NOTE: The above functionality has has been pulled out to consider_local.rb, +as interfering with rails local determination is orthogonal to notification, +unnecessarily clutters backtraces, and even occasionally errs on odd ip or +requests bugs. To return original functionality add an initializer with: + + ActionController::Base.send :include, ConsiderLocal + +or just include it per controller that wants it + + class MyController < ApplicationController + include ExceptionNotification::ConsiderLocal + end + +== Customization + +By default, the notification email includes four parts: request, session, +environment, and backtrace (in that order). You can customize how each of those +sections are rendered by placing a partial named for that part in your +app/views/exception_notifier directory (e.g., _session.rhtml). Each partial has +access to the following variables: + +* @controller: the controller that caused the error +* @request: the current request object +* @exception: the exception that was raised +* @host: the name of the host that made the request +* @backtrace: a sanitized version of the exception's backtrace +* @rails_root: a sanitized version of RAILS_ROOT +* @data: a hash of optional data values that were passed to the notifier +* @sections: the array of sections to include in the email + +You can reorder the sections, or exclude sections completely, by altering the +ExceptionNotification::Notifier.sections variable. You can even add new sections that +describe application-specific data--just add the section's name to the list +(whereever you'd like), and define the corresponding partial. Then, if your +new section requires information that isn't available by default, make sure +it is made available to the email using the exception_data macro: + + class ApplicationController < ActionController::Base + ... + protected + exception_data :additional_data + + def additional_data + { :document => @document, + :person => @person } + end + ... + end + +In the above case, @document and @person would be made available to the email +renderer, allowing your new section(s) to access and display them. See the +existing sections defined by the plugin for examples of how to write your own. + +== 404s errors + +Notification is skipped if you return a 404 status, which happens by default +for an ActiveRecord::RecordNotFound or ActionController::UnknownAction error. + +== Manually notifying of error in a rescue block + +If your controller action manually handles an error, the notifier will never be +run. To manually notify of an error call notify_about_exception from within the +rescue block + + def index + #risky operation here + rescue StandardError => error + #custom error handling here + notify_about_exception(error) + end + +== Support and tickets + +https://rails.lighthouseapp.com/projects/8995-rails-plugins + +Copyright (c) 2005 Jamis Buck, released under the MIT license
\ No newline at end of file diff --git a/vendor/plugins/exception_notification/exception_notification.gemspec b/vendor/plugins/exception_notification/exception_notification.gemspec new file mode 100644 index 000000000..b3ff82322 --- /dev/null +++ b/vendor/plugins/exception_notification/exception_notification.gemspec @@ -0,0 +1,11 @@ +Gem::Specification.new do |s| + s.name = 'exception_notification' + s.version = '2.3.3.0' + s.authors = ["Jamis Buck", "Josh Peek", "Tim Connor"] + s.date = %q{2010-03-13} + s.summary = "Exception notification by email for Rails apps - 2.3-stable compatible version" + s.email = "timocratic@gmail.com" + + s.files = ['README'] + Dir['lib/**/*'] + Dir['views/**/*'] + s.require_path = 'lib' +end diff --git a/vendor/plugins/exception_notification/init.rb b/vendor/plugins/exception_notification/init.rb new file mode 100644 index 000000000..ef215f809 --- /dev/null +++ b/vendor/plugins/exception_notification/init.rb @@ -0,0 +1 @@ +require "exception_notification" diff --git a/vendor/plugins/exception_notification/lib/exception_notification.rb b/vendor/plugins/exception_notification/lib/exception_notification.rb new file mode 100644 index 000000000..bf5975201 --- /dev/null +++ b/vendor/plugins/exception_notification/lib/exception_notification.rb @@ -0,0 +1,7 @@ +require "action_mailer" +module ExceptionNotification + autoload :Notifiable, 'exception_notification/notifiable' + autoload :Notifier, 'exception_notification/notifier' + #autoload :NotifierHelper, 'exception_notification/notifier_helper' + autoload :ConsiderLocal, 'exception_notification/consider_local' +end
\ No newline at end of file diff --git a/vendor/plugins/exception_notification/lib/exception_notification/consider_local.rb b/vendor/plugins/exception_notification/lib/exception_notification/consider_local.rb new file mode 100644 index 000000000..6b9e236f7 --- /dev/null +++ b/vendor/plugins/exception_notification/lib/exception_notification/consider_local.rb @@ -0,0 +1,31 @@ +#This didn't belong on ExceptionNotifier and made backtraces worse. To keep original functionality in place +#'ActionController::Base.send :include, ExceptionNotification::ConsiderLocal' or just include in your controller +module ExceptionNotification::ConsiderLocal + def self.included(target) + require 'ipaddr' + target.extend(ClassMethods) + end + + module ClassMethods + def consider_local(*args) + local_addresses.concat(args.flatten.map { |a| IPAddr.new(a) }) + end + + def local_addresses + addresses = read_inheritable_attribute(:local_addresses) + unless addresses + addresses = [IPAddr.new("127.0.0.1")] + write_inheritable_attribute(:local_addresses, addresses) + end + addresses + end + end + +private + + def local_request? + remote = IPAddr.new(request.remote_ip) + !self.class.local_addresses.detect { |addr| addr.include?(remote) }.nil? + end + +end diff --git a/vendor/plugins/exception_notification/lib/exception_notification/notifiable.rb b/vendor/plugins/exception_notification/lib/exception_notification/notifiable.rb new file mode 100644 index 000000000..19895e8db --- /dev/null +++ b/vendor/plugins/exception_notification/lib/exception_notification/notifiable.rb @@ -0,0 +1,66 @@ +# Copyright (c) 2005 Jamis Buck +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +module ExceptionNotification::Notifiable + def self.included(target) + target.extend(ClassMethods) + target.skip_exception_notifications false + end + + module ClassMethods + def exception_data(deliverer=self) + if deliverer == self + read_inheritable_attribute(:exception_data) + else + write_inheritable_attribute(:exception_data, deliverer) + end + end + + def skip_exception_notifications(boolean=true) + write_inheritable_attribute(:skip_exception_notifications, boolean) + end + + def skip_exception_notifications? + read_inheritable_attribute(:skip_exception_notifications) + end + end + +private + + def rescue_action_in_public(exception) + super + notify_about_exception(exception) if deliver_exception_notification? + end + + def deliver_exception_notification? + !self.class.skip_exception_notifications? && ![404, "404 Not Found"].include?(response.status.to_s) + end + + def notify_about_exception(exception) + deliverer = self.class.exception_data + data = case deliverer + when nil then {} + when Symbol then send(deliverer) + when Proc then deliverer.call(self) + end + + ExceptionNotification::Notifier.deliver_exception_notification(exception, self, request, data) + end +end diff --git a/vendor/plugins/exception_notification/lib/exception_notification/notifier.rb b/vendor/plugins/exception_notification/lib/exception_notification/notifier.rb new file mode 100644 index 000000000..2cab3f963 --- /dev/null +++ b/vendor/plugins/exception_notification/lib/exception_notification/notifier.rb @@ -0,0 +1,80 @@ +require 'pathname' + +# Copyright (c) 2005 Jamis Buck +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +class ExceptionNotification::Notifier < ActionMailer::Base + self.mailer_name = 'exception_notifier' + self.view_paths << "#{File.dirname(__FILE__)}/../../views" + + # next line is a hack to fix + # undefined method `find_template' for #<Array:0x000001009cd230> + # after Rails 2.3.8 -> 2.3.11 upgrade + self.view_paths = ActionView::PathSet.new(self.view_paths) unless self.view_paths.respond_to?(:find_template) + + @@sender_address = %("Exception Notifier" <exception.notifier@default.com>) + cattr_accessor :sender_address + + @@exception_recipients = [] + cattr_accessor :exception_recipients + + @@email_prefix = "[ERROR] " + cattr_accessor :email_prefix + + @@sections = %w(request session environment backtrace) + cattr_accessor :sections + + def self.reloadable?() false end + + def exception_notification(exception, controller, request, data={}) + source = self.class.exception_source(controller) + content_type "text/plain" + + subject "#{email_prefix}#{source} (#{exception.class}) #{exception.message.inspect}" + + recipients exception_recipients + from sender_address + + body data.merge({ :controller => controller, :request => request, + :exception => exception, :exception_source => source, :host => (request.env["HTTP_X_FORWARDED_HOST"] || request.env["HTTP_HOST"]), + :backtrace => sanitize_backtrace(exception.backtrace), + :rails_root => rails_root, :data => data, + :sections => sections }) + end + + def self.exception_source(controller) + if controller.respond_to?(:controller_name) + "in #{controller.controller_name}##{controller.action_name}" + else + "outside of a controller" + end + end + +private + + def sanitize_backtrace(trace) + re = Regexp.new(/^#{Regexp.escape(rails_root)}/) + trace.map { |line| Pathname.new(line.gsub(re, "[RAILS_ROOT]")).cleanpath.to_s } + end + + def rails_root + @rails_root ||= Pathname.new(RAILS_ROOT).cleanpath.to_s + end +end diff --git a/vendor/plugins/exception_notification/lib/exception_notification/notifier_helper.rb b/vendor/plugins/exception_notification/lib/exception_notification/notifier_helper.rb new file mode 100644 index 000000000..942e1c527 --- /dev/null +++ b/vendor/plugins/exception_notification/lib/exception_notification/notifier_helper.rb @@ -0,0 +1,67 @@ +require 'pp' + +# Copyright (c) 2005 Jamis Buck +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +module ExceptionNotification::NotifierHelper + PARAM_FILTER_REPLACEMENT = "[FILTERED]" + + def render_section(section) + RAILS_DEFAULT_LOGGER.info("rendering section #{section.inspect}") + summary = render("exception_notifier/#{section}").strip + unless summary.blank? + title = render("exception_notifier/title", :locals => { :title => section }).strip + "#{title}\n\n#{summary.gsub(/^/, " ")}\n\n" + end + end + + def inspect_model_object(model, locals={}) + render('exception_notifier/inspect_model', + :locals => { :inspect_model => model, + :show_instance_variables => true, + :show_attributes => true }.merge(locals)) + end + + def inspect_value(value) + len = 512 + result = object_to_yaml(value).gsub(/\n/, "\n ").strip + result = result[0,len] + "... (#{result.length-len} bytes more)" if result.length > len+20 + result + end + + def object_to_yaml(object) + object.to_yaml.sub(/^---\s*/m, "") + end + + def exclude_raw_post_parameters? + @controller && @controller.respond_to?(:filter_parameters) + end + + def filter_sensitive_post_data_parameters(parameters) + exclude_raw_post_parameters? ? @controller.__send__(:filter_parameters, parameters) : parameters + end + + def filter_sensitive_post_data_from_env(env_key, env_value) + return env_value unless exclude_raw_post_parameters? + return PARAM_FILTER_REPLACEMENT if (env_key =~ /RAW_POST_DATA/i) + return @controller.__send__(:filter_parameters, {env_key => env_value}).values[0] + end + +end diff --git a/vendor/plugins/exception_notification/test/exception_notifier_helper_test.rb b/vendor/plugins/exception_notification/test/exception_notifier_helper_test.rb new file mode 100644 index 000000000..e077f405b --- /dev/null +++ b/vendor/plugins/exception_notification/test/exception_notifier_helper_test.rb @@ -0,0 +1,62 @@ +require 'test_helper' +require 'exception_notification/notifier_helper' + +class ExceptionNotifierHelperTest < Test::Unit::TestCase + + class ExceptionNotifierHelperIncludeTarget + include ExceptionNotification::NotifierHelper + end + + def setup + @helper = ExceptionNotifierHelperIncludeTarget.new + end + + # No controller + + def test_should_not_exclude_raw_post_parameters_if_no_controller + assert !@helper.exclude_raw_post_parameters? + end + + # Controller, no filtering + + class ControllerWithoutFilterParameters; end + + def test_should_not_filter_env_values_for_raw_post_data_keys_if_controller_can_not_filter_parameters + stub_controller(ControllerWithoutFilterParameters.new) + assert @helper.filter_sensitive_post_data_from_env("RAW_POST_DATA", "secret").include?("secret") + end + def test_should_not_exclude_raw_post_parameters_if_controller_can_not_filter_parameters + stub_controller(ControllerWithoutFilterParameters.new) + assert !@helper.exclude_raw_post_parameters? + end + def test_should_return_params_if_controller_can_not_filter_parameters + stub_controller(ControllerWithoutFilterParameters.new) + assert_equal :params, @helper.filter_sensitive_post_data_parameters(:params) + end + + # Controller with filtering + + class ControllerWithFilterParameters + def filter_parameters(params) + { "PARAM" => ExceptionNotification::NotifierHelper::PARAM_FILTER_REPLACEMENT } + end + end + + def test_should_filter_env_values_for_raw_post_data_keys_if_controller_can_filter_parameters + stub_controller(ControllerWithFilterParameters.new) + assert !@helper.filter_sensitive_post_data_from_env("RAW_POST_DATA", "secret").include?("secret") + end + def test_should_exclude_raw_post_parameters_if_controller_can_filter_parameters + stub_controller(ControllerWithFilterParameters.new) + assert @helper.exclude_raw_post_parameters? + end + def test_should_delegate_param_filtering_to_controller_if_controller_can_filter_parameters + stub_controller(ControllerWithFilterParameters.new) + assert_equal({"PARAM" => "[FILTERED]" }, @helper.filter_sensitive_post_data_parameters({"PARAM" => 'secret'})) + end + + private + def stub_controller(controller) + @helper.instance_variable_set(:@controller, controller) + end +end
\ No newline at end of file diff --git a/vendor/plugins/exception_notification/test/test_helper.rb b/vendor/plugins/exception_notification/test/test_helper.rb new file mode 100644 index 000000000..5831a1784 --- /dev/null +++ b/vendor/plugins/exception_notification/test/test_helper.rb @@ -0,0 +1,8 @@ +require 'test/unit' +require 'rubygems' +require 'active_support' + +RAILS_ROOT = '.' unless defined?(RAILS_ROOT) + +$:.unshift File.join(File.dirname(__FILE__), '../lib') +require 'exception_notification'
\ No newline at end of file diff --git a/vendor/plugins/exception_notification/views/exception_notifier/_backtrace.rhtml b/vendor/plugins/exception_notification/views/exception_notifier/_backtrace.rhtml new file mode 100644 index 000000000..7d13ba007 --- /dev/null +++ b/vendor/plugins/exception_notification/views/exception_notifier/_backtrace.rhtml @@ -0,0 +1 @@ +<%= @backtrace.join "\n" %> diff --git a/vendor/plugins/exception_notification/views/exception_notifier/_environment.rhtml b/vendor/plugins/exception_notification/views/exception_notifier/_environment.rhtml new file mode 100644 index 000000000..42dd803f1 --- /dev/null +++ b/vendor/plugins/exception_notification/views/exception_notifier/_environment.rhtml @@ -0,0 +1,7 @@ +<% max = @request.env.keys.max { |a,b| a.length <=> b.length } -%> +<% @request.env.keys.sort.each do |key| -%> +* <%= "%-*s: %s" % [max.length, key, filter_sensitive_post_data_from_env(key, @request.env[key].to_s.strip)] %> +<% end -%> + +* Process: <%= $$ %> +* Server : <%= `hostname -s`.chomp %> diff --git a/vendor/plugins/exception_notification/views/exception_notifier/_inspect_model.rhtml b/vendor/plugins/exception_notification/views/exception_notifier/_inspect_model.rhtml new file mode 100644 index 000000000..e817847e4 --- /dev/null +++ b/vendor/plugins/exception_notification/views/exception_notifier/_inspect_model.rhtml @@ -0,0 +1,16 @@ +<% if show_attributes -%> +[attributes] +<% attrs = inspect_model.attributes -%> +<% max = attrs.keys.max { |a,b| a.length <=> b.length } -%> +<% attrs.keys.sort.each do |attr| -%> +* <%= "%*-s: %s" % [max.length, attr, object_to_yaml(attrs[attr]).gsub(/\n/, "\n ").strip] %> +<% end -%> +<% end -%> + +<% if show_instance_variables -%> +[instance variables] +<% inspect_model.instance_variables.sort.each do |variable| -%> +<%- next if variable == "@attributes" -%> +* <%= variable %>: <%= inspect_value(inspect_model.instance_variable_get(variable)) %> +<% end -%> +<% end -%> diff --git a/vendor/plugins/exception_notification/views/exception_notifier/_request.rhtml b/vendor/plugins/exception_notification/views/exception_notifier/_request.rhtml new file mode 100644 index 000000000..25423093f --- /dev/null +++ b/vendor/plugins/exception_notification/views/exception_notifier/_request.rhtml @@ -0,0 +1,4 @@ +* URL : <%= @request.protocol %><%= @host %><%= @request.request_uri %> +* IP address: <%= @request.env["HTTP_X_FORWARDED_FOR"] || @request.env["REMOTE_ADDR"] %> +* Parameters: <%= filter_sensitive_post_data_parameters(@request.parameters).inspect %> +* Rails root: <%= @rails_root %> diff --git a/vendor/plugins/exception_notification/views/exception_notifier/_session.rhtml b/vendor/plugins/exception_notification/views/exception_notifier/_session.rhtml new file mode 100644 index 000000000..308684885 --- /dev/null +++ b/vendor/plugins/exception_notification/views/exception_notifier/_session.rhtml @@ -0,0 +1,2 @@ +* session id: <%= @request.session_options[:id] %> +* data: <%= @request.session.inspect %>
\ No newline at end of file diff --git a/vendor/plugins/exception_notification/views/exception_notifier/_title.rhtml b/vendor/plugins/exception_notification/views/exception_notifier/_title.rhtml new file mode 100644 index 000000000..1ed5a3f2b --- /dev/null +++ b/vendor/plugins/exception_notification/views/exception_notifier/_title.rhtml @@ -0,0 +1,3 @@ +------------------------------- +<%= title.to_s.humanize %>: +------------------------------- diff --git a/vendor/plugins/exception_notification/views/exception_notifier/exception_notification.rhtml b/vendor/plugins/exception_notification/views/exception_notifier/exception_notification.rhtml new file mode 100644 index 000000000..715c105bf --- /dev/null +++ b/vendor/plugins/exception_notification/views/exception_notifier/exception_notification.rhtml @@ -0,0 +1,6 @@ +A <%= @exception.class %> occurred <%= @exception_source %>: + + <%= @exception.message %> + <%= @backtrace.first %> + +<%= @sections.map { |section| render_section(section) }.join %> |