<commit_before>require 'csv'
require 'timeout'
require 'net/http'
require 'net/smtp'
require 'uri'

class CampaignsController < ModuleController

  layout 'manage'
  
  permit 'mailing_mailing', :except => [ :view, :link, :image ]
  
  component_info 'Mailing'
  
  skip_before_filter :context_translate_before, :only => [ :view, :link, :image ]
  skip_after_filter :context_translate_after, :only => [ :view, :link, :image ]
  skip_before_filter :check_ssl, :only => [ :view, :link, :image ]
  skip_before_filter :validate_is_editor, :only => [ :view, :link, :image ]
  
  helper :campaigns
  include CampaignsHelper
  
  before_filter :verify_mail_module, :except => ['missing_mail_module', 'mail_module_setup']
  before_filter :strip_param_spaces

  cms_admin_paths "mail",
                  'Content' => { :controller => '/content' },
                  'Options' =>   { :controller => '/options' },
                  'Modules' =>  { :controller => '/modules' },
                  'Mail' => { :controller => '/mail_manager' },
                  'Email Campaigns' => { :action => 'index' }

  def verify_mail_module

    if !check_mail_module
      redirect_to :action => :missing_mail_module 
      return false
    elsif ! verify_reply_to_and_address
      redirect_to :action => :mail_module_setup 
      return false
    else
      return true
    end
  end
  
  
  private
  
  def check_mail_module
    SiteVersion.default.site_nodes.find_by_module_name_and_node_type('/mailing/mail','M') ? true : false
  end
  
  def verify_campaign_setup
    if ! @campaign.under_construction?
      redirect_to :action => 'status', :path => @campaign.id
      return false;
    else
      return true
    end
  end
  
  def verify_reply_to_and_address
    ! Configuration.reply_to_email.blank? && ! Configuration.options.company_address.blank?
  end

  public
   
   def view
   
    if params[:queue_hash] == 'QUEUE'
      @campaign = MarketCampaign.find_by_identifier_hash(params[:campaign_hash]) || raise(InvalidPageDataException.new("Invalid Mailing"))
      raise(InvalidPageDataException.new("Invalid Mailing")) unless  @campaign.under_construction?
      @under_construction = true
    else
      @campaign = MarketCampaign.find_by_identifier_hash(params[:campaign_hash]) || raise(InvalidPageDataException.new("Invalid Mailing"))
      @queue= @campaign.market_campaign_queues.find_by_queue_hash(params[:queue_hash]) || raise(InvalidPageDataException.new("Invalid Mailing"))
    
      # Make sure we have a user 
      @user = EndUser.find_target @queue.email, :source => 'website'
      @user.elevate_user_level EndUser::UserLevel::VISITED

      if !@queue.opened?
	      @queue.reload(:lock => true)
	
	      if !@queue.opened?
	        @campaign.reload(:lock => true)
	        # Update the number of openings
	        @campaign.stat_opened += 1
	        # and we know this queue entry has opened the mail
	        @queue.opened_at = Time.now
	        @queue.opened = true
	
	        @campaign.save
	        @queue.save
                
	      end
      end
    
    end
    
    mail_template, tracking_variables = @campaign.prepare_mail_template(true)
    
    message = @campaign.market_campaign_message
    mdl = @campaign.data_model_class
    
    if @under_construction
      entry = mdl.find(:first)
      if @campaign.data_model == 'subscription'
        entry = entry.end_user if entry.end_user_id
      end

      vars = message.field_values(entry.attributes,'QUEUE')
      vars.merge!(@campaign.add_tracking_links(tracking_variables,'QUEUE'))
    else
      entry = mdl.find_by_id(@queue.model_id)
      if @campaign.data_model == 'subscription'
        entry = entry.end_user if entry.end_user_id
      end
      
      vars = message.field_values(entry.attributes,@queue.queue_hash)
      vars.merge!(@campaign.add_tracking_links(tracking_variables,@queue.queue_hash))
    end
    
    
    if mail_template.is_html
     render :text => mail_template.render_html(vars), :layout => 'simple'
    else
     render :text => mail_template.render_text(vars), :layout => 'simple'
    end
  end
   
  def link 
    campaign_hash = params[:campaign_hash].to_s.gsub(/[^A-F0-9]+/,'')
    queue_hash = params[:queue_hash].to_s.gsub(/[^A-F0-9]+/,'')
    link_hash = params[:link_hash].to_s.gsub(/[^A-F0-9]+/,'')


    @tst_msg = "This link was sent in a test message and is no longer valid once a campaign has been sent.".t
    @real_msg = "You have clicked on an invalid link.".t

    begin
        if queue_hash == 'QUEUE'
          @campaign = MarketCampaign.find_by_identifier_hash(campaign_hash) || raise(InvalidPageDataException.new(@real_msg))
          @market_link = @campaign.market_links.find_by_link_hash(link_hash) || raise(InvalidPageDataException.new(@real_msg))
          raise InvalidPageDataException.new(@tst_msg) unless @campaign.under_construction?
          <commit_msg>Fix to queue links<commit_after>require 'csv'
require 'timeout'
require 'net/http'
require 'net/smtp'
require 'uri'

class CampaignsController < ModuleController

  layout 'manage'
  
  permit 'mailing_mailing', :except => [ :view, :link, :image ]
  
  component_info 'Mailing'
  
  skip_before_filter :context_translate_before, :only => [ :view, :link, :image ]
  skip_after_filter :context_translate_after, :only => [ :view, :link, :image ]
  skip_before_filter :check_ssl, :only => [ :view, :link, :image ]
  skip_before_filter :validate_is_editor, :only => [ :view, :link, :image ]
  
  helper :campaigns
  include CampaignsHelper
  
  before_filter :verify_mail_module, :except => ['missing_mail_module', 'mail_module_setup']
  before_filter :strip_param_spaces

  cms_admin_paths "mail",
                  'Content' => { :controller => '/content' },
                  'Options' =>   { :controller => '/options' },
                  'Modules' =>  { :controller => '/modules' },
                  'Mail' => { :controller => '/mail_manager' },
                  'Email Campaigns' => { :action => 'index' }

  def verify_mail_module

    if !check_mail_module
      redirect_to :action => :missing_mail_module 
      return false
    elsif ! verify_reply_to_and_address
      redirect_to :action => :mail_module_setup 
      return false
    else
      return true
    end
  end
  
  
  private
  
  def check_mail_module
    SiteVersion.default.site_nodes.find_by_module_name_and_node_type('/mailing/mail','M') ? true : false
  end
  
  def verify_campaign_setup
    if ! @campaign.under_construction?
      redirect_to :action => 'status', :path => @campaign.id
      return false;
    else
      return true
    end
  end
  
  def verify_reply_to_and_address
    ! Configuration.reply_to_email.blank? && ! Configuration.options.company_address.blank?
  end

  public
   
   def view
   
    if params[:queue_hash] == 'QUEUE'
      @campaign = MarketCampaign.find_by_identifier_hash(params[:campaign_hash]) || raise(InvalidPageDataException.new("Invalid Mailing"))
      raise(InvalidPageDataException.new("Invalid Mailing")) unless  @campaign.under_construction?
      @under_construction = true
    else
      @campaign = MarketCampaign.find_by_identifier_hash(params[:campaign_hash]) || raise(InvalidPageDataException.new("Invalid Mailing"))
      @queue= @campaign.market_campaign_queues.find_by_queue_hash(params[:queue_hash]) || raise(InvalidPageDataException.new("Invalid Mailing"))
    
      # Make sure we have a user 
      @user = EndUser.find_target @queue.email, :source => 'website'
      @user.elevate_user_level EndUser::UserLevel::VISITED

      if !@queue.opened?
	      @queue.reload(:lock => true)
	
	      if !@queue.opened?
	        @campaign.reload(:lock => true)
	        # Update the number of openings
	        @campaign.stat_opened += 1
	        # and we know this queue entry has opened the mail
	        @queue.opened_at = Time.now
	        @queue.opened = true
	
	        @campaign.save
	        @queue.save
                
	      end
      end
    
    end
    
    mail_template, tracking_variables = @campaign.prepare_mail_template(true)
    
    message = @campaign.market_campaign_message
    mdl = @campaign.data_model_class
    
    if @under_construction
      entry = mdl.find(:first)
      if @campaign.data_model == 'subscription'
        entry = entry.end_user if entry.end_user_id
      end

      vars = message.field_values(entry.attributes,'QUEUE')
      vars.merge!(@campaign.add_tracking_links(tracking_variables,'QUEUE'))
    else
      entry = mdl.find_by_id(@queue.model_id)
      if @campaign.data_model == 'subscription'
        entry = entry.end_user if entry.end_user_id
      end
      
      vars = message.field_values(entry.attributes,@queue.queue_hash)
      vars.merge!(@campaign.add_tracking_links(tracking_variables,@queue.queue_hash))
    end
    
    
    if mail_template.is_html
     render :text => mail_template.render_html(vars), :layout => 'simple'
    else
     render :text => mail_template.render_text(vars), :layout => 'simple'
    end
  end
   
  def link 
    campaign_hash = params[:campaign_hash].to_s.gsub(/[^A-F0-9]+/,'')
    queue_hash = params[:queue_hash].to_s.gsub(/[^A-Z0-9]+/,'')
    link_hash = params[:link_hash].to_s.gsub(/[^A-F0-9]+/,'')


    @tst_msg = "This link was sent in a test message and is no longer valid once a campaign has been sent.".t
    @real_msg = "You have clicked on an invalid link.".t

    begin
        if queue_hash == 'QUEUE'
          @campaign = MarketCampaign.find_by_identifier_hash(campaign_hash) || raise(InvalidPageDataException.new(@real_msg))
          @market_link = @campaign.market_links.find_by_link_hash(link_hash) || raise(InvalidPageDataException.new(@real_msg))
          raise InvalidPageDataException.new(@tst_msg) unless @campaign.under_construction?
          
{'fraction_non_alphanumeric': 0.11304968880726457, 'fraction_numerical': 0.0014284256708499133, 'mean_word_length': 3.1081307627829005, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2219317', 'n_tokens_mistral': 3152, 'n_tokens_neox': 3001, 'n_words': 601}