2013-11-30 13:35:42 -05:00
var nconf = require ( 'nconf' ) ,
2013-10-22 15:54:02 -04:00
fs = require ( 'fs' ) ,
2013-11-30 13:35:42 -05:00
path = require ( 'path' ) ,
winston = require ( 'winston' ) ,
2014-02-19 15:07:38 -05:00
async = require ( 'async' ) ,
2013-11-30 13:35:42 -05:00
2013-12-02 21:20:55 -05:00
db = require ( './../database' ) ,
2013-11-30 13:35:42 -05:00
user = require ( './../user' ) ,
groups = require ( '../groups' ) ,
topics = require ( './../topics' ) ,
2014-01-25 16:39:27 -05:00
pkg = require ( './../../package' ) ,
2013-11-30 13:35:42 -05:00
categories = require ( './../categories' ) ,
meta = require ( '../meta' ) ,
2013-12-08 13:12:52 -05:00
plugins = require ( '../plugins' ) ,
2014-02-19 15:07:38 -05:00
widgets = require ( '../widgets' ) ,
2014-02-09 00:33:10 -05:00
image = require ( './../image' ) ,
2014-02-13 19:41:54 -05:00
file = require ( './../file' ) ,
2014-01-09 13:29:41 -05:00
Languages = require ( '../languages' ) ,
2013-12-29 18:10:42 -05:00
events = require ( './../events' ) ,
2014-01-25 16:39:27 -05:00
utils = require ( './../../public/src/utils' ) ,
templates = require ( './../../public/src/templates' ) ;
2013-11-30 13:35:42 -05:00
2013-09-24 14:18:41 -04:00
( function ( Admin ) {
Admin . isAdmin = function ( req , res , next ) {
2013-11-27 12:47:00 -05:00
user . isAdministrator ( ( req . user && req . user . uid ) ? req . user . uid : 0 , function ( err , isAdmin ) {
2013-11-28 22:29:32 -05:00
if ( ! isAdmin ) {
2014-01-14 14:33:43 -05:00
res . status ( 403 ) ;
res . redirect ( '/403' ) ;
2013-11-28 22:29:32 -05:00
} else {
next ( ) ;
}
2013-05-29 15:40:48 -04:00
} ) ;
}
2013-11-12 12:41:16 -05:00
Admin . buildHeader = function ( req , res , callback ) {
2013-10-13 13:34:15 -04:00
var custom _header = {
2013-11-28 10:42:25 -05:00
'plugins' : [ ] ,
'authentication' : [ ]
2013-10-13 13:34:15 -04:00
} ;
2013-11-12 12:41:16 -05:00
user . getUserFields ( req . user . uid , [ 'username' , 'userslug' , 'picture' ] , function ( err , userData ) {
plugins . fireHook ( 'filter:admin.header.build' , custom _header , function ( err , custom _header ) {
callback ( err , templates [ 'admin/header' ] . parse ( {
csrf : res . locals . csrf _token ,
relative _path : nconf . get ( 'relative_path' ) ,
plugins : custom _header . plugins ,
2013-11-28 10:42:25 -05:00
authentication : custom _header . authentication ,
2013-11-12 12:41:16 -05:00
userpicture : userData . picture ,
username : userData . username ,
2014-01-19 18:02:03 -05:00
userslug : userData . userslug ,
'cache-buster' : meta . config [ 'cache-buster' ] ? 'v=' + meta . config [ 'cache-buster' ] : '' ,
2014-01-31 12:25:53 -05:00
env : process . env . NODE _ENV ? true : false
2013-11-12 12:41:16 -05:00
} ) ) ;
} ) ;
2013-07-11 14:32:50 -04:00
} ) ;
}
2013-11-12 12:41:16 -05:00
Admin . createRoutes = function ( app ) {
2013-11-28 22:29:32 -05:00
app . all ( '/api/admin/*' , Admin . isAdmin ) ;
app . all ( '/admin/*' , Admin . isAdmin ) ;
2013-12-04 16:25:42 -05:00
app . get ( '/admin' , Admin . isAdmin ) ;
2013-11-28 22:29:32 -05:00
2013-09-24 14:18:41 -04:00
( function ( ) {
2013-09-17 13:10:14 -04:00
var routes = [
'categories/active' , 'categories/disabled' , 'users' , 'topics' , 'settings' , 'themes' ,
2014-01-26 23:34:54 -05:00
'database' , 'events' , 'motd' , 'groups' , 'plugins' , 'languages' , 'logger' ,
2014-01-09 13:29:41 -05:00
'users/latest' , 'users/sort-posts' , 'users/sort-reputation' , 'users/search'
2013-09-17 13:10:14 -04:00
] ;
for ( var i = 0 , ii = routes . length ; i < ii ; i ++ ) {
2013-09-24 14:18:41 -04:00
( function ( route ) {
2013-11-28 22:29:32 -05:00
app . get ( '/admin/' + route , function ( req , res ) {
2013-11-12 12:41:16 -05:00
Admin . buildHeader ( req , res , function ( err , header ) {
2013-10-13 13:34:15 -04:00
res . send ( header + app . create _route ( 'admin/' + route ) + templates [ 'admin/footer' ] ) ;
} ) ;
2013-05-09 03:33:53 +00:00
} ) ;
} ( routes [ i ] ) ) ;
}
2013-06-26 16:05:59 -04:00
var unit _tests = [ 'categories' ] ;
2013-09-17 13:10:14 -04:00
for ( var i = 0 , ii = unit _tests . length ; i < ii ; i ++ ) {
2013-09-24 14:18:41 -04:00
( function ( route ) {
2013-11-28 22:29:32 -05:00
app . get ( '/admin/testing/' + route , function ( req , res ) {
2013-11-12 12:41:16 -05:00
Admin . buildHeader ( req , res , function ( err , header ) {
2013-10-13 13:34:15 -04:00
res . send ( header + app . create _route ( 'admin/testing/' + route ) + templates [ 'admin/footer' ] ) ;
} ) ;
2013-06-26 16:05:59 -04:00
} ) ;
} ( unit _tests [ i ] ) ) ;
}
2013-05-09 03:33:53 +00:00
} ( ) ) ;
2013-09-24 14:18:41 -04:00
app . namespace ( '/admin' , function ( ) {
2013-11-28 22:29:32 -05:00
app . get ( '/' , function ( req , res ) {
2013-11-12 12:41:16 -05:00
Admin . buildHeader ( req , res , function ( err , header ) {
2013-10-13 13:34:15 -04:00
res . send ( header + app . create _route ( 'admin/index' ) + templates [ 'admin/footer' ] ) ;
} ) ;
2013-09-17 13:10:14 -04:00
} ) ;
2013-05-09 03:33:53 +00:00
2013-11-28 22:29:32 -05:00
app . get ( '/index' , function ( req , res ) {
2013-11-12 12:41:16 -05:00
Admin . buildHeader ( req , res , function ( err , header ) {
2013-10-13 13:34:15 -04:00
res . send ( header + app . create _route ( 'admin/index' ) + templates [ 'admin/footer' ] ) ;
} ) ;
2013-09-17 13:10:14 -04:00
} ) ;
2013-10-22 15:54:02 -04:00
2013-12-08 13:12:52 -05:00
app . post ( '/category/uploadpicture' , function ( req , res ) {
2014-02-13 19:41:54 -05:00
if ( ! req . user ) {
2013-12-08 13:12:52 -05:00
return res . redirect ( '/403' ) ;
2014-02-13 19:41:54 -05:00
}
2013-12-08 13:12:52 -05:00
2014-02-26 21:55:29 -05:00
var allowedTypes = [ 'image/png' , 'image/jpeg' , 'image/jpg' , 'image/gif' ] ,
params = null , er ;
2013-12-31 03:14:22 -05:00
try {
params = JSON . parse ( req . body . params ) ;
} catch ( e ) {
2014-02-26 21:55:29 -05:00
er = {
2013-12-31 03:14:22 -05:00
error : 'Error uploading file! Error :' + e . message
2014-02-26 21:55:29 -05:00
} ;
return res . send ( req . xhr ? er : JSON . stringify ( er ) ) ;
2013-12-31 03:14:22 -05:00
}
2013-12-08 13:12:52 -05:00
if ( allowedTypes . indexOf ( req . files . userPhoto . type ) === - 1 ) {
2014-02-26 21:55:29 -05:00
er = {
2013-12-08 13:12:52 -05:00
error : 'Allowed image types are png, jpg and gif!'
2014-02-26 21:55:29 -05:00
} ;
res . send ( req . xhr ? er : JSON . stringify ( er ) ) ;
2013-12-08 13:12:52 -05:00
return ;
}
2013-12-31 03:36:42 -05:00
var filename = 'category-' + params . cid + path . extname ( req . files . userPhoto . name ) ;
2013-12-08 13:12:52 -05:00
2013-12-31 03:36:42 -05:00
uploadImage ( filename , req , res ) ;
2013-12-08 13:12:52 -05:00
} ) ;
2013-12-09 13:01:57 -05:00
app . post ( '/uploadfavicon' , function ( req , res ) {
2014-02-13 19:41:54 -05:00
if ( ! req . user ) {
2013-12-09 13:01:57 -05:00
return res . redirect ( '/403' ) ;
2014-02-13 19:41:54 -05:00
}
2013-12-09 13:01:57 -05:00
2014-02-26 21:55:29 -05:00
var allowedTypes = [ 'image/x-icon' , 'image/vnd.microsoft.icon' ] ,
er ;
2013-12-09 13:01:57 -05:00
if ( allowedTypes . indexOf ( req . files . userPhoto . type ) === - 1 ) {
2014-02-26 21:55:29 -05:00
er = { error : 'You can only upload icon file type!' } ;
res . send ( req . xhr ? er : JSON . stringify ( er ) ) ;
2013-12-09 13:01:57 -05:00
return ;
}
2014-02-13 19:41:54 -05:00
file . saveFileToLocal ( 'favicon.ico' , req . files . userPhoto . path , function ( err , image ) {
fs . unlink ( req . files . userPhoto . path ) ;
2014-02-09 00:33:10 -05:00
if ( err ) {
2014-02-26 21:55:29 -05:00
er = { error : err . message } ;
return res . send ( req . xhr ? er : JSON . stringify ( er ) ) ;
2014-02-09 00:33:10 -05:00
}
2014-02-26 21:55:29 -05:00
var rs = { path : image . url } ;
res . send ( req . xhr ? rs : JSON . stringify ( rs ) ) ;
2014-02-09 00:33:10 -05:00
} ) ;
2013-12-09 13:01:57 -05:00
} ) ;
2013-11-28 22:29:32 -05:00
app . post ( '/uploadlogo' , function ( req , res ) {
2013-10-22 15:54:02 -04:00
2014-02-13 19:41:54 -05:00
if ( ! req . user ) {
2013-10-22 15:54:02 -04:00
return res . redirect ( '/403' ) ;
2014-02-13 19:41:54 -05:00
}
2013-10-22 15:54:02 -04:00
2014-02-26 21:55:29 -05:00
var allowedTypes = [ 'image/png' , 'image/jpeg' , 'image/pjpeg' , 'image/jpg' , 'image/gif' ] ,
er ;
2013-10-22 15:54:02 -04:00
if ( allowedTypes . indexOf ( req . files . userPhoto . type ) === - 1 ) {
2014-02-26 21:55:29 -05:00
er = { error : 'Allowed image types are png, jpg and gif!' } ;
res . send ( req . xhr ? er : JSON . stringify ( er ) ) ;
2013-10-22 15:54:02 -04:00
return ;
}
2014-02-09 00:33:10 -05:00
var filename = 'site-logo' + path . extname ( req . files . userPhoto . name ) ;
2013-10-22 15:54:02 -04:00
2013-12-31 03:36:42 -05:00
uploadImage ( filename , req , res ) ;
2013-12-31 03:14:22 -05:00
} ) ;
2014-01-07 14:01:32 -05:00
app . get ( '/users/csv' , function ( req , res ) {
user . getUsersCSV ( function ( err , data ) {
res . attachment ( 'users.csv' ) ;
res . setHeader ( 'Content-Type' , 'text/csv' ) ;
res . end ( data ) ;
} ) ;
} ) ;
2013-12-31 03:14:22 -05:00
} ) ;
2013-10-22 15:54:02 -04:00
2013-12-31 03:36:42 -05:00
function uploadImage ( filename , req , res ) {
2014-02-09 00:33:10 -05:00
function done ( err , image ) {
2014-02-26 21:55:29 -05:00
var er , rs ;
2014-02-13 19:41:54 -05:00
fs . unlink ( req . files . userPhoto . path ) ;
2014-02-09 00:33:10 -05:00
if ( err ) {
2014-02-26 21:55:29 -05:00
er = { error : err . message } ;
return res . send ( req . xhr ? er : JSON . stringify ( er ) ) ;
2014-02-09 00:33:10 -05:00
}
2014-02-26 21:55:29 -05:00
rs = { path : image . url } ;
res . send ( req . xhr ? rs : JSON . stringify ( rs ) ) ;
2014-02-09 00:33:10 -05:00
}
if ( plugins . hasListeners ( 'filter:uploadImage' ) ) {
2014-02-13 19:41:54 -05:00
plugins . fireHook ( 'filter:uploadImage' , req . files . userPhoto , done ) ;
2014-02-09 00:33:10 -05:00
} else {
2014-02-13 19:41:54 -05:00
file . saveFileToLocal ( filename , req . files . userPhoto . path , done ) ;
2013-12-31 03:36:42 -05:00
}
2013-12-31 03:14:22 -05:00
}
2013-08-02 14:29:40 -04:00
2013-10-13 14:30:39 -04:00
var custom _routes = {
'routes' : [ ] ,
2013-12-16 15:40:59 -05:00
'api' : [ ]
2013-10-13 14:30:39 -04:00
} ;
plugins . ready ( function ( ) {
plugins . fireHook ( 'filter:admin.create_routes' , custom _routes , function ( err , custom _routes ) {
var routes = custom _routes . routes ;
2013-10-22 15:54:02 -04:00
2013-10-13 14:30:39 -04:00
for ( var route in routes ) {
if ( routes . hasOwnProperty ( route ) ) {
2013-11-04 23:59:33 -05:00
( function ( route ) {
app [ routes [ route ] . method || 'get' ] ( '/admin' + routes [ route ] . route , function ( req , res ) {
routes [ route ] . options ( req , res , function ( options ) {
2013-11-12 12:41:16 -05:00
Admin . buildHeader ( req , res , function ( err , header ) {
2013-11-04 23:59:33 -05:00
res . send ( header + options . content + templates [ 'admin/footer' ] ) ;
} ) ;
2013-10-13 14:30:39 -04:00
} ) ;
} ) ;
2013-11-04 23:59:33 -05:00
} ( route ) ) ;
2013-10-13 14:30:39 -04:00
}
}
2013-12-16 15:40:59 -05:00
var apiRoutes = custom _routes . api ;
for ( var route in apiRoutes ) {
if ( apiRoutes . hasOwnProperty ( route ) ) {
( function ( route ) {
2013-12-16 15:45:52 -05:00
app [ apiRoutes [ route ] . method || 'get' ] ( '/api/admin' + apiRoutes [ route ] . route , function ( req , res ) {
2013-12-16 15:40:59 -05:00
apiRoutes [ route ] . callback ( req , res , function ( data ) {
res . json ( data ) ;
} ) ;
} ) ;
} ( route ) ) ;
}
}
2013-10-22 15:54:02 -04:00
} ) ;
2013-10-13 14:30:39 -04:00
} ) ;
2013-11-28 22:29:32 -05:00
2013-09-24 14:18:41 -04:00
app . namespace ( '/api/admin' , function ( ) {
2013-11-28 22:29:32 -05:00
2013-09-24 14:18:41 -04:00
app . get ( '/index' , function ( req , res ) {
2013-09-17 13:10:14 -04:00
res . json ( {
2013-11-12 12:41:16 -05:00
version : pkg . version ,
2013-09-17 13:10:14 -04:00
} ) ;
2013-08-02 14:29:40 -04:00
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/users/search' , function ( req , res ) {
2013-09-17 13:10:14 -04:00
res . json ( {
2013-09-24 14:18:41 -04:00
search _display : 'block' ,
loadmore _display : 'none' ,
users : [ ]
2013-09-17 13:10:14 -04:00
} ) ;
2013-08-02 14:29:40 -04:00
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/users/latest' , function ( req , res ) {
user . getUsers ( 'users:joindate' , 0 , 49 , function ( err , data ) {
res . json ( {
search _display : 'none' ,
loadmore _display : 'block' ,
users : data ,
yourid : req . user . uid
} ) ;
2013-09-17 13:10:14 -04:00
} ) ;
2013-08-02 14:29:40 -04:00
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/users/sort-posts' , function ( req , res ) {
user . getUsers ( 'users:postcount' , 0 , 49 , function ( err , data ) {
res . json ( {
search _display : 'none' ,
loadmore _display : 'block' ,
users : data ,
yourid : req . user . uid
} ) ;
2013-09-17 13:10:14 -04:00
} ) ;
2013-08-02 14:29:40 -04:00
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/users/sort-reputation' , function ( req , res ) {
user . getUsers ( 'users:reputation' , 0 , 49 , function ( err , data ) {
res . json ( {
search _display : 'none' ,
loadmore _display : 'block' ,
users : data ,
yourid : req . user . uid
} ) ;
} ) ;
2013-08-02 14:29:40 -04:00
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/users' , function ( req , res ) {
user . getUsers ( 'users:joindate' , 0 , 49 , function ( err , data ) {
res . json ( {
search _display : 'none' ,
users : data ,
yourid : req . user . uid
} ) ;
2013-08-08 12:49:01 -04:00
} ) ;
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/categories' , function ( req , res ) {
2013-11-26 14:25:46 -05:00
categories . getAllCategories ( 0 , function ( err , data ) {
2013-09-24 14:18:41 -04:00
res . json ( data ) ;
2013-08-08 12:49:01 -04:00
} ) ;
} ) ;
2013-08-02 14:29:40 -04:00
2013-09-24 14:18:41 -04:00
app . get ( '/categories/active' , function ( req , res ) {
2013-11-26 14:25:46 -05:00
categories . getAllCategories ( 0 , function ( err , data ) {
2013-09-24 14:18:41 -04:00
data . categories = data . categories . filter ( function ( category ) {
2014-02-19 15:33:59 -05:00
return ! category . disabled ;
2013-09-24 14:18:41 -04:00
} ) ;
res . json ( data ) ;
2013-08-02 14:29:40 -04:00
} ) ;
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/categories/disabled' , function ( req , res ) {
2013-11-26 14:25:46 -05:00
categories . getAllCategories ( 0 , function ( err , data ) {
2013-09-24 14:18:41 -04:00
data . categories = data . categories . filter ( function ( category ) {
2014-02-19 15:33:59 -05:00
return category . disabled ;
2013-09-24 14:18:41 -04:00
} ) ;
res . json ( data ) ;
} ) ;
} ) ;
2013-08-02 14:29:40 -04:00
2013-09-24 14:18:41 -04:00
app . get ( '/topics' , function ( req , res ) {
2014-02-14 13:38:10 -05:00
topics . getAllTopics ( 0 , 19 , function ( err , topics ) {
2013-09-24 14:18:41 -04:00
res . json ( {
2013-11-24 22:08:37 -05:00
topics : topics ,
2013-11-24 22:29:36 -05:00
notopics : topics . length === 0
2013-09-24 14:18:41 -04:00
} ) ;
} ) ;
} ) ;
2013-09-17 13:10:14 -04:00
2013-12-05 14:24:18 -05:00
app . namespace ( '/database' , function ( ) {
2013-09-24 15:36:17 -04:00
app . get ( '/' , function ( req , res ) {
2013-12-02 21:20:55 -05:00
db . info ( function ( err , data ) {
res . json ( data ) ;
2013-09-24 15:36:17 -04:00
} ) ;
2013-09-24 14:18:41 -04:00
} ) ;
2013-08-02 14:29:40 -04:00
} ) ;
2013-05-17 14:22:34 -04:00
2013-12-29 18:10:42 -05:00
app . get ( '/events' , function ( req , res , next ) {
events . getLog ( function ( err , data ) {
if ( err ) {
return next ( err ) ;
}
2014-02-19 13:57:09 -05:00
if ( data ) {
data = data . toString ( ) . split ( '\n' ) . reverse ( ) . join ( '\n' ) ;
}
res . json ( 200 , { eventdata : data } ) ;
2013-12-29 18:10:42 -05:00
} ) ;
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/plugins' , function ( req , res ) {
plugins . showInstalled ( function ( err , plugins ) {
if ( err || ! Array . isArray ( plugins ) ) plugins = [ ] ;
2013-08-02 14:29:40 -04:00
2013-09-24 14:18:41 -04:00
res . json ( 200 , {
plugins : plugins
} ) ;
2013-08-02 14:29:40 -04:00
} ) ;
} ) ;
2013-05-09 03:33:53 +00:00
2014-01-09 13:29:41 -05:00
app . get ( '/languages' , function ( req , res ) {
Languages . list ( function ( err , languages ) {
res . send ( 200 , {
languages : languages
} ) ;
} ) ;
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/settings' , function ( req , res ) {
res . json ( 200 , { } ) ;
} ) ;
2013-08-02 14:29:40 -04:00
2013-09-24 14:18:41 -04:00
app . get ( '/motd' , function ( req , res ) {
res . json ( 200 , { } ) ;
} ) ;
2013-06-20 14:45:38 -04:00
2013-10-02 00:25:46 -04:00
app . get ( '/logger' , function ( req , res ) {
res . json ( 200 , { } ) ;
} ) ;
2013-09-24 14:18:41 -04:00
app . get ( '/themes' , function ( req , res ) {
2014-02-19 16:11:16 -05:00
async . parallel ( {
areas : function ( next ) {
plugins . fireHook ( 'filter:widgets.getAreas' , [ ] , next ) ;
} ,
widgets : function ( next ) {
plugins . fireHook ( 'filter:widgets.getWidgets' , [ ] , next ) ;
}
} , function ( err , data ) {
async . each ( data . areas , function ( area , next ) {
2014-02-19 15:07:38 -05:00
widgets . getArea ( area . template , area . location , function ( err , areaData ) {
area . data = areaData ;
next ( err ) ;
} ) ;
} , function ( err ) {
2014-02-20 17:58:12 -05:00
for ( var w in data . widgets ) {
if ( data . widgets . hasOwnProperty ( w ) ) {
data . widgets [ w ] . content += "<br /><label>Title:</label><input type=\"text\" class=\"form-control\" name=\"title\" placeholder=\"Title (only shown on some containers)\" /><br /><label>Container:</label><textarea rows=\"4\" class=\"form-control container-html\" name=\"container\" placeholder=\"Drag and drop a container or enter HTML here.\"></textarea>" ;
}
}
2014-02-19 15:07:38 -05:00
res . json ( 200 , {
2014-02-19 16:11:16 -05:00
areas : data . areas ,
widgets : data . widgets
2014-02-19 15:07:38 -05:00
} ) ;
2014-02-19 11:43:25 -05:00
} ) ;
} ) ;
2013-09-24 14:18:41 -04:00
} ) ;
2013-08-02 14:29:40 -04:00
2013-09-24 14:18:41 -04:00
app . get ( '/testing/categories' , function ( req , res ) {
res . json ( 200 , { } ) ;
} ) ;
2013-06-20 14:45:38 -04:00
2013-09-24 14:18:41 -04:00
app . get ( '/groups' , function ( req , res ) {
2014-02-19 18:44:31 -05:00
async . parallel ( [
function ( next ) {
groups . list ( {
expand : true
} , next ) ;
} ,
function ( next ) {
groups . listSystemGroups ( {
expand : true
} , next ) ;
}
] , function ( err , data ) {
var groups = data [ 0 ] . concat ( data [ 1 ] ) ;
2013-09-24 14:18:41 -04:00
res . json ( 200 , {
2014-01-21 19:37:01 +00:00
groups : groups ,
yourid : req . user . uid
2013-09-24 14:18:41 -04:00
} ) ;
2013-08-29 14:55:30 -04:00
} ) ;
} ) ;
} ) ;
2013-05-09 03:33:53 +00:00
} ;
2013-10-02 00:25:46 -04:00
} ( exports ) ) ;