]> git.openstreetmap.org Git - rails.git/blob - app/controllers/trace_controller.rb
Fix referer params for welcome page
[rails.git] / app / controllers / trace_controller.rb
1 class TraceController < ApplicationController
2   layout 'site'
3
4   skip_before_filter :verify_authenticity_token, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
5   before_filter :authorize_web
6   before_filter :set_locale
7   before_filter :require_user, :only => [:mine, :create, :edit, :delete]
8   before_filter :authorize, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
9   before_filter :check_database_readable, :except => [:api_read, :api_data]
10   before_filter :check_database_writable, :only => [:create, :edit, :delete, :api_create, :api_update, :api_delete]
11   before_filter :check_api_readable, :only => [:api_read, :api_data]
12   before_filter :check_api_writable, :only => [:api_create, :api_update, :api_delete]
13   before_filter :require_allow_read_gpx, :only => [:api_read, :api_data]
14   before_filter :require_allow_write_gpx, :only => [:api_create, :api_update, :api_delete]
15   before_filter :offline_warning, :only => [:mine, :view]
16   before_filter :offline_redirect, :only => [:create, :edit, :delete, :data, :api_create, :api_delete, :api_data]
17   around_filter :api_call_handle_error, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
18
19   # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
20   #  target_user - if set, specifies the user to fetch traces for.  if not set will fetch all traces
21   def list
22     # from display name, pick up user id if one user's traces only
23     display_name = params[:display_name]
24     if !display_name.blank?
25       target_user = User.active.where(:display_name => display_name).first
26       if target_user.nil?
27         render_unknown_user display_name
28         return
29       end
30     end
31
32     # set title
33     if target_user.nil?
34       @title = t 'trace.list.public_traces'
35     elsif @user and @user == target_user
36       @title = t 'trace.list.your_traces'
37     else
38       @title = t 'trace.list.public_traces_from', :user => target_user.display_name
39     end
40
41     @title += t 'trace.list.tagged_with', :tags => params[:tag] if params[:tag]
42
43     # four main cases:
44     # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
45     # 2 - all traces, not logged in = all public traces
46     # 3 - user's traces, logged in as same user = all user's traces
47     # 4 - user's traces, not logged in as that user = all user's public traces
48     if target_user.nil? # all traces
49       if @user
50         @traces = Trace.visible_to(@user) #1
51       else
52         @traces = Trace.public #2
53       end
54     else
55       if @user and @user == target_user
56         @traces = @user.traces #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
57       else
58         @traces = target_user.traces.public #4
59       end
60     end
61
62     if params[:tag]
63       @traces = @traces.tagged(params[:tag])
64     end
65
66     @page = (params[:page] || 1).to_i
67     @page_size = 20
68
69     @traces = @traces.visible
70     @traces = @traces.order("timestamp DESC")
71     @traces = @traces.offset((@page - 1) * @page_size)
72     @traces = @traces.limit(@page_size)
73     @traces = @traces.includes(:user, :tags)
74
75     # put together SET of tags across traces, for related links
76     tagset = Hash.new
77     @traces.each do |trace|
78       trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
79       trace.tags.each do |tag|
80         tagset[tag.tag] = tag.tag
81       end
82     end
83
84     # final helper vars for view
85     @target_user = target_user
86     @display_name = target_user.display_name if target_user
87     @all_tags = tagset.values
88   end
89
90   def mine
91     redirect_to :action => :list, :display_name => @user.display_name
92   end
93
94   def view
95     @trace = Trace.find(params[:id])
96
97     if @trace and @trace.visible? and
98        (@trace.public? or @trace.user == @user)
99       @title = t 'trace.view.title', :name => @trace.name
100     else
101       flash[:error] = t 'trace.view.trace_not_found'
102       redirect_to :controller => 'trace', :action => 'list'
103     end
104   rescue ActiveRecord::RecordNotFound
105     flash[:error] = t 'trace.view.trace_not_found'
106     redirect_to :controller => 'trace', :action => 'list'
107   end
108
109   def create
110     if params[:trace]
111       logger.info(params[:trace][:gpx_file].class.name)
112
113       if params[:trace][:gpx_file].respond_to?(:read)
114         begin
115           do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
116                     params[:trace][:description], params[:trace][:visibility])
117         rescue => ex
118           logger.debug ex
119         end
120
121         if @trace.id
122           logger.info("id is #{@trace.id}")
123           flash[:notice] = t 'trace.create.trace_uploaded'
124
125           if @user.traces.count(:conditions => { :inserted => false }) > 4
126             flash[:warning] = t 'trace.trace_header.traces_waiting', :count => @user.traces.count(:conditions => { :inserted => false })
127           end
128
129           redirect_to :action => :list, :display_name => @user.display_name
130         end
131       else
132         @trace = Trace.new({:name => "Dummy",
133                             :tagstring => params[:trace][:tagstring],
134                             :description => params[:trace][:description],
135                             :visibility => params[:trace][:visibility],
136                             :inserted => false, :user => @user,
137                             :timestamp => Time.now.getutc})
138         @trace.valid?
139         @trace.errors.add(:gpx_file, "can't be blank")
140       end
141     else
142       @trace = Trace.new({:visibility => default_visibility}, :without_protection => true)
143     end
144
145     @title = t 'trace.create.upload_trace'
146   end
147
148   def data
149     trace = Trace.find(params[:id])
150
151     if trace.visible? and (trace.public? or (@user and @user == trace.user))
152       if Acl.no_trace_download(request.remote_ip)
153         render :text => "", :status => :forbidden
154       elsif request.format == Mime::XML
155         send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
156       else
157         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
158       end
159     else
160       render :text => "", :status => :not_found
161     end
162   rescue ActiveRecord::RecordNotFound
163     render :text => "", :status => :not_found
164   end
165
166   def edit
167     @trace = Trace.find(params[:id])
168
169     if @user and @trace.user == @user
170       @title = t 'trace.edit.title', :name => @trace.name
171       if params[:trace]
172         @trace.description = params[:trace][:description]
173         @trace.tagstring = params[:trace][:tagstring]
174         @trace.visibility = params[:trace][:visibility]
175         if @trace.save
176           redirect_to :action => 'view', :display_name => @user.display_name
177         end
178       end
179     else
180       render :text => "", :status => :forbidden
181     end
182   rescue ActiveRecord::RecordNotFound
183     render :text => "", :status => :not_found
184   end
185
186   def delete
187     trace = Trace.find(params[:id])
188
189     if @user and trace.user == @user
190       if trace.visible?
191         trace.visible = false
192         trace.save
193         flash[:notice] = t 'trace.delete.scheduled_for_deletion'
194         redirect_to :action => :list, :display_name => @user.display_name
195       else
196         render :text => "", :status => :not_found
197       end
198     else
199       render :text => "", :status => :forbidden
200     end
201   rescue ActiveRecord::RecordNotFound
202     render :text => "", :status => :not_found
203   end
204
205   def georss
206     traces = Trace.public.visible
207
208     if params[:display_name]
209       traces = traces.joins(:user).where(:users => {:display_name => params[:display_name]})
210     end
211
212     if params[:tag]
213       traces = traces.tagged(params[:tag])
214     end
215
216     traces = traces.order("timestamp DESC")
217     traces = traces.limit(20)
218     traces = traces.includes(:user)
219
220     rss = OSM::GeoRSS.new
221
222     traces.each do |trace|
223       rss.add(trace.latitude, trace.longitude, trace.name, trace.user.display_name, url_for({:controller => 'trace', :action => 'view', :id => trace.id, :display_name => trace.user.display_name}), "<img src='#{url_for({:controller => 'trace', :action => 'icon', :id => trace.id, :display_name => trace.user.display_name})}'> GPX file with #{trace.size} points from #{trace.user.display_name}", trace.timestamp)
224     end
225
226     render :text => rss.to_s, :content_type => "application/rss+xml"
227   end
228
229   def picture
230     trace = Trace.find(params[:id])
231
232     if trace.inserted?
233       if trace.public? or (@user and @user == trace.user)
234         expires_in 7.days, :private => !trace.public?, :public => trace.public?
235         send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
236       else
237         render :text => "", :status => :forbidden
238       end
239     else
240       render :text => "", :status => :not_found
241     end
242   rescue ActiveRecord::RecordNotFound
243     render :text => "", :status => :not_found
244   end
245
246   def icon
247     trace = Trace.find(params[:id])
248
249     if trace.inserted?
250       if trace.public? or (@user and @user == trace.user)
251         expires_in 7.days, :private => !trace.public?, :public => trace.public?
252         send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
253       else
254         render :text => "", :status => :forbidden
255       end
256     else
257       render :text => "", :status => :not_found
258     end
259   rescue ActiveRecord::RecordNotFound
260     render :text => "", :status => :not_found
261   end
262
263   def api_read
264     trace = Trace.visible.find(params[:id])
265
266     if trace.public? or trace.user == @user
267       render :text => trace.to_xml.to_s, :content_type => "text/xml"
268     else
269       render :text => "", :status => :forbidden
270     end
271   end
272
273   def api_update
274     trace = Trace.visible.find(params[:id])
275
276     if trace.user == @user
277       new_trace = Trace.from_xml(request.raw_post)
278
279       unless new_trace and new_trace.id == trace.id
280         raise OSM::APIBadUserInput.new("The id in the url (#{trace.id}) is not the same as provided in the xml (#{new_trace.id})")
281       end
282
283       trace.description = new_trace.description
284       trace.tags = new_trace.tags
285       trace.visibility = new_trace.visibility
286       trace.save!
287
288       render :text => "", :status => :ok
289     else
290       render :text => "", :status => :forbidden
291     end
292   end
293
294   def api_delete
295     trace = Trace.visible.find(params[:id])
296
297     if trace.user == @user
298       trace.visible = false
299       trace.save!
300
301       render :text => "", :status => :ok
302     else
303       render :text => "", :status => :forbidden
304     end
305   end
306
307   def api_data
308     trace = Trace.find(params[:id])
309
310     if trace.public? or trace.user == @user
311       if request.format == Mime::XML
312         send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
313       else
314         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
315       end
316     else
317       render :text => "", :status => :forbidden
318     end
319   end
320
321   def api_create
322     tags = params[:tags] || ""
323     description = params[:description] || ""
324     visibility = params[:visibility]
325
326     if visibility.nil?
327       if params[:public] && params[:public].to_i.nonzero?
328         visibility = "public"
329       else
330         visibility = "private"
331       end
332     end
333
334     if params[:file].respond_to?(:read)
335       do_create(params[:file], tags, description, visibility)
336
337       if @trace.id
338         render :text => @trace.id.to_s, :content_type => "text/plain"
339       elsif @trace.valid?
340         render :text => "", :status => :internal_server_error
341       else
342         render :text => "", :status => :bad_request
343       end
344     else
345       render :text => "", :status => :bad_request
346     end
347   end
348
349 private
350
351   def do_create(file, tags, description, visibility)
352     # Sanitise the user's filename
353     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
354
355     # Get a temporary filename...
356     filename = "/tmp/#{rand}"
357
358     # ...and save the uploaded file to that location
359     File.open(filename, "wb") { |f| f.write(file.read) }
360
361     # Create the trace object, falsely marked as already
362     # inserted to stop the import daemon trying to load it
363     @trace = Trace.new({
364       :name => name,
365       :tagstring => tags,
366       :description => description,
367       :visibility => visibility,
368       :inserted => true,
369       :user => @user,
370       :timestamp => Time.now.getutc
371     }, :without_protection => true)
372
373     Trace.transaction do
374       begin
375         # Save the trace object
376         @trace.save!
377
378         # Rename the temporary file to the final name
379         FileUtils.mv(filename, @trace.trace_name)
380       rescue Exception => ex
381         # Remove the file as we have failed to update the database
382         FileUtils.rm_f(filename)
383
384         # Pass the exception on
385         raise
386       end
387
388       begin
389         # Clear the inserted flag to make the import daemon load the trace
390         @trace.inserted = false
391         @trace.save!
392       rescue Exception => ex
393         # Remove the file as we have failed to update the database
394         FileUtils.rm_f(@trace.trace_name)
395
396         # Pass the exception on
397         raise
398       end
399     end
400
401     # Finally save the user's preferred privacy level
402     if pref = @user.preferences.where(:k => "gps.trace.visibility").first
403       pref.v = visibility
404       pref.save
405     else
406       @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
407     end
408
409   end
410
411   def offline_warning
412     flash.now[:warning] = t 'trace.offline_warning.message' if STATUS == :gpx_offline
413   end
414
415   def offline_redirect
416     redirect_to :action => :offline if STATUS == :gpx_offline
417   end
418
419   def default_visibility
420     visibility = @user.preferences.where(:k => "gps.trace.visibility").first
421
422     if visibility
423       visibility.v
424     elsif @user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
425       "private"
426     else
427       "public"
428     end
429   end
430
431 end