-
Notifications
You must be signed in to change notification settings - Fork 0
/
eatStaticBlog.class.php
executable file
·670 lines (538 loc) · 16.8 KB
/
eatStaticBlog.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<?php
/**
* @version 0.1.2
* 2011-07-13 - Rick Hurst added version number 0.1.0
* 2011-09-05 - Rick Hurst merged with working version from www.rickhurst.co.uk
* - Rick Hurst ES_ROOT replaced with SITE_ROOT
* - Rick Hurst added search function
* 2011-09-20 - Rick Hurst removed pipe from line splitting, upped version to 0.1.2
*/
class eatStaticBlog extends eatStatic {
var $post_folder;
var $recent_limit = POSTS_PER_PAGE;
var $post_files = array();
var $gallery_ids = array();
function __construct() {
$this->post_folder = DATA_ROOT.'/posts/';
}
public function getPostFiles($use_cache=USE_CACHE){
// reset post files array
$this->post_files = array();
if($use_cache){
// see if cache file exists
if(file_exists(CACHE_ROOT.'/all_posts.json')){
$this->post_files = json_decode(eatStatic::read_file(CACHE_ROOT.'/all_posts.json'));
} else {
// load posts from disk
$this->getPostFilesFromDisk();
// write array of post files to disk as JSON
eatStatic::write_file(json_encode($this->post_files), CACHE_ROOT.'/all_posts.json');
}
} else {
$this->getPostFilesFromDisk();
}
}
private function getPostFilesFromDisk(){
if (is_dir($this->post_folder)) {
if ($dh = opendir($this->post_folder)) {
while (($file = readdir($dh)) !== false) {
//echo "filename: $file : filetype: " . filetype($this->post_folder . $file) . "\n";
if(
(filetype($this->post_folder . $file) == 'file') &&
(substr($file,-3) == "txt" || substr($file,-2) == "md")
){
// for each post found
$this->post_files[] = $this->post_folder . $file;
}
}
sort($this->post_files);
closedir($dh);
}
}
}
public function getRecentPosts($limit=false){
$posts = array();
//$post_count = 0;
if(USE_CACHE){
// see if cache file exists
if(file_exists(CACHE_ROOT.'/recent_files.json')){
$recent_files = json_decode($this->read_file(CACHE_ROOT.'/recent_files.json'));
} else {
$recent_files = $this->getRecentPostsArray();
$this->write_file(json_encode($recent_files), CACHE_ROOT.'/recent_files.json');
}
} else {
$recent_files = $this->getRecentPostsArray();
}
foreach($recent_files as $post_file){
// for each post found
$post = new eatStaticBlogPost;
$post->data_file_path = $post_file;
$post->hydrate();
//$post_count++;
$posts[] = $post;
}
if($limit){
$posts = array_slice($posts, 0, $limit);
}
return $posts;
}
public function getSlicedPosts($page, $posts_per_page=POSTS_PER_PAGE){
$this->getPostFiles();
$all_files = array_reverse($this->post_files);
//print_r($all_files);
$sliced = array_slice($all_files, ($posts_per_page*$page), $posts_per_page);
//echo (POSTS_PER_PAGE*$page);
//print_r($sliced);
//die();
foreach($sliced as $post_file){
// for each post found
$post = new eatStaticBlogPost;
$post->data_file_path = $post_file;
$post->hydrate();
//$post_count++;
$posts[] = $post;
}
return $posts;
}
public function getDrafts(){
$posts = array();
$this->getPostFilesFromDisk();
sort($this->post_files);
$drafts = array_reverse($this->post_files);
foreach($drafts as $post_file){
// for each post found
$post = new eatStaticBlogPost;
$post->data_file_path = $post_file;
$post->hydrate();
//$post_count++;
$posts[] = $post;
}
return $posts;
}
private function getRecentPostsArray(){
$this->getPostFiles();
sort($this->post_files);
//print_r($this->post_files);
$recent_files = array_reverse($this->post_files);
array_splice($recent_files, $this->recent_limit);
return $recent_files;
}
public function getArchiveIndex(){
$this->getPostFiles();
$last_month = '';
$items = array();
sort($this->post_files);
foreach($this->post_files as $file){
$current_month = substr(basename($file), 0, 7);
if($current_month != $last_month){
$item = new eatStaticBlogArchiveItem;
$item->month_slug = $current_month;
$item->month_desc = date('F Y', strtotime($current_month."-01 00:00:00"));
$item->month = date('F', strtotime($current_month."-01 00:00:00"));
$item->year = date('Y', strtotime($current_month."-01 00:00:00"));
if(WP_URLS){
$item->uri = SITE_ROOT.date('Y', strtotime($current_month."-01 00:00:00")).'/'.date('m', strtotime($current_month."-01 00:00:00")).'/';
} else {
$item->uri = SITE_ROOT.'archive/'.$item->month_slug.PAGE_EXT;
}
$items[] = $item;
}
$last_month = $current_month;
}
$items = array_reverse($items);
return $items;
}
public function getArchiveList($month_slug){
$this->getPostFiles();
$posts = array();
//echo '['.$month_slug.']';
$post_count = 0;
foreach($this->post_files as $post_file){
//echo '['.substr(basename($file), 0, 7).']';
if(substr(basename($post_file), 0, 7) == $month_slug){
//echo 'yes';
// for each post found
$post = new eatStaticBlogPost;
$post->data_file_path = $post_file;
$post->hydrate();
$post_count++;
$posts[] = $post;
}
}
return $posts;
}
/**
* @desc return list of posts: slug, date, title
* as this is for admin only, always return uncached
*/
public function getPostList(){
$this->getPostFiles();
$all_files = array_reverse($this->post_files);
foreach($all_files as $post_file){
// for each post found
$post = new eatStaticBlogPost;
$post->data_file_path = $post_file;
$post->hydrate();
$item = array();
$item['slug'] = $post->slug;
$item['date'] = $post->date;
$item['title'] = $post->date;
//$post_count++;
$posts[] = $item;
}
return $posts;
}
// taken from http://www.zachstronaut.com/posts/2009/01/20/php-relative-date-time-string.html
public static function time_elapsed_string($ptime) {
$etime = time() - $ptime;
if ($etime < 1) {
return '0 seconds';
}
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($a as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's' : '');
}
}
}
public static function obsoleteWarning($ptime){
$etime = time() - $ptime;
$years = round($etime /(12 * 30 * 24 * 60 * 60));
if ($years > 4 ){
?>
<div class="warning">
This post was written <b><?php echo $years ?> years ago</b>, which in internet time is <em>really, really</em> old. This means that what is written above, and the links contained within, may now be obsolete, inaccurate or wildly out of context, so please bear that in mind :)
</div>
<?php
}
}
/**
* @desc search all posts and return array of post slugs
*/
public function search($term){
$matches = array();
$dir = DATA_ROOT.'/posts/';
//echo $dir;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
//echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
if(
(filetype($dir . $file) == 'file') &&
(substr($file,-4) == '.txt')
){
// for each post found
$content = eatStatic::read_file($dir.$file);
$match_count = substr_count($content,$term);
if($match_count > 0){
$matches[] = array('count'=>$match_count, 'file'=>$file);
}
//$matches[] = array('1', $file);
}
}
closedir($dh);
}
}
// see http://php.net/manual/en/function.array-multisort.php
// sort the array by matches
// Obtain a list of columns
foreach ($matches as $key => $row) {
$count[$key] = $row['count'];
$file[$key] = $row['file'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($count, SORT_DESC, $file, SORT_ASC, $matches);
return $matches;
}
}
class eatStaticPaginator {
var $cat='';
var $page_size=POSTS_PER_PAGE;
var $current=1;
var $partial='skin/global/templates/paginator.php';
var $total = 0;
var $pages = 0;
var $pagination_root = '';
public function render(){
if ($this->cat == ''){
if($this->total > $this->page_size){
$this->pages = ceil($this->total/$this->page_size);
}
if ($this->pages > 1 ){
require ROOT."/".$this->partial;
}
}
}
}
class eatStaticBlogPost extends eatStatic {
var $title;
var $body;
var $formatted_body;
var $data_file_path;
var $raw_data;
var $file_name;
var $slug;
var $date;
var $nice_date;
var $gallery_items = array();
var $tags=array();
var $author = BLOG_AUTHOR;
var $keywords = '';
var $description = '';
var $uri;
var $next_url = '';
var $prev_url = '';
var $source_format = 'text';
var $fields = array(); // store custom meta fields
function hydrate(){
$this->raw_data = $this->read_file($this->data_file_path);
if($this->raw_data == ''){
// no post content found
}
$ext = $this->getExtension($this->data_file_path);
if($ext == 'md'){
$this->source_format = 'markdown';
require_once(LIB_ROOT."/php-markdown/Markdown.inc.php");
}
$parts = explode("\n", $this->raw_data);
$str = '';
$format_str = '';
// get title from first line
$this->title = $parts[0];
$body = true;
$meta = false;
$raw_body = '';
$raw_meta = '';
// the rest is body
for($i=1; $i<sizeof($parts); $i++){
$str = $str.$parts[$i];
if($i > 1){
// the body section can be the rest of the file,
// or you can mark the end of the body section with --
// you can then put meta data fields in the file
if(eatStatic::stripLineBreaks($parts[$i]) == '--' && $meta == false){
$body = false;
$meta = true;
}
if($body){
$raw_body = $raw_body.$parts[$i]."\n";
// formatted body - for text format, line breaks need to be replaced with br, but not between html elements
if($this->source_format == 'text'){
if(substr($parts[$i],-1) != '>'){
$format_str = $format_str.$parts[$i]."<br />\n";
} else {
$format_str = $format_str.$parts[$i]."\n";
}
}
}
// get meta info
if($meta && eatStatic::stripLineBreaks($parts[$i]) != '--'){
//die('meta:'.$parts[$i]);
if($parts[$i] != ''){
$this->handleMeta($parts[$i]);
$raw_meta = $raw_meta.$parts[$i]."\n";
}
}
}
}
if($this->source_format == 'markdown'){
// remove hashes from title
$this->title = str_replace('#', '', $this->title);
// parse the markdown into HTML
$this->formatted_body = Michelf\Markdown::defaultTransform($raw_body);
} else {
$this->formatted_body = $format_str;
}
$this->raw_body = $raw_body;
$this->raw_meta = $raw_meta;
$this->body = $str;
$this->file_name = basename($this->data_file_path);
if($this->source_format == 'markdown'){
$this->slug = str_replace('.md','',$this->file_name);
} else {
$this->slug = str_replace('.txt','',$this->file_name);
}
$this->date = substr($this->file_name, 0, 10);
$this->slug_trimmed = str_replace($this->date.'-', '', $this->slug);
$this->nice_date = date(NICE_DATE_FORMAT, strtotime($this->date));
$this->timestamp = strtotime($this->date);
// get gallery items if there are any
$gallery = new eatStaticGallery($this->slug.'/');
$this->gallery_items = $gallery->gallery_items;
// set up the URI
if(WP_URLS){
$date_str = date('Y', strtotime($this->date)).'-'.date('m', strtotime($this->date)).'-'.date('d', strtotime($this->date));
$this->uri = SITE_ROOT.date('Y', strtotime($this->date)).'/'.date('m', strtotime($this->date)).'/'.date('d', strtotime($this->date)).'/'.str_replace($date_str .'-','',$this->slug).'/';
} else {
$this->uri = SITE_ROOT.'posts/'.$this->slug.PAGE_EXT;
}
// get the next and previous urls
$blog = new eatStaticBlog();
$blog->getPostFiles();
foreach($blog->post_files as $key=>$val){
if($val == $this->data_file_path){
if($key > 0){
$this->prev_url = $this->uriFromFilename(basename($blog->post_files[$key - 1]));
}
if($key < (count($blog->post_files)-1)){
$this->next_url = $this->uriFromFilename(basename($blog->post_files[$key + 1]));
}
}
}
}
private function uriFromFilename($file_name){
$post_date = substr($file_name, 0, 10);
$post_time = strtotime($post_date);
$post_slug = str_replace('.txt','',$file_name);
$post_slug = str_replace('.md','',$post_slug);
if(WP_URLS){
$date_str = date('Y', $post_time).'-'.date('m', $post_time).'-'.date('d', $post_time);
$uri = SITE_ROOT.date('Y', $post_time).'/'.date('m', $post_time).'/'.date('d', $post_time).'/'.str_replace($date_str .'-','',$post_slug).'/';
} else {
$uri = SITE_ROOT.'posts/'.$post_slug.PAGE_EXT;
}
return $uri;
}
private function handleMeta($str){
$parts = explode(":",$str);
//print_r($parts);
// echo "----<br>";
// echo "[".$str."]<br>";
// print_r($parts);
// echo count($parts)."<br>";
if(count($parts) > 1){
$key = $parts[0];
$value = $parts[1];
switch($key){
case "tags":
$tags = explode(",", $value);
foreach($tags as $tag){
if(trim($tag) != ''){
$this->tags[] = trim($tag);
}
}
break;
case "keywords":
$this->keywords = $value;
break;
case "description":
$this->description = $value;
break;
case "author":
$this->author = $value;
break;
default:
$this->fields[$key] = $value;
break;
}
}
}
function getField($key){
if(isset($this->fields[$key])){
return $this->fields[$key];
}
}
}
class eatStaticBlogArchiveItem {
var $month_desc;
var $month_slug;
}
class eatStaticGallery extends eatStatic {
var $folder;
var $thumb_width = 150;
var $view_width = 700;
var $gallery_folder;
var $gallery_items = array();
var $captions = array();
var $allowed_extensions = array('JPG','jpg','png');
function __construct($folder, $thumb_width=''){
$this->folder = $folder;
$this->gallery_folder = DATA_ROOT.'/images/'.$this->folder;
if($thumb_width != '') $this->thumb_width = $thumb_width;
$this->doGallery();
}
function doGallery(){
$this->getCaptions();
$this->getGalleryFiles();
}
/**
*@desc load array of captions from index.txt if it exists
*/
private function getCaptions(){
if (is_dir($this->gallery_folder) && file_exists($this->gallery_folder.'index.txt')){
$captions = $this->read_file($this->gallery_folder.'index.txt');
$caption_lines = explode("\n", $captions);
foreach($caption_lines as $line){
$parts = explode(":", $line);
$this->captions[$parts[0]] = trim($parts[1]);
}
}
//print_r($this->captions);
//die();
}
private function getGalleryFiles(){
if (is_dir($this->gallery_folder)) {
if ($dh = opendir($this->gallery_folder)) {
while (($file = readdir($dh)) !== false) {
$arr = explode(".", $file);
$ext = end($arr);
$ext = strtolower($ext);
//print_r($this->captions);
//die();
//echo $ext;
//echo "filename: $file : filetype: " . filetype($this->gallery_folder . $file) . "\n";
if((filetype($this->gallery_folder . $file) == 'file') && (in_array($ext, $this->allowed_extensions))){
// for each post found
$item = new eatStaticGalleryItem;
$item->ext = $this->getExtension($file);
$item->source_file_path = $this->gallery_folder . $file;
$item->thumb_url = "/images/".$this->folder.str_replace('.'.$item->ext, '_'.$this->thumb_width.'.'.$ext , $file);
$item->view_url = "/images/".$this->folder.str_replace('.'.$item->ext, '_'.$this->view_width.'.'.$ext, $file);
$file = str_replace($ext, strtolower($ext), $file);
if(isset($this->captions[$file])){
$item->caption = $this->captions[$file];
}
//$item->caption = $file;
$this->gallery_items[] = $item;
}
}
closedir($dh);
}
}
}
}
class eatStaticGalleryItem {
var $source_file_path;
var $thumb_url;
var $view_url;
var $title;
var $caption;
var $ext;
}
class eatStaticBlogFeed {
var $blog_title;
var $blog_link;
var $blog_description;
var $pub_date;
var $items = array();
}
class eatStaticBlogFeedItem {
var $title;
var $pub_date;
var $author;
var $url;
var $summary;
var $formatted_body;
var $tags=array();
}
?>