<?php


if (empty($_GET['url'])) {
  die();
}

if (!isset($_GET['o'])) {
  die();
}

function filter($text){
  $text = trim($text);
  $text = stripslashes($text);
  $text = htmlspecialchars($text);
  $text = preg_replace('/[\r\n]+/', '', $text);
  $text = preg_replace('/\s+/', ' ', $text);
  $text = str_replace('\'', '&#039;', $text);
  return $text;
}

$_GET['url'] = filter($_GET['url']);
$GLOBALS['num'] = (isset($_GET['num']) && is_numeric($_GET['num']) && $_GET['num'] > 0) ? $_GET['num'] : 50;

function read_reg($url,$reg){
  if(preg_match($reg,read_file($url),$m)){
	return filter($m[1]);
  }
  return false;
}


function run_url($text, $v = false) {

  $text = stripslashes($text);
  //die($text);

  $text = preg_replace('/\'[\s\r\n]*\:[\s\r\n]*\'/', '\' => \'', $text);
  $uarr = eval('return array('.$text.');');

  $urlv[$_GET['url']] = $uarr[$_GET['url']];
  unset($uarr[$_GET['url']]);

  $uarr = array($_GET['url'] => $urlv[$_GET['url']]) + $uarr;
  if (count($uarr) > $GLOBALS['num']) {
    $uarr = array_slice($uarr, 0, $GLOBALS['num']);
  }

  $text_new = var_export($uarr, TRUE);
  $text_new = str_replace('=>', ':', $text_new);
  $text_new = preg_replace('/array[\s\r\n]*\(/i', '', $text_new);
  $text_new = preg_replace('/\)[\s\r\n]*;?/', '', $text_new);
  $text_new = '
'.trim($text_new).'
';

  return $text_new;
}

@ require ('readonly/function/read_file.php');
@ require ('readonly/function/write_file.php');

$text = file_get_contents('writable/js/referrer_top.js');
$t = '';

if ($_GET['o'] == 0) {

} else {
  if (!$sitetitle = read_reg($_GET['url'], '/<title>(.+)<\/title>/isU')){
    err('系统尝试连接网址：'.$_GET['url'].'失败！');
  }
  if ($cha = read_reg($_GET['url'], '/charset[\s\r\n]*=[\s\r\n]*([\w\-]+)[^\>]*>/i')) {
    if (strtolower($cha) != 'utf-8') {
      $sitetitle = iconv($cha, 'utf-8', $sitetitle);
    }
  }
  $t = '\''.$_GET['url'].'\' : \''.$sitetitle.'\',';

}

$text = preg_replace('/(var[\s\r\n]+referrerTop[\s\r\n]*=[\s\r\n]*\{)(.*)(\})/iseU', '"$1".run_url("'.$t.'$2")."$3"', $text);

write_file('writable/js/referrer_top.js', $text);



?>