From 7023990086855bf5c4e5ff46af49cf9f214d025f Mon Sep 17 00:00:00 2001 From: bohung Date: Wed, 3 Aug 2022 12:51:25 +0800 Subject: [PATCH] Update temp file. --- temp_file/app/models/site.rb | 51 ++++++++++++++++++++++++- temp_file/config/set_global_variable.rb | 26 ++++++++++++- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/temp_file/app/models/site.rb b/temp_file/app/models/site.rb index 74a346e..4bd8f8d 100644 --- a/temp_file/app/models/site.rb +++ b/temp_file/app/models/site.rb @@ -104,6 +104,12 @@ class Site field :site_title_1, :type => Hash, :default=>{:zh_tw=>"主頁",:en=>"home_page"} field :orbitbar_language_login_btn_mobile_configuration , type: String, :default => "" field :is_hidden_orbit_bar , type: Boolean, :default => false + field :orbit_bar_not_display , type: Boolean, :default => false + field :hide_login_button , type: Boolean, :default => false + field :privileged_ip_login_only , type: Boolean, :default => false + field :privileged_ip, type: Array, default: [] #ex: [['192.168.1.1', '192.168.1.254']] + field :privileged_ip_range, type: Array, default: [] #ex: [[3232235777, 3232236030]] # IPAddr.new("192.168.1.1").to_i + field :privileged_page_count, type: Integer, default: 0 field :orbit_bar_background_color , type: String, :default => "" field :orbit_bar_background_hover_color field :orbit_bar_text_color , type: String, :default => "#ffffff" @@ -138,10 +144,32 @@ class Site # File.open("test.html","w+"){|f| f.write(e.backtrace.join(""))} # puts "cache header failed" # end + @changed = self.changed? + if self.privileged_ip_changed? + self.privileged_ip_range = self.privileged_ip.map do |a, b| + if a.present? && b.blank? + b = a.sub(/[\d]+$/,'254') + elsif a.blank? && b.present? + a = b.sub(/[\d]+$/,'1') + end + if a.include?('x') + b = a.gsub('x','254') + a = a.gsub('x','1') + end + if a.blank? + nil + else + a = IPAddr.new(a).to_i rescue 0 + b = IPAddr.new(b).to_i rescue 0 + [a, b] + end + end.compact + end end after_save do File.open("config/member_extra_db.txt","w+"){|f| f.write(self.member_extra_db)} - OrbitHelper::SharedHash['current_site']['site'] = self if self.changed? && OrbitHelper::SharedHash['current_site'] + OrbitHelper::SharedHash['current_site']['site'] = self if @changed && OrbitHelper::SharedHash['current_site'] + OrbitHelper::SharedHash['privileged_page_count'].value = self.privileged_page_count end after_initialize do |record| if !record.new_record? && !@skip_callback @@ -155,12 +183,33 @@ class Site save_flag = true end if save_flag + @changed = true @skip_callback = true record.save @skip_callback = false end end end + def need_process_ip + OrbitHelper::SharedHash['privileged_page_count'].value != 0 + end + def has_privileged_page + OrbitHelper::SharedHash['privileged_page_count'].value != 0 && self.privileged_ip_range.present? + end + def check_ip_is_privileged?(ip, user=(user_nil=true; nil)) + flag = (self.privileged_ip_range.blank?) + user = OrbitHelper.current_user if user_nil + if user && user.user_name == 'rulingcom' + flag = true + else + ip_int = IPAddr.new(ip).to_i rescue 0 + self.privileged_ip_range.each do |r| + flag = Range.new(r[0], r[1]).include?(ip_int) + break if flag + end + end + return flag + end def default_locale tmp = super() tmp.blank? ? "zh_tw" : tmp diff --git a/temp_file/config/set_global_variable.rb b/temp_file/config/set_global_variable.rb index 724dc0c..b707477 100644 --- a/temp_file/config/set_global_variable.rb +++ b/temp_file/config/set_global_variable.rb @@ -87,7 +87,20 @@ begin elsif h[0] == Float h[1].write_float(v) else - h[1].write_object(v) + tmp_obj = Marshal.dump(v) + if tmp_obj.bytesize > h[1].size + puts "#{v.class}: out of memory" + puts v + puts "-------------------------------" + else + begin + h[1].write_string(tmp_obj) + rescue => e + puts "#{v.class}: #{e.to_s}" + puts v + puts "-------------------------------" + end + end end end @@ -111,7 +124,13 @@ begin [Float,tmp] else tmp_obj = Marshal.dump(v) - tmp = ProcessShared::SharedMemory.new(:uint8, tmp_obj.bytesize*2) + size = tmp_obj.bytesize*2 + if v.respond_to?(:min_memory) + size = [size, v.min_memory].max + else + size = [size, 1000].max + end + tmp = ProcessShared::SharedMemory.new(:uint8, size) tmp.write_string(tmp_obj) [Object,tmp] end @@ -165,6 +184,9 @@ begin @shared_hash[gem_name] = ProcesssShareWraper.new(tmp) end end + s = Site.first + @shared_hash['current_site'] = ProcesssShareWraper.new({"site"=> s}) + @shared_hash['privileged_page_count'] = ProcesssShareWraper.new((s.privileged_page_count rescue 0)) Thread.current[:shared_hash] = @shared_hash Thread.current[:shared_mutex] = @mutex rescue LoadError => e