Skip to content

Instantly share code, notes, and snippets.

@jakebathman
Created January 16, 2021 05:39
Show Gist options
  • Save jakebathman/5c48af8380d1f9930c47993b6d6f5be9 to your computer and use it in GitHub Desktop.
Save jakebathman/5c48af8380d1f9930c47993b6d6f5be9 to your computer and use it in GitHub Desktop.

Revisions

  1. jakebathman created this gist Jan 16, 2021.
    52 changes: 52 additions & 0 deletions interview.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?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);
    }