Skip to content

Commit 2f55ea8

Browse files
Merge pull request #1 from JSON-ms/dev
merge dev into master
2 parents 5b4fb04 + 65e8acb commit 2f55ea8

27 files changed

Lines changed: 449 additions & 102 deletions

.env.example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# General
2-
INTERFACE_EDITOR_DOMAIN=localhost:3000
32
INTERFACE_EDITOR_URL=http://localhost:3000
43
ACCESS_CONTROL_ALLOW_ORIGIN=http://localhost:3000
5-
PUBLIC_FILE_PATH=http://localhost:9200/private/files/
64
JSONMS_CYPHER_KEY=urjMdK071cL935eKdczjEQ==
75

86
# Database

.github/workflows/deploy-dev.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Deploy DEV to host
2+
3+
on:
4+
push:
5+
branches:
6+
- dev # Change this to your default branch
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Set up PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: '8.3'
20+
tools: composer
21+
22+
- name: Install dependencies
23+
run: composer install --no-dev --optimize-autoloader
24+
25+
- name: Zip the folder
26+
run: zip -r archive.zip . -x "./.git/*" "./.github/*"
27+
28+
- name: Deploy via SSH
29+
uses: appleboy/scp-action@v0.1.7
30+
with:
31+
host: ${{ secrets.FTP_SERVER }}
32+
username: ${{ secrets.FTP_USERNAME }}
33+
password: ${{ secrets.FTP_PASSWORD }}
34+
source: "archive.zip"
35+
target: ${{ secrets.FTP_SERVER_DEV_PATH }}
36+
37+
- name: SSH into server and unzip
38+
uses: appleboy/ssh-action@v1.0.3
39+
with:
40+
host: ${{ secrets.FTP_SERVER }}
41+
username: ${{ secrets.FTP_USERNAME }}
42+
password: ${{ secrets.FTP_PASSWORD }}
43+
script: |
44+
cd ${{ secrets.FTP_SERVER_DEV_PATH }}
45+
unzip -o archive.zip -d .
46+
rm archive.zip
47+
echo "INTERFACE_EDITOR_URL=https://dev.json.ms" > .env
48+
echo "ACCESS_CONTROL_ALLOW_ORIGIN=https://dev.json.ms" >> .env
49+
echo "JSONMS_CYPHER_KEY=${{ secrets.JSONMS_CYPHER_KEY }}" >> .env
50+
echo "GOOGLE_OAUTH_CLIENT_ID=637442439591-qrrpb3v9d3n5m8b8gheorfa1fbi5o6qc.apps.googleusercontent.com" >> .env
51+
echo "GOOGLE_OAUTH_CLIENT_SECRET=${{ secrets.GOOGLE_OAUTH_CLIENT_SECRET }}" >> .env
52+
echo "GOOGLE_OAUTH_CALLBACK_URL=https://server.dev.json.ms/google/callback" >> .env
53+
echo "DATABASE_HOST=${{ secrets.DATABASE_HOST }}" >> .env
54+
echo "DATABASE_DBNAME=${{ secrets.DATABASE_DEV_DBNAME }}" >> .env
55+
echo "DATABASE_USERNAME=${{ secrets.DATABASE_USERNAME }}" >> .env
56+
echo "DATABASE_PASSWORD=${{ secrets.DATABASE_PASSWORD }}" >> .env

.github/workflows/deploy-prod.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Deploy DEV to host
2+
3+
on:
4+
push:
5+
branches:
6+
- master # Change this to your default branch
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Set up PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: '8.3'
20+
tools: composer
21+
22+
- name: Install dependencies
23+
run: composer install --no-dev --optimize-autoloader
24+
25+
- name: Zip the folder
26+
run: zip -r archive.zip . -x "./.git/*" "./.github/*"
27+
28+
- name: Deploy via SSH
29+
uses: appleboy/scp-action@v0.1.7
30+
with:
31+
host: ${{ secrets.FTP_SERVER }}
32+
username: ${{ secrets.FTP_USERNAME }}
33+
password: ${{ secrets.FTP_PASSWORD }}
34+
source: "archive.zip"
35+
target: ${{ secrets.FTP_SERVER_PROD_PATH }}
36+
37+
- name: SSH into server and unzip
38+
uses: appleboy/ssh-action@v1.0.3
39+
with:
40+
host: ${{ secrets.FTP_SERVER }}
41+
username: ${{ secrets.FTP_USERNAME }}
42+
password: ${{ secrets.FTP_PASSWORD }}
43+
script: |
44+
cd ${{ secrets.FTP_SERVER_PROD_PATH }}
45+
unzip -o archive.zip -d .
46+
rm archive.zip
47+
echo "INTERFACE_EDITOR_URL=https://json.ms" > .env
48+
echo "ACCESS_CONTROL_ALLOW_ORIGIN=https://json.ms" >> .env
49+
echo "JSONMS_CYPHER_KEY=${{ secrets.JSONMS_CYPHER_KEY }}" >> .env
50+
echo "GOOGLE_OAUTH_CLIENT_ID=637442439591-qrrpb3v9d3n5m8b8gheorfa1fbi5o6qc.apps.googleusercontent.com" >> .env
51+
echo "GOOGLE_OAUTH_CLIENT_SECRET=${{ secrets.GOOGLE_OAUTH_CLIENT_SECRET }}" >> .env
52+
echo "GOOGLE_OAUTH_CALLBACK_URL=https://server.json.ms/google/callback" >> .env
53+
echo "DATABASE_HOST=${{ secrets.DATABASE_HOST }}" >> .env
54+
echo "DATABASE_DBNAME=${{ secrets.DATABASE_PROD_DBNAME }}" >> .env
55+
echo "DATABASE_USERNAME=${{ secrets.DATABASE_USERNAME }}" >> .env
56+
echo "DATABASE_PASSWORD=${{ secrets.DATABASE_PASSWORD }}" >> .env

.pre-commit

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
# Path to composer.json
4+
COMPOSER_FILE="composer.json"
5+
6+
# Check if composer.json exists
7+
if [ ! -f "$COMPOSER_FILE" ]; then
8+
echo "$COMPOSER_FILE not found!"
9+
exit 1
10+
fi
11+
12+
# Use jq to bump the patch version
13+
if command -v jq &> /dev/null; then
14+
# Read the current version
15+
CURRENT_VERSION=$(jq -r '.version' "$COMPOSER_FILE")
16+
17+
# Split the version into an array
18+
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
19+
20+
# Increment the patch version
21+
NEW_PATCH=$((PATCH + 1))
22+
23+
# Create the new version string
24+
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
25+
26+
# Update the composer.json file
27+
jq --arg new_version "$NEW_VERSION" '.version = $new_version' "$COMPOSER_FILE" > tmp.$.json && mv tmp.$.json "$COMPOSER_FILE"
28+
29+
echo "Bumped version from $CURRENT_VERSION to $NEW_VERSION"
30+
31+
# Stage the changes to package.json
32+
git add composer.json
33+
else
34+
echo "jq is not installed. Please install jq to use this hook."
35+
exit 1
36+
fi

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2025, Danny Coulombe
3+
Copyright (c) 2025, JSON.ms
44

55
Redistribution and use in source and binary forms, with or without
66
modification, are permitted provided that the following conditions are met:

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @jsonms/server
2+
3+
The server to use with your instance of [jsonms-www](https://github.com/JSON-ms/www).

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
{
2+
"version": "1.0.8",
3+
"name": "jsonms/server",
4+
"description": "The JSON.ms Request Handler Server is a robust backend solution designed to manage and process all incoming requests from the main JSON.ms website.",
5+
"license": "BSD-3-Clause",
6+
"scripts": {
7+
"setup-hooks": "cp .pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit"
8+
},
29
"require": {
310
"google/apiclient": "^2.18",
411
"vlucas/phpdotenv": "^5.6",
512
"ext-curl": "*",
613
"ext-openssl": "*",
714
"monolog/monolog": "^2.5",
815
"psr/log": "^2.0",
9-
"ext-pdo": "*"
16+
"ext-pdo": "*",
17+
"ext-fileinfo": "*"
1018
}
11-
}
19+
}

src/controllers/BaseController.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,24 @@ public function query(string $query, array $params = []): PDOStatement {
7979
return $this->queryRaw($sql, $params);
8080
}
8181

82-
public function getCurrentUser() {
83-
if ($this->user == null) {
82+
public function getCurrentUserId() {
83+
if ($this->user == null && isset($_SESSION['user_id'])) {
8484
$stmt = $this->query('get-user-by-id', [
85-
'id' => $_SESSION['user']['id']
85+
'id' => $_SESSION['user_id']
8686
]);
8787
$this->user = $stmt->fetch(PDO::FETCH_OBJ);
88+
if ($this->user) {
89+
return $this->user->id;
90+
}
91+
return null;
8892
}
89-
return $this->user;
93+
return $this->user->id;
94+
}
95+
96+
protected function getHash($length = 10): string {
97+
$bytes = random_bytes($length);
98+
$result = bin2hex($bytes);
99+
return substr($result, 0, $length);
90100
}
91101

92102
public function encrypt($data, $encryptionKey) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use JSONms\Controllers\RestfulController;
4+
5+
class ErrorController extends RestfulController
6+
{
7+
public function saveAction($errors)
8+
{
9+
foreach ($errors as $error) {
10+
$this->query('insert-error', [
11+
'key' => $error->key,
12+
'message' => $error->message,
13+
'source' => $error->source,
14+
'line' => $error->line,
15+
'column' => $error->column,
16+
'stack' => $error->stack,
17+
'occurred_on' => $error->occurred_on,
18+
'last_timestamp' => $error->last_timestamp,
19+
'version' => $error->version,
20+
'route' => $error->route,
21+
'count' => $error->count,
22+
'user_agent' => $error->user_agent,
23+
'created_by' => $this->getCurrentUserId(),
24+
]);
25+
}
26+
27+
$this->responseJson(true);
28+
}
29+
}

src/controllers/GoogleController.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,25 @@ public function callbackAction() {
3030
if ($stmt->rowCount() > 0) {
3131
// User exists, fetch data
3232
$user = $stmt->fetch(PDO::FETCH_ASSOC);
33+
$userId = $user['id'];
3334
} else {
3435
// User does not exist, insert new user
3536
$this->query('insert-user', [
3637
'id' => $userInfo->id,
3738
'name' => $userInfo->name,
3839
'email' => $userInfo->email,
39-
'picture' => $userInfo->picture
40+
'avatar' => $userInfo->picture
4041
]);
4142

4243
$userId = $this->getLastInsertedId(); // Get the new user's ID
43-
$user = [
44-
'id' => $userId,
45-
'googleId' => $userInfo->id,
46-
'name' => $userInfo->name,
47-
'email' => $userInfo->email,
48-
'avatar' => $userInfo->picture,
49-
];
5044
}
5145

5246
// Store user information in the session
53-
$_SESSION['user'] = $user;
47+
$_SESSION['user_id'] = $userId;
5448
$_SESSION['access_token'] = $token['access_token'];
5549

5650
// Redirect to a protected page or dashboard
5751
$decodedState = json_decode(urldecode($state), true);
58-
setcookie("PHPSESSID", session_id(), time() + 3600 * 24 * 7, "/", $_ENV['INTERFACE_EDITOR_DOMAIN']);
5952
header('Location: ' . $_ENV['INTERFACE_EDITOR_URL'] . $decodedState['path']);
6053
exit;
6154
} else {

0 commit comments

Comments
 (0)