<?php
    
    
    # Plugins page
    # Plugins can be either 'system' or 'user'
    # System plugins can be controlled by admins only. User is for everyone. 



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



    # user rights - user info
    $acc_info = get_account($_SESSION['userID']);
    $rights = $acc_info['rights'];
    


    # find installed plugins
    $plugins = get_plugins();
    $count_p = count($plugins);
    

    # Plugins form submitted
    if (isset($_POST['submitted'])) {
        
        if ($count_p > 0) {
        
            $createlog = simplexml_object('log.xml','e',null);

            foreach($plugins as $key=>$val) {

                $status = isset($_POST[$key]) ? 'on' : 'off';

                if ($val[3] == '../plugins' && $rights == 'administrator')
                    $createlog->plugins->$key = $status;

                elseif ($val[3] == '../plugins/user') {

                    $u=0;
                    foreach ($createlog->user as $user) {

                        if ($user->name == $_SESSION['username']) {
                            $createlog->user[$u]->$key = $status;
                            break;
                        }

                        $u++;
                    }
                }
            }

            simplexml_object_save('log.xml',$createlog,null);
            $_SESSION['MON_plugins'] = true;
            
        }
        
        else
            $_SESSION['MON_plugins_not_exist'] = true;
        
        
        header('Location:plugins.php');
        exit();
        
    }



    # extensions
    include 'command.php'
        


?>
<!doctype html>
<html>
<head>
<title>Plugins</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="cmstyle.css">
<?php 
        include 'csstheme.php';
        if ($count_p > 0) :
    ?>
<link rel="stylesheet" type="text/css" href="cmsavebutton.css">
<?php endif ?>    
</head>
<body>
<?php


    # panel
    include 'mypanel.php';
    action_confirm()



?>
    <div class="main">
        <h1 id="plugins-h">Plugins</h1>
        <div class="clear-h"></div>
        <?php if ($count_p > 0) : ?>
        <form action="plugins.php" id="MONform" method="post"></form>

        <?php
        
            # load the plugins
        
            $id=0;

            foreach($plugins as $key=>$val) {

                if ($val[3] == '../plugins' && $acc_info['rights'] != 'administrator')
                    continue;

                $title = $val[1];
                $description = $val[2];
                $chk = $val[0] == 'on' ? 'checked' : '';


                echo '<span class="plonoff"><label for="onoff-'.$id.'" title="'.$title.'">'.$title.'</label><span class="pldes">'.$description.'</span></span><input type="checkbox" name="'.$key.'" value="on" class="moncheck" form="MONform" id="onoff-'.$id.'" autocomplete="off" '.$chk.'><label for="onoff-'.$id.'" title="'.$title.'"></label><div class="clear pluginsclear"></div>'.PHP_EOL;

                $id++;

            }
            
        
            else :
                echo '<i id="no-results">:: no plugins</i>'; 
        
            endif
                
        ?>
        
    </div>
<?php 



    # plugins
    include 'execute.php' 



?>
</body>
</html>