<commit_before><?php

namespace Phaxio;

class Phaxio
{
    private $debug = false;
    private $api_key = null;
    private $api_secret = null;
    private $host = "https://api.phaxio.com/v1/";

    public function __construct($api_key = null, $api_secret = null, $host = null)
    {
        $this->api_key = $api_key ? $api_key : $this->getApiKey();
        $this->api_secret = $api_secret ? $api_secret : $this->getApiSecret();
        if ($host != null) {
            $this->host = $host;
        }
    }

    public function faxList($startTimestamp, $endTimestamp, $options = array()){
        if (!$startTimestamp || !$endTimestamp) {
            throw new PhaxioException("You must provide a start and end timestamp. ");
        }

        $params = array('start' => $startTimestamp, 'end' => $endTimestamp);

        $this->paramsCopy(
            array('maxperpage', 'page', 'number'),
            $options,
            $params
        );

        if (is_array($options['tags'])){
            foreach($options['tags'] as $name => $value){
                $params["tag[$name]"] = $value;
            }
        }

        $result = $this->doRequest($this->host . "faxList", $params);
        return $result;
    }

    public function faxStatus($faxId)
    {
        if (! $faxId) {
            throw new PhaxioException("You must include a fax id. ");
        }

        $params = array('id' => $faxId);

        $result = $this->doRequest($this->host . "faxStatus", $params);
        return $result;
    }

    public function sendFax($to, $filenames = array(), $options = array())
    {
        if (! is_array($filenames)) {
            $filenames = array($filenames);
        }

        if (!$to) {
            throw new PhaxioException("You must include a 'to' number. ");
        } elseif (count($filenames) == 0 && !$options['string_data']) {
            throw new PhaxioException("You must include a file to send.");
        }

        $params = array();

        //setup the to parameter
        $to = (is_array($to) ? $to : array($to));
        $i = 0;
        foreach ($to as $toNumber) {
            $params["to[$i]"] = $toNumber;
            $i++;
        }

        $i = 0;
        foreach ($filenames as $filename) {
            if (! file_exists($filename)) {
                throw new PhaxioException("The file '$filename' does not exist.");
            }
            $params["filename[$i]"] = "@$filename";
            $i++;
        }

        $this->paramsCopy(
            array('string_data', 'string_data_type', 'batch', 'batch_delay', 'callback_url'),
            $options,
            $params
        );

        $result = $this->doRequest($this->host . "send", $params);

        return $result;
    }

    public function fireBatch($batchId)
    {
        if (! $batchId) {
            throw new PhaxioException("You must provide a batchId.");
        }

        $params = array('id' => $batchId);
        $result = $this->doRequest($this->host . "fireBatch", $params);

        return $result;
    }

    public function closeBatch($batchId)
    {
        if (! $batchId) {
            throw new PhaxioException("You must provide a batchId.");
        }

        $params = array('id' => $batchId);
        $result = $this->doRequest($this->host . "closeBatch", $params);

        return $result;
    }

    public function getApiKey()
    {
        return $this->api_key;
    }

    public function getApiSecret()
    {
        return $this->api_secret;
    }

    private function doRequest($address, $params, $wrapInPhaxioOperationResult = true)
    {
        $ch = curl_init($address);

        $params['api_key'] = $this->getApiKey();
        $params['api_secret'] = $this->getApiSecret();

        if ($this->debug) {
            echo "Request address: \n\n $address?" . http_build_query($params) . "\n\n";
        }

        $result = $this->curlPost($address, $params, false);

        if ($this->debug) {
            echo "Response: \n\n";
            var_dump($result);
            echo "\n\n";
        }

        if ($wrapInPhaxioOperationResult) {
            $result = json_decode($result, true);

            if (! $result) {
                $opResult = new PhaxioOperationResult(false, "No data received from service.");
            } else {
                $opResult = new PhaxioOperationResult($result['success'], $result['message'], $result['data']);

                if ($result['paging']){
                    $opResult->addPagingData($result['paging']);
                }
            }

            return $opResult;
        } else {
            return $result;
        }
    }

    private function curlPost($host, $params = array(), $async = false)
    {
        $handle = curl_init($host);
        curl_setopt($handle, CURLOPT_POST, true);

        if ($async) {
            curl_setopt($handle, CURLOPT_TIMEOUT, 1);
        } else {
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        }

        $this->curlSetoptCustomPostfields($handle, $params);
        $result = curl_exec($handle);

        if ($result === false) {
            throw new PhaxioException('Curl error: ' . curl_error($handle));
        }

        return $result;
    }

    private function paramsCopy($names, $options, &$params)
    {
        foreach ($names as $name) {
            if (isset($options[$name])) {
                $params[$name] = $options[$name];
            }
        }
    }

    private function curlSetoptCustomPostfields($ch, $postfields, $headers = null)
    {
        $algos = hash_algos();
        $hashAlgo = null;

        foreach (array('sha1', 'md5') as $preferred) {
            if (in_array($preferred, $algos)) {
                $hashAlgo = $preferred;
                break;
            }
        }
        if ($hashAlgo === null) {
            list($hashAlgo) = $algos;
        }
        $boundary =
                '----------------------------' .
                substr(hash($hashAlgo, 'cURL-php-multiple-value-same-key-support' . microtime()), 0, 12);

        $body = array();
        $crlf = "\r\n";
        $fields = array();
        foreach ($postfields as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $v) {
                    $fields[] = array($key, $v);
                }
            } else {
                $fields[] = array($key, $value);
            }
        }
        foreach ($fields as $field) {
            list($key, $value) = $field;
            if (strpos($value, '@') === 0) {
                preg_match('/^@(.*?)$/', $value, $matches);
                list($dummy, $filename) = $matches;
                $body[] = '--' . $boundary;
                $body[] = 'Content-Disposition: form-data; name="' . $key . '"; filename="' . basename($filename) . '"';
                $body[] = 'Content-Type: application/octet-stream';
                $body[] = '';
                $body[] = file_get_contents($filename);
            } else {
                $body[] = '--' . $boundary;
                $body[] = 'Content-Disposition: form-data; name="' . $key . '"';
                $body[] = '';
                $body[] = $value;
            }
        }
        $body[] = '--' . $boundary . '--';
        $body[] = '';
        $contentType = 'multipart/form-data; boundary=' . $boundary;
        $content = join($crlf, $body);
        $contentLength = strlen($content);

        curl_setopt(
            $ch,
            CURLOPT_HTTPHEADER,
            array(
                'Content-Length: ' . $contentLength,
                'Expect: 100-continue',
                'Content-Type: ' . $contentType,
            )
        );

        curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    }
}
<commit_msg>added functionality for phone numbers, account status, and supported countries<commit_after><?php

namespace Phaxio;

class Phaxio
{
    private $debug = false;
    private $api_key = null;
    private $api_secret = null;
    private $host = "https://api.phaxio.com/v1/";

    public function __construct($api_key = null, $api_secret = null, $host = null)
    {
        $this->api_key = $api_key ? $api_key : $this->getApiKey();
        $this->api_secret = $api_secret ? $api_secret : $this->getApiSecret();
        if ($host != null) {
            $this->host = $host;
        }
    }

    public function accountStatus() {
        $result = $this->doRequest($this->host . "accountStatus");
        return $result;
    }

    public function faxList($startTimestamp, $endTimestamp, $options = array()){
        if (!$startTimestamp || !$endTimestamp) {
            throw new PhaxioException("You must provide a start and end timestamp. ");
        }

        $params = array('start' => $startTimestamp, 'end' => $endTimestamp);

        $this->paramsCopy(
            array('maxperpage', 'page', 'number'),
            $options,
            $params
        );

        if (is_array($options['tags'])){
            foreach($options['tags'] as $name => $value){
                $params["tag[$name]"] = $value;
            }
        }

        $result = $this->doRequest($this->host . "faxList", $params);
        return $result;
    }

    public function faxStatus($faxId)
    {
        if (! $faxId) {
            throw new PhaxioException("You must include a fax id. ");
        }

        $params = array('id' => $faxId);

        $result = $this->doRequest($this->host . "faxStatus", $params);
        return $result;
    }

    public function resendFax($faxId) {
        if (!$faxId) throw new PhaxioException("You must include a fax id. ");

        $params = array('id' => $faxId);

        $result = $this->doRequest($this->host . "resendFax", $params);
        return $result;
    }

    public function sendFax($to, $filenames = array(), $options = array())
    {
        if (! is_array($filenames)) {
            $filenames = array($filenames);
        }

        if (!$to) {
            throw new PhaxioException("You must include a 'to' number. ");
        } elseif (count($filenames) == 0 && !$options['string_data']) {
            throw new PhaxioException("You must include a file to send.");
        }

        $params = array();

        //setup the to parameter
        $to = (is_array($to) ? $to : array($to));
        $i = 0;
        foreach ($to as $toNumber) {
            $params["to[$i]"] = $toNumber;
            $i++;
        }

        $i = 0;
        foreach ($filenames as $filename) {
            if (! file_exists($filename)) {
                throw new PhaxioException("The file '$filename' does not exist.");
            }
            $params["filename[$i]"] = "@$filename";
            $i++;
        }

        $this->paramsCopy(
            array('string_data', 'string_data_type', 'batch', 'batch_delay', 'batch_collision_avoidance', 'callback_url', 'caller_id', 'cancel_timeout','header_text'),
            $options,
            $params
        );

        if ($options['tags']){
            foreach($options['tags'] as $name => $value){
                $params["tag[$name]"] = $value;
            }
        }

        $result = $this->doRequest($this->host . "send", $params);

        return $result;
    }

    public function fireBatch($batchId)
    {
        if (! $batchId) {
            throw new PhaxioException("You must provide a batchId.");
        }

        $params = array('id' => $batchId);
        $result = $this->doRequest($this->host . "fireBatch", $params);

        return $result;
    }

    public function closeBatch($batchId)
    {
        if (! $batchId) {
            throw new PhaxioException("You must provide a batchId.");
        }

        $params = array('id' => $batchId);
        $result = $this->doRequest($this->host . "closeBatch", $params);

        return $result;
    }

    // PHONE NUMBERS

    public function provisionNumber($areaCode, $callbackUrl = null){
        $params = array('area_code' => $areaCode);

        if ($callbackUrl) $params['callback_url'] = $callbackUrl;
        return $this->doRequest($this->host . "provisionNumber", $params);
    }

    public function releaseNumber($phoneNumber){
        $params = array('number' => $phoneNumber);
        return $this->doRequest($this->host . "releaseNumber", $params);
    }

    public function listNumbers($options){
        $params = array();

        $this->paramsCopy(
            array('area_code', 'number'),
            $options,
            $params
        );

        return $this->doRequest($this->host . "numberList", $params);
    }

    public function getAvailableAreaCodes($options){
        $params = array();

        $this->paramsCopy(
            array('is_toll_free', 'state'),
            $options,
            $params
        );

        return $this->doRequest($this->host . "areaCodes", $params);
    }


    public function getSupportedCountries($options){
        return $this->doRequest($this->host . "supportedCountries");
    }



    public function getApiKey()
    {
        return $this->api_key;
    }

    public function getApiSecret()
    {
        return $this->api_secret;
    }

    private function doRequest($address, $params = array(), $wrapInPhaxioOperationResult = true)
    {
        $ch = curl_init($address);

        $params['api_key'] = $this->getApiKey();
        $params['api_secret'] = $this->getApiSecret();

        if ($this->debug) {
            echo "Request address: \n\n $address?" . http_build_query($params) . "\n\n";
        }

        $result = $this->curlPost($address, $params, false);

        if ($this->debug) {
            echo "Response: \n\n";
            var_dump($result);
            echo "\n\n";
        }

        if ($wrapInPhaxioOperationResult) {
            $result = json_decode($result, true);

            if (! $result) {
                $opResult = new PhaxioOperationResult(false, "No data received from service.");
            } else {
                $opResult = new PhaxioOperationResult($result['success'], $result['message'], $result['data']);

                if ($result['paging']){
                    $opResult->addPagingData($result['paging']);
                }
            }

            return $opResult;
        } else {
            return $result;
        }
    }

    private function curlPost($host, $params = array(), $async = false)
    {
        $handle = curl_init($host);
        curl_setopt($handle, CURLOPT_POST, true);

        if ($async) {
            curl_setopt($handle, CURLOPT_TIMEOUT, 1);
        } else {
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        }

        $this->curlSetoptCustomPostfields($handle, $params);
        $result = curl_exec($handle);

        if ($result === false) {
            throw new PhaxioException('Curl error: ' . curl_error($handle));
        }

        return $result;
    }

    private function paramsCopy($names, $options, &$params)
    {
        foreach ($names as $name) {
            if (isset($options[$name])) {
                $params[$name] = $options[$name];
            }
        }
    }

    private function curlSetoptCustomPostfields($ch, $postfields, $headers = null)
    {
        $algos = hash_algos();
        $hashAlgo = null;

        foreach (array('sha1', 'md5') as $preferred) {
            if (in_array($preferred, $algos)) {
                $hashAlgo = $preferred;
                break;
            }
        }
        if ($hashAlgo === null) {
            list($hashAlgo) = $algos;
        }
        $boundary =
                '----------------------------' .
                substr(hash($hashAlgo, 'cURL-php-multiple-value-same-key-support' . microtime()), 0, 12);

        $body = array();
        $crlf = "\r\n";
        $fields = array();
        foreach ($postfields as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $v) {
                    $fields[] = array($key, $v);
                }
            } else {
                $fields[] = array($key, $value);
            }
        }
        foreach ($fields as $field) {
            list($key, $value) = $field;
            if (strpos($value, '@') === 0) {
                preg_match('/^@(.*?)$/', $value, $matches);
                list($dummy, $filename) = $matches;
                $body[] = '--' . $boundary;
                $body[] = 'Content-Disposition: form-data; name="' . $key . '"; filename="' . basename($filename) . '"';
                $body[] = 'Content-Type: application/octet-stream';
                $body[] = '';
                $body[] = file_get_contents($filename);
            } else {
                $body[] = '--' . $boundary;
                $body[] = 'Content-Disposition: form-data; name="' . $key . '"';
                $body[] = '';
                $body[] = $value;
            }
        }
        $body[] = '--' . $boundary . '--';
        $body[] = '';
        $contentType = 'multipart/form-data; boundary=' . $boundary;
        $content = join($crlf, $body);
        $contentLength = strlen($content);

        curl_setopt(
            $ch,
            CURLOPT_HTTPHEADER,
            array(
                'Content-Length: ' . $contentLength,
                'Expect: 100-continue',
                'Content-Type: ' . $contentType,
            )
        );

        curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    }
}

{'fraction_non_alphanumeric': 0.16172910662824208, 'fraction_numerical': 0.0016138328530259365, 'mean_word_length': 1.7462804685026907, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 89, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5409540', 'n_tokens_mistral': 5204, 'n_tokens_neox': 4961, 'n_words': 1063}