/
pagex.class.php
613 lines (520 loc) · 13.9 KB
/
pagex.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<?php defined('BLUDIT') or die('Bludit CMS.');
class Page {
protected $vars;
function __construct($key)
{
global $pages;
$this->vars['key'] = $key;
// If key is FALSE, the page is create with default values, like an empty page
// Useful for Page Not Found
if ($key===false) {
$row = $pages->getDefaultFields();
} else {
if (Text::isEmpty($key) || !$pages->exists($key)) {
$errorMessage = 'Page not found in database by key ['.$key.']';
Log::set(__METHOD__.LOG_SEP.$errorMessage);
throw new Exception($errorMessage);
}
$row = $pages->getPageDB($key);
}
foreach ($row as $field=>$value) {
if ($field=='date') {
$this->setField('dateRaw', $value);
} else {
$this->setField($field, $value);
}
}
}
public function getValue($field)
{
if (isset($this->vars[$field])) {
return $this->vars[$field];
}
return false;
}
public function setField($field, $value)
{
$this->vars[$field] = $value;
return true;
}
// Returns the raw content
// This content is not markdown parser
// (boolean) $sanitize, TRUE returns the content sanitized
public function contentRaw($sanitize=false)
{
$key = $this->key();
$filePath = PATH_PAGES.$key.DS.FILENAME;
$contentRaw = file_get_contents($filePath);
if ($sanitize) {
return Sanitize::html($contentRaw);
}
return $contentRaw;
}
// Returns the full content
// This content is markdown parser
// (boolean) $sanitize, TRUE returns the content sanitized
public function content($sanitize=false)
{
// If already set the content, return it
$content = $this->getValue('content');
if (!empty($content)) {
return $content;
}
// Get the raw content
$content = $this->contentRaw();
// Parse Markdown
if (MARKDOWN_PARSER) {
$parsedown = new Parsedown();
$content = $parsedown->text($content);
}
// Parse img src relative to absolute (with domain)
if (IMAGE_RELATIVE_TO_ABSOLUTE) {
$domain = IMAGE_RESTRICT?DOMAIN_UPLOADS_PAGES.$this->uuid().'/':DOMAIN_UPLOADS;
$content = Text::imgRel2Abs($content, $domain);
}
if ($sanitize) {
return Sanitize::html($content);
}
return $content;
}
// Returns the first part of the content if the content is splited, otherwise is returned the full content
// This content is markdown parser
// (boolean) $sanitize, TRUE returns the content sanitized
public function contentBreak($sanitize=false)
{
$content = $this->content($sanitize);
$explode = explode(PAGE_BREAK, $content);
return $explode[0];
}
// Returns the date according to locale settings and the format defined in the system
public function date($format=false)
{
$dateRaw = $this->dateRaw();
if ($format===false) {
global $site;
$format = $site->dateFormat();
}
return Date::format($dateRaw, DB_DATE_FORMAT, $format);
}
// Returns the date according to locale settings and format as database stored
public function dateRaw()
{
// This field is set in the constructor
return $this->getValue('dateRaw');
}
// Returns the date according to locale settings and format settings
public function dateModified($format=false)
{
$dateRaw = $this->getValue('dateModified');
if ($format===false) {
global $site;
$format = $site->dateFormat();
}
return Date::format($dateRaw, DB_DATE_FORMAT, $format);
}
// Returns the username who created the page
public function username()
{
return $this->getValue('username');
}
// TODO: Check if necessary this function
public function getDB()
{
return $this->vars;
}
// Returns the permalink
// (boolean) $absolute, TRUE returns the page link with the DOMAIN, FALSE without the DOMAIN
public function permalink($absolute=true)
{
// Get the key of the page
$key = $this->key();
if($absolute) {
return DOMAIN_PAGES.$key;
}
return HTML_PATH_ROOT.PAGE_URI_FILTER.$key;
}
// Returns the previous page key
public function previousKey()
{
global $pages;
return $pages->previousPageKey($this->key());
}
// Returns the next page key
public function nextKey()
{
global $pages;
return $pages->nextPageKey($this->key());
}
// Returns the category name
public function category()
{
return $this->categoryMap('name');
}
// Returns the category template
public function categoryTemplate()
{
return $this->categoryMap('template');
}
// Returns the category description
public function categoryDescription()
{
return $this->categoryMap('description');
}
// Returns the category key
public function categoryKey()
{
return $this->getValue('category');
}
// Returns the category permalink
public function categoryPermalink()
{
return DOMAIN_CATEGORIES.$this->categoryKey();
}
// Returns the field from the array
// categoryMap = array( 'name'=>'', 'list'=>array() )
public function categoryMap($field)
{
global $categories;
$categoryKey = $this->categoryKey();
$map = $categories->getMap($categoryKey);
if ($field=='key') {
return $this->categoryKey();
} elseif(isset($map[$field])) {
return $map[$field];
}
return false;
}
// Returns the user object or passing the method returns the object User method
public function user($method=false)
{
$username = $this->username();
try {
$user = new User($username);
if ($method) {
return $user->{$method}();
}
return $user;
} catch (Exception $e) {
return false;
}
}
public function template()
{
return $this->getValue('template');
}
// Returns the description field
public function description()
{
return $this->getValue('description');
}
// Returns the tags separated by comma
// (boolean) $returnsArray, TRUE to get the tags as an array, FALSE to get the tags separated by comma
// The tags in array format returns array( tagKey => tagName )
public function tags($returnsArray=false)
{
$tags = $this->getValue('tags');
if ($returnsArray) {
if (empty($tags)) {
return array();
}
return $tags;
}
if (empty($tags)) {
return '';
}
// Return string with tags separated by comma.
return implode(',', $tags);
}
public function json($returnsArray=false)
{
$tmp['key'] = $this->key();
$tmp['title'] = $this->title();
$tmp['content'] = $this->content(); // Markdown parsed
$tmp['contentRaw'] = $this->contentRaw(true); // No Markdown parsed
$tmp['description'] = $this->description();
$tmp['type'] = $this->type();
$tmp['slug'] = $this->slug();
$tmp['date'] = $this->date();
$tmp['dateRaw'] = $this->dateRaw();
$tmp['tags'] = $this->tags(false);
$tmp['username'] = $this->username();
$tmp['category'] = $this->category();
$tmp['uuid'] = $this->uuid();
$tmp['dateUTC'] = Date::convertToUTC($this->dateRaw(), DB_DATE_FORMAT, DB_DATE_FORMAT);
$tmp['permalink'] = $this->permalink(true);
$tmp['coverImage'] = $this->coverImage(true);
$tmp['coverImageFilename'] = $this->coverImage(false);
if ($returnsArray) {
return $tmp;
}
return json_encode($tmp);
}
// Returns the endpoint of the coverimage, FALSE if the page doesn't have a cover image
// (boolean) $absolute, TRUE returns the complete URL, FALSE returns the filename
// If the user defined an external cover image the function returns it
public function coverImage($absolute=true)
{
$filename = $this->getValue('coverImage');
if (empty($filename)) {
return false;
}
// Check is external cover image
if (filter_var($filename, FILTER_VALIDATE_URL)) {
return $filename;
}
if ($absolute) {
if (IMAGE_RESTRICT) {
return DOMAIN_UPLOADS_PAGES.$this->uuid().'/'.$filename;
}
return DOMAIN_UPLOADS.$filename;
}
return $filename;
}
// Returns the endpoint of the thumbnail cover image, FALSE if the page doesn't have a cover image
public function thumbCoverImage()
{
$filename = $this->coverImage(false);
if ($filename==false) {
return false;
}
// Check is external cover image
if (filter_var($filename, FILTER_VALIDATE_URL)) {
return $filename;
}
if (IMAGE_RESTRICT) {
return DOMAIN_UPLOADS_PAGES.$this->uuid().'/thumbnails/'.$filename;
}
return DOMAIN_UPLOADS_THUMBNAILS.$filename;
}
// Returns TRUE if the content has the text splited
public function readMore()
{
$content = $this->contentRaw();
return Text::stringContains($content, PAGE_BREAK);
}
public function uuid()
{
return $this->getValue('uuid');
}
// Returns the field key
public function key()
{
return $this->getValue('key');
}
// (boolean) Returns TRUE if the page is published, FALSE otherwise
public function published()
{
return ($this->getValue('type')==='published');
}
// (boolean) Returns TRUE if the page is scheduled, FALSE otherwise
public function scheduled()
{
return ($this->getValue('type')==='scheduled');
}
// (boolean) Returns TRUE if the page is draft, FALSE otherwise
public function draft()
{
return ($this->getValue('type')=='draft');
}
// (boolean) Returns TRUE if the page is autosave, FALSE otherwise
public function autosave()
{
return ($this->getValue('type')=='autosave');
}
// (boolean) Returns TRUE if the page is sticky, FALSE otherwise
public function sticky()
{
return ($this->getValue('type')=='sticky');
}
// (boolean) Returns TRUE if the page is static, FALSE otherwise
public function isStatic()
{
return ($this->getValue('type')=='static');
}
// (string) Returns type of the page
public function type()
{
return $this->getValue('type');
}
// Returns the title field
public function title()
{
return $this->getValue('title');
}
// Returns TRUE if the page has enabled the comments, FALSE otherwise
public function allowComments()
{
return $this->getValue('allowComments');
}
// Returns the page position
public function position()
{
return $this->getValue('position');
}
// Returns the page noindex
public function noindex()
{
return $this->getValue('noindex');
}
// Returns the page nofollow
public function nofollow()
{
return $this->getValue('nofollow');
}
// Returns the page noarchive
public function noarchive()
{
return $this->getValue('noarchive');
}
// Returns the page slug
public function slug()
{
$explode = explode('/', $this->key());
return end($explode);
}
// Returns the parent key, if the page doesn't have a parent returns FALSE
public function parent()
{
return $this->parentKey();
}
// Returns the parent key, if the page doesn't have a parent returns FALSE
public function parentKey()
{
$explode = explode('/', $this->key());
if (isset($explode[1])) {
return $explode[0];
}
return false;
}
// Returns TRUE if the page is a parent, has or not children
public function isParent()
{
return $this->parentKey()===false;
}
// Returns the parent method output, if the page doesn't have a parent returns FALSE
public function parentMethod($method)
{
$parentKey = $this->parentKey();
if ($parentKey) {
try {
$page = new Page($parentKey);
return $page->{$method}();
} catch (Exception $e) {
// Continoue
}
}
return false;
}
// Returns TRUE if the page is a child, FALSE otherwise
public function isChild()
{
return $this->parentKey()!==false;
}
// Returns TRUE if the page has children
public function hasChildren()
{
$childrenKeys = $this->childrenKeys();
return !empty($childrenKeys);
}
// Returns an array with all children's keys
public function childrenKeys()
{
global $pages;
$key = $this->key();
return $pages->getChildren($key);
}
// Returns an array with all children as Page-Object
public function children()
{
global $pages;
$list = array();
$childrenKeys = $pages->getChildren($this->key());
foreach ($childrenKeys as $childKey) {
try {
$child = new Page($childKey);
array_push($list, $child);
} catch (Exception $e) {
// Continue
}
}
return $list;
}
// Returns the amount of minutes takes to read the page
public function readingTime() {
global $L;
$words = $this->content(true);
$words = strip_tags($words);
$words = str_word_count($words);
$average = $words / 200;
$minutes = round($average);
if ($minutes>1) {
return $minutes.' '.$L->get('minutes');
}
return '~1 '.$L->get('minute');
}
// Returns relative time (e.g. "1 minute ago")
// Based on http://stackoverflow.com/a/18602474
// Modified for Bludit
// $complete = false : short version
// $complete = true : full version
public function relativeTime($complete = false) {
$current = new DateTime;
$past = new DateTime($this->getValue('dateRaw'));
$elapsed = $current->diff($past);
$elapsed->w = floor($elapsed->d / 7);
$elapsed->d -= $elapsed->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach($string as $key => &$value) {
if($elapsed->$key) {
$value = $elapsed->$key . ' ' . $value . ($elapsed->$key > 1 ? 's' : ' ');
} else {
unset($string[$key]);
}
}
if(!$complete) {
$string = array_slice($string, 0 , 1);
}
return $string ? implode(', ', $string) . ' ago' : 'Just now';
}
// Returns the value from the field, false if the fields doesn't exists
// If you set the $option as TRUE, the function returns an array with all the values of the field
public function custom($field, $options=false)
{
if (isset($this->vars['custom'][$field])) {
if ($options) {
return $this->vars['custom'][$field];
}
return $this->vars['custom'][$field]['value'];
}
return false;
}
// Returns an array with all pages key related to the page
// The relation is based on the tags
public function related() {
global $tags;
$pageTags = $this->tags(true);
$list = array();
// For each tag get the list of related pages
foreach ($pageTags as $tagKey=>$tagName) {
$pagesRelated = $tags->getList($tagKey, 1, -1);
if(is_array($pagesRelated)) {
$list = array_merge($list, $pagesRelated);
}
}
// Remove duplicates
$list = array_unique($list);
// Remove himself from the list
if (($key = array_search($this->key(), $list)) !== false) {
unset($list[$key]);
}
return $list;
}
}