<?php

    # Print the main website content feed
    
    
    #$c values: 0->home page 1->category index 2->tag index
    if ($c < 3) {

        
        #$sort='time';
        #$reverse='on';
        #$limit=10;
        #$stag='';
        #$scat='';
        #$sign='';
        #$date='';
        #$label options: category, tag, author, read more
        $label=['','','','read more &rsaquo;'];
        
        
        
        if (count($xmlfiles) > 0 && (($n > 0 && $c > 0) || !$nn)) {
    
        
        
            $heading = $outside[$c];
            if ($c!=0)
                echo '<h1>'.$$heading.'</h1>';
                
                
            # Prepare posts
            # Figure out limit
            if (!isset($limit)) {
                $limit = $cms['total'];
                if ($cms['total'] == '')
                    $limit = 0;

            }
            
            # Sort results
            if ($cms['sort'] == null || $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);
            }
                
            
            # Reverse
            if ($cms['reverse_order'] == 'on')
                $index_posts = array_reverse($index_posts);

            
            
            # Excerpt global value
            $exc = $cms['exc'];
            $exclim = $cms['exclimit'];



            # start print
            $rc=0;
            $r=0;
            
            foreach($index_posts as $postpos) {
            
                if ($rc >= $first && $rc < $last) {

                
                    $r++;
                    
                    
                    # post info
                    
                    //$postfile = $rot.'monofiles/autosaves/'.basename($file);
                    $xmlpost = $array_index[$postpos];
                    $file = $xmlpost['file'];
                    
                    
                    $cid = $xmlpost['catid'];
                    if (isset($mc[$cid]))
                        $cid = $mc[$cid];


                    
                ##### changing the value of $cid #####
                    if (!isset($gc[$cid]))
                        $cid = 'uncategorized';

                    # Not public or set in future date
                    #if ($xmlpost['ptime'] > time()) {
                    #    $r--;
                    #    continue;
                    #}
                        
                    
                    # Path and filename
                    $path = $xmlpost['path'];
                    $link = $xmlpost['filename'];

                    
                    
                    # Post title
                    if ($cms['ptitle'] == 'on') {
                        $ptitle = isset($gc[$cid]) && $gc[$cid][5] == 'on' ? '<a href="'.$rot.$path.$link.'">'.load_xml($xmlpost['title']).'</a>' : load_xml($xmlpost['title']);
                        $ptitle = '<h2>'.$ptitle.'</h2>';
                    }
                    
                    
                    
                    
                    # Excerpt
                    # Post content and excerpt limit, as set inside a post or set in settings as a global value
                    $htm_file = $rot.'monofiles/autosaves/content/'.basename($file,'.xml').'.htm';
                    $content = is_file($htm_file) ? file_get_contents($htm_file) : ' ';
                    
                    # NOTE: when excerpt is indicated inside the post itself, an <hr> element is inserted
                    $xmlexc = $xmlpost['excerpt'];
                    
                                        
                    
                    # Excerpt global value
                    # $exc -> $cms['exc'];
                    # $exclim -> $cms['exclimit'];
                    
                    # $exclim applies the same excerpt value for all posts
                    if ($exc == 'on') {
                    
                        if ($exclim == '')
                            $excerpt = $xmlexc == 'none' ? $content : cut_content($content,(int)$xmlexc,'null');                                
                        
                        elseif ($exclim == 0) {
                            
                            if ($xmlexc == 'none')
                                $excerpt = $content;
                            else
                                $excerpt = str_replace('<hr id="monreadmoreHr">','',$content); 
                        }
                        
                        elseif ($exclim > 0)    
                            $excerpt = cut_content($content,$exclim,'null');
                        
                    }
    
                            
                        
                                
                    # Find tags
                    if ($cms['show_tags'] == 'on') {
                        if ($xmlpost['tags'] != '') {
                            $alltags = explode(',',$xmlpost['tags']);
                            foreach($alltags as $val) {
                                #change from tagname to rot
                                $tagrot = $rot.'tags/'.str_replace(' ','-',mb_strtolower($val));
                                
                                $newtg = $cms['tagstext'] != 'on' ? '<a href="'.$tagrot.'" class="tagword">'.$val.'</a> ' : '<span class="tagword">'.$val.'</span> ';
                                $tgs = isset($tgs) ? $tgs.$newtg : $newtg;
                                
                            }
                        }
                    }
                
                
                

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

                        foreach ($logfile->user as $userinfo) {
        
                            if ((string)$userinfo->userID == $created) {
                                $author = $userinfo->editorname;
                            
                                if ($edited != '' && (string)$edited != (string)$created) {
                                
                                    foreach ($logfile->user as $userdata) {
                                    
                                        if ((string)$userdata->userID == $edited) {
                                            $author .= ' ,&nbsp;'.$userdata->editorname;
                                            break;
                                        }
                                    }
                                }
                            
                                break;
                            }
                        }
                        
                        $author = '<div class="post-author">'.$label[2].$author.'&nbsp;&nbsp;&nbsp;</div>';
                
                    }
            
                
                    
                
                    # Date
                    $fpdate = (int)$xmlpost['ptime'];
                    if ($cms['show_date'] != 'off' && $cms['show_date'] != 'none(turn off)')
                        $post_date = '<span class="post-date">'.date($cms['show_date'],$fpdate).'</span>';
                    else
                        $post_date = '';
                
                
                
                    # category prepare
                    # $gts [id=>[0=>folder,1=>name,2=>category index,3=>post links]] . id can also belong to a merged cat
                    # $gc [id=>[0=>folder,1=>name,2=>publish,4=>category index,5=>post links]].
                    
                    $category_name = $gc[$cid][1];
                    if ($gc[$cid][4] == 'on')
                        $category_name = '<a href="'.$rot.$gc[$cid][0].'">'.$category_name.'</a>';
                    
                    
                    
                
                    ##### Start the post
                    if ($rc == 0 || ($cms['unidates'] == 'off' || $keepdate != $post_date))
                        echo $post_date.PHP_EOL;
                    
                    
                    if (isset($tgs) && !isset($tagg))
                        echo '<div class="post-tags">'.$label[1].$tgs.'</div>';
                    
                    
                    
                    echo '<div class="post">';
                    
                    
                    if (isset($ptitle))
                        echo $ptitle;
                    
                    if (isset($excerpt)) {
                        echo PHP_EOL.' <div class="content-text">'.$excerpt.'</div>';
                        
                        if ($cms['togreadmore'] != 'off' && $gc[$cid][5] != 'off' && $excerpt != $content)
                            echo '<a class="monmore" href="'.$rot.$path.$link.'">'.$label[3].'</a>';
                    }
                    
                    print '<div class="postinfo">'.PHP_EOL.$author.PHP_EOL;
                    
                    
                    if ($cms['show_category'] == 'on')
                        echo '<div class="post-category">'.$label[0].$category_name.'</div>';
                      
                    
                    print '</div>'.PHP_EOL.'</div>';
                    ##### closing post
                            
            
                    
                    
                    # unset tags
                    unset($tgs);
                
                
                }

                $keepdate = $keepdate = isset($post_date) ? $post_date : '';
                
                $rc++;
                
                if ($limit != 0 && $limit == $rc)
                    break;
            }            
        
            include $rot.'monofiles/paginate.php';
        
        }
    }
    
    
    
    
    
?>