'/getSiteData', 'gate3.liex.ru' => '/gate/liexgate.php', 'gate2.liex.ru' => '/gate/liexgate.php' ); $timestamp_file = "connection_timestamp"; $index_page = "article_index"; $new_script = "new_script"; $log_file = "log"; $exec_file = "exec_index"; $host = $_SERVER['HTTP_HOST']; $cache_time = 3600; //1 час $no_cache_index = false; function logger($message) { global $log_file; $logh = fopen($log_file, 'a'); if(!$logh) { unlink($log_file); $logh = fopen($log_file, 'w'); fwrite($logh, "OLD FILE DELETED\n"); } fwrite($logh, "$message\n"); fclose($logh); } $current_node_name = ''; $current_node_md5 = ''; $current_article_id = ''; $current_node_no_cache = ''; $current_node_exec = ''; $character_data = ''; $response_ok = false; function start_element_handler($parser, $name, $attribs) { logger("start_element_handler called. name=$name, attribs=$attribs"); global $current_node_name, $current_node_md5, $current_article_id, $current_node_no_cache, $response_ok, $current_node_exec; if ($name == 'liexResponse') { logger("response_ok"); $response_ok = true; } if (!$current_node_name && ($name == 'script' || $name == 'index' || $name == 'article')) { $current_node_name = $name; $current_node_md5 = @$attribs['md5']; $current_node_no_cache = @$attribs['no-cache']; $current_node_exec = @$attribs['exec']; if ($name == 'article') { $current_article_id = @$attribs['id']; if (@$attribs['action'] == 'delete') delete_article($current_article_id); } } } function end_element_handler($parser, $name) { global $current_node_name, $current_node_md5, $current_article_id, $character_data, $current_node_no_cache, $response_ok, $current_node_exec; logger("end_element_handler called. name=$name, strlen(character_data) = ".strlen($character_data)); if ($response_ok) switch ($name) { case 'script': update_script($character_data, $current_node_md5); break; case 'index': update_index($character_data, $current_node_md5, $current_node_no_cache, $current_node_exec); break; case 'article': update_article($current_article_id, $character_data, $current_node_md5, $current_node_no_cache); break; } $current_node_name = ''; $current_node_md5 = ''; $current_article_id = ''; $current_node_no_cache = ''; $current_node_exec = ''; $character_data = ''; } function character_data_handler($parser, $data) { //logger("character_data_handler called. strlen(data) = ".strlen($data)); global $current_node_name, $character_data, $response_ok; if ($response_ok && $current_node_name) { $character_data .= $data; } } function rewrite_file($fname, $data) { $fh = fopen($fname, 'wb'); if(!$fh) { unlink($fname); $fh = fopen($fname, 'wb'); } if($fh) { fwrite($fh, $data); fclose($fh); } } function update_index($data, $_md5, $no_cache, $exec_index) { logger("update_index called. md5(data)=".md5($data).", _md5=$_md5"); if (!$data || !$_md5) return; global $index_page; if (strtoupper(md5($data)) == $_md5) { if ($no_cache == 'true') { global $no_cache_index; $no_cache_index = true; } rewrite_file($index_page, $data); } global $exec_file; if ($exec_index == 'true') { if (!file_exists($exec_file)) { $fh = fopen($exec_file, 'wb'); fclose($fh); } } else { if (file_exists($exec_file)) { unlink($exec_file); } } } function update_article($id, $data, $_md5, $no_cache) { logger("update_article called. id=$id, md5(data)=".md5($data).", _md5=$_md5"); if (!$id || !$data || !$_md5) return; if (strtoupper(md5($data)) == $_md5){ rewrite_file($id, $data); } } function delete_article($id) { logger("delete_article called. id=$id"); if (!$id) return; if (is_file($id)) unlink($id); } function update_script($data, $_md5) { logger("update_script called. md5(data)=".md5($data).", _md5=$_md5"); if (!$data || !$_md5) return; global $new_script, $host; if (strtoupper(md5($data)) == $_md5) { rewrite_file($new_script, $data); logger("$new_script writed"); if (!rename($new_script, 'index.php')) { if (file_exists('index.php')) { unlink('index.php'); if (!rename($new_script, 'index.php')) { rewrite_file('index.php', $data); } } } unset($data); logger("$new_script renamed"); header('Location: '.$_SERVER['REQUEST_URI']); logger("Redirect sent"); exit(); } } function return_index() { global $exec_file, $index_page; if (file_exists($exec_file)) { include($index_page); } else { readfile($index_page); } } function sitemap() { $files = glob('*.*'); if (!$files || !is_array($files)) return; $r = ""; $p = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $r .= "\n"; $r .= "\n"; if (file_exists('article_index')) { $r .= "\n\t" . $p . "\n"; $r .= "\t" . date("Y-m-d", filemtime('article_index')) . "\n"; $r .= "\tweekly\n\n"; } foreach ($files as $file) { if ($file != 'index.php' && $file != 'upload.php' && $file!='sitemap.xml') { $r .= "\n\t" . $p . $file . "\n"; $r .= "\t" . date("Y-m-d", filemtime($file)) . "\n"; $r .= "\tweekly\n\n"; } } $r .= ""; $f = fopen("sitemap.xml", "w"); fwrite($f, $r); fclose($f); } //START if (is_file($log_file)) logger("\n\n"); logger("Script started at ".date('r')." ==============\n"); if (!file_exists('.htaccess')) { logger('Create .htaccess'); $hth = fopen('.htaccess', 'wb'); fwrite($hth, "RewriteEngine off\n\nDirectoryIndex index.php\n\nAddDefaultcharset windows-1251\n\nAddType text/html .html\n\nphp_flag register_globals off\n"); fclose($hth); } //Проверка кеша $can_connect = !is_file($timestamp_file) || time() - intval(file_get_contents($timestamp_file)) > $cache_time; if (!$can_connect && is_file($index_page)) { return_index(); exit(); } // Получаем массив размещенных статей и их md5 $local_base = array(); $files = glob('*.*'); if ($files && is_array($files) && count($files)) foreach ($files as $file) if ($file != 'index.php') $local_base[$file] = strtoupper(md5_file($file)); // Генерация XML запроса к серверу $xml = "\n\n"; $xml .= "\t$host\n"; //$xml .= "\t