-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_config.php
102 lines (83 loc) · 2.4 KB
/
global_config.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
<?php
session_start();
set_include_path( __DIR__ . ':' . __DIR__ . '/server/includes:' . __DIR__ . '/server/ajax');
$root = $_SERVER["DOCUMENT_ROOT"];
require_once __DIR__ . '/.env.php';
require_once __DIR__ . '/server/includes/mailers.php';
if( !isset( $_SESSION['confirmed'] ) || !$_SESSION['confirmed'] ){
$is_manager = false;
$is_logged = false;
}else{
$is_manager = isset( $_SESSION['role'] ) && $_SESSION['role'] === 'manager' ? true : false;
$is_logged = ( is_admin( $_SESSION ) || $is_manager ) ? true : false;
}
// return json_reject('testing', new stdClass() );
// _LOG( time('2021-04-25 21:22:15') );
try{
$pdo=new PDO("mysql:host=" . $env->db_host . "; port=3306; dbname=" . $env->db_name, $env->db_user, $env->db_password );
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
_LOG( $e->getMessage() );
}
if( !isset( $pdo )){
_LOG('failed to init db');
}
function _LOG( ...$msgs ){
global $env;
foreach ($msgs as $key => $value) {
file_put_contents($env->logfile, date('D:H:i') . ': ' . $value . PHP_EOL, FILE_APPEND | LOCK_EX);
}
}
function get_post(){
$json = file_get_contents('php://input');
$data = json_decode($json);
return $data;
}
function header_row( ...$values ){
$size = sizeof($values);
$row = '<div class="row header">';
foreach ($values as $key => $value) {
$row = $row . '<div class="column column-' . $size . '">' . $value . '</div>';
}
$row = $row . '</div>';
return $row;
}
function random_hex( $length ){
$hex = '';
for( $i = 0; $i < $length; $i++ ){
$hex .= dechex(rand(0, 15));
}
return $hex;
}
function sql_datetime( $time ){
return gmdate( 'Y-m-d H:i:s', $time );
}
function mail_wrap( $to, $subject, $body ){
global $env;
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\n";
if( isset( $env->PRODUCTION ) && $env->PRODUCTION === true ){
mail( $to, $subject, $body, $headers );
}else{
_LOG('SKIP local email: ', $to, $subject, $body);
}
}
function json_reject( $msg, $res ){
$res->success = false;
$res->msg = $msg;
_LOG( $msg );
echo json_encode($res);
return;
}
function hash_func($password) {
$hash = password_hash($password, PASSWORD_BCRYPT);
return $hash;
}
function is_admin( $session ){
global $env;
if( isset( $env->admins ) && isset( $session['email'] ) && in_array( $session['email'], $env->admins ) ){
return true;
}
return false;
}