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