<?php

    
    # General settings
    


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


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


    # submit form data
    function submit_settings_values() {


        $save = simplexml_object('settings.xml','e',null);
        $_POST['cphomepage'] = $cphome = basename($_POST['cphomepage'],'.php').'.php';
        $_POST['animations'] = isset($_POST['animations']) ? 'on' : 'off';
        $_POST['listall'] = isset($_POST['listall']) ? 'on' : 'off';
        
        
        
        if (!is_file($cphome))
            $error = 'ERROR:cphome';    
        
                
        #settings values
        $on_off_values = ['revorder'];
        $select_values = ['sort'];
        
        
        foreach($on_off_values as $val) {
            
            if (isset($_POST[$val]))
                $v = 'on';
            else 
                $v = 'off';
            
            $save->general->$val = $v;    
        }
        
        
        foreach($select_values as $val)
            if (isset($_POST[$val]))
                $save->general->$val = $_POST[$val];
                
        
        
        if (isset($_POST['total'])) {
        
                $c = get_log()['posts'];
                $intt = $_POST['total'];
                
                if ($intt >= 0 && ctype_digit($intt))
                    $save->general->total = $intt;
                else
                    $error = 'ERROR:total';

        }
        
        
        
        if (!isset($error)) {
            
            $save->general->modified = time();
            
            #save general settings and user account            
            simplexml_object_save('settings.xml',$save,null);
            update_account($_SESSION['userID'],'set');
            
            
            $_SESSION['settingsset'] = true;
        }

        else
            $_SESSION[$error] = true;

        
        header('Location:advanced.php');
        exit();
        
        return true;
        
    }



    if (isset($_POST['submitted']))
        submit_settings_values();





    #extensions
    include 'command.php'


?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Advanced settings</title>
<link rel="stylesheet" type="text/css" href="cmstyle.css">
<?php include 'csstheme.php' ?>
<link rel="stylesheet" type="text/css" href="cmsavebutton.css">
</head>
<body>
<?php



    # panel
    include 'mypanel.php';
    

    
    if($xmls['revorder'] == 'on')
        $revorder = 'checked';
    else
        $revorder = '';
    
    
    # select-options
    $sortby = $xmls['sort'];
    
    if (isset($xmls['total']) && $xmls['total'] != '')
        $totalres = $xmls['total'];
    else
        $totalres = 0;
    

    # user accout info
    $acc_info = get_account($_SESSION['userID']);
    
    
    # control panel home page
    $cphomepage = $acc_info['cphomepage'] == '' ? 'opensaved.php' : $acc_info['cphomepage'];
    
    # control panel animations
    $anim = isset($acc_info['animations']) && $acc_info['animations'] == 'off' ? '' : 'checked';
    
    # List all files in uploads.php
    $listall = isset($acc_info['listall']) && $acc_info['listall'] == 'on' ? 'checked' : '';
     
    

?>
    <div class="main">
        <h1 class="set">Advanced settings</h1>
        <form action="advanced.php" method="post" id="MONform">
        <div class="center-container">
        <?php
        
        action_confirm();

        if(isset($_SESSION['usericon']))
            unset($_SESSION['usericon']);
        
        ?>
            <div id="settingspage">
                
                
            <!-- Advanced settings -->
            
                
                <!-- Sort results --> 
                <div class="sel">
                    <label for="rad-0" class="lab-5" title="Sort results by">Sort results by</label><br>
                    <span class="des">General sorting of posts.</span>
                </div>
                
                
                <input name="sort" type="radio" id="rad-0" class="monradio" value="time" title=""<?php if ($sortby == 'time' || $sortby == '') print ' checked' ?>>
                <label for="rad-0" title="Chronological sorting">Time</label> &nbsp;&nbsp;

                <input name="sort" type="radio" id="rad-1" class="monradio" value="alpha" title=""<?php if ($sortby == 'alpha') print ' checked' ?>>
                <label for="rad-1" title="Alphabetical sorting, based on the post title">Alpha</label> &nbsp;&nbsp;
                
                
                <div class="clear-s"></div>
                
                
                <!-- Reverse results -->
                <div class="sel">
                    <label for="onoff-5" class="lab-5" title="Reverse results">Reverse results</label><br>
                    <span class="des">Reverse display order of posts.</span>
                </div>
                <input name="revorder" type="checkbox" id="onoff-5" class="moncheck" value="on" title="" <?php print $revorder ?>>
                <label for="onoff-5" class="lab-5" title="Reverse results"></label>
                <div class="clear-l"></div>
                
                
                <!-- Total number of posts --> 
                <div class="sel">
                    <label for="rad-2" class="lab-5" title="Total number of posts">Limit results</label><br>
                    <span class="des">Set a limit to the number of posts to be shown in home page (0 = no limit).</span>
                </div>
                <input name="total" type="text" id="rad-2" class="inp-4" value="<?php print $totalres ?>" title="Limit the total number of posts">
                <div class="clear"></div>
                <div class="newoptions"></div>
                
                    
                <!-- Control panel home page --> 
                <div class="sel">
                    <label for="homepage" class="lab-5" title="Control panel home page">Control panel home page</label><br>
                    <span class="des">A page to redirect to, after you login. The file must be in monofiles folder.</span>
                </div>
                <input type="text" id="homepage" name="cphomepage" class="inp-76" title="Filename of a page to redirect to" value="<?php print $cphomepage ?>">
                <div class="clear"></div>    
                
                
                <!-- Remove animations --> 
                <div class="sel">
                    <label for="anim" class="lab-5" title="Animations">Animations</label><br>
                    <span class="des">Turn animations on/off for the control panel.</span>
                </div>
                <input type="checkbox" id="anim" name="animations" class="moncheck" value="on" title="Turn animations on/off" <?php print $anim ?>>
                <label for="anim" class="lab-5" title="Animations"></label>
                <div class="clear"></div>
                
                    
                <!-- List files in uploads.php --> 
                <div class="sel">
                    <label for="uploads" class="lab-5" title="List all files in uploads">Show all uploaded files</label><br>
                    <span class="des">Show all the file types in "Images", or turn off to restrict to those specified by the xml file.</span>
                </div>
                <input type="checkbox" id="uploads" name="listall" class="moncheck" title="List all uploaded files" value="on" <?php print $listall ?>>
                <label for="uploads" class="lab-5" title="List all files in uploads"></label>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        </div>
        </form>
    </div>
    <?php 



    # plugins
    include 'execute.php'



    ?>
</body>
</html>