1 class SiteController < ApplicationController
 
   3   layout :map_layout, :only => [:index, :export]
 
   5   before_action :authorize_web
 
   6   before_action :set_locale
 
   7   before_action :redirect_browse_params, :only => :index
 
   8   before_action :redirect_map_params, :only => [:index, :edit, :export]
 
   9   before_action :require_oauth, :only => [:index]
 
  10   before_action :require_user, :only => [:id]
 
  11   before_action :update_totp, :only => [:index]
 
  13   authorize_resource :class => false
 
  15   content_security_policy(:only => :edit) do |policy|
 
  16     policy.frame_src(*policy.frame_src, :blob)
 
  19   content_security_policy(:only => :id) do |policy|
 
  20     policy.connect_src("*")
 
  21     policy.img_src(*policy.img_src, "*", :blob)
 
  22     policy.script_src(*policy.script_src, :unsafe_eval)
 
  23     policy.style_src(*policy.style_src, :unsafe_inline)
 
  27     session[:location] ||= OSM.ip_location(request.env["REMOTE_ADDR"]) unless %w[database_readonly database_offline].include?(Settings.status)
 
  31     lon, lat, zoom = ShortLink.decode(params[:code])
 
  32     new_params = params.except(:host, :controller, :action, :code, :lon, :lat, :zoom, :layers, :node, :way, :relation, :changeset)
 
  36       new_params[:mlat] = lat
 
  37       new_params[:mlon] = lon
 
  40     new_params[:anchor] = "map=#{zoom}/#{lat}/#{lon}"
 
  41     new_params[:anchor] += "&layers=#{params[:layers]}" if params.key? :layers
 
  43     options = new_params.to_unsafe_h.to_options
 
  45     path = if params.key? :node
 
  46              node_path(params[:node], options)
 
  47            elsif params.key? :way
 
  48              way_path(params[:way], options)
 
  49            elsif params.key? :relation
 
  50              relation_path(params[:relation], options)
 
  51            elsif params.key? :changeset
 
  52              changeset_path(params[:changeset], options)
 
  61     expires_in 7.days, :public => true
 
  62     @key = YAML.load_file(Rails.root.join("config/key.yml"))
 
  63     @key.each_value do |layer_data|
 
  64       layer_data.each do |entry|
 
  65         entry["name"] = Array(entry["name"])
 
  67       layer_data.each_cons(2) do |entry, next_entry|
 
  68         entry["max_zoom"] = next_entry["min_zoom"] - 1 if entry["name"] == next_entry["name"] && !entry["max_zoom"] && next_entry["min_zoom"]
 
  71     render :layout => false
 
  75     editor = preferred_editor
 
  79       render :action => :index, :layout => map_layout
 
  87         bbox = Node.visible.find(params[:node]).bbox.to_unscaled
 
  88         @lat = bbox.centre_lat
 
  89         @lon = bbox.centre_lon
 
  92         bbox = Way.visible.find(params[:way]).bbox.to_unscaled
 
  93         @lat = bbox.centre_lat
 
  94         @lon = bbox.centre_lon
 
  97         note = Note.visible.find(params[:note])
 
 101       elsif params[:gpx] && current_user
 
 102         trace = Trace.visible_to(current_user).find(params[:gpx])
 
 103         @lat = trace.latitude
 
 104         @lon = trace.longitude
 
 107     rescue ActiveRecord::RecordNotFound
 
 108       # don't try and derive a location from a missing/deleted object
 
 111     if api_status != "online"
 
 112       flash.now[:warning] = { :partial => "layouts/offline_flash" }
 
 113     elsif current_user && !current_user.data_public?
 
 114       flash.now[:warning] = { :partial => "not_public_flash" }
 
 116       @enable_editor = true
 
 122     @locale = params[:copyright_locale] || I18n.locale
 
 130     @locale = params[:about_locale] || I18n.locale
 
 134     @local_chapters = Community.where(:type => "osm-lc").where.not(:id => "OSMF")
 
 140     flash.now[:warning] = { :partial => "layouts/offline_flash" }
 
 141     render :html => nil, :layout => true
 
 145     if params[:text].blank?
 
 146       flash.now[:warning] = t("layouts.nothing_to_preview")
 
 147       render :partial => "layouts/flash"
 
 149       render :html => RichText.new(params[:type], params[:text]).to_html
 
 154     render :layout => false
 
 159   def redirect_browse_params
 
 161       redirect_to node_path(params[:node])
 
 163       redirect_to way_path(params[:way])
 
 164     elsif params[:relation]
 
 165       redirect_to relation_path(params[:relation])
 
 167       redirect_to note_path(params[:note])
 
 169       redirect_to search_path(:query => params[:query])
 
 173   def redirect_map_params
 
 176     anchor << "map=#{params.delete(:zoom) || 5}/#{params.delete(:lat)}/#{params.delete(:lon)}" if params[:lat] && params[:lon]
 
 179       anchor << "layers=#{params.delete(:layers)}"
 
 180     elsif params.delete(:notes) == "yes"
 
 184     redirect_to params.to_unsafe_h.merge(:only_path => true, :anchor => anchor.join("&")) if anchor.present?