<?PHP

/**
* Mysql class
*/
class MySql{

	/**
	* creat the sql connection
	*/
	function connect(){

		$sql['host'] = constant('sqlconf[host]');
		$sql['port'] = constant('sqlconf[port]');
		$sql['database'] = constant('sqlconf[database]');
		$sql['user'] = constant('sqlconf[user]');
		$sql['password'] = constant('sqlconf[password]');

		$this->database = $sql['database'];

		if(!empty($sql['host']) && $sql['host'] != 'DB_HOST'){

			$this->connection = @mysqli_connect($sql['host'].($sql['port'] && $sql['port'] != 3306 ? ':'.$sql['port'] : ''), $sql['user'], $sql['password']);
			if($this->connection){

				mysqli_select_db($this->connection, $sql['database']);
				mysqli_query($this->connection, "SET NAMES utf8");
				mysqli_query($this->connection, "SET SESSION group_concat_max_len = 6000;");
			}
			else{

				$this->sqlError('cant connect '.mysqli_error($this->connection), '', __LINE__);
			}
		}
	}

	/**
	* close sql connection
	*/
	function close(){

		mysqli_close($this->connection);
	}

	private function sqlError($error, $query, $line){

		$Name = "no-replay@mysql.ee"; //senders name
		$email = "no-replay@mysql.ee"; //senders e-mail adress
		$recipient = constant('sqlconf[error_report_email]'); //recipient
		$mail_body = "MYSQL error: ".$error."\r\n\r\n"."on query: ".$query."\r\n\r\n"."line: ".$this->LINE."\r\n\r\nPAGE="._RootUrl.'/'._FULL_URL."\r\n\r\nclass.MYSQL.php LINE:".$line; //mail body
		$subject = "Mysql Error ON "._RootUrl; //subject
		$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

		//ini_set('sendmail_from', 'me@domain.com'); //Suggested by "Some Guy"

		file_put_contents(_RootFolder.'/tmp/sqlError.txt', $error."\n".$query."\n\n");

		$reg_dir = _TmpBaseFolder.'/requests';
		$reg_file = $reg_dir.'/'.date('d.m.Y').'.php';

		if(!is_dir($reg_dir))
			mkdir($reg_dir);

		$data = '';
		$data = $_GET ? '<? //GET('.date('d.m.Y H:i:s').') '.json_encode($_GET)." ?>\n" : "";
		$data .= $_POST ? '<? //POST('.date('d.m.Y H:i:s').') '.json_encode($_POST)." ?>\n" : "";
		$mail_body .= "\r\n\r\nHTTP_REQUEST=".$data;

		if(!empty($_GET) || !empty($_POST)){

			file_put_contents($reg_file, $data);
		}

		if(constant('sqlconf[show_error]') == true)
			die(str_replace("\r\n", '<br/>', $mail_body));
		else
			mail($recipient, $subject, $mail_body, $header); //mail command :)
		$this->mysqli_error = true;
	}

	private function mtimeStart(){

		return microtime(true);
	}

	private function mtimeEnd($starttime){

		$end = microtime(true);
		return round(($end - $starttime), 4);
	}

	private function writeMtime($query){

		if($this->queryTime > 1){

			if( !is_dir(_RootFolder.'/tmp/logs') )
				mkdir(_RootFolder.'/tmp/logs');

			$content = "TIME: ".date('H:i:s')."\nLOAD TIME: ".$this->queryTime."\n".$query;
			if(is_file(_RootFolder.'/tmp/logs/query_'.date('d.m.Y').'.sql'))
				$content .= "\n\n\n".file_get_contents(_RootFolder.'/tmp/logs/query_'.date('d.m.Y').'.sql');

			file_put_contents(_RootFolder.'/tmp/logs/query_'.date('d.m.Y').'.sql', $content);
		}
	}

	/**
	* execute SQL query
	* @param String $query AS query string
	* @param Boolean $return_data = false AS execute string and return array
	*/
	function query($query, $return_data = false){

		if($this->connection){

			if(constant('sqlconf[log_time]') == true)
				$this->getQueryTime = true;

			unset($this->Numrows);

			if($this->getQueryTime)
				$mtimeStart = $this->mtimeStart();

			$this->Query = $query;
			$check_query = explode(' ', trim($query));
			if(strtoupper($check_query[0]) != 'SELECT' && strtoupper($check_query[0]) != 'SHOW'){

				if( (preg_match('/UPDATE([[:space:]_]+)/i', strtoupper($query)) || preg_match('/DELETE([[:space:]_]+)FROM/i', strtoupper($query)))  && (!preg_match('/WHERE(.*?)([A-Z\_[:space:]_]+)\=|\<|\>/i', strtoupper($query)) && !preg_match('/WHERE(.*?)IN\(/i', strtoupper($query)) && !preg_match('/WHERE(.*?)LIKE/i', strtoupper($query))) ){

					if(strtoupper($check_query[0]) != 'INSERT')
						die('<b style="color:red;">Update and Delete query Must have WHERE</b><br>'.$query);
				}

				mysqli_query($this->connection, "TRUNCATE TABLE `memory_page_generator`");

				mysqli_query($this->connection, $query) or $this->sqlError(mysqli_error($this->connection), $query, __LINE__);
				if($this->getQueryTime)
					$this->queryTime = $this->mtimeEnd($mtimeStart);
				if(constant('sqlconf[log_time]') == true)
					$this->writeMtime($query);
			}
			else{

				$limit_last_pos = strrpos($query, "LIMIT");
				if($this->queryLimit && (strlen($query)-10) >= $limit_last_pos) $query = $this->setQueryLimit($query);

				$this->Result = mysqli_query($this->connection, $query) or $this->sqlError(mysqli_error($this->connection), $query, __LINE__);
				$this->Numrows = mysqli_num_rows($this->Result);

				if($this->getQueryTime)
					$this->queryTime = $this->mtimeEnd($mtimeStart);
				if(constant('sqlconf[log_time]') == true)
					$this->writeMtime($query);
				if($this->queryLimitMaxRows)
					$this->Numrows = $this->queryLimitMaxRows;

				if($return_data == true && $this->Numrows > 0){

					$Rows = array();
					while($r = mysqli_fetch_assoc($this->Result)){

						$Rows[] = $r;
					}
					unset($this->Result);
					return $Rows;
				}
			}
		}
	}

	private function queryMemoryLimit($max_limit_query, $type){

		$q = mysqli_query($this->connection, "SELECT * FROM memory_page_generator WHERE query = '".md5($max_limit_query)."'") or $this->sqlError(mysqli_error($this->connection), $query, __LINE__);
		if( mysqli_num_rows($q) == 1 ){

			$r = mysqli_fetch_assoc($q);
			$this->queryLimitMaxRows = $r['numrows'];
		}
		else{

			$q = mysqli_query($this->connection, $max_limit_query) or $this->sqlError(mysqli_error($this->connection), $max_limit_query, __LINE__);
			$max_rows = @mysqli_num_rows($q);
			$this->queryLimitMaxRows = $max_rows;

			mysqli_query($this->connection, "INSERT INTO memory_page_generator (query, Numrows, created_at) VALUES ('".md5($max_limit_query)."', ".$this->queryLimitMaxRows.", NOW())") or $this->sqlError(mysqli_error($this->connection), $max_limit_query, __LINE__);
		}
	}

	private function setQueryLimit($query){

		$max_limit_query = $query;

		$this->queryMemoryLimit($max_limit_query, '');

		$fullSql = $query;
		if($this->queryLimit < $this->queryLimitMaxRows){

			$this->pageGenerator($this->queryLimitMaxRows);
			if(preg_match('/LIMIT /i', $query))
				$fullSql = "SELECT a.* FROM (".$query.") AS a ".$this->getQueryLimit();
			else
				$fullSql = $query.' '.$this->getQueryLimit();
		}
		unset($this->queryLimit);
		return $fullSql;
	}

	private function getQueryLimit(){

		$cur_page = $_GET['page_number'] && $_GET['page_number']>0 ? $_GET['page_number'] : 1;
		$offset = $this->pageList[$cur_page]['offset'] ? $this->pageList[$cur_page]['offset'] : 0;
		return 'LIMIT '.$offset.', '.$this->queryLimit;
	}

	private function pageGenerator($max_rows){

		$list = array();

		$pages = ceil($max_rows/$this->queryLimit);
		$cur_page = $_GET['page_number'];
		$show_page_numbers = 3;
		if($pages == 1 && $max_rows > $this->queryLimit) $pages = 2;
		for($i=1; $i<=$pages; $i++){

			$ij = $i-1;

			//algus
			if($i <= $show_page_numbers){

				$list[$i] = array('page_number' => $i, 'offset' => ($this->queryLimit*$ij), 'is_visible' => true, 'is_first' => ($i == 1 ? true : false), 'show_page_numbers' => $show_page_numbers);
			}
			//vahe
			else{

				$list[$i] = array('page_number' => $i, 'offset' => ($this->queryLimit*$ij), 'is_visible' => false, 'show_page_numbers' => $show_page_numbers);

				if(($cur_page) -1 == $i){

					$list[$i] = array('page_number' => $i, 'offset' => ($this->queryLimit*$ij), 'is_visible' => true, 'show_page_numbers' => $show_page_numbers);
				}
				if($cur_page == $i){

					$list[$i] = array('page_number' => $i, 'offset' => ($this->queryLimit*$ij), 'is_visible' => true, 'show_page_numbers' => $show_page_numbers);
				}
				if(($cur_page) +1 == $i){

					$list[$i] = array('page_number' => $i, 'offset' => ($this->queryLimit*$ij), 'is_visible' => true, 'show_page_numbers' => $show_page_numbers);
				}
			}
			//lopp
			if($i >= ($pages-($show_page_numbers-1))){

				$list[$i] = array('page_number' => $i, 'offset' => ($this->queryLimit*$ij), 'is_visible' => true, 'is_last' => ($i == $pages ? true : false), 'show_page_numbers' => $show_page_numbers);
			}
		}
		$this->pageList = $list;
	}
}

/**
* extend Mysql class
*/
class SqlRequest extends MySql{

	/**
	* forward query select string
	* @param String $query = '' AS part of the select query string like "WHERE id = 1" OR full query string
	* @param Boolean $return_data = false AS make object data array $this->table_name->Rows
	* @return full query string
	* @example $this->table_name->Select("WHERE id = 1", true)
	* while($row = $this->table_name->Each())
	* @example $this->table_name->Select("WHERE id = 1")
	* while($row = $this->table_name->Assoc())
	*/
	function Select($query = '', $return_data = false){

		$exp = explode(' ', trim($query));

		if(trim(strtoupper($exp[0])) != 'SELECT' && trim(strtoupper($exp[0])) != 'SHOW')
			$query = "SELECT * FROM ".$this->table." ".$query;

		$query = str_replace('select ', 'SELECT ', $query);
		$query = str_replace('from ', 'FROM ', $query);

		$this->Result = '';
		$this->Numrows = 0;

		if($this->connection){

			if($this->selectColumns){

				$query = $this->replaceColumns($query, $this->selectColumns);
				unset($this->selectColumns);
			}

			if($return_data == true){

				$this->Rows = $this->Query($query, $return_data);
			}
			else{

				$this->Query($query, $return_data);
			}
		}
		return $this->Query;
	}

	/**
	* forward query insert string
	* @param Array,String $params = array() AS array('column' => 'value') OR full query string
	* @return full query string
	*/
	function Insert($params = array()){

		if(gettype($params) == 'string')
			$query = $params;

		if(!preg_match('/INSERT([[:space:]_]+)INTO([[:space:]_]+)'.$this->table.'/i', $query) && !preg_match('/INSERT([[:space:]_]+)INTO([[:space:]_]+)`'.$this->table.'/i', $query)){

			$query = "INSERT INTO ".$this->table." ({columns}) VALUES ({values})";
		}
		else{

			$this->Query($query, false);
			return $query;
		}

		$this->Insertid = 0;

		if($this->connection){

			unset($params['id']);

			$columns = '';
			$values = '';
			if(is_array($params)){

				$column_exists = $this->getColumns($this->table);

				foreach($column_exists['list'] as $column){

					if($params[$column] || $this->tableDefaults[$column]){

						$value = $params[$column];
						if(!$value)
							$value = $this->tableDefaults[$column];
						preg_match('/(.*?)\(/s', $value, $match);
						$start = $match[1];

						$columns .= $columns ? ', `'.$column.'`' : '`'.$column.'`';
						if(gettype($value) == 'string' && $start == strtoupper($start) && strlen($start) > 1 && substr($value, -1) == ')' && preg_match('/([A-Z\_[:space:]_]+)\(/i', $value)){

							$values .= $values ? ', '.$value : $value;
						}
						else{

							$values .= $values ? ', '."'".$value."'" : "'".$value."'";
						}
					}
				}
			}
			$query = str_replace('{columns}', $columns, $query);
			$query = str_replace('{values}', $values, $query);
			$this->Query($query, false);
			$this->Insertid = mysqli_insert_id($this->connection);
		}
		unset($this->tableDefaults);
		return $query;
	}

	/**
	* forward query update string
	* @param Array,String $params = array() AS array('column' => 'value') OR full query string
	* @param String $query AS part of query string like "WHERE id = ".$_POST['id']
	* @return full query string
	*/
	function Update($params = array(), $query){

		if(gettype($params) == 'string' && !$query)
			$query = $params;

		if(!preg_match('/UPDATE([[:space:]_]+)'.$this->table.'/i', $query) && !preg_match('/UPDATE([[:space:]_]+)`'.$this->table.'/i', $query)){

			$query = "UPDATE ".$this->table." SET {set} ".$query;
		}
		else{

			$this->Query($query, false);
			return $query;
		}

		$this->Affectedrows = 0;

		if($this->connection){

			if(is_array($params)){

				unset($params['id']);

				$set = '';
				$column_exists = $this->getColumns($this->table);

				foreach($column_exists['list'] as $column){

					if($params[$column] || $this->tableDefaults[$column]){

						$value = $params[$column];
						if(!$value)
							$value = $this->tableDefaults[$column];

						preg_match('/(.*?)\(/s', $value, $match);
						$start = $match[1];

						if($start == strtoupper($start) && strlen($start) > 1 && substr($value, -1) == ')' && preg_match('/([A-Z\_[:space:]_]+)\(/i', $value)){

							$set .= $set ? ', `'.$column.'` = '.$value : '`'.$column.'` = '.$value;
						}
						else{

							$set .= $set ? ', `'.$column.'` = '."'".$value."'" : '`'.$column.'` = '."'".$value."'";
						}
					}
				}
			}
			$query = str_replace('{set}', $set, $query);
			if(trim($set)){

				$this->Query($query, false);
				$this->Affectedrows = mysqli_affected_rows($this->connection);
			}
		}
		unset($this->tableDefaults);
		return $query;
	}

	/**
	* forward query replace string
	* @param Array,String $params = array() AS array('column' => 'value') OR full query string
	* @param String $query AS part of query string like "WHERE id = ".$_POST['id']
	* @return full query string
	*/
	function Replace($params = array(), $query){

		if(gettype($params) == 'string' && !$query)
			$query = $params;

		if(!preg_match('/UPDATE([[:space:]_]+)'.$this->table.'/i', $query) && !preg_match('/UPDATE([[:space:]_]+)`'.$this->table.'/i', $query)){

			$query = "REPLACE INTO ".$this->table." SET {set} ".$query;
		}
		else{

			$this->Query($query, false);
			return $query;
		}

		$this->Affectedrows = 0;

		if($this->connection){

			if(is_array($params)){

				unset($params['id']);

				$set = '';
				$column_exists = $this->getColumns($this->table);

				foreach($column_exists['list'] as $column){

					if($params[$column] || $this->tableDefaults[$column]){

						$value = $params[$column];
						if(!$value)
							$value = $this->tableDefaults[$column];

						preg_match('/(.*?)\(/s', $value, $match);
						$start = $match[1];

						if($start == strtoupper($start) && strlen($start) > 1 && substr($value, -1) == ')' && preg_match('/([A-Z\_[:space:]_]+)\(/i', $value)){

							$set .= $set ? ', `'.$column.'` = '.$value : '`'.$column.'` = '.$value;
						}
						else{

							$set .= $set ? ', `'.$column.'` = '."'".$value."'" : '`'.$column.'` = '."'".$value."'";
						}
					}
				}
			}
			$query = str_replace('{set}', $set, $query);
			if(trim($set)){

				$this->Query($query, false);
				$this->Affectedrows = mysqli_affected_rows($this->connection);
			}
		}
		unset($this->tableDefaults);
		return $query;
	}

	/**
	* forward query update string
	* @param String $query = '' AS full queery string OR part query string like "WHERE id = ".$_GET['id']
	* @return full query string
	*/
	function Delete($query = ''){

		if(!preg_match('/DELETE([[:space:]_]+)FROM([[:space:]_]+)'.$this->table.'/i', $query) && !preg_match('/DELETE([[:space:]_]+)FROM([[:space:]_]+)`'.$this->table.'/i', $query))
			$query = "DELETE FROM  ".$this->table." ".$query;

		$this->Query($query, false);
		return $query;
	}

	/**
	* set default values for insert,update or replace values
	* @param Array $array AS dataarray array('column' => 0)
	* @example $this->table_name->defaults(array('parent_id' => 0));//if $_POST['parent_id'] is not represented then query parent_id value will be 0
	*/
	function defaults($array){

		$tableDefaults = array();
		foreach($array as $k => $v){

			$key = is_numeric($k) ? $v : $k;
			$value = isset($v) ? $v : '';
			$tableDefaults[$key] = $value;
		}

		$this->tableDefaults = $tableDefaults;
	}

	/**
	* sets columns for query string or returns columns list
	* @param String $columns_or_table AS columns list OR table_name
	* @param String $as = '' AS table_name alias "t1"
	* @param Array $exclude_columns = array('id') AS list of columns what exlude from return value
	* @param Boolean $return = false AS will be columns returned or not
	* @example $this->table_name->columns("id,name,etc");
	* @example $this->table_name->columns("id,name,etc")->append();
	* @example $this->table_name->columns("table_name", "p1", array('id'), true);
	* @return column list array
	*/
	function columns($columns_or_table, $as = '', $exclude_columns = array('id'), $return = false){

		if($return == false)
			$columns = $columns_or_table;
		if($return == true)
			$table = $columns_or_table;

		if($columns)
			$this->selectColumns = $this->selectColumns ? $this->selectColumns.', '.$columns : $columns;

		if( $return == true ){

			$columns = $this->getColumns($table);
			$list = '';
			$as = $as && !preg_match('/\./i', $as) ? $as.'.' : $as;
			foreach($columns['list'] as $k => $v){

				if($exclude_columns[$v]) $v = $v.' AS '.$exclude_columns[$v];
				if(!in_array($v, $exclude_columns))
					$list .= $list ? ' ,'.$as.$v : $as.$v;
			}
			return $list;
		}
		return $this;
	}

	/**
	* convert query date to string, new column will be column_name_str
	* @param Array $array = array() AS columns list
	* @param String $dateStr = '%d.%m.%Y' AS dateformat
	* @example $this->table_name->toDate(array('column'), '%d.%m.%Y');
	*/
	function toDate($array = array(), $dateStr = '%d.%m.%Y'){

		$columns = '';
		foreach($array as $v){

			if(preg_match('/\./i', $v))
				$column = str_replace('.', '.`', $v).'`';
			else
				$column = '`'.$v.'`';
			
			list($alias, $column_as) = explode('.', $column);
			if($column_as)
				$column_as = $column_as.'_'.$alias.'_str';
			else
				$column_as = $alias.'_str';
			$column_as = str_replace('`', '', $column_as);

			$columns .= $columns ? ", DATE_FORMAT(".$column.", '".$dateStr."') AS ".$column_as : "DATE_FORMAT(".$column.", '".$dateStr."') AS ".$column_as;
		}
		$this->columns($columns)->append();
	}

	/**
	* convert query datetime to string, new column will be column_name_str
	* @param Array $array = array() AS columns list
	* @param String $dateTimeStr = '%d.%m.%Y %H:%I:%s' AS datetimeformat
	* @example $this->table_name->toDateTime(array('column'), '%d.%m.%Y');
	*/
	function toDateTime($array = array(), $dateTimeStr = '%d.%m.%Y %H:%i:%s'){

		$columns = '';
		foreach($array as $v){

			if(preg_match('/\./i', $v))
				$column = str_replace('.', '.`', $v).'`';
			else
				$column = '`'.$v.'`';
			
			list($alias, $column_as) = explode('.', $column);
			if($column_as)
				$column_as = $column_as.'_'.$alias.'_str';
			else
				$column_as = $alias.'_str';
			$column_as = str_replace('`', '', $column_as);

			$columns .= $columns ? ", DATE_FORMAT(".$column.", '".$dateTimeStr."') AS ".$column_as : "DATE_FORMAT(".$column.", '".$dateTimeStr."') AS ".$column_as;
		}
		$this->columns($columns)->append();
	}

	/**
	* append columns to query string view "function column"
	*/
	function append(){

		if($this->selectColumns)
			$this->action['append']['columns'] = true;
	}

	/**
	* use controller method
	* @param Array $array
	* @example $this->table_name->useMethod(array($this, 'controllerMethodName'));
	*/
	function useMethod($array){

		$this->useMethod = $array;
	}

	/**
	* mysql dataseek
	*/
	function dataSeek(){

		$this->dataSeek[$this->table] = true;
	}

	/**
	* mysqli_fetch_assoc()
	*/
	function Assoc(){

		if($this->Numrows > 0){

			$result = $this->Result;
			if($this->dataSeek[$this->table]){

				$this->dataSeek($result, 0);
			}
			$row = $result->fetch_assoc();
			if($this->useMethod && $row){

				$returnRow = $this->executeMethod($row);
				if($returnRow) $row = $returnRow;
			}
			if(!$row){

				unset($this->useMethod);
			}
			return $row;
		}
		return false;
	}

	/**
	* if is returned then use each
	* @example $this->table_name->Select("WHERE id = 1", true)
	* while($row = $this->table_name->Each())
	*/
	function Each(){

		if($this->Numrows > 0){

			$each = each($this->Rows);
			$row = $each['value'];
			if($this->useMethod && $row){

				$returnRow = $this->executeMethod($row);
				if($returnRow) $row = $returnRow;
			}
			return $row;
		}
		return false;
	}

	/**
	* query string limit
	* also it generates html pagelist
	* $this->pageGenerator->page_generator($this->language->pageList, '')
	*/
	function limit($limit){

		$this->queryLimit = $limit;
	}

	private function replaceColumns($query, $replace_to){

		preg_match('/SELECT(.*?)FROM([[:space:]_]+)'.$this->table.'/s', $query, $match);
		if(!$match[0])
			preg_match('/SELECT(.*?)FROM([a-zA-Z\_[:space:]_]+)AS([[:space:]_]+)'.$this->table.'/s', $query, $match);
		if(!$match[0])
			preg_match('/SELECT(.*?)FROM([a-zA-Z\_[:space:]_]+)'.$this->table.'/s', $query, $match);


		$match[0] = substr_replace($match[0], "FROM", strrpos($match[0], "FROM"));

		if($this->action['append']['columns'] == true){

			$columns = $this->findQueryColumns($query);
			return str_replace($columns, $columns.', '.$replace_to.' ', $query);
		}
		return str_replace($match[0], "SELECT ".($replace_to)." FROM", $query);
	}

	private function findQueryColumns($query){

		$exp = explode('SELECT', $query);
		$columns = '';
		$select_count = 1;
		$from_count = 0;

		foreach($exp as $k => $v){

			if($v){

				$str_word_count = substr_count($v, 'FROM');
				$from_count += $str_word_count;

				if($select_count > 1) $v = 'SELECT'.$v;

				if($select_count == $from_count){

					$columns .= substr_replace($v, "", strrpos($v, "FROM"));
					return $columns;
				}
				$columns .= $v;

				$select_count ++;
			}
		}
	}

	private function executeMethod($row){

		$call_user_func_array_params = array($row, $this->useMethod[2]);

		if(is_array($this->useMethod[1])){

			foreach($this->useMethod[1] as $v){

				$returnRow = call_user_func_array(array($this->useMethod[0], $v), $call_user_func_array_params);
			}
		}
		else{

			$returnRow = call_user_func_array(array($this->useMethod[0], $this->useMethod[1]), $call_user_func_array_params);
		}

		return $returnRow;
	}

	private function getColumns($table=''){

		$table = !$table ? $this->table : $table;

		$q = mysqli_query($this->connection, "SHOW COLUMNS FROM ".constant('sqlconf[database]').".".$table);
		$ret = array();
		$type = array();
		$default = array();
		while($v = mysqli_fetch_Assoc($q)){

			$ret[] = $v['Field'];
			$type[$v['Field']] = $v['Type'];
			$default[$v['Field']] = $v['Default'];
		}
		return array('list' => $ret, 'type' => $type, 'default' => $default);
	}
}

?>