<?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');
    
    # Posts categories -> array(folder=>name)
    $cats = get_categories();



    # Find posts
    if (count($xmlfiles) != 0) {
    
        
        
        $tags_label = 'Tags: ';
        $category_label = 'Category: ';
        $sign_label = 'Posted by ';
        
        
        
        
        $array_xml = array();
        $array_ptime = array();
        
        
        # if a tag or category is set, get only tagged or categorized posts 
        foreach ($xmlfiles as $file) {
            
            
            $file = basename($file);
            $Post = new getXMLPost($rot.'monofiles/autosaves/'.$file);
            $nu = substr(basename($file,'.xml'),-2,2);
            
            
            
            $alltags = array_values(preg_split('/[,]/',$Post->tags,null,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;
                }
            }    
        }
        
        
        if (isset($catg))
            echo '<h2>'.$catg.'</h2>';
        
        else
            if (isset($tagg))
                echo '<h2>'.$tagg.'</h2>';
        
        
        
        
        # Sort and print
        
        $index_posts = array_combine($array_ptime,$array_xml);
        krsort($index_posts);
        
        include $rot.'monofiles/navinfo.php';
        
        $c=0;
        $r=0;
        
        foreach($index_posts as $file) {
            

            
            if (($c >= $first) && ($c < $last)) {
                
                
            
                $r++;
                
                
                # load info
                $postfile = $rot.'monofiles/autosaves/'.basename($file);
                $xmlpost = new getXMLPost($postfile);

                $title = load_xml($xmlpost->title);
                $path = $xmlpost->path;
                $link = $xmlpost->filename;
                $htm_file = $rot.'monofiles/autosaves/content/'.basename($file,'.xml').'.htm';
                
                $content = file_exists($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);
                    /*$folders = glob($rot.'category/*',GLOB_ONLYDIR);
                    
                    
                    foreach ($folders as $catf) {
                        
                        
                        $base = basename($catf);
                        if ($base == $catinfo) {
                            $category = $catinfo;
                            $cat_folder = $catf;*/
                        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>';
                            //break;
                        //}
                    //}
                }
                                
                
                endif;
                
                
                
                
                
                
                
                
                # 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++;
            
        }
    }


?>