<?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') {
                
                $xmlfile = simplexml_object($page,'l','page');
                if ($xmlfile == ['falsexml'])
                    continue;

                $pgsum++;
                $xmlname = basename($page,'.xml');
                
    
                if (!is_array($xmlfile))
                    if (!is_file('../'.$xmlfile->pageinfo->page->filename.'.php') && $xmlfile->pageinfo->page->published != 'hidden' && $xmlfile->pageinfo->page->published !== false )
                        if (empty($xmlfile->pageinfo->page->path)) {
                            file_put_contents('../'.(string)$xmlfile->pageinfo->page->filename.'.php','<?php $pn = "'.$xmlname.'"; 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>'
    
?>