Skip to content

Commit a559189

Browse files
committed
Initial server commit.
1 parent 22d9d02 commit a559189

38 files changed

+3436
-6
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ build/
2727
#-----------------------------
2828
# PROJECT SPECIFIC
2929
#-----------------------------
30-
_db.php
3130
regexr.css
3231
*.map
3332
scripts.min.js
3433
yarn.lock
3534
TMP_*
3635
/deploy
37-
/server
38-
/index.php
36+
server/cache/
37+
server/Config.php
38+
server/**/.php_cs.cache
39+
server/vendor/

gulpfile.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ gulp.task("build-js", function() {
5454
sourceMap: true,
5555
moduleContext: {"dev/lib/codemirror.js":"window"},
5656
plugins: [babel({
57-
presets: [["es2015",{"modules": false}]],
57+
presets: [["es2015",{"modules": false}]],
5858
plugins: ["external-helpers"],
5959
include: "./dev/src/**",
6060
babelrc: false
@@ -75,7 +75,7 @@ gulp.task("build-js-prod", function() {
7575
entry: "dev/src/app.js",
7676
moduleContext: {"dev/lib/codemirror.js":"window"},
7777
plugins: [babel({
78-
presets: [["es2015",{"modules": false}]],
78+
presets: [["es2015",{"modules": false}]],
7979
plugins: ["external-helpers"],
8080
include: "./dev/src/**",
8181
babelrc: false
@@ -152,6 +152,7 @@ gulp.task("copy-build", function() {
152152
'deploy/**', 'assets/**', 'index.*', 'server/**',
153153
'!deploy/*.map',
154154
'!server/**/composer.*',
155+
'!server/**/*.sql',
155156
'!server/**/*.md',
156157
'!server/gulpfile.js',
157158
'!server/Config*.php',
@@ -165,7 +166,7 @@ gulp.task("copy-build", function() {
165166

166167
function createFileHash(filename) {
167168
const hash = crypto.createHash('sha256');
168-
169+
169170
const fileContents = fs.readFileSync(filename, 'utf-8');
170171
hash.update(fileContents);
171172
return hash.digest('hex').slice(0, 9);

index.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/*
3+
RegExr: Learn, Build, & Test RegEx
4+
Copyright (C) 2017 gskinner.com, inc.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
try {
21+
require_once("./server/bootstrap.php");
22+
$api = new \core\API();
23+
$api->connect();
24+
$db = $api->getDB();
25+
26+
$userProfile = (object)(new \account\verify())->run($db);
27+
} catch (\Exception $ex) {
28+
$userProfile = [];
29+
}
30+
31+
$pattern = 'null';
32+
$result = [];
33+
$success = preg_match("/([a-z0-9]+)\$/", $_SERVER['REQUEST_URI'], $result);
34+
if ($success == true) {
35+
$stringId = $result[0];
36+
$id = convertFromURL($stringId);
37+
if (!is_null($id) && $id > 0) {
38+
try {
39+
$pattern = json_encode((new \patterns\load(['patternId'=>$stringId]))->run($db));
40+
} catch (\Exception $ex) {
41+
$pattern = null;
42+
}
43+
}
44+
}
45+
46+
$defaults = json_encode([
47+
"userId" => idx($userProfile, 'userId'),
48+
"authenticate" => idx($userProfile, 'authenticated'),
49+
"username" => idx($userProfile, 'username'),
50+
"author" => idx($userProfile, 'author'),
51+
"type" => idx($userProfile, 'type')
52+
]);
53+
54+
$versions = json_encode([
55+
"PCREVersion" => PCRE_VERSION,
56+
"PHPVersion" => PHP_VERSION
57+
]);
58+
59+
$pattern = is_null($pattern)?'null':$pattern;
60+
61+
$initTemplate = "regexr.init($pattern,$defaults,$versions);";
62+
63+
$indexFile = file_get_contents('./index.html');
64+
65+
$openScriptTag = '<script id="phpinject">';
66+
$closeScriptTag = '</script>';
67+
$scriptIdx = strrpos($indexFile, $openScriptTag) + strlen($openScriptTag);
68+
$endIndexFile = strrpos($indexFile, $closeScriptTag, $scriptIdx);
69+
70+
ob_start('ob_gzhandler');
71+
echo(substr($indexFile, 0, $scriptIdx) . $initTemplate . substr($indexFile, $endIndexFile));
72+
ob_flush();

server/.htaccess

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Switch to PHP 7.x
2+
AddHandler phplatest-script .php

server/Config.sample.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/*
3+
RegExr: Learn, Build, & Test RegEx
4+
Copyright (C) 2017 gskinner.com, inc.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
// Database defaults.
21+
define('DB_HOST', 'YOUR_DB_HOST');
22+
define('DB_USER_NAME', 'YOUR_DB_USER_NAME');
23+
define('DB_PASSWORD', 'YOUR_DB_PASSWORD');
24+
define('DB_NAME', 'YOUR_DB_NAME');
25+
26+
define('CACHE_PATH', './cache/');
27+
define('DEBUG', true);
28+
29+
// OAUTH
30+
define('SESSION_NAME', 'session');
31+
32+
if (DEBUG) {
33+
// We can pass a special session header for debugging, this key must match to use it.
34+
define('DEBUG_SESSION', '--some random string--');
35+
}
36+
37+
// Create one at: https://console.developers.google.com/apis/credentials
38+
// You also need to enable Google+ support https://console.developers.google.com/apis/api/plus.googleapis.com
39+
define('GOOGLE_ID', '--');
40+
define('GOOGLE_SECRET', '--');
41+
42+
// Create one at: https://github.com/settings/applications/
43+
define('GITHUB_ID', '--');
44+
define('GITHUB_SECRET', '--');
45+
46+
// https://developers.facebook.com/apps
47+
define('FACEBOOK_ID', '--');
48+
define('FACEBOOK_SECRET', '--');

server/Maintenance.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/*
3+
RegExr: Learn, Build, & Test RegEx
4+
Copyright (C) 2017 gskinner.com, inc.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
require_once("bootstrap.php");
21+
22+
if (!isCLI()) {
23+
exit("cli only, bye.");
24+
}
25+
26+
/**
27+
* Maintenance class that should run on a cron.
28+
* Runs cleanup functions on the database / filesystem.
29+
*/
30+
31+
$api = new \core\API();
32+
33+
$api->connect();
34+
$db = $api->getDB();
35+
36+
// TODO: Write flag for if this is done or not.
37+
$runCount = 0;
38+
$deletedCount = 0;
39+
while (true) {
40+
// Delete temporary users, and their private patterns, when they have no session.
41+
$users = $db->query("SELECT u.id as userId
42+
FROM users as u
43+
LEFT JOIN sessions as s ON s.userId=u.id
44+
WHERE u.type='temporary' && s.id IS NULL LIMIT 100000");
45+
46+
$tmpUserCount = count($users);
47+
48+
if ($tmpUserCount == 0) {
49+
break;
50+
}
51+
52+
$deletedCount += $tmpUserCount;
53+
54+
$runCount++;
55+
56+
$usersToDelete = quoteStringArray(array_column($users, "userId"));
57+
58+
$db->begin();
59+
$db->query("DELETE FROM patterns WHERE visibility='private' && owner IN ($usersToDelete)");
60+
$db->query("DELETE FROM favorites WHERE userId IN ($usersToDelete)");
61+
$db->query("DELETE FROM users WHERE id IN ($usersToDelete)");
62+
$db->commit();
63+
64+
sleep(3);
65+
}
66+
67+
echo("Completed! Deleted $deletedCount users.\n");

0 commit comments

Comments
 (0)