Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$host = 'palindromer-bd7e0fc867d57915.elb.us-east-1.amazonaws.com';
$port = 7777;
$socket = fsockopen($host, $port);
if (! $socket) {
die('Error: connection not successful');
}
echo 'Processing lines ';
while ($line = fgets($socket)) {
echo '.';
// Divide the string into word chunks along spaces
$words = explode(' ', $line);
// Check for special line, denoting flag
if (stripos($line, '!!!') === 0) {
if (preg_match('/!!! flag\[(.*?)\]/i', $line, $matches)) {
$response = $matches[1] . "\n";
echo "\n\nFlag received: {$response}";
fputs($socket, $response);
return fclose($socket);
}
}
// Get the palindromes
$return = [];
foreach ($words as $word) {
if (isPalindromic($word)) {
$return[] = $word;
}
}
// Must respond with newline at the end
$response = trim(implode(' ', $return)) . "\n";
// Send response string
fputs($socket, $response);
}
function isPalindromic($word)
{
$word = trim($word);
return $word == strrev($word);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment