<?php

/* Developed by Harold Kim (https://harold.kim), PHP7+ */

//error_reporting(7);
//ini_set('display_errors', 'on');


/*
    0=not listed, but links can be read
    1=not listed, links are even blocked
    2=listed, but links are blocked
*/
define("__REAL_SECRET__""0");
define("__TEMPLATE__""./template/");
define("__ARTICLE__""./_data/");

@require(
"./vendor/autoload.php");

class 
Article {
    public 
$title;
    public 
$date;
    public 
$tag;
    public 
$category;
    public 
$content;
    public 
$url;
    public 
$secret false// optional
}

class 
ArticleHandler {
    protected 
$articles;
    protected 
$url_filter "/[^\dA-Za-z\-\_가-힣]/im";
    protected 
$dir __ARTICLE__;
    protected 
$sep '----';
    protected 
$sort_by '';

    private function 
load_articles(){
        
/* load articles from the directory. */
        
if(is_array($articles)) return false;
        
$articles = @array_diff(scandir($this->dir), ['.''..']);

        foreach(
$articles as $article){
            
$parsed_data = new Article();

            
// read file //
            
$article_filename $this->dir $article;
            
$article = @file_get_contents($article_filename);
            if(!
$article) continue;

            
// divide header and content //
            
$article explode($this->sep$article);
            
$_header trim($article[1]);
            
$_content trim(implode($this->separray_splice($article2)));

            
// parse header //
            
$_header explode("\n"$_header);
            foreach(
$_header as $_h){
                
$_h trim($_h);
                if(!
$_h) continue;
                
$_h explode(":"$_h);
                if(!
$_h[0] || !$_h[1] || count($_h) < 2) continue;
                
// $article->name = val
                
$_h_key lcfirst(trim($_h[0]));
                
$_h array_splice($_h1);
                
$_h_val trim(implode(":"$_h));
                
$parsed_data->$_h_key $_h_val;
            }
            if(!
$parsed_data->title) continue;

            
// allocate permalink //
            
$parsed_data->url strtolower(@preg_replace($this->url_filter'-'$parsed_data->title));
            
$parsed_data->content $_content;

            
// check secret //
            
if(__REAL_SECRET__ == 1){
                if(
$parsed_data->secret == 'true' || $parsed_data->secret == 1) continue;
            }

            
$this->articles[] = $parsed_data;
        }
        return 
true;
    }

    private function 
compare_articles($a$b){
        
/* algorithm for sort */
        
$sort_by $this->sort_by;
        switch(
$sort_by){
            case 
"date":
                
$a = new DateTime($a->$sort_by);
                
$a $a->getTimestamp();
                
$b = new DateTime($b->$sort_by);
                
$b $b->getTimestamp();
                if(
$a == $b) return 0;
                return (
$a $b) ? : - 1;
            default:
                if(
$a->$sort_by == $b->$sort_by) return 0;
                return 
strnatcasecmp($a->$sort_by$b->$sort_by);
        }
    }

    public function 
sort_articles(string $sort_by){
        
/* sort articles based on correct types given */
        
$valid_types = ['title''date''tag''category'];
        if(!
in_array($sort_by$valid_types)) return false;
        
$this->sort_by $sort_by;
        
uasort($this->articles, [$this'compare_articles']);
        
$this->articles array_reverse($this->articles);
        return 
$this->articles;
    }

    public function 
read_article(int $yearint $monthstring $title){
        
/* read articles based on types given */
        
$year = (string)$year;
        
$month = (string)$month;

        
// get end of month //
        
$end_of_month = new DateTime("$year-$month-23");
        
$end_of_month = new DateTime($end_of_month->format('Y-m-t 23:59:59'));
        
$end_of_month $end_of_month->getTimestamp();
        
$start_of_month = new DateTime("$year-$month-01 00:00:00");

        foreach(
$this->articles as $article){
            
$_date = new DateTime($article->date);
            
$_date $_date->getTimestamp();
            if(
$_date >= $start_of_month && $_date <= $end_of_month){
                if(
strtolower((string)$article->url) === (string)$title){
                    
$parser = new \cebe\markdown\GithubMarkdown();
                    
$parser->html5 true;
                    
$parser->keepListStartNumber true;
                    if(
$article->secret == 'true' || $article->secret == '1'){
                        if(
__REAL_SECRET__ == 2){
                            
$article->content 'This post is currently in a secret mode. Please come back later to see this page.';
                        }
                    }
                    
$article->content $parser->parse($article->content);
                    return 
$article;
                }
            }
        }
        return 
false;
    }

    public function 
__construct(){
        
// load articles on construct //
        
$this->load_articles();
    }
}

// fyi, it's not vulnerable :p //
if(@$_REQUEST['show_source'] && $_SERVER['PHP_SELF'] === "/blog.php"){
    @
show_source(__FILE__);
    exit;
}

// for designing.. //
if($_SERVER['PHP_SELF'] === "/index.php"){
    
$blog_title "blog.harold.kim &raquo; ";

    
$articles = new ArticleHandler();
    
$article_list $articles->sort_articles('date');

    if(isset(
$_GET['year']) && isset($_GET['month']) && isset($_GET['title'])){
        
$year = (int)$_GET['year'];
        
$month = (int)$_GET['month'];
        
$title = (string)$_GET['title'];
        
$article = @$articles->read_article($year$month$title);
        if(
$article->title$blog_title .= htmlspecialchars($article->title);
    }else{
        
$blog_title .= 'article list';
    }
}

?>