]> git.openstreetmap.org Git - rails.git/blob - app/controllers/site_controller.rb
Merge remote-tracking branch 'upstream/pull/4394'
[rails.git] / app / controllers / site_controller.rb
1 class SiteController < ApplicationController
2   layout "site"
3   layout :map_layout, :only => [:index, :export]
4
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]
12
13   authorize_resource :class => false
14
15   def index
16     session[:location] ||= OSM.ip_location(request.env["REMOTE_ADDR"]) unless Settings.status == "database_readonly" || Settings.status == "database_offline"
17   end
18
19   def permalink
20     lon, lat, zoom = ShortLink.decode(params[:code])
21     new_params = params.except(:host, :controller, :action, :code, :lon, :lat, :zoom, :layers, :node, :way, :relation, :changeset)
22
23     if new_params.key? :m
24       new_params.delete :m
25       new_params[:mlat] = lat
26       new_params[:mlon] = lon
27     end
28
29     new_params[:anchor] = "map=#{zoom}/#{lat}/#{lon}"
30     new_params[:anchor] += "&layers=#{params[:layers]}" if params.key? :layers
31
32     options = new_params.to_unsafe_h.to_options
33
34     path = if params.key? :node
35              node_path(params[:node], options)
36            elsif params.key? :way
37              way_path(params[:way], options)
38            elsif params.key? :relation
39              relation_path(params[:relation], options)
40            elsif params.key? :changeset
41              changeset_path(params[:changeset], options)
42            else
43              root_url(options)
44            end
45
46     redirect_to path
47   end
48
49   def key
50     expires_in 7.days, :public => true
51     @key = YAML.load_file(Rails.root.join("config/key.yml"))
52     @key.each_value do |layer_data|
53       layer_data.each_cons(2) do |entry, next_entry|
54         entry["max_zoom"] = next_entry["min_zoom"] - 1 if entry["name"] == next_entry["name"] && !entry["max_zoom"] && next_entry["min_zoom"]
55       end
56     end
57     render :layout => false
58   end
59
60   def edit
61     editor = preferred_editor
62
63     if editor == "remote"
64       require_oauth
65       render :action => :index, :layout => map_layout
66       return
67     else
68       require_user
69     end
70
71     if %w[id].include?(editor)
72       append_content_security_policy_directives(
73         :frame_src => %w[blob:]
74       )
75     end
76
77     begin
78       if params[:node]
79         bbox = Node.visible.find(params[:node]).bbox.to_unscaled
80         @lat = bbox.centre_lat
81         @lon = bbox.centre_lon
82         @zoom = 18
83       elsif params[:way]
84         bbox = Way.visible.find(params[:way]).bbox.to_unscaled
85         @lat = bbox.centre_lat
86         @lon = bbox.centre_lon
87         @zoom = 17
88       elsif params[:note]
89         note = Note.visible.find(params[:note])
90         @lat = note.lat
91         @lon = note.lon
92         @zoom = 17
93       elsif params[:gpx] && current_user
94         trace = Trace.visible_to(current_user).find(params[:gpx])
95         @lat = trace.latitude
96         @lon = trace.longitude
97         @zoom = 16
98       end
99     rescue ActiveRecord::RecordNotFound
100       # don't try and derive a location from a missing/deleted object
101     end
102   end
103
104   def copyright
105     @locale = params[:copyright_locale] || I18n.locale
106   end
107
108   def welcome; end
109
110   def help; end
111
112   def about
113     @locale = params[:about_locale] || I18n.locale
114   end
115
116   def communities
117     @local_chapters = Community.where(:type => "osm-lc").where.not(:id => "OSMF")
118   end
119
120   def export; end
121
122   def offline
123     flash.now[:warning] = if Settings.status == "database_offline"
124                             t("layouts.osm_offline")
125                           else
126                             t("layouts.osm_read_only")
127                           end
128     render :html => nil, :layout => true
129   end
130
131   def preview
132     render :html => RichText.new(params[:type], params[:text]).to_html
133   end
134
135   def id
136     append_content_security_policy_directives(
137       :connect_src => %w[*],
138       :img_src => %w[* blob:],
139       :script_src => %w[dev.virtualearth.net 'unsafe-eval'],
140       :style_src => %w['unsafe-inline']
141     )
142
143     render :layout => false
144   end
145
146   private
147
148   def redirect_browse_params
149     if params[:node]
150       redirect_to node_path(params[:node])
151     elsif params[:way]
152       redirect_to way_path(params[:way])
153     elsif params[:relation]
154       redirect_to relation_path(params[:relation])
155     elsif params[:note]
156       redirect_to note_path(params[:note])
157     elsif params[:query]
158       redirect_to search_path(:query => params[:query])
159     end
160   end
161
162   def redirect_map_params
163     anchor = []
164
165     anchor << "map=#{params.delete(:zoom) || 5}/#{params.delete(:lat)}/#{params.delete(:lon)}" if params[:lat] && params[:lon]
166
167     if params[:layers]
168       anchor << "layers=#{params.delete(:layers)}"
169     elsif params.delete(:notes) == "yes"
170       anchor << "layers=N"
171     end
172
173     redirect_to params.to_unsafe_h.merge(:only_path => true, :anchor => anchor.join("&")) if anchor.present?
174   end
175 end