2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class SiteSetting < ActiveRecord::Base
|
2024-11-19 15:44:34 +08:00
|
|
|
VALID_AREAS = %w[flags about emojis]
|
2024-09-03 07:25:45 +08:00
|
|
|
|
2018-12-19 17:20:48 +08:00
|
|
|
extend GlobalPath
|
2013-02-06 03:16:51 +08:00
|
|
|
extend SiteSettingExtension
|
|
|
|
|
2022-06-09 07:24:30 +08:00
|
|
|
has_many :upload_references, as: :target, dependent: :destroy
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
validates_presence_of :name
|
|
|
|
validates_presence_of :data_type
|
|
|
|
|
2022-06-09 07:24:30 +08:00
|
|
|
after_save do
|
|
|
|
if saved_change_to_value?
|
|
|
|
if self.data_type == SiteSettings::TypeSupervisor.types[:upload]
|
|
|
|
UploadReference.ensure_exist!(upload_ids: [self.value], target: self)
|
|
|
|
elsif self.data_type == SiteSettings::TypeSupervisor.types[:uploaded_image_list]
|
|
|
|
upload_ids = self.value.split("|").compact.uniq
|
|
|
|
UploadReference.ensure_exist!(upload_ids: upload_ids, target: self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-20 03:43:01 +08:00
|
|
|
load_settings(File.join(Rails.root, "config", "site_settings.yml"))
|
|
|
|
|
2023-05-25 07:53:57 +08:00
|
|
|
if Rails.env.test?
|
|
|
|
SAMPLE_TEST_PLUGIN =
|
|
|
|
Plugin::Instance.new(
|
|
|
|
Plugin::Metadata.new.tap { |metadata| metadata.name = "discourse-sample-plugin" },
|
|
|
|
)
|
|
|
|
|
|
|
|
Discourse.plugins_by_name[SAMPLE_TEST_PLUGIN.name] = SAMPLE_TEST_PLUGIN
|
|
|
|
|
|
|
|
load_settings(
|
|
|
|
File.join(Rails.root, "spec", "support", "sample_plugin_site_settings.yml"),
|
|
|
|
plugin: SAMPLE_TEST_PLUGIN.name,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-01-11 20:30:22 +08:00
|
|
|
if GlobalSetting.load_plugins?
|
2014-12-12 00:08:47 +08:00
|
|
|
Dir[File.join(Rails.root, "plugins", "*", "config", "settings.yml")].each do |file|
|
2020-05-10 19:07:45 +08:00
|
|
|
load_settings(file, plugin: file.split("/")[-3])
|
2014-12-12 00:08:47 +08:00
|
|
|
end
|
2013-11-20 03:43:01 +08:00
|
|
|
end
|
|
|
|
|
2018-11-14 17:32:32 +08:00
|
|
|
setup_deprecated_methods
|
2014-03-28 13:36:17 +08:00
|
|
|
client_settings << :available_locales
|
2014-02-08 11:24:10 +08:00
|
|
|
|
|
|
|
def self.available_locales
|
2017-12-14 05:17:36 +08:00
|
|
|
LocaleSiteSetting.values.to_json
|
2014-02-08 11:24:10 +08:00
|
|
|
end
|
2013-11-20 03:43:01 +08:00
|
|
|
|
2013-02-27 00:27:59 +08:00
|
|
|
def self.topic_title_length
|
|
|
|
min_topic_title_length..max_topic_title_length
|
|
|
|
end
|
|
|
|
|
2013-06-05 05:58:25 +08:00
|
|
|
def self.private_message_title_length
|
2018-01-31 13:56:00 +08:00
|
|
|
min_personal_message_title_length..max_topic_title_length
|
2013-06-05 05:58:25 +08:00
|
|
|
end
|
|
|
|
|
2013-03-01 02:54:12 +08:00
|
|
|
def self.post_length
|
|
|
|
min_post_length..max_post_length
|
|
|
|
end
|
2013-03-28 21:01:13 +08:00
|
|
|
|
2015-03-19 22:17:55 +08:00
|
|
|
def self.first_post_length
|
|
|
|
min_first_post_length..max_post_length
|
|
|
|
end
|
|
|
|
|
2013-06-13 16:18:17 +08:00
|
|
|
def self.private_message_post_length
|
2018-01-31 13:56:00 +08:00
|
|
|
min_personal_message_post_length..max_post_length
|
2013-06-13 16:18:17 +08:00
|
|
|
end
|
|
|
|
|
2013-06-22 04:31:40 +08:00
|
|
|
def self.top_menu_items
|
|
|
|
top_menu.split("|").map { |menu_item| TopMenuItem.new(menu_item) }
|
|
|
|
end
|
|
|
|
|
2013-03-28 21:01:13 +08:00
|
|
|
def self.homepage
|
2013-06-22 04:31:40 +08:00
|
|
|
top_menu_items[0].name
|
2013-03-28 21:01:13 +08:00
|
|
|
end
|
|
|
|
|
2013-07-16 07:59:23 +08:00
|
|
|
def self.anonymous_menu_items
|
2013-12-24 07:50:36 +08:00
|
|
|
@anonymous_menu_items ||= Set.new Discourse.anonymous_filters.map(&:to_s)
|
2013-07-16 07:59:23 +08:00
|
|
|
end
|
|
|
|
|
2013-03-28 21:01:13 +08:00
|
|
|
def self.anonymous_homepage
|
2013-07-16 07:59:23 +08:00
|
|
|
top_menu_items
|
|
|
|
.map { |item| item.name }
|
|
|
|
.select { |item| anonymous_menu_items.include?(item) }
|
|
|
|
.first
|
|
|
|
end
|
|
|
|
|
2014-04-22 04:59:53 +08:00
|
|
|
def self.should_download_images?(src)
|
|
|
|
setting = disabled_image_download_domains
|
2019-06-04 02:17:25 +08:00
|
|
|
return true if setting.blank?
|
2014-04-22 04:59:53 +08:00
|
|
|
|
2014-05-08 01:49:16 +08:00
|
|
|
host = URI.parse(src).host
|
2019-06-04 02:17:25 +08:00
|
|
|
!setting.split("|").include?(host)
|
2018-08-14 18:23:32 +08:00
|
|
|
rescue URI::Error
|
2019-06-04 02:17:25 +08:00
|
|
|
true
|
2014-04-22 04:59:53 +08:00
|
|
|
end
|
|
|
|
|
2013-12-16 18:44:59 +08:00
|
|
|
def self.scheme
|
2016-06-27 17:26:43 +08:00
|
|
|
force_https? ? "https" : "http"
|
2013-12-16 18:44:59 +08:00
|
|
|
end
|
|
|
|
|
2016-10-12 01:22:43 +08:00
|
|
|
def self.min_redirected_to_top_period(duration)
|
2019-06-04 02:17:25 +08:00
|
|
|
ListController.best_period_with_topics_for(duration)
|
2015-09-22 02:28:20 +08:00
|
|
|
end
|
|
|
|
|
2016-03-17 05:28:01 +08:00
|
|
|
def self.email_polling_enabled?
|
2023-06-26 13:16:03 +08:00
|
|
|
SiteSetting.manual_polling_enabled? || SiteSetting.pop3_polling_enabled? ||
|
|
|
|
DiscoursePluginRegistry.mail_pollers.any?(&:enabled?)
|
2016-03-17 05:28:01 +08:00
|
|
|
end
|
2016-08-03 23:55:54 +08:00
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
def self.blocked_attachment_content_types_regex
|
2022-03-11 23:16:56 +08:00
|
|
|
current_db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
@blocked_attachment_content_types_regex ||= {}
|
|
|
|
@blocked_attachment_content_types_regex[current_db] ||= begin
|
|
|
|
Regexp.union(SiteSetting.blocked_attachment_content_types.split("|"))
|
|
|
|
end
|
2016-08-03 23:55:54 +08:00
|
|
|
end
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
def self.blocked_attachment_filenames_regex
|
2022-03-11 23:16:56 +08:00
|
|
|
current_db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
@blocked_attachment_filenames_regex ||= {}
|
|
|
|
@blocked_attachment_filenames_regex[current_db] ||= begin
|
|
|
|
Regexp.union(SiteSetting.blocked_attachment_filenames.split("|"))
|
|
|
|
end
|
2016-08-03 23:55:54 +08:00
|
|
|
end
|
2017-10-06 13:20:01 +08:00
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
def self.allowed_unicode_username_characters_regex
|
2022-03-11 23:16:56 +08:00
|
|
|
current_db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
@allowed_unicode_username_regex ||= {}
|
|
|
|
@allowed_unicode_username_regex[current_db] ||= begin
|
|
|
|
if SiteSetting.allowed_unicode_username_characters.present?
|
|
|
|
Regexp.new(SiteSetting.allowed_unicode_username_characters)
|
|
|
|
end
|
|
|
|
end
|
2019-04-23 18:22:47 +08:00
|
|
|
end
|
|
|
|
|
2017-10-06 13:20:01 +08:00
|
|
|
# helpers for getting s3 settings that fallback to global
|
|
|
|
class Upload
|
|
|
|
def self.s3_cdn_url
|
|
|
|
SiteSetting.enable_s3_uploads ? SiteSetting.s3_cdn_url : GlobalSetting.s3_cdn_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.s3_region
|
|
|
|
SiteSetting.enable_s3_uploads ? SiteSetting.s3_region : GlobalSetting.s3_region
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.s3_upload_bucket
|
|
|
|
SiteSetting.enable_s3_uploads ? SiteSetting.s3_upload_bucket : GlobalSetting.s3_bucket
|
|
|
|
end
|
|
|
|
|
2018-07-16 12:44:55 +08:00
|
|
|
def self.s3_endpoint
|
|
|
|
SiteSetting.enable_s3_uploads ? SiteSetting.s3_endpoint : GlobalSetting.s3_endpoint
|
|
|
|
end
|
|
|
|
|
2023-11-10 07:50:23 +08:00
|
|
|
def self.enable_s3_transfer_acceleration
|
|
|
|
if SiteSetting.enable_s3_uploads
|
|
|
|
SiteSetting.enable_s3_transfer_acceleration
|
|
|
|
else
|
|
|
|
GlobalSetting.enable_s3_transfer_acceleration
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-07 09:06:39 +08:00
|
|
|
def self.use_dualstack_endpoint
|
2024-11-15 10:05:59 +08:00
|
|
|
return false if !SiteSetting.Upload.enable_s3_uploads
|
|
|
|
return false if SiteSetting.Upload.s3_endpoint.present?
|
|
|
|
!SiteSetting.Upload.s3_region.start_with?("cn-")
|
2024-11-07 09:06:39 +08:00
|
|
|
end
|
|
|
|
|
2017-10-06 13:20:01 +08:00
|
|
|
def self.enable_s3_uploads
|
|
|
|
SiteSetting.enable_s3_uploads || GlobalSetting.use_s3?
|
|
|
|
end
|
|
|
|
|
2018-07-06 15:52:23 +08:00
|
|
|
def self.s3_base_url
|
|
|
|
path = self.s3_upload_bucket.split("/", 2)[1]
|
|
|
|
"#{self.absolute_base_url}#{path ? "/" + path : ""}"
|
|
|
|
end
|
|
|
|
|
2017-10-06 13:20:01 +08:00
|
|
|
def self.absolute_base_url
|
2018-07-16 12:44:55 +08:00
|
|
|
url_basename = SiteSetting.s3_endpoint.split("/")[-1]
|
2018-05-17 04:10:15 +08:00
|
|
|
bucket =
|
2023-01-09 20:20:10 +08:00
|
|
|
(
|
2018-05-17 04:10:15 +08:00
|
|
|
if SiteSetting.enable_s3_uploads
|
|
|
|
Discourse.store.s3_bucket_name
|
2023-01-09 20:20:10 +08:00
|
|
|
else
|
2018-05-17 04:10:15 +08:00
|
|
|
GlobalSetting.s3_bucket_name
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
|
|
|
)
|
2017-10-06 13:20:01 +08:00
|
|
|
|
2018-07-07 06:15:28 +08:00
|
|
|
# cf. http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
|
2019-02-06 00:50:27 +08:00
|
|
|
if SiteSetting.s3_endpoint.blank? || SiteSetting.s3_endpoint.end_with?("amazonaws.com")
|
2024-11-07 09:06:39 +08:00
|
|
|
if SiteSetting.Upload.use_dualstack_endpoint
|
2018-08-27 09:22:46 +08:00
|
|
|
"//#{bucket}.s3.dualstack.#{SiteSetting.Upload.s3_region}.amazonaws.com"
|
2024-11-07 09:06:39 +08:00
|
|
|
else
|
|
|
|
"//#{bucket}.s3.#{SiteSetting.Upload.s3_region}.amazonaws.com.cn"
|
2018-07-16 12:44:55 +08:00
|
|
|
end
|
2018-07-07 06:15:28 +08:00
|
|
|
else
|
2018-07-16 12:44:55 +08:00
|
|
|
"//#{bucket}.#{url_basename}"
|
2018-07-07 06:15:28 +08:00
|
|
|
end
|
2017-10-06 13:20:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.Upload
|
|
|
|
SiteSetting::Upload
|
|
|
|
end
|
|
|
|
|
2020-03-15 18:17:28 +08:00
|
|
|
def self.require_invite_code
|
|
|
|
invite_code.present?
|
|
|
|
end
|
|
|
|
client_settings << :require_invite_code
|
|
|
|
|
2018-11-14 15:03:02 +08:00
|
|
|
%i[
|
|
|
|
site_logo_url
|
|
|
|
site_logo_small_url
|
|
|
|
site_mobile_logo_url
|
|
|
|
site_favicon_url
|
2020-08-18 03:43:20 +08:00
|
|
|
site_logo_dark_url
|
|
|
|
site_logo_small_dark_url
|
|
|
|
site_mobile_logo_dark_url
|
2018-11-14 15:03:02 +08:00
|
|
|
].each { |client_setting| client_settings << client_setting }
|
|
|
|
|
2019-01-02 15:29:17 +08:00
|
|
|
%i[
|
|
|
|
logo
|
|
|
|
logo_small
|
|
|
|
digest_logo
|
|
|
|
mobile_logo
|
2020-08-18 03:43:20 +08:00
|
|
|
logo_dark
|
|
|
|
logo_small_dark
|
|
|
|
mobile_logo_dark
|
2019-01-02 15:29:17 +08:00
|
|
|
large_icon
|
2019-05-01 21:44:45 +08:00
|
|
|
manifest_icon
|
2019-01-02 15:29:17 +08:00
|
|
|
favicon
|
|
|
|
apple_touch_icon
|
|
|
|
twitter_summary_large_image
|
|
|
|
opengraph_image
|
|
|
|
push_notifications_icon
|
|
|
|
].each do |setting_name|
|
|
|
|
define_singleton_method("site_#{setting_name}_url") do
|
2019-05-01 21:44:45 +08:00
|
|
|
if SiteIconManager.respond_to?("#{setting_name}_url")
|
|
|
|
return SiteIconManager.public_send("#{setting_name}_url")
|
|
|
|
end
|
|
|
|
|
2019-01-02 15:29:17 +08:00
|
|
|
upload = self.public_send(setting_name)
|
|
|
|
upload ? full_cdn_url(upload.url) : ""
|
2019-01-11 15:46:31 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
def self.shared_drafts_enabled?
|
|
|
|
c = SiteSetting.shared_drafts_category
|
|
|
|
c.present? && c.to_i != SiteSetting.uncategorized_category_id.to_i
|
|
|
|
end
|
|
|
|
|
2022-03-11 23:16:56 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def self.clear_cache!
|
|
|
|
super
|
|
|
|
|
|
|
|
@blocked_attachment_content_types_regex = nil
|
|
|
|
@blocked_attachment_filenames_regex = nil
|
|
|
|
@allowed_unicode_username_regex = nil
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: site_settings
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2019-01-12 03:29:56 +08:00
|
|
|
# name :string not null
|
2013-05-24 10:48:32 +08:00
|
|
|
# data_type :integer not null
|
|
|
|
# value :text
|
2014-08-27 13:30:17 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
2019-04-02 13:17:55 +08:00
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_site_settings_on_name (name) UNIQUE
|
|
|
|
#
|