<commit_before><?php

namespace bgaluszka\Mbank;

class Mbank
{
    protected $curl;

    protected $tab, $token;

    protected $document;

    protected $xpath;

    protected $opts = array();

    public $url = 'https://online.mbank.pl';

    public function __construct()
    {
        $this->curl = curl_init();

        $this->opts = array(
            CURLOPT_URL => null,
            CURLOPT_POST => false,
            CURLOPT_FOLLOWLOCATION => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_CAINFO => dirname(dirname(dirname(__DIR__))) . '/crt/cacert.pem',
            CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1,
            // http://blog.volema.com/curl-rce.html
            CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
            CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS,
            // http://stackoverflow.com/a/1490482
            CURLOPT_COOKIEJAR => '/dev/null',
        );

        $this->document = new \DOMDocument('1.0', 'UTF-8');
        $this->document->preserveWhiteSpace = false;
    }

    public function __destruct()
    {
        curl_close($this->curl);
    }

    public function login($username, $password)
    {
        $opts = array(
            CURLOPT_URL => $this->url . '/pl/Login',
        );
        $response = $this->curl($opts);

        $opts = array(
            CURLOPT_URL => $this->url . '/pl/Account/JsonLogin',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => array(
                'UserName' => $username,
                'Password' => <PASSWORD>,
                'Seed' => '',
                'Lang' => '',
            ),
        );
        $response = $this->curl($opts);

        if (empty($response['successful'])) {
            throw new \Exception('login() failed');
        }

        $this->tab = $response['tabId'];

        $opts = array(
            CURLOPT_URL => $this->url . '/pl',
        );
        $response = $this->curl($opts);

        $this->load($response);

        $this->token = $this->xpath->evaluate('string(//meta[@name="__AjaxRequestVerificationToken"]/@content)');

        return true;
    }

    public function profile($profile)
    {
        $profiles = array(
            'individual' => 'I',
            'business' => 'F',
        );

        if (!isset($profiles[$profile])) {
            throw new \Exception('Invalid profile (individual|business)');
        }

        $opts = array(
            CURLOPT_URL => $this->url . '/pl/LoginMain/Account/JsonActivateProfile',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => array(
                'profileCode' => $profiles[$profile],
            ),
        );

        return $this->curl($opts);
    }

    public function accounts()
    {
        $opts = array(
            CURLOPT_URL => $this->url . '/pl/Accounts/Accounts/List',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => array(),
        );

        $response = $this->curl($opts);
<commit_msg>Fixed issue with accounts() returning HTTP status code 500<commit_after><?php

namespace bgaluszka\Mbank;

class Mbank
{
    protected $curl;

    protected $tab, $token;

    protected $document;

    protected $xpath;

    protected $opts = array();

    public $url = 'https://online.mbank.pl';

    public function __construct()
    {
        $this->curl = curl_init();

        $this->opts = array(
            CURLOPT_URL => null,
            CURLOPT_POST => false,
            CURLOPT_FOLLOWLOCATION => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_CAINFO => dirname(dirname(dirname(__DIR__))) . '/crt/cacert.pem',
            CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1,
            // http://blog.volema.com/curl-rce.html
            CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
            CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS,
            // http://stackoverflow.com/a/1490482
            CURLOPT_COOKIEJAR => '/dev/null',
        );

        $this->document = new \DOMDocument('1.0', 'UTF-8');
        $this->document->preserveWhiteSpace = false;
    }

    public function __destruct()
    {
        curl_close($this->curl);
    }

    public function login($username, $password)
    {
        $opts = array(
            CURLOPT_URL => $this->url . '/pl/Login',
        );
        $response = $this->curl($opts);

        $opts = array(
            CURLOPT_URL => $this->url . '/pl/Account/JsonLogin',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => array(
                'UserName' => $username,
                'Password' => $password,
                'Seed' => '',
                'Lang' => '',
            ),
        );
        $response = $this->curl($opts);

        if (empty($response['successful'])) {
            throw new \Exception('login() failed');
        }

        $this->tab = $response['tabId'];

        $opts = array(
            CURLOPT_URL => $this->url . '/pl',
        );
        $response = $this->curl($opts);

        $this->load($response);

        $this->token = $this->xpath->evaluate('string(//meta[@name="__AjaxRequestVerificationToken"]/@content)');

        return true;
    }

    public function profile($profile)
    {
        $profiles = array(
            'individual' => 'I',
            'business' => 'F',
        );

        if (!isset($profiles[$profile])) {
            throw new \Exception('Invalid profile (individual|business)');
        }

        $opts = array(
            CURLOPT_URL => $this->url . '/pl/LoginMain/Account/JsonActivateProfile',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => array(
                'profileCode' => $profiles[$profile],
            ),
        );

        return $this->curl($opts);
    }

    public function accounts()
    {
        $opts = array(
            CURLOPT_URL => $this->url . '/pl/Accounts/Accounts/List',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => array(),
            CURLOPT_HTTPHEADER => array('X-Requested-With: XMLHttpRequest'),
        );

        $response = $this->curl($opts);

{'fraction_non_alphanumeric': 0.14852773710753214, 'fraction_numerical': 0.004067024564828371, 'mean_word_length': 1.7958162801273305, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 103, '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': '3686578', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1820, 'n_words': 313}