<?php 
    

    # Create or edit a page



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


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

    
    function loadpage() {
        
        if (isset($_GET['savedname'])) {
            
            $savedname = $_GET['savedname'];
            $xmlfile = simplexml_object($savedname,'l','page');

            if ($xmlfile == ['falsexml'])
                return 'File has errors';
            elseif ($xmlfile == ['missingfile'])
                return 'File is missing';
            else {
                $filebase = basename($savedname,".xml");
                $htmfile = 'savedpages/'.$filebase.'.htm';
                $html_content = (is_file($htmfile)) ? file_get_contents($htmfile) : '';
                $values = ['title' => $xmlfile->pageinfo->page->title,
                           'description' => $xmlfile->pageinfo->page->description,
                           'filename' => $xmlfile->pageinfo->page->filename,
                           'filebase' => $filebase,
                           'html_content' => $html_content
                          ];

                return $values;

            }
        }
    
        else {
            $values = ['title' => '',
                       'description' => '',
                       'filename' => '',
                       'html_content' => '',
                       'filebase' => $_SESSION['userID'].time()
                      ];
            
            return $values;
        }
    }
    
    $pageval = loadpage();

    if (!is_array($pageval)) {
        $_SESSION['ERROR'] = $pageval;
        header('Location:pages.php');
        exit();
    }

    $texteditor = get_account($_SESSION['userID'])['texteditor'];


    
    # extensions
    include 'command.php'



?>
<!doctype html>
<html>
<head>
<title>Page</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="cmstyle.css">
<?php include 'csstheme.php' ?>
</head>
<body>
<style>
    .main{left:0;position:relative;width:100%;margin-right:0;max-width:88%;float:right;margin-top:80px}
    #newpage{<?php include 'cmcurrentpage.css' ?>}
</style>
    <?php


    # panel
    include 'mypanel.php'



    ?>
    <div class="main">
        <form action="pagepost.php" method="post" id="mainform" onkeydown="return event.key != 'Enter';"></form>
        <div class="clear"></div>
        <?php
            if ($texteditor == 'on') include 'text-editor-controls.php'
        ?>
        <div id="page" class="nocontent">
        <div id="action-block">
            <div id="result-block">
                <div id="resultt"></div>
            </div>
            <!-- Unique id hidden -->
            <input type="text" id="uuid" name="uniqueid" value="<?php print $pageval['filebase'] ?>" form="mainform" hidden>
            <div class="postmore">
                <span class="dot"></span>
                <span class="dot"></span>
                <span class="dot"></span>
                <div class="pmoptions">
                    <button class="close-but" onclick="closeEditor();location.href='pages.php'" type="button">Close</button>
                </div>
            </div>
            <input name="button" type="button" id="sd" class="stylish-button" value="Save page" onclick="sendTopagepost();">
        </div>
            <div class="newpageleft">
                <div id="editor-container">
                    <input type="text" name="title" class="grow" id="headline-page" form="mainform" placeholder="Title" value="<?php print $pageval['title'] ?>">
                    <?php if ($texteditor == 'on') : ?> 
                    <div id="editor" contenteditable></div>
                    <div id="output-container" class="hide">
                    <?php else : ?>
                        
                        <div id="output-container">
                        
                        
                        <?php endif ?>
                        <textarea name="content" id="output" autocomplete="off" form="mainform"><?php

                           if (isset($pageval['html_content']))
                               print $pageval['html_content']


                          ?>
</textarea>
                    </div>
                </div>
                
                <div class="clear"></div>
            </div>


            <!-- Meta & info -->
            <div class="newpageright">
                <div class="postinfo">Meta description</div>
                <textarea name="description" class="mini-editor" id="metades" form="mainform"><?php print $pageval['description'] ?></textarea>
                <div class="clear"></div>
                <label for="filename" style="clear:both">Filename</label>
                <textarea type="text" name="filename" id="filename" form="mainform"><?php print $pageval['filename'] ?></textarea>
            </div>
        </div>
    </div>
    <script src="javascript/createpage.js"></script>
    <script src="javascript/text-editor.js"></script>    
    <?php



    # plugins
    include 'execute.php'



    ?>
</body>
</html>