<?php

require_once $settings['functions'] . 'function.sanitize.maybe_binary_to_hex.php';
////	$_GET['info_hash']
// IF 20 Character Binay, convert to 40 Character Hex
// OR 40 Character Hex
// THEN Sanatize
$peer['info_hash'] = false;
$peer['info_hashes'] = array();
$params = explode('&', $_SERVER['QUERY_STRING']);
foreach ($params as $param) {
    $param = explode('=', $param, 2);
    if ($param[0] == 'info_hash') {
        $peer['info_hashes'][] = maybe_binary_to_hex($param[1]);
    }
}
if (!empty($peer['info_hashes'][0])) {
    $peer['info_hash'] = $peer['info_hashes'][0];
}
////	$_GET['peer_id']
// IF 20 Character Binay, convert to 40 Character Hex
// OR 40 Character Hex
// THEN Sanatize
$peer['peer_id'] = false;
if (isset($_GET['peer_id'])) {
    $peer['peer_id'] = maybe_binary_to_hex($_GET['peer_id']);
}
$binary = 'whyonearthwouldidoth';
$result = maybe_binary_to_hex($binary);
$length = strlen($result);
if ($length != 40) {
    echo 'String length is wrong for test 1, it is ' . $length . PHP_EOL;
    $failure = true;
}
// 40 hex gives sanitized 40 hex
$binary = '7768796f6e6561727468776f756c6469646f7468';
$result = maybe_binary_to_hex($binary);
$length = strlen($result);
if ($length != 40) {
    echo 'String length is wrong for test 2, it is ' . $length . PHP_EOL;
    $failure = true;
}
// other length gives "false"
$binary = '!"£$%^&*()_+-={}[]:@~;\'#';
$result = maybe_binary_to_hex($binary);
if ($result) {
    echo 'Positive result returned for test 3.' . PHP_EOL;
    $failure = true;
}
// variables may be url encoded
// should return string(40) "fce720af722a813a184c5550a924aaa60a8d9af1"
$binary = '%fc%e7%20%afr%2a%81%3a%18LUP%a9%24%aa%a6%0a%8d%9a%f1';
$result = maybe_binary_to_hex($binary);
var_dump($result);
if (!$result) {
    echo 'Positive result returned for test 4.' . PHP_EOL;
    $failure = true;
}