diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 08a6ab72..779fefc5 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -22,6 +22,7 @@ //= require sweet-alert/sweet-alert //= require jquery.cookie //= require jquery.deparam +//= require jquery_md5 //= require highlightjs //= require chartkick //= require highcharts diff --git a/app/assets/javascripts/pages/errors.js b/app/assets/javascripts/pages/errors.js index 38e9773d..4dcedd6e 100644 --- a/app/assets/javascripts/pages/errors.js +++ b/app/assets/javascripts/pages/errors.js @@ -58,4 +58,18 @@ $(function () { return false } }); + + $(document).ready(function() { + $('#broadcast-form').on("ajax:success", function(e, data, status, xhr) { + if (data.length && data.length > 0){ + $('.subscriber-count h5').text("This message will be sent to " + data.length + " " + (data.length > 1 ? "users" : "user")) + $('.subscriber-icons').empty() + for (i = 0; i < data.length; i++) { + $('.subscriber-icons').append("") + } + } + }).on("ajax:error", function(e, xhr, status, error) { + console.log('failed syncing') + }); + }); }); \ No newline at end of file diff --git a/app/assets/stylesheets/pages/_error.scss b/app/assets/stylesheets/pages/_error.scss index e7615775..693e410a 100644 --- a/app/assets/stylesheets/pages/_error.scss +++ b/app/assets/stylesheets/pages/_error.scss @@ -141,6 +141,10 @@ .broadcast{ .subscribers{ + .subscriber-icons{ + float: left; + } + img{ border-radius:50%; overflow:hidden; diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index 5e37cf2a..28464a0b 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -51,22 +51,37 @@ def update end def notify_subscribers - unless params[:message].blank? - return redirect_to error_path(@error), flash: { error: 'Message too short!' } if params[:message].length < 10 - if params[:intercom] - begin - current_website.intercom_integration.driver.send_message(@error.subscribers.pluck(:email), params[:message]) + #true when form has been submitted + if params[:notify_submit] + unless params[:message].blank? + return redirect_to error_path(@error), flash: { error: 'Message too short!' } if params[:message].length < 10 + if params[:subscriber_type][:intercom] == 'true' + begin + current_website.intercom_integration.driver.send_message(@error.subscribers.pluck(:email), params[:message]) + redirect_to error_path(@error), flash: { success: 'Message successfully sent!' } + rescue => e + Raven.capture_exception(e) + redirect_to error_path(@error), flash: { error: 'Operation failed!' } + end + else + @message = Message.create( content: params[:message], issue_id: @error.issues.last.id ) + @error.subscribers.each do |subscriber| + GroupedIssueMailer.notify_subscriber(@error, subscriber, current_user, @message).deliver_later + end redirect_to error_path(@error), flash: { success: 'Message successfully sent!' } - rescue => e - Raven.capture_exception(e) - redirect_to error_path(@error), flash: { error: 'Operation failed!' } end else - @message = Message.create( content: params[:message], issue_id: @error.issues.last.id ) - @error.subscribers.each do |subscriber| - GroupedIssueMailer.notify_subscriber(@error, subscriber, current_user, @message).deliver_later - end - redirect_to error_path(@error), flash: { success: 'Message successfully sent!' } + redirect_to error_path(@error), flash: { error: 'Message cannot be empty!', status: 403 } + end + #syncing the subscribers + elsif params[:subscriber_type] + if params[:subscriber_type][:intercom] == 'true' + intercom_subscribers = current_website.intercom_integration.driver.list_subscribers + common_subscribers = intercom_subscribers.map{ |x| x[:email] } & @error.subscribers.pluck(:email) + render json: common_subscribers + elsif params[:subscriber_type][:intercom] == 'false' + subscribers = @error.subscribers.pluck(:email) + render json: subscribers end end end diff --git a/app/controllers/integrations_controller.rb b/app/controllers/integrations_controller.rb index b5d83b65..c757f02a 100644 --- a/app/controllers/integrations_controller.rb +++ b/app/controllers/integrations_controller.rb @@ -2,6 +2,9 @@ class IntegrationsController < ApplicationController load_and_authorize_resource def update + if integration_params[:app_name] && params[:app_list] + integration_params[:app_owner] = params[:app_list].select { |k,v| v.include?(integration_params[:app_name]) }.keys[0] + end if @integration.update_attributes(integration_params) redirect_to settings_path(main_tab: 'integrations', integration_tab: @integration.provider), notice: 'Integration updated!' end @@ -24,6 +27,6 @@ def create_task end def integration_params - @integration_params ||= params.require(:integration).permit(:application) + @integration_params ||= params.require(:integration).permit(:app_name, :app_owner) end end diff --git a/app/mailers/grouped_issue_mailer.rb b/app/mailers/grouped_issue_mailer.rb index 3005ff8b..d0c6641e 100644 --- a/app/mailers/grouped_issue_mailer.rb +++ b/app/mailers/grouped_issue_mailer.rb @@ -25,14 +25,14 @@ def more_than_10_errors(member) def notify_daily(user, websites) date = Time.now - 1.day - user = User.find(user) + user = User.find(user.id) @data = find_grouped_issues(websites, date) mail to: user.email, subject: "Daily report email provides you some information about changes on your website." end def notify_weekly(user, websites) date = Time.now - 1.week - user = User.find(user) + user = User.find(user.id) @data = find_grouped_issues(websites, date) @days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] @@ -49,8 +49,8 @@ def notify_weekly(user, websites) def find_grouped_issues(websites, date) data = [] - websites.each do |website_id| - website = Website.find(website_id) + websites.each do |website| + website = Website.find(website.id) issues = website.grouped_issues.where('updated_at > ? AND muted = ?', date, false) data.push( { title: website.title, domain: website.domain, platform: website.platform, created_at: website.created_at, issues: issues } ) end diff --git a/app/models/integration.rb b/app/models/integration.rb index 9a3859fb..ae8f1835 100644 --- a/app/models/integration.rb +++ b/app/models/integration.rb @@ -6,16 +6,17 @@ class Integration < ActiveRecord::Base validates :name, :provider, presence: true store_accessor :configuration - attr_accessor :application + attr_accessor :app_name, :app_owner - before_update :select_application, if: -> { application } + before_update :select_application, if: -> { app_name && app_owner } def driver Integrations.create(self) end def select_application - self.configuration["selected_application"] = application + self.configuration["selected_application"] = app_name + self.configuration["application_owner"] = app_owner end def assign_configuration(auth_hash) diff --git a/app/models/website.rb b/app/models/website.rb index 87698a29..4b169b5d 100644 --- a/app/models/website.rb +++ b/app/models/website.rb @@ -35,6 +35,10 @@ class Website < ActiveRecord::Base define_method("#{integration.to_s}_integration") do self.integrations.find_by(provider: integration) end + + define_method("#{integration.to_s}_configuration") do + self.integrations.find_by(provider: integration).try(:configuration) + end EOF end diff --git a/app/views/errors/_overview.html.haml b/app/views/errors/_overview.html.haml index 817386cd..a7da5b9f 100644 --- a/app/views/errors/_overview.html.haml +++ b/app/views/errors/_overview.html.haml @@ -75,23 +75,25 @@ Broadcast .panel-body .subscribers - = form_for @error, url: notify_subscribers_error_path(), remote: true, html: { method: :post , class: ' broadcast-textarea'}, :data => {:confirm => 'Are you sure?'} do |f| + = form_for @error, url: notify_subscribers_error_path(), remote: true, html: { method: :post , class: 'broadcast-textarea', id: 'broadcast-form'} do |f| = render partial: 'shared/flash_messages' - - subscribers.each do |subscriber| - = image_tag subscriber.avatar_url(30) - - if subscribers.size > 10 - = " and #{subscribers.size - 10} more" - - break - - if subscribers.size > 0 - %h5.pull-right - = t('subscribers.count', count: subscribers.size) + .subscriber-icons + - subscribers.each do |subscriber| + = image_tag subscriber.avatar_url(30) + - if subscribers.size > 10 + = " and #{subscribers.size - 10} more" + - break + .subscriber-count + - if subscribers.size > 0 + %h5.pull-right + = t('subscribers.count', count: subscribers.size) .notify = text_area_tag 'message', '', placeholder: 'Write a message ...' - if current_website.intercom_integration? .intercom-check.pull-left %label.intercom-message - = check_box_tag 'intercom' + = check_box 'subscriber_type', 'intercom', { onclick: "$('#broadcast-form').submit()", id: 'intercom-check' }, 'true', 'false' Send with Intercom - %button.btn.btn-primary{:type => 'submit'} + = button_to '', name: 'notify_submit', value: 'true', class: 'btn btn-primary' do %span.fa.fa-paper-plane Send \ No newline at end of file diff --git a/app/views/errors/show.html.haml b/app/views/errors/show.html.haml index 39c34abf..2885b2d6 100644 --- a/app/views/errors/show.html.haml +++ b/app/views/errors/show.html.haml @@ -18,7 +18,7 @@ %li %span.label.badge{:title => 'Checksum'}= @error.checksum .col-xs-5.text-right - - if @error.website.github_integration? + - if @error.website.github_configuration.try(:[], 'selected_application') .btn-group = button_to 'Create Github Issue', create_task_integration_path(@error.website.github_integration, error_id: @error.id, title: @error.message), class: "btn pull-right btn-primary " .btn-group diff --git a/app/views/installations/header_tabs/providers/_github.html.haml b/app/views/installations/header_tabs/providers/_github.html.haml index 9f87ad97..84349733 100644 --- a/app/views/installations/header_tabs/providers/_github.html.haml +++ b/app/views/installations/header_tabs/providers/_github.html.haml @@ -1,6 +1,7 @@ - if @integration.selected_application.nil? - = form_tag integration_path(@integration), :method => 'put' do - = select_tag "integration[application]", grouped_options_for_select(@integration.get_applications), { class: "pull-left selectpicker" } + - applications = @integration.get_applications + = form_tag integration_path(@integration, 'app_list' => applications), :method => 'put' do + = select_tag 'integration[app_name]', grouped_options_for_select(applications), { class: "pull-left selectpicker" } = submit_tag "Submit", { class: "pull-right col-sm-4 btn btn-default btn-primary"} - else = 'Your repository is ' + @integration.selected_application \ No newline at end of file diff --git a/lib/integrations/base_driver.rb b/lib/integrations/base_driver.rb index 600f9b42..ced80206 100644 --- a/lib/integrations/base_driver.rb +++ b/lib/integrations/base_driver.rb @@ -53,11 +53,15 @@ def build_configuration(auth_hash) end def get_request(url) - RestClient.get url, header rescue 'Operation failed!' + RestClient.get url, header + rescue => e + e.message end def post_request(url, data) - RestClient.post url, data.to_json, header rescue 'Operation failed!' + RestClient.post url, data.to_json, header + rescue => e + e.message end end end diff --git a/lib/integrations/drivers/github.rb b/lib/integrations/drivers/github.rb index 5e9dd53e..150b306f 100644 --- a/lib/integrations/drivers/github.rb +++ b/lib/integrations/drivers/github.rb @@ -14,7 +14,7 @@ def api_url end def create_task(title) - url = api_url + 'repos/' + self.configuration["username"].to_s + '/' + self.configuration["selected_application"].to_s + '/issues' + url = api_url + 'repos/' + self.configuration["application_owner"].to_s + '/' + self.configuration["selected_application"].to_s + '/issues' data = { title: title } response = post_request(url, data) if response diff --git a/spec/controllers/errors_controller_spec.rb b/spec/controllers/errors_controller_spec.rb index f830b52c..f4ad5c52 100644 --- a/spec/controllers/errors_controller_spec.rb +++ b/spec/controllers/errors_controller_spec.rb @@ -13,81 +13,83 @@ context 'if logged in' do let(:params) { { message: 'message that is going to create record', id: group.id, format: :js } } - context 'email' do - it 'should email subscribers' do - mailer = double('GroupedIssueMailer') - expect(mailer).to receive(:deliver_later) - expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, subscriber, user, an_instance_of(Message)).and_return(mailer).once - - post_with user, :notify_subscribers, params - end - - it 'should email 2 subscribers' do - subscriber2 = create :subscriber, website: website - issue2 = create :issue, subscriber: subscriber2, group: group - mailer = double('GroupedIssueMailer') - expect(mailer).to receive(:deliver_later).twice - expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, an_instance_of(Subscriber), an_instance_of(User), an_instance_of(Message)).and_return(mailer).twice - post_with user, :notify_subscribers, params - end - - it 'assigns message' do - post_with user, :notify_subscribers, params - expect(subject.instance_variable_get(:@message)).to be_an_instance_of(Message) - end - - it 'redirects to the error path with success message' do - post_with user, :notify_subscribers, params - expect(response).to redirect_to(error_path(group.slug)) - expect(flash[:success]).to eq('Message successfully sent!') - end - end + context 'form has been submitted (notify_submit is true)' do + context 'email' do + it 'should email subscribers' do + mailer = double('GroupedIssueMailer') + expect(mailer).to receive(:deliver_later) + expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, subscriber, user, an_instance_of(Message)).and_return(mailer).once + + post_with user, :notify_subscribers, params + end - context 'intercom' do - let!(:integration) { create :integration, website: website, provider: :intercom } - let(:driver) { Integrations.get_driver(integration.provider.to_sym) } - let(:integration_driver) { driver.new(Integrations::Integration.new(integration, driver)) } - before(:each) do session[:epiclogger_website_id] = website.id end - let(:intercom_params) { params.merge( intercom: true ) } + it 'should email 2 subscribers' do + subscriber2 = create :subscriber, website: website + issue2 = create :issue, subscriber: subscriber2, group: group + mailer = double('GroupedIssueMailer') + expect(mailer).to receive(:deliver_later).twice + expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, an_instance_of(Subscriber), an_instance_of(User), an_instance_of(Message)).and_return(mailer).twice + post_with user, :notify_subscribers, params + end - context 'success' do - it 'calls the send_message method in the driver' do - expect_any_instance_of(Integrations::Drivers::Intercom).to receive(:send_message) - post_with user, :notify_subscribers, intercom_params + it 'assigns message' do + post_with user, :notify_subscribers, params + expect(subject.instance_variable_get(:@message)).to be_an_instance_of(Message) end - it 'redirects to the error with success message' do - stub_request(:post, integration_driver.api_url + 'messages') - .with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}", - :headers => {'Authorization'=>'Basic Og=='}) - .to_return(:status => 200, :body => web_response_factory('intercom/intercom_post_message'), :headers => {}) - post_with user, :notify_subscribers, intercom_params + it 'redirects to the error path with success message' do + post_with user, :notify_subscribers, params expect(response).to redirect_to(error_path(group.slug)) expect(flash[:success]).to eq('Message successfully sent!') end end - context 'failure' do + context 'intercom' do + let!(:integration) { create :integration, website: website, provider: :intercom } + let(:driver) { Integrations.get_driver(integration.provider.to_sym) } + let(:integration_driver) { driver.new(Integrations::Integration.new(integration, driver)) } + before(:each) do session[:epiclogger_website_id] = website.id end + let(:intercom_params) { params.merge( intercom: true ) } + + context 'success' do + it 'calls the send_message method in the driver' do + expect_any_instance_of(Integrations::Drivers::Intercom).to receive(:send_message) + post_with user, :notify_subscribers, intercom_params + end - it 'redirects to error path with error message if the message does not meet the criteria' do - stub_request(:post, integration_driver.api_url + 'messages'). - with(:body => "{\"message_type\":\"inapp\",\"body\":\"smallmsg\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}", - :headers => {'Authorization'=>'Basic Og=='}). - to_return(:status => 500, :body => "eroare", :headers => {}) - intercom_params[:message] = 'smallmsg' - post_with user, :notify_subscribers, intercom_params - expect(response).to redirect_to(error_path(group.slug)) - expect(flash[:error]).to eq('Message too short!') + it 'redirects to the error with success message' do + stub_request(:post, integration_driver.api_url + 'messages') + .with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}", + :headers => {'Authorization'=>'Basic Og=='}) + .to_return(:status => 200, :body => web_response_factory('intercom/intercom_post_message'), :headers => {}) + post_with user, :notify_subscribers, intercom_params + expect(response).to redirect_to(error_path(group.slug)) + expect(flash[:success]).to eq('Message successfully sent!') + end end - it 'redirects to error path with error message if sending the message fails' do - stub_request(:post, integration_driver.api_url + 'messages'). - with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}", - :headers => {'Authorization'=>'Basic Og=='}). - to_return(:status => 500, :body => "eroare", :headers => {}) - post_with user, :notify_subscribers, intercom_params - expect(response).to redirect_to(error_path(group.slug)) - expect(flash[:error]).to eq('Operation failed!') + context 'failure' do + + it 'redirects to error path with error message if the message does not meet the criteria' do + stub_request(:post, integration_driver.api_url + 'messages'). + with(:body => "{\"message_type\":\"inapp\",\"body\":\"smallmsg\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}", + :headers => {'Authorization'=>'Basic Og=='}). + to_return(:status => 500, :body => "eroare", :headers => {}) + intercom_params[:message] = 'smallmsg' + post_with user, :notify_subscribers, intercom_params + expect(response).to redirect_to(error_path(group.slug)) + expect(flash[:error]).to eq('Message too short!') + end + + it 'redirects to error path with error message if sending the message fails' do + stub_request(:post, integration_driver.api_url + 'messages'). + with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}", + :headers => {'Authorization'=>'Basic Og=='}). + to_return(:status => 500, :body => "eroare", :headers => {}) + post_with user, :notify_subscribers, intercom_params + expect(response).to redirect_to(error_path(group.slug)) + expect(flash[:error]).to eq('Operation failed!') + end end end end diff --git a/spec/controllers/integrations_controller_spec.rb b/spec/controllers/integrations_controller_spec.rb index d0652ad5..579f5902 100644 --- a/spec/controllers/integrations_controller_spec.rb +++ b/spec/controllers/integrations_controller_spec.rb @@ -9,12 +9,13 @@ before(:each) do session[:epiclogger_website_id] = website.id end describe 'PUT#update' do - let(:params) { default_params.merge(integration: { application: 'gogu' } ) } + let(:params) { default_params.merge(integration: { app_name: 'gogu', app_owner: 'ion' } ) } it 'updates the integration' do expect { put_with user, :update, params integration.reload }.to change { integration.configuration['selected_application'] }.from('test').to('gogu') + .and change { integration.configuration['application_owner'] }.from(nil).to('ion') end it 'redirects to settings_path with success message' do diff --git a/spec/models/integration_spec.rb b/spec/models/integration_spec.rb index abbaa374..d944a2c3 100644 --- a/spec/models/integration_spec.rb +++ b/spec/models/integration_spec.rb @@ -30,8 +30,9 @@ describe 'select_application' do it 'should save the selected application into the configuration hash' do - integration.update_attributes(application: 'my_app') + integration.update_attributes(app_name: 'my_app', app_owner: 'gogu') expect(integration.configuration['selected_application']).to eq('my_app') + expect(integration.configuration['application_owner']).to eq('gogu') end end diff --git a/vendor/assets/javascripts/jquery_md5.js b/vendor/assets/javascripts/jquery_md5.js new file mode 100644 index 00000000..bf9bbe97 --- /dev/null +++ b/vendor/assets/javascripts/jquery_md5.js @@ -0,0 +1,269 @@ +/* + * jQuery MD5 Plugin 1.2.1 + * https://github.com/blueimp/jQuery-MD5 + * + * Copyright 2010, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://creativecommons.org/licenses/MIT/ + * + * Based on + * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message + * Digest Algorithm, as defined in RFC 1321. + * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009 + * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet + * Distributed under the BSD License + * See http://pajhome.org.uk/crypt/md5 for more info. + */ + +/*jslint bitwise: true */ +/*global unescape, jQuery */ + +(function ($) { + 'use strict'; + + /* + * Add integers, wrapping at 2^32. This uses 16-bit operations internally + * to work around bugs in some JS interpreters. + */ + function safe_add(x, y) { + var lsw = (x & 0xFFFF) + (y & 0xFFFF), + msw = (x >> 16) + (y >> 16) + (lsw >> 16); + return (msw << 16) | (lsw & 0xFFFF); + } + + /* + * Bitwise rotate a 32-bit number to the left. + */ + function bit_rol(num, cnt) { + return (num << cnt) | (num >>> (32 - cnt)); + } + + /* + * These functions implement the four basic operations the algorithm uses. + */ + function md5_cmn(q, a, b, x, s, t) { + return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b); + } + function md5_ff(a, b, c, d, x, s, t) { + return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); + } + function md5_gg(a, b, c, d, x, s, t) { + return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); + } + function md5_hh(a, b, c, d, x, s, t) { + return md5_cmn(b ^ c ^ d, a, b, x, s, t); + } + function md5_ii(a, b, c, d, x, s, t) { + return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); + } + + /* + * Calculate the MD5 of an array of little-endian words, and a bit length. + */ + function binl_md5(x, len) { + /* append padding */ + x[len >> 5] |= 0x80 << ((len) % 32); + x[(((len + 64) >>> 9) << 4) + 14] = len; + + var i, olda, oldb, oldc, oldd, + a = 1732584193, + b = -271733879, + c = -1732584194, + d = 271733878; + + for (i = 0; i < x.length; i += 16) { + olda = a; + oldb = b; + oldc = c; + oldd = d; + + a = md5_ff(a, b, c, d, x[i], 7, -680876936); + d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586); + c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819); + b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330); + a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897); + d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426); + c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341); + b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983); + a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416); + d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417); + c = md5_ff(c, d, a, b, x[i + 10], 17, -42063); + b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162); + a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682); + d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101); + c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290); + b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329); + + a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510); + d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632); + c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713); + b = md5_gg(b, c, d, a, x[i], 20, -373897302); + a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691); + d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083); + c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335); + b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848); + a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438); + d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690); + c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961); + b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501); + a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467); + d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784); + c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473); + b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734); + + a = md5_hh(a, b, c, d, x[i + 5], 4, -378558); + d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463); + c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562); + b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556); + a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060); + d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353); + c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632); + b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640); + a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174); + d = md5_hh(d, a, b, c, x[i], 11, -358537222); + c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979); + b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189); + a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487); + d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835); + c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520); + b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651); + + a = md5_ii(a, b, c, d, x[i], 6, -198630844); + d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415); + c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905); + b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055); + a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571); + d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606); + c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523); + b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799); + a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359); + d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744); + c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380); + b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649); + a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070); + d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379); + c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259); + b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551); + + a = safe_add(a, olda); + b = safe_add(b, oldb); + c = safe_add(c, oldc); + d = safe_add(d, oldd); + } + return [a, b, c, d]; + } + + /* + * Convert an array of little-endian words to a string + */ + function binl2rstr(input) { + var i, + output = ''; + for (i = 0; i < input.length * 32; i += 8) { + output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF); + } + return output; + } + + /* + * Convert a raw string to an array of little-endian words + * Characters >255 have their high-byte silently ignored. + */ + function rstr2binl(input) { + var i, + output = []; + output[(input.length >> 2) - 1] = undefined; + for (i = 0; i < output.length; i += 1) { + output[i] = 0; + } + for (i = 0; i < input.length * 8; i += 8) { + output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32); + } + return output; + } + + /* + * Calculate the MD5 of a raw string + */ + function rstr_md5(s) { + return binl2rstr(binl_md5(rstr2binl(s), s.length * 8)); + } + + /* + * Calculate the HMAC-MD5, of a key and some data (raw strings) + */ + function rstr_hmac_md5(key, data) { + var i, + bkey = rstr2binl(key), + ipad = [], + opad = [], + hash; + ipad[15] = opad[15] = undefined; + if (bkey.length > 16) { + bkey = binl_md5(bkey, key.length * 8); + } + for (i = 0; i < 16; i += 1) { + ipad[i] = bkey[i] ^ 0x36363636; + opad[i] = bkey[i] ^ 0x5C5C5C5C; + } + hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8); + return binl2rstr(binl_md5(opad.concat(hash), 512 + 128)); + } + + /* + * Convert a raw string to a hex string + */ + function rstr2hex(input) { + var hex_tab = '0123456789abcdef', + output = '', + x, + i; + for (i = 0; i < input.length; i += 1) { + x = input.charCodeAt(i); + output += hex_tab.charAt((x >>> 4) & 0x0F) + + hex_tab.charAt(x & 0x0F); + } + return output; + } + + /* + * Encode a string as utf-8 + */ + function str2rstr_utf8(input) { + return unescape(encodeURIComponent(input)); + } + + /* + * Take string arguments and return either raw or hex encoded strings + */ + function raw_md5(s) { + return rstr_md5(str2rstr_utf8(s)); + } + function hex_md5(s) { + return rstr2hex(raw_md5(s)); + } + function raw_hmac_md5(k, d) { + return rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)); + } + function hex_hmac_md5(k, d) { + return rstr2hex(raw_hmac_md5(k, d)); + } + + $.md5 = function (string, key, raw) { + if (!key) { + if (!raw) { + return hex_md5(string); + } else { + return raw_md5(string); + } + } + if (!raw) { + return hex_hmac_md5(key, string); + } else { + return raw_hmac_md5(key, string); + } + }; + +}(typeof jQuery === 'function' ? jQuery : this)); \ No newline at end of file