]> git.ipfire.org Git - ipfire.org.git/blame - src/web/__init__.py
fireinfo: List all countries in which IPFire is running
[ipfire.org.git] / src / web / __init__.py
CommitLineData
81675874 1#/usr/bin/python
2
feb02477 3import logging
401827c2 4import itertools
81675874 5import os.path
c01ad253 6import phonenumbers
e96e445b 7import phonenumbers.geocoder
81675874 8import tornado.locale
a49b5422 9import tornado.options
81675874 10import tornado.web
11
a95c2f97 12import ipfire
574a88c7 13import ipfire.countries
feb02477 14
11347e46 15from .handlers import *
81675874 16
08df6527 17from . import auth
12e5de7e 18from . import blog
f301d952 19from . import boot
e77cd04c 20from . import download
96c9bb79 21from . import fireinfo
699a0911 22from . import iuse
f5b01fc2 23from . import location
95483f04 24from . import mirrors
5613b94b 25from . import newsletter
a41085fb 26from . import nopaste
03706893 27from . import people
df6180a5 28from . import ui_modules
12e5de7e 29
81675874 30class Application(tornado.web.Application):
3e7c6ccd
MT
31 def __init__(self, config, **kwargs):
32 # Initialize backend
a95c2f97 33 self.backend = ipfire.Backend(config)
a6dc0bad 34
9ed02e3b
MT
35 settings = {
36 # Do not compress responses
37 "gzip" : False,
38
39 # Enable XSRF cookies
40 "xsrf_cookies" : True,
41
42 # Login
43 "login_url" : "/login",
44
45 # Setup directory structure
46 "static_path" : self.backend.config.get("global", "static_dir"),
47 "template_path" : self.backend.config.get("global", "templates_dir"),
48
49 # UI Modules
50 "ui_methods" : {
574a88c7 51 "format_country_name" : self.format_country_name,
e96e445b
MT
52 "format_month_name" : self.format_month_name,
53 "format_phone_number" : self.format_phone_number,
54 "format_phone_number_to_e164" : self.format_phone_number_to_e164,
55 "format_phone_number_location" : self.format_phone_number_location,
56 "grouper" : grouper,
cc3b928d 57 },
9ed02e3b 58 "ui_modules" : {
f5b01fc2 59 # Blog
7e64f6a3
MT
60 "BlogHistoryNavigation": blog.HistoryNavigationModule,
61 "BlogList" : blog.ListModule,
f91dfcc7 62 "BlogPost" : blog.PostModule,
8a897d25 63 "BlogPosts" : blog.PostsModule,
f91dfcc7 64
93feb275
MT
65 # Boot
66 "BootMenuConfig" : boot.MenuConfigModule,
67 "BootMenuHeader" : boot.MenuHeaderModule,
68 "BootMenuSeparator" : boot.MenuSeparatorModule,
69
f5b01fc2 70 # Location
df6180a5 71 "Map" : ui_modules.MapModule,
f5b01fc2 72
917434b8 73 # Talk
dbb0c109
MT
74 "AccountsList" : people.AccountsListModule,
75 "CDR" : people.CDRModule,
76 "Channels" : people.ChannelsModule,
68ece434 77 "MOS" : people.MOSModule,
b5e2077f 78 "Password" : people.PasswordModule,
dbb0c109 79 "Registrations" : people.RegistrationsModule,
7afd64bb 80 "SIPStatus" : people.SIPStatusModule,
917434b8 81
e1814f16
MT
82 # Nopaste
83 "Code" : nopaste.CodeModule,
84
23f0179e 85 # Fireinfo
df6180a5 86 "ProgressBar" : ui_modules.ProgressBarModule,
96c9bb79
MT
87 "FireinfoDeviceTable" : fireinfo.DeviceTableModule,
88 "FireinfoDeviceAndGroupsTable" : fireinfo.DeviceAndGroupsTableModule,
89 "FireinfoGeoTable" : fireinfo.GeoTableModule,
81675874 90 },
3403dc5e
MT
91
92 # Call this when a page wasn't found
b22bc8e8 93 "default_handler_class" : base.NotFoundHandler,
9ed02e3b 94 }
9068dba1 95 settings.update(kwargs)
5cf160e0 96
ae0228e1
MT
97 tornado.web.Application.__init__(self, **settings)
98
66862195 99 authentication_handlers = [
08df6527
MT
100 (r"/login", auth.LoginHandler),
101 (r"/logout", auth.LogoutHandler),
66862195
MT
102 ]
103
d4dc517c 104 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
940227cb
MT
105 # Entry site that lead the user to index
106 (r"/", IndexHandler),
940227cb 107
81675874 108 # Download sites
60b0917c 109 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
e77cd04c
MT
110 (r"/download", download.IndexHandler),
111 (r"/download/([0-9a-z\-\.]+)", download.ReleaseHandler),
940227cb 112
e64ce07e
MT
113 # Donate
114 (r"/donate", DonateHandler),
115 (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }),
8d48f4ef 116
5613b94b
MT
117 # Newsletter
118 (r"/newsletter/subscribe", newsletter.SubscribeHandler),
119
de683d7c 120 # RSS feed
f0714277 121 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }),
7f9dbcc0
MT
122
123 # Redirect news articles to blog
124 (r"/news/(.*)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/posts/{1}" }),
de683d7c 125
45592df5 126 # Static Pages
84151dbf 127 (r"/chat", StaticHandler, { "template" : "chat.html" }),
45592df5 128 (r"/features", StaticHandler, { "template" : "features.html" }),
45592df5 129 (r"/legal", StaticHandler, { "template" : "legal.html" }),
00026d8b 130 (r"/support", StaticHandler, { "template" : "support.html" }),
45592df5 131
14cd4fa8
MT
132 # Handle old pages that have moved elsewhere
133 (r"/imprint", tornado.web.RedirectHandler, { "url" : "/legal" }),
3808b871 134 (r"/(de|en)/(.*)", LangCompatHandler),
37ed7c3c
MT
135
136 # Export arbitrary error pages
b22bc8e8 137 (r"/error/([45][0-9]{2})", base.ErrorHandler),
baa693a3
MT
138
139 # Block page
b22bc8e8 140 (r"/blocked", base.BlockedHandler),
940227cb
MT
141 ])
142
12e5de7e
MT
143 # blog.ipfire.org
144 self.add_handlers(r"blog(\.dev)?\.ipfire\.org", [
8a897d25 145 (r"/", blog.IndexHandler),
cfc0823a 146 (r"/authors/(\w+)", blog.AuthorHandler),
541c952b 147 (r"/compose", blog.ComposeHandler),
0b342a05 148 (r"/drafts", blog.DraftsHandler),
d17a2624 149 (r"/post/([0-9a-z\-\._]+)", blog.PostHandler),
914238a5 150 (r"/post/([0-9a-z\-\._]+)/delete", blog.DeleteHandler),
d17a2624
MT
151 (r"/post/([0-9a-z\-\._]+)/edit", blog.EditHandler),
152 (r"/post/([0-9a-z\-\._]+)/publish", blog.PublishHandler),
e6b18dce 153 (r"/search", blog.SearchHandler),
8d7487d2 154 (r"/tags/([0-9a-z\-\.]+)", blog.TagHandler),
7e64f6a3 155 (r"/years/([0-9]+)", blog.YearHandler),
f0714277
MT
156
157 # RSS Feed
158 (r"/feed.xml", blog.FeedHandler),
08df6527 159 ] + authentication_handlers)
12e5de7e 160
940227cb 161 # downloads.ipfire.org
80594ae3 162 self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [
ed8116c7
MT
163 (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }),
164 (r"/(.*)", download.FileHandler),
54af860e 165 ])
940227cb
MT
166
167 # mirrors.ipfire.org
8fceca0a 168 self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [
95483f04
MT
169 (r"/", mirrors.IndexHandler),
170 (r"/mirrors/(.*)", mirrors.MirrorHandler),
3808b871 171 ])
940227cb 172
d0d074e0 173 # planet.ipfire.org
7cad1818 174 self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [
3d4ce901
MT
175 (r"/", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/" }),
176 (r"/post/([A-Za-z0-9_-]+)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/posts/{1}" }),
177 (r"/user/([a-z0-9_-]+)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/authors/{1}" }),
bcc3ed4d
MT
178
179 # RSS
f0714277 180 (r"/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }),
3d4ce901 181 (r"/user/([a-z0-9_-]+)/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/authors/{1}.rss" }),
f0714277 182 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }),
3808b871 183 ])
d0d074e0 184
66862195 185 # fireinfo.ipfire.org
8fceca0a 186 self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [
96c9bb79 187 (r"/", fireinfo.IndexHandler),
8ab37e0b
MT
188
189 # Vendors
190 (r"/vendors", fireinfo.VendorsHandler),
191 (r"/vendors/(pci|usb)/([0-9a-f]{4})", fireinfo.VendorHandler),
66862195 192
1e3b2aad 193 # Driver
0cd21a36 194 (r"/drivers/(.*)", fireinfo.DriverDetail),
1e3b2aad 195
66862195 196 # Show profiles
96c9bb79 197 (r"/profile/random", fireinfo.RandomProfileHandler),
b84b407f 198 (r"/profile/([a-z0-9]{40})", fireinfo.ProfileHandler),
91a446f0 199
30852a9e 200 # Send profiles.
96c9bb79 201 (r"/send/([a-z0-9]+)", fireinfo.ProfileSendHandler),
66862195
MT
202
203 # Stats handlers
96c9bb79
MT
204 (r"/statistics", fireinfo.StatsHandler),
205 (r"/statistics/processors", fireinfo.StatsProcessorsHandler),
206 (r"/statistics/processors/(arm|x86)", fireinfo.StatsProcessorDetailHandler),
207 (r"/statistics/geo-locations", fireinfo.StatsGeoHandler),
96c9bb79 208 (r"/statistics/releases", fireinfo.StatsReleasesHandler),
3808b871 209 ])
5cf160e0 210
c37ec602 211 # i-use.ipfire.org
8fceca0a 212 self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [
e2f2865d 213 (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }),
395c1ac0 214 (r"/profile/([a-f0-9]{40})/([0-9]+).png", iuse.ImageHandler),
c37ec602
MT
215 ])
216
8e2e1261
MT
217 # boot.ipfire.org
218 BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot")
8fceca0a 219 self.add_handlers(r"boot(\.dev)?\.ipfire\.org", [
f301d952 220 (r"/", tornado.web.RedirectHandler, { "url" : "https://wiki.ipfire.org/installation/pxe" }),
8e2e1261
MT
221
222 # Configurations
f301d952
MT
223 (r"/premenu.cfg", boot.PremenuCfgHandler),
224 (r"/menu.gpxe", boot.MenuGPXEHandler),
225 (r"/menu.cfg", boot.MenuCfgHandler),
8e2e1261
MT
226
227 # Static files
37b5c0cf 228 (r"/(boot\.png|pxelinux\.0|menu\.c32|vesamenu\.c32)",
8e2e1261
MT
229 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
230 ])
231
60024cc8 232 # nopaste.ipfire.org
8fceca0a 233 self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [
a41085fb
MT
234 (r"/", nopaste.CreateHandler),
235 (r"/raw/(.*)", nopaste.RawHandler),
236 (r"/view/(.*)", nopaste.ViewHandler),
3808b871 237 ] + authentication_handlers)
60024cc8 238
f5b01fc2
MT
239 # location.ipfire.org
240 self.add_handlers(r"location(\.dev)?\.ipfire\.org", [
241 (r"/", location.IndexHandler),
242 (r"/lookup/(.+)", location.LookupHandler),
243 ])
244
9068dba1 245 # geoip.ipfire.org
8fceca0a 246 self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [
f5b01fc2 247 (r"/", tornado.web.RedirectHandler, { "url" : "https://location.ipfire.org/" }),
3808b871 248 ])
9068dba1 249
66862195
MT
250 # talk.ipfire.org
251 self.add_handlers(r"talk(\.dev)?\.ipfire\.org", [
786e9ca8
MT
252 (r"/", tornado.web.RedirectHandler, { "url" : "https://people.ipfire.org/" }),
253 ])
66862195 254
03706893
MT
255 # people.ipfire.org
256 self.add_handlers(r"people(\.dev)?\.ipfire\.org", [
786e9ca8 257 (r"/", people.IndexHandler),
30aeccdb 258 (r"/conferences", people.ConferencesHandler),
786e9ca8
MT
259 (r"/search", people.SearchHandler),
260 (r"/users", people.UsersHandler),
261 (r"/users/(\w+)", people.UserHandler),
03706893 262 (r"/users/(\w+)\.jpg", people.AvatarHandler),
68ece434 263 (r"/users/(\w+)/calls/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})", people.CallHandler),
bdaf6b46 264 (r"/users/(\w+)/calls(?:/(\d{4}-\d{2}-\d{2}))?", people.CallsHandler),
e96e445b 265 (r"/users/(\w+)/edit", people.UserEditHandler),
3ea97943 266 (r"/users/(\w+)/passwd", people.UserPasswdHandler),
f4672785 267 (r"/users/(\w+)/ssh-keys", people.SSHKeysIndexHandler),
44b75370
MT
268 (r"/users/(\w+)/ssh-keys/(SHA256\:.*)/delete", people.SSHKeysDeleteHandler),
269 (r"/users/(\w+)/ssh-keys/(SHA256\:.*)", people.SSHKeysDownloadHandler),
0d1fb712 270 (r"/users/(\w+)/ssh-keys/upload", people.SSHKeysUploadHandler),
e0daee8f 271 (r"/users/(\w+)/sip", people.SIPHandler),
786e9ca8 272 ] + authentication_handlers)
2cd9af74 273
ae0228e1 274 # ipfire.org
45592df5 275 self.add_handlers(r"ipfire\.org", [
ba43a892 276 (r".*", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org" })
5cf160e0 277 ])
3add293a 278
feb02477
MT
279 logging.info("Successfully initialied application")
280
574a88c7
MT
281 def format_country_name(self, handler, country_code):
282 return ipfire.countries.get_name(country_code)
283
cc3b928d
MT
284 def format_month_name(self, handler, month):
285 _ = handler.locale.translate
286
287 if month == 1:
288 return _("January")
289 elif month == 2:
290 return _("February")
291 elif month == 3:
292 return _("March")
293 elif month == 4:
294 return _("April")
295 elif month == 5:
296 return _("May")
297 elif month == 6:
298 return _("June")
299 elif month == 7:
300 return _("July")
301 elif month == 8:
302 return _("August")
303 elif month == 9:
304 return _("September")
305 elif month == 10:
306 return _("October")
307 elif month == 11:
308 return _("November")
309 elif month == 12:
310 return _("December")
311
312 return month
401827c2 313
e96e445b
MT
314 def format_phone_number(self, handler, number):
315 if not isinstance(number, phonenumbers.PhoneNumber):
316 try:
01e73b0e 317 number = phonenumbers.parse(number, None)
e96e445b
MT
318 except phonenumbers.phonenumberutil.NumberParseException:
319 return number
c01ad253
MT
320
321 return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
322
e96e445b
MT
323 def format_phone_number_to_e164(self, handler, number):
324 return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164)
325
326 def format_phone_number_location(self, handler, number):
327 s = [
328 phonenumbers.geocoder.description_for_number(number, handler.locale.code),
329 phonenumbers.region_code_for_number(number),
330 ]
331
332 return ", ".join((e for e in s if e))
333
401827c2
MT
334
335def grouper(handler, iterator, n):
336 """
337 Returns groups of n from the iterator
338 """
339 i = iter(iterator)
340
341 while True:
342 ret = list(itertools.islice(i, 0, n))
343 if not ret:
344 break
345
346 yield ret