| 
									
										
										
										
											2017-03-02 14:57:33 +03:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | var winston = require('winston'); | 
					
						
							|  |  |  | var validator = require('validator'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var meta = require('../meta'); | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 16:56:38 -05:00
										 |  |  | exports.handle404 = function handle404(req, res) { | 
					
						
							| 
									
										
										
										
											2017-03-02 14:57:33 +03:00
										 |  |  | 	var relativePath = nconf.get('relative_path'); | 
					
						
							|  |  |  | 	var isClientScript = new RegExp('^' + relativePath + '\\/assets\\/src\\/.+\\.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (plugins.hasListeners('action:meta.override404')) { | 
					
						
							|  |  |  | 		return plugins.fireHook('action:meta.override404', { | 
					
						
							|  |  |  | 			req: req, | 
					
						
							|  |  |  | 			res: res, | 
					
						
							|  |  |  | 			error: {}, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (isClientScript.test(req.url)) { | 
					
						
							|  |  |  | 		res.type('text/javascript').status(200).send(''); | 
					
						
							| 
									
										
										
										
											2018-10-20 14:40:48 -04:00
										 |  |  | 	} else if (req.path.startsWith(relativePath + '/assets/uploads') || (req.get('accept') && !req.get('accept').includes('text/html')) || req.path === '/favicon.ico') { | 
					
						
							| 
									
										
										
										
											2017-03-02 14:57:33 +03:00
										 |  |  | 		meta.errors.log404(req.path || ''); | 
					
						
							|  |  |  | 		res.sendStatus(404); | 
					
						
							|  |  |  | 	} else if (req.accepts('html')) { | 
					
						
							|  |  |  | 		if (process.env.NODE_ENV === 'development') { | 
					
						
							|  |  |  | 			winston.warn('Route requested but not found: ' + req.url); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		meta.errors.log404(req.path.replace(/^\/api/, '') || ''); | 
					
						
							| 
									
										
										
										
											2017-05-21 19:45:25 -04:00
										 |  |  | 		exports.send404(req, res); | 
					
						
							| 
									
										
										
										
											2017-03-02 14:57:33 +03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		res.status(404).type('txt').send('Not found'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-05-21 19:45:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | exports.send404 = function (req, res) { | 
					
						
							|  |  |  | 	res.status(404); | 
					
						
							|  |  |  | 	var path = String(req.path || ''); | 
					
						
							|  |  |  | 	if (res.locals.isAPI) { | 
					
						
							|  |  |  | 		return res.json({ path: validator.escape(path.replace(/^\/api/, '')), title: '[[global:404.title]]' }); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var middleware = require('../middleware'); | 
					
						
							|  |  |  | 	middleware.buildHeader(req, res, function () { | 
					
						
							|  |  |  | 		res.render('404', { path: validator.escape(path), title: '[[global:404.title]]' }); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; |