$stmt->execute(array($url_to_shorten)); $tmp = $stmt->fetch(PDO::FETCH_NUM); $already_shortened = $tmp[0]; if (!empty($already_shortened)) { // URL has already been shortened $shortened_url = getShortenedURLFromID($already_shortened); } else { // URL not in database, insert try { $dbh->beginTransaction(); $stmt2 = $dbh->prepare('INSERT INTO ' . DB_TABLE . " (long_url, created, creator) VALUES (?,?,?)"); $stmt2->execute(array($url_to_shorten, time(), $_SERVER['REMOTE_ADDR'])); $stmt->execute(array($url_to_shorten)); $tmp = $stmt->fetch(PDO::FETCH_NUM); print_r($tmp); $shortened_url = getShortenedURLFromID($tmp[0]); if (empty($shortend_url)) { die('Insertion ratee'); } $dbh->commit(); } catch (Exception $e) { $dbh->rollBack(); echo "Failed: " . $e->getMessage(); } } echo BASE_HREF . $shortened_url; } function getShortenedURLFromID($integer, $base = ALLOWED_CHARS) { $integer += 65536; $length = strlen($base);
} $url = $db->real_escape_string($_REQUEST['url']); $q1 = "SELECT `id` FROM `redirs` WHERE `url` = '{$url}';"; $r = $db->query($q1); if ($r->num_rows == 0) { $db->query("LOCK TABLES `redirs` WRITE;"); $q = "INSERT INTO `redirs`(`url`) VALUES('{$url}');"; $res = $db->query($q); if ($db->error) { die('Fehler'); } $u = getShortenedURLFromID($db->insert_id); $db->query("UNLOCK TABLES;"); } else { $row = $r->fetch_array(); $u = getShortenedURLFromID($row[0]); } if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { print "<a href=\"http://saul.li/{$u}\">http://saul.li/{$u}</a>"; } else { print "http://saul.li/{$u}"; } } else { $res = $db->query("SELECT COUNT(`id`) FROM `redirs`;"); $row = $res->fetch_array(); $num = $row[0]; ?> <!DOCTYPE html> <html> <head> <title>URL-Shortener</title>
<?php require_once 'configuration.php'; check(); $res = $db->query("SELECT * FROM `redirs` ORDER BY `id` DESC;"); $ctnt = ''; while ($row = $res->fetch_assoc()) { $ctnt .= '<tr id="r_' . $row['id'] . '"><td>'; $id = getShortenedURLFromID($row['id']); $ctnt .= '<a href="http://saul.li/' . $id . '">' . $id . '</a>'; $ctnt .= '</td><td class="url"><a href="'; $ctnt .= $row['url'] . '">' . shortLongUrl($row['url']) . '</a></td><td>' . $row['referrals'] . '</td>'; $ctnt .= '<td style="cursor:pointer;" onclick="remove(' . $row['id'] . ')"><img src="/x_small.png" alt="löschen" /></td>'; $ctnt .= '</tr>'; } header("Content-type: text/html; charset=UTF-8"); ?> <!DOCTYPE html> <html> <head> <title>Shortened URLs</title> <link rel="stylesheet" href="/stylesheet.css" type="text/css" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript"> function remove(lid) { $.post('del_url.php', { id : lid }, function(data) { if (data['type'] == 'error') { alert(data['msg']); } else if (data['type'] == 'success') {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($ch); $response_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($response_status == '404') { die('Not a valid URL'); } } // check if the URL has already been shortened $already_shortened = mysql_result(mysql_query('SELECT id FROM ' . DB_TABLE . ' WHERE long_url="' . mysql_real_escape_string($url_to_shorten) . '"'), 0, 0); if (!empty($already_shortened)) { // URL has already been shortened $shortened_url = getShortenedURLFromID($already_shortened); } else { // URL not in database, insert mysql_query('LOCK TABLES ' . DB_TABLE . ' WRITE;'); mysql_query('INSERT INTO ' . DB_TABLE . ' (long_url, created, creator) VALUES ("' . mysql_real_escape_string($url_to_shorten) . '", "' . time() . '", "' . mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . '")'); $shortened_url = getShortenedURLFromID(mysql_insert_id()); mysql_query('UNLOCK TABLES'); } echo BASE_HREF . $shortened_url; } function getShortenedURLFromID($integer, $base = ALLOWED_CHARS) { $length = strlen($base); while ($integer > $length - 1) { $out = $base[fmod($integer, $length)] . $out; $integer = floor($integer / $length); } return $base[$integer] . $out; }
$url_to_shorten = get_magic_quotes_gpc() ? stripslashes(trim($_REQUEST['longurl'])) : trim($_REQUEST['longurl']); if (!empty($url_to_shorten) && preg_match('|^https?://|', $url_to_shorten)) { // check if the URL has already been shortened $q = 'SELECT id FROM ' . DB_TABLE . ' WHERE long_url="' . sqlite_escape_string($url_to_shorten) . '"'; $already_shortened = $db->singleQuery($q); if (!empty($already_shortened)) { // URL has already been shortened $shortened_url = getShortenedURLFromID($already_shortened); } else { // URL not in database, insert building an unique id $q = 'SELECT max(id) FROM ' . DB_TABLE; $res = $db->singleQuery($q); $new_id = intval($res) + 1; $q = 'INSERT INTO ' . DB_TABLE . ' (id, long_url, created, creator) VALUES ("' . $new_id . '", "' . sqlite_escape_string($url_to_shorten) . '", "' . time() . '", "' . sqlite_escape_string($_SERVER['REMOTE_ADDR']) . '")'; $res = $db->query($q); $shortened_url = getShortenedURLFromID($new_id); } // Output the shortened url die(SHORTENER_BASE_HREF . $shortened_url); } // if empty() && preg_match() } // if _REQUEST[longurl] // Requesting a short url to be redirected to long_url if ($_REQUEST['u']) { if (!preg_match('|^[0-9a-zA-Z]{1,6}$|', $_GET['u'])) { header('Location: ' . FALLBACK_URL); exit; } $shortened_id = getIDFromShortenedURL($_GET['u']); $q = 'SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . sqlite_escape_string($shortened_id) . '"';