<commit_before><?php

/*
    All Emoncms code is released under the GNU Affero General Public License.
    See COPYRIGHT.txt and LICENSE.txt.

    ---------------------------------------------------------------------
    Emoncms - open source energy visualisation
    Part of the OpenEnergyMonitor project:
    http://openenergymonitor.org

*/

// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');

function user_controller()
{
    global $mysqli, $redis, $user, $path, $session, $route , $settings;

    $result = false;

    $allowusersregister = true;
    // Disables further user creation after first admin user is created
    if ($settings["interface"]["enable_multi_user"]==false && $user->get_number_of_users()>0) {
        $allowusersregister = false;
    }

    // Load html,css,js pages to the client
    if ($route->format == 'html')
    {
        if ($route->action == 'login' && !$session['read']) {
            $route_query = array();
            // pass through the referring path
            parse_str($route->query, $route_query );
            $msg = empty($route_query['msg']) ? get('msg') : $route_query['msg'];
            $ref = empty($route_query['ref']) ? get('ref') : $route_query['ref'];
            $message = filter_var(urldecode($msg), FILTER_SANITIZE_STRING);
            $referrer = htmlentities(filter_var(urldecode(base64_decode($ref)), FILTER_SANITIZE_URL));
            // load login template with the above parameters
            $result = view("Modules/user/login_block.php", array(
                'allowusersregister'=>$allowusersregister,
                'verify'=>array(),
                'message'=>$message,
                'referrer'=>$referrer,
                'v' => 3
            ));
        }
        if ($route->action == 'view' && $session['write']) $result = view("Modules/user/profile/profile.php", array());
        
        if ($route->action == 'logout') {
            // decode url parameters
            $next = $path;
            $message = filter_var(urldecode(get('msg')), FILTER_SANITIZE_STRING);
            $referrer = filter_var(urldecode(base64_decode(get('ref'))), FILTER_SANITIZE_URL);
            
            // encode url parameters to pass through to login page
            $msg = urlencode($message);
            $ref = base64_encode(urlencode($referrer));
            if(!empty($ref)) {
                $next = sprintf('%s?msg=%s&ref=%s',$path, $msg, $ref);
            }

            $user->logout(); 
            call_hook('on_logout',[]);
            header('Location: '.$next);
            exit();
        }
        
        if ($route->action == 'verify' && $settings["interface"]["email_verification"] && !$session['read'] && isset($_GET['key'])) { 
            $verify = $user->verify_email($_GET['email'], $_GET['key']);
            $result = view("Modules/user/login_block.php", array('allowusersregister'=>$allowusersregister,'verify'=>$verify));
        }

        if ($route->action == 'bookmarks' && $session['write']) {
            $bookmarks = $user->getUserBookmarks($session['userid']);
            $result = view("Modules/user/Views/bookmarks.php", array('path'=>$path, 'bookmarks'=>$bookmarks));
        }

    }

    // JSON API
    if ($route->format == 'json')
    {
        // Core session
        if ($route->action == 'login' && !$session['read']) $result = $user->login(post('username'),post('password'),post('rememberme'),post('referrer'));<commit_msg>filter referrer<commit_after><?php

/*
    All Emoncms code is released under the GNU Affero General Public License.
    See COPYRIGHT.txt and LICENSE.txt.

    ---------------------------------------------------------------------
    Emoncms - open source energy visualisation
    Part of the OpenEnergyMonitor project:
    http://openenergymonitor.org

*/

// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');

function user_controller()
{
    global $mysqli, $redis, $user, $path, $session, $route , $settings;

    $result = false;

    $allowusersregister = true;
    // Disables further user creation after first admin user is created
    if ($settings["interface"]["enable_multi_user"]==false && $user->get_number_of_users()>0) {
        $allowusersregister = false;
    }

    // Load html,css,js pages to the client
    if ($route->format == 'html')
    {
        if ($route->action == 'login' && !$session['read']) {
            $route_query = array();
            // pass through the referring path
            parse_str($route->query, $route_query );
            $msg = empty($route_query['msg']) ? get('msg') : $route_query['msg'];
            $ref = empty($route_query['ref']) ? get('ref') : $route_query['ref'];
            $message = filter_var(urldecode($msg), FILTER_SANITIZE_STRING);
            $referrer = htmlentities(filter_var(urldecode(base64_decode($ref)), FILTER_SANITIZE_URL));
            $referrer = preg_replace('/[^\p{N}\p{L}_\s\/-]/u','',$referrer);
            
            // load login template with the above parameters
            $result = view("Modules/user/login_block.php", array(
                'allowusersregister'=>$allowusersregister,
                'verify'=>array(),
                'message'=>$message,
                'referrer'=>$referrer,
                'v' => 3
            ));
        }
        if ($route->action == 'view' && $session['write']) $result = view("Modules/user/profile/profile.php", array());
        
        if ($route->action == 'logout') {
            // decode url parameters
            $next = $path;
            $message = filter_var(urldecode(get('msg')), FILTER_SANITIZE_STRING);
            $referrer = filter_var(urldecode(base64_decode(get('ref'))), FILTER_SANITIZE_URL);
            $referrer = preg_replace('/[^\p{N}\p{L}_\s\/-]/u','',$referrer);
            
            // encode url parameters to pass through to login page
            $msg = urlencode($message);
            $ref = base64_encode(urlencode($referrer));
            if(!empty($ref)) {
                $next = sprintf('%s?msg=%s&ref=%s',$path, $msg, $ref);
            }

            $user->logout(); 
            call_hook('on_logout',[]);
            header('Location: '.$next);
            exit();
        }
        
        if ($route->action == 'verify' && $settings["interface"]["email_verification"] && !$session['read'] && isset($_GET['key'])) { 
            $verify = $user->verify_email($_GET['email'], $_GET['key']);
            $result = view("Modules/user/login_block.php", array('allowusersregister'=>$allowusersregister,'verify'=>$verify));
        }

        if ($route->action == 'bookmarks' && $session['write']) {
            $bookmarks = $user->getUserBookmarks($session['userid']);
            $result = view("Modules/user/Views/bookmarks.php", array('path'=>$path, 'bookmarks'=>$bookmarks));
        }

    }

    // JSON API
    if ($route->format == 'json')
    {
        // Core session
        if ($route->action == 'login' && !$session['read']) $result = $user->login(post('username'),post('password'),post('rememberme'),post('referrer'));
{'fraction_non_alphanumeric': 0.1890029531711433, 'fraction_numerical': 0.002250035156799325, 'mean_word_length': 2.3499764484220442, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4800904', 'n_tokens_mistral': 2069, 'n_tokens_neox': 2040, 'n_words': 394}