]> git.openstreetmap.org Git - rails.git/blob - app/controllers/site_controller.rb
Merge pull request #3907 from Dimitar5555/patch-1
[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     render :layout => false
52   end
53
54   def edit
55     editor = preferred_editor
56
57     if editor == "remote"
58       require_oauth
59       render :action => :index, :layout => map_layout
60       return
61     else
62       require_user
63     end
64
65     if %w[id].include?(editor)
66       append_content_security_policy_directives(
67         :frame_src => %w[blob:]
68       )
69     end
70
71     begin
72       if params[:node]
73         bbox = Node.visible.find(params[:node]).bbox.to_unscaled
74         @lat = bbox.centre_lat
75         @lon = bbox.centre_lon
76         @zoom = 18
77       elsif params[:way]
78         bbox = Way.visible.find(params[:way]).bbox.to_unscaled
79         @lat = bbox.centre_lat
80         @lon = bbox.centre_lon
81         @zoom = 17
82       elsif params[:note]
83         note = Note.visible.find(params[:note])
84         @lat = note.lat
85         @lon = note.lon
86         @zoom = 17
87       elsif params[:gpx] && current_user
88         trace = Trace.visible_to(current_user).find(params[:gpx])
89         @lat = trace.latitude
90         @lon = trace.longitude
91         @zoom = 16
92       end
93     rescue ActiveRecord::RecordNotFound
94       # don't try and derive a location from a missing/deleted object
95     end
96   end
97
98   def copyright
99     @locale = params[:copyright_locale] || I18n.locale
100   end
101
102   def welcome; end
103
104   def help; end
105
106   def about
107     @locale = params[:about_locale] || I18n.locale
108   end
109
110   def communities
111     @local_chapters = Community.where(:type => "osm-lc").where.not(:id => "OSMF")
112   end
113
114   def export; end
115
116   def offline
117     flash.now[:warning] = if Settings.status == "database_offline"
118                             t("layouts.osm_offline")
119                           else
120                             t("layouts.osm_read_only")
121                           end
122     render :html => nil, :layout => true
123   end
124
125   def preview
126     render :html => RichText.new(params[:type], params[:text]).to_html
127   end
128
129   def id
130     append_content_security_policy_directives(
131       :connect_src => %w[*],
132       :img_src => %w[* blob:],
133       :script_src => %w[dev.virtualearth.net 'unsafe-eval'],
134       :style_src => %w['unsafe-inline']
135     )
136
137     render :layout => false
138   end
139
140   private
141
142   def redirect_browse_params
143     if params[:node]
144       redirect_to node_path(params[:node])
145     elsif params[:way]
146       redirect_to way_path(params[:way])
147     elsif params[:relation]
148       redirect_to relation_path(params[:relation])
149     elsif params[:note]
150       redirect_to note_path(params[:note])
151     elsif params[:query]
152       redirect_to search_path(:query => params[:query])
153     end
154   end
155
156   def redirect_map_params
157     anchor = []
158
159     anchor << "map=#{params.delete(:zoom) || 5}/#{params.delete(:lat)}/#{params.delete(:lon)}" if params[:lat] && params[:lon]
160
161     if params[:layers]
162       anchor << "layers=#{params.delete(:layers)}"
163     elsif params.delete(:notes) == "yes"
164       anchor << "layers=N"
165     end
166
167     redirect_to params.to_unsafe_h.merge(:only_path => true, :anchor => anchor.join("&")) if anchor.present?
168   end
169 end