The idea that you can pass the location from your address bar to a script on the web, and then have that script interact with that page fascinates me in all kinds of ways. I just cobbled together a complementary script to my Pikachoors system so I can grab all the images from a page, list them on a new page, and pick the one I wanted. (Right now it's only keyed to images, but it'd be easy enough to sniff out links and backgrounds.) Bookmarklets are awesome!
<?php
// Error messages
function PikachoorError($msg = null) {
if ($msg != null) {
echo('ERROR: ' . $msg);
exit;
}
else {
PikachoorError('No error message passed.');
}
}
function FetchPage($url = null) {
if ($url == null) {
PikachoorError("No url passed to FetchImage...");
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$html = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
return $html;
}
else {
PikachoorError("Page not found...");
}
}
function CheckImage($url = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1); // get the header
curl_setopt($ch, CURLOPT_NOBODY, 1); // and *only* get the header
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status != 200) {
return false;
}
if (!preg_match("/image/", $type)) {
return false;
}
return true;
}
function AbsImage($img = null, $url = null) {
$startPos = strpos($url, "://") + 3;
$uParts = split("/", substr($url, $startPos));
$iParts = split("/", $img);
$tParts = array();
$tcnt = 1;
$turl = "";
foreach ($iParts as $i) {
if ($i == "..") {
$tcnt++;
}
else if ($i != ".") {
$tParts[] = $i;
}
}
for($i=0; $i < (count($uParts) - $tcnt); $i++) {
$turl .= $uParts[$i] . '/';
}
return $turl . implode('/', $tParts);
}
// Main
$url = isset($_GET['url']) ? htmlspecialchars(stripslashes($_GET['url'])) : null;
$x = array();
$y = array();
if ($url == null)
{
PikachoorError("No url...");
}
preg_match_all('/src="(.*)"/', FetchPage($url), $x);
foreach ($x[1] as $z)
{
if (strpos($z, '"') != false) {
$z = substr($z, 0, strpos($z, '"'));
}
if (substr($z, 0, 4) != "http") {
$z = AbsImage($z, $url);
}
if (CheckImage($z)) {
$y[] = $z;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<title>Pikachoors in a page</title>
</head>
<body>
<?php foreach($y as $image) { ?>
<p>
<img src="<?php echo($image); ?>" alt="<?php echo($image); ?>">
<br>
<a href="http://pics.mytrapster.com/wp-pikachoor-post.php?url=<?php echo($image); ?>">Add This Image</a>
</p>
<?php } ?>
</body>
</html>
|