<commit_before><?php
require_once('phpws/websocket.client.php');

new SMSInterceptor();

class SMSInterceptor {

    protected $socketServer = '172.16.17.32:8080';

    public function __construct()
    {
        header("Access-Control-Allow-Orgin: *");
        header("Access-Control-Allow-Methods: *");
        header("Content-Type: application/json");

        switch($_SERVER['REQUEST_METHOD'])
        {
            case 'POST':
                $this->post($this->cleanInputs($_POST));
                break;
            case 'GET':
            case 'PUT':
            case 'DELETE':
            default:
                $this->response('Invalid method', 405);
                break;

        }
    }

    // Get the data we need, then send it via JSON to the web sockets server.
    private function post($data)
    {
        // Only interested in two fields: content, from.
        $clockwork_request = array(
            'operation' => 'SMS',
            'type' => 'incoming',
            'recipient' => $data['to'],
            'message' => $data['content'], // TODO: Cleanup the input, at the moment we're just chuking it through
            'sender' => $data['from'],
            'id' => $data['msg_id']
        );

        $msg = WebSocketMessage::create(json_encode($clockwork_request));
        $client = new WebSocket('ws://' . $this->socketServer);
        $client->open();
        $client->sendMessage($msg); // Fire off message to socket server.
        $client->close();

        echo $this->response('Message passed onto the socket server');
    }

    // Clean up data BEFORE we use it
    private function cleanInputs($data)
    {
        $clean_input = Array();

        if (is_array($data)) {
            foreach ($data as $k => $v) {
                $clean_input[$k] = $this->cleanInputs($v);
            }
        } else {
            $clean_input = trim(strip_tags($data));
        }

        return $clean_input;
    }

    private function explainStatus($errorCode)<commit_msg>added validating to SMS handler, don't bova sending stuff upstream if it isn't a valid message<commit_after><?php
require_once('phpws/websocket.client.php');

new SMSInterceptor();

class SMSInterceptor {

    protected $socketServer = '172.16.17.32:8080';

    public function __construct()
    {
        header("Access-Control-Allow-Orgin: *");
        header("Access-Control-Allow-Methods: *");
        header("Content-Type: application/json");

        switch($_SERVER['REQUEST_METHOD'])
        {
            case 'POST':
                $this->post($this->cleanInputs($_POST));
                break;
            case 'GET':
            case 'PUT':
            case 'DELETE':
            default:
                $this->response('Invalid method', 405);
                break;

        }
    }

    private function validateMessageContent($content)
    {
        // Validate that date, only accept certain commands
        $message = explode(' ', $content);

        // Check only command + argument
        if(count($message) < 0 && count($message) > 4)
            return false;

        $command = $message[0];

        // Check it's a valid command
        $possibleCommands = array('join', 'move', 'leave');
        if(!in_array($command, $possibleCommands))
            return false;

        return true;
    }

    // Get the data we need, then send it via JSON to the web sockets server.
    private function post($data)
    {
        if(!$this->validateMessageContent($data['content']))
            return $this->response('Message content not valid for our server, but thanks anyway.');

        // Only interested in two fields: content, from.
        $clockwork_request = array(
            'operation' => 'SMS',
            'type' => 'incoming',
            'recipient' => $data['to'],
            'message' => $data['content'], // TODO: Cleanup the input, at the moment we're just chuking it through
            'sender' => $data['from'],
            'id' => $data['msg_id']
        );

        $msg = WebSocketMessage::create(json_encode($clockwork_request));
        $client = new WebSocket('ws://' . $this->socketServer);
        $client->open();
        $client->sendMessage($msg); // Fire off message to socket server.
        $client->close();

        return  $this->response('Message passed onto the socket server');
    }

    // Clean up data BEFORE we use it
    private function cleanInputs($data)
    {
        $clean_input = Array();

        if (is_array($data)) {
            foreach ($data as $k => $v) {
                $clean_input[$k] = $this->cleanInputs($v);
            }
        } else {
            $clean_input = trim(strip_tags($data));
        }

        return $clean_input;
    }

    private function explainStatus($errorCode)
{'fraction_non_alphanumeric': 0.1398996235884567, 'fraction_numerical': 0.007319113341698034, 'mean_word_length': 2.0062853551225643, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5488074', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1277, 'n_words': 334}