Tag Archives: PHP

Extracting a Zip file with PHP

I recently had to make a quick script to extract the contents of a zip file, this zip file is copied via ftp from a remote server and stored locally. The file name to request and transfer is set via URL Encoding. This were originally a function, which I converted into a class and now devolved totally for this small script.

Sometimes quick and dirty tasks, require quick and dirty code, using the class would have been overkill. As usual there are a dozen ways to do this, and you could remove the error checks and echo’s to make the code even more compact.

// get the absolute path to $file ($file is set via the transfer process, but is JUST the file name no extension).
$path = pathinfo(realpath($file.'.zip'), PATHINFO_DIRNAME);
$link = $path.'/'.$file.'.zip';
$linkz = $path.'/'.$file.'.csv';
// assuming file.zip is in the same directory as the executing script.
	if (file_exists($linkz)) {
		die("<p>The .csv file exists, exit processs.</p>");
	} 
	if(file_exists($link)){
		echo "<p>The $link exists, and $linkz file doesn't exist.</p>";		
		//prepare for handling a zip file.
		$zip = new ZipArchive; 
		// load the zip file.
		$res = $zip->open($file.'.zip');
		if ($res === TRUE) { // check the file has been loaded.
			echo "<p>Extracting File...</p>";
			$zip->extractTo($path); //begin extracting file
			$zip->close(); //unloa and release the zip file.
			echo "<p>$file.zip extracted to $path</p>";
		} else {
			echo "<p>Doh!, stupid $file.zip wouldn't open.</p>";
		}
	}else{

		die("<p>$file.zip doesn't exist, exit process.</p>");

	}

Its a little messy, but were procedural code, converted into a function, then part of a class and back to a procedural layout.

.UK Family Whois Splitter

I were looking for a snippet of code for someone when I came across an old tool. This isn’t a parser or a tool like phois or similar, its just a usable snippet of code to do a job. Really I should update it, and maybe use a function to handle the job, but it works just fine for its use.

The job in this instance were to monitor my domains for changes in status, in a few situations.

1, When a domain were sold, I monitored the tag and registrant name (now I’m using the DAC I only monitor the tag for .uk).
2, When using another registrar/registry I monitored expiry to avoid losing domains due to non-renewal or errors.

In order to do this, I needed code to (1), connect to a whois server (this is easy),  (2), split the required bits of info from the result (not so easy) and (3), place it in a database (easy). Then work on the data from there, (4), load the “watched”, “sold” or “close to expiry” domains, (5), scan the whois and compare, then (6), act on the result.

90% of the various registries whois outputs, out there are fairly uniform, but much like Nominets EPP, their whois is non-standard, so I needed a custom splitter. I’m not sure I would do it the same if I wrote the code now… some years later.

I have updated the code a little to include a ROFR (Right of First Refusal) detection, this won’t be needed come june (2019) when the period expires.

switch(trim($ext)){
	case "uk":
		$def = array();
		if(preg_grep("/Right of reg/", $arr)){
			$def['domain'] = $arr[1];
			$def["rofr"] = $arr[3]; 
			$type = "rofr";			
			break;	
		}

		$def['domain'] = $arr[1];
		$def['registrant'] = $arr[3]; 

		preg_match('/Tag = (.*)]/', $res, $matches);
		$def['tag'] = trim($matches[1]);
			
		preg_match("/Registered on: (.*)/", $res, $matches);
		$def['created'] = date("Y-m-d",strtotime(trim($matches[1])));
			
		preg_match("/Last updated: (.*)/", $res, $matches);
		$def['updated'] = date("Y-m-d",strtotime(trim($matches[1])));
				
		preg_match("/Expiry date: (.*)/", $res, $matches);
		$def['expiry'] = date("Y-m-d",strtotime(trim($matches[1])));

		$type = "uk";	
		break;

	default:
		break;
}

This will produce an array called $def, which when calling a valid name would look like…

Array
(
    [domain] => steven.uk
    [registrant] => Steve Morley
    [tag] => MORLEY
    [created] => 2014-06-12
    [updated] => 2016-02-29
    [expiry] => 2024-06-12
)

or if the the .UK has right of first registration intact…

Array
(
    [domain] => steven.uk
    [rofr] => steven.co.uk
)

Working from this array, you can you can put these into a database, compare them to the database return and act on it from there. I suggest sending yourself an email or text message (api’s are easy to follow).

Querying The Dac with PHP

I see the same thing over and over, and some people imply or suggest its easy or incredibly hard to connect and query/poll the Nominet DAC (Domain Availability Checker) to get basic data about a domain.

There are a few reasons you would want to do this, the main ones are to build a drop list database or build a drop catching script, but both are built on the same foundations.

Before you can use this, you need to be a Nominet Member, and have a DAC Subscription, but you can access the DAC Testbed for Free without Membership or Subscription.

I’m going to use a high level language for this example, which in this case is PHP, but Perl is faster, and a low level compiled language like C would be way way quicker, but PHP is more than adequate for database building and non-prime drop catching.

Basic Connect to and Poll the Dac

<?php 
$sock = socket_create(AF_INET, SOCK_STREAM, 0); 
echo "Socket Created.<br>";
socket_connect($sock , 'dac.nic.uk' , 3043); 
//change dac.nic.uk to testbed-dac.nominet.org.uk to access testbed.
echo "Socket Connected.<br>";
socket_send ($sock, "steven.co.uk\r\n", 16, 0);
echo "Message Sent.<br />";
$resp = socket_read($sock, 1000);
echo "Response: $resp.  <br />";
socket_send ($sock, "#exit\r\n", 9, 0);
echo "Dac Session Exit Sent.<br />";
socket_shutdown($sock, 2);
echo "Socket Shutdown.<br />";
socket_close($sock);
echo "Socket Closed.<br />";
?>

The above code will result in the following output, it really is as simple a half a dozen lines of code,.

Socket Created.
Socket Connected.
Message Sent.
Response: steven.co.uk,Y,N,1998-08-18,2022-08-18,MORLEY.
Dac Session Exit Sent.
Socket Shutdown.
Socket Closed.

You can now act upon the returned $resp variable, explode it into manageable chunks like…

$dac = split(",",$resp);
echo "Split Dac. <br />";

This will return an array of 6 blocks numbered 0-5, which will be…

Array(
    [0] => steven.co.uk
    [1] => Y
    [2] => N
    [3] => 1998-08-18
    [4] => 2022-08-18
    [5] => MORLEY
)

From here you can put it into a database…

Query a List of Names

Its most likely you would want to add some sort of loop to load a list of names…

<?php 
$arrList = array("steven.uk", "steven.co.uk");
$sock = socket_create(AF_INET, SOCK_STREAM, 0); 
socket_connect($sock , 'dac.nic.uk' , 3043); 

foreach($arrList as $message) {
 socket_send ($sock, $message . "\r\n", len($message)+4, 0);
 $resp = socket_read($sock, 1000);
 echo $resp;
}

socket_send ($sock, "#exit\r\n", 9, 0);
socket_shutdown($sock, 2);
socket_close($sock);

?>

Thats the basics covered where most people seem to strugle, its really endless where you can take a script.

I may revisit this code in future and expand on it, but for now, lets see what you do with it.