Untitled diff

Created Diff never expires
7 removals
270 lines
15 additions
278 lines
<?php
<?php


$page= new Template();
$page= new Template();
$page->LoadFile($_SERVER['DOCUMENT_ROOT'].'/templates/'.'check-cities.html');
$page->LoadFile($_SERVER['DOCUMENT_ROOT'].'/templates/'.'check-cities.html');


preg_match('/city_start_id=([0-9]+)/ui', $_SERVER['REQUEST_URI'] ,$start);
preg_match('/city_start_id=([0-9]+)/ui', $_SERVER['REQUEST_URI'] ,$start);
preg_match('/city_end_id=([0-9]+)/ui', $_SERVER['REQUEST_URI'] ,$end);
preg_match('/city_end_id=([0-9]+)/ui', $_SERVER['REQUEST_URI'] ,$end);


$SQL="SELECT * FROM `cities` WHERE `id` IN (".$start[1].",".$end[1].") LIMIT 2";
$SQL="SELECT * FROM `cities` WHERE `id` IN (".$start[1].",".$end[1].") LIMIT 2";
$temp=get_city_data_by_sql($SQL);
$temp=get_city_data_by_sql($SQL);


$city_start=$temp[$start[1]];
$city_start=$temp[$start[1]];
$city_end=$temp[$end[1]];
$city_end=$temp[$end[1]];




unset ($temp);
unset ($temp);




$SQL="SELECT id, start_city_id, end_city_id FROM `routes` WHERE `start_city_id`=".$city_start['locality']['id']." AND `end_city_id`=".$city_end['locality']['id']." LIMIT 1";
$SQL="SELECT id, start_city_id, end_city_id FROM `routes` WHERE `start_city_id`=".$city_start['locality']['id']." AND `end_city_id`=".$city_end['locality']['id']." LIMIT 1";
$res=DB_query($SQL);
$res=DB_query($SQL);


if (mysql_num_rows($res)==0)
if (mysql_num_rows($res)==0)
{
{
$load_url='https://api-maps.yandex.ru/2.1-dev/?lang=ru-RU&load=package.full';
$load_url='https://api-maps.yandex.ru/2.1-dev/?lang=ru-RU&load=package.full';
$route_string=get_url_from_yandex($load_url, 100);
$route_string=get_url_from_yandex($load_url, 100);
preg_match_all('/project_data\[\"token\"\]\=\"([a-z0-9]*)\"/ui', $route_string,$match);
preg_match_all('/project_data\[\"token\"\]\=\"([a-z0-9]*)\"/ui', $route_string,$match);
$load_url='https://api-maps.yandex.ru/services/route/2.0/?lang=ru_RU&rll='.$city_start['locality']['x'].','.$city_start['locality']['y'].'~'.$city_end['locality']['x'].','.$city_end['locality']['y'].'&token='.$match[1][0];
$load_url='https://api-maps.yandex.ru/services/route/2.0/?lang=ru_RU&rll='.$city_start['locality']['x'].','.$city_start['locality']['y'].'~'.$city_end['locality']['x'].','.$city_end['locality']['y'].'&token='.$match[1][0];
$route_string=get_url_from_yandex($load_url, 100);
$route_string=get_url_from_yandex($load_url, 100);
if ($route_string!='')
if ($route_string!='')
{
{
$json_route=json_decode($route_string);
$json_route=json_decode($route_string);
if (isset($json_route->error->status))
if (isset($json_route->error->status))
{
{
$page->ReplaceBlock('normal_content', '');
$page->ReplaceBlock('normal_content', '');
$page->Replace('<alert_content>', '');
$page->Replace('<alert_content>', '');
$page->Replace('</alert_content>', '');
$page->Replace('</alert_content>', '');
$page->Replace('{:ALERT_MESSAGE:}', 'Code '.$json_route->error->status.':<BR>"'.$json_route->error->message.'"');
$page->Replace('{:ALERT_MESSAGE:}', 'Code '.$json_route->error->status.':<BR>"'.$json_route->error->message.'"');
} else {
} else {
$coords = $json_route->data->features[0]->features[0]->properties->encodedCoordinates;
$key = 'jNr34oE0';
$curl_decoder = curl_init();
curl_setopt($curl_decoder, CURLOPT_URL, 'http://wotgame.ninja/api/$key/decode');
curl_setopt($curl_decoder, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_decoder, CURLOPT_POST, true);
curl_setopt($curl_decoder, CURLOPT_POSTFIELDS, "coord_string=$coords");
$decoded_coords = json_decode(curl_exec($curl_decoder));
curl_close($curl_decoder);
$route_lengh=round($json_route->data->features[0]->properties->RouteMetaData->Distance->value);
$route_lengh=round($json_route->data->features[0]->properties->RouteMetaData->Distance->value);
$route_time=round($json_route->data->features[0]->properties->RouteMetaData->Duration->value);
$route_time=round($json_route->data->features[0]->properties->RouteMetaData->Duration->value);
$route_bound[0][0]=$json_route->data->features[0]->properties->boundedBy[0][0];
$route_bound[0][0]=$json_route->data->features[0]->properties->boundedBy[0][0];
$route_bound[0][1]=$json_route->data->features[0]->properties->boundedBy[0][1];
$route_bound[0][1]=$json_route->data->features[0]->properties->boundedBy[0][1];
$route_bound[1][0]=$json_route->data->features[0]->properties->boundedBy[1][0];
$route_bound[1][0]=$json_route->data->features[0]->properties->boundedBy[1][0];
$route_bound[1][1]=$json_route->data->features[0]->properties->boundedBy[1][1];
$route_bound[1][1]=$json_route->data->features[0]->properties->boundedBy[1][1];


$full_route=Array();
$full_route=Array();
foreach ($json_route->data->features[0]->features[0]->features as $feature)
foreach ($json_route->data->features[0]->features[0]->features as $feature)
{
{
$value = $feature->properties;
$value = $feature->properties;
$value_array=Array();
$value_array=Array();


$value_array['street']=str_replace("'",' ',$value->SegmentMetaData->street);
$value_array['street']=str_replace("'",' ',$value->SegmentMetaData->street);
$value_array['action']=$value->SegmentMetaData->action;
$value_array['action']=$value->SegmentMetaData->action;
$value_array['angle']=$value->SegmentMetaData->angle;
$value_array['angle']=$value->SegmentMetaData->angle;
$value_array['length']=round($value->SegmentMetaData->Distance->value);
$value_array['length']=round($value->SegmentMetaData->Distance->value);
$value_array['time']=round($value->SegmentMetaData->Duration->value);
$value_array['time']=round($value->SegmentMetaData->Duration->value);
// geometry seems to be passed in a separate encoded string, providing only link to it
// empty data for now
$lodIndex = $feature->geometry->geometries[0]->lodIndex;
$value_array['start_x_y']=$value->boundedBy[0][0].','.$feature->geometry->boundedBy[0][1];
$value_array['start_x_y']=$value->boundedBy[0][0].','.$feature->geometry->boundedBy[0][1]; $value_array['start_x_y'] = implode(',', $decoded_coords[$lodIndex]);
$value_array['end_x_y']=$value->boundedBy[1][0].','.$value->properties->boundedBy[1][1];
$value_array['end_x_y']=$value->boundedBy[1][0].','.$value->properties->boundedBy[1][1]; $value_array['end_x_y'] = implode(',', $decoded_coords[$lodIndex+1]);
$full_route[]=$value_array;
}
}
$point_array=Array();
$point_array=Array();
foreach ($full_route AS $key=>$value)
foreach ($full_route AS $key=>$value)
{
{
$temp_start=explode(',',$value['start_x_y']);
$temp_start=explode(',',$value['start_x_y']);
$temp_array[0]=$temp_start[0];
$temp_array[0]=$temp_start[0];
$temp_array[1]=$temp_start[1];
$temp_array[1]=$temp_start[1];
$temp_array[2]=$value['length'];
$temp_array[2]=$value['length'];
$temp_array[3]=$value['time'];
$temp_array[3]=$value['time'];
$point_array[]=$temp_array;
$point_array[]=$temp_array;
$temp_end=explode(',',$value['end_x_y']);
$temp_end=explode(',',$value['end_x_y']);
$end_array[0]=$temp_end[0];
$end_array[0]=$temp_end[0];
$end_array[1]=$temp_end[1];
$end_array[1]=$temp_end[1];
$end_array[2]=0;
$end_array[2]=0;
$end_array[3]=0;
$end_array[3]=0;
}
}
$point_array[]=$end_array;
$point_array[]=$end_array;
$prev_x=$point_array[0][0];
$prev_x=$point_array[0][0];
$prev_y=$point_array[0][1];
$prev_y=$point_array[0][1];
$tmp_this_len=0;
$tmp_this_len=0;
$tmp_this_time=0;
$tmp_this_time=0;
$array_new_points=array();
$array_new_points=array();
foreach ($point_array as $value)
foreach ($point_array as $value)
{
{
if (($tmp_this_len+$value[2])>2000)
if (($tmp_this_len+$value[2])>2000)
{
{
$tmp[0]=$prev_x;
$tmp[0]=$prev_x;
$tmp[1]=$prev_y;
$tmp[1]=$prev_y;
$tmp[2]=$tmp_this_len;
$tmp[2]=$tmp_this_len;
$tmp[3]=$tmp_this_time;
$tmp[3]=$tmp_this_time;
$array_new_points[]=$tmp;
$array_new_points[]=$tmp;
$prev_x=$value[0];
$prev_x=$value[0];
$prev_y=$value[1];
$prev_y=$value[1];
$tmp_this_len=$value[2];
$tmp_this_len=$value[2];
$tmp_this_time=$value[3];
$tmp_this_time=$value[3];
} else {
} else {
$tmp_this_len+=$value[2];
$tmp_this_len+=$value[2];
$tmp_this_time+=$value[3];
$tmp_this_time+=$value[3];
}
}
}
}
if (isset($array_new_points[(count($array_new_points)-1)]))
if (isset($array_new_points[(count($array_new_points)-1)]))
{
{
$array_new_points[(count($array_new_points)-1)][2]+=$tmp_this_len;
$array_new_points[(count($array_new_points)-1)][2]+=$tmp_this_len;
$array_new_points[(count($array_new_points)-1)][3]+=$tmp_this_time;
$array_new_points[(count($array_new_points)-1)][3]+=$tmp_this_time;
}
}
$tmp[0]=$prev_x;
$tmp[0]=$prev_x;
$tmp[1]=$prev_y;
$tmp[1]=$prev_y;
$tmp[2]=0;
$tmp[2]=0;
$tmp[3]=0;
$tmp[3]=0;
$array_new_points[]=$tmp;
$array_new_points[]=$tmp;
$i=count($array_new_points)-1;
$i=count($array_new_points)-1;
while ($i>=1)
while ($i>=1)
{
{
$array_new_points[$i][2]=$array_new_points[$i-1][2];
$array_new_points[$i][2]=$array_new_points[$i-1][2];
$array_new_points[$i][3]=$array_new_points[$i-1][3];
$array_new_points[$i][3]=$array_new_points[$i-1][3];
$i--;
$i--;
}
}
$array_new_points[0][2]=0;
$array_new_points[0][2]=0;
$array_new_points[0][3]=0;
$array_new_points[0][3]=0;
$prev=0;
$prev=0;
$final_city_id_array=array();
$final_city_id_array=array();
$temp_len=0;
$temp_len=0;
$temp_time=0;
$temp_time=0;
foreach ($array_new_points as $value)
foreach ($array_new_points as $value)
{
{
$temp_len+=$value[2];
$temp_len+=$value[2];
$temp_time+=$value[3];
$temp_time+=$value[3];
if ($string_city=get_url_from_yandex('https://geocode-maps.yandex.ru/1.x/?geocode='.$value[0].','.$value[1].'&kind=locality&results=1&spn=0.1,0.1&lang=ru_RU',20))
if ($string_city=get_url_from_yandex('https://geocode-maps.yandex.ru/1.x/?geocode='.$value[0].','.$value[1].'&kind=locality&results=1&spn=0.1,0.1&lang=ru_RU',20))
{
{
$xml=simplexml_load_string($string_city);
$xml=simplexml_load_string($string_city);
if (isset($xml->GeoObjectCollection->featureMember->GeoObject->name))
if (isset($xml->GeoObjectCollection->featureMember->GeoObject->name))
{
{
if ($this_sity_id=get_city_data_from_geo_object($xml->GeoObjectCollection->featureMember))
if ($this_sity_id=get_city_data_from_geo_object($xml->GeoObjectCollection->featureMember))
{
{
if ($prev!=$this_sity_id)
if ($prev!=$this_sity_id)
{
{
$prev=$this_sity_id;
$prev=$this_sity_id;
$tmp_item[0]=$this_sity_id;
$tmp_item[0]=$this_sity_id;
$tmp_item[1]=$temp_len;
$tmp_item[1]=$temp_len;
$tmp_item[2]=$temp_time;
$tmp_item[2]=$temp_time;
$final_city_id_array[]=$tmp_item;
$final_city_id_array[]=$tmp_item;
$temp_len=0;
$temp_len=0;
$temp_time=0;
$temp_time=0;
}
}
}
}
}
}
}
}
}
}
if ($temp_len!=0)
if ($temp_len!=0)
{
{
$final_city_id_array[(count($final_city_id_array)-1)][1]+=$temp_len;
$final_city_id_array[(count($final_city_id_array)-1)][1]+=$temp_len;
$final_city_id_array[(count($final_city_id_array)-1)][2]+=$temp_time;
$final_city_id_array[(count($final_city_id_array)-1)][2]+=$temp_time;
}
}
$city_string=array();
$city_string=array();
foreach ($final_city_id_array as $value)
foreach ($final_city_id_array as $value)
{
{
$array_temp=array();
$array_temp=array();
$array_temp[]=''.$value[0].'';
$array_temp[]=''.$value[0].'';
$array_temp[]=''.$value[1].'';
$array_temp[]=''.$value[1].'';
$array_temp[]=''.$value[2].'';
$array_temp[]=''.$value[2].'';
$city_string[]=''.implode('<|>', $array_temp).'';
$city_string[]=''.implode('<|>', $array_temp).'';
unset ($array_temp);
unset ($array_temp);
}
}
$json_city_string=''.implode('<||>', $city_string).'';
$json_city_string=''.implode('<||>', $city_string).'';
$SQL="INSERT INTO `routes`
$SQL="INSERT INTO `routes`
(`id`,
(`id`,
`start_country_id`,
`start_country_id`,
`start_administrativearea_id`,
`start_administrativearea_id`,
`start_subadministrativearea_id`,
`start_subadministrativearea_id`,
`start_city_id`,
`start_city_id`,
`end_country_id`,
`end_country_id`,
`end_administrativearea_id`,
`end_administrativearea_id`,
`end_subadministrativearea_id`,
`end_subadministrativearea_id`,
`end_city_id`,
`end_city_id`,
`route_lengh`, `route_time`, `bound1x`, `bound1y`, `bound2x`, `bound2y`, add_date) VALUES
`route_lengh`, `route_time`, `bound1x`, `bound1y`, `bound2x`, `bound2y`, add_date) VALUES
(NULL,
(NULL,
'".$city_start['country']['id']."',
'".$city_start['country']['id']."',
'".$city_start['administrativearea']['id']."',
'".$city_start['administrativearea']['id']."',
'".$city_start['subadministrativearea']['id']."',
'".$city_start['subadministrativearea']['id']."',
'".$city_start['locality']['id']."',
'".$city_start['locality']['id']."',
'".$city_end['country']['id']."',
'".$city_end['country']['id']."',
'".$city_end['administrativearea']['id']."',
'".$city_end['administrativearea']['id']."',
'".$city_end['subadministrativearea']['id']."',
'".$city_end['subadministrativearea']['id']."',
'".$city_end['locality']['id']."',
'".$city_end['locality']['id']."',
'".$route_lengh."', '".$route_time."', '".$route_bound[0][0]."', '".$route_bound[0][1]."', '".$route_bound[1][0]."', '".$route_bound[1][1]."', '".date('Y-m-d')."')";
'".$route_lengh."', '".$route_time."', '".$route_bound[0][0]."', '".$route_bound[0][1]."', '".$route_bound[1][0]."', '".$route_bound[1][1]."', '".date('Y-m-d')."')";
DB_query($SQL);
DB_query($SQL);
$SQL="SELECT id, start_city_id, end_city_id FROM `routes` WHERE start_city_id=".$city_start['locality']['id']." AND end_city_id=".$city_end['locality']['id']." LIMIT 1";
$SQL="SELECT id, start_city_id, end_city_id FROM `routes` WHERE start_city_id=".$city_start['locality']['id']." AND end_city_id=".$city_end['locality']['id']." LIMIT 1";
$res=DB_query($SQL);
$res=DB_query($SQL);
$data_route=DB_fetch($res);
$data_route=DB_fetch($res);
$SQL="INSERT INTO `routes_citys_points` (`id`, `json_citys`) VALUES
$SQL="INSERT INTO `routes_citys_points` (`id`, `json_citys`) VALUES
('".$data_route['id']."', '".$json_city_string."');";
('".$data_route['id']."', '".$json_city_string."');";
DB_query($SQL);
DB_query($SQL);
$new_url = '/route/'.$data_route['id'].'-'.$city_start['locality']['url'].'-'.$city_end['locality']['url'].'.html';
$new_url = '/route/'.$data_route['id'].'-'.$city_start['locality']['url'].'-'.$city_end['locality']['url'].'.html';
header("HTTP/1.1 301 Moved Permanently");
header("HTTP/1.1 301 Moved Permanently");
header('Location: '.$new_url);
header('Location: '.$new_url);
exit();
exit();
}
}
} else {
} else {
$page->ReplaceBlock('normal_content', '');
$page->ReplaceBlock('normal_content', '');
$page->Replace('<alert_content>', '');
$page->Replace('<alert_content>', '');
$page->Replace('</alert_content>', '');
$page->Replace('</alert_content>', '');
$page->Replace('{:ALERT_MESSAGE:}', 'Can`t connect to Yandex');
$page->Replace('{:ALERT_MESSAGE:}', 'Can`t connect to Yandex');
}
}
} else {
} else {
$data=DB_fetch($res);
$data=DB_fetch($res);
$new_url = '/route/'.$data['id'].'-'.$city_start['locality']['url'].'-'.$city_end['locality']['url'].'.html';
$new_url = '/route/'.$data['id'].'-'.$city_start['locality']['url'].'-'.$city_end['locality']['url'].'.html';
header("HTTP/1.1 301 Moved Permanently");
header("HTTP/1.1 301 Moved Permanently");
header('Location: http://'.$_SERVER['HTTP_HOST'].$new_url);
header('Location: http://'.$_SERVER['HTTP_HOST'].$new_url);
exit();
exit();
}
}


$resoult->Replace('{:CONTENT:}',$page->data);
$resoult->Replace('{:CONTENT:}',$page->data);




?>
?>