-
Notifications
You must be signed in to change notification settings - Fork 0
/
phingle.php
318 lines (266 loc) · 7.54 KB
/
phingle.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/* ----------------------------------------------------------------------------
* Phingle - Single File App Template
*
* @package Phingle
* @author A.Tselegidis <[email protected]>
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/license/gpl-3-0 - GPLv3
* @link https://alextselegidis.com
* @version 1.0.0
* ---------------------------------------------------------------------------- */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
/*
|--------------------------------------------------------------------------
| Configuration
|--------------------------------------------------------------------------
|
| Describe the functionality and purpose of single file tool. This will be
| the first thing developers will see.
|
*/
const SCRIPT_NAME = 'Scriptname';
const AUTH_USERNAME = 'administrator';
const AUTH_PASSWORD = ''; // Set a password to enable HTTP Basic Auth.
/*
|--------------------------------------------------------------------------
| Disable CLI
|--------------------------------------------------------------------------
|
| This PHP template will not work with the CLI, so return a friendly message
| to the user.
|
*/
if (PHP_SAPI === 'cli')
{
die('This file does not support CLI requests.');
}
/*
|--------------------------------------------------------------------------
| Phingle
|--------------------------------------------------------------------------
|
| The following class contains the required features the script needs
| in order to perform core operations, such as render a template, process
| a request etc.
|
*/
class Phingle {
/**
* @var array
*/
private $routes = [];
/**
* @var array
*/
private $messages = [];
/**
* Route an action callback.
*
* @param string $action
* @param callable $callback
*/
public function route(string $action, callable $callback)
{
$this->routes[$action] = $callback;
}
/**
* Get the current action (returns 'default' if non specified).
*
* @return string
*/
public function action(): string
{
return $_REQUEST['action'] ?? 'default';
}
/**
* Make an HTTP request.
*
* @param string $method
* @param string $url
* @param array $headers
* @param string $body
*
* @return string
*/
public function request(string $method, string $url, array $headers = [], string $body = ''): string
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_CUSTOMREQUEST => strtoupper($method),
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $body
]);
$response = curl_exec($curl);
if ( ! $response)
{
die('Curl error: ' . curl_error($curl));
}
curl_close($curl);
return $response;
}
/**
* Queue a message for display in the page render (optional).
*
* @param string $content
* @param string $type Provide one of 'info', 'success', 'warn', 'danger'.
*/
public function message(string $content, string $type = 'info')
{
if (empty($this->messages[$type]))
{
$this->messages[$type] = [];
}
$this->messages[$type][] = $content;
}
/**
* Get the base URL.
*
* @param string $segment
*
* @return string
*/
public function baseUrl(string $segment = ''): string
{
$protocol =
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
|| (isset($_SERVER['SERVER_PORT']) && (int)$_SERVER['SERVER_PORT'] === 443)
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
? 'https://' : 'http://';
$domain = $_SERVER['HTTP_HOST'] ?? 'localhost';
$requestUri = dirname($_SERVER['SCRIPT_NAME']);
return rtrim($protocol . $domain . $requestUri, '/') . '/' . $segment;
}
/**
* Get the script URL.
*
* @param string $segment
*
* @return string
*/
public function scriptUrl(string $segment = ''): string
{
$file = basename(__FILE__);
return $this->baseUrl($file . $segment);
}
/**
* Render the content on the screen (may optionally use a layout too).
*
* @param string $content
*/
public function render(string $content)
{
$year = date('Y');
$scriptName = SCRIPT_NAME;
echo <<<HTML
<!doctype html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>{$scriptName}</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container py-4">
<header class="pb-3 mb-4 border-bottom">
<a href="{$this->scriptUrl()}" class="d-flex align-items-center text-dark text-decoration-none">
<spanre class="fs-4">{$scriptName}</spanre>
</a>
</header>
{$content}
<footer class="pt-3 mt-4 text-muted border-top d-flex justify-content-between align-items-center">
<div>
<a href="https://alextselegidis.com" target="_blank">Alex Tselegidis</a>
© {$year}
</div>
<div>
Based On
<a href="https://github.com/alextselegidis/phingle" target="_blank">
Phingle
</a>
</div>
</footer>
</div>
</body>
</html>
HTML;
}
/**
* Authorize the current request.
*/
public function auth()
{
if (empty(AUTH_USERNAME) || empty(AUTH_PASSWORD))
{
return;
}
if (
! isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])
|| (
$_SERVER['PHP_AUTH_USER'] !== AUTH_USERNAME
|| $_SERVER['PHP_AUTH_PW'] !== AUTH_PASSWORD
)
)
{
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
die('Permission denied');
}
}
/**
* Run the application.
*/
public function run()
{
$this->auth();
if (empty($this->routes))
{
die('No routes defined.');
}
$action = $this->action();
if ( ! array_key_exists($action, $this->routes))
{
die('The requested action has no callback routed: ' . $action);
}
$callback = Closure::fromCallable($this->routes[$action]);
$callback->call($this);
}
}
/*
|--------------------------------------------------------------------------
| Be Creative
|--------------------------------------------------------------------------
|
| Define your own functions, callbacks and logic for this single file app.
|
*/
$app = new Phingle;
$app->route('default', function () {
$content = <<<HTML
<div class="p-5 mb-4 bg-light rounded-3">
<div class="container-fluid py-5">
<h1 class="display-5 fw-bold">Welcome!</h1>
<p class="col-md-8 fs-4">
Using this single file template you can create your own scripts for any use case.
</p>
<a href="https://github.com/alextselegidis/phingle" class="btn btn-primary btn-lg">
Find out more
</a>
</div>
</div>
HTML;
$this->render($content);
});
// Add your custom routes here ...
$app->run();