Post
Topic
Board Meta
Merits 20 from 5 users
Re: Little things that bug you/me about the forum
by
PowerGlove
on 16/07/2025, 15:54:26 UTC
⭐ Merited by Welsh (6) ,LoyceV (6) ,vapourminer (4) ,dkbit98 (3) ,joker_josue (1)
Yes, of course that's what worries me the most.  Grin
I'm not insinuating that anything like that was the motivation. I'm simply saying "Thanks" to the people I mentioned (I should have included TryNinja), and I'm pointing out that I often don't merit posts that I personally really appreciate.



@Cyrus: I'm still stop-and-start working on your nice link-sharing suggestion from a few pages back, and I have a suggestion of my own for your bta.lk project.

This ended up being a bit of a back-and-forth with theymos (who added some nice ideas), but, instead of re-hashing the whole thought process that we went through, I'll just share the summary:

Basically, using your suggestion post as an example, with your existing approach, its URL would be shortened to: bta.lk/t/5503118.msg65275936#msg65275936. But, by taking advantage of the asterisk-topic-ID patch I did in 2023, and by using a Base58-encoded message ID (plus a bit of arithmetic that I'll justify later), you could implement an approach that would allow it to be shortened to: bta.lk/CY7XN. (I mean, that's pretty cool, right? 12 characters instead of 40.) Smiley

How it would work is that you take the message ID (in this case: 65275936), then multiply it by 2 and add 1 (resulting in: 130551873). Then you encode that integer using Base58 (resulting in: CY7XN). Then, on your end, you set things up so that your server responds to URLs of the form: bta.lk/CY7XN, by first Base58-decoding the CY7XN part back into 130551873, and then checking that it's numerically odd before floor-dividing it by 2, which gets you all the way back to 65275936. Finally, you use that recovered message ID to do a 301 redirect to: https://bitcointalk.org/index.php?topic=*.msg65275936#msg65275936. (The multiplying/dividing by 2, and the adding/subtracting of 1 are details that have to do with keeping the scheme amenable to extension. In code, those steps don't appear in their arithmetical form, and are bitwise operations that are there to make sure that the least significant bit is set to 1 for message IDs. The least significant bit being unset is reserved for future use.)

To get this working on your end, the first step would be to add this PHP file to your document root:

Code: ("link.php")
<?php declare(strict_types=1);

function 
int_from_base58(string $base58): int null {

    
$result strlen($base58) >= null;

    foreach(
str_split($base58) as $digit) {

        
$value strpos('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'$digit);

        if(
$value === false || $result intdiv(PHP_INT_MAX $value58)) {

            return 
null;
        }

        
$result $result 58 $value;
    }

    return 
$result;
}

function 
redirect_die(string $location): void {

    
header('Location: ' $locationtrue301);

    die(
'<a href="' $location '">' $location '</a>');
}

if(isset(
$_GET['id']) && is_string($_GET['id'])) {

    
$decoded int_from_base58($_GET['id']);

    if(!
is_null($decoded)) {

        if(
$decoded == 1) {

            
$message_id $decoded >> 1;

            
redirect_die('https://bitcointalk.org/index.php?topic=*.msg' $message_id '#msg' $message_id);
        }
    }
}

http_response_code(400);

die(
'Malformed shortlink.');

?>

I noticed this in your server's response:

Code:
x-powered-by: PHP/5.6.40

I wrote and tested the above code on PHP 8.2 (but, I think it would run on 7.4, too). To get the above code working on PHP 5.6, you could try the below version (I haven't tested it, but, if you can't upgrade your PHP interpreter for some reason, then, you should try it and let me know if it doesn't work):

Code: ("link.php")
<?php

function int_from_base58($base58) {

    
$result strlen($base58) >= null;

    foreach(
str_split($base58) as $digit) {

        
$value strpos('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'$digit);

        if(
$value === false || $result floor((PHP_INT_MAX $value) / 58)) {

            return 
null;
        }

        
$result $result 58 $value;
    }

    return 
$result;
}

function 
redirect_die($location) {

    
header('Location: ' $locationtrue301);

    die(
'<a href="' $location '">' $location '</a>');
}

if(isset(
$_GET['id']) && is_string($_GET['id'])) {

    
$decoded int_from_base58($_GET['id']);

    if(!
is_null($decoded)) {

        if(
$decoded == 1) {

            
$message_id $decoded >> 1;

            
redirect_die('https://bitcointalk.org/index.php?topic=*.msg' $message_id '#msg' $message_id);
        }
    }
}

http_response_code(400);

die(
'Malformed shortlink.');

?>

You'll know that you've got this step working when you can navigate to: https://bta.lk/link.php?id=CY7XN, and be redirected to: https://bitcointalk.org/index.php?topic=*.msg65275936#msg65275936.

The next and final step is to hide the link.php?id= part behind some URL rewriting.

I mocked this up a few different ways in my test environment...

On nginx, I got it working by adding the following line to the server section of the config:

Code:
rewrite ^/([1-9A-HJ-NP-Za-km-z]+)$ /link.php?id=$1 break;

On apache2, I got it working (after enabling mod_rewrite) by adding the following line to the VirtualHost section of the config:

Code:
RewriteRule ^/([1-9A-HJ-NP-Za-km-z]+)$ /link.php?id=$1

I'm not sure what you'd have to do for LiteSpeed, which is what I see you're running, but, hopefully the above is enough for you to figure it out. (I'd take everything I've said about this URL-rewriting step with a large grain of salt: DYOR and all that; I can't remember the last time I put a web server into production that I didn't write myself, and I don't have the temperament to learn configuration syntax and struggle with an off-the-shelf tool when it's easier for me to just write an application-specific server in C++ and do everything in code. Basically, what I'm saying is: I don't employ this style of problem-solving when I can help it, so, probably someone more versed in HTTP server configuration can find fault with or otherwise add value to my URL-rewriting snippets.)

Anyway, in case it isn't clear, the whole idea here is that when I get around to finishing the link-sharing patch, this new kind of really compact shortlink is what I'd like to generate in addition to a normal Bitcointalk link. (And, in the meantime, I guess people could make use of it early by manually taking any message ID, multiplying it by 2 and adding 1, then using an online Base58 encoder to derive a working shortlink.)