<?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,
                           'catalogue'=>$xmlfile->pageinfo->page->path,
                           'html_content' => $html_content
                          ];

                return $values;

            }
        }
    
        else {
            $values = ['title' => '',
                       'description' => '',
                       'filename' => '',
                       'catalogue'=>'',
                       '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'];

    if (isset($_GET['showpub']))
        $g='?showpub';
    elseif(isset($_GET['showunp']))
        $g='?showunp';
    else
        $g='';
        
        
        
    # 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} 
</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?showpub=1<?php print $g ?>'" 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>
                <div class="postinfo">Filename <a class="qmark" href="javascript:;">?<span class="details">If changed, renames the page.</span></a></div>
                <textarea type="text" name="filename" id="filename" form="mainform"><?php print $pageval['filename'] ?></textarea>
                <div class="clear"></div>
                <div class="postinfo">Path <a class="qmark" href="javascript:;">?<span class="details">Leave empty to save in home directory.</span></a></div>
                <textarea type="text" name="catalogue" id="catalogue" form="mainform"><?php print $pageval['catalogue'] ?></textarea>
            </div>
        </div>
    </div>
    <script src="javascript/createpage.js"></script>
    <?php
    
    if ($texteditor == 'on')

        print '<script src="javascript/text-editor.js"></script>';    



    # plugins
    include 'execute.php'



    ?>
</body>
</html>