Skip to content

Commit a9371b4

Browse files
Merge pull request #3 from JSON-ms/dev
post-install index.php creation script
2 parents 6443e95 + 651dfd4 commit a9371b4

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
2-
"version": "1.0.5",
2+
"version": "1.0.6",
33
"name": "jsonms/php",
44
"description": "A JSON.ms requests handler to install on your own server.",
55
"type": "library",
66
"license": "MIT",
77
"scripts": {
8-
"setup-hooks": "cp .pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit"
8+
"setup-hooks": "cp .pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit",
9+
"post-install-cmd": [
10+
"php install.php"
11+
]
912
},
1013
"autoload": {
1114
"psr-4": {

install.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
// Define ANSI color codes
4+
define('COLOR_GREEN', "\033[32m");
5+
define('COLOR_WHITE', "\033[0m");
6+
7+
// Function to prompt a question and get user input
8+
function prompt($question, $default = null) {
9+
echo COLOR_GREEN . $question . COLOR_WHITE . ": ";
10+
$handle = fopen("php://stdin", "r");
11+
$response = trim(fgets($handle));
12+
fclose($handle);
13+
return empty($response) ? $default : $response;
14+
}
15+
16+
$createFile = prompt('Would you like to automatically create an index.php that instantiate JSON.ms? (Y) ', 'Y');
17+
if (strtoupper($createFile) == 'Y') {
18+
$privatePath = preg_replace('/(\/|\\\)+/', DIRECTORY_SEPARATOR , getcwd() . '/private/');
19+
$privatePath = prompt('Where in your file system do you want to save your data? (default: ' . $privatePath . ') ', $privatePath);
20+
$privatePath = str_replace('\\', '\\\\', $privatePath);
21+
22+
$publicUrl = 'http://localhost:8080';
23+
$publicUrl = prompt('What will be your public endpoint URL? (default: ' . $publicUrl . ') ', $publicUrl);
24+
25+
$acao = 'https://json.ms';
26+
$acao = prompt('Define the Access-Control-Allow-Origin (default: ' . $acao . '): ', $acao);
27+
28+
$secretKey = prompt('Your webhook secret key? ');
29+
$cypherKey = prompt('Your webhook cypher key? ');
30+
31+
$fileContent = <<<EOD
32+
<?php
33+
34+
use JSONms\JSONms;
35+
36+
require 'vendor/autoload.php';
37+
38+
// Load JSONms configurations
39+
\$jsonms = new JSONms(
40+
'$privatePath', // Where to read/save your data in your file system?
41+
'$publicUrl', // Public path of your server (webhook)
42+
'$acao', // Set to "https://json.ms" if you do not need your own instance of JSON.ms. You can add multiple URLs by seperating them by a comma.
43+
'$secretKey', // Obtain from your Webhook Endpoint in Settings panel in Advanced mode.
44+
'$cypherKey', // Obtain from your Webhook Endpoint in Settings panel in Advanced mode.
45+
);
46+
47+
// Handle errors (if required) and requests
48+
\$jsonms->handleErrors(); // Optional. Remove if you prefer to handle errors yourself.
49+
\$jsonms->handleRequests(); // You can pass an URI param. (ex: /data/get/YOUR_HASH)`);
50+
51+
EOD;
52+
53+
file_put_contents(dirname(__FILE__) . '/index.php', $fileContent);
54+
55+
echo COLOR_GREEN . 'A new index.php file has been created!' . COLOR_WHITE;
56+
}

0 commit comments

Comments
 (0)