<?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();
    }



    # page functions
    # 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'];
        $indexposts = $_POST['indexposts'];
        $urlicon = $_POST['urlicon'];
        
        
        if ((!ctype_digit($max)) || ($max < 0) || ($max > 20000))
                $error = 'ERROR:maximagesize'; # see line 108 - $_SESSION[$error]
                
        if (!ctype_digit($indexposts) || $indexposts < 0 || $indexposts > 1000)
            $error = 'ERROR:indexposts'; # see line 108 - $_SESSION[$error]
        
        
        
        
        
        #user related info
        if (strlen($_POST['usereditorname']) > 40)
            $error = 'ERROR:editorname'; # see line 108 - $_SESSION[$error]

        $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];
        $personal_on_off = ['texteditor','themecustom'];
        
        
        #administrator settings
        foreach($on_off_values as $onoff) {
            $val = $_POST[$onoff];
            if ($val != 'on')
                $val = '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;

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

    
        
        if (!isset($error)) {
            
            
            $save->general->dateshow = $_POST['dateshow'];
            $save->general->modified = time();
            
            #save            
            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
    $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[] = '';
    

    # user accout info
    $acc_info = get_account($_SESSION['userID']);
    $editorname = $acc_info['editorname'];
    $urlicon = $acc_info['urlicon'];
    
    
    
    # Date format
    # get dates from dates.xml
    
    $datesxml = simplexml_object('dates.xml','l',null);
    $php_dates = phpdate($datesxml->dateformats->df);
    
    array_push($php_dates,'none(turn off)','off');
    $cssval = array_search((string)$date,$php_dates)
    

?>
    <!-- style a loaded value -->
    <style>
        @charset "utf-8";
        #dinp-<?php print $cssval+1 ?>:checked + label:before{opacity:.5;<?php print ($user_theme == 'light') ? 'box-shadow:inset 0 0 0 2px #f7f7f7;background-color:#999;' : 'background-color:#aaa;box-shadow:inset 0 0 0 2px #222' ?>}
        #dinp-<?php print $cssval+1 ?> + label{opacity:.5}
        #dinp-<?php print $cssval+1 ?>:checked + label{opacity:1}
        #settings{<?php include 'cmcurrentpage.css' ?>}
    </style>
    <div class="main">
    
    <h1>Settings</h1>
    
        <div class="center-container">
        <?php
        
        action_confirm();

        if(isset($_SESSION['usericon']))
            unset($_SESSION['usericon']);
        
        ?>
            <div id="settingspage">
            
                <form action="general.php" method="post" id="MONform">
                    
                <!-- Website settings -->
                        
                        
                    <!-- Title -->
                    <div class="sel">
                        <label for="website-title" class="lab-5">Title</label><br>
                        <span class="des">Your website's title.</span>
                    </div>
                        <input name="title" type="text" id="website-title" class="inp-2" value="<?php print $website_title ?>">
                    <div class="clear-s"></div>




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

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

                    <!--



                    Date format (previous Date show) -->
                    <div class="sel">
                        <span class="lab-5">Date</span><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" ';

                    $cid = 1;
                    foreach($php_dates as $dopt) {

                        $check = ($date == $dopt) ? 'checked' : '';                        

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

                        $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">none(turn off)</label>'

                    ?>
                    </div>
                    <div class="clear"></div>
        
                    
                    <!-- Results in home page -->
                    <div class="sel">
                        <label for="results-in-homepage" class="lab-5"> Results in home page</label><br>
                        <span class="des">The number of posts to appear in your blog's posts flow.</span>
                    </div>
                    <input name="indexposts" type="text" id="results-in-homepage" class="inp-4" value="<?php print $posts ?>">
                    <a class="qmark" href="javascript:;">?<span class="details">Write '0' to completely remove from home page.</span></a>
                    <div class="clear"></div>
                    
                    <!-- Sign posts -->
                    <div class="sel">
                        <label for="onoff-1" class="lab-5">Sign posts</label><br>
                        <span class="des">Sign a post with the author's editor 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"></label>
                    <div class="clear-s"></div>
                    
                    <!-- Categories -->
                    <div class="sel">
                        <label for="onoff-2" class="lab-5">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"></label>
                    <div class="clear-s"></div>
                    
                    <!-- Tags -->
                    <div class="sel">
                        <label for="onoff-3" class="lab-5">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"></label>
                    <div class="clear-s"></div>
                    
                    <!-- Read old posts -->
                    <div class="sel">
                        <label for="onoff-4" class="lab-5">Read older posts 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"></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"> Max upload image size</label><br>
                        <span class="des">Set the maximum size - in kilobytes - of the uploaded image files, to save disk space and get faster loading speeds of your website.</span>
                    </div>
                    <input name="maximagesize" type="text" id="maxupload" class="inp-7" value="<?php print $maxsize ?>">kb
                    <div class="clear"></div>
                    
                    <!-- Text editor -->
                    <div class="sel">
                        <label for="onoff-0" class="lab-5">Text/HTML editor</label><br>
                        <span class="des">Use the old Document.execCommand() 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"></label>
                    <div class="clear"></div>
                    

                    <!-- Color theme -->
                    <div class="sel">
                        <label class="lab-5" style="margin-right:12px;cursor:default">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" title="Standard"<?php if ($user_theme == 'light') print ' checked' ?>>
                        <label for="rd-1">Light</label> &nbsp;&nbsp;

                        <input name="theme" type="radio" id="rd-3" class="monradio" value="dark" title="Dark"<?php if ($user_theme == 'cmdark') print ' checked' ?>>
                        <label for="rd-3">Dark</label>
                        
                        
                    </div>
                    <div class="clear"></div>


                    <!-- Refresh system -->
                    <div class="sel">
                        <span class="lab-5">System refresh</span><br>
                        <span class="des">Refresh your system by checking files health.</span>
                    </div>
                    <a href="refresh.php" target="_blank" class="setlink">Refresh system</a>
                    <a class="qmark" href="javascript:;">?<span class="details">This is going to: <li>- adjust the file counters,</li><li>- recover accidentally deleted pages.</li></span></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">Editor name</label><br>
                        <span class="des">Appears instead of username.</span>
                    </div>
                    <input name="usereditorname" type="text" id="editorname" class="inp-76" value="<?php print $editorname ?>">
                    <div class="clear"></div>


                    <!-- User icon -->

                    <div><span class="lab-5">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>
                        <p><label for="usericon" class="lab-5 sublab">Image file</label></p>
                        <input id="usericon" type="text" name="urlicon" value="<?php print $urlicon ?>" class="inp-1" style="margin-left:20px"> <a class="qmark" href="javascript:;">?<span class="details">Overrides upload image option.</span></a>
                    <div class="clear"></div>
                    
                    
                    
                    <!-- Change login pasword -->
                    <div class="sel">
                        <span class="lab-5">Login password</span><br>
                        <span class="des">Create a new login password.</span>
                    </div>
                    <a href="password_change.php" class="setlink">Change your log in password</a><br>
                    <div class="clear"></div>
                </form>
            </div>
        </div>
    </div>
    <?php 



    # plugins
    include 'execute.php'



    ?>
</body>
</html>