forked from johnroper100/dropplets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
665 lines (508 loc) · 24.9 KB
/
index.php
File metadata and controls
665 lines (508 loc) · 24.9 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
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
<?php
if (file_exists('./dropplets/config/config-settings.php')) {
/*-----------------------------------------------------------------------------------*/
/* Debug Mode
/*-----------------------------------------------------------------------------------*/
$display_errors = false;
/*-----------------------------------------------------------------------------------*/
/* Post Cache ('on' or 'off')
/*-----------------------------------------------------------------------------------*/
$post_cache = 'off';
$index_cache = 'off';
/*-----------------------------------------------------------------------------------*/
/* Configuration & Options
/*-----------------------------------------------------------------------------------*/
include('./dropplets/config/config-settings.php');
include('./dropplets/config/config-template.php');
// A few definitions.
$language = 'en-us';
$feed_max_items = '10';
$date_format = 'F jS, Y';
$error_title = 'Sorry, But That’s Not Here';
$error_text = 'Really sorry, but what you’re looking for isn’t here. Click the button below to find something else that might interest you.';
$powered_by = '<a class="powered-by" href="http://dropplets.com" target="_blank"><img src="' . $blog_url . '/dropplets/style/images/powered-by.png" />Powered by Dropplets</a>';
/*-----------------------------------------------------------------------------------*/
/* Post Configuration
/*-----------------------------------------------------------------------------------*/
$pagination_on_off = "on"; //Infinite scroll by default?
$posts_per_page = 4;
$infinite_scroll = "on"; //Infinite scroll works only if pagination is on.
$post_directory = './posts/';
$cache_directory = './posts/cache/';
if (glob($post_directory . '*.md') != false)
{
$posts_dir = './posts/';
} else {
$posts_dir = './dropplets/welcome/';
}
define('POSTS_DIR', $posts_dir);
define('FILE_EXT', '.md');
/*-----------------------------------------------------------------------------------*/
/* Cache Configuration
/*-----------------------------------------------------------------------------------*/
define('CACHE_DIR', $cache_directory);
if (!file_exists(CACHE_DIR) && ($post_cache != 'off' || $index_cache != 'off')) {
mkdir(CACHE_DIR,0755,TRUE);
}
/*-----------------------------------------------------------------------------------*/
/* Template Files
/*-----------------------------------------------------------------------------------*/
$template_dir = './templates/' . $template . '/';
$template_dir_url = $blog_url . '/templates/' . $template . '/';
// define the default locations of the template files.
$index_file = $template_dir . 'index.php';
$intro_file = $template_dir . 'intro.php';
$post_file = $template_dir . 'post.php';
$posts_file = $template_dir . 'posts.php';
$not_found_file = $template_dir . '404.php';
/*-----------------------------------------------------------------------------------*/
/* Let's Get Started
/*-----------------------------------------------------------------------------------*/
// Display errors if there are any.
ini_set('display_errors', $display_errors);
// Get required plugins.
foreach(glob('./dropplets/plugins/' . '*.php') as $plugin){
include_once $plugin;
}
// Reading file names.
$category = NULL;
if (empty($_GET['filename'])) {
$filename = NULL;
} else if($_GET['filename'] == 'rss' || $_GET['filename'] == 'atom') {
$filename = $_GET['filename'];
} else {
//Filename can be /some/blog/post-filename.md We should get the last part only
$filename = explode('/',$_GET['filename']);
// File name could be the name of a category
if($filename[count($filename) - 2] == "category") {
$category = $filename[count($filename) - 1];
$filename = null;
} else {
// Individual Post
$filename = POSTS_DIR . $filename[count($filename) - 1] . FILE_EXT;
}
}
/*-----------------------------------------------------------------------------------*/
/* The Home Page (All Posts)
/*-----------------------------------------------------------------------------------*/
if ($filename==NULL) {
$page = (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1) ? $_GET['page'] : 1;
$offset = ($page == 1) ? 0 : ($page - 1) * $posts_per_page;
//Index page cache file name, will be used if index_cache = "on"
$cachefile = CACHE_DIR . ($category ? $category : "index") .$page. '.html';
//If index cache file exists, serve it directly wihout getting all posts
if (file_exists($cachefile) && $index_cache != 'off') {
// Get the cached post.
include $cachefile;
exit;
// If there is a file for the selected permalink, display and cache the post.
}
if($category) {
$all_posts = get_posts_for_category($category);
} else {
$all_posts = get_all_posts();
}
$pagination = ($pagination_on_off != "off") ? get_pagination($page,round(count($all_posts)/ $posts_per_page)) : "";
$posts = ($pagination_on_off != "off") ? array_slice($all_posts,$offset,($posts_per_page > 0) ? $posts_per_page : null) : $all_posts;
if($posts) {
ob_start();
$content = '';
foreach($posts as $post) {
// Get the post title.
$post_title = str_replace(array("\n",'<h1>','</h1>'), '', $post['post_title']);
// Get the post author.
$post_author = $post['post_author'];
// Get the post author twitter id.
$post_author_twitter = $post['post_author_twitter'];
// Get the published ISO date.
$published_iso_date = $post['post_date'];
// Generate the published date.
$published_date = date_format(date_create($published_iso_date), $date_format);
// Get the post category.
$post_category = $post['post_category'];
// Get the post status.
$post_status = $post['post_status'];
// Get the post intro.
$post_intro = $post['post_intro'];
// Get the post content
$post_content = $post['post_content'];
// Get the post link.
if ($category) {
$post_link = trim(strtolower($post_category)).'/'.str_replace(FILE_EXT, '', $post['fname']);
} else {
$post_link = str_replace(FILE_EXT, '', $post['fname']);
}
// Get the post image url.
$image = str_replace(array(FILE_EXT), '', POSTS_DIR.$post['fname']).'.jpg';
if (file_exists($image)) {
$post_image = $blog_url.'/'.str_replace(array(FILE_EXT, '../'), '', POSTS_DIR.$post['fname']).'.jpg';
} else {
$post_image = get_twitter_profile_img($post_author_twitter);
}
// Get the site intro template file.
if ($category) {
// No intro for categories.
} else {
include_once $intro_file;
}
// Get the milti-post template file.
include $posts_file;
}
echo $content;
$content = ob_get_contents();
// Get the site title
$page_title = $blog_title;
$blog_image = 'https://api.twitter.com/1/users/profile_image?screen_name='.$blog_twitter.'&size=bigger';
// Get the page description and author meta.
$get_page_meta[] = '<meta name="description" content="' . $meta_description . '">';
$get_page_meta[] = '<meta name="author" content="' . $blog_title . '">';
// Get the Twitter card.
$get_page_meta[] = '<meta name="twitter:card" content="summary">';
$get_page_meta[] = '<meta name="twitter:site" content="' . $blog_twitter . '">';
$get_page_meta[] = '<meta name="twitter:title" content="' . $blog_title . '">';
$get_page_meta[] = '<meta name="twitter:description" content="' . $meta_description . '">';
$get_page_meta[] = '<meta name="twitter:creator" content="' . $blog_twitter . '">';
$get_page_meta[] = '<meta name="twitter:image:src" content="' . $blog_image . '">';
$get_page_meta[] = '<meta name="twitter:domain" content="' . $blog_url . '">';
// Get the Open Graph tags.
$get_page_meta[] = '<meta property="og:type" content="website">';
$get_page_meta[] = '<meta property="og:title" content="' . $blog_title . '">';
$get_page_meta[] = '<meta property="og:site_name" content="' . $blog_title . '">';
$get_page_meta[] = '<meta property="og:url" content="' .$blog_url . '">';
$get_page_meta[] = '<meta property="og:description" content="' . $meta_description . '">';
$get_page_meta[] = '<meta property="og:image" content="' . $blog_image . '">';
// Get all page meta.
$page_meta = implode("\n", $get_page_meta);
ob_end_clean();
} else {
ob_start();
// Define the site title.
$page_title = $error_title;
$page_meta = '';
// Get the 404 page template.
include $not_found_file;
//Get the contents
$content = ob_get_contents();
//Flush the buffer so that we dont get the page 2x times
ob_end_clean();
}
ob_start();
// Get the index template file.
include_once $index_file;
//Now that we have the whole index page generated, put it in cache folder
if ($index_cache != 'off') {
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
}
}
/*-----------------------------------------------------------------------------------*/
/* RSS Feed
/*-----------------------------------------------------------------------------------*/
else if ($filename == 'rss' || $filename == 'atom') {
($filename=='rss') ? $feed = new FeedWriter(RSS2) : $feed = new FeedWriter(ATOM);
$feed->setTitle($blog_title);
$feed->setLink($blog_url);
if($filename=='rss') {
$feed->setDescription($meta_description);
$feed->setChannelElement('language', $language);
$feed->setChannelElement('pubDate', date(DATE_RSS, time()));
} else {
$feed->setChannelElement('author', $blog_title.' - ' . $blog_email);
$feed->setChannelElement('updated', date(DATE_RSS, time()));
}
$posts = get_all_posts();
if($posts) {
$c=0;
foreach($posts as $post) {
if($c<$feed_max_items) {
$item = $feed->createNewItem();
// Remove HTML from the RSS feed.
$item->setTitle(substr($post['post_title'], 4, -6));
$item->setLink(rtrim($blog_url, '/').'/'.str_replace(FILE_EXT, '', $post['fname']));
$item->setDate($post['post_date']);
// Remove Meta from the RSS feed.
$remove_metadata_from = file(rtrim(POSTS_DIR, '/').'/'.$post['fname']);
if($filename=='rss') {
$item->addElement('author', str_replace('-', '', $remove_metadata_from[1]).' - ' . $blog_email);
$item->addElement('guid', rtrim($blog_url, '/').'/'.str_replace(FILE_EXT, '', $post['fname']));
}
// Remove the metadata from the RSS feed.
unset($remove_metadata_from[0], $remove_metadata_from[1], $remove_metadata_from[2], $remove_metadata_from[3], $remove_metadata_from[4], $remove_metadata_from[5]);
$remove_metadata_from = array_values($remove_metadata_from);
$item->setDescription(Markdown(implode($remove_metadata_from)));
$feed->addItem($item);
$c++;
}
}
}
$feed->genarateFeed();
}
/*-----------------------------------------------------------------------------------*/
/* Single Post Pages
/*-----------------------------------------------------------------------------------*/
else {
ob_start();
// Define the post file.
$fcontents = file($filename);
$slug_array = explode("/", $filename);
$slug_len = count($slug_array);
// This was hardcoded array index, it should always return the last index.
$slug = str_replace(array(FILE_EXT), '', $slug_array[$slug_len - 1]);
// Define the cached file.
$cachefile = CACHE_DIR.$slug.'.html';
// If there's no file for the selected permalink, grab the 404 page template.
if (!file_exists($filename)) {
//Change the cache file to 404 page.
$cachefile = CACHE_DIR.'404.html';
// Define the site title.
$page_title = $error_title;
// Get the 404 page template.
include $not_found_file;
// Get the contents.
$content = ob_get_contents();
// Flush the buffer so that we dont get the page 2x times.
ob_end_clean();
// Start new buffer.
ob_start();
// Get the index template file.
include_once $index_file;
// Cache the post on if caching is turned on.
if ($post_cache != 'off')
{
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
}
// If there is a cached file for the selected permalink, display the cached post.
} else if (file_exists($cachefile)) {
// Define site title
$page_title = str_replace('# ', '', $fcontents[0]);
// Get the cached post.
include $cachefile;
exit;
// If there is a file for the selected permalink, display and cache the post.
} else {
// Get the post title.
$post_title = str_replace(array("\n", '# '), '', $fcontents[0]);
// Get the post title.
$post_intro = htmlspecialchars($fcontents[7]);
// Get the post author.
$post_author = str_replace(array("\n", '-'), '', $fcontents[1]);
// Get the post author Twitter ID.
$post_author_twitter = str_replace(array("\n", '- '), '', $fcontents[2]);
// Get the published date.
$published_iso_date = str_replace('-', '', $fcontents[3]);
// Generate the published date.
$published_date = date_format(date_create($published_iso_date), $date_format);
// Get the post category.
$post_category = str_replace(array("\n", '-'), '', $fcontents[4]);
// Get the post link.
$post_link = $blog_url.'/'.str_replace(array(FILE_EXT, POSTS_DIR), '', $filename);
// Get the post image url.
$image = str_replace(array(FILE_EXT), '', $filename).'.jpg';
if (file_exists($image)) {
$post_image = $blog_url.'/'.str_replace(array(FILE_EXT, '../'), '', $filename).'.jpg';
} else {
$post_image = get_twitter_profile_img($post_author_twitter);
}
// Get the site title.
$page_title = str_replace('# ', '', $fcontents[0]);
// Generate the page description and author meta.
$get_page_meta[] = '<meta name="description" content="' . $post_intro . '">';
$get_page_meta[] = '<meta name="author" content="' . $post_author . '">';
// Generate the Twitter card.
$get_page_meta[] = '<meta name="twitter:card" content="summary">';
$get_page_meta[] = '<meta name="twitter:site" content="' . $blog_twitter . '">';
$get_page_meta[] = '<meta name="twitter:title" content="' . $page_title . '">';
$get_page_meta[] = '<meta name="twitter:description" content="' . $post_intro . '">';
$get_page_meta[] = '<meta name="twitter:creator" content="' . $post_author_twitter . '">';
$get_page_meta[] = '<meta name="twitter:image:src" content="' . $post_image . '">';
$get_page_meta[] = '<meta name="twitter:domain" content="' . $post_link . '">';
// Get the Open Graph tags.
$get_page_meta[] = '<meta property="og:type" content="article">';
$get_page_meta[] = '<meta property="og:title" content="' . $page_title . '">';
$get_page_meta[] = '<meta property="og:site_name" content="' . $page_title . '">';
$get_page_meta[] = '<meta property="og:url" content="' . $post_link . '">';
$get_page_meta[] = '<meta property="og:description" content="' . $post_intro . '">';
$get_page_meta[] = '<meta property="og:image" content="' . $post_image . '">';
// Generate all page meta.
$page_meta = implode("\n", $get_page_meta);
// Generate the post.
$post = Markdown(join('', $fcontents));
// Get the post template file.
include $post_file;
$content = ob_get_contents();
ob_end_clean();
ob_start();
// Get the index template file.
include_once $index_file;
// Cache the post on if caching is turned on.
if ($post_cache != 'off')
{
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
}
}
}
/*-----------------------------------------------------------------------------------*/
/* Run Setup if No Config
/*-----------------------------------------------------------------------------------*/
} else {
// Fetch the current url.
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
// Subdirectory support.
$dir = dirname($_SERVER['REQUEST_URI']) . basename($_SERVER['REQUEST_URI']);
$url = $protocol . '://' . $host . $dir;
$is_writable = (TRUE == is_writable(dirname(__FILE__) . '/dropplets/config/'));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Let's Get Started</title>
<link rel="stylesheet" href="./dropplets/style/style.css" />
<link rel="shortcut icon" href="./dropplets/style/images/favicon.png">
</head>
<body>
<img src="./dropplets/style/images/logo.png" alt="Dropplets" />
<h1>Let's Get Started</h1>
<p>With Dropplets, there's no database or confusing admins to worry about, just simple markdown blogging goodness. To get started, enter your site information below (all fields are required) and then click the check mark at the bottom. That's all there's to it :)</p>
<?php if (!$is_writable) { ?>
<p style="color:red;">It seems that your config folder is not writable, please add the necessary permissions.</p>
<?php } ?>
<form method="POST" action="./dropplets/config/submit-settings.php">
<fieldset>
<div class="input">
<input type="text" name="blog_email" id="blog_email" required placeholder="The Email Address for Your Blog">
</div>
<div class="input">
<input type="text" name="blog_twitter" id="blog_twitter" required placeholder="The Twitter ID for Your Blog (e.g. "dropplets")">
</div>
</fieldset>
<fieldset>
<div class="input hidden">
<input type="text" name="blog_url" id="blog_url" required readonly value="<?php echo($url) ?>">
</div>
<div class="input">
<input type="text" name="blog_title" id="blog_title" required placeholder="Your Blog Title">
</div>
<div class="input">
<textarea name="meta_description" id="meta_description" rows="6" required placeholder="Add your site description here... just a short sentence that describes what your blog is going to be about."></textarea>
</div>
</fieldset>
<fieldset>
<div class="input">
<input type="text" name="intro_title" id="intro_title" required placeholder="Your Intro Title">
</div>
<div class="input">
<textarea name="intro_text" id="intro_text" rows="12" required placeholder="Add your intro text here. The "intro" displayed at the top of the home page of your blog and is generally intended to introduce who you are to your readers, kind of like an "about" page."></textarea>
</div>
</fieldset>
<fieldset>
<div class="input">
<input type="password" name="password" id="password" required placeholder="Enter a Password">
</div>
<div class="input">
<input type="password" name="password-confirmation" id="password-confirmation" required placeholder="Confirm Your Password" onblur="confirmPass()">
</div>
</fieldset>
<fieldset class="hidden">
<div class="input">
<input type="text" name="template" id="template" required value="simple">
</div>
</fieldset>
<button type="submit" name="submit" value="submit"></button>
</form>
<script>
function confirmPass() {
var pass = document.getElementById("password").value
var confPass = document.getElementById("password-confirmation").value
if(pass != confPass) {
alert('Your passwords do not match!');
}
}
</script>
</body>
</html>
<?php }
/*-----------------------------------------------------------------------------------*/
/* Get All Posts Function (Used For the Home Page Above)
/*-----------------------------------------------------------------------------------*/
function get_all_posts($options = array()) {
global $dropplets;
if($handle = opendir(POSTS_DIR)) {
$files = array();
$filetimes = array();
while (false !== ($entry = readdir($handle))) {
if(substr(strrchr($entry,'.'),1)==ltrim(FILE_EXT, '.')) {
// Define the post file.
$fcontents = file(POSTS_DIR.$entry);
// Define the post title.
$post_title = Markdown($fcontents[0]);
// Define the post author.
$post_author = str_replace(array("\n", '-'), '', $fcontents[1]);
// Define the post author Twitter account.
$post_author_twitter = str_replace(array("\n", '- '), '', $fcontents[2]);
// Define the published date.
$post_date = str_replace('-', '', $fcontents[3]);
// Define the post category.
$post_category = str_replace(array("\n", '-'), '', $fcontents[4]);
// Early return if we only want posts from a certain category
if($options["category"] && $options["category"] != trim(strtolower($post_category))) {
continue;
}
// Define the post status.
$post_status = str_replace(array("\n", '- '), '', $fcontents[5]);
// Define the post intro.
$post_intro = Markdown($fcontents[7]);
// Define the post content
$post_content = Markdown(join('', array_slice($fcontents, 6, $fcontents.length -1)));
// Pull everything together for the loop.
$files[] = array('fname' => $entry, 'post_title' => $post_title, 'post_author' => $post_author, 'post_author_twitter' => $post_author_twitter, 'post_date' => $post_date, 'post_category' => $post_category, 'post_status' => $post_status, 'post_intro' => $post_intro, 'post_content' => $post_content);
$post_dates[] = $post_date;
$post_titles[] = $post_title;
$post_authors[] = $post_author;
$post_authors_twitter[] = $post_author_twitter;
$post_categories[] = $post_category;
$post_statuses[] = $post_status;
$post_intros[] = $post_intro;
$post_contents[] = $post_content;
}
}
array_multisort($post_dates, SORT_DESC, $files);
return $files;
} else {
return false;
}
}
/*-----------------------------------------------------------------------------------*/
/* Get Posts for Category
/*-----------------------------------------------------------------------------------*/
function get_posts_for_category($category) {
$category = trim(strtolower($category));
return get_all_posts(array("category" => $category));
}
function get_pagination($page,$total) {
$string = '';
$string .= "<ul style=\"list-style:none; width:400px; margin:15px auto;\">";
for ($i = 1; $i<=$total;$i++) {
if ($i == $page) {
$string .= "<li style='display: inline-block; margin:5px;' class=\"active\"><a class=\"button\" href='#'>".$i."</a></li>";
} else {
$string .= "<li style='display: inline-block; margin:5px;'><a class=\"button\" href=\"?page=".$i."\">".$i."</a></li>";
}
}
$string .= "</ul>";
return $string;
}
/*-----------------------------------------------------------------------------------*/
/* Get Twitter Profile Image
/*-----------------------------------------------------------------------------------*/
function get_twitter_profile_img($username, $size = '') {
$api_call = 'https://twitter.com/users/'.$username.'.json';
$results = json_decode(file_get_contents($api_call));
return str_replace('_normal', $size, $results->profile_image_url);
}