Jump to content

Image Grabber - Is this well written?


spadez

Recommended Posts

Hi,

 

I found this code online. I currently doesnt work but I think it probably just needs setting up / tweaking. Here is a link to the code online:

 

www.zombiemod.com/wip/wip/test2.php

 

Here is the code:

<?

/* IMAGE GRABBER SCRIPT */
/* By Dan Silvers 	*/
/* dan@moose.cc		*/
/* version .02		*/

/*
This is really terrible looking code, but whatever.
It's still useful... email me bugs/crap/comments/etc.
*/


/***********************************/
/****	EDIT THESE THREE LINES 	****/
/***********************************/

/* YOU MUST HAVE TRAILING SLASHES OR THIS SCRIPT WILL MAKE FUNNY LINKS */

$DUMP = "/home/you/somewhere/yes";		// Full pathname to the directory to dump the images
					// CHMOD this directory 777 so we can write to it.

$URL  = "http://www.yourdomains.com";		// URL to wherever to dump the files

$TYPES = "jpg|gif|png|jpeg";			// File types to be allowed to grab. Separate by a pipe ('|'). 
					// Not case sensitive.

/********************************************************************************/


/* Timer class and functions */
/* I stole this from somewhere, but I forget where. */

class Timer {
    var $startTime,
        $endTime,
        $timeDifference;

    function start() {
        $this->startTime = $this->currentTime();
    }

    function finish() {
        $this->endTime = $this->currentTime();
    }

    function getTime() {
        $this->timeDifference = $this->endTime - $this->startTime;
        return round($this->timeDifference, 5);
    }

    function currentTime() {
        list($usec, $sec) = explode(' ',microtime());
        return ((float)$usec + (float)$sec);
    }

}

$timer = new Timer();
$timer->start();

print	'
<html>
<head>
<title>Image Grabber</title>
</head>
        <style type="text/css">
        body {  color: white;
                background-color: black ;
                font-family: "Arial", serif }
        table   { border-collapse: collapse; }
        img {border: 0;}
        A:link {text-decoration: none; color: green}
        A:visited {text-decoration: none; color: green}
        A:active {text-decoration: none; color: green}
        A:hover {text-decoration: none; color: green;}
        A.g:link {text-decoration: none; color: red}
        A.g:visited {text-decoration: none; color: red}
        A.g:active {text-decoration: none; color: red}
        A.g:hover {text-decoration: none; color: red;}
        A.y:link {text-decoration: none; color: orange}
        A.y:visited {text-decoration: none; color: orange}
        A.y:active {text-decoration: none; color: orange}
        A.y:hover {text-decoration: none; color: orange;}
        A.e:link {text-decoration: none; color: gray}
        A.e:visited {text-decoration: none; color: gray}
        A.e:active {text-decoration: none; color: gray}
        A.e:hover {text-decoration: none; color: red;}
</style>
<body>
Put image URLs in here, one per line. <font color="gray">(jpg,jpeg,png,gif)</font><br>
<form action="' . $_SERVER['PHP_SELF'] . '" method="post"><br>
<textarea name="urls" rows="10" cols="80"></textarea> <p>
<input type="submit" value="Go!">
</form><p>';

/* If 'urls' is set, the user submitted some urls. */

if (isset($_POST['urls']))
{
$urlsFresh = explode("\n",$_POST['urls']);			// Seperate and count the URLs
$urls = array();
foreach($urlsFresh as $url)			// Make sure each url is at least 5 chars
{
	if (strlen($url) > 5)
		$urls[] = $url;
}

$num = count($urls);

print "Attempting to grab $num URLs....<p>";

$item = 0;
$alreadyCounter = 0;
if (count($urls) > 0)
{
    foreach($urls as $url)
    {

	$url = trim($url); 				// Trim off whitespace at the end of the url
	$item++;
	if (eregi("^.*\.(" . $TYPES . ")$", $url))
	{

		$filename = explode("/",$url);		// Break the filename up into chunks
		$fn = $filename[count($filename) - 1];
		$fn_chunks = explode(".",$fn);

		$fileA = "";				// FileA is the first part, ex. foo of foo.bar
		$ch = 0;

		foreach($fn_chunks as $chunk)
		{
			if ($ch != ( count($fn_chunks) - 1) )
				$fileA .= $chunk;
			else
				$fileB = $chunk;
			$ch++;
		}

		$c = 1;
		$fileAsave = $fileA;
							// Check and see if the file exists
							// If it does, rename the file and check again.

		while (file_exists($DUMP . $fileA . "." . $fileB))
		{
				$fileA = $fileAsave . $c;
				$c++;
		}

		$fn = $fileA . "." . $fileB;		// Final filename.

		/* If $c is > 0, we renamed this file */

		if ($c > 1)
		{
			print $item . ".) " . "<a class=\"y\" href=\"" . $url . "\">" . $url . "</a><br>"; 
			$already[$alreadyCounter][0] = $url;
			$already[$alreadyCounter][1] = $URL . $fn;
			$alreadyCounter++;
		}
		else
			print $item . ".) " . "<a href=\"" . $url . "\">" . $url . "</a><br>"; 


		/* write the file to disk */

		$f = fopen($DUMP . $fn,"w");

		$curl = curl_init($url);

		curl_setopt ($curl, CURLOPT_FILE, $f);
		curl_setopt ($curl, CURLOPT_HEADER, 0);
		$curl_result = curl_exec($curl);

		curl_close($curl);
		fclose($f);

		/* Save the file to the valid array */

		$valid[] = $URL . $fn;

	}

		/* If we end up here we have an invalid filename */
	else
	{

		print $item . ".) " . "<a class=\"g\"  href=\"" . $url . "\">" . $url . "</a> " . ereg("^.*\.(jpg|gif|png|jpeg)$", $url) . "<br>"; 
		$invalid[] = $url;
	}
   }
}

}

/* Print out the results */

if(count($valid) > 0)
{
print '<p> <p>I uploaded the following files:<p>' .
	'<textarea name="urls" rows="' . ( $num + 1 ) . '" cols="80">';
foreach($valid as $v)
{
	print "[img=" . $v . "]\n";
}
print '</textarea><br>' .
                '<textarea name="urls2" rows="' . ( $num + 1 ) . '" cols="80">';
        foreach($valid as $v)
        {
                print $v . "\n";
        }
        print '</textarea><br>' .
                '<textarea name="urls2" rows="' . ( $num + 1 ) . '" cols="80">';
        foreach($valid as $v)
        {
                print "<a href=\"" . $v . "\">" . $v . "</a>\n";
        }
        print '</textarea><p> <p>';

}

if (count($invalid) > 0)
{
print "<p>I could not upload these files:<br>";
$c = 1;
foreach($invalid as $v)
{
	print $c . ".) " . "<a class=\"g\" href=\"" . $v . "\">" . $v . "</a><br>\n";
	$c++;
}
print "<p> <p>";
}

if (count($already) > 0)
{
print "These files already existed, but I renamed them:<br>";
$c = 1;
foreach($already as $v)
{
	print $c . ".) <a href=\"" . $v[0] . "\">" . $v[0] . "</a><br>" .
	 	"      to   <a class=\"y\" href=\"" . $v[1] . "\">" . $v[1] . "</a><p>";
	$c++;
}
}

if(!file_exists($DUMP . "grabberdata.txt"))
{
$grabCount = 0;
$grabDate = time();
}
else
{
$data = explode("\n",file_get_contents($DUMP . "grabberdata.txt"));
$grabDate = $data[0];
$grabCount = $data[1];
}

$grabCount += count($valid);

$handle = fopen($DUMP . "grabberdata.txt","w");
fwrite($handle, $grabDate . "\n" . $grabCount);
fclose($handle);


/* Print out some pretty data on the footer */

$timer->finish();


print 	"<p> <p><font size=1 color=\"gray\">" .
"parsed in " . $timer->getTime() . " seconds. grabbed " . $grabCount . " urls since " . date("m/d/y",$grabDate) . "<br>" . 
"<a class=\"e\" href=\"http://moose.cc/scripts/grabber/\">image grabber</a> v.02 by <a class=\"e\" href=\"mailto:dan@moose.cc\">dan silvers</a>" .
"</body></html>";

?>

 

My question is though. I aim to use this script to build upon. However, Id like to know if this script is well coded, because I dont want to start work on a bad base. If possible, could someone have a quick scan through the code and tell me if it is to a high standard or if I should look for another script online.

 

Thank you!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.