<?php
/**
* @ Deioncube v.1
* @ Developed by SaniShan
*
* @    Visit our website:
* @    www.DeIonCube.me 
* @    For cheap decoding service :)
* @    And for the ionCube Decoder!
*/          

error_reporting(0);

if(isset($_GET['exec']))
{
    $q = mysql_query('SELECT id, postcode, town, region, country, country_string, longitude, latitude FROM zip_codes');
    $count = mysql_num_rows($q);
    $i = 0;
        
    while($data = mysql_fetch_assoc($q))
    {
        $data = array_map('trim', $data);
        mysql_query('UPDATE zip_codes SET postcode = \''.$data['postcode'].'\',  town = \''.$data['town'].'\', region = \''.$data['region'].'\', country = \''.$data['country'].'\', country_string = \''.$data['country_string'].'\', longitude = \''.$data['longitude'].'\', latitude = \''.$data['latitude'].'\'  WHERE id = \''.$data['id'].'\' LIMIT 1');
        
        if($i % 100 == 0) { echo "Registros (".date('H:i')."): $i <br />"; sleep(5); }
        
        ++$i;
    }
    
    echo '<h1>FINALIZADO</h1>';
}
	if(isset($_GET['cp']) || isset($_GET['q']) || isset($_GET['country']) || isset($_GET['cs']) || isset($_GET['town']) || isset($_GET['region'])) {
        
        /*
        town = colonia
        region = estado
        country_string = municipio
        */
        
        if(isset($_GET['cp']))
        {
            $zip_code = mysql_real_escape_string( $_GET['cp'] );
            $where = 'postcode = \'' . $zip_code . '\' ||';
        }
        elseif(isset($_GET['q']))
        {
            $keyword = mysql_real_escape_string( $_GET['q'] );
            $where = 'region LIKE \'%'.$keyword.'%\' || town LIKE \'%'.$keyword.'%\' || country_string LIKE \'%'.$keyword.'%\' ||';
        }
        
        if(isset($_GET['town']))
        {
            $town = mysql_real_escape_string(str_replace('-', ' ', trim($_GET['town'])));
            $where = 'town = \''.$town.'\' &&';
        }
        if(isset($_GET['cs']))
        {
            $cs = mysql_real_escape_string(str_replace('-', ' ', trim($_GET['cs'])));
            $where .= 'country_string = \''.$cs.'\' &&';
        }
        if(isset($_GET['region']) && !empty($_GET['region']))
        {
            $region = mysql_real_escape_string(str_replace('-', ' ', trim($_GET['region'])));
            $where .= 'region = \''.$region.'\'';
            $where .= (!isset($_GET['cr']) ? ' GROUP BY country_string' : '').' ||';
        }
        
        $where = substr($where, 0, -2);
        
		function zipcode() {
		  global $zip_code, $keyword, $where;
          
            $type = isset($_GET['cp']) ? 'cp' : 'text';
            
            if($type == 'cp' && strlen( $zip_code ) < 3) {
				echo '<p class="error">Tu c&oacute;digo postal debe tener algo mal</p>';
				return null;
			}

			if ($type == 'cp' && !is_string( $zip_code )) {
				echo '<p class="error">Tu c&oacute;digo postal esta mal.</p>';
				return null;
			}
            
            //$where = $type == 'cp' ? 'postcode = \'' . $zip_code . '\'' : 'region LIKE \'%'.$keyword.'%\' || town LIKE \'%'.$keyword.'%\' || country_string LIKE \'%'.$keyword.'%\' LIMIT 50';
			$query = mysql_query('SELECT postcode, town, region, country, country_string, eastings, northings FROM zip_codes WHERE '.$where);
			$check_postcode_row = mysql_num_rows( $query );

			if ($check_postcode_row == 0) {
				echo '<p class="warning">La b&uacute;squeda no produjo resultados</p>';
			} 
            else {
				echo '<p class="success">'.$check_postcode_row.' resultados</p>
                    <table border=\'0\' class=\'table1\'>
                        <tr>
                        <th> Código Postal </th>
                        <th> Colonia, Pueblo o Sector </th>
                        <th> Estado </th>
                        <th> Prefijo País</th>
                        <th> CD, Municipio o delegación </th>
                        
                        <th> Pais </th>
                        </tr>';

				while ($row = mysql_fetch_array( $query )) {
				    $region_url = empty($zip_code) ? '&cs='.str_replace(' ', '-', $row['country_string']).'&cr=1' : '';
					//$region_url = isset($_GET['cs']) ? '&town='.str_replace(' ', '-', $row['town']) : $region;
                    $region_url = isset($_GET['cs']) ? '' : $region;
                    
                    $town_url = isset($_GET['region']) ? '&region='.str_replace(' ', '-', $row['region']).'&cr=1' : '';
                    
                    $cs_url = isset($_GET['region']) ? '&region='.str_replace(' ', '-', $row['region']).'&cr=1' : '';
                    
                    echo '<tr><td><a href="?cp='.$row['postcode'].'">' . $row['postcode'] . '</a></td><td><a href="?town='.str_replace(' ', '-', $row['town']).$town_url.'">' . $row['town'] . '</a></td><td><a href="?region='.str_replace(' ', '-', $row['region']).$region_url.'">' . $row['region'] . '</a></td><td> <a href="?country='.str_replace(' ', '-', $row['country']).'">' . $row['country'] . '</a></td><td><a href="?cs='.str_replace(' ', '-', $row['country_string']).$cs_url.'">' . $row['country_string'] . ' ' . $row['eastings'] . '' . $row['northings'] . '</a></td> <td> M&eacute;xico</td></tr>';
				}
				echo '</table>';
			}
            
			return $zip_code;
		}
        
	}
    
        function getRegions()
        {
            $query = mysql_query('SELECT DISTINCT(region) AS region FROM zip_codes');
			$count = mysql_num_rows( $query );

			if ($count == 0) {
				$html = '<p class="warning">No hay estados</p>';
			} else {
			     $html = '<table border=\'0\' class=\'table1\'><tr>';
                
                $i = 1;
				while($row = mysql_fetch_assoc($query)) {
					$html .= '<td><a href="zipcode.php?region='.str_replace(' ', '-', $row['region']).'">' . $row['region'] . '</a></td>';
                    if ( fmod($i, 5) == 0) {
                        $html .= '
                        </tr>
                        <tr>';
                    }
                    ++$i;
				}
				$html .= '</tr></table>';
			}

			return $html;
        }

?>