<?php

    
    # General settings
    


    # login
    include_once 'data_connect.php';
    include_once '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);
        $website_title = strip_tags($_POST['title']);
        $website_description = htmlspecialchars(strip_tags($_POST['description']),ENT_COMPAT);
        $max = $_POST['maximagesize'] > 0 ? ltrim($_POST['maximagesize'],'0') : $_POST['maximagesize'];
        $indexposts = $_POST['indexposts'] > 0 ? ltrim($_POST['indexposts'],'0') : $_POST['indexposts'];
        $urlicon = $_POST['urlicon'];
        
        if (!ctype_digit($max) || $max < 0 || $max > 20000)
            $error = 'ERROR:maximagesize';
                
        if (!ctype_digit($indexposts) || $indexposts < 0 || $indexposts > 1000)
            $error = 'ERROR:indexposts';    
        
        
        
        
        
        # user related info
        if (strlen($_POST['usereditorname']) > 40)
            $error = 'ERROR:editorname';

        $userrelated = array('usereditorname','urlicon');
        foreach ($userrelated as $user) {
            
            if ($user == 'urlicon')
                $_POST[$user] = trim($_POST[$user]);
                
            $_POST[$user] = (isset($_POST[$user])) ? strip_tags($_POST[$user]) : ' ';

        }
        
        # settings values
        $on_off_values = ['togsign','togcategories','togtags','togreadold'];
        $textual_values = ['maximagesize'=>$max,'indexposts'=>$indexposts,'description'=>$website_description,'title'=>$website_title];
        $user_on_off = ['texteditor','themecustom'];

        
        # administrator settings
        foreach($on_off_values as $onoff) {
            $val = isset($_POST[$onoff]) ? 'on' : 'off';
            $save->general->$onoff = $val;
            
        }
        
        
        foreach($textual_values as $key=>$val)
            if ($val != '')
                if ($key == 'indexposts') 
                    $save->general->$key->results = $val;
                else
                    $save->general->$key = $val;

        
        # user on off settings
        foreach($user_on_off as $val)
            if (!isset($_POST[$val]))
                $_POST[$val] = 'off';


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

        else
            $_SESSION[$error] = true;

        
        header('Location:general.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>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';
    


    # Load settings
    # textual values
    $website_title = $xmls['title'];
    $website_description = $xmls['description'];
    $posts = $xmls['indexresults'];
    $maxsize = $xmls['maximagesize'];
    $date = $xmls['dateshow'];
    

    # onoff values
    $tog[] = $acc_info['texteditor'] == 'on' ? 'checked' : '';
    
    $togvalues = ['togsign','togcategories','togtags','togreadold'];
    $texteditor = $acc_info['texteditor'];
        foreach ($togvalues as $val)
            if($xmls[$val] == 'on')
                $tog[] = 'checked';
            else
                $tog[] = '';
    
    
    # select-options
    $sortby = $xmls['sort'];
    

    # user accout info
    $acc_info = get_account($_SESSION['userID']);
    $editorname = $acc_info['editorname'];
    $urlicon = $acc_info['urlicon'];
        
    
    # Date format
    # get dates from dates.xml
    # $php_dates supports older version. See phpdate_ in log.php
    $datesxml = simplexml_object('dates.xml','l',null);
    $php_dates = $datesxml->dateformats->dn == '' ? phpdate_($datesxml->dateformats->df) : phpdate($datesxml->dateformats->dn);

    
    array_push($php_dates,'none(turn off)','off');
    #$cssval = array_search((string)$date,$php_dates);
    

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

        if(isset($_SESSION['usericon']))
            unset($_SESSION['usericon']);
        
        ?>
            <div id="settingspage">

            
                    
                <!-- Website settings -->
                <!-- Title -->
                <div class="sel">
                    <label for="website-title" class="lab-5" title="Website title">Title</label><br>
                    <span class="des">Your home page title.</span>
                </div>
                    <input name="title" type="text" id="website-title" class="inp-2" title="Website title" value="<?php print $website_title ?>">
                <div class="clear-s"></div>




                <!-- Description -->
                <div class="sel">
                    <label for="website-description" class="lab-5" title="Website description">Description</label><br>
                    <span class="des">A description for your website.</span>
                </div>
                <textarea name="description" id="website-description" class="inp-2" rows="5" cols="40" style="resize:none" title="Website description"><?php  print $website_description ?></textarea>
                <div class="clear"></div>

                
                
                
                <!-- Date format -->
                <div class="sel">
                    <label for="dinp-1" class="lab-5" title="Choose a date format, included in 'filesinfo/dates.xml'">Date</label><br>
                    <span class="des">Choose a date format for your website, or completely remove from your posts. </span>
                </div>
                <div class="sel-r">
                <?php

                $off = array_pop($php_dates);
                $turnoff = array_pop($php_dates);

                $date_inp = '<input name="dateshow" class="monradio" type="radio" ';


                $dtitle = 'Choose a date format';
                $dtcheck = '';
                $cid = 1;
                foreach($php_dates as $dopt) {

                    if ($date == (string)$dopt) {
                        $dtitle = 'Current date';
                        $dtcheck = 'checked';
                        $cidc = $cid;
                        $cid = 'c';
                        
                    }        

                    print $date_inp.'id="dinp-'.$cid.'" value="'.$dopt.'" '.$dtcheck.'><label for="dinp-'.$cid.'" class="lab-5" title="'.$dtitle.'">'.date($dopt).'</label><br>'; 

                    if (isset($cidc)) {
                        $dtitle = 'Choose a date format';
                        $cid = $cidc;
                        unset($cidc);
                        $dtcheck = '';
                    }
                    
                    $cid++;

                }

                $offch = $date == $off || $date == $turnoff ? 'checked' : '';

                print $date_inp.'id="dinp-'.$cid.'" value="none(turn off)" '.$offch.'><label for="dinp-'.$cid. '" class="lab-5" title="Remove date">none(turn off)</label>'

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


    
                
                <!-- website results (Results in home page) -->
                <div class="sel">
                    <label for="results-in-homepage" class="lab-5" title="Results per page"> Results per page</label><br>
                    <span class="des">The number of posts to appear per page. Write '0' to completely remove from home page.</span>
                </div>
                <input name="indexposts" type="text" id="results-in-homepage" class="inp-4" title="Results per page" value="<?php print $posts ?>">
                <div class="clear"></div>
                


                <!-- Sign posts -->
                <div class="sel">
                    <label for="onoff-1" class="lab-5" title="Sign posts">Sign posts</label><br>
                    <span class="des">Sign a post with the editor's name.</span>
                </div>
                <input name="togsign" type="checkbox" id="onoff-1" class="moncheck" value="on" title="" <?php print $tog[1] ?>>
                <label for="onoff-1" class="lab-5" title="Sign posts"></label>
                <div class="clear-s"></div>


                
                <!-- Categories -->
                <div class="sel">
                    <label for="onoff-2" class="lab-5" title="Show categories">Show Categories</label><br>
                    <span class="des">Show the category of each post.</span>
                </div>
                <input name="togcategories" type="checkbox" id="onoff-2" class="moncheck" value="on" title="" <?php print $tog[2] ?>>
                <label for="onoff-2" class="lab-5" title="Show categories"></label>
                <div class="clear-s"></div>


                
                <!-- Tags -->
                <div class="sel">
                    <label for="onoff-3" class="lab-5" title="Show tags">Show Tags</label><br>
                    <span class="des">Show tags related to each post.</span>
                </div>
                <input name="togtags" type="checkbox" id="onoff-3" class="moncheck" value="on" title="" <?php print $tog[3] ?>>
                <label for="onoff-3" class="lab-5" title="Show tags"></label>
                <div class="clear"></div>
                
                
                
                <!-- Read old posts -->
                <div class="sel">
                    <label for="onoff-4" class="lab-5" title="Go to next page link">Go to next page link</label><br>
                    <span class="des">Appears on your home page, when results per page are less than the total number of posts.</span>
                </div>
                <input name="togreadold" type="checkbox" id="onoff-4" class="moncheck" value="on" title="" <?php print $tog[4] ?>>
                <label for="onoff-4" class="lab-5" title="Go to next page link"></label>
                <div class="clear"></div>
                <div class="newoptions"></div>
                <div class="clear"></div>
    



                <!-- Control panel settings -->
                <!-- Upload image size -->
                <div class="sel">
                    <label for="maxupload" class="lab-5" title="Max uploads size"> Max upload image size</label><br>
                    <span class="des">Set the maximum size - in kilobytes - of the uploaded image files.</span>
                </div>
                <input name="maximagesize" type="text" id="maxupload" class="inp-7" title="File size in kilobytes" value="<?php print $maxsize ?>">kb
                <div class="clear"></div>
                
                <!-- Text editor -->
                <div class="sel">
                    <label for="onoff-0" class="lab-5" title="Text/HTML editor">Text/HTML editor</label><br>
                    <span class="des">Use an old document "wysiwyg" editor to write posts and pages.</span>
                </div>
                <input type="checkbox" name="texteditor" value="on" id="onoff-0" class="moncheck" title="Text editor" <?php print $tog[0] ?>>
                <label for="onoff-0" class="lab-5" title="Text/HTML editor"></label>
                <div class="clear"></div>
                
                

                <!-- Color theme -->
                <div class="sel">
                    <label for="rd-1" class="lab-5" style="margin-right:12px" title="Control panel theme">Color theme</label><br>
                    <span class="des">Light theme is the default control panel theme.</span>
                </div>
                <div class="sel-r">
                    <input name="theme" type="radio" id="rd-1" class="monradio" value="light"<?php if ($user_theme == 'light') print ' checked' ?>>
                    <label for="rd-1" title="Light theme">Light</label> &nbsp;&nbsp;
                    <input name="theme" type="radio" id="rd-3" class="monradio" value="dark"<?php if ($user_theme == 'cmdark') print ' checked' ?>>
                    <label for="rd-3" title="Dark theme">Dark</label>    
                </div>
                <div class="clear"></div>



                <!-- Refresh system -->
                <div class="sel">
                    <span class="labspan" title="System refresh">System refresh</span><br>
                    <span class="des">Refresh your system by checking files health. This is going to: <br>Adjust the file counters<br>Recover accidentally deleted pages from the home directory.</span>
                </div>
                <a href="refresh.php" target="_blank" class="setlink" title="Refresh system">Refresh system</a>
                <div class="clear"></div>
                <div class="newoptions"></div>
                <div class="clear"></div>


                                    
                <!-- User settings -->
                <!-- Editor name-->
                <div class="sel">
                    <label for="editorname" class="lab-5" title="Editor name">Editor name</label><br>
                    <span class="des">Appears instead of username.</span>
                </div>
                <input name="usereditorname" type="text" id="editorname" class="inp-76" title="Editor name" value="<?php print $editorname ?>">
                <div class="clear"></div>



                <!-- User icon -->
                <div><span class="lab-5" title="User icon">User icon</span><br>
                    <span class="des">Upload an icon or use an image file from your uploaded images.</span>
                </div>
                
                
                <!-- Upload iframe -->
                <iframe src="upload_icon.php" name="user-image" id="user-image"></iframe>
                <div class="clear-s"></div>
                
                
                <!-- Image file -->
                    <p>
                    <label for="usericon" class="lab-5 sublab" title="Image file">Image file</label><br>
                    <span class="des">An image file from images folder. Overrides upload image option.</span>
                    </p>
                    <input id="usericon" type="text" name="urlicon" value="<?php print $urlicon ?>" class="inp-1" style="margin-left:20px"  title="Image filename" placeholder="empty">
                <div class="clear"></div>
                
                
                
                <!-- Change login pasword -->
                <div class="sel">
                    <span title="Login password">Login password</span><br>
                    <span class="des">Create a new login password.</span>
                </div>
                <a href="password_change.php" class="setlink" title="Go to change password page">Change your log in password</a><br>
                <div class="clear"></div>
                </div>
                <div class="clear"></div>
                <div class="clear"></div>
            </div>
        </form>
    </div>
    <?php 



    # plugins
    include 'execute.php'



    ?>
</body>
</html>