1 class TraceController < ApplicationController
 
   4   before_filter :authorize_web
 
   5   before_filter :set_locale
 
   6   before_filter :require_user, :only => [:mine, :create, :edit, :delete, :make_public]
 
   7   before_filter :authorize, :only => [:api_details, :api_data, :api_create]
 
   8   before_filter :check_database_readable, :except => [:api_details, :api_data, :api_create]
 
   9   before_filter :check_database_writable, :only => [:create, :edit, :delete, :make_public]
 
  10   before_filter :check_api_readable, :only => [:api_details, :api_data]
 
  11   before_filter :check_api_writable, :only => [:api_create]
 
  13   # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
 
  14   #  target_user - if set, specifies the user to fetch traces for.  if not set will fetch all traces
 
  15   def list(target_user = nil, action = "list")
 
  16     # from display name, pick up user id if one user's traces only
 
  17     display_name = params[:display_name]
 
  18     if target_user.nil? and !display_name.blank?
 
  19       target_user = User.find(:first, :conditions => [ "visible = ? and display_name = ?", true, display_name])
 
  21         @title = t'trace.no_such_user.title'
 
  22         @not_found_user = display_name
 
  23         render :action => 'no_such_user', :status => :not_found
 
  30       @title = t 'trace.list.public_traces'
 
  31     elsif @user and @user == target_user
 
  32       @title = t 'trace.list.your_traces'
 
  34       @title = t 'trace.list.public_traces_from', :user => target_user.display_name
 
  37     @title += t 'trace.list.tagged_with', :tags => params[:tag] if params[:tag]
 
  40     # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
 
  41     # 2 - all traces, not logged in = all public traces
 
  42     # 3 - user's traces, logged in as same user = all user's traces 
 
  43     # 4 - user's traces, not logged in as that user = all user's public traces
 
  44     if target_user.nil? # all traces
 
  46         conditions = ["(gpx_files.public = ? OR gpx_files.user_id = ?)", true, @user.id] #1
 
  48         conditions  = ["gpx_files.public = ?", true] #2
 
  51       if @user and @user == target_user
 
  52         conditions = ["gpx_files.user_id = ?", @user.id] #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
 
  54         conditions = ["gpx_files.public = ? AND gpx_files.user_id = ?", true, target_user.id] #4
 
  61       files = Tracetag.find_all_by_tag(params[:tag]).collect { |tt| tt.gpx_id }
 
  64         conditions[0] += " AND gpx_files.id IN (#{files.join(',')})"
 
  66         conditions[0] += " AND 0 = 1"
 
  70     conditions[0] += " AND gpx_files.visible = ?"
 
  73     @trace_pages, @traces = paginate(:traces,
 
  74                                      :include => [:user, :tags],
 
  75                                      :conditions => conditions,
 
  76                                      :order => "gpx_files.timestamp DESC",
 
  79     # put together SET of tags across traces, for related links
 
  82       @traces.each do |trace|
 
  83         trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
 
  84         trace.tags.each do |tag|
 
  85           tagset[tag.tag] = tag.tag
 
  90     # final helper vars for view
 
  92     @display_name = target_user.display_name if target_user
 
  93     @all_tags = tagset.values
 
  97     # Load the preference of whether the user set the trace public the last time
 
  99     if @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil?
 
 100       @trace.public = false
 
 108     @trace = Trace.find(params[:id])
 
 110     if @trace and @trace.visible? and
 
 111        (@trace.public? or @trace.user == @user)
 
 112       @title = t 'trace.view.viewing_trace', :name => @trace.name
 
 114       flash[:notice] = t 'trace.view.trace_not_found'
 
 115       redirect_to :controller => 'trace', :action => 'list'
 
 117   rescue ActiveRecord::RecordNotFound
 
 118     flash[:notice] = t 'trace.view.trace_not_found'
 
 119     redirect_to :controller => 'trace', :action => 'list'
 
 124       logger.info(params[:trace][:gpx_file].class.name)
 
 125       if params[:trace][:gpx_file].respond_to?(:read)
 
 126         do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
 
 127                   params[:trace][:description], params[:trace][:public])
 
 130           logger.info("id is #{@trace.id}")
 
 131           flash[:notice] = t 'trace.create.trace_uploaded'
 
 133           redirect_to :action => 'mine'
 
 136         @trace = Trace.new({:name => "Dummy",
 
 137                             :tagstring => params[:trace][:tagstring],
 
 138                             :description => params[:trace][:description],
 
 139                             :public => params[:trace][:public],
 
 140                             :inserted => false, :user => @user,
 
 141                             :timestamp => Time.now.getutc})
 
 143         @trace.errors.add(:gpx_file, "can't be blank")
 
 146     @title = t 'trace.create.upload_trace'
 
 150     trace = Trace.find(params[:id])
 
 152     if trace.visible? and (trace.public? or (@user and @user == trace.user))
 
 153       if request.format == Mime::XML
 
 154         send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
 
 156         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
 
 159       render :nothing => true, :status => :not_found
 
 161   rescue ActiveRecord::RecordNotFound
 
 162     render :nothing => true, :status => :not_found
 
 166     @trace = Trace.find(params[:id])
 
 168     if @user and @trace.user == @user
 
 170         @trace.description = params[:trace][:description]
 
 171         @trace.tagstring = params[:trace][:tagstring]
 
 173           redirect_to :action => 'view'
 
 177       render :nothing => true, :status => :forbidden
 
 179   rescue ActiveRecord::RecordNotFound
 
 180     render :nothing => true, :status => :not_found
 
 184     trace = Trace.find(params[:id])
 
 186     if @user and trace.user == @user
 
 187       if request.post? and trace.visible?
 
 188         trace.visible = false
 
 190         flash[:notice] = t 'trace.delete.scheduled_for_deletion'
 
 191         redirect_to :controller => 'traces', :action => 'mine'
 
 193         render :nothing => true, :status => :bad_request
 
 196       render :nothing => true, :status => :forbidden
 
 198   rescue ActiveRecord::RecordNotFound
 
 199     render :nothing => true, :status => :not_found
 
 203     trace = Trace.find(params[:id])
 
 205     if @user and trace.user == @user
 
 206       if request.post? and !trace.public?
 
 209         flash[:notice] = t 'trace.make_public.made_public'
 
 210         redirect_to :controller => 'trace', :action => 'view', :id => params[:id]
 
 212         render :nothing => true, :status => :bad_request
 
 215       render :nothing => true, :status => :forbidden
 
 217   rescue ActiveRecord::RecordNotFound
 
 218     render :nothing => true, :status => :not_found
 
 222     conditions = ["gpx_files.public = ?", true]
 
 224     if params[:display_name]
 
 225       conditions[0] += " AND users.display_name = ?"
 
 226       conditions << params[:display_name]
 
 230       conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
 
 231       conditions << params[:tag]
 
 234     traces = Trace.find(:all, :include => :user, :conditions => conditions, 
 
 235                         :order => "timestamp DESC", :limit => 20)
 
 237     rss = OSM::GeoRSS.new
 
 239     traces.each do |trace|
 
 240       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, :user_login => trace.user.display_name})}'> GPX file with #{trace.size} points from #{trace.user.display_name}", trace.timestamp)
 
 243     render :text => rss.to_s, :content_type => "application/rss+xml"
 
 247     trace = Trace.find(params[:id])
 
 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.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
 
 254         render :nothing => true, :status => :forbidden
 
 257       render :nothing => true, :status => :not_found
 
 259   rescue ActiveRecord::RecordNotFound
 
 260     render :nothing => true, :status => :not_found
 
 264     trace = Trace.find(params[:id])
 
 267       if trace.public? or (@user and @user == trace.user)
 
 268         expires_in 7.days, :private => !trace.public, :public => trace.public
 
 269         send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
 
 271         render :nothing => true, :status => :forbidden
 
 274       render :nothing => true, :status => :not_found
 
 276   rescue ActiveRecord::RecordNotFound
 
 277     render :nothing => true, :status => :not_found
 
 281     trace = Trace.find(params[:id])
 
 283     if trace.public? or trace.user == @user
 
 284       render :text => trace.to_xml.to_s, :content_type => "text/xml"
 
 286       render :nothing => true, :status => :forbidden
 
 288   rescue ActiveRecord::RecordNotFound
 
 289     render :nothing => true, :status => :not_found
 
 293     trace = Trace.find(params[:id])
 
 295     if trace.public? or trace.user == @user
 
 296       send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
 
 298       render :nothing => true, :status => :forbidden
 
 300   rescue ActiveRecord::RecordNotFound
 
 301     render :nothing => true, :status => :not_found
 
 306       tags = params[:tags] || ""
 
 307       description = params[:description] || ""
 
 308       pub = params[:public] || false
 
 310       if params[:file].respond_to?(:read)
 
 311         do_create(params[:file], tags, description, pub)
 
 314           render :text => @trace.id.to_s, :content_type => "text/plain"
 
 316           render :nothing => true, :status => :internal_server_error
 
 318           render :nothing => true, :status => :bad_request
 
 321         render :nothing => true, :status => :bad_request
 
 324       render :nothing => true, :status => :method_not_allowed
 
 330   def do_create(file, tags, description, public)
 
 331     # Sanitise the user's filename
 
 332     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
 
 334     # Get a temporary filename...
 
 335     filename = "/tmp/#{rand}"
 
 337     # ...and save the uploaded file to that location
 
 338     File.open(filename, "w") { |f| f.write(file.read) }
 
 340     # Create the trace object, falsely marked as already
 
 341     # inserted to stop the import daemon trying to load it
 
 345       :description => description,
 
 349       :timestamp => Time.now.getutc
 
 352     # Save the trace object
 
 354       # Rename the temporary file to the final name
 
 355       FileUtils.mv(filename, @trace.trace_name)
 
 357       # Clear the inserted flag to make the import daemon load the trace
 
 358       @trace.inserted = false
 
 361       # Remove the file as we have failed to update the database
 
 362       FileUtils.rm_f(filename)
 
 365     # Finally save whether the user marked the trace as being public
 
 367       if @user.trace_public_default.nil?
 
 368         @user.preferences.create(:k => "gps.trace.public", :v => "default")
 
 371       pref = @user.trace_public_default
 
 372       pref.destroy unless pref.nil?