Skip to content

Instantly share code, notes, and snippets.

@jcubic
Last active December 22, 2017 11:10

Revisions

  1. jcubic revised this gist Dec 22, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.php
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ function self_url() {
    } elseif (isset($_GET['url'])) {
    $res = $db->prepare("INSERT INTO urls VALUES(?, ?)");
    if ($res->execute(array($hash, $_GET['url']))) {
    echo $hash;
    echo self_url() . $hash;
    }
    }
    }
  2. jcubic revised this gist Dec 21, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.php
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ function self_url() {
    $hash = $_SERVER['QUERY_STRING'];
    }

    if ($strlen($hash) == 6 && $query->execute(array($hash))) {
    if (strlen($hash) > 0 && $query->execute(array($hash))) {
    $result = $query->fetchAll(PDO::FETCH_ASSOC);
    if (count($result) > 0) {
    if (isset($_GET['url'])) {
  3. jcubic created this gist Dec 21, 2017.
    5 changes: 5 additions & 0 deletions .htaccess
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    RewriteEngine on

    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule (.*) /index.php?$1
    50 changes: 50 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    <?php

    header('Content-Type: text/plain');

    function hash36($str) {
    $arr = unpack("C*", pack("L", crc32($str)));
    return implode(array_map(function($number) {
    return base_convert($number, 10, 36);
    }, $arr));
    }

    function self_url() {
    $path = preg_replace("/\?.*$/", "", $_SERVER['REQUEST_URI']);
    $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    return $protocol . $_SERVER['HTTP_HOST'] . $path;
    }

    $filename = "db.sqlite";
    $init = !file_exists($filename);
    $db = new PDO('sqlite:' . $filename);
    //$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if ($init) {
    $db->query("CREATE TABLE urls(hash VARCHAR(10), url TEXT)");
    }

    $query = $db->prepare("SELECT * FROM urls WHERE hash = ?");

    if (isset($_GET['url'])) {
    $hash = hash36($_GET['url']);
    } else {
    $hash = $_SERVER['QUERY_STRING'];
    }

    if ($strlen($hash) == 6 && $query->execute(array($hash))) {
    $result = $query->fetchAll(PDO::FETCH_ASSOC);
    if (count($result) > 0) {
    if (isset($_GET['url'])) {
    echo self_url() . $result[0]['hash'];
    } else {
    header('Location: ' . $result[0]['url'], true, 301);
    }
    } elseif (isset($_GET['url'])) {
    $res = $db->prepare("INSERT INTO urls VALUES(?, ?)");
    if ($res->execute(array($hash, $_GET['url']))) {
    echo $hash;
    }
    }
    }

    ?>