rewrite($url);
if ($newUrl !== FALSE)
{
return $newUrl;
}
}
return $url;
}
load_modules(array("mod_google"));
/* Set all the GET variables below */
if (isset($_GET['url']))
{
$url = rewrite_url(properizeURL(urldecode($_GET['url'])));
}
if (isset($_GET['history']))
{
$historyView = TRUE;
}
if ($historyView && isset($_GET['startDate']) &&
isValidDate(urldecode($_GET['startDate'])))
{
$versionsBefore = urldecode($_GET['startDate']);
}
if (isset($_GET['tosticky']))
{
$toSticky = TRUE;
}
/* This is the difference (in seconds) between the host's and MySql Database's
* timestamp. MySql server is assumed to be ahead.
*/
$timeDiff = 0;
$prefix = getPagePrefix();
$hrefData = "javascript:(function(){ x=document.location; " .
"document.location='http://" . $_SERVER['HTTP_HOST'] .
"/stickylinks/?snapshot=1&tosticky=1&url='+escape(x);})();";
?>
StickyLinks: Let others see exactly what you see on a page |
| Create Bookmarklet: Stickify |
\n";
global $error, $verror, $noData, $toSec;
/* Note: DO NOT add quotes to the URL passed to wget since we are
* already calling escapeshellcmd().
*/
$wgetCommand = "nohup wget " . escapeshellarg($url) .
" -k -U \"Firefox/2.0.0.20\" -O \"$targetPath\" &> /dev/null & echo $!";
// echo "WGET COMMAND: $wgetCommand
\n";
// return FALSE;
$wgetPID = trim(shell_exec($wgetCommand));
// echo "WGETPID: $wgetPID
\n";
$wgetAlive = TRUE;
for ($i = 0; $i < $toSec && $wgetAlive; ++$i)
{
sleep(1);
$wgetAlive = file_exists("/proc/$wgetPID");
// echo ($wgetAlive?"YES\n":"NO\n");
}
if ($wgetAlive === TRUE)
{
$error = TRUE;
$verror = "Either the file is too big OR the server took too long to respond";
shell_exec("kill $wgetPID");
unlink($targetPath);
return FALSE;
}
$so = file_get_contents($targetPath);
if ($so === FALSE || strlen($so) === 0)
{
$noData = TRUE;
unlink($targetPath);
return FALSE;
}
unlink($targetPath);
return $so;
}
$conn = db_connect_select();
if ($conn === FALSE)
{
$error = TRUE;
$verror = mysql_error();
}
$res = query_all_rows_assoc("SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) as serverTime", $conn);
if ($res === FALSE || sizeof($res) < 1)
{
$error = TRUE;
$verror = mysql_error();
}
$timeDiff = $res[0]['serverTime'] - time();
while ($url !== FALSE && !$error)
{
if (strlen(trim($url)) == 0)
{
$noData = TRUE;
break;
}
if (startsWith($url, "http://") === TRUE ||
startsWith($url, "https://") === TRUE ||
startsWith($url, "ftp://") === TRUE)
{ }
else
{
$url = "http://" . $url;
}
$qval = $url;
$revUrl = mysql_real_escape_string(strrev($url));
$urlSet = getURLSetForINQuery($revUrl);
if ($historyView === TRUE)
{
break;
}
/* Check if a version of this page up to $delta seconds ago exists or not */
$q = "SELECT UNIX_TIMESTAMP(created) AS created, exposedUrlKey, title
FROM links
WHERE created >= FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - $delta) AND
realUrl IN ($urlSet)
ORDER BY created DESC
LIMIT 1";
// echo "QUERY: $q
\n";
$rows = query_all_rows_assoc($q, $conn);
if ($rows === FALSE)
{
$error = TRUE;
$verror = mysql_error();
break;
}
if (sizeof($rows) > 0)
{
$existingKey = $rows[0]['exposedUrlKey'];
$created = $rows[0]['created'];
$title = $rows[0]['title'];
break;
}
/* We were unable to find an existing key for this URL. Get
* the URL using wget.
*/
$targetPath = tempnam("/tmp", "WEBH");
if ($targetPath === FALSE)
{
$error = TRUE;
$verror = "Could not create temporary file";
break;
}
$numWgetProcs = intval(shell_exec("ps aux | grep 'wget' | wc -l"));
if ($numWgetProcs > $MAX_WGET_PROCS)
{
$error = TRUE;
$verror = "Too many simultaneous requests. Back off buster!!";
break;
}
// echo "downloading from: " . escapeshellcmd($url) . "
\n";
// echo "SAVING to: $targetPath
\n";
/* Check if we have reached our write limit */
$count = updateAndGetReadsOrWrites(time(), $FOR_WRITE, $conn);
if ($count === FALSE)
{
$error = TRUE;
$verror = "Unable to update counter.";
break;
}
if ($count > $MAX_WRITES)
{
$error = TRUE;
$verror = "Too many requests. Please try again in a few minutes.";
break;
}
$so = downloadFile($url, $targetPath);
if ($so === FALSE && canAddTrailingSlash($url))
{
/* Try downloading the file again. This time, with the
* trailing slash.
*/
$url .= "/";
$so = downloadFile($url, $targetPath);
}
if ($so === FALSE)
{
/* $error, $verror and $noData have already been set by
* the callee.
*/
break;
}
$qval = $url;
/* INSERT the data into the table with a new key */
$existingKey = uniqid(md5($url), TRUE);
$escapedExistingKey = mysql_real_escape_string($existingKey);
$so = mysql_real_escape_string($so);
$title = html_entity_decode(getPageTitle($so, $url));
$escapedTitle = mysql_real_escape_string($title);
/* We assume that the key above is truly unique, and hope for the best!! */
$q = "INSERT INTO links (realUrl, exposedUrlKey, pageData, title)
VALUES('$revUrl', '$escapedExistingKey', '$so', '$escapedTitle')";
if (mysql_query($q, $conn) == FALSE)
{
$error = TRUE;
$verror = mysql_error();
break;
}
$created = time() + $timeDiff;
$addedSticky = TRUE;
break;
} // while()
$showedStuff = TRUE;
if ($error === TRUE)
{
echo "Sorry, we weren't able to process your request. Please try again later.
\n";
echo "Reason: $verror
\n";
$stickyCreationError = TRUE;
}
else if ($noData === TRUE)
{
echo "The URL you specified either was an invalid URL or contained no data.
\n";
$stickyCreationError = TRUE;
}
else if ($existingKey !== FALSE)
{
$sharingTitle = $title . " on " . getFormattedTimeStamp($created);
$sharingUrl = "http://" . $_SERVER['HTTP_HOST'] .
versionURLFromKey($prefix, $existingKey);
echo "The URL for this page as it looked " .
relativeTime($created - $timeDiff) . " is: " .
versionLinkHTMLFromKey($prefix, $existingKey, "here") . " " .
generateSharingHTML($sharingUrl, $sharingTitle) . "
\n";
}
else
{
$showedStuff = FALSE;
$stickyCreationError = TRUE;
}
if (!$showedStuff)
echo "
\n";
$rows = FALSE;
/* Try to show the last 10 versions of this URL. We only bother about
* DB connection errors here.
*/
if ($conn !== FALSE && $url !== FALSE && $toSticky === FALSE)
{
$error = FALSE;
$q = "SELECT UNIX_TIMESTAMP(created) AS created, exposedUrlKey
FROM links
WHERE realUrl IN ($urlSet)
AND created <= str_to_date('$versionsBefore', '%c/%e/%Y')
ORDER BY created DESC
LIMIT 7";
// echo "QUERY: $q
\n";
$rows = query_all_rows_assoc($q, $conn);
if ($rows === FALSE)
{
$error = TRUE;
}
}
if ($error === FALSE && $conn !== FALSE &&
$rows !== FALSE && sizeof($rows) > 0 &&
$toSticky === FALSE)
{
$showedStuff = TRUE;
$tableWidth = 7*sizeof($rows) + 4;
?>
Past Versions of the page:
".versionLinkHTMLFromKey($prefix, $linkKey,
relativeTime($linkCreated - $timeDiff)) .
"\n";
} // foreach()
} // if()
?>
\n";
}
else if ($historyView)
{
echo "Sorry, we don't have any history for this URL. You can create a" .
" version now by clicking the 'Stickify' button below.
";
echo "
\n";
}
?>
$MAX_RECENT_LINKS recently created Stickies
\n\n";
foreach ($rows as $row)
{
echo "| " . versionLinkHTMLFromKey($prefix, $row['exposedUrlKey'],
shortenText($row['title'], 50)) .
" | " .
relativeTime($row['created'] - $timeDiff) . " | |
\n";
}
echo "
\n";
/* The break; below is _essential_!! */
break;
}
if ($toSticky && !$stickyCreationError)
{
ob_clean();
// echo "URL: $url";
header("Location: http://" . $_SERVER['HTTP_HOST'] .
versionURLFromKey($prefix, $existingKey));
exit(0);
}
else
{
ob_end_flush();
}
?>