<?php
	$MainDir = $_SERVER['DOCUMENT_ROOT']."/".substr(substr($_SERVER['REQUEST_URI'], 1), 0, strpos(substr($_SERVER['REQUEST_URI'], 1), '/'));
	$dir = $MainDir."/phone_gap";
	require_once($dir."/dados.php");
	require_once($MainDir."/criptar.php");
	
	class Dados {
		
		private $CodManif = "'5.904', '5.414', '6.904', '6.414', '5.415', '6.415'";
		private $CodBonif = "'5.910', '6.910'";
		private $versao = 20;
		//Nao da pra concatenar
		// private $Chave = ;

		public function getCodBonif(){
			return $this->CodBonif;
		}

		public function getCodManif(){
			return $this->CodManif;
		}
		
		public function getVersao() {
			return $this->versao;
		}
		
		public function Meses(){
			$meses = array('Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro');
			return $meses;
		}
		
		public function htmlentities2($str) {
			return htmlentities($str, ENT_QUOTES, 'ISO-8859-1');
		}
		
		public function selectSQL($sql){
			$query = ibase_query($sql);
			if (ibase_errmsg()) {
				 // echo "<b style='color: red;'>ERRO!</b><br>".ibase_errmsg()."<br>".$sql;
				 // die();
			} else {
				$array = array();
				while($row = ibase_fetch_assoc($query, IBASE_TEXT)){
					$array[] = array_map(array($this, 'htmlentities2'), $row);
				}
				return $array;
			}
		}
		
		public function filtroSQL($col, $val){
			if($val != '' and $col != ''){
				return " and ".$col." = '".$val."' ";
			}else{
				return '';
			}
		}
		
		public function DatePickerMonthToDate($str){
			$data = explode(' - ', $str);
			$result = array_search($data[0], $this->Meses());
			$result = $data[1].'-'.($result + 1);
			$result = date('Y-m', strtotime($result));
			return $result;
		}
		
		public function DatePickerToDate($str){
			$data = explode(' - ', $str);
			$result = array_search($data[1], $this->Meses());
			$result = $data[2].'-'.($result + 1).'-'.$data[0];
			$result = date('Y-m-d', strtotime($result));
			return $result;
		}
		
		public function filtroDataSQL($col, $dataini, $datafin){
			$result = " and ".$col." >= '".date('d.m.Y', strtotime($dataini))."' and ".$col." <= '".date('d.m.Y', strtotime($datafin))."'";
			return $result;
		}
		
		public function filtroMesSQL($col, $periodo){
			$result = " and ".$col." >= '".date('01.m.Y', strtotime($periodo))."' and ".$col." <= '".date('t.m.Y', strtotime($periodo))."'";
			return $result;
		}
		
		public function ifthen($bool, $true, $false = ""){
			if($bool){
				return $true;
			}else{
				return $false;
			}
		}
		
		public function StrToFloat($s){
			if (strpos($s, ',') !== false) {
				$numero = strtr($s, ',', '.');
			} else {
				$numero = $s;
			}
			return $numero;
		}
		
		public function FloatToStr($num) {
			$ret = 0;
			if ($num != 0){
				$value =  $num;
				$arr = explode(".", $value);
				$ret = implode(",", $arr);
			}else{
				$ret = '0,00';
			}
			return $ret;
		}
		
		public function DateToText($data) {
			if ($data != "") {
				return date('d/m/Y', strtotime($data));
			}
		}
		
		public function array_dump($array){
			echo '<pre>';
			print_r($array);
			echo '</pre>';
		}
		
		public function fixObject($obj){
			return unserialize(serialize($obj));
		}

		public function formatarNumero($num){
			$numero = $this->StrToFloat($num);
			return number_format($numero, 2, ',', '.');
		}
		
		public function multi_array_search($search, $array, $col){
			$expSearch = explode(";", $search);
			$expCol = explode(";", $col);
			$count = count($expSearch);
			if($count != count($expCol)){
				return false;
			}else{
				$result = array();
				if($count > 1){
					foreach($expCol as $id => $c){
						$res[$id] = $this->multi_array_search($expSearch[$id], $array, $c);
					}
					$result = $res[0];
					for($i = 1; $i < $count; $i++){
						$result = array_intersect($result, $res[$i]);
					}
				}else{
					foreach($array as $id => $a){
						$palavra1 = trim($a[$col]);
						$palavra2 = trim($search);
						if($palavra1 == $palavra2){
							$result[] = $id; 
						}
					}
				}
				$result = array_values($result);
				return $result;
			}
		}
		
		function multi_ksort(&$array){
			ksort($array);
			foreach(array_keys($array) as $k){
				if(gettype($array[$k]) == "array"){
					$this->multi_ksort($array[$k]);
				}
			}
		}
		
		public function qsort(&$array, $column = 0, $order = SORT_ASC, $first = 0, $last = -2){
			$keys = array_keys($array);
			if($last == -2){
				$last = count($array) - 1;
			}
			if($last > $first){ 
				$alpha = $first; 
				$omega = $last;
				$key_alpha = $keys[$alpha]; 
				$key_omega = $keys[$omega];
				$guess = $array[$key_alpha][$column]; 
				while($omega >= $alpha){ 
					if($order == SORT_ASC){ 
						while($array[$key_alpha][$column] < $guess){
							$alpha++; 
							$key_alpha = $keys[$alpha];
						}
						while($array[$key_omega][$column] > $guess){
							$omega--;
							$key_omega = $keys[$omega];
						}
					}else{
						while($array[$key_alpha][$column] > $guess){
							$alpha++;
							$key_alpha = $keys[$alpha];
						}
						while($array[$key_omega][$column] < $guess){
							$omega--;
							$key_omega = $keys[$omega];
						}
					} 
					if($alpha > $omega){
						break; 
					}
					$temporary = $array[$key_alpha]; 
					$array[$key_alpha] = $array[$key_omega];
					$alpha++;
					$key_alpha = $keys[$alpha];
					$array[$key_omega] = $temporary;
					$omega--;
					$key_omega = $keys[$omega];
				} 
				$this->qsort($array, $column, $order, $first, $omega); 
				$this->qsort($array, $column, $order, $alpha, $last); 
			} 
		}
		
		public function multi_sort(&$array, $busca){
			foreach($busca as $id => $b){
				if($id > 0){
					$resp = array();
					$temp = $array[0][$busca[$id - 1]];
					foreach($array as $a){
						if($temp === $a[$busca[$id - 1]]){
							$temparray[] = $a;
						}else{
							$this->qsort($temparray, $b, SORT_ASC);
							$resp = array_merge($resp, $temparray);
							$temparray = array($a);
							$temp = $a[$busca[$id - 1]];
						}
					}
					$this->qsort($temparray, $b, SORT_ASC);
					$array = array_merge($resp, $temparray);
				}else{
					$this->qsort($array, $b, SORT_ASC);
				}
			}
		}
		
		function js_str($s){
			return '"'.addcslashes((string) $s, "\0..\37\"\\").'"';
		}
		
		public function js_array($array){
			$temp = array_map('js_str', $array);
			return '['.implode(',', $temp).']';
		}
		
		public function arrayToJS($array){
			if(!$array){
				return '[]';
			}else{
				$arrayJS = '[';
				foreach($array as $a){
					$arrayJS .= $this->js_array($a).',';
				}
				$arrayJS = substr($arrayJS, 0, -1);
				$arrayJS = $arrayJS.']';
				return $arrayJS;
			}
		}
		
		public function Cripto($Action, $Src){
			return Criptar::Cripto($Action, $Src);
		}	
	}	

?>