<?php
if (isset($_GET['source'])) {
    
highlight_file(__FILE__);
    exit;
}

// Using a dictionary seems to help about 5%, when taking the contents of some random Wikipedia page.
// Doesn't seem worth it to me to add this complexity, especially considering that people would need to
// somehow access the dictionary if this site goes down, defeating the premise of CAU.
/* Just to document how I did this, in case it's ever useful: words.txt is
    1. https://github.com/first20hours/google-10000-english/blob/master/google-10000-english.txt
    2. Reverse sort so that most frequent word last (easiest to reference):
       <google-10000-english.txt tac | awk '{if(length($0)>2){print $0}}' > tmp.txt
    3. Remove overlap, e.g. "for" already occurs in "before" and others:
       awk '{keep=1;for(w in seen){if(index(w, $0)){keep=0;break;}}if(keep){print $0;seen[$0]=1;}}'
         <tmp.txt >words.txt
    4. vim words.txt, join (J) all lines, then count N chars from the end (LZ77 context window
         minus a little bit because user input would otherwise push out the first bytes anyway)
         and delete what comes before
*/
function load_dictionary(): string {
    return 
file_get_contents('words.txt');
}
function 
compressWithDictionary($data) {
    
$dictionary load_dictionary();
    
$ctx deflate_init(ZLIB_ENCODING_RAW, ['level' => 9'window' => 15'dictionary' => $dictionary]);
    return 
deflate_add($ctx$dataZLIB_FINISH);
}
function 
decompressWithDictionary($data) {
    
$dictionary load_dictionary();
    
$ctx inflate_init(ZLIB_ENCODING_RAW, ['dictionary' => $dictionary]);
    return 
inflate_add($ctx$dataZLIB_FINISH);
}

if (isset(
$_POST['rd'])) {
    if (isset(
$_POST['english'])) {
        
header('Location: ?ben&' urlencode(base64_encode(compressWithDictionary($_POST['rd']))));
    }
    else if (isset(
$_POST['obfuscate'])) {
        
header('Location: ?b64&' urlencode(base64_encode(gzdeflate($_POST['rd']))));
    }
    else {
        
header('Location: ?' urlencode($_POST['rd']));
    }
    exit;
}

?>
<!DOCTYPE html>
<meta charset="UTF-8">
<meta name=viewport content="width=device-width">
<?php

if (empty($_SERVER['QUERY_STRING'])) {
    
?>
        Content-addressed URL. This allows you to link to text without:
        <ul>
            <li> having to create a page first, or
            <li> the data being lost if the site goes down.
        </ul>
        Your text:
        <form method=POST>
            <textarea name=rd cols=80 rows=5 autofocus></textarea><br>
            <input type=submit value='Go to URL'>
            <input type=submit value='Obfuscate/compress and go' name=obfuscate>
            <!-- <input type=submit value='Extra compression for English text' name=english> -->
        </form>
        <br><br>

        Example usage: footnotes, e.g. the link in "<i>clouds can weigh a million kg[url=https://...]¹[/url]</i>"
        could be a content-addressed URL with more context (like how you calculated the value, or conversions to other unit systems).<br><br>

        You could also use <tt>data:text/plain,...</tt> (data URL) which doesn't need a server, but this is often not parsed/clickable, or not allowed altogether.
        This is an alternative to data URLs, really.<br><br>

        Only plain text is allowed so that I hopefully don't have to police this. The size limit is a few KB. Other systems don't like URLs beyond a certain size anyway.<br><br>

        <a href='?source'>Source code</a>
    <?php
    
exit;
}

$data urldecode($_SERVER['QUERY_STRING']);
if (
substr($data04) === 'b64&') {
    
$data substr($data4);
    
$decompress 1;
}
if (
substr($data04) === 'ben&') { // note: this has never been live, so we can remove or change its data format anytime. The logic behind the TLA is "Base64 ENglish"
    
$data substr($data4);
    
$decompress 2;
}
if (
substr($data04) === 'raw&') {
    
header('Content-Type: text/plain');
    
$data substr($data4);
    if (
$decompress) {
        
$data gzinflate(base64_decode($data));
    }
    print(
$data);
    exit;
}

if (
$decompress == 1) {
    
$data gzinflate(base64_decode($data));
}
else if (
$decompress == 2) {
    
$data decompressWithDictionary(base64_decode($data));
}

// TODO why do ISO-8859-1 and UTF-8 both work identically with ïÿỹỸë°C¸şç as input value, does this parameter do anything?
echo '<title>' htmlspecialchars(substr($data0256), ENT_COMPAT ENT_HTML401'UTF-8') . '</title><main style="max-width: 650px; line-height: 130%; margin-bottom: 90px;">';
echo 
nl2br(str_replace('  ''&nbsp;&nbsp;'preg_replace('/^ /''&nbsp;'htmlspecialchars($dataENT_COMPAT ENT_HTML401'UTF-8'))));
echo 
'</main>';
echo 
'<a style="color:#888; font-size: 9pt;" href=".">Create your own long URL</a>';