forked from relev/ziptastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ziptastic.php
207 lines (175 loc) · 5.73 KB
/
ziptastic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?
//ini_set("display_errors", "1"); error_reporting(E_ALL ^ E_NOTICE);
require( 'ziptastic_db.php' ); // DB settings for db_connect()
function db_connect()
{
$db = mysql_connect( DB_HOST, DB_USER, DB_PASS );
mysql_select_db( DB_NAME );
}
function find_street( $street = false )
{
$res = array();
if ( $street )
{
db_connect();
mysql_query( 'set group_concat_max_len=1024*1024' );
$resource = mysql_query( "SELECT type_short, name, GROUP_CONCAT(DISTINCT CONCAT(region,',',city,',',zip,',',area) ORDER BY zip SEPARATOR ';') AS localities FROM ziptastic_street LEFT JOIN PIndx07 USING(zip) WHERE name LIKE '$street%' GROUP BY type_short, name ORDER BY type_weight DESC, name ASC LIMIT 20" );
if ( $resource )
{
while( $row = mysql_fetch_assoc($resource) )
{
// $row['localities'] = prepare_localities($row['localities']);
// $row['type_short'] = prepare_type_short($row['type_short']);
$res[] = $row;
}
}
}
return $res;
}
function find_zip( $zip = 0 )
{
$zip = (int)$zip;
$res = 0;
if ( $zip > 0 )
{
db_connect();
$resource = mysql_query( "SELECT zip, region, area, IF(city_1='',city,CONCAT(city,' (',city_1,')')) AS city FROM PIndx07 WHERE zip=$zip" );
if ( $resource )
$res = mysql_fetch_assoc( $resource );
}
return $res;
}
function mb_uc_first( $word, $need_lower_case = true )
{
return mb_strtoupper( mb_substr($word, 0, 1, 'UTF-8'), 'UTF-8')
. mb_substr( $need_lower_case ? mb_convert_case($word, MB_CASE_LOWER, 'UTF-8') : $word, 1, mb_strlen($word), 'UTF-8');
}
function mb_uc_words( $string, $word_separators = ' ')
{
$string = mb_uc_first( $string );
for ( $j = strlen( $word_separators ) - 1; $j >= 0; $j-- )
{
$words = mb_split( $word_separators[$j], $string );
for ( $i = count($words)-1; $i >= 0; $i-- )
$words[$i] = mb_uc_first( $words[$i], false );
$string = implode( $word_separators[$j], $words );
}
return $string;
}
function prepare_type_short( $type_short )
{
return ( mb_strlen($type_short, 'UTF-8') > 3 || mb_strpos( $type_short, '-' ) !== false )
? $type_short
: $type_short.'.';
}
/*
function prepare_localities( $localities )
{
$locality = mb_split( ';', $localities );
for( $i = count($locality)-1; $i >= 0; $i-- )
$locality[$i] = prepare_locality( $locality[$i] );
return implode( ';', $locality );
}
function prepare_locality($locality)
{
list( $region, $city, $zip ) = mb_split( ',', $locality );
$region = prepare_region( $region );
$city = prepare_city( $city );
return implode( ',', array( $region, $city, $zip ) );
}
*/
$FIX_REGIONS = array(
' Область' => ' область',
' Край' => ' край',
'якутия' => 'Якутия',
'балкарская' => 'Балкарская',
'черкесская' => 'Черкесская',
'алания' => 'Алания',
);
function prepare_region( $region )
{
global $FIX_REGIONS, $SEEN;
$r = mb_uc_words( $region );
if ( mb_strpos( $r, 'Респу' ) !== false && mb_strpos( $r, 'ская ' ) === false )
{
$r = str_replace( ' Республика', '', $r );
$r = str_replace( ' Республи', '', $r );
$r = 'Республика '.$r;
}
foreach ( $FIX_REGIONS as $k => $v )
$r = str_replace( $k, $v, $r );
return $r;
}
function prepare_city( $city )
{
$city = mb_uc_words( $city, ' -' );
$city = str_replace( '-На-', '-на-', $city );
return $city;
}
function array_compact( $array )
{
foreach ( $array as $key => $val )
if ( empty($val) )
unset($array[$key]);
return $array;
}
function fix_region_city()
{
db_connect();
// truncate table PIndx07; insert into PIndx07 select * from PIndx07_all;
$resource = mysql_query( 'SELECT * FROM PIndx07 WHERE region NOT LIKE "%а%"' );
while( $row = mysql_fetch_assoc($resource) )
{
if ( !$row['city'] )
{
list( $region, $city ) = $row['region'] == 'МОСКВА'
? array( 'Московская область', 'Москва' )
: array( 'Ленинградская область', 'Санкт-Петербург' );
}
else
{
$region = prepare_region($row['region']);
$city = prepare_city($row['city']);
}
$city_1 = $row['city_1'] ? prepare_city($row['city_1']) : '';
$area = $row['area'] ? mb_uc_first($row['area']) : '';
mysql_query( sprintf( 'UPDATE PIndx07 SET region="%s", city="%s", city_1="%s", area="%s" WHERE zip=%d', $region, $city, $city_1, $area, $row['zip'] ) );
}
}
function fix_type_short()
{
db_connect();
mysql_query( 'UPDATE ziptastic_street SET type_short = CONCAT(type_short,".") WHERE LENGTH(type_short)<15 AND type_short NOT LIKE "%-%" AND type_short NOT LIKE "%."' );
}
$query = addslashes( $_GET['q'] );
/** /
if ( true || $query == 'fix' )
{
fix_region_city();
// fix_type_short();
print 'fix';
exit;
}
/**/
$res = preg_match('/^\d{6}$/', $query )
? find_zip( $query )
: ( ( mb_strlen( $query ) < 2 || preg_match( '/\d.*\d.*\d/', $query ) )
? false
: find_street( mb_uc_first($query) ) );
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8', TRUE);
if ( !empty($res) )
{
//print_r($res); exit;
$json = json_encode(array_compact($res));
$out = $_GET['callback']
? sprintf( '%s(%s);', $_GET['callback'], $json )
: $json;
echo $out;
}
else
{
header( 'HTTP/1.0 404 Not Found' );
echo '{}';
}
?>