├── .npmignore ├── .editorconfig ├── .jsbeautifyrc ├── .gitattributes ├── plugin.json ├── templates ├── custom-page.tpl └── admin │ └── plugins │ └── custom-pages.tpl ├── README.md ├── package.json ├── LICENSE ├── static └── lib │ └── admin.js ├── .gitignore ├── .eslintrc └── library.js /.npmignore: -------------------------------------------------------------------------------- 1 | sftp-config.json 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [{*.js, *.css, *.tpl, *.json}] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = false 9 | -------------------------------------------------------------------------------- /.jsbeautifyrc: -------------------------------------------------------------------------------- 1 | { 2 | "indent_size": 4, 3 | "indent_char": " ", 4 | "indent_level": 0, 5 | "indent_with_tabs": true, 6 | "preserve_newlines": true, 7 | "max_preserve_newlines": 10, 8 | "jslint_happy": true, 9 | "brace_style": "collapse", 10 | "keep_array_indentation": false, 11 | "keep_function_indentation": false, 12 | "space_before_conditional": true, 13 | "break_chained_methods": false, 14 | "eval_code": false, 15 | "unescape_strings": false, 16 | "wrap_line_length": 0 17 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /plugin.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "nodebb-plugin-custom-pages", 3 | "name": "Custom pages for NodeBB", 4 | "description": "Allows you to add as many new pages as you like to your NodeBB forum", 5 | "url": "https://github.com/NodeBB/nodebb-plugin-custom-pages", 6 | "library": "./library.js", 7 | "hooks": [ 8 | { 9 | "hook": "static:app.load", "method": "init" 10 | }, 11 | { 12 | "hook": "filter:admin.header.build", "method": "addAdminNavigation" 13 | }, 14 | { 15 | "hook": "filter:widgets.getAreas", "method": "setWidgetAreas" 16 | } 17 | ], 18 | "modules": { 19 | "../admin/plugins/custom-pages.js": "static/lib/admin.js" 20 | }, 21 | "templates": "./templates" 22 | } -------------------------------------------------------------------------------- /templates/custom-page.tpl: -------------------------------------------------------------------------------- 1 |
2 | 3 | {widgets.header.html} 4 | 5 |
6 | 7 |
8 |
9 |
10 | 11 | {widgets.content.html} 12 | 13 |
14 |
15 |
16 | 17 | {widgets.sidebar.html} 18 | 19 |
20 |
21 | 22 |
23 | 24 | {widgets.footer.html} 25 | 26 |
27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://packages.nodebb.org/api/v1/plugins/nodebb-plugin-custom-pages/compatibility.png) 2 | 3 | # Custom Pages for NodeBB 4 | 5 | Allows you to add as many new pages as you like to your NodeBB forum. Each new page has four widget areas (header, footer, content, and sidebar) which you can use to add HTML to in the Widgets ACP. 6 | 7 | ### Tips 8 | 9 | * You can set custom permissions for each individual page (ex. group-level access, or registered users only access, etc). 10 | * Use NodeBB's widget system (_Extend -> Widgets_) to add any type of content. 11 | * Utilize [benchpress](https://github.com/benchpressjs/benchpressjs) markup for advanced logic. 12 | * Add a navigation link in the header that points to your custom page in _General -> Navigation_ and selecting "Custom Route". 13 | * Make a custom page your landing page / homepage under _General -> Homepage_ and selecting "Custom" 14 | 15 | ## Manual Installation 16 | 17 | npm install nodebb-plugin-custom-pages 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodebb-plugin-custom-pages", 3 | "version": "2.1.1", 4 | "description": "Allows you to add as many new pages as you like to your NodeBB forum", 5 | "main": "library.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/psychobunny/nodebb-plugin-custom-pages" 9 | }, 10 | "keywords": [ 11 | "nodebb", 12 | "plugin", 13 | "static", 14 | "custom", 15 | "page" 16 | ], 17 | "author": { 18 | "name": "psychobunny", 19 | "email": "psycho.bunny@hotmail.com" 20 | }, 21 | "nbbpm": { 22 | "compatibility": "^4.0.0" 23 | }, 24 | "dependencies": { 25 | "async": "3.2.4", 26 | "mkdirp": "1.0.4" 27 | }, 28 | "devDependencies": { 29 | "eslint": "^6.0.0", 30 | "eslint-config-airbnb-base": "^13.1.0", 31 | "eslint-plugin-import": "^2.17.3" 32 | }, 33 | "license": "BSD-2-Clause", 34 | "bugs": { 35 | "url": "https://github.com/psychobunny/nodebb-plugin-custom-pages/issues" 36 | }, 37 | "readmeFilename": "README.md" 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013-2014, psychobunny 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /static/lib/admin.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* globals define, $, socket, app, ajaxify, jQuery */ 4 | 5 | define('admin/plugins/custom-pages', ['alerts'], function (alerts) { 6 | var admin = {}; 7 | 8 | function addCloseHandler() { 9 | $('#custom-pages .fa-times').on('click', function () { 10 | $(this).parents('.card').remove(); 11 | }); 12 | } 13 | 14 | function addTagsInputForGroups(el) { 15 | el = el || $('#custom-pages .groups-list'); 16 | 17 | el.tagsinput({ 18 | confirmKeys: [13, 44], 19 | trimValue: true, 20 | }); 21 | 22 | app.loadJQueryUI(function () { 23 | var input = $('.page-admin-custom-pages .bootstrap-tagsinput input'); 24 | input.autocomplete({ 25 | delay: 100, 26 | position: { my: 'left bottom', at: 'left top', collision: 'flip' }, 27 | open: function () { 28 | $(this).autocomplete('widget').css('z-index', 20000); 29 | }, 30 | source: ajaxify.data.groups, 31 | select: function () { 32 | // when autocomplete is selected from the dropdown simulate a enter key down to turn it into a tag 33 | // http://stackoverflow.com/a/3276819/583363 34 | var e = jQuery.Event('keypress'); 35 | e.which = 13; 36 | e.keyCode = 13; 37 | setTimeout(function () { 38 | input.trigger(e); 39 | }, 100); 40 | }, 41 | }); 42 | }); 43 | } 44 | 45 | admin.init = function () { 46 | $('#add').on('click', function () { 47 | var clone = $('.template').clone().removeClass('template hidden'); 48 | $('#custom-pages').append(clone); 49 | 50 | addCloseHandler(); 51 | addTagsInputForGroups(clone.find('.groups-list')); 52 | }); 53 | 54 | addCloseHandler(); 55 | addTagsInputForGroups(); 56 | 57 | $('#save').on('click', function () { 58 | var arr = []; 59 | $('#custom-pages form').each(function () { 60 | var data = $(this).serializeArray(); 61 | if (data[1].value && !data[1].value.match(' ') && data[1].value !== '') { 62 | arr.push({ 63 | name: data[0].value, 64 | route: data[1].value, 65 | groups: data[2].value, 66 | }); 67 | } 68 | }); 69 | 70 | socket.emit('admin.settings.saveCustomPages', arr, function () { 71 | alerts.success('Custom pages saved and activated'); 72 | }); 73 | }); 74 | }; 75 | 76 | return admin; 77 | }); 78 | -------------------------------------------------------------------------------- /templates/admin/plugins/custom-pages.tpl: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |

{title}

5 |
6 |
7 | 8 | 9 |
10 |
11 | 12 |
13 |
14 |

15 | Define and customise your new routes here. 16 |

17 |

18 | You can add content to your new routes from Extend → Widgets. 19 |

20 |

21 | You can add your new route to the site navigation from General → Navigation and select "Custom Route". 22 |

23 |

24 | If you wish to set a custom page as your homepage, go to General → Homepage and select "Custom". 25 |

26 | 27 |
28 | 29 |
30 |
31 | 32 |
33 | 36 | 37 | 40 |
41 | 42 |
43 | 44 |
45 | 46 |
47 |
48 | 49 |
50 |
51 |
52 |
53 | 54 | 73 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | 217 | sftp-config.json 218 | node_modules/ 219 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb-base", 3 | "parserOptions": { 4 | "sourceType": "script" 5 | }, 6 | 7 | "rules": { 8 | // Customized 9 | "handle-callback-err": [ "error","^(e$|(e|(.*(_e|E)))rr)" ], 10 | "comma-dangle": ["error", { 11 | "arrays": "always-multiline", 12 | "objects": "always-multiline", 13 | "imports": "always-multiline", 14 | "exports": "always-multiline", 15 | "functions": "never" 16 | }], 17 | "no-empty": ["error", { "allowEmptyCatch": true }], 18 | "no-underscore-dangle": "off", 19 | "newline-per-chained-call": "off", 20 | "no-console": "off", 21 | "no-mixed-operators": ["error", { "allowSamePrecedence": true }], 22 | "strict": ["error", "global"], 23 | "consistent-return": "off", 24 | "func-names": "off", 25 | "no-tabs": "off", 26 | "indent": ["error", "tab"], 27 | "no-eq-null": "off", 28 | "camelcase": "off", 29 | "no-new": "off", 30 | "no-shadow": "off", 31 | "no-use-before-define": ["error", "nofunc"], 32 | "no-prototype-builtins": "off", 33 | "new-cap": "off", 34 | "no-plusplus": ["error", { "allowForLoopAfterthoughts": true }], 35 | "import/no-unresolved": "error", 36 | 37 | // ES6 38 | "prefer-destructuring": "off", 39 | "prefer-rest-params": "off", 40 | "prefer-spread": "off", 41 | "prefer-arrow-callback": "off", 42 | "prefer-template": "off", 43 | "no-var": "off", 44 | "object-shorthand": "off", 45 | "vars-on-top": "off", 46 | 47 | // TODO 48 | "import/no-extraneous-dependencies": "off", 49 | "import/no-dynamic-require": "off", 50 | "import/newline-after-import": "off", 51 | "no-bitwise": "off", 52 | "global-require": "off", 53 | "max-len": "off", 54 | "no-param-reassign": "off", 55 | "no-restricted-syntax": "off", 56 | "no-script-url": "off", 57 | "default-case": "off", 58 | "linebreak-style": "off", 59 | 60 | // "no-multi-assign": "off", 61 | // "one-var": "off", 62 | // "no-undef": "off", 63 | // "max-nested-callbacks": "off", 64 | // "no-mixed-requires": "off", 65 | // "brace-style": "off", 66 | // "max-statements-per-line": "off", 67 | // "no-unused-vars": "off", 68 | // "no-mixed-spaces-and-tabs": "off", 69 | // "no-useless-concat": "off", 70 | // "require-jsdoc": "off", 71 | // "eqeqeq": "off", 72 | // "no-negated-condition": "off", 73 | // "one-var-declaration-per-line": "off", 74 | // "no-lonely-if": "off", 75 | // "radix": "off", 76 | // "no-else-return": "off", 77 | // "no-useless-escape": "off", 78 | // "block-scoped-var": "off", 79 | // "operator-assignment": "off", 80 | // "yoda": "off", 81 | // "no-loop-func": "off", 82 | // "no-void": "off", 83 | // "valid-jsdoc": "off", 84 | // "no-cond-assign": "off", 85 | // "no-redeclare": "off", 86 | // "no-unreachable": "off", 87 | // "no-nested-ternary": "off", 88 | // "operator-linebreak": "off", 89 | // "guard-for-in": "off", 90 | // "no-unneeded-ternary": "off", 91 | // "no-sequences": "off", 92 | // "no-extend-native": "off", 93 | // "no-shadow-restricted-names": "off", 94 | // "no-extra-boolean-cast": "off", 95 | // "no-path-concat": "off", 96 | // "no-unused-expressions": "off", 97 | // "no-return-assign": "off", 98 | // "no-restricted-modules": "off", 99 | // "object-curly-spacing": "off", 100 | // "indent": "off", 101 | // "padded-blocks": "off", 102 | // "eol-last": "off", 103 | // "lines-around-directive": "off", 104 | // "strict": "off", 105 | // "comma-dangle": "off", 106 | // "no-multi-spaces": "off", 107 | // "quotes": "off", 108 | // "keyword-spacing": "off", 109 | // "no-mixed-operators": "off", 110 | // "comma-spacing": "off", 111 | // "no-trailing-spaces": "off", 112 | // "key-spacing": "off", 113 | // "no-multiple-empty-lines": "off", 114 | // "spaced-comment": "off", 115 | // "space-in-parens": "off", 116 | // "block-spacing": "off", 117 | // "quote-props": "off", 118 | // "space-unary-ops": "off", 119 | // "no-empty": "off", 120 | // "dot-notation": "off", 121 | // "func-call-spacing": "off", 122 | // "array-bracket-spacing": "off", 123 | // "object-property-newline": "off", 124 | // "no-continue": "off", 125 | // "no-extra-semi": "off", 126 | // "no-spaced-func": "off", 127 | // "no-useless-return": "off" 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /library.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const plugin = module.exports; 4 | 5 | const mkdirp = require('mkdirp'); 6 | const async = require('async'); 7 | 8 | const nconf = require.main.require('nconf'); 9 | const winston = require.main.require('winston'); 10 | 11 | const db = require.main.require('./src/database'); 12 | const user = require.main.require('./src/user'); 13 | const widgets = require.main.require('./src/widgets'); 14 | const groups = require.main.require('./src/groups'); 15 | const controllerHelpers = require.main.require('./src/controllers/helpers'); 16 | const pubsub = require.main.require('./src/pubsub'); 17 | 18 | const fs = require('fs'); 19 | const path = require('path'); 20 | 21 | plugin.init = async function (params) { 22 | var app = params.router; 23 | var middleware = params.middleware; 24 | 25 | var helpers = require.main.require('./src/routes/helpers'); 26 | helpers.setupAdminPageRoute(app, '/admin/plugins/custom-pages', renderAdmin); 27 | 28 | app.get('*', function routeToCustomPage(req, res, next) { 29 | if (!plugin.pagesHash || !plugin.pagesHash[cleanPath(req.path)]) { 30 | return setImmediate(next); 31 | } 32 | 33 | res.locals.isAPI = req.path.startsWith('/api'); 34 | let middlewares = [middleware.maintenanceMode, middleware.registrationComplete, middleware.pageView, middleware.pluginHooks]; 35 | if (!res.locals.isAPI) { 36 | middlewares = [middleware.busyCheck, middleware.applyCSRF, middleware.buildHeader].concat(middlewares); 37 | } 38 | async.eachSeries(middlewares, function (middleware, next) { 39 | middleware(req, res, next); 40 | }, async function (err) { 41 | if (err) { 42 | return next(err); 43 | } 44 | await renderCustomPage(req, res, next); 45 | }); 46 | }); 47 | 48 | var SocketAdmin = require.main.require('./src/socket.io/admin'); 49 | SocketAdmin.settings.saveCustomPages = async function (socket, data) { 50 | await resetWidgets(data); 51 | pubsub.publish('custom-pages:save', data); 52 | await db.set('plugins:custom-pages', JSON.stringify(data)); 53 | }; 54 | 55 | const pages = await getCustomPages(); 56 | await plugin.saveTemplates(pages); 57 | }; 58 | 59 | pubsub.on('custom-pages:save', async function (pages) { 60 | storeData(pages); 61 | await plugin.saveTemplates(pages); 62 | }); 63 | 64 | function cleanPath(path) { 65 | return path.replace(/\/(api\/)?/, '').replace(/\/$/, ''); 66 | } 67 | 68 | async function renderCustomPage(req, res) { 69 | var path = cleanPath(req.path); 70 | var groupList = plugin.pagesHash[path].groups ? plugin.pagesHash[path].groups.split(',') : []; 71 | 72 | const isAdmin = await user.isAdministrator(req.uid); 73 | 74 | if (!isAdmin && groupList.length) { 75 | const groupMembership = await groups.isMemberOfGroups(req.uid, groupList); 76 | if (!groupMembership.some(a => !!a)) { 77 | return controllerHelpers.notAllowed(req, res); 78 | } 79 | } 80 | const userData = await user.getUsers([req.uid], req.uid); 81 | res.render(path, { 82 | title: plugin.pagesHash[path].name, 83 | user: userData[0], 84 | }); 85 | } 86 | 87 | async function renderAdmin(req, res) { 88 | const [pages, groups] = await Promise.all([ 89 | getCustomPages(), 90 | getGroupList(), 91 | ]); 92 | res.render('admin/plugins/custom-pages', { 93 | title: 'Custom Pages', 94 | pages: pages, 95 | groups: groups, 96 | }); 97 | } 98 | 99 | async function getCustomPages() { 100 | if (plugin.pagesCache) { 101 | return plugin.pagesCache; 102 | } 103 | 104 | const data = await db.get('plugins:custom-pages'); 105 | storeData(JSON.parse(data)); 106 | return plugin.pagesCache; 107 | } 108 | 109 | function storeData(pages) { 110 | if (pages == null) { 111 | pages = []; 112 | } 113 | 114 | // Eliminate errors in route definition 115 | plugin.pagesCache = pages.map(function (pageObj) { 116 | pageObj.route = pageObj.route.replace(/^\/*/g, ''); // trim leading slashes from route 117 | return pageObj; 118 | }); 119 | 120 | plugin.pagesHash = plugin.pagesCache.reduce(function (memo, cur) { 121 | memo[cur.route] = cur; 122 | return memo; 123 | }, {}); 124 | } 125 | 126 | async function getGroupList() { 127 | const groupNames = await groups.getGroups('groups:createtime', 0, -1); 128 | return groupNames.filter(groupName => !groups.isPrivilegeGroup(groupName)); 129 | } 130 | 131 | plugin.setWidgetAreas = async function (areas) { 132 | const data = await getCustomPages(); 133 | 134 | for (var d in data) { 135 | if (data.hasOwnProperty(d)) { 136 | areas = areas.concat([ 137 | { 138 | name: data[d].name + ' Header', 139 | template: data[d].route + '.tpl', 140 | location: 'header', 141 | }, 142 | { 143 | name: data[d].name + ' Footer', 144 | template: data[d].route + '.tpl', 145 | location: 'footer', 146 | }, 147 | { 148 | name: data[d].name + ' Sidebar', 149 | template: data[d].route + '.tpl', 150 | location: 'sidebar', 151 | }, 152 | { 153 | name: data[d].name + ' Content', 154 | template: data[d].route + '.tpl', 155 | location: 'content', 156 | }, 157 | ]); 158 | } 159 | } 160 | return areas; 161 | }; 162 | 163 | plugin.addAdminNavigation = async function (header) { 164 | header.plugins.push({ 165 | route: '/plugins/custom-pages', 166 | icon: 'fa-mobile', 167 | name: 'Custom Pages', 168 | }); 169 | return header; 170 | }; 171 | 172 | plugin.saveTemplates = async function (pages) { 173 | if (!nconf.get('isPrimary')) { 174 | return; 175 | } 176 | 177 | const bjs = require.main.require('benchpressjs'); 178 | const customTPL = await fs.promises.readFile(path.join(__dirname, 'templates/custom-page.tpl'), 'utf-8'); 179 | try { 180 | await async.each(pages, async function (pageObj) { 181 | const route = pageObj.route; 182 | 183 | const jsPath = path.join(nconf.get('views_dir'), route + '.js'); 184 | const tplPath = path.join(nconf.get('views_dir'), route + '.tpl'); 185 | 186 | const compiled = await bjs.precompile(customTPL, {}); 187 | 188 | if (path.dirname(route) !== '.') { 189 | // Subdirectories specified 190 | await mkdirp(path.join(nconf.get('views_dir'), path.dirname(route))); 191 | } 192 | await saveFiles(jsPath, tplPath, compiled, customTPL); 193 | }); 194 | } catch (err) { 195 | winston.error('[plugin/custom-pages] Could not save templates!'); 196 | winston.error(' ' + err.message); 197 | throw err; 198 | } 199 | }; 200 | 201 | async function saveFiles(jsPath, tplPath, compiled, customTPL) { 202 | await fs.promises.writeFile(jsPath, compiled); 203 | await fs.promises.writeFile(tplPath, customTPL); 204 | } 205 | 206 | async function resetWidgets(data) { 207 | var removedRoutes = []; 208 | if (plugin.pagesHash) { 209 | Object.keys(plugin.pagesHash).forEach(function (route) { 210 | var match = data.find(page => page.route === route); 211 | 212 | if (!match) { 213 | removedRoutes.push(route); 214 | } 215 | }); 216 | } 217 | 218 | await widgets.resetTemplates(removedRoutes); 219 | } 220 | --------------------------------------------------------------------------------