| 
									
										
										
										
											2011-04-16 06:43:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Redmine | 
					
						
							|  |  |  |   module CodesetUtil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.replace_invalid_utf8(str) | 
					
						
							|  |  |  |       return str if str.nil? | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       str.force_encoding('UTF-8') | 
					
						
							|  |  |  |       if ! str.valid_encoding? | 
					
						
							|  |  |  |         str = str.encode("US-ASCII", :invalid => :replace, | 
					
						
							|  |  |  |               :undef => :replace, :replace => '?').encode("UTF-8") | 
					
						
							| 
									
										
										
										
											2011-04-16 06:43:49 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |       str | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-11-14 23:04:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def self.to_utf8(str, encoding) | 
					
						
							|  |  |  |       return str if str.nil? | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       str.force_encoding("ASCII-8BIT") | 
					
						
							| 
									
										
										
										
											2011-11-14 23:04:45 +00:00
										 |  |  |       if str.empty? | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |         str.force_encoding("UTF-8") | 
					
						
							| 
									
										
										
										
											2011-11-14 23:04:45 +00:00
										 |  |  |         return str | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       enc = encoding.blank? ? "UTF-8" : encoding | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       if enc.upcase != "UTF-8" | 
					
						
							|  |  |  |         str.force_encoding(enc) | 
					
						
							|  |  |  |         str = str.encode("UTF-8", :invalid => :replace, | 
					
						
							|  |  |  |               :undef => :replace, :replace => '?') | 
					
						
							| 
									
										
										
										
											2011-11-14 23:04:45 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |         str.force_encoding("UTF-8") | 
					
						
							|  |  |  |         if ! str.valid_encoding? | 
					
						
							|  |  |  |           str = str.encode("US-ASCII", :invalid => :replace, | 
					
						
							|  |  |  |                 :undef => :replace, :replace => '?').encode("UTF-8") | 
					
						
							| 
									
										
										
										
											2011-11-14 23:04:45 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       str | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-11-16 05:07:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-17 11:53:15 +00:00
										 |  |  |     def self.to_utf8_by_setting(str) | 
					
						
							|  |  |  |       return str if str.nil? | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       self.to_utf8_by_setting_internal(str).force_encoding('UTF-8') | 
					
						
							| 
									
										
										
										
											2011-11-17 11:53:15 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.to_utf8_by_setting_internal(str) | 
					
						
							|  |  |  |       return str if str.nil? | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       str.force_encoding('ASCII-8BIT') | 
					
						
							| 
									
										
										
										
											2011-11-17 11:53:15 +00:00
										 |  |  |       return str if str.empty? | 
					
						
							|  |  |  |       return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       str.force_encoding('UTF-8') | 
					
						
							| 
									
										
										
										
											2011-11-17 11:53:15 +00:00
										 |  |  |       encodings = Setting.repositories_encodings.split(',').collect(&:strip) | 
					
						
							|  |  |  |       encodings.each do |encoding| | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |         begin | 
					
						
							|  |  |  |           str.force_encoding(encoding) | 
					
						
							|  |  |  |           utf8 = str.encode('UTF-8') | 
					
						
							|  |  |  |           return utf8 if utf8.valid_encoding? | 
					
						
							|  |  |  |         rescue | 
					
						
							|  |  |  |           # do nothing here and try the next encoding | 
					
						
							| 
									
										
										
										
											2011-11-17 11:53:15 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       self.replace_invalid_utf8(str).force_encoding('UTF-8') | 
					
						
							| 
									
										
										
										
											2011-11-17 11:53:15 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-16 05:07:16 +00:00
										 |  |  |     def self.from_utf8(str, encoding) | 
					
						
							|  |  |  |       str ||= '' | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       str.force_encoding('UTF-8') | 
					
						
							|  |  |  |       if encoding.upcase != 'UTF-8' | 
					
						
							|  |  |  |         str = str.encode(encoding, :invalid => :replace, | 
					
						
							|  |  |  |                          :undef => :replace, :replace => '?') | 
					
						
							| 
									
										
										
										
											2011-11-16 05:07:16 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |         str = self.replace_invalid_utf8(str) | 
					
						
							| 
									
										
										
										
											2011-11-16 05:07:16 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-04-16 06:43:49 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |