-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.php
353 lines (276 loc) · 11.5 KB
/
model.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
<?php
include('_globals.php');
# DB access to be used throughout the code
function getRecentPostSummaries($num) {
$query = "SELECT guid,title,pubDate,seoName " .
"FROM posts " .
"WHERE draft=0 " .
"ORDER BY pubDate DESC LIMIT $num";
# get result
$result = mysql_query($query) or die("Unable to retrieve recent post summaries");
$posts = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
# reformat date
$dateStamp = strtotime( $row['pubDate'] );
$row['isoDate'] = strftime( "%Y-%m-%dT%H:%M:%IZ", $dateStamp );
$row['rssDate'] = strftime( "%a, %d %h %Y %H:%M:%I %z", $dateStamp );
$row['pubDate'] = strftime( "%A, %B %e, %Y", $dateStamp );
$row['pubTime'] = strftime( "%l:%M %P", $dateStamp );
array_push($posts, $row);
}
return $posts;
}
function getRecentPosts($num, $offset=0) {
$query = "SELECT posts.*, count(comments.post_guid) AS comments, group_concat(tags.tag) as taglist " .
"FROM posts LEFT OUTER JOIN comments ON posts.guid=comments.post_guid " .
"LEFT OUTER JOIN tags ON tags.post_guid=posts.guid " .
"WHERE draft=0 " .
"GROUP BY guid " .
"ORDER BY pubDate DESC LIMIT $num OFFSET $offset";
# get result
$result = mysql_query($query) or die("Unable to retrieve selected post(s)");
#if (mysql_num_rows($result) == 0) { show404(); }
$posts = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
# reformat date
$dateStamp = strtotime( $row['pubDate'] );
$row['isoDate'] = strftime( "%Y-%m-%dT%H:%M:%IZ", $dateStamp );
$row['rssDate'] = strftime( "%a, %d %h %Y %H:%M:%I %z", $dateStamp );
$row['pubDate'] = strftime( "%A, %B %e, %Y", $dateStamp );
$row['pubTime'] = strftime( "%l:%M %P", $dateStamp );
$row['tags'] = explode(',',$row['taglist']);
array_push($posts, $row);
}
return $posts;
}
function _getPostTags($guid) {
# get post tags (helper function)
$query = "SELECT tag FROM tags WHERE post_guid='$guid' ORDER BY tag";
$result = mysql_query($query) or die("Unable to retrieve selected post tags");
$tags = array();
while ($row = mysql_fetch_row($result)) {
array_push($tags, $row[0]);
}
return $tags;
}
function getPostBySEOName($name) {
# get post details
$query = "SELECT * FROM posts WHERE seoName='$name'";
$result = mysql_query($query) or die("Unable to retrieve post data for '" . $name . "'");
$postdata = mysql_fetch_array($result, MYSQL_ASSOC);
$guid = $postdata['guid'];
# reformat date
$dateStamp = strtotime( $postdata['pubDate'] );
$postdata['isoDate'] = strftime( "%Y-%m-%dT%H:%M:%IZ", $dateStamp );
$postdata['pubDate'] = strftime( "%A, %B %e, %Y", $dateStamp );
$postdata['pubTime'] = strftime( "%l:%M %P", $dateStamp );
# get post comments
$query = "SELECT * FROM comments WHERE post_guid='$guid' ORDER BY addDate ASC";
$result = mysql_query($query) or die("Unable to retrieve selected post comments");
$comments = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
array_push($comments, $row);
}
# get post tags
$tags = _getPostTags($guid);
return array(
'post' => $postdata,
'comments' => $comments,
'tags' => $tags
);
}
function getPostDetails($guid) {
# get post details
$query = "SELECT * FROM posts WHERE guid='$guid'";
$result = mysql_query($query) or die("Unable to retrieve post data for #" . $guid);
$postdata = mysql_fetch_array($result, MYSQL_ASSOC);
# reformat date
$dateStamp = strtotime( $postdata['pubDate'] );
$postdata['isoDate'] = strftime( "%Y-%m-%dT%H:%M:%IZ", $dateStamp );
$postdata['pubDate'] = strftime( "%A, %B %e, %Y", $dateStamp );
$postdata['pubTime'] = strftime( "%l:%M %P", $dateStamp );
# get post comments
$query = "SELECT * FROM comments WHERE post_guid='$guid' ORDER BY addDate ASC";
$result = mysql_query($query) or die("Unable to retrieve selected post comments");
$comments = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
array_push($comments, $row);
}
# get post tags
$tags = _getPostTags($guid);
return array(
'post' => $postdata,
'comments' => $comments,
'tags' => $tags
);
}
function getPostsByTag($tag, $num=0, $offset=0) {
# get posts first, then grab comment counts & tag lists for each
#$query = "SELECT * FROM posts " .
# "WHERE guid in (SELECT distinct post_guid FROM tags where tag='$tag') AND draft=0 " .
# "ORDER BY pubDate DESC";
# get blog constants
global $blog;
$me = $blog['author'];
$criteria = "guid in (SELECT distinct post_guid FROM tags where tag='$tag') AND draft=0 ";
$query = "SELECT posts.*, count(post_guid) AS comments " .
"FROM posts LEFT OUTER JOIN comments ON posts.guid=comments.post_guid " .
"WHERE $criteria " .
"GROUP BY guid " .
"ORDER BY pubDate DESC";
if ($num) { $query = $query . " LIMIT $num"; }
if ($offset) { $query = $query . " OFFSET $offset"; }
# get result
$result = mysql_query($query) or die("Unable to retrieve posts for tag '$tag'.");
$posts = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
# massage datestamps a bit
$dateStamp = strtotime( $row['pubDate'] );
$row['pubDate'] = strftime( "%A, %B %e, %Y", $dateStamp );
$row['pubTime'] = strftime( "%l:%M %P", $dateStamp );
# get tags for this post
$row['tags'] = _getPostTags($row['guid']);
array_push($posts, $row);
}
return $posts;
}
function savePost($guid, $title, $dateStr, $content, $draft, $tags, $seoName) {
# get blog constants
global $blog;
$me = $blog['author'];
if ($guid) {
// update existing post
$update = "UPDATE posts SET title='$title', pubDate='$dateStr', ".
"author='$me', body='$content', draft=$draft, seoName='$seoName' " .
"WHERE guid=$guid";
$results = mysql_query($update) or safe_die("Failed to save post.", $update);
// save tags (removing any existing ones first)
$delTags = "DELETE FROM tags WHERE post_guid=$guid";
$results = mysql_query($delTags) or safe_die("Failed to save post.", $delTags);
$tagArray = explode(",", $tags);
foreach ($tagArray as &$tag) {
$trimmed = trim($tag);
$insert = "INSERT INTO tags (tag, post_guid) VALUES ('$trimmed', $guid)";
mysql_query($insert);
}
} else {
// save new post
$insert = "INSERT INTO posts (title, pubDate, author, body, draft, seoName)" .
" VALUES ('$title', '$dateStr', '$me', '$content', $draft, '$seoName')";
$results = mysql_query($insert) or safe_die("Failed to save post.", $insert);
// get guid
$select = "SELECT guid FROM posts WHERE seoName='$seoName' ORDER BY guid DESC";
$errMsg = "Can't get post ID; your post has been saved, but its tags were not.<br/>Error details: ";
$results = mysql_query($select) or safe_die($errMsg, $select);
$row = mysql_fetch_row($result);
$guid = $row[0];
// save tags
$tagArray = explode(",", $tags);
foreach ($tagArray as &$tag) {
$trimmed = trim($tag);
$insert = "INSERT INTO tags (tag, post_guid) VALUES ('$trimmed', $guid)";
mysql_query($insert);
}
}
return 0;
}
function deletePosts($guids='') {
if ($guids) {
$guidArray = explode(",", $guids);
foreach ($guidArray as &$guid) {
# remove post
$sql = "DELETE FROM posts WHERE guid=$guid";
mysql_query($sql) or die( "Failed to delete post. <br/>Error details: " . mysql_error() );
# remove post tags
$sql = "DELETE FROM tags WHERE post_guid=$guid";
mysql_query($sql) or die( "Failed to delete post tags. <br/>Error details: " . mysql_error() );
# remove post comments
$sql = "DELETE FROM comments WHERE post_guid=$guid";
mysql_query($sql) or die( "Failed to delete post comments. <br/>Error details: " . mysql_error() );
}
}
return 0;
}
function getTags() {
$query = "SELECT tag,count(post_guid) FROM tags INNER JOIN posts ON guid=post_guid WHERE draft=0 GROUP BY tag";
$result = mysql_query($query) or die("Unable to retrieve selected post(s)");
$tags = array();
while ($row = mysql_fetch_row($result)) {
$tags[$row[0]] = (int)$row[1];
}
return $tags;
}
function getLastUpdateDate() {
$query = "SELECT pubDate FROM posts " .
"ORDER BY pubDate DESC LIMIT 1";
# get result
$result = mysql_query($query) or die("Unable to retrieve updated date information");
if (mysql_num_rows($result) == 0) {
$theDate = time();
} else {
$row = mysql_fetch_row($result);
$theDate = strtotime($row[0]);
}
return $theDate;
}
function getPageDetails($name) {
# get page details
$query = "SELECT * FROM pages WHERE name='$name'";
$result = mysql_query($query) or die("Unable to retrieve page data for page '" . $name . "'");
$pagedata = mysql_fetch_array($result, MYSQL_ASSOC);
# reformat date
$dateStamp = strtotime( $pagedata['pubDate'] );
$pagedata['isoDate'] = strftime( "%Y-%m-%dT%H:%M:%IZ", $dateStamp );
$pagedata['pubDate'] = strftime( "%A, %B %e, %Y", $dateStamp );
$pagedata['pubTime'] = strftime( "%l:%M %P", $dateStamp );
return $pagedata;
}
function savePage($name, $dateStr, $content, $standalone) {
# get blog constants
global $blog;
$me = $blog['author'];
if ($name) {
// see if that page already exists
$select = "SELECT name FROM pages WHERE name='$name'";
$errMsg = "Can't get existing page details; your post cannot be saved.<br/>Error details: ";
$result = mysql_query($select) or die( $errMsg . mysql_error());
$row = mysql_fetch_row($result);
$existing_name = $row[0];
if ($existing_name) {
// update existing page
$update = "UPDATE pages SET pubDate='$dateStr', ".
"author='$me', body='$content', standalone=$standalone " .
"WHERE name='$name'";
mysql_query($update) or die( "Failed to save page. <br/>Error details: " . mysql_error() );
} else {
// save new page
$insert = "INSERT INTO pages (name, pubDate, author, body, standalone)" .
" VALUES ('$name', '$dateStr', '$me', '$content', $standalone)";
mysql_query($insert) or die( "Failed to save page. <br/>Error details: " . mysql_error() );
}
} else {
return "No name provided";
}
return 0;
}
function deletePages($names='') {
if ($names) {
$nameArray = explode(",", $names);
foreach ($nameArray as &$name) {
# remove page
$sql = "DELETE FROM pages WHERE name='$name'";
mysql_query($sql) or die( "Failed to delete page. <br/>Error details: " . mysql_error() );
}
}
return 0;
}
$blacklist = array("a", "an", "the");
$whitespace = array(" ", ".", "\n", "\r", "\t");
function getSEO($title) {
# get blog constants
global $blacklist;
global $whitespace;
$work = str_ireplace($whitespace, "-", str_ireplace($blacklist, "", $title));
return strtolower($work);
}
?>