вопрос
Помогите мне с кодом? Мне нужно чтобы файл weather.txt записывался в utf8 без BOM сигнатуры.
Что мне нужно в коде подправить?
<?php
if(!defined('DATALIFEENGINE')) {
die("Hacking attempt!");
}
define('ROOT_DIR', '../..');
define('ENGINE_DIR', '..');
require_once ENGINE_DIR . '/data/config.php';
$file=array(
'update_time'=>4*60*60, // промежуток обновления кэша в секундах
'filename'=>ENGINE_DIR ."/cache/weather.txt", // куда писать кэш страницы
);
function get_city_name()
{
$city_name = '';
$ip_to_test= $_SERVER['REMOTE_ADDR'];
if ($ip_to_test!="")
{
$IP=strip_tags(substr($ip_to_test,0,20));
if (preg_match_all("!^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$!si", $IP, $match))
{
$bits['a']=implode($match[1]);
$bits['b']=implode($match[2]);
$bits['c']=implode($match[3]);
$bits['d']=implode($match[4]);
}
else
{
//echo"Illegal IP-address: <font color=\"red\">$IP</font> Enter another IP-address<br>";
return $city_name;
}
$error = 0;
if (!($bits['a'] >= '0' and $bits['a'] <= '255')) $error=1;
if (!($bits['b'] >= '0' and $bits['b'] <= '255')) $error=1;
if (!($bits['c'] >= '0' and $bits['c'] <= '255')) $error=1;
if (!($bits['d'] >= '0' and $bits['d'] <= '255')) $error=1;
if ($error == 0)
{
$ADDRESS = $bits['a']*256*256*256+$bits['b']*256*256+$bits['c']*256+$bits['d'];
$master_fale= ROOT_DIR .'/dbcity/cidr_ru_master_index.db';
$master_record=file($master_fale);
$master_index=0;
foreach($master_record as $key=>$master_new_record)
{
$new_record=trim($new_record);
list($start,$stop,$inetnum,$country,$city,$region,$district,$status,$slave,$n_slave) = preg_split('!\t!', $master_new_record);
$master[$master_index] =array(start => $start,stop => $stop,inetnum => $inetnum,country => $country,city=>$city,region=>$region,district=>$district,status => $status,slave => $slave,n_slave => $n_slave);
$master_index++;
}
unset($master_record);
foreach($master as $key=>$new_master)
{
if ($ADDRESS>=$new_master['start'] && $ADDRESS<=$new_master['stop'])
$NEW_ADDRESS_0=$new_master;
}
unset($master);
if ($NEW_ADDRESS_0!="")
{
$block_0 = $NEW_ADDRESS_0['inetnum'];
$country_0 = $NEW_ADDRESS_0['country'];
$city_0 = $NEW_ADDRESS_0['city'];
$region_0 = $NEW_ADDRESS_0['region'];
$district_0 = $NEW_ADDRESS_0['district'];
$status_0 = $NEW_ADDRESS_0['status'];
$block_0= "Block:\t".$block_0."<br>\n".
"Country: \t".$country_0."<br>\n".
"City: \t".$city_0."<br>\n".
"Region: \t".$region_0."<br>\n".
"District: \t".$district_0."<br>\n".
"Status: \t".$status_0."<br><br>\n";
$city_name = $city_0;
}
else
{
//echo"No RIPE-region. Enter another IP-address.<br>\n";
$NEW_ADDRESS_0['slave']=-1;
}
if ($NEW_ADDRESS_0['slave']!=-1)
{
$slave_fale= ROOT_DIR .'/dbcity/cidr_ru_slave_index.db';
$slave_record=file ($slave_fale);
$slave_index=0;
$min=$NEW_ADDRESS_0['slave'];
$max=$min+$NEW_ADDRESS_0['n_slave'];
while($min<$max)
{
$slave_record[$min]=trim($slave_record[$min]);
list($start,$stop,$inetnum,$country,$city,$region,$district,$status,$rest) = preg_split('!\t!', $slave_record[$min]);
$_slave[$slave_index]=array(start => $start,stop => $stop,inetnum => $inetnum,country => $country,city=>$city,region=>$region,district=>$district,status=>$status);
$slave_index++;
$min++;
}
foreach($_slave as $key=>$new_slave)
{
if ($ADDRESS>=$new_slave['start'] && $ADDRESS<=$new_slave['stop'])
$NEW_ADDRESS_1=$new_slave;
}
$block_1 = $NEW_ADDRESS_1['inetnum'];
$country_1 = $NEW_ADDRESS_1['country'];
$city_1 = $NEW_ADDRESS_1['city'];
$region_1 = $NEW_ADDRESS_1['region'];
$district_1 = $NEW_ADDRESS_1['district'];
$status_1 = $NEW_ADDRESS_1['status'];
$block_1="Block:\t".$block_1."<br>\n".
"Country: \t".$country_1."<br>\n".
"City: \t".$city_1."<br>\n".
"Region: \t".$region_1."<br>\n".
"District: \t".$district_1."<br>\n".
"Status: \t".$status_1."<br>\n";
//echo $block_1;
$city_name = $city_1;
}
else
{
//echo $block_0;
}
}
else
{
//echo"Illegal IP-address: <font color=\"red\">$IP.</font> Enter another IP-address<br>";
}
}
return $city_name;
}
unset ($citynum);
unset ($city_auto);
$city_auto = '';
$city_auto = get_city_name();
require_once('engine/classes/gismeteo/baza_gismeteo.php');
$xmlq = @file_get_contents($file['filename']);
$gg=explode("tuxon",$xmlq);
// проверим сколько времени прошло
if (time()-$gg[0]>$file['update_time'])
{
//считываем содержимое файла в переменную $xml
$xmlq = file_get_contents('http://informer.gismeteo.ru/xml/'.$citynum.'_1.xml');
//передаём содержимое xml файла расширению SimpleXML
$res1 = simplexml_load_string($xmlq);
//Запускаем цикл для отображения погоды
for ($i = 0; $i < count ($res1->REPORT->TOWN->FORECAST); $i++) {
//время суток прогноза погоды
switch ((int)$res1->REPORT->TOWN->FORECAST[$i]->attributes()->tod):
case 0:$time = 'ночь'; $cs='t';break;
case 1:$time = 'утро'; $cs='s';break;
case 2:$time = 'день'; $cs='s';break;
case 3:$time = 'вечер';$cs='t';break;
default:$time = '';break;
endswitch;
//день недели на который осуществляется прогноз
switch ((int)$res1->REPORT->TOWN->FORECAST[$i]->attributes()->weekday):
case 1:$weekday = 'Воскресенье';break;
case 2:$weekday = 'Понедельник';break;
case 3:$weekday = 'Вторник';break;
case 4:$weekday = 'Среда';break;
case 5:$weekday = 'Четверг';break;
case 6:$weekday = 'Пятница';break;
case 7:$weekday = 'Суббота';break;
default:$weekday = '';break;
endswitch;
//выбор облачности
switch ((int) $res1->REPORT->TOWN->FORECAST[$i]->PHENOMENA->attributes()->cloudiness):
case 0:$cloudiness = 'ясно';$cl=1;break;
case 1:$cloudiness = 'малооблачно';$cl=2;break;
case 2:$cloudiness = 'облачно';$cl=3;break;
case 3:$cloudiness = 'пасмурно';$cl=4;break;
default:$cloudiness = '';break;
endswitch;
//определение типа осадков
switch ((int) $res1->REPORT->TOWN->FORECAST[$i]->PHENOMENA->attributes()->precipitation):
case 4:$precipitation = 'дождь';$cd=1;break;
case 5:$precipitation = 'ливень';$cd=2;break;
case 6:$precipitation = 'снег';$cd=3;break;
case 7:$precipitation = 'снег';$cd=3;break;
case 8:$precipitation = 'гроза';$cd=2;break;
case 9:$precipitation = 'нет данных';$cd=5;break;
case 10:$precipitation = 'без осадков';$cd=0;break;
default:$precipitation = '';break;
endswitch;
//определение направления ветра
switch ((int) $res1->REPORT->TOWN->FORECAST[$i]->WIND->attributes()->direction):
case 0:$w_direct = 'с';break;
case 1:$w_direct = 'с-в';break;
case 2:$w_direct = 'в';break;
case 3:$w_direct = 'ю-в';break;
case 4:$w_direct = 'ю';break;
case 5:$w_direct = 'ю-з';break;
case 6:$w_direct = 'з';break;
case 7:$w_direct = 'с';break;
default:$w_direct = '';break;
endswitch;
//город для которого осуществляется прогноз
$namecity = urldecode ($res1->REPORT->TOWN->attributes()->sname);
//день на который осуществляется прогноз
$daygismeteo = $res1->REPORT->TOWN->FORECAST[$i]->attributes()->day;
//месяц на который осуществляется прогноз
$monthgismeteo = $res1->REPORT->TOWN->FORECAST[$i]->attributes()->month;
//год на который осуществляется прогноз
$yeargismeteo = $res1->REPORT->TOWN->FORECAST[$i]->attributes()->year;
// температура
$t_min = $res1->REPORT->TOWN->FORECAST[$i]->TEMPERATURE->attributes()->min;
$t_max = $res1->REPORT->TOWN->FORECAST[$i]->TEMPERATURE->attributes()->max;
if ($t_min >0 ) $t_min='+'.$t_min;
if ($t_max >0 ) $t_max='+'.$t_max;
// давление
$d_min = $res1->REPORT->TOWN->FORECAST[$i]->PRESSURE->attributes()->min;
$d_max = $res1->REPORT->TOWN->FORECAST[$i]->PRESSURE->attributes()->max;
// скорость ветра
$w_min = $res1->REPORT->TOWN->FORECAST[$i]->WIND->attributes()->min;
$w_max = $res1->REPORT->TOWN->FORECAST[$i]->WIND->attributes()->max;
// влажность
$r_min = $res1->REPORT->TOWN->FORECAST[$i]->RELWET->attributes()->min;
$r_max = $res1->REPORT->TOWN->FORECAST[$i]->RELWET->attributes()->max;
// комфорт
$h_min = $res1->REPORT->TOWN->FORECAST[$i]->HEAT->attributes()->min;
$h_max = $res1->REPORT->TOWN->FORECAST[$i]->HEAT->attributes()->max;
if ($h_min >0 ) {$h_min='+'.$h_min;$h_col='+';}
if ($h_max >0 ) {$h_max='+'.$h_max;$h_col='+';}
//выводим пользователю прогноз
$gismeteo .="<div id='modpogoda'><br>".$daygismeteo.".".$monthgismeteo.".".$yeargismeteo." <span class='mptime'> ".$weekday." ".$time."</span></div>
<table><tr>
<td valign='top'><img src='//s2356.онега-авто.рф/img/pogoda/".$cs.$cl.$cd.".png' alt='' title='".$cloudiness.", ".$precipitation.", комфорт ".$h_min."...".$h_max." °C'width='48'><br></td>
<td><span class='other2'>".$cloudiness.", ".$precipitation."</span><br>
<span class='tempp'>".$t_min." ... ".$t_max." °C</span><br>
<span class='other'>давление: ".$d_min."...".$d_max." мм<br>
ветер: ".$w_direct." ".$w_min."...".$w_max." м/с<br>
влажность: ".$r_min."...".$r_max." %<br>
комфорт: ".$h_min."...".$h_max." °C</span><br>
</td>
</tr></table>";}
$gg[1]="<div class='namecity'>".$namecity."</div>".$gismeteo;
$fp=fopen($file['filename'],'wb+');
//fwrite($fp, pack("ССС",0xEF,0xBB,0xBF)); //Пишем сигнатуру UTF-8
fwrite($fp,b"\xEF\xBB\xBF".$con);
fwrite($fp, time().'tuxon'.$gg[1]);
fclose($fp);
@chmod($file['filename'], 0666);
}
@header("HTTP/1.0 200 OK");
@header("HTTP/1.1 200 OK");
@header("Cache-Control: no-cache, must-revalidate, max-age=0");
@header("Expires: 0");
@header("Pragma: no-cache");
$tpl->result['gismeteo'] .=$gg[1];
?>
Что мне нужно в коде подправить?
$gg[1]="<div class='namecity'>".$namecity."</div>".$gismeteo;
$fp=fopen($file['filename'],'wb+');
//fwrite($fp, pack("ССС",0xEF,0xBB,0xBF)); //Пишем сигнатуру UTF-8
fwrite($fp,b"\xEF\xBB\xBF".$con);
fwrite($fp, time().'tuxon'.$gg[1]);
fclose($fp);
@chmod($file['filename'], 0666);