-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_posts.php
More file actions
113 lines (63 loc) · 2.53 KB
/
class_posts.php
File metadata and controls
113 lines (63 loc) · 2.53 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
<?php
/* Author: Nuri Hodges
Script: class_posts.php - provides the mechanisms to access the post database, and eventually add to it.
----------------------------------------------------------------------------------------------------------- */
class fapr_posts {
public function __construct($directory) {
$this->directory = $directory;
}
public function fetch($granularity = NULL) {
switch($granularity) {
case is_numeric($granularity):
return $this->_fapr_fetch_post($granularity);
break;
case 'recent':
return $this->_fapr_fetch_posts();
break;
default:
return 'FAILURE: Invalid request!';
}
}
private function _fapr_fetch_post($granularity) {
$post_path = $this->directory . 'posts/' . $granularity . '.txt';
if(file_exists($post_path)) {
$post_tmp = array();
$post_raw = file($post_path);
# Grab post name and generate link (first line)
$post_tmp['name'] = $post_raw[0];
$post_tmp['link'] = strtolower(trim(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $post_raw[0])), '-'));
unset($post_raw[0]); $post_raw = array_values($post_raw);
$post_tmp['body'] = nl2br(implode("\r\n", $post_raw));
return $post_tmp;
} else {
return 'FAILURE: Post does not exist!';
}
}
private function _fapr_fetch_posts($post_limit = NULL) {
$fapr_count = 0;
$fapr_posts = array();
# Loop through all text files in provided directory
foreach(array_reverse(glob($this->directory . "posts/*.txt")) as $filename) {
$filename_parts = explode(".", (strstr($filename, '/') ? end(explode('/', $filename)) : $filename));
# Check for posts generated by FAPR
if(count($filename_parts) == 2 && is_numeric(reset($filename_parts)) && end($filename_parts) == 'txt') {
$post_tmp = array();
$post_raw = file($filename);
# Grab post date and name; then generate link (first line)
$post_tmp['date'] = reset($filename_parts);
$post_tmp['name'] = $post_raw[0];
$post_tmp['link'] = 'posts/' . reset($filename_parts) . '-' . strtolower(trim(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $post_raw[0])), '-'));
unset($post_raw[0]); $post_raw = array_values($post_raw);
$post_tmp['body'] = nl2br(implode("\r\n", $post_raw));
$fapr_posts[] = $post_tmp;
}
$fapr_count++; if(!is_numeric($post_limit)) { $post_limit = 5; } if($fapr_count >= $post_limit) { break; }
}
if(empty($fapr_posts)) {
return false;
} else {
return $fapr_posts;
}
}
}
?>