forked from Forceu/barcodebuddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.php
executable file
·144 lines (119 loc) · 4.59 KB
/
setup.php
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Barcode Buddy for Grocy
*
* PHP version 7
*
* LICENSE: This source file is subject to version 3.0 of the GNU General
* Public License v3.0 that is attached to this project.
*
*
* Setup file
*
* @author Marc Ole Bulling
* @copyright 2019 Marc Ole Bulling
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU GPL v3.0
* @since File available since Release 1.2
*/
require_once __DIR__ . "/incl/configProcessing.inc.php";
require_once __DIR__ . "/incl/db.inc.php";
require_once __DIR__ . "/incl/webui.inc.php";
require_once __DIR__ . "/incl/api.inc.php";
require_once __DIR__ . "/incl/processing.inc.php";
require_once __DIR__ . "/incl/internalChecking.inc.php";
$CONFIG->checkIfAuthenticated(true);
$result = true;
//If BB is already setup and for some reason an internal check failed
//(eg. missing php module), the use is redirected to setup.php
//If the problem is fixed and he clicks "Refresh", redirect him to index.php
//instead of showing the setup again
if (isset($_POST["was_internal_check"])) {
if ($BBCONFIG["GROCY_API_URL"] != null && $BBCONFIG["GROCY_API_KEY"] != null) {
header("Location: index.php");
die();
}
}
//If settings were submitted, save and check them
if (isset($_POST["GROCY_API_URL"])) {
$apiWithTrailingSlash = rtrim($_POST["GROCY_API_URL"], '/') . '/';
$result = API::checkApiConnection($apiWithTrailingSlash, $_POST["GROCY_API_KEY"]);
if ($result === true) {
$db->updateConfig("GROCY_API_URL", sanitizeString($apiWithTrailingSlash));
$db->updateConfig("GROCY_API_KEY", sanitizeString($_POST["GROCY_API_KEY"]));
header("Location: index.php");
die();
}
}
$webUi = new WebUiGenerator(MENU_SETUP);
$webUi->addHeader();
if (checkExtensionsInstalled()["result"] == RESULT_REQ_MISSING) {
$webUi->addCard("Setup", getHtmlSetupExtMissing());
} else {
$webUi->addCard("Setup", getHtmlSetupTable($result));
}
$webUi->addFooter();
$webUi->printHtml();
function getHtmlSetupTable($result) {
global $BBCONFIG;
$html = new UiEditor();
$html->addHtml('Welcome to Barcode Buddy! Please enter your Grocy API details below. For more information, please visit the <a target="_blank" href="https://barcodebuddy-documentation.readthedocs.io/en/latest/">documentation</a>.');
$html->addLineBreak(3);
$editValue = "";
if (isset($_POST["GROCY_API_URL"]))
$editValue = $_POST["GROCY_API_URL"];
else if ($BBCONFIG["GROCY_API_URL"] != null)
$editValue = $BBCONFIG["GROCY_API_URL"];
$html->buildEditField('GROCY_API_URL', 'Grocy API URL', $editValue)
->pattern('https://.*/api/|http://.*/api/|https://.*/api|http://.*/api')
->setPlaceholder('e.g. https://your.grocy.com/api/')
->generate();
$html->addLineBreak();
$editValue = "";
if (isset($_POST["GROCY_API_KEY"]))
$editValue = $_POST["GROCY_API_KEY"];
$html->buildEditField('GROCY_API_KEY', 'Grocy API Key', $editValue)
->pattern('[A-Za-z0-9]{50}')
->generate();
if ($result !== true) {
$html->addHtml('<font color="red">Unable to connect to API: '.$result.'</font>');
}
$html->addLineBreak(2);
$html->buildButton("button_save", "Save")
->setSubmit()
->setRaised()
->setIsAccent()
->generate();
return $html->getHtml();
}
function getHtmlSetupExtMissing() {
$html = new UiEditor();
$html->addHtml('Welcome to Barcode Buddy! Please make sure the following extensions are installed and enabled:');
$html->addLineBreak(2);
$extensions = checkExtensionsInstalled();
foreach ($extensions["req"] as $ext=>$installed) {
$html->addHtml(formatExtentioncolor($ext,$installed,true));
}
foreach ($extensions["opt"] as $ext=>$installed) {
$html->addHtml(formatExtentioncolor($ext,$installed,false));
}
$html->addLineBreak(2);
$html->addHiddenField("was_internal_check","1");
$html->buildButton("button_refresh", "Refresh")
->setSubmit()
->setRaised()
->setIsAccent()
->generate();
return $html->getHtml();
}
function formatExtentioncolor($name, $installed, $req) {
if ($installed) {
return 'Installed: <span style="color:green">' . $name . '</span><br>';
} else {
if ($req) {
return '<b>Not installed: <span style="color:red">' . $name . ' (required)</span></b><br>';
} else {
return 'Not installed: <span style="color:#d0a42c">' . $name . ' (optional)</span><br>';
}
}
}
?>