<?php
require ('authentication_manager.php');
?>
<style type="text/css">
<!--
.column__ { margin-bottom:10px; line-height:39px; text-align:left; clear:both; background-color:#ECECEC; }
-->
</style>
<?php

/* 友情链接 */
/* 162100源码 - 162100.com */


function get_links($matches) {
  if ($matches[1] && $matches[2]) {
    return '<div class="li">名称<input type="text" name="linkname[]" value="'.htmlspecialchars($matches[2]).'" style="width:180px;" /> 网址<input type="text" name="linkhttp[]" value="'.htmlspecialchars($matches[1]).'" style="width:200px;" />
<button type="button" title="删除" onclick="removeOption(this)"><b>╳</b></button>
<button type="button" title="插入" onclick="insertLink(this)"><b>↖</b></button> 
<button type="button" title="上移" onclick="upLink(this)"><b>↑</b></button>
<button type="button" title="上移" onclick="downLink(this)"><b>↓</b></button></div>';
  }
}

$text = $err = '';
if (isset($_GET['mode']) && $_GET['mode'] && file_exists('inc/css/'.$_GET['mode'])) { 
  echo '<div class="list_title"><a href="?get=friendlink">&lt;&lt;返回</a><a class="list_title_in">友情链接编辑</a></div>
<ul class="manager_menu">所属模板：'.substr($_GET['mode'], 0, 1).''.str_pad('', strlen($_GET['mode'])-1, '*').' ['.file_get_contents('inc/css/'.$_GET['mode'].'/title.txt').'] </ul>
';

$n = 0;
if ($content_i = @file_get_contents('inc/css/'.$_GET['mode'].'/friendlink.txt')) {
  $arr_link = @preg_replace("/[\r\n]+/", "", $content_i);
  $arr_link = @explode("  ", $arr_link);
  $arr_link = @array_filter($arr_link);
  $n = count($arr_link);
  if ($n > 0) {
    foreach ($arr_link as $link) {
      $text .= preg_replace_callback('/<a[^>]+href="([^>"]+)"[^>]*>(.+)<\/a>/i', 'get_links', $link);
    }
  }
}
unset($content_i);
?>
<div class="column"><b>提示：</b><br />
  1、友情链接网址必须注意书写规范，如：http://www.162100.com<br />
  3、否则系统将自动过滤掉。</div>
<script type="text/javascript">
<!--
//添加链接
function addlink(){
  var par=$('links');
  var newDiv=document.createElement('DIV');
  newDiv.className='li';
  newDiv.innerHTML='名称<input type="text" name="linkname[]" style="width:180px;" /> 网址<input type="text" name="linkhttp[]"  style="width:200px;" />\
<button type="button" title="删除" onclick="removeOption(this)"><b>╳</b></button> \
<button type="button" title="插入" onclick="insertLink(this)"><b>↖</b></button> \
<button type="button" title="上移" onclick="upLink(this)"><b>↑</b></button> \
<button type="button" title="上移" onclick="downLink(this)"><b>↓</b></button>';
  par.insertBefore(newDiv,null);
}

//插入
function insertLink(obj){
  var tar=obj.parentNode;
  var par=$('links');
  var newDiv=document.createElement('DIV');
  newDiv.className='li';
  newDiv.innerHTML='名称<input type="text" name="linkname[]" style="width:180px;" /> 网址<input type="text" name="linkhttp[]"  style="width:200px;" />\
<button type="button" title="删除" onclick="removeOption(this)"><b>╳</b></button> \
<button type="button" title="插入" onclick="insertLink(this)"><b>↖</b></button> \
<button type="button" title="上移" onclick="upLink(this)"><b>↑</b></button> \
<button type="button" title="上移" onclick="downLink(this)"><b>↓</b></button>';
  par.insertBefore(newDiv,tar);
}

//排序：链接 向上
function upLink(obj){
  var tar=obj.parentNode;
  var par=$('links');
  var prevObj=tar.previousSibling;
  while(prevObj!=null && prevObj.nodeType!=1){
    prevObj=prevObj.previousSibling;
  }
  if(prevObj==null){
    alert('已是最上级！');
    return;
  }else{
    try{par.insertBefore(tar,prevObj);}catch(err){alert('向上移动失败！请稍候再试');return;}
    //par.removeChild(tar);
  }
}

//排序：链接 向下
function downLink(obj,class_title){
  var tar=obj.parentNode;
  var par=$('links');
  var nextObj=tar.nextSibling;
  while(nextObj!=null && nextObj.nodeType!=1){
    nextObj=nextObj.nextSibling;
  }
  if(nextObj==null){
    alert('已是最下级！');
    return;
  }
  var endObj;
  if(nextObj!=null){
    var nextnextObj=nextObj.nextSibling;
    while(nextnextObj!=null && nextnextObj.nodeType!=1){
      nextnextObj=nextnextObj.nextSibling;
    }
    var endObj=nextnextObj!=null?nextnextObj:null;
  }else{
    endObj=null;
  }
  try{par.insertBefore(tar,endObj);}catch(err){alert('向下移动失败！请稍候再试');return;}
  //par.removeChild(tar);
}
//删除
function removeOption(obj){
  var tar=obj.parentNode;
  var par=tar.parentNode;
  //if(confirm('确定删除此'+v+'吗？！')){
    try{
      tar.style.position='relative';
      tar.innerHTML+='<div style="padding:0 5px; position:absolute; top:100%; left:0; background-color:#FF6600; color:#FFFFFF;">已删除此链接</div>';
      setTimeout(function(){par.removeChild(tar);},500);
    }catch(err){
    }
  //}
}

-->
</script>
<div class="list_title"><a class="list_title_in">友情链接列表<span>（<?php echo $n; ?>）</span></a></div>
<div class="manager_menu">
  <span class="redword">请保留程序作者的几个友情链接，以示对源码作者最起码的支持。谢谢！</span>
</div>
<form action="?post=friendlink&mode=<?php echo $_GET['mode']; ?>" method="post" id="setfriendlinkform" onsubmit="addSubmitSafe()">
  <div id="links"> <?php echo $text; ?> </div>
<div class="red_err">提交前请确定inc/require目录具备写权限</div>
 <button type="button" class="large" onclick="addlink();"><b>点此添加</b></button> 
  <button class="large" name="submit" type="submit" onclick="return confirm('提交吗？请确定无误后再执行');"><b>提交</b></button>
</form>
<?php

$text = $text__ = $err = '';
} else {
  $_GET['mode'] = $web['sitemode'];
  if ($m_arr = @glob('./inc/css/*', GLOB_ONLYDIR)) {
    foreach ($m_arr as $m_f) {
      $base_index_mode = basename($m_f);
	  $title = @file_get_contents($m_f.'/title.txt');
      if (file_exists('./inc/css/'.$base_index_mode.'/index.php') && file_exists('./inc/css/'.$base_index_mode.'/list.php') && file_exists('./inc/css/'.$base_index_mode.'/article.php')) {
       $text .= '<div class="column__"><a href="?mode='.$base_index_mode.'&get=friendlink">'.substr($base_index_mode, 0, 1).'*** ['.$title.'] 的友情链接编辑</a></div>';
      } else {
        $text__ .= '<div class="column__">'.substr($base_index_mode, 0, 1).'*** ['.$title.'] 该风格尚未安装，请<a href="http://wpa.qq.com/msgrd?v=3&uin=184830962&site=qq&menu=yes" target="_blank">QQ联系程序作者</a>定制</div>';
      }
      unset($base_index_mode, $title);
    }
  } else {
    $err .= '没有任何模板，也无法进行友情链接管理！';
  }
}


echo $text, $text__, $err;


?>
