<?php


        # Check number of posts and pages
        # Retrieve accidentally deleted pages from the home dir
        # Update log.xml
        
        include 'data_connect.php';
        include 'log.php';
        
        
        echo 'Checking system..';
        
    
        $logxml = simplexml_object('log.xml','e',null);


        $pages = scandir('savedpages');


        $pgsum=0;
        $pgr=0;

        echo '.';


        foreach($pages as $page) {

            if (is_file('savedpages/'.$page)) {

                if (substr($page,-4) == '.xml') {
                    
                    $pgsum++;
                    $xmlname = basename($page,'.xml');
                    $xmlfile = simplexml_load_file('savedpages/'.$page);

                    if (!is_file('../'.$xmlfile->pageinfo->page->filename.'.php')) {

                        file_put_contents('../'.(string)$xmlfile->pageinfo->page->filename.'.php','<?php 
                        $pn = "'.$xmlname.'"; if(!file_exists("pagep.php")) exit("Error loading content. Please come back later."); include "pagep.php"    ?>');
                        $pgr++;


                    }
                }
            }
        }


        $logxml->info->pages = $pgsum;
        echo '.';

        // posts 
        $posts = scandir('autosaves');


        $psum = 0;
        foreach ($posts as $post)
            if (is_file('autosaves/'.$post))
                if (substr($post,-4) == '.xml' && substr($post,-6) != 'nu.xml')
                    $psum++;


        $logxml->info->posts = $psum;

        echo '.';
        

    
        $logxml->info->modified = time();
        simplexml_object_save('log.xml',$logxml,null);


        echo '<p>System check completed</p><br>
                <p>
                    <ul>
                        <li>Total number of posts: '.$psum.'</li>
                        <li>Total number of pages: '.$pgsum.'</li>
                        <li>Pages retrieved: '.$pgr.'</li>
                    </ul>
                </p>'
        
?>