<?php
    

    # Saved Pages
    


    # login
    include_once 'data_connect.php';
    include_once 'log.php';
    


    # user rights - admins only
    if (get_account($_SESSION['userID'])['rights'] != 'administrator') {
        header('Location:opensaved.php');
        exit();
    }


    # Delete a given page
    function delete_page() {
        
        if ( isset($_GET['page']) && isset($_GET['savedpage']) ) {
            
            $pagexml = 'savedpages/'.$_GET['savedpage'];
            $page = '../'.$_GET['page'].'.php';

            if (file_exists($pagexml)) {
                unlink($pagexml);
                $_SESSION['MONpagedeleted'] = true;
                $sum = get_log()['pages'];
                $sum -= 1;
                update_log('pages',$sum);

            }
            
            if (file_exists($page))
                unlink($page);
        }
        return true;
    }
    
    delete_page();



    #extensions
    include 'command.php'



?>
<!doctype html>
<html>
<head>
<title>Pages</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="cmstyle.css">
<?php include 'csstheme.php' ?>
<script type="text/javascript">function conf(form){return confirm('Delete page?');}</script>
</head>
<body>
    <style>
        #allpages{<?php include 'cmcurrentpage.css' ?>}
    </style>
    <?php

        
        
    # panel
    include 'mypanel.php';
    action_confirm()

        
    
    ?>
    <div class="main">
        <h1>Pages <a class="newpostbut" href="createpage.php" id="newpage" title="Create a page"><span class="addnew">+</span>&nbsp;Publish a new page</a></h1>
        <div class="headpages" style="height:37px">
            <div class="results-set results-set-pages">
                <div class="normal-res">
                    <span style="font-size:12px">Results per page &nbsp;</span>

                    <?php

                    //Sort results
                    //GET parameter for res-set.php
                    $gt = array('sortbytitle','sortbyalpha','sortbyrev');
                    foreach($gt as $set) {
                        if (isset($_GET[$set])) {
                            $snes = '?sort='.$set.'&amp;sortval='.$_GET[$set].'&amp;';
                            break;
                        }
                        else
                            $snes = '?';
                    }
            

                    pageResults('pages',$snes)
                    
    
                ?>
                </div>    
            </div>
        </div>

        <div id="main-container">
            <div class="resultspages">
                <div class="pbar">
                    <span style="width:61%;padding-left:17px;text-align:left;float:left"><a href="pages.php?sortby<?php
                    
                    if (isset($_GET['sortbytitle'])) 
                        print 'alpha=1" class="sort1';
                    
                    elseif (isset($_GET['sortbyalpha']))
                        print 'title=1" class="sort2';
                        
                    else print 'title=1'
                    
                    
                                ?>
">Title</a></span>
                    <span style="width:90px;margin-right:10px"><a href="pages.php<?php 
                                
                        if (isset($_GET['sortbyrev'])) 
                            print '" class="sort2' ;
                        
                        elseif  ( !isset($_GET['sortbytitle']) && !isset($_GET['sortbyalpha']) )
                            print '?sortbyrev=1" class="sort1'
                            
                        
                                ?>
">Last modified</a></span>
                </div>
                <?php

                    $array_xml = array();
                    $array_param = array();
                    
                    $directory = new DirectoryIterator('savedpages/');
                    
                    $num = 1;
                    foreach ($directory as $fileinfo) {
                
                        if ($fileinfo->isFile() && (substr($fileinfo->getBasename(),-4) == '.xml')) {
                                            
                            $name = $fileinfo->getBasename();
                            $sp = simplexml_object($name,'l','page');
                            $mtime = $sp->pageinfo->page->modified;
                            $array_xml[]= $name;
                            $array_param[] = (isset($_GET['sortbytitle']) || isset($_GET['sortbyalpha'])) ? strtolower($sp->pageinfo->page->title).$num : $mtime;
                        }
                        $num++;
                    }
                    
                    $array_res = array_combine($array_param,$array_xml);
                    
                    
                    if ( isset($_GET['sortbyrev']) || isset($_GET['sortbytitle']) )
                        #normal order
                        ksort($array_res);
                            
                            
                    else

                    #reverse order
                    krsort($array_res);
                            
                    $cwd = 'pages';
                    include 'navinfo.php';
                
                    $c=0;
                    $r=0;
                    
                
                    include 'en_code.php';
                    include 'dateform.php';
                
                    foreach($array_res as $file) {
                        
                        if (filesize('savedpages/'.$file) > 100) {

                            if (($c >= $first) && ($c < $last)) {
                
                                $r++;
                
                        $fxml = simplexml_object($file,'l','page');
                        $title = $fxml->pageinfo->page->title;
                        $filename = $fxml->pageinfo->page->filename;
                        $utime = (int)$fxml->pageinfo->page->modified;
                        $ftime = date_form($utime);

                        if ($title == '') 
                            $title = '(no title)';
                        
                        #$cssbg = (($c % 2) == 1) ? ' -bg' : '';

                            echo '<div class="savedpage">
                            <span class="page-title"><a href="createpage.php?savedname='.$file.'">'.load_xml($title).'</a></span>
                            <div class="page-options"><a href="createpage.php?savedname='.$file.'" class="options-button" title="Edit page">Edit Page</a>
                            <a href="javascript:" onclick="if (conf(this)) window.location.href=\'pages.php?page='.$filename.'&amp;savedpage='.$file.'&amp;max='.$ofp.'\'" class="options-button not" title="Delete page">Delete</a>
                            <a href="../'.$filename.'.php" class="options-button" title="Visit page" target="_blank">Visit</a>
                            </div>
                            <div class="lastmod" title="'.date("H:i",$utime).'">'.$ftime.'</div>
                            </div>';
                                        }
                                    }
                        
                            $c++;
                        }
                    
                    if ($c==0)
                        print '<i id="no-results">:: no pages found</i>';
                    
                    else {

                        print '<div id="res-nav">';
                        include 'pagenav.php';
                        print '</div>';
                        
                    }
                ?>

            </div>
        </div>
    </div>
    <?php
    
    
    
    # plugins
    include 'execute.php'        
    

    
    ?>
</body>
</html>