/** @todo document */
function randomUrlList($length)
{
    $list = array();
    for ($i = 0; $i < $length; $i++) {
        $list[] = randomUrl();
    }
    return $list;
}
Beispiel #2
0
            $possible = "0123456789";
            break;
    }
    $i = 0;
    while ($i < $length) {
        $str .= substr($possible, mt_rand(0, strlen($possible) - 1), 1);
        $i++;
    }
    return $str;
}
if (isset($_POST['url']) && empty($_POST['antispam'])) {
    $database = DB::getInstance();
    $url_to_shorten = $database->filter($_POST['url']);
    if (filter_var($url_to_shorten, FILTER_VALIDATE_URL) !== FALSE) {
        $check_column = 'long_url';
        $check_for = array('long_url' => $url_to_shorten);
        $exists = $database->exists('shortened_urls', $check_column, $check_for);
        if ($exists) {
            list($shortened_url) = $database->get_row("SELECT short_url FROM shortened_urls WHERE long_url = '{$url_to_shorten}' ");
            echo $shortened_url;
        } else {
            $shortening = array('long_url' => $url_to_shorten, 'short_url' => randomUrl(), 'date_created' => time());
            $add_query = $database->insert('shortened_urls', $shortening);
            if ($add_query) {
                echo $shortening['short_url'];
            }
        }
    } else {
        echo 'INVALID_URL';
    }
}