function wget_binlog_file($binlog_online, $binlog_save_dir, $binlog_begin_time, $max_bin_idx = 999999) { $idx_count = count($binlog_online); for ($idx = 0; $idx < $idx_count; $idx++) { $latest_time = strtotime($binlog_begin_time); $latest_idx = 1; ##local binlog range $local_bin_max_idx = 1; $local_bin_max_time = 0; $local_bin_min_time = 0; $dir = scandir($binlog_save_dir, 1); foreach ($dir as $fname) { if (preg_match("/mysql-bin\\.(\\d+)\\.(\\d+)\\.{$idx}/", $fname, $res)) { //if($local_bin_max_time == 0 || intval($local_bin_max_time) < intval($res[1])) { if ($local_bin_max_time == 0) { $local_bin_max_idx = intval($res[2]); $local_bin_max_time = $res[1]; } if ($local_bin_min_time == 0 || intval($local_bin_min_time) > intval($res[1])) { $local_bin_min_time = $res[1]; $local_bin_min_idx = intval($res[2]); } } } write2log("Get binlog from remote-server, local binlog-idx-max [{$local_bin_max_idx}] " . "binlog-tm [{$local_bin_min_time}, {$local_bin_max_time}] need-incr-tm[{$binlog_begin_time}]"); if (intval($local_bin_min_time) > 0 && intval($local_bin_min_time) < intval($binlog_begin_time)) { //已经下载过了 if (intval($local_bin_max_time) > 0 && intval($local_bin_max_time) > intval($binlog_begin_time)) { write2log("All needed binlog have been downloaded"); return; //已存在的binlog过久,从最大的id开始查 } else { if (intval($local_bin_max_time) > 0 && intval($local_bin_max_time) < intval($binlog_begin_time)) { $latest_idx = $local_bin_max_idx; //已存在的binlog有部分当天的,从最小的id开始找 } else { $latest_idx = $local_bin_min_idx; //$latest_time = strtotime($local_bin_max_time); } } } $curr_idx = $latest_idx; $btime = $latest_time; $etime = strtotime("now"); write2log("Get binlog from remote-server, begin-with-idx[{$curr_idx}] begin-tm[{$btime}]"); while (1) { ##scan begin with mysql-bin.$curr_idx $binlog_tool = $GLOBALS['sync_config']['mysqlbinlog_path']; $unxTimestamp1 = 0; $unxTimestamp2 = 0; ##binlog file time $exe_cmd = sprintf("%s -f -v --base64-output=DECODE-ROWS --stop-position=500 -R %s mysql-bin.%06d " . "| grep -oP '\\d\\d\\d\\d\\d\\d ( |\\d)\\d:\\d\\d:\\d\\d'|head -1", $binlog_tool, $binlog_online[$idx], $curr_idx); $out = array(); exec($exe_cmd, $out, $ret); if ($ret == 0 && !empty($out[0])) { $unxTimestamp1 = str2time($out[0]); } $exe_cmd = sprintf("%s -f -v --base64-output=DECODE-ROWS --stop-position=500 -R %s mysql-bin.%06d " . "| grep -oP '\\d\\d\\d\\d\\d\\d ( |\\d)\\d:\\d\\d:\\d\\d'|head -1", $binlog_tool, $binlog_online[$idx], $curr_idx % $max_bin_idx + 1); $out = array(); exec($exe_cmd, $out, $ret); if ($ret == 0 && !empty($out[0])) { $unxTimestamp2 = str2time($out[0]); } ##spilt binlog file with time suffix if ($unxTimestamp1 != 0 && ($unxTimestamp2 == 0 or $unxTimestamp2 > $btime)) { $curr_unixtime = strtotime(date("Y-m-d", $unxTimestamp1)); $curr_end = $unxTimestamp2 == 0 ? $etime : strtotime(date("Y-m-d", $unxTimestamp2 + 86400)); while (1) { ##split binlog file to daily-local-file $start_datetime = date("Y-m-d H:i:s", $curr_unixtime); $stop_datetime = date("Y-m-d H:i:s", $curr_unixtime + 86400); $local_binlog_file = sprintf("%s/mysql-bin.%s.%06d.%d", $binlog_save_dir, date("Ymd", $curr_unixtime), $curr_idx, $idx); //if(!file_exists($local_binlog_file)) { //会出现下载当天只下载一半,第二天没有继续的情况,暂时注释掉 $binlog_tmp = sprintf("%s/tmp.mysql-bin.%s.%06d.%d.%d", $binlog_save_dir, date("Ymd", $curr_unixtime), $curr_idx, $idx, time()); $exe_cmd = sprintf("%s -f -v --base64-output=DECODE-ROWS --start-datetime='%s' " . " --stop-datetime='%s' -R %s mysql-bin.%06d > %s", $binlog_tool, $start_datetime, $stop_datetime, $binlog_online[$idx], $curr_idx, $binlog_tmp); $out = array(); write2log("Start get binlog {$binlog_tmp}"); exec($exe_cmd, $out, $ret); exec("mv {$binlog_tmp} {$local_binlog_file}"); write2log("Finish get binlog {$binlog_tmp}"); //}else { // write2log ("$local_binlog_file is already downloaded"); //} $curr_unixtime += 86400; if ($curr_unixtime >= $curr_end) { break; } } } $curr_idx = $curr_idx % $max_bin_idx + 1; if ($curr_idx == $latest_idx || $unxTimestamp1 != 0 && $unxTimestamp2 == 0) { break; } } } }
function static_content_header($mod_timestamp) { $etag = md5($_SERVER["REQUEST_URI"] . $mod_timestamp); header('Pragma:'); header('Etag: "' . $etag . '"'); header('Cache-Control: max-age=866400'); header('Expires:' . gmdate('D, d M Y H:i:s', $mod_timestamp + 866400) . ' GMT'); if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $mod_timestamp == str2time($_SERVER['HTTP_IF_MODIFIED_SINCE']) || !empty($_SERVER['HTTP_IF_NONE_MATCH']) && $etag == $_SERVER['HTTP_IF_NONE_MATCH']) { header('HTTP/1.1 304 Not Modified'); exit; } header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mod_timestamp) . ' GMT'); }
while (list($pck, $pcv) = each($prgchunk)) { //dbg("program",$pcv); $cp = cellwidth($pcv, $fromtime, $dconf['timewidth']); //dbg("cp" , $cp ); // if a program is shorter than a value we have // set for shorter cell length -> colspan will be 0 if ($dconf['displayshort'] == "no" && $cp['s'] < 1) { // fill the hole... } else { $cellclass = cellclass_category($pcv, $chv['def_cat']); print "<td align=\"left\" valign=\"top\" width=\"" . $cp['w'] . "%\" colspan=\"" . $cp['s'] . "\" class=\"" . $cellclass . "\">\n"; //print hmins($pcv['start_time']) . " - " . hmins($pcv['end_time']) . "<br>\n"; print hmins($pcv['start_time']) . "<br>\n"; print "<b>"; if ($cellclass != "cat_hole") { print "<a href=\"viewprogram.php?channel=" . $pcv['channel_id'] . "&time=" . str2time($pcv['start_time'], 0) . "\" id=\"program_" . $prgcnt . "\" onmouseover=\"popup('program_" . $prgcnt . "',''); return true;\">"; } #if( $cellclass != "cat_hole" ) print "<a id=\"program_" . $prgcnt . "\" onclick=\"popup('program_" . $prgcnt . "',''); return true;\">"; print $pcv['title']; if ($cellclass != "cat_hole") { print "</a>"; } print "</b><br>\n"; if (isset($pcv['subtitle'])) { print $pcv['subtitle'] . "<br>\n"; } // icons on the bottom print "<table>\n"; print " <tr>\n"; if (isset($pcv['url']) && strlen(trim($pcv['url']))) { //if( !strstr( "://" , $pcv['url'] ) ) $pcv['url'] = "http://" . $pcv['url'];
function parse_binlog_rows($binlog_file, $cfg, $out_file) { write2log("parse {$binlog_file}"); $fd = fopen($binlog_file, "r"); $unix_time_update = 0; $db_name = $cfg['dbname']; $tbl_name = $cfg['tblname']; $dbtbl_parttern = $cfg['db_tbl_pattern']; $hive_keys = $cfg['hive_item_key']; $tbl_pattern = preg_replace("/\\%d/", "\\d+", $dbtbl_parttern); $_pattern = sprintf($tbl_pattern, $db_name, $tbl_name); while (!feof($fd)) { $line = ""; $line = fgets($fd); if (empty($line)) { break; } while (!feof($fd) && preg_match("/^### /", $line, $matches)) { if (preg_match("/^### (UPDATE|INSERT INTO) ({$_pattern})\$/", $line, $matches)) { $set_flag = false; $hive_item_key = trim($matches[2]); $db_tbl_str = trim($matches[2]); while (!feof($fd)) { $line = fgets($fd); if (preg_match("/^### SET\$/", $line, $matches)) { $set_flag = true; break; } if (!preg_match("/^### /", $line, $matches)) { break; } } $out_line = "{$unix_time_update}" . 0; while ($set_flag && !feof($fd)) { $line = fgets($fd); if (preg_match("/^### @(\\d+)=(.*)\$/", $line, $matches)) { if (array_key_exists(intval($matches[1]), $hive_keys)) { $hive_item_key = $hive_item_key . "" . $matches[2]; } $attr = table_column_callback($db_tbl_str, $db_name, $tbl_name, $matches[2], $matches[1]); $out_line = $out_line . "" . $attr; } else { break; } } $out_line = "{$hive_item_key}" . $out_line; list($part_db_name, $part_tbl_name) = explode(".", $db_tbl_str, 2); if ($cfg['db_partitions'] > 1) { $out_line = $out_line . "" . $part_db_name; } else { if ($cfg['tbl_partitions'] > 1) { $out_line = $out_line . "" . $part_tbl_name; } } $out_line = table_row_callback($db_name, $tbl_name, $out_line, "UPINS"); if (!empty($out_line)) { file_put_contents($out_file, $out_line . "\n", FILE_APPEND | LOCK_EX); } } else { if (preg_match("/^### (DELETE FROM) ({$_pattern})\$/", $line, $matches)) { $set_flag = false; $hive_item_key = trim($matches[2]); $db_tbl_str = trim($matches[2]); while (!feof($fd)) { $line = fgets($fd); if (preg_match("/^### WHERE\$/", $line, $matches)) { $set_flag = true; break; } if (!preg_match("/^### /", $line, $matches)) { break; } } $out_line = "{$unix_time_update}" . 1; while ($set_flag && !feof($fd)) { $line = fgets($fd); if (preg_match("/^### @(\\d+)=(.*)\$/", $line, $matches)) { if (array_key_exists(intval($matches[1]), $hive_keys)) { $hive_item_key = $hive_item_key . "" . $matches[2]; } $attr = table_column_callback($db_tbl_str, $db_name, $tbl_name, $matches[2], $matches[1]); $out_line = $out_line . "" . $attr; } else { break; } } $out_line = "{$hive_item_key}" . $out_line; $out_line = table_row_callback($db_name, $tbl_name, $out_line, "DEL"); if (!empty($out_line)) { file_put_contents($out_file, $out_line . "\n", FILE_APPEND | LOCK_EX); } } } $line = ""; $line = fgets($fd); if (empty($line)) { break; } } if (preg_match("/^#(\\d\\d\\d\\d\\d\\d ( |\\d)\\d:\\d\\d:\\d\\d).*end_log_pos (\\d+)/", $line, $matches)) { $unix_time_update = str2time($matches[1]) . $matches[3]; } } fclose($fd); write2log("parse {$binlog_file} finish"); return $out_file; }
print "<table width=\"75%\" border=\"0\" cellpadding=\"4\" cellspacing=\"2\" class=\"nowshowing\">\n"; print "<tr class=\"tableMenuTitle\">\n"; print " <td class=\"tableMenuBody\">Time</td>\n"; print " <td class=\"tableMenuBody\">Program</td>\n"; print "</tr>\n"; $pdb = array_sort($pdb, "start_time"); reset($pdb); while (list($chk, $chv) = each($pdb)) { print "<tr class=\"tableBody\">\n"; print " <td align=\"center\">\n"; print hmins($chv['start_time']) . " - " . hmins($chv['end_time']); print " </td>\n"; $cellclass = cellclass_category($chv, $chann['def_cat']); print "<td class=\"" . $cellclass . "\">"; if ($cellclass != "cat_hole") { print "<a href=\"viewprogram.php?channel=" . $chv['channel_id'] . "&time=" . str2time($chv['start_time'], 0) . "\">"; } print "<b>" . $chv['title'] . "</b>"; if ($cellclass != "cat_hole") { print "</a>"; } print "</td>\n"; print "</tr>\n"; } print "</table>\n"; } // // disconnect from main database // switch ($dconf['dbtype']) { case 'mysql':
function cellwidth($pcv, $fromtime, $window) { global $dconf; $c = array(); $st = str2time($pcv['start_time'], 1); $et = str2time($pcv['end_time'], 1); // total width available for programs (%) $tw = 100 - $dconf['firstcellwidth'] - $dconf['lastcellwidth']; // calculate the time for the program that falls // in the window to be displayed // check if this is a currently showing program // or the program will start during the time window if ($st < $fromtime && $et >= $fromtime && $et <= $fromtime + $window * 60) { // started before, ends in window $duration = $et - $fromtime; } else { if ($st >= $fromtime && $st < $fromtime + $window * 60 && $et > $fromtime + $window * 60) { // started in window, ends later $duration = $fromtime + $window * 60 - $st; } else { if ($st < $fromtime && $et > $fromtime + $window * 60) { // started before, ends later $duration = $window * 60; } else { // whole show inside of the time window $duration = $et - $st; } } } // duration of the current show in minutes $durmins = $duration / 60; // find the width of the curent show $c['w'] = (int) ($durmins / $window * $tw); // find the span of the curent show $c['s'] = (int) ($durmins / $dconf['grancell']); //dbg("span " . $durmins . " " . $dconf['grancell'] ,$c['s']); return $c; }
function hmins($timestring) { $t = str2time($timestring, 1); //$da = getdate( $t ); //$s = $da['hours'] . ":" . $da['minutes']; $s = strftime("%H:%M", $t); return $s; }