-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.php
executable file
·292 lines (257 loc) · 9.96 KB
/
class.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
<?php
class Page {
// Constructor function //////////////////////////////////////////////////////
public function __construct($id="", $search=false) {
if (empty($id)) {
$path = explode('?', $_SERVER['REQUEST_URI']);
$path[0] = substr($path[0], -1) != '/' ? $path[0] . '/' : $path[0];
if ($path[0] == '/' || empty($path[0]) || $path[0] == '/home/') {
$id = config('home-id');
} else if (strtolower($path[0]) == "/search/") {
$search = true;
$this->setSearch();
} else {
$post = getPostByLocation($path[0]);
$id = !empty($post) ? $post['id'] : config('404-id');
}
}
if (!$search) {
$this->id = $this->validateUnpublished($id);
$post = $GLOBALS['allposts']["$this->id"];
foreach($post as $key => $detail) {
$this->{$key} = $detail;
}
$PD = new Parsedown();
$this->formattedBody = $PD->text($this->body);
$this->description = substr(strip_tags($this->formattedBody), 0, 150) . "...";
$this->date = (!empty($this->time)) ? date_format(date_create($this->time), "M. j, Y - g:i A") : "";
$this->parentDetails = ($this->parent != 0) ? $GLOBALS['allposts']["$this->parent"] : "";
$this->breadcrumbs = ["<li class='active'>$this->title</li>"];
$this->setParents();
$this->setBanner();
$this->section = !empty($parents) ? $parents[0]["title"] : "";
$this->url = "";
$this->search = false;
$this->children = getChildren($this->id);
$this->setWords();
$this->total_word_count();
$this->setBooktitle();
$this->setSidebar();
}
}
// Validate viewing of unpublished page //////////////////////////////////////
public function validateUnpublished ($id) {
if (empty($GLOBALS['allposts']["$id"])) { // Check if exists
return config('404-id');
}
$page = $GLOBALS['allposts']["$id"]; // Check preview code
if (empty($page["time"]) || strtotime($page["time"]) > strtotime(date("Y-m-d H:i:s"))) { //if needs preview
if (empty($_GET['preview']) || !password_verify($page["title"].config('obfuscate'), urldecode($_GET['preview']))) { //if fail
return config('404-id');
}
}
return $id;
}
// Set parent details ////////////////////////////////////////////////////////
public function setParents() {
$this->parents = [];
if ($this->parent != 0) {
$crntParent = $this->parentDetails;
while (!empty($crntParent)) {
$this->parents[] = $crntParent;
array_unshift($this->breadcrumbs, "<li><a href='$crntParent[location]'>$crntParent[title]</a></li>");
if ($crntParent['parent'] != 0) {
$crntParent = $GLOBALS['allposts']["$crntParent[parent]"];
} else {
$crntParent = NULL;
}
}
}
}
// Set banner image //////////////////////////////////////////////////////////
public function setBanner() {
if (empty($this->banner)) {
foreach ($this->parents as $p) {
if (!empty($p["banner"])) {
$this->banner = $p["banner"];
break;
}
}
}
if (empty($this->banner)) {
$this->banner = config('banner');
}
}
// Set word count and read time //////////////////////////////////////////////
public function setWords() {
$this->words = str_word_count(strip_tags($this->formattedBody),0,"'");
$this->readTime = "< 1 min.";
if ($this->words > 224) {
$slowRead = round($this->words/150);
$fastRead = round($this->words/200);
$this->readTime = "$fastRead - $slowRead mins.";
}
}
// Get total word count of story /////////////////////////////////////////////
public function total_word_count() {
$wordcount = 0;
$children = $this->children;
foreach(mysqli_fetch_assoc($children) as $child) {
$c = new Page($child["id"]);
$wordcount += $c->words;
}
$this->total_words = $wordcount;
}
// Get total read time ///////////////////////////////////////////////////////
public function total_read_time() {
return round($this->total_words/200) . " - " . round($this->total_words/150) . " mins.";
}
// Set booktitle and title prefix ////////////////////////////////////////////
public function setBooktitle() {
$this->booktitle = "";
$this->titlePrefix = "";
if (($this->type == 'chapter' || $this->type == 'post') && !empty($this->parentDetails)) {
$title = $this->parentDetails['title'];
$this->booktitle = "<h1>$title</h1>";
$this->titlePrefix = "$title: ";
} elseif ($this->type == 'story') {
$this->booktitle = "<h1>$this->title</h1>";
}
}
// Set page sidebar //////////////////////////////////////////////////////////
public function setSidebar() {
$PD = new Parsedown();
if (!empty($this->sidebar)) {
$this->sidebar = $PD->text($this->sidebar);
} elseif (!empty($this->parentDetails) && ($this->type == 'chapter' || $this->type == 'post')) {
$this->sidebar = $PD->text($this->parentDetails['sidebar']);
} else {
$this->sidebar = "";
}
}
// Get Next Chapter //////////////////////////////////////////////////////////
public function NextPrev () {
$nextsql = "SELECT id,title,location FROM posts
WHERE parent = $this->parent
AND time < CURRENT_TIMESTAMP
AND (sort > $this->sort OR time > '$this->time')
AND id != $this->id
ORDER BY sort ASC, time ASC
LIMIT 1";
$nextChapter = "";
$nextChapLi = "<li class='next disabled'><a href='#'><small>Next <span aria-hidden='true'>→</span></small></a></li>";
if ($nextresult = mysqli_query(dbConnect(), $nextsql)) {
$nextChapter = mysqli_fetch_array($nextresult);
if (!empty($nextChapter)) {
$nextChapLi = "<li class='next'><a href='$nextChapter[location]' id='nextPage' rel='next'><small>Next <span aria-hidden='true'>→</span></small></a></li>";
}
}
// Previous Chapter
$prevsql = "SELECT id,title,location FROM posts
WHERE parent = $this->parent
AND time < CURRENT_TIMESTAMP
AND (sort < $this->sort OR time < '$this->time')
AND id != $this->id
ORDER BY sort DESC, time DESC
LIMIT 1";
$prevChapter = "";
$prevChapLi = "<li class='previous disabled'><a href='#'><small><span aria-hidden='true'>←</span> Previous</small></a></li>";
if ($prevresult = mysqli_query(dbConnect(), $prevsql)) {
$prevChapter = mysqli_fetch_array($prevresult);
if (mysqli_num_rows($prevresult)) {
$prevChapLi = "<li class='previous'><a href='$prevChapter[location]' id='prevPage' rel='prev'><small><span aria-hidden='true'>←</span> Previous</a></small></li>";
}
}
echo "$prevChapLi $nextChapLi";
}
// Get breadcrumbs ///////////////////////////////////////////////////////////
public function breadcrumbs() {
if ($this->parent != 0) {
echo "
<div class='row'>
<div class='col-xs-12'>
<ol class='breadcrumb'>
";
foreach($this->breadcrumbs as $bc) {
echo $bc;
}
echo "
</ol>
</div>
</div>
";
}
}
// Get Table of Contents /////////////////////////////////////////////////////
public function tableOfContents() {
echo "$this->sidebar
<!-- <ul> -->
";
if ($this->type == 'story' || $this->type == 'blog') {
findChildren($this->id, $this->id);
} else {
findChildren($this->parent, $this->id);
}
}
// Get tags //////////////////////////////////////////////////////////////////
public function tags() {
if (!empty($this->tags)) {
echo "<i class='glyphicon glyphicon-tag'></i> ";
$tags = explode(",", $this->tags);
foreach($tags as $tag) {
echo "<a href='/search/?tag=$tag' class='label label-default'>$tag</a> ";
}
}
}
// Set Search Details ////////////////////////////////////////////////////////
public function setSearch() {
$this->keyword = "";
$this->urlkeyword = "?a=a";
$this->s = "";
$this->tag = "";
if (!empty($_GET['s'])) { $this->s = $_GET['s']; $this->keyword = $this->s; $this->urlkeyword = "?s=$this->s"; }
if (!empty($_GET['tag'])) { $this->tag = $_GET['tag']; $this->keyword = $this->tag; $this->urlkeyword = "?tag=$this->tag"; }
$this->title = "Search";
$this->parent = 0;
$this->time = "";
$this->tags = "";
$this->location = "/search/";
$this->banner = config('banner');
$this->titlePrefix = "";
$this->booktitle = "";
$this->description = "Search results for $this->keyword.";
$this->section = "";
$this->type = "search";
}
// Get search results ////////////////////////////////////////////////////////
public function searchResults() {
if (!empty($this->tag)) {
echo "<h2>Results for "$this->tag" tag</h2>";
$sql = "SELECT * FROM posts WHERE time IS NOT NULL AND time < CURRENT_TIMESTAMP AND tags LIKE '%$this->tag%' ORDER BY time DESC";
} else {
echo "<h2>Results for "$this->s"</h2>";
$sql = "SELECT * FROM posts WHERE time IS NOT NULL AND time < CURRENT_TIMESTAMP AND (body LIKE '%$this->s%' OR title LIKE '%$this->s%') ORDER BY time DESC";
}
if ($result = mysqli_query(dbConnect(), $sql)) {
while ($row = mysqli_fetch_array($result)) {
$rowdate = "";
if ($row['type'] == "chapter" || $row['type'] == "post") {
$rowdate = date_format(date_create($row['time']), "M. j, Y - g:i A");
}
$resultPage = new Page($row['id']);
echo "
<div class='panel panel-default'>
<div class='panel-body'>
<p class='text-muted pull-right'><small>$rowdate</small></p>
<h3>" . ucfirst($row['type']) . ": <a href='$row[location]'>$row[title]</a></h3>
<p>" . substr(strip_tags($resultPage->formattedBody), 0, 200) . " . . .</p>
";
$resultPage->tags();
echo "
</div>
</div>
";
}
}
}
}