<commit_before><?php

namespace Difra;

class Ajax {

	public $isAjax = false;
	public $isIframe = false;
	public $parameters = array();
	public $response = array();
	private $actions = array();
	private $problem = false;

	/**
	 * Конструктор
	 */
	public function __construct() {

		if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) {
			$this->isAjax = true;
			$this->parameters = $this->getRequest();
		} elseif( isset( $_POST['_method'] ) and $_POST['_method'] == 'iframe' ) {
			$this->isAjax = true;
			$this->isIframe = true;
			$this->parameters = $_POST;
			unset( $this->parameters['method_'] );
			if( !empty( $_FILES ) ) {
				foreach( $_FILES as $k => $file ) {
					if( $file['error'] == UPLOAD_ERR_NO_FILE ) {
						continue;
					}
					$this->parameters[$k] = $file;
				}
			}
		}
	}

	/**
	 * Синглтон
	 * @static
	 * @return Ajax
	 */
	static function getInstance() {

		static $_instance = null;
		return $_instance ? $_instance : $_instance = new self;
	}

	/**
	 * Получает данные от ajaxer
	 * @return array
	 */
	private function getRequest() {

		$res = array();
		if( !empty( $_POST['json'] ) ) {
			$res = json_decode( $_POST['json'], true );
			if( !empty( $res['form'] ) ) {
				foreach( $res['form'] as $item ) {
					$res[$item['name']] = trim( $item['value'] );
				}
				unset( $res['form'] );
			}<commit_msg>Добавлена обработка массивов (например, элементов с именами param[5][2]) при передаче форм через ajaxer<commit_after><?php

namespace Difra;

class Ajax {

	public $isAjax = false;
	public $isIframe = false;
	public $parameters = array();
	public $response = array();
	private $actions = array();
	private $problem = false;

	/**
	 * Конструктор
	 */
	public function __construct() {

		if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) {
			$this->isAjax = true;
			$parameters = $this->getRequest();
			if( empty( $parameters ) ) {
				return;
			}
			foreach( $parameters as $k => $v ) {
				$this->parseParam( $k, $v );
			}
		} elseif( isset( $_POST['_method'] ) and $_POST['_method'] == 'iframe' ) {
			$this->isAjax = true;
			$this->isIframe = true;
			$this->parameters = $_POST;
			unset( $this->parameters['method_'] );
			if( !empty( $_FILES ) ) {
				foreach( $_FILES as $k => $file ) {
					if( $file['error'] == UPLOAD_ERR_NO_FILE ) {
						continue;
					}
					$this->parameters[$k] = $file;
				}
			}
		}
	}

	private function parseParam( $k, $v ) {

		$keys = array();
		$keys = explode( '[', $k );
		if( sizeof( $keys ) == 1 ) {
			$this->parameters[$k] = $v;
			return;
		}
		for( $i = 1; $i < sizeof( $keys ); $i++ ) {
			if( $keys[$i]{strlen($keys[$i])-1} == ']' ) {
				$keys[$i] = substr( $keys[$i], 0, -1 );
			}
		}
		$this->putParam( $this->parameters, $keys, $v );
	}

	private function putParam( &$arr, $keys, $v ) {

		if( empty( $keys ) ) {
			$arr = $v;
			return;
		}
		$k = array_shift( $keys );
		if( $k ) {
			if( !isset( $arr[$k] ) ) {
				$arr[$k] = array();
			}
			$arr2 = &$arr[$k];
		} else {
			$arr[] = array();
			end( $arr );
			$arr2 = &$arr[key( $arr )];
		}
		$this->putParam( $arr2, $keys, $v );
	}

	/**
	 * Синглтон
	 * @static
	 * @return Ajax
	 */
	static function getInstance() {

		static $_instance = null;
		return $_instance ? $_instance : $_instance = new self;
	}

	/**
	 * Получает данные от ajaxer
	 * @return array
	 */
	private function getRequest() {

		$res = array();
		if( !empty( $_POST['json'] ) ) {
			$res = json_decode( $_POST['json'], true );
			if( !empty( $res['form'] ) ) {
				foreach( $res['form'] as $item ) {
					$res[$item['name']] = trim( $item['value'] );
				}
				unset( $res['form'] );
			}
{'fraction_non_alphanumeric': 0.2058273188986902, 'fraction_numerical': 0.002673082063619353, 'mean_word_length': 3.041036717062635, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 26, 'https://': 0, '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': '5125742', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1420, 'n_words': 317}