aboutsummaryrefslogtreecommitdiff
path: root/php/cache.js
blob: 2d44cb65764b3ed6a8095195026a6054fd2af10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const uuid = require('uuid');
const fs = require('fs');
const url = require('url');
const path = require('path');
const os = require('os');

function addPhpServerVariable(server, name, value) {
    if (typeof value == "string") {
        return server + "\n$_SERVER['" + name.split("'").join("\\'") + "'] = \"" + value.split("\"").join("\\\"") + "\";";
    } else {
        return server + "\n$_SERVER['" + name.split("'").join("\\'") + "'] = " + value + ";";
    }
}

module.exports = (req, res, file, post, files) => {
    cid = uuid.v4();
    fid = "PHP_" + cid;
    parsed = url.parse(req.url, true);

    content = "<?php\n\nset_include_path(\"" + publicDir + "\" . PATH_SEPARATOR . get_include_path());\n$_PHPID = \"" + cid + "\";\nrequire \"headers.php\";\n\n"
    + "\nregister_shutdown_function(\"__electrode_end_hooks\");"
    + "\n$oldsrv = $_SERVER; $_SERVER = [];"
    + "\n$__electrode_version = \"" + version + "\";"
    + "\n$__electrode_node_version = \"" + process.versions.node + "\";";
    content = addPhpServerVariable(content, "PHP_SELF", path.resolve(file));
    content = addPhpServerVariable(content, "GATEWAY_INTERFACE", "CGI/1.1");
    content = addPhpServerVariable(content, "SERVER_ADDR", Object.values(require('os').networkInterfaces()).reduce((r, list) => r.concat(list.reduce((rr, i) => rr.concat(i.family==='IPv4' && !i.internal && i.address || []), [])), [])[0]);
    content = addPhpServerVariable(content, "SERVER_NAME", os.hostname());
    content = addPhpServerVariable(content, "SERVER_SOFTWARE", config.product.name);
    content = addPhpServerVariable(content, "SERVER_PROTOCOL", "HTTP/" + req.httpVersion);
    content = addPhpServerVariable(content, "REQUEST_METHOD", req.method);
    content = addPhpServerVariable(content, "REQUEST_TIME", REQUEST_START);
    content = addPhpServerVariable(content, "REQUEST_TIME_FLOAT", REQUEST_START_FLOAT);
    content = addPhpServerVariable(content, "QUERY_STRING", parsed.search);
    content = addPhpServerVariable(content, "DOCUMENT_ROOT", path.resolve("./public"));
    if (typeof req.headers["accept"] != "undefined") {
        content = addPhpServerVariable(content, "HTTP_ACCEPT", req.headers["accept"]);
    }
    if (typeof req.headers["accept-charset"] != "undefined") {
        content = addPhpServerVariable(content, "HTTP_ACCEPT_CHARSET", req.headers["accept-charset"]);
    }
    if (typeof req.headers["accept-encoding"] != "undefined") {
        content = addPhpServerVariable(content, "HTTP_ACCEPT_ENCODING", req.headers["accept-encoding"]);
    }
    if (typeof req.headers["accept-language"] != "undefined") {
        content = addPhpServerVariable(content, "HTTP_ACCEPT_LANGUAGE", req.headers["accept-language"]);
    }
    if (typeof req.headers["connection"] != "undefined") {
        content = addPhpServerVariable(content, "HTTP_CONNECTION", req.headers["connection"]);
    }
    if (typeof req.headers["host"] != "undefined") {
        content = addPhpServerVariable(content, "HTTP_HOST", req.headers["host"]);
    }
    content = addPhpServerVariable(content, "HTTP_REFERER", req.url);
    if (typeof req.headers["user-agent"] != "undefined") {
        content = addPhpServerVariable(content, "HTTP_USER_AGENT", req.headers["user-agent"]);
    }
    content = addPhpServerVariable(content, "REMOTE_ADDR", req.connection.remoteAddress);
    content = addPhpServerVariable(content, "REMOTE_PORT", req.connection.remotePort);
    content = addPhpServerVariable(content, "SCRIPT_FILENAME", path.resolve(file));
    content = addPhpServerVariable(content, "SERVER_ADMIN", config.network.admin);
    content = addPhpServerVariable(content, "SERVER_PORT", config.network.port);
    content = addPhpServerVariable(content, "SERVER_SIGNATURE", "<address>" + config.product.name + " version " + version + "</address>");
    content = addPhpServerVariable(content, "PATH_TRANSLATED", path.resolve(file));
    content = addPhpServerVariable(content, "SCRIPT_NAME", file.substr("./public/".length));
    content = addPhpServerVariable(content, "REQUEST_URI", parsed.pathname);
    content = content + "\n$_FILES = [];";
    content = content + "\n$_POST = [];";
    content = content + "\n$_GET = [];";
    content = content + "\n$GLOBALS[\"SYSTEM_ROOT\"] = \"\";";
    Object.keys(parsed.query).forEach((key) => {
        content = content + "\n$_GET[\"" + key.split("\"").join("\\\"").split("$").join("\\\$") + "\"] = \"" + parsed.query[key].split("\"").join("\\\"").split("$").join("\\\$") + "\";"
    })
    Object.keys(post).forEach((key) => {
        content = content + "\n$_POST[\"" + key.split("\"").join("\\\"").split("$").join("\\\$") + "\"] = \"" + post[key].split("\"").join("\\\"").split("$").join("\\\$") + "\";"
    })
    if (typeof files == "object") {
        Object.keys(files).forEach((key) => {
            content = content + "\n$_FILES[\"" + key.split("\"").join("\\\"").split("$").join("\\\$") + "\"] = ['name' => \"" + files[key]["name"].split("\"").join("\\\"") + "\",'type' => \"" + files[key]["type"].split("\"").join("\\\"") + "\",'tmp_name' => \"" + files[key]["path"].split("\"").join("\\\"").split("$").join("\\\$") + "\",'error' => 0,'size' => " + files[key]["size"] + "];"
        })
    }
    cookies = core.cookies(req);
    Object.keys(cookies).forEach((key) => {
        content = content + "\n$_COOKIE[\"" + key.split("\"").join("\\\"").split("$").join("\\\$") + "\"] = \"" + cookies[key].split("\"").join("\\\"").split("$").join("\\\$") + "\";"
    })

    p = "./public"

    regex = /require_once\(["'`].*resources\/private\/relative\.php["'`]\);/gm;
    regex2 = /require_once ["'`].*resources\/private\/relative\.php["'`];/gm;

    fs.writeFileSync(file + ".ELECTRODECACHE~.php", fs.readFileSync(file).toString().replace(regex, "").replace(regex2, ""));
    content = content + "\nchdir(\"" + p + "\");\nfunction getRelativeDetails() {};\nrequire_once \"." + file.split("\"").join("\\\"").substr(8) + ".ELECTRODECACHE~.php" + "\";\n\n__electrode_end_hooks();";

    fs.writeFileSync("./cache/" + fid + ".php", content);
    return [
        "./cache/" + fid + ".php",
        cid,
        file + ".ELECTRODECACHE~.php"
    ];
}