<?php require 'authentication_manager.php'; ?>
<?php
//栏目分类设置
if (POWER != 5) {
  err('<script> alert("该命令必须以基本管理员身份登陆！请重登陆"); </script>');
}

@ require 'readonly/function/img.php';
@ require 'readonly/function/pinyin.php';
  @ require 'readonly/function/write_file.php';
@ini_set('max_file_uploads', '100');


//过滤主题
function filter1($text) {
  $text = trim($text);
  $text = stripslashes($text);
  //$text = htmlspecialchars($text);
  $text = str_replace('"', '&quot;', $text);
  $text = str_replace('\'', '&#039;', $text);
  $text = str_replace('<', '&lt;', $text);
  $text = str_replace('>', '&gt;', $text);
  $text = preg_replace('/[\s\r\n]+|&nbsp;|　/i', '', $text);
  return $text;
}

$text_s = '';

if ($_POST['act'] == 'base') {
  if (!$text = @file_get_contents('writable/set/set.php')) {
    err('无法读取设置文件！稍候再试！');
  }
  $text = preg_replace('/\$web\[(\'|\")search_bar(\'|\")\][^\;]+/', '$web[\'search_bar\'] = '.(abs($_POST['search_bar']) > 3 ? 1 : abs($_POST['search_bar'])).'', $text);
  write_file('writable/set/set.php', $text);


  
  $text_s = @file_get_contents('writable/js/search.js');
  if (!empty($_POST['search_type_id'])) {
    list($search_type, $search_id) = @explode('-', $_POST['search_type_id']);
    //if (preg_match('/^(shangpin|wangye|tupian|yinyue|shipin|xinwen|youxi|ditu)$/i', $search_type)) {
    if ($search_type && $search_id) {
      $text_s = preg_replace('/\/\* search_default_bar \*\/.+\/\* \/search_default_bar \*\//isU', '/* search_default_bar */
var search_default_type = \''.$search_type.'\';
var search_default_id = \''.$search_id.'\';
/* /search_default_bar */', $text_s);
    }
  }
//  $('search_hot_word').innerHTML = getCookie('searchRemenber') == 'c' ? search_under_ad : search_under_ad + val;
  if ($_POST['zmn'] == 1) {
    $text_s = preg_replace('/\$\(\'search_hot_word\'\)\.innerHTML\s*\=\s*.+/i', '$(\'search_hot_word\').innerHTML = getCookie(\'searchRemenber\') == \'c\' ? search_under_ad : search_under_ad + val;', $text_s);
  } else {
    $text_s = preg_replace('/\$\(\'search_hot_word\'\)\.innerHTML\s*\=\s*.+/i', '$(\'search_hot_word\').innerHTML = getCookie(\'searchRemenber\') == \'o\' ? search_under_ad + val : search_under_ad;', $text_s);
  }
  
  $_POST['zn2'] = is_numeric($_POST['zn2']) && $_POST['zn2'] == 1 ? 'y' : 'x';
  $text_s = preg_replace('/var\s+search\_lenovo\s*\=\s*.*\;/iU', '', $text_s);
  $text_s = trim($text_s);
  $text_s = $text_s.'

var search_lenovo = \''.(abs($_POST['zn']) == 1 ? 'y'.$_POST['zn2'] : 'x').'\';

';
  
  if ($text_s) {
    write_file('writable/js/search.js', $text_s);
    unset($text_s);
  }

  //$v = gmdate('YmdHis', time() + (floatval($web['time_pos']) * 3600));
  //write_file('writable/require/browse_reload.txt', $v);
  if ($web['html'] == true) {
    $SET_RELOAD = 1;
    @ require 'readonly/run/post_manager_html.php';
  }

  alert('集成搜索引擎基本设置 设置完毕！', 'webmaster_central.php?get=search#po1');

} elseif ($_POST['act'] == 'up') {
  if (empty($_POST['up'])) {
    err('<script> alert("参数缺失！[01]"); </script>');
  }
  list($search_bar, $type, $n) = @explode('_', $_POST['up']);

  //print_r($_FILES['uploadfile_'.$_POST['up']]);
  if (is_array($_FILES['uploadfile_'.$_POST['up']]) && $_FILES['uploadfile_'.$_POST['up']]['size']) {
    if ('im'.$search_bar == 'im1') {
      $w = 68;
      $h = 28;
    } elseif ('im'.$search_bar == 'im2') {
      $w = 16;
      $h = 16;
    } else {
      err('<script> alert("参数错误！[02]"); </script>');
    }
    $img_info = getimagesize($_FILES['uploadfile_'.$_POST['up']]['tmp_name']);
    if ($img_info[0] != $w || $img_info[1] != $h) {
      $bian = 1;
      if (extension_loaded('gd')) {
        if (!function_exists('gd_info')) {
          err('<script> alert("重要提示：你的gd版本很低，图片处理功能可能受到约束"); </script>');
        }
      } else {
        err('<script> alert("重要提示：你尚未加载gd库，图片处理功能可能受到约束"); </script>');
      }
    }

    if (!file_exists('writable/images/searchmark') && !@mkdir('writable/images/searchmark')) {
      err('<script> alert("图片无法上传，上传目录writable/images/searchmark不存在"); </script>');
    }
    //@chmod('writable/images/searchmark', 0777);
    eval('@chmod(\'writable/images/searchmark\', 0'.$web['chmod'].');');
    $inis = ini_get_all();
    $uploadmax = $inis['upload_max_filesize'];
    if ($_FILES['uploadfile_'.$_POST['up']]['size'] > 30 * 1024) {
      err('<script> alert("图片上传不成功！上传的文件请小于30KB"); </script>');
    }
    if (!preg_match('/\.(jpg|gif|png)$/i', strtolower($_FILES['uploadfile_'.$_POST['up']]['name']), $matches)) {
      err('<script> alert("图片上传不成功！请选择一个有效的文件：允许的格式有（jpg|gif|png）"); </script>');
    }
    if ($fp = @fopen($_FILES['uploadfile_'.$_POST['up']]['tmp_name'], 'rb')) {
      $img_contents = @fread($fp, $_FILES['uploadfile_'.$_POST['up']]['size']);
      @fclose($fp);
    } else {
      $img_contents = @file_get_contents($_FILES['uploadfile_'.$_POST['up']]['tmp_name']);
    }
    if (preg_match('/<\?php|eval|POST|base64_decode|base64_encode/i', $img_contents, $m_err)) {
      err('<script> alert("提示！禁止提交。该文件含有禁止的代码'.str_replace('?', '\?', $m_err[0]).'"); </script>');
    }

    $tpy = pinyin($_POST['type_name'][$type]);
    $npy = pinyin($_POST['bar_name'][$type][$n]);

    $im_base = $_POST['up'];
    if ($tpy != $type || $npy != $n) {
      $im_base = $search_bar.'_'.$tpy.'_'.$npy;
    }
    $im = 'writable/images/searchmark/'.$im_base.'.'.$matches[1];

    if (@move_uploaded_file($_FILES['uploadfile_'.$_POST['up']]['tmp_name'], $im)) {

      if (isset($bian)) {
        if (!run_img_resize($im, $im, 0, 0, $w, $h, false, false, $web['pic_quality'])) {
          err('<script> alert("截图失败！"); </script>');
        }
      }
      $im = typeto($im, 'gif');

      err('<script>
try{
  parent.$("bar_img_'.$_POST['up'].'").value="'.$im.'";
  parent.$("img_'.$_POST['up'].'").src="'.$im.'?'.time().'";
}catch(e){}

alert("图片上传成功！");

</script>');


    } else {
      err('<script> alert("图片上传不成功！[03]"); </script>');
    }
  } else {
    err('<script> alert("图片不存在！路径不正确；或系统出错，请稍候再试"); </script>');
  }

}





$_POST['type_name'] = (array)$_POST['type_name'];

//print_r($_POST);die;


$text = '';
$arr_py_type = $arr_py_n = $s_img = array();

$cnum1 = count($_POST['type_name']);

if ($cnum1 == 0) {
  err('搜索类别数目不能空！');
}
if ($cnum1 > count(array_unique($_POST['type_name']))) {
  err('搜索类别发现有重名！');
}
if ($cnum1 > count(array_filter(preg_replace('/^\s+|\s+$/', '', $_POST['type_name'])))) {
  err('搜索类别发现有空值！');
}

$text .= '
searchSelect = {';

$step1 = $step2 = 0;
foreach ($_POST['type_name'] as $type => $tname) {

  $step1++;

  $tname = filter1($tname);
  $type_py = pinyin($tname);

  $text .= '
  \''.$type_py.'\': {
    0 : \'搜'.$tname.'\',';

  if (isset($arr_py_type[$type_py])) {
    err('搜索类别不能有同音：“'.$arr_py_type[$type_py].'”“'.$tname.'”，请修改');
  } else {
    $arr_py_type[$type_py] = $tname;
  }

  $cnum2 = count($_POST['bar_name'][$type]);
  if ($cnum2 == 0) {
    err('搜索类别“'.$tname.'”中：引擎名数目不能空！');
  }

  foreach ($_POST['bar_name'][$type] as $n => $sname) {

    $step2++;

    $sname = filter1($sname);
    $s_py = pinyin($sname);
    $surl = filter1($_POST['bar_url'][$type][$n]);
    $surl_ = filter1($_POST['bar_url_'][$type][$n]);
    if (!empty($surl_)) {
      $surl .= ' '.$surl_;
    }
    if (isset($arr_py_n[$type_py][$s_py])) {
      err('搜索类别“'.$tname.'”中：引擎名不能有同音：“'.$arr_py_n[$type_py][$s_py].'”“'.$sname.'”，请修改');
    } else {
      $arr_py_n[$type_py][$s_py] = $sname;
    }

    if ($sname == '') {
      err('搜索类别“'.$tname.'”中：发现引擎名有空值！');
    }
    if ($surl == '') {
      err('搜索类别“'.$tname.'”、引擎名为“'.$sname.'”中：发现引擎URL有空值！');
    }

    if ($type != $type_py || $n != $s_py) {
      if (!file_exists('writable/images/searchmark/1_'.$type.'_'.$n.'.gif') && !file_exists('writable/images/searchmark/1_'.$type_py.'_'.$s_py.'.gif')) {
        err('搜索类别“'.$tname.'”、引擎名为“'.$sname.'”中：发现引擎样式1图标未上传！');
      } else {
        if (file_exists('writable/images/searchmark/1_'.$type.'_'.$n.'.gif')) {
          @rename('writable/images/searchmark/1_'.$type.'_'.$n.'.gif', 'writable/images/searchmark/1_'.$type_py.'_'.$s_py.'.gif');
        }
      }
      if (!file_exists('writable/images/searchmark/2_'.$type.'_'.$n.'.gif') && !file_exists('writable/images/searchmark/2_'.$type_py.'_'.$s_py.'.gif')) {
        err('搜索类别“'.$tname.'”、引擎名为“'.$sname.'”中：发现引擎样式2图标未上传！');
      } else {
        if (file_exists('writable/images/searchmark/2_'.$type.'_'.$n.'.gif')) {
          @rename('writable/images/searchmark/2_'.$type.'_'.$n.'.gif', 'writable/images/searchmark/2_'.$type_py.'_'.$s_py.'.gif');
        }
      }
    }
    $s_img['1_'.$type_py.'_'.$s_py.'.gif'] = 1;
    $s_img['2_'.$type_py.'_'.$s_py.'.gif'] = 1;

    $text .= '
    \''.$s_py.'\': [\''.$surl.'\', \''.$sname.'\']'.($step2 < $cnum2 ? ',' : '').'';

  }
  $step2 = 0;
  unset($cnum2);

  $text .= '
  }'.($step1 < $cnum1 ? ',' : '').'';
}

$text .= '
};
';


if ($imgs = @glob('writable/images/searchmark/*')) {
  foreach ($imgs as $im) {
    //if (!array_key_exists(basename($im), $s_img)) {
    if (!isset($s_img[basename($im)])) {
      @unlink($im);
    }
  }
}


if ($text_s = @file_get_contents('writable/js/search.js')) {
  $text_s = preg_replace('/\/\* search_bar \*\/.+\/\* \/search_bar \*\//isU', '/* search_bar */'.$text.'/* /search_bar */', $text_s);
  write_file('writable/js/search.js', $text_s);
} else {
  err('读取模板文件writable/js/search.js失败！');
}
unset($text, $text_s);

//$v = gmdate('YmdHis', time() + (floatval($web['time_pos']) * 3600));
//write_file('writable/require/browse_reload.txt', $v);
if ($web['html'] == true) {
  $SET_RELOAD = 1;
  @ require 'readonly/run/post_manager_html.php';
}
alert('编辑搜索引擎成功！', 'webmaster_central.php?get=search#po2');












?>