Skip to content
Snippets Groups Projects
commons_admin.php 2.52 KiB
Newer Older
  • Learn to ignore specific revisions
  • Manuel Herrmann's avatar
    Manuel Herrmann committed
    <?php
    
    require_once("../config.inc.php");
    
    function generateNavigationItems($page, $menu)
    {
        $text = '';
        foreach($menu as $name => $page)
        {
            $text .= "<a href='?page=$page'>$name</a>";
        }
        return $text;
    }
    
    function checkIfLogin()
    {
    
    
        if(isset($_GET['logout']))
            setLoggedIn("");
    
    
    Manuel Herrmann's avatar
    Manuel Herrmann committed
        if(!isset($_POST['user']) || !isset($_POST['password']))
            return;
    
        $user = $_POST['user'];
        $password = $_POST['password'];
    
    
    Manuel Herrmann's avatar
    Manuel Herrmann committed
        if (isValidUser($user, $password))
            setLoggedIn($user);
    }
    
    function isValidUser($user, $password)
    {
    
    Manuel Herrmann's avatar
    Manuel Herrmann committed
        foreach($config_admins as $cfg_user => $cfg_password)
        {
            if ($cfg_user != $user)
                continue;
    
            $cfg_password = $cfg_password["pw"];
    
    Manuel Herrmann's avatar
    Manuel Herrmann committed
            if ($cfg_password[0] == '{')
            {
                if (strpos($cfg_password, "{SHA254}") >= 0)
                {
                    $beginOfSalt = strpos($cfg_password, "$");
                    $salt = substr($cfg_password, 9, strpos($cfg_password, "$") - 9);
                    $hash = substr($cfg_password, $beginOfSalt + 1);
    
                    if (hash('sha256', $password . $salt) == $hash)
                        return true;
                }
            }
            else
            {
                // TODO: ONLY sha256 yet, others not implemented
            }
        }
        return false;
    }
    
    
    function readUserFile(){
        global $config_userfile;
        $ret = [];
    
        $handle = fopen($config_userfile, "r");
        if ($handle) {
            while (($line = fgets($handle)) !== false) {
                $tmp = explode(" ", $line);
                if(count($tmp)>=3){
                    $ret[$tmp[1]] = ["pw" => $tmp[2], "sa" => $tmp[0]];
                }
            }
        } else { }
        fclose($handle);
        return $ret;
    }
    
    function isSuperAdmin(){
        $config_admins = readUserFile();
        return isset($_SESSION['loggedIn']) && isset($config_admins[$_SESSION['loggedIn']]) && $config_admins[$_SESSION['loggedIn']]['sa'] === "S";
    }
    
    
    Manuel Herrmann's avatar
    Manuel Herrmann committed
    function isLoggedIn()
    {
        return isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] != '';
    }
    
    function setLoggedIn($user)
    {
    
        if ($user != ""){
            comm_admin_verbose(2,"login");
    
    Manuel Herrmann's avatar
    Manuel Herrmann committed
            $_SESSION['loggedIn'] = $user;
    
    Manuel Herrmann's avatar
    Manuel Herrmann committed
        {
    
            comm_admin_verbose(2,"logout");
    
    Manuel Herrmann's avatar
    Manuel Herrmann committed
            session_destroy();
            header("location: ..");
        }
    }
    
    
    function comm_admin_verbose($level, $text){
        global $config_admin_verbose_level;
    
        if($config_admin_verbose_level >= $level)  {
            if(is_array($text)){
                echo "<pre>"; print_r($text); echo "</pre>";
            } else
                echo $text.'<br />';
        }