Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 365
- Log:
Hub integration and development mode bug fix - require "instiki_errors"
in application.rb, rather than environment.rb, since the latter is not
reloaded in development mode but the former is and the required code
seems to be forgotten, leading to exceptions on anything but the first
development mode patch fetch under Apache+Passenger in development mode.
- Author:
- rool
- Date:
- Sat Mar 19 22:05:55 +0000 2011
- Size:
- 9712 Bytes
- Properties:
- Property svn:mergeinfo is set
1 | # This must be done here rather than environment.rb so that it gets picked up |
2 | # properly in Development mode, when the controller is re-read for each request |
3 | # but the environment may not be (e.g. it isn't with Passenger). |
4 | require_dependency 'instiki_errors' |
5 | |
6 | # The filters added to this controller will be run for all controllers in the application. |
7 | # Likewise will all the methods added be available for all controllers. |
8 | class ApplicationController < ActionController::Base |
9 | |
10 | # Hub single sign-on support. |
11 | |
12 | require 'hub_sso_lib' |
13 | include HubSsoLib::Core |
14 | |
15 | before_filter :hubssolib_beforehand |
16 | after_filter :hubssolib_afterwards |
17 | |
18 | protect_forms_from_spam |
19 | before_filter :connect_to_model, :check_authorization, :setup_url_generator, :set_content_type_header, :set_robots_metatag |
20 | after_filter :remember_location, :teardown_url_generator |
21 | |
22 | # For injecting a different wiki model implementation. Intended for use in tests |
23 | def self.wiki=(the_wiki) |
24 | # a global variable is used here because Rails reloads controller and model classes in the |
25 | # development environment; therefore, storing it as a class variable does not work |
26 | # class variable is, anyway, not much different from a global variable |
27 | #$instiki_wiki_service = the_wiki |
28 | logger.debug("Wiki service: #{the_wiki.to_s}") |
29 | end |
30 | |
31 | def self.wiki |
32 | Wiki.new |
33 | end |
34 | |
35 | helper_method :xhtml_enabled?, :html_ext, :darken |
36 | |
37 | protected |
38 | |
39 | def xhtml_enabled? |
40 | in_a_web? and [:markdownMML, :markdownPNG, :markdown].include?(@web.markup) |
41 | end |
42 | |
43 | def html_ext |
44 | if xhtml_enabled? && request.env.include?('HTTP_ACCEPT') && |
45 | Mime::Type.parse(request.env["HTTP_ACCEPT"]).include?(Mime::XHTML) |
46 | 'xhtml' |
47 | else |
48 | 'html' |
49 | end |
50 | end |
51 | |
52 | def darken(s) |
53 | n=s.length/3 |
54 | s.scan( %r(\w{#{n},#{n}}) ).collect {|a| (a.hex * 2/3).to_s(16).rjust(n,'0')}.join |
55 | end |
56 | |
57 | def check_authorization |
58 | if in_a_web? and authorization_needed? and not authorized? |
59 | redirect_to :controller => 'wiki', :action => 'login', :web => @web_name |
60 | return false |
61 | end |
62 | end |
63 | |
64 | def connect_to_model |
65 | @action_name = params['action'] || 'index' |
66 | @web_name = params['web'] |
67 | @wiki = wiki |
68 | @author = cookies['author'] || 'AnonymousCoward' |
69 | if @web_name |
70 | @web = @wiki.webs[@web_name] |
71 | if @web.nil? |
72 | render(:status => 404, :text => "Unknown web '#{@web_name}'", :layout => 'error') |
73 | return false |
74 | end |
75 | end |
76 | end |
77 | |
78 | FILE_TYPES = { |
79 | '.aif' => 'audio/x-aiff', |
80 | '.aiff'=> 'audio/x-aiff', |
81 | '.avi' => 'video/x-msvideo', |
82 | '.exe' => 'application/octet-stream', |
83 | '.gif' => 'image/gif', |
84 | '.jpg' => 'image/jpeg', |
85 | '.pdf' => 'application/pdf', |
86 | '.png' => 'image/png', |
87 | '.oga' => 'audio/ogg', |
88 | '.ogg' => 'audio/ogg', |
89 | '.ogv' => 'video/ogg', |
90 | '.mov' => 'video/quicktime', |
91 | '.mp3' => 'audio/mpeg', |
92 | '.mp4' => 'video/mp4', |
93 | '.spx' => 'audio/speex', |
94 | '.txt' => 'text/plain', |
95 | '.tex' => 'text/plain', |
96 | '.wav' => 'audio/x-wav', |
97 | '.zip' => 'application/zip' |
98 | } unless defined? FILE_TYPES |
99 | |
100 | DISPOSITION = { |
101 | 'application/octet-stream' => 'attachment', |
102 | 'application/pdf' => 'inline', |
103 | 'image/gif' => 'inline', |
104 | 'image/jpeg' => 'inline', |
105 | 'image/png' => 'inline', |
106 | 'audio/mpeg' => 'inline', |
107 | 'audio/x-wav' => 'inline', |
108 | 'audio/x-aiff' => 'inline', |
109 | 'audio/speex' => 'inline', |
110 | 'audio/ogg' => 'inline', |
111 | 'video/ogg' => 'inline', |
112 | 'video/mp4' => 'inline', |
113 | 'video/quicktime' => 'inline', |
114 | 'video/x-msvideo' => 'inline', |
115 | 'text/plain' => 'inline', |
116 | 'application/zip' => 'attachment' |
117 | } unless defined? DISPOSITION |
118 | |
119 | def determine_file_options_for(file_name, original_options = {}) |
120 | original_options[:type] ||= (FILE_TYPES[File.extname(file_name)] or 'application/octet-stream') |
121 | original_options[:disposition] ||= (DISPOSITION[original_options[:type]] or 'attachment') |
122 | original_options[:stream] ||= false |
123 | original_options[:x_sendfile] = true if request.env.include?('HTTP_X_SENDFILE_TYPE') && |
124 | ( request.remote_addr == LOCALHOST || defined?(PhusionPassenger) ) |
125 | original_options |
126 | end |
127 | |
128 | def send_file(file, options = {}) |
129 | determine_file_options_for(file, options) |
130 | super(file, options) |
131 | end |
132 | |
133 | def password_check(password) |
134 | if password == @web.password |
135 | cookies[CGI.escape(@web_name)] = password |
136 | true |
137 | else |
138 | false |
139 | end |
140 | end |
141 | |
142 | def password_error(password) |
143 | if password.nil? or password.empty? |
144 | 'Please enter the password.' |
145 | else |
146 | 'You entered a wrong password. Please enter the right one.' |
147 | end |
148 | end |
149 | |
150 | def redirect_home(web = @web_name) |
151 | if web |
152 | redirect_to_page('HomePage', web) |
153 | else |
154 | redirect_to '/' |
155 | end |
156 | end |
157 | |
158 | def redirect_to_page(page_name = @page_name, web = @web_name) |
159 | redirect_to :web => web, :controller => 'wiki', :action => 'show', |
160 | :id => (page_name or 'HomePage') |
161 | end |
162 | |
163 | def remember_location |
164 | if request.method == :get and |
165 | @status == '200' and not \ |
166 | %w(locked save back file pic import).include?(action_name) |
167 | session[:return_to] = request.request_uri |
168 | logger.debug "Session ##{session.object_id}: remembered URL '#{session[:return_to]}'" |
169 | end |
170 | end |
171 | |
172 | def rescue_action_in_public(exception) |
173 | render :status => 500, :text => <<-EOL |
174 | <html xmlns="http://www.w3.org/1999/xhtml"><body> |
175 | <h2>Internal Error</h2> |
176 | <p>An application error occurred while processing your request.</p> |
177 | <!-- \n#{exception.to_s.purify.gsub!(/-{2,}/, '- -') }\n#{exception.backtrace.join("\n")}\n --> |
178 | </body></html> |
179 | EOL |
180 | end |
181 | |
182 | def return_to_last_remembered |
183 | # Forget the redirect location |
184 | redirect_target, session[:return_to] = session[:return_to], nil |
185 | tried_home, session[:tried_home] = session[:tried_home], false |
186 | |
187 | # then try to redirect to it |
188 | if redirect_target.nil? |
189 | if tried_home |
190 | raise 'Application could not render the index page' |
191 | else |
192 | logger.debug("Session ##{session.object_id}: no remembered redirect location, trying home") |
193 | redirect_home |
194 | end |
195 | else |
196 | logger.debug("Session ##{session.object_id}: " + |
197 | "redirect to the last remembered URL #{redirect_target}") |
198 | redirect_to(redirect_target) |
199 | end |
200 | end |
201 | |
202 | def set_content_type_header |
203 | response.charset = 'utf-8' |
204 | if %w(atom_with_content atom_with_headlines).include?(action_name) |
205 | response.content_type = Mime::ATOM |
206 | elsif %w(tex tex_list).include?(action_name) |
207 | response.content_type = Mime::TEXT |
208 | elsif xhtml_enabled? |
209 | if request.user_agent =~ /Validator/ or request.env.include?('HTTP_ACCEPT') && |
210 | Mime::Type.parse(request.env["HTTP_ACCEPT"]).include?(Mime::XHTML) |
211 | response.content_type = Mime::XHTML |
212 | elsif request.user_agent =~ /MathPlayer/ |
213 | response.charset = nil |
214 | response.content_type = Mime::XHTML |
215 | response.extend(MathPlayerHack) |
216 | else |
217 | response.content_type = Mime::HTML |
218 | end |
219 | else |
220 | response.content_type = Mime::HTML |
221 | end |
222 | end |
223 | |
224 | def set_robots_metatag |
225 | if controller_name == 'wiki' and %w(show published s5).include? action_name and !(params[:mode] == 'diff') |
226 | @robots_metatag_value = 'index,follow' |
227 | else |
228 | @robots_metatag_value = 'noindex,nofollow' |
229 | end |
230 | end |
231 | |
232 | def setup_url_generator |
233 | PageRenderer.setup_url_generator(UrlGenerator.new(self)) |
234 | end |
235 | |
236 | def teardown_url_generator |
237 | PageRenderer.teardown_url_generator |
238 | end |
239 | |
240 | def wiki |
241 | self.class.wiki |
242 | end |
243 | |
244 | private |
245 | |
246 | def in_a_web? |
247 | not @web_name.nil? |
248 | end |
249 | |
250 | def authorization_needed? |
251 | not %w(login authenticate feeds published atom_with_headlines atom_with_content file blahtex_png).include?(action_name) |
252 | end |
253 | |
254 | def authorized? |
255 | @web.nil? or |
256 | @web.password.nil? or |
257 | cookies[CGI.escape(@web_name)] == @web.password or |
258 | password_check(params['password']) or |
259 | (@web.published? and action_name == 's5') |
260 | end |
261 | |
262 | def is_post |
263 | unless (request.post? || Rails.env.test?) |
264 | layout = 'error' |
265 | layout = false if %w(tex tex_list).include?(action_name) |
266 | headers['Allow'] = 'POST' |
267 | render(:status => 405, :text => 'You must use an HTTP POST', :layout => layout) |
268 | return false |
269 | end |
270 | return true |
271 | end |
272 | |
273 | end |
274 | |
275 | module Mime |
276 | # Fix HTML |
277 | #HTML = Type.new "text/html", :html, %w( application/xhtml+xml ) |
278 | self.class.const_set("HTML", Type.new("text/html", :html) ) |
279 | |
280 | # Add XHTML |
281 | XHTML = Type.new "application/xhtml+xml", :xhtml |
282 | |
283 | # Fix xhtml and html lookups |
284 | LOOKUP["text/html"] = HTML |
285 | LOOKUP["application/xhtml+xml"] = XHTML |
286 | end |
287 | |
288 | module MathPlayerHack |
289 | def charset=(encoding) |
290 | self.headers["Content-Type"] = "#{content_type || Mime::HTML}" |
291 | end |
292 | end |
293 | |
294 | module Instiki |
295 | module VERSION #:nodoc: |
296 | MAJOR = 0 |
297 | MINOR = 19 |
298 | TINY = 1 |
299 | SUFFIX = '(MML+)' |
300 | PRERELEASE = false |
301 | if PRERELEASE |
302 | STRING = [MAJOR, MINOR].join('.') + PRERELEASE + SUFFIX |
303 | else |
304 | STRING = [MAJOR, MINOR, TINY].join('.') + SUFFIX |
305 | end |
306 | end |
307 | end |
308 | |
309 | # Monkey patch, to make Hash#key work in Ruby 1.8 |
310 | class Hash |
311 | alias_method(:key, :index) unless method_defined?(:key) |
312 | end |
313 | |
314 | # Monkey patch, to ensure ActionCache doesn't muck with the content-type header. |
315 | module ActionController #:nodoc: |
316 | module Caching |
317 | module Actions |
318 | class ActionCacheFilter |
319 | private |
320 | def set_content_type!(controller, extension) |
321 | return |
322 | end |
323 | end |
324 | end |
325 | end |
326 | end |