Tahoe-LAFS repair.php

This script is a simple repair script that lets a visitor input a gateway URL, base64-encoded Tahoe-LAFS URI, or plaintext Tahoe-LAFS URI, and will then check and repair (if necessary) the file on the locally connected Tahoe-LAFS grid. It returns the JSON object provided by Tahoe-LAFS.

Requirements

  • PHP
  • cURL (+ php-curl extension)
  • PHP-JSON support

Demo

The script can be seen in action at http://cryto.net/tahoe/repair.php (keep in mind this copy only works for the Cryto Tahoe-LAFS Storage Grid).

License

This script is licensed under the WTFPL, and as such can be used freely without the need for explicit permission, and free of royalties or licensing costs. It can be used for both commercial and non-commercial applications, and attribution is not required. In other words: you can do with it whatever the fuck you want to.

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
                    Version 2, December 2004 

 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> 

 Everyone is permitted to copy and distribute verbatim or modified 
 copies of this license document, and changing it is allowed as long 
 as the name is changed. 

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 

  0. You just DO WHAT THE FUCK YOU WANT TO. 

Source code

repair.php
<!doctype html>
<html>
	<head>
		<title>Cryto Tahoe-LAFS Storage Grid repair utility</title>
		<style>
			pre
			{
				max-height: 300px;
				overflow: auto;
				background-color: #E7E7E7;
				padding: 12px;
			}
		</style>
	</head>
	<body>
		<?php
 
		function curl_post($url, $variables)
		{
			if(is_array($variables))
			{
				foreach($variables as $key => $value)
				{
					$variables[$key] = urlencode($value);
					$variable_strings[] = "{$key}={$value}";
				}
 
				$post_string = implode("&", $variable_strings);
			}
			else
			{
				$post_string = "";
			}
 
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POST, count($variables));
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
 
			$result = curl_exec($ch);
 
			$error = curl_error($ch);
			if(empty($error))
			{
				$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
				$return_object->result = $result;
				$return_object->code = $code;
				return $return_object;
			}
			else
			{
				return false;
			}
 
			curl_close($ch);
		}
 
		if(isset($_POST['submit']))
		{
			$uri = $_POST['uri'];
			$process = true;
 
			// process repair request
			if(preg_match("/http:\/\/[^\/]+\/download\/([^\/]+)(\/.*)?/i", $uri, $matches))
			{
				// gateway url
				$uri = $matches[1];
			}
 
			if(!preg_match("/^[a-zA-Z0-9=_:]+$/i", $uri))
			{
				$process = false;
				echo("ERROR: Your input was in an invalid format.<br><br>");
			}
 
			if(strpos($uri, ":") === false)
			{
				// probably base64
				$uri = base64_decode($uri);
 
				if($uri === false)
				{
					$process = false;
					echo("ERROR: Your input was invalid. Check if it's really a valid Tahoe-LAFS gateway URL, base64-encoded URI, or plaintext URI.<br><br>");
				}
			}
 
			if($process === true)
			{
				$result = curl_post("http://localhost:3456/uri/{$uri}?t=check&repair=true&output=json", false);
 
				if($result->code == 200)
				{
					$result_object = json_decode($result->result);
 
					$attempted = $result_object->{'repair-attempted'};
 
					if($attempted)
					{
						$succeeded = $result_object->{'repair-successful'};
					}
					else
					{
						$succeeded = false;
					}
 
					if($attempted === false)
					{
						echo("<strong>The file is still healthy, and does not need to be repaired.</strong>");
					}
					elseif($attempted === true && $succeeded === false)
					{
						echo("<strong>Repair of the file was attempted, but FAILED.</strong>");
					}
					else
					{
						echo("<strong>Repair of the file was SUCCESSFULLY completed.</strong>");
					}
 
					echo("<pre>{$result->result}</pre>");
				}
				else
				{
					echo("ERROR: Repair failed. Your input may be in the wrong format or the server may be down.<br><br>");
				}
			}
		}
		?>
 
		<form method="post" action="repair.php">
			URI/base64/URL: <input type="text" name="uri">
			<button type="submit" name="submit" value="submit">Repair!</button>
		</form>
	</body>
</html>
projects/tahoe-lafs/repair.php.txt · Last modified: 2012/03/27 21:13 by joepie91
Back to top
CC Attribution 3.0 Unported
Donate Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0