<?php
    
    
    # Prepares blog posts for home page
    # Must be included in index.php.
    # Note: $catg & $tagg are set in ..path/index.php
    


    if (strpos('set-posts',basename(get_included_files()[0],'.php')) !== false) 
        exit();
    

    
    $files_to_load = $cms['results_per_page'];
    
    $xmlfiles = glob($rot.'monofiles/autosaves/*.xml');
    $cxml = count($xmlfiles);
    

    # Set a limit    
    $x = $cms['total'] != '' ? (int)$cms['total'] : 0;


    # Posts categories -> array(folder=>name)
    $cats = get_categories();

    

    # Find posts
    if ($cxml > 0) {
    
        
        $tags_label = 'Tags: ';
        $category_label = 'Category: ';
        $sign_label = 'Posted by ';
        
        
        $array_xml = array();
        $array_ptime = array();
        $toppost = array();
        
        $c=1;
        
        # if a tag or category is set, get only tagged or categorized posts 
        foreach ($xmlfiles as $file) {
            
            
            $file = basename($file);
            $post = getxmlpost($rot.'monofiles/autosaves/'.$file);
            $nu = substr(basename($file,'.xml'),-2,2);
            
            
            
            $alltags = array_values(preg_split('/[,]/',$post['tags'],PREG_SPLIT_NO_EMPTY));
            
            
            
            if ( $post['type'] != 'Draft' && $nu != 'nu' ) {
                
                if ((isset($catg) && $post['category'] !== false && $catg == $post['category']) or (isset($tagg) && $post['tags'] !== false && array_search($tagg,$alltags,true) !== false) or (!isset($catg) && !isset($tagg))) {

                    $array_xml[] = $file;
                    $array_ptime[] = (int)$post['ptime'];
                    $array_alpha[] = strtolower($post['title']).$c;
                    
                    if (isset($post['top']) && $post['top'] != '')
                        $toppost[$post['top']] = $file;
                    
                    $c++;
                }
            }    
        }
        
        
        
        
        
        # category or tag index page
        if (isset($catg)) 
            echo '<h2>'.$catg.'</h2>';
        else 
            if (isset($tagg))
                echo '<h2>'.$tagg.'</h2>';
        
        
        
        
        
        # sort results options
        if (!empty($cms['sort']) && !empty($cms['reverse_order'])) {
        
            if ($cms['sort'] == 'time') {
                $index_posts = array_combine($array_ptime,$array_xml);
                krsort($index_posts);
            }
            
            else {
                $index_posts = array_combine($array_alpha,$array_xml);
                ksort($index_posts);
            }
            
            if ($cms['reverse_order'] == 'on')
                $index_posts = array_reverse($index_posts);
        }
        
        
        
        else {
            $index_posts = array_combine($array_ptime,$array_xml);
            krsort($index_posts);
        }




        
        
        
        # Post on top
        if (count($toppost) > 0) {
        
            ksort($toppost);
            foreach($toppost as $key=>$val) {
                
                $indexkey = array_search($val,$index_posts);
                if ($indexkey !== false) {
                
                    unset($index_posts[$indexkey]);
                    array_unshift($index_posts,$val);
                
                }
            }
        }
        
        
        
        
        # print
        # $x is the limit of the blog posts to print in home page, set by the user. $x=0 means 'show all'.  
        $catnum = $x == 0 ? count($index_posts) : $x;
        include $rot.'monofiles/navinfo.php';
        
        $c=0;
        $r=0;
        
        foreach($index_posts as $file) {
            
            if ($files_to_load > 0) {

                if ($c >= $first && $c < $last) {


                    $r++;
                    
                    
                    # load info
                    $postfile = $rot.'monofiles/autosaves/'.basename($file);
                    $xmlpost = getxmlpost($postfile);

                    if ($xmlpost['ptime'] > time()) 
                        continue;
                        
                    $title = load_xml($xmlpost['title']);
                    $path = $xmlpost['path'];
                    $link = $xmlpost['filename'];
                    $htm_file = $rot.'monofiles/autosaves/content/'.basename($file,'.xml').'.htm';
                    
                    $content = is_file($htm_file) ? file_get_contents($htm_file) : ' ';
                
                    
                    $excerpt = $xmlpost['excerpt'];
                    # when we insert an <hr> element to indicate the excerpt limit,
                    # we also format the selection as a div block. The <hr> "cuts" this block in half
                    # and so we close it right here.
                    $excerpt = ($excerpt == 'none') ? $content : substr($content,0,(int)$excerpt).'&#8230;</div>';
                    
                    
                    
                                    
                    # Find tags
                    if ($xmlpost['tags'] != '') {
                        
                        $alltags = array_values(preg_split('/[,]/',$xmlpost['tags'],null,PREG_SPLIT_NO_EMPTY));

                        foreach($alltags as $val) {

                            #change from tagname to root
                            $tagrot = $rot.'tags/'.str_replace(' ','-',mb_strtolower($val));
                            $newtg = '<a href="'.$tagrot.'" class="tagword">'.$val.'</a> ';

                            if (isset($tgs))
                                $tgs .= $newtg;
                            else                        
                                $tgs = $newtg;

                        }
                    }
                    
                    
                    

                    # the author
                    if ($cms['sign_posts'] == 'on') :
                    
                    $the_author = '';
                    $created = $xmlpost['createdby'];
                    $edited = $xmlpost['editedby'];

                    $logfile = simplexml_object('log.xml','l',null);
                    foreach ($logfile->user as $userinfo) {

                        if ((string)$userinfo->userID == $created) {
                            $the_author = $userinfo->editorname;
                            
                            if ($edited != '' && (string)$edited != (string)$created) {
                                
                                foreach ($logfile->user as $userdata) {
                                    
                                    if ((string)$userdata->userID == $edited) {
                                        $the_author .= ' ,&nbsp;'.$userdata->editorname;
                                        break;
                                    }
                                }
                            }
                            
                            break;
                        }
                    }
                    
                    endif;
                    
                    
                    
                    # the post category
                    if ($cms['show_category'] == 'on') {
                    
                    
                    
                        $post_category  = '';
                        
                        if (isset($catg))
                            $catg_cat = '';
                        
                        
                        else {
                            $postcatname = load_xml($xmlpost['category']);
                            if (is_array($cats) && in_array($postcatname,$cats))
                                $post_category = $category_label.'<a class="ch-tag" href="'.$rot.'category/'.array_search($postcatname,$cats).'/">'.$postcatname.'</a>';
                        }
                                    
                    
                    }
            
                    
                    
                    
                    # date
                    if ($xmlpost['ptime'] !== false) {
                        
                        $form = (string)$cms['show_date'];
                        $fpdate = (int)$xmlpost['ptime'];
                        
                        if ($form != 'none(turn off)')
                            $post_date = date($form,$fpdate);
                        
                        
                    }        
                    
                    
                    
                    
                    # Start the post here
                    if (isset($post_date))
                        if ($c == 0 || $keepdate != $post_date)
                            echo '<span class="post-date">'.$post_date.'</span>'.PHP_EOL;
                    
                    
                    echo '<div class="post"><h2><a href="'.$rot.$path.$link.'">'.$title.'</a></h2>'.PHP_EOL.' <div class="content-text">'.$excerpt.'</div>';
                    
                    if ($excerpt != $content)
                        echo '<div><a class="monmore" href="'.$rot.$path.$link.'">Read post</a></div>';
                    
                    
                    print '<div class="postinfo">'.PHP_EOL;
                        
                                    
                    if ($cms['sign_posts'] == 'on')
                        echo '<div class="post-author">'.$sign_label.$the_author.'&nbsp;&nbsp;&nbsp;</div>';
                    
                    if ($cms['show_category'] == 'on')
                        echo '<div class="post-category">'.$post_category.'&nbsp;&nbsp;&nbsp;</div>';
                      
                    if ($cms['show_tags'] == 'on' && isset($tgs))
                        echo '<div class="post-tags">'.$tags_label.$tgs.'</div>';
                    
                    
                    print '</div></div>';

                    # closing post here 

                    
                    # unset tags when inside a loop
                    unset($tgs);    
                    
                    
                }
            }
            
            
            $keepdate = isset($post_date) ? $post_date : '';
            
            $c++;
            
            if ($x != 0 && $x == $c)
                break;
            
        }
    }


?>