Merge commit 'discon/master' into ja-i18n
[shapado:shapado.git] / app / controllers / application_controller.rb
1 # Filters added to this controller apply to all controllers in the application.
2 # Likewise, all the methods added will be available for all controllers.
3
4 class ApplicationController < ActionController::Base
5   include AuthenticatedSystem
6   include Subdomains
7   include Sweepers
8
9   if AppConfig.exception_notification['activate']
10     include ExceptionNotifiable
11     include SuperExceptionNotifier
12     include ExceptionNotifierHelper
13
14     self.exception_notifiable_silent_exceptions = []
15     self.exception_notifiable_silent_exceptions << SuperExceptionNotifier::CustomExceptionClasses::PageNotFound
16     self.exception_notifiable_silent_exceptions << ActionController::InvalidAuthenticityToken
17
18     local_addresses.clear
19
20     exception_data :additional_data
21     def additional_data
22       { :group => find_group}
23     end
24     protected :additional_data
25   end
26
27   self.error_layout = 'application'
28
29   protect_from_forgery
30
31   before_filter :find_group
32   before_filter :check_group_access
33   before_filter :set_locale
34   before_filter :find_languages
35   layout :set_layout
36
37   protected
38
39   def check_group_access
40     if ((!current_group.registered_only || is_bot?) && !current_group.private) || devise_controller? || (params[:controller] == "users" && action_name == "new" )
41       return
42     end
43
44     if logged_in?
45       if !current_user.user_of?(@current_group)
46         access_denied
47       end
48     else
49       respond_to do |format|
50         format.json { render :json => {:message => "Permission denied" }}
51         format.html { redirect_to new_user_session_path }
52       end
53     end
54   end
55
56   def find_group
57     @current_group ||= begin
58       subdomains = request.subdomains
59       subdomains.delete("www") if request.host == "www.#{AppConfig.domain}"
60       _current_group = Group.first(:state => "active", :domain => request.host)
61       unless _current_group
62         if subdomain = subdomains.first
63           _current_group = Group.first(:state => "active", :subdomain => subdomain)
64           unless _current_group.nil?
65             redirect_to domain_url(:custom => _current_group.domain)
66             return
67           end
68         end
69         flash[:warn] = t("global.group_not_found", :url => request.host)
70         redirect_to domain_url(:custom => AppConfig.domain)
71         return
72       end
73       _current_group
74     end
75     @current_group
76   end
77
78   def current_group
79     @current_group
80   end
81   helper_method :current_group
82
83   def current_tags
84     @current_tags ||=  if params[:tags].kind_of?(String)
85       params[:tags].split("+")
86     elsif params[:tags].kind_of?(Array)
87       params[:tags]
88     else
89       []
90     end
91   end
92   helper_method :current_tags
93
94   def current_languages
95     @current_languages ||= find_languages.join("+")
96   end
97   helper_method :current_languages
98
99   def find_languages
100     @languages ||= begin
101       if AppConfig.enable_i18n
102         if languages = current_group.language
103           languages = [languages]
104         else
105           if logged_in?
106             languages = current_user.languages_to_filter
107           elsif session["user.language_filter"]
108             if session["user.language_filter"] == 'any'
109               languages = AVAILABLE_LANGUAGES
110             else
111               languages = [session["user.language_filter"]]
112             end
113           elsif params[:mylangs]
114             languages = params[:mylangs].split(' ')
115           elsif params[:feed_token] && (feed_user = User.find_by_feed_token(params[:feed_token]))
116             languages = feed_user.languages_to_filter
117           else
118             languages = [I18n.locale.to_s.split("-").first]
119           end
120         end
121         languages
122       else
123         [current_group.language || AppConfig.default_language]
124       end
125     end
126   end
127   helper_method :find_languages
128
129   def language_conditions
130     conditions = {}
131     conditions[:language] = { :$in => find_languages}
132     conditions
133   end
134   helper_method :language_conditions
135
136   def scoped_conditions(conditions = {})
137     unless current_tags.empty?
138       conditions.deep_merge!({:tags => {:$all => current_tags}})
139     end
140     conditions.deep_merge!({:group_id => current_group.id})
141     conditions.deep_merge!(language_conditions)
142   end
143   helper_method :scoped_conditions
144
145   def available_locales; AVAILABLE_LOCALES; end
146
147   def set_locale
148     locale = AppConfig.default_language || 'en'
149     if AppConfig.enable_i18n
150       if logged_in?
151         locale = current_user.language
152         Time.zone = current_user.timezone || "UTC"
153       elsif params[:feed_token] && (feed_user = User.find_by_feed_token(params[:feed_token]))
154         locale = feed_user.language
155       elsif params[:lang] =~ /^(\w\w)/
156         locale = find_valid_locale($1)
157       elsif request.env['HTTP_ACCEPT_LANGUAGE'] =~ /^(\w\w)/
158         locale = find_valid_locale($1)
159       end
160     end
161     I18n.locale = locale.to_s
162   end
163
164   def find_valid_locale(lang)
165     case lang
166       when /^es/
167         'es-AR'
168       when /^pt/
169         'pt-PT'
170       when "fr"
171         'fr'
172       when "ja"
173         'ja'
174       when /^el/
175         'el'
176       else
177         'en'
178     end
179   end
180   helper_method :find_valid_locale
181
182   def set_layout
183     devise_controller? || (action_name == "new" && controller_name == "users") ? 'sessions' : 'application'
184   end
185
186   def after_sign_in_path_for(resource)
187     if return_to = session.delete("return_to")
188       return_to
189     else
190       super
191     end
192   end
193
194   def set_page_title(title)
195     @page_title = title
196   end
197
198   def page_title
199     if @page_title
200       if current_group.name == AppConfig.application_name
201         "#{@page_title} - #{AppConfig.application_name}: #{t("layouts.application.title")}"
202       else
203         if current_group.isolate
204           "#{@page_title} - #{current_group.name} #{current_group.legend}"
205         else
206           "#{@page_title} - #{current_group.name} - #{AppConfig.application_name} -  #{current_group.legend}"
207         end
208       end
209     else
210       if current_group.name == AppConfig.application_name
211         "#{AppConfig.application_name} - #{t("layouts.application.title")}"
212       else
213         if current_group.isolate
214           "#{current_group.name} - #{current_group.legend}"
215         else
216           "#{current_group.name} - #{current_group.legend} - #{AppConfig.application_name}"
217         end
218       end
219     end
220   end
221   helper_method :page_title
222
223   def feed_urls
224     @feed_urls ||= Set.new
225   end
226   helper_method :feed_urls
227
228   def add_feeds_url(url, title="atom")
229     feed_urls << [title, url]
230   end
231
232   def admin_required
233     unless current_user.admin?
234       access_denied
235     end
236   end
237
238   def moderator_required
239     unless current_user.mod_of?(current_group)
240       access_denied
241     end
242   end
243
244   def owner_required
245     unless current_user.owner_of?(current_group)
246       access_denied
247     end
248   end
249
250   def is_bot?
251     request.user_agent =~ /\b(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg|Java|Yandex|Linguee|LWP::Simple|Exabot|ia_archiver|Purebot|Twiceler|StatusNet|Baiduspider)\b/i
252   end
253
254   def build_date(params, name)
255     Time.zone.parse("#{params["#{name}(1i)"]}-#{params["#{name}(2i)"]}-#{params["#{name}(3i)"]}") rescue nil
256   end
257
258   def build_datetime(params, name)
259     Time.zone.parse("#{params["#{name}(1i)"]}-#{params["#{name}(2i)"]}-#{params["#{name}(3i)"]} #{params["#{name}(4i)"]}:#{params["#{name}(5i)"]}") rescue nil
260   end
261 end