-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.inc.php
136 lines (113 loc) · 3.57 KB
/
global.inc.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
<?php
session_start();
// header('Access-Control-Allow-Origin: *');
// header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
// global blog vars
$blogTitle = "Flolon Blog";
$blogDesc = "Flolon Fox's blog for himself and others";
$rootUrl = "https://blog.flolon.cc/";
$rootAssetUrl = $rootUrl."assets/";
$rootApiUrl = $rootUrl."api/";
$rootDir = $_SERVER['DOCUMENT_ROOT'];
$blogCopyrightOwner = "Flolon";
$blogCopyrightLink = "https://flolon.cc/";
$blogCopyrightYear = date("Y");
// logged in?
if(isset($_SESSION['auth'])) {
$loggedIn = true;
}else {
$loggedIn = false;
}
// escape HTML function
function escape($html) {
return htmlspecialchars($html, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
}
// replace all linebreaks to <br>
function nl2br2($string) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br>", $string);
return $string;
}
// replace all <br> to linebreaks
function br2nl2($string) {
$string = str_ireplace(array("<br />","<br>","<br/>"), "\r\n", $string);
return $string;
}
// date format function
function formatDate($date, $type = null) {
$spacer = ' at ';
$newDate = date("M j, Y", strtotime($date));
$time = date("H:i T", strtotime($date));
if($type == 'time') {
$formatedDate = $newDate.$spacer.$time;
}else {
$formatedDate = $newDate;
}
return $formatedDate;
}
// create md5 hash of email
function emailHash($email) {
$email = trim($email);
$emailHash = md5(strtolower($email));
return $emailHash;
}
// create pfp link with hash for gravatar
function pfpFromEmailHash($emailHash, $default = 'mp') {
return 'https://www.gravatar.com/avatar/'.$emailHash.'?d='.$default;
}
// create pfp link via email for gravatar
function pfpFromEmail($email, $default = 'mp') {
$emailHash = emailHash($email);
return pfpFromEmailHash($emailHash, $default);
}
function seoSlug($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = trim(preg_replace("/[\s-]+/", " ", $string));
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
// -- db functions -- //
// function to check if username exists
function usernameExists($connection, $username) {
$stmt = $connection->prepare("SELECT 1 FROM users WHERE username=?");
$stmt->execute([$username]);
return $stmt->fetchColumn();
}
function getAllPosts($connection) {
// read from db table
try {
$sql = "SELECT *
FROM posts
ORDER BY id DESC
";
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $error) {
echo $sql . "<br />" . $error->getMessage();
}
// return results
return $result;
}
function getPost($connection, $slug) {
// read from db table
try {
$sql = "SELECT *
FROM posts
WHERE slug = :slug
";
$statement = $connection->prepare($sql);
$statement->bindParam(':slug', $slug, PDO::FETCH_ASSOC);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $error) {
echo $sql . "<br />" . $error->getMessage();
}
// return results
return $result[0];
}
?>