2017-07-19 05:29:52 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
module ApplicationModel::ChecksAttributeValuesAndLength
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_create :check_attribute_values_and_length
|
|
|
|
before_update :check_attribute_values_and_length
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
1) check string/varchar size and cut them if needed
|
|
|
|
|
|
|
|
2) check string for null byte \u0000 and remove it
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def check_attribute_values_and_length
|
|
|
|
columns = self.class.columns_hash
|
|
|
|
attributes.each { |name, value|
|
|
|
|
next if value.blank?
|
|
|
|
next if !value.instance_of?(String)
|
|
|
|
column = columns[name]
|
|
|
|
next if !column
|
|
|
|
|
2017-08-09 22:34:28 +00:00
|
|
|
if column.type == :binary
|
|
|
|
self[name].force_encoding('BINARY')
|
|
|
|
end
|
|
|
|
|
2017-07-19 05:29:52 +00:00
|
|
|
# strip null byte chars (postgresql will complain about it)
|
2017-08-09 22:34:28 +00:00
|
|
|
if column.type == :text
|
2017-07-19 05:29:52 +00:00
|
|
|
if Rails.application.config.db_null_byte == false
|
|
|
|
self[name].delete!("\u0000")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# for varchar check length and replace null bytes
|
|
|
|
limit = column.limit
|
|
|
|
if limit
|
|
|
|
current_length = value.length
|
|
|
|
if limit < current_length
|
|
|
|
logger.warn "WARNING: cut string because of database length #{self.class}.#{name}(#{limit} but is #{current_length}:#{value})"
|
|
|
|
self[name] = value[0, limit]
|
|
|
|
end
|
|
|
|
|
|
|
|
# strip null byte chars (postgresql will complain about it)
|
|
|
|
if Rails.application.config.db_null_byte == false
|
|
|
|
self[name].delete!("\u0000")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# strip 4 bytes utf8 chars if needed (mysql/mariadb will complain it)
|
|
|
|
next if self[name].blank?
|
|
|
|
self[name] = self[name].utf8_to_3bytesutf8
|
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|