]>
Commit | Line | Data |
---|---|---|
940227cb MT |
1 | #!/usr/bin/python |
2 | ||
60024cc8 MT |
3 | from __future__ import division |
4 | ||
cc3b928d | 5 | import datetime |
66862195 MT |
6 | import dateutil.parser |
7 | import httplib | |
8 | import ipaddr | |
47ed77ed | 9 | import logging |
940227cb MT |
10 | import time |
11 | import tornado.locale | |
12 | import tornado.web | |
13 | ||
66862195 | 14 | import backend.util |
60024cc8 | 15 | |
940227cb | 16 | class BaseHandler(tornado.web.RequestHandler): |
de683d7c MT |
17 | rss_url = None |
18 | ||
940227cb MT |
19 | def get_account(self, uid): |
20 | # Find the name of the author | |
21 | return self.accounts.find(uid) | |
22 | ||
60024cc8 | 23 | def get_user_locale(self): |
45c763aa | 24 | # The planet is always in english. |
4ca1a601 | 25 | if self.hostname == "planet.ipfire.org": |
dbf95cf2 | 26 | return tornado.locale.get("en_US") |
45c763aa | 27 | |
dbf95cf2 MT |
28 | # Otherwise take the browser locale. |
29 | return self.get_browser_locale() | |
940227cb | 30 | |
4ca1a601 MT |
31 | @property |
32 | def hostname(self): | |
33 | # Remove the development prefix | |
34 | return self.request.host.replace(".dev.", ".") | |
35 | ||
940227cb MT |
36 | @property |
37 | def render_args(self): | |
cc3b928d MT |
38 | today = datetime.date.today() |
39 | ||
940227cb | 40 | return { |
66862195 MT |
41 | "format_size" : backend.util.format_size, |
42 | "format_time" : backend.util.format_time, | |
4ca1a601 | 43 | "hostname" : self.hostname, |
940227cb | 44 | "lang" : self.locale.code[:2], |
de683d7c | 45 | "rss_url" : self.rss_url, |
cc3b928d | 46 | "year" : today.year, |
940227cb MT |
47 | } |
48 | ||
bcc3ed4d MT |
49 | def render(self, *args, **_kwargs): |
50 | kwargs = self.render_args | |
51 | kwargs.update(_kwargs) | |
940227cb MT |
52 | tornado.web.RequestHandler.render(self, *args, **kwargs) |
53 | ||
bcc3ed4d MT |
54 | def render_string(self, *args, **_kwargs): |
55 | kwargs = self.render_args | |
56 | kwargs.update(_kwargs) | |
940227cb MT |
57 | return tornado.web.RequestHandler.render_string(self, *args, **kwargs) |
58 | ||
02f2d7fe | 59 | def write_error(self, status_code, **kwargs): |
940227cb MT |
60 | if status_code in (404, 500): |
61 | render_args = ({ | |
62 | "code" : status_code, | |
63 | "exception" : kwargs.get("exception", None), | |
64 | "message" : httplib.responses[status_code], | |
65 | }) | |
02f2d7fe | 66 | self.render("error-%s.html" % status_code, **render_args) |
940227cb | 67 | else: |
02f2d7fe | 68 | return tornado.web.RequestHandler.write_error(self, status_code, **kwargs) |
940227cb | 69 | |
3d540c40 MT |
70 | def static_url(self, path, static=True): |
71 | ret = tornado.web.RequestHandler.static_url(self, path) | |
72 | ||
1de8dd23 MT |
73 | if self.settings.get("debug", False): |
74 | return ret | |
75 | ||
76 | elif self.request.host == "admin.ipfire.org": | |
77 | return ret | |
78 | ||
79 | elif static: | |
80 | return "http://static.ipfire.org%s" % ret | |
3d540c40 MT |
81 | |
82 | return ret | |
83 | ||
9068dba1 MT |
84 | def get_remote_ip(self): |
85 | # Fix for clients behind a proxy that sends "X-Forwarded-For". | |
66862195 | 86 | remote_ips = self.request.remote_ip.split(", ") |
494d80e6 | 87 | |
66862195 MT |
88 | for remote_ip in remote_ips: |
89 | try: | |
90 | addr = ipaddr.IPAddress(remote_ip) | |
91 | except ValueError: | |
92 | # Skip invalid IP addresses. | |
93 | continue | |
9068dba1 | 94 | |
66862195 MT |
95 | # Check if the given IP address is from a |
96 | # private network. | |
97 | if addr.is_private: | |
98 | continue | |
9068dba1 | 99 | |
66862195 | 100 | return remote_ip |
9068dba1 | 101 | |
494d80e6 MT |
102 | # Return the last IP if nothing else worked |
103 | return remote_ips.pop() | |
104 | ||
9068dba1 MT |
105 | def get_remote_location(self): |
106 | if not hasattr(self, "__remote_location"): | |
107 | remote_ip = self.get_remote_ip() | |
108 | ||
109 | self.__remote_location = self.geoip.get_location(remote_ip) | |
110 | ||
111 | return self.__remote_location | |
112 | ||
66862195 MT |
113 | def get_argument_date(self, arg, *args, **kwargs): |
114 | value = self.get_argument(arg, *args, **kwargs) | |
115 | if value is None: | |
116 | return | |
117 | ||
118 | try: | |
119 | return dateutil.parser.parse(value) | |
120 | except ValueError: | |
121 | raise tornado.web.HTTPError(400) | |
122 | ||
123 | # Login stuff | |
124 | ||
125 | def get_current_user(self): | |
126 | session_id = self.get_cookie("session_id") | |
127 | if not session_id: | |
128 | return | |
129 | ||
130 | # Get account from the session object | |
131 | account = self.backend.accounts.get_by_session(session_id, self.request.host) | |
132 | ||
133 | # If the account was not found or the session was not valid | |
134 | # any more, we will remove the cookie. | |
135 | if not account: | |
136 | self.clear_cookie("session_id") | |
137 | ||
138 | return account | |
139 | ||
140 | def login(self, username, password): | |
141 | # Find account | |
142 | account = self.backend.accounts.find_account(username) | |
143 | if not account: | |
144 | logging.warning(401, "unknown account: %s" % username) | |
145 | return False | |
146 | ||
147 | # Check credentials | |
148 | if not account.check_password(password): | |
149 | logging.warning("invalid password for %s" % account) | |
150 | return False | |
151 | ||
152 | # User has logged in, create a session | |
153 | session_id, session_expires = self.backend.accounts.create_session(account, | |
154 | self.request.host) | |
155 | ||
156 | # Check if a new session was created | |
157 | if not session_id: | |
158 | logging.warning("Could not create session") | |
159 | return False | |
160 | ||
161 | # Send session cookie to the client | |
162 | self.set_cookie("session_id", session_id, | |
163 | domain=self.request.host, expires=session_expires) | |
164 | ||
165 | return True | |
166 | ||
167 | def logout(self): | |
168 | session_id = self.get_cookie("session_id") | |
169 | ||
170 | if not session_id: | |
171 | return | |
172 | ||
173 | success = self.backend.accounts.destroy_session(session_id, self.request.host) | |
174 | if success: | |
175 | self.clear_cookie("session_id") | |
176 | ||
a6dc0bad MT |
177 | @property |
178 | def backend(self): | |
179 | return self.application.backend | |
180 | ||
9068dba1 MT |
181 | @property |
182 | def db(self): | |
183 | return self.backend.db | |
184 | ||
a0048e66 MT |
185 | @property |
186 | def advertisements(self): | |
9068dba1 | 187 | return self.backend.advertisements |
a0048e66 | 188 | |
940227cb MT |
189 | @property |
190 | def accounts(self): | |
a6dc0bad | 191 | return self.backend.accounts |
940227cb MT |
192 | |
193 | @property | |
9068dba1 MT |
194 | def downloads(self): |
195 | return self.backend.downloads | |
196 | ||
66862195 MT |
197 | @property |
198 | def fireinfo(self): | |
199 | return self.backend.fireinfo | |
200 | ||
9068dba1 MT |
201 | @property |
202 | def iuse(self): | |
203 | return self.backend.iuse | |
940227cb | 204 | |
b3250465 MT |
205 | @property |
206 | def memcached(self): | |
9068dba1 | 207 | return self.backend.memcache |
b3250465 | 208 | |
940227cb MT |
209 | @property |
210 | def mirrors(self): | |
9068dba1 MT |
211 | return self.backend.mirrors |
212 | ||
213 | @property | |
214 | def netboot(self): | |
215 | return self.backend.netboot | |
940227cb MT |
216 | |
217 | @property | |
218 | def news(self): | |
9068dba1 | 219 | return self.backend.news |
940227cb MT |
220 | |
221 | @property | |
222 | def config(self): | |
9068dba1 | 223 | return self.backend.settings |
940227cb MT |
224 | |
225 | @property | |
226 | def releases(self): | |
9068dba1 | 227 | return self.backend.releases |
940227cb MT |
228 | |
229 | @property | |
9068dba1 MT |
230 | def geoip(self): |
231 | return self.backend.geoip | |
940227cb MT |
232 | |
233 | @property | |
66862195 MT |
234 | def talk(self): |
235 | return self.backend.talk | |
940227cb MT |
236 | |
237 | @property | |
238 | def tracker(self): | |
9068dba1 | 239 | return self.backend.tracker |
bcc3ed4d MT |
240 | |
241 | @property | |
242 | def planet(self): | |
a6dc0bad | 243 | return self.backend.planet |
7771acea MT |
244 | |
245 | @property | |
246 | def wishlist(self): | |
9068dba1 | 247 | return self.backend.wishlist |
66862195 MT |
248 | |
249 | ||
250 | class LoginHandler(BaseHandler): | |
251 | def get(self): | |
252 | self.render("auth/login.html") | |
253 | ||
254 | def post(self): | |
255 | username = self.get_argument("username") | |
256 | password = self.get_argument("password") | |
257 | ||
258 | if not self.login(username, password): | |
259 | raise tornado.web.HTTPError(401) | |
260 | ||
261 | next = self.get_argument("next", "/") | |
262 | return self.redirect(next) | |
263 | ||
264 | ||
265 | class LogoutHandler(BaseHandler): | |
266 | def get(self): | |
267 | self.logout() | |
268 | ||
269 | # Get back to the start page | |
270 | self.redirect("/") |