-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathboot.php
More file actions
164 lines (125 loc) · 3.14 KB
/
boot.php
File metadata and controls
164 lines (125 loc) · 3.14 KB
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
<?php
/**
* OpenTHC BONG Application Bootstrap
*
* SPDX-License-Identifier: MIT
*/
use \Edoceo\Radix\DB\SQL;
define('APP_ROOT', __DIR__);
error_reporting(E_ALL & ~ E_NOTICE & ~ E_WARNING);
openlog('openthc-bong', LOG_ODELAY|LOG_PID, LOG_LOCAL0);
require_once(APP_ROOT . '/vendor/autoload.php');
if ( ! \OpenTHC\Config::init(APP_ROOT) ) {
_exit_html_fail('<h1>Invalid Application Configuration [ALB-035]</h1>', 500);
}
define('OPENTHC_SERVICE_ORIGIN', \OpenTHC\Config::get('openthc/bong/origin'));
/**
* Database Connection
*/
function _dbc()
{
static $ret;
if (empty($ret)) {
$cfg = \OpenTHC\Config::get('database');
$dsn = sprintf('pgsql:host=%s;dbname=%s;application_name=openthc-bong', $cfg['hostname'], $cfg['database']);
if (getenv('PGBOUNCER_PORT')) {
$dsn = sprintf('pgsql:port=6543;dbname=%s;application_name=openthc-bong-pool', $cfg['database']);
}
$ret = new \Edoceo\Radix\DB\SQL($dsn, $cfg['username'], $cfg['password']);
}
return $ret;
}
/**
* Hands work Directly to View Script
*/
function _from_cre_file($f0, $REQ, $RES, $ARG)
{
$f0 = trim($f0, '/');
$f1 = sprintf('%s/lib/CRE/%s/%s', APP_ROOT, $_SESSION['cre']['engine'], $f0);
if ( ! is_file($f1)) {
return $RES->withJSON([
'data' => [
'f0' => $f0,
'f1' => $f1,
'cre' => $_SESSION['cre']['engine'],
],
'meta' => [
'note' => 'Interface not implemented [OBB-056]',
]
], 501);
}
$out = require_once($f1);
return $out;
}
/**
* Set Option
*/
function _set_option($dbc, $key, $val)
{
if ($dbc->fetchOne("SELECT id FROM base_option WHERE key = :k", [ ':k' => $key ])) {
$dbc->update('base_option', [ 'val' => $val ], [ 'key' => $key ]);
} else {
$dbc->insert('base_option', [
'id' => _ulid(),
'key' => $key,
'val' => $val,
]);
}
}
/**
*
*/
function _date_diff_in_minutes($dt0, $dt1) : float {
$ddX = $dt0->diff($dt1);
$tms = ($ddX->days * 86400) + ($ddX->h * 3600) + ($ddX->i * 60) + $ddX->s + $ddX->f;
$tms = ceil($tms / 60);
return $tms;
}
/**
* Date Age Helper
*/
function _date_diff_in_seconds($dt0, $dt1) : float {
// $dt0 = new \DateTime($rec['created_at']);
// $dt1 = new \DateTime();
$ddX = $dt0->diff($dt1);
$tms = ($ddX->days * 86400) + ($ddX->h * 3600) + ($ddX->i * 60) + $ddX->s + $ddX->f;
return $tms;
}
function _nice_date($x0) {
if (empty($x0)) {
return '-';
}
$x1 = null;
if (is_string($x0)) {
try {
$x1 = new \DateTime($x0);
} catch (\Exception $e) {
return $x0;
}
}
// $dt0 = new \DateTime();
// Diff? strtotime integer diff?
$xM = _date_diff_in_minutes(new \DateTime(), $x1);
$t0 = time();
$t1 = intval($x1->format('s'));
$a0 = $t0 - $t1;
if ($a0 < 900) {
return sprintf('%d m ago', ($a0 / 60));
}
return sprintf('<span title="%s Minutes Ago">%s</span>', $xM, $x1->format('Y-m-d H:i:s'));
}
function _stat_card($name, $data)
{
$html = [];
$html[] = '<div class="col-2 mb-2">';
$html[] = '<div class="card h-100">';
$html[] = '<div class="card-header text-center">';
$html[] = $name;
$html[] = '</div>';
$html[] = '<div class="card-body text-center">';
$html[] = $data;
$html[] = '</div>';
$html[] = '</div>';
$html[] = '</div>';
return implode('', $html);
};