Browse Source

[wip]

master
Muthu Kumar 6 years ago
parent
commit
b67a3190af
  1. 4
      README.md
  2. 14
      config.js
  3. 124
      package-lock.json
  4. 1925
      php.ini
  5. 120
      php.js
  6. 14
      plugins/loader.js
  7. 40
      server.js

4
README.md

@ -22,6 +22,4 @@ Steps 1-3 will have an automated option later.
Thanks to @wbhob for donating the npm `w` package to me.
Because npm versions are immutable, our public version number starts directly to `v.1.1.0`. The project should be considered unstable until `v.2.0.0` which will be WordBoxed's first stable release version.
Like many software, `WordBox` will have an internal version number and external version number. The current internal version is `v.0.x.x` and external version is `v.1.x.x`. As the internal version reaches public release, it will skip over from `v.0.9.x` to `v.2.0.0` and catch up with its external version.
Because npm versions are immutable, our public version number starts directly to `v.1.1.0`. The project should be considered unstable until `v.2.0.0` which will be WordBox's first stable release version.

14
config.js

@ -2,10 +2,12 @@
const { normalize } = require('path')
module.exports =
{
module.exports = {
port: 8080,
phpBin: normalize(__dirname + '/php/php-cgi'),
publicPath: normalize(__dirname + '/public'),
plugins: []
}
plugins: [],
php: {
cgi: normalize(__dirname + '/php/php-cgi'),
ini: normalize(__dirname + '/php.ini'),
public: normalize(__dirname + '/public'),
},
};

124
package-lock.json

@ -9,7 +9,7 @@
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.4.tgz",
"integrity": "sha1-hiRnWMfdbSGmR0/whKR0DsBesh8=",
"requires": {
"mime-types": "2.1.17",
"mime-types": "~2.1.16",
"negotiator": "0.6.1"
}
},
@ -23,7 +23,7 @@
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.0.tgz",
"integrity": "sha512-NnSOmMEYtVR2JVMIGTzynRkkaxtiq1xnFBcdQD/DnNCYPoEPsVJhM98BDyaoNOQIi7p4okdi3E27eN7GQbsUug==",
"requires": {
"color-convert": "1.9.1"
"color-convert": "^1.9.0"
}
},
"app-root-path": {
@ -42,15 +42,15 @@
"integrity": "sha1-h2eKGdhLR9hZuDGZvVm84iKxBFQ=",
"requires": {
"bytes": "3.0.0",
"content-type": "1.0.4",
"content-type": "~1.0.4",
"debug": "2.6.9",
"depd": "1.1.2",
"http-errors": "1.6.2",
"depd": "~1.1.1",
"http-errors": "~1.6.2",
"iconv-lite": "0.4.19",
"on-finished": "2.3.0",
"on-finished": "~2.3.0",
"qs": "6.5.1",
"raw-body": "2.3.2",
"type-is": "1.6.15"
"type-is": "~1.6.15"
}
},
"bytes": {
@ -63,9 +63,9 @@
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.3.0.tgz",
"integrity": "sha512-Az5zJR2CBujap2rqXGaJKaPHyJ0IrUimvYNX+ncCy8PJP4ltOGTrHUIo097ZaL2zMeKYpiCdqDvS6zdrTFok3Q==",
"requires": {
"ansi-styles": "3.2.0",
"escape-string-regexp": "1.0.5",
"supports-color": "4.5.0"
"ansi-styles": "^3.1.0",
"escape-string-regexp": "^1.0.5",
"supports-color": "^4.0.0"
}
},
"color-convert": {
@ -73,7 +73,7 @@
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.1.tgz",
"integrity": "sha512-mjGanIiwQJskCC18rPR6OmrZ6fm2Lc7PeGFYwCmy5J34wC6F1PzdGL6xeMfmgicfYcNLGuVFA3WzXtIDCQSZxQ==",
"requires": {
"color-name": "1.1.3"
"color-name": "^1.1.1"
}
},
"color-name": {
@ -149,36 +149,36 @@
"resolved": "https://registry.npmjs.org/express/-/express-4.16.2.tgz",
"integrity": "sha1-41xt/i1kt9ygpc1PIXgb4ymeB2w=",
"requires": {
"accepts": "1.3.4",
"accepts": "~1.3.4",
"array-flatten": "1.1.1",
"body-parser": "1.18.2",
"content-disposition": "0.5.2",
"content-type": "1.0.4",
"content-type": "~1.0.4",
"cookie": "0.3.1",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "1.1.2",
"encodeurl": "1.0.1",
"escape-html": "1.0.3",
"etag": "1.8.1",
"depd": "~1.1.1",
"encodeurl": "~1.0.1",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"finalhandler": "1.1.0",
"fresh": "0.5.2",
"merge-descriptors": "1.0.1",
"methods": "1.1.2",
"on-finished": "2.3.0",
"parseurl": "1.3.2",
"methods": "~1.1.2",
"on-finished": "~2.3.0",
"parseurl": "~1.3.2",
"path-to-regexp": "0.1.7",
"proxy-addr": "2.0.2",
"proxy-addr": "~2.0.2",
"qs": "6.5.1",
"range-parser": "1.2.0",
"range-parser": "~1.2.0",
"safe-buffer": "5.1.1",
"send": "0.16.1",
"serve-static": "1.13.1",
"setprototypeof": "1.1.0",
"statuses": "1.3.1",
"type-is": "1.6.15",
"statuses": "~1.3.1",
"type-is": "~1.6.15",
"utils-merge": "1.0.1",
"vary": "1.1.2"
"vary": "~1.1.2"
},
"dependencies": {
"setprototypeof": {
@ -198,10 +198,10 @@
"resolved": "https://registry.npmjs.org/express-htaccess-middleware/-/express-htaccess-middleware-0.1.0.tgz",
"integrity": "sha1-n2qzO1Pw/4fNAWDBeMdj+VcFht4=",
"requires": {
"app-root-path": "1.4.0",
"chalk": "1.1.3",
"app-root-path": "^1.2.1",
"chalk": "^1.1.3",
"htaccess-parser": "0.1.0",
"lodash": "4.17.4"
"lodash": "^4.13.1"
},
"dependencies": {
"ansi-styles": {
@ -214,11 +214,11 @@
"resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",
"integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=",
"requires": {
"ansi-styles": "2.2.1",
"escape-string-regexp": "1.0.5",
"has-ansi": "2.0.0",
"strip-ansi": "3.0.1",
"supports-color": "2.0.0"
"ansi-styles": "^2.2.1",
"escape-string-regexp": "^1.0.2",
"has-ansi": "^2.0.0",
"strip-ansi": "^3.0.0",
"supports-color": "^2.0.0"
}
},
"supports-color": {
@ -234,12 +234,12 @@
"integrity": "sha1-zgtoVbRYU+eRsvzGgARtiCU91/U=",
"requires": {
"debug": "2.6.9",
"encodeurl": "1.0.1",
"escape-html": "1.0.3",
"on-finished": "2.3.0",
"parseurl": "1.3.2",
"statuses": "1.3.1",
"unpipe": "1.0.0"
"encodeurl": "~1.0.1",
"escape-html": "~1.0.3",
"on-finished": "~2.3.0",
"parseurl": "~1.3.2",
"statuses": "~1.3.1",
"unpipe": "~1.0.0"
},
"dependencies": {
"statuses": {
@ -269,7 +269,7 @@
"resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz",
"integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=",
"requires": {
"ansi-regex": "2.1.1"
"ansi-regex": "^2.0.0"
}
},
"has-flag": {
@ -282,7 +282,7 @@
"resolved": "https://registry.npmjs.org/htaccess-parser/-/htaccess-parser-0.1.0.tgz",
"integrity": "sha1-j0NZviVaBkaNsYnfKkmAeGXWA3s=",
"requires": {
"graceful-fs": "4.1.11"
"graceful-fs": "^4.1.4"
}
},
"http-errors": {
@ -293,7 +293,7 @@
"depd": "1.1.1",
"inherits": "2.0.3",
"setprototypeof": "1.0.3",
"statuses": "1.4.0"
"statuses": ">= 1.3.1 < 2"
},
"dependencies": {
"depd": {
@ -319,9 +319,9 @@
"integrity": "sha1-1LUFvemUaYfM8PxY2QEP+WB+P6A="
},
"lodash": {
"version": "4.17.4",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz",
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4="
"version": "4.17.11",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz",
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg=="
},
"media-typer": {
"version": "0.3.0",
@ -353,7 +353,7 @@
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.17.tgz",
"integrity": "sha1-Cdejk/A+mVp5+K+Fe3Cp4KsWVXo=",
"requires": {
"mime-db": "1.30.0"
"mime-db": "~1.30.0"
}
},
"ms": {
@ -389,7 +389,7 @@
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.2.tgz",
"integrity": "sha1-ZXFQT0e7mI7IGAJT+F3X4UlSvew=",
"requires": {
"forwarded": "0.1.2",
"forwarded": "~0.1.2",
"ipaddr.js": "1.5.2"
}
},
@ -425,18 +425,18 @@
"integrity": "sha512-ElCLJdJIKPk6ux/Hocwhk7NFHpI3pVm/IZOYWqUmoxcgeyM+MpxHHKhb8QmlJDX1pU6WrgaHBkVNm73Sv7uc2A==",
"requires": {
"debug": "2.6.9",
"depd": "1.1.2",
"destroy": "1.0.4",
"encodeurl": "1.0.1",
"escape-html": "1.0.3",
"etag": "1.8.1",
"depd": "~1.1.1",
"destroy": "~1.0.4",
"encodeurl": "~1.0.1",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"fresh": "0.5.2",
"http-errors": "1.6.2",
"http-errors": "~1.6.2",
"mime": "1.4.1",
"ms": "2.0.0",
"on-finished": "2.3.0",
"range-parser": "1.2.0",
"statuses": "1.3.1"
"on-finished": "~2.3.0",
"range-parser": "~1.2.0",
"statuses": "~1.3.1"
},
"dependencies": {
"statuses": {
@ -451,9 +451,9 @@
"resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.13.1.tgz",
"integrity": "sha512-hSMUZrsPa/I09VYFJwa627JJkNs0NrfL1Uzuup+GqHfToR2KcsXFymXSV90hoyw3M+msjFuQly+YzIH/q0MGlQ==",
"requires": {
"encodeurl": "1.0.1",
"escape-html": "1.0.3",
"parseurl": "1.3.2",
"encodeurl": "~1.0.1",
"escape-html": "~1.0.3",
"parseurl": "~1.3.2",
"send": "0.16.1"
}
},
@ -472,7 +472,7 @@
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
"integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=",
"requires": {
"ansi-regex": "2.1.1"
"ansi-regex": "^2.0.0"
}
},
"supports-color": {
@ -480,7 +480,7 @@
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.5.0.tgz",
"integrity": "sha1-vnoN5ITexcXN34s9WRJQRJEvY1s=",
"requires": {
"has-flag": "2.0.0"
"has-flag": "^2.0.0"
}
},
"type-is": {
@ -489,7 +489,7 @@
"integrity": "sha1-yrEPtJCeRByChC6v4a1kbIGARBA=",
"requires": {
"media-typer": "0.3.0",
"mime-types": "2.1.17"
"mime-types": "~2.1.15"
}
},
"unpipe": {

1925
php.ini

File diff suppressed because it is too large

120
php.js

@ -1,31 +1,26 @@
// The base work for this module was found at
// https://npmjs.com/package/node-php
// All credits to original author
// I've ES6'd and fixed deprecated Express functions
'use strict';
'use strict'
const url = require('url');
const child = require('child_process');
const path = require('path');
const fs = require('fs');
const url = require('url')
const child = require('child_process')
const path = require('path')
const fs = require('fs')
function runPHP(req, res, { cgi, ini, public }) {
const parts = url.parse(req.url);
const query = parts.query;
function runPHP(req, response, next, phpdir, phpCgi) {
const parts = url.parse(req.url)
const query = parts.query
let file = path.join(phpdir, parts.pathname)
let file = path.join(public, parts.pathname);
if (!fs.existsSync(file)) {
file = path.join(phpdir, "index.php")
file = path.join(public, "index.php");
} else if (fs.statSync(file).isDirectory()) {
file = path.join(file, "index.php")
file = path.join(file, "index.php");
}
let pathinfo = ""
const i = req.url.indexOf(".php")
if (i > 0) pathinfo = parts.pathname.substring(i + 4)
else pathinfo = parts.pathname
let pathinfo = "";
const i = req.url.indexOf(".php");
if (i > 0) pathinfo = parts.pathname.substring(i + 4);
else pathinfo = parts.pathname;
const env = {
SERVER_SIGNATURE: "WordBox Server",
@ -83,71 +78,64 @@ function runPHP(req, response, next, phpdir, phpCgi) {
APPL_MD_PATH: "", //The virtual path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to /.
APPL_PHYSICAL_PATH: "", //The real path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to the same value as DOCUMENT_ROOT.
IS_SUBREQ: "", //It is set to true if the current request is a subrequest, i.e. a request not directly invoked by a client. Otherwise, it is set to true. Subrequests are generated by the server for internal processing. XSSI includes for example result in subrequests.
REDIRECT_STATUS: 1
}
REDIRECT_STATUS: 1,
};
Object
.keys(req.headers)
.map(x =>
env["HTTP_" + x.toUpperCase().replace("-", "_")] = req.headers[x])
.forEach(x =>
env["HTTP_" + x.toUpperCase().replace("-", "_")] = req.headers[x]);
if (/.*?\.php$/.test(file)) {
let res = "", err = ""
let response = "", error = "";
const php = child.spawn(phpCgi, [], {
env: env
})
const php = child.spawn(cgi, [ '-c', ini ], { env });
php.stdin.write(req.rawBody)
php.stdin.write(req.rawBody);
php.stdout.on("data", data => res += data.toString())
php.stderr.on("data", () => err += err.toString())
php.on("error", err => console.error(err))
php.stdout.on("data", data => response += data.toString());
php.stderr.on("data", data => error += data.toString());
php.on("error", err => console.error(err));
php.on("exit", () => {
php.stdin.end()
php.stdin.end();
const lines = res.split("\r\n")
let line = 0
let html = ""
const lines = response.split("\r\n");
let line = 0;
let html = "";
if (lines.length) {
do {
const m = lines[line].split(": ")
if (m[0] === "") break
if (m[0] == "Status") {
response.statusCode = parseInt(m[1])
}
if (m.length == 2) {
response.setHeader(m[0], m[1])
}
line++
} while (lines[line] !== "")
html = lines.splice(line + 1).join("\n")
const m = lines[line].split(": ");
if (m[0] === "") break;
if (m[0] == "Status") res.statusCode = parseInt(m[1]);
if (m.length == 2) res.setHeader(m[0], m[1]);
line++;
} while (lines[line] !== "");
html = lines.splice(line + 1).join("\n");
} else {
html = res
html = response;
}
response.status(response.statusCode).send(html)
response.end()
})
res.status(res.statusCode).send(html);
res.end();
});
} else {
response.sendFile(file)
res.sendFile(file);
}
}
exports.cgi = function (phproot, phpCgi) {
return function (req, res, next) {
let data = null
exports.cgi = function ({ php: config }) {
return function (req, res) {
let data = null;
//req.setEncoding('utf8')
req.setEncoding('utf8');
req.on('data', function (chunk) {
if (!data) data = chunk
else data = data + chunk
})
if (!data) data = chunk;
else data = data + chunk;
});
req.on('end', function () {
req.rawBody = data || ""
runPHP(req, res, next, phproot, phpCgi)
})
}
}
req.rawBody = data || "";
runPHP(req, res, config);
});
};
};

14
plugins/loader.js

@ -1,13 +1,13 @@
'use strict'
const { EOL } = require('os')
const { EOL } = require('os');
function plugins() {
function plugins(app) {
const { plugins } = require('../config')
return plugins.map(plugin => {
require('../plugins/' + plugin + '/main.js')
console.log(EOL + 'Loaded plugin ' + plugin + '...')
})
}
require('../plugins/' + plugin)(app);
console.log(EOL + 'Loaded plugin ' + plugin + '...');
});
};
module.exports = plugins
module.exports = plugins;

40
server.js

@ -1,38 +1,26 @@
'use strict'
'use strict';
const path = require('path')
const fs = require('fs')
const { EOL } = require('os')
const fs = require('fs');
const { EOL } = require('os');
const express = require('express')
const chalk = require('chalk')
const express = require('express');
const chalk = require('chalk');
const php = require('./php')
const config = require('./config')
const plugins = require('./plugins/loader')
const php = require('./php');
const config = require('./config');
const plugins = require('./plugins/loader');
const htaccess = require('express-htaccess-middleware')
const RewriteOptions = {
file: path.resolve(config.publicPath, '.htaccess'),
verbose: (process.env.ENV_NODE == 'development'),
watch: (process.env.ENV_NODE == 'development'),
}
const app = express();
const app = express()
app.use("/", php.cgi(config));
app
.use("/", php.cgi(config.publicPath, config.phpBin))
.use(htaccess(RewriteOptions))
plugins(app); // Load any plugins declared in config
plugins() // Load any plugins declared in config
app.listen(config.port)
const fancy = fs.readFileSync('./fancy.txt')
const fancy = fs.readFileSync('./fancy.txt');
const listeningMsg = (
chalk.yellow(fancy) + EOL +
chalk.green(` Server listening on port ${config.port}` + EOL +
'+---------------------------------+')
)
);
console.log(listeningMsg)
app.listen(config.port, () => console.log(listeningMsg));

Loading…
Cancel
Save