<commit_before># Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/

class ImportOtrsController < ApplicationController

  def url_check
    return if setup_done_response

    # validate
    if !params[:url] || params[:url] !~ %r{^(http|https)://.+?$}
      render json: {
        result: 'invalid',
        message: 'Invalid URL!',
      }
      return
    end

    # connection test
    translation_map = {
      'authentication failed'                                     => 'Authentication failed!',
      'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
      'No route to host'                                          => 'No route to host!',
      'Connection refused'                                        => 'Connection refused!',
    }

    url_parts = params[:url].split(';')

    response = UserAgent.request( url_parts[0] )

    if !response.success? && response.code.to_s !~ /^40.$/
      message_human = ''
      translation_map.each {|key, message|
        if response.error.to_s =~ /#{Regexp.escape(key)}/i
          message_human = message
        end
      }
      render json: {
        result: 'invalid',
        message_human: message_human,
        message: response.error.to_s,
      }
      return
    end

    result = {}
    if response.body =~ /zammad migrator/

      key_parts = url_parts[1].split('=')

      Setting.set('import_backend', 'otrs')
      Setting.set('import_otrs_endpoint', url_parts[0])
      Setting.set('import_otrs_endpoint_key', key_parts[1])

      result = {
        result: 'ok',
        url: params[:url],
      }
    elsif response.body =~ /(otrs\sag|otrs\.com|otrs\.org)/i
      result = {
        result: 'invalid',
        message_human: 'Host found, but no OTRS migrator is installed!'
      }
    else
      result = {
        result: 'invalid',<commit_msg>Fixed bug: Invalid API keys are validated as correct.<commit_after># Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/

class ImportOtrsController < ApplicationController

  def url_check
    return if setup_done_response

    # validate
    if !params[:url] || params[:url] !~ %r{^(http|https)://.+?$}
      render json: {
        result: 'invalid',
        message: 'Invalid URL!',
      }
      return
    end

    # connection test
    translation_map = {
      'authentication failed'                                     => 'Authentication failed!',
      'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
      'No route to host'                                          => 'No route to host!',
      'Connection refused'                                        => 'Connection refused!',
    }

    response = UserAgent.request( params[:url] )

    if !response.success? && response.code.to_s !~ /^40.$/
      message_human = ''
      translation_map.each {|key, message|
        if response.error.to_s =~ /#{Regexp.escape(key)}/i
          message_human = message
        end
      }
      render json: {
        result: 'invalid',
        message_human: message_human,
        message: response.error.to_s,
      }
      return
    end

    result = {}
    if response.body =~ /zammad migrator/

      migrator_response = JSON.parse(response.body)

      if migrator_response['Success'] == 1

        url_parts = params[:url].split(';')
        key_parts = url_parts[1].split('=')

        Setting.set('import_backend', 'otrs')
        Setting.set('import_otrs_endpoint', url_parts[0])
        Setting.set('import_otrs_endpoint_key', key_parts[1])

        result = {
          result: 'ok',
          url: params[:url],
        }
      else
        result = {
          result: 'invalid',
          message_human: migrator_response['Error']
        }
      end
    elsif response.body =~ /(otrs\sag|otrs\.com|otrs\.org)/i
      result = {
        result: 'invalid',
        message_human: 'Host found, but no OTRS migrator is installed!'
      }
    else
      result = {
        result: 'invalid',
{'fraction_non_alphanumeric': 0.1256492703438041, 'fraction_numerical': 0.006925550333910463, 'mean_word_length': 1.9518248175182482, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5095407', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1161, 'n_words': 269}