Skip to content
Snippets Groups Projects
Select Git revision
  • 74d351e9cd953dee861ff57a8d2bfc91c2dc02d1
  • main default protected
  • rewrite
  • production
4 results

accounts_handler.php

Blame
  • accounts_handler.php 5.01 KiB
    <?php
    // This file carries functions related to accounts.
    
    function get_avatar_url($bcid):string {
    
        $exists = db_execute('SELECT public FROM avatars WHERE id = ? LIMIT 1', [$bcid]);
    
        if (empty($exists)) {
            return '/assets/default.png';
        }
    
        return '/public/avatars/' . $bcid;
    }
    
    function get_display_name($bcid, $use_bcid_fallback=true, $put_bcid_in_parenthesis=false, $format_bcid=false):string {
    	$display_name = db_execute("SELECT display_name FROM accounts WHERE id = ?", [$bcid])['display_name'];
    	if (!empty($display_name)) {
            if ($put_bcid_in_parenthesis) {
                return $display_name . " ($bcid)";
            }
    		return $display_name;
    	}
    
    	if ($use_bcid_fallback) {
    		return $bcid;
    	}
    
    	return "";
    }
    
    // Tokens so apps can get VERY BASIC information
    
    function generate_basic_access_token($bcid): array
    {
        // Returns an access token, a refresh token and an expiry timestamp.
    
        $access_token = md5(uniqid(more_entropy: true).rand(1000000, 9999999));
        $refresh_token = md5(uniqid("rfish").rand(1000000, 9999999));
    
        $valid_time = 12; // in hours
        $expiry = time() + ($valid_time * 60 * 60);
    
    //    echo $access_token . ":" . $refresh_token;
    
        db_execute(
            "INSERT INTO tokens (access_token, refresh_token, expiry, owner_id) VALUES (?,?,?,?)",
            [$access_token, $refresh_token, $expiry, $bcid]
        );
    
        return [
            "access" => $access_token,
            "refresh" => $refresh_token,
            "expiry" => $expiry,
            "id" => $bcid
        ];
    }
    
    function generate_cookie_access_token($bcid) {
        $access_token = md5(uniqid(prefix: "COOKIECOOKIECOOKIE", more_entropy: true).rand(1000000, 9999999));
    
        $valid_time = 365 * 24; // 1 year
        $expiry = time() + ($valid_time * 60 * 60);
    
    //    echo $access_token . ":" . $refresh_token;
    
        db_execute(
            "INSERT INTO tokens (access_token, expiry, owner_id, type) VALUES (?,?,?,'cookie')",
            [$access_token, $expiry, $bcid]
        );