<?PHP
/*
=====================================================
 DataLife Engine v9.3 - by DLE Farsi Group 
-----------------------------------------------------
 http://datalifeengine.ir/
-----------------------------------------------------
 Copyright (c) 2006,2011 DLE Farsi Group
=====================================================
*/

if(!defined('DATALIFEENGINE')) {
	die("Hacking attempt!");
}

if ($action == "sort" || $action == "delete" || $_POST['obmen-submit']) {
	@unlink( ENGINE_DIR . '/cache/system/obmen.php' );
}

$js_array[] = "engine/skins/obmen/colorpicker.js";
$colorPicker = <<<HTML
<link rel="stylesheet" media="screen" type="text/css" href="engine/skins/obmen/colorpicker.css" />
<script>
\$('#colorSelector').ColorPicker({
	color: \$('#colorSelector').val(),
	onShow: function (colpkr) {
		\$(colpkr).fadeIn(500);
		return false;
	},
	onHide: function (colpkr) {
		\$(colpkr).fadeOut(500);
		return false;
	},
	onChange: function (hsb, hex, rgb) {
		\$('#colorSelector').val(hex);
	}
});
</script>
HTML;

if ($action == "edit") {

	if( isset($_POST['obmen-submit']) AND ($_POST['obmen-submit'] == "true") ){
		$id = stripslashes($_POST['id']); 
		$title = stripslashes($_POST['title']);
		$color = stripslashes($_POST['color']);
		$bold = stripslashes($_POST['bold']);
		$link = stripslashes($_POST['link']);
		$mail = stripslashes($_POST['mail']);
		$icq = stripslashes($_POST['icq']);
		$posit = stripslashes($_POST['posit']);
		$description = $db->safesql(substr(strip_tags(stripslashes($_REQUEST['description'])), 0, 350));
		$db->query("UPDATE " . PREFIX . "_obmen set title='$title', color='$color', bold='$bold', link='$link', icq='$icq', mail='$mail', description='$description', posit='$posit' where id='$id'");
		msg("info", "اطلاعات", "لینک {$title} با موفقیت بروز شد.", "$PHP_SELF?mod=obmen");
	} else {
		echoheader("obmen", 'Friends');

		$db->query("SELECT * FROM " . PREFIX . "_obmen WHERE id=" . (int)$_REQUEST['id'] . " ORDER BY posit ASC");
		$row = $db->get_row();

		if ($row["bold"]) $boldchecked = " checked";
		$id = $row['id'];
		$title = $row['title'];
		$link = $row['link'];
		$color = $row['color'];
		$mail = $row['mail'];
		$posit = $row['posit'];
		$icq = $row['icq'];
		$description = $row['description'];
		echo <<<HTML
<form method="post" action="">
<div style="padding-top:5px;padding-bottom:2px;">
<table width="100%">
	<tr>
		<td width="4"><img src="engine/skins/images/tl_lo.gif" width="4" height="4" border="0"></td>
		<td background="engine/skins/images/tl_oo.gif"><img src="engine/skins/images/tl_oo.gif" width="1" height="4" border="0"></td>
		<td width="6"><img src="engine/skins/images/tl_ro.gif" width="6" height="4" border="0"></td>
	</tr>
	<tr>
		<td background="engine/skins/images/tl_lb.gif"><img src="engine/skins/images/tl_lb.gif" width="4" height="1" border="0"></td>
		<td style="padding:5px;" bgcolor="#FFFFFF">
<table width="100%">
	<tr>
		<td bgcolor="#EFEFEF" height="29" style="padding-right:10px;"><div class="navigation">ویرایش سایت شماره $id - $title</div></td>
	</tr>
</table>
<div class="unterline"></div>
<table width="100%">
	<tr>
		<td width="260" style="padding:4px;">نام لینک:</td>
		<td><input class="edit" value="$title" type="text" size="45" name="title"><input type="hidden" name="id" value="$id"></td>
	</tr>
	<tr>
		<td style="padding:4px;">رنگ لینک:</td>
		<td>
			<input name="color" id="colorSelector" maxlength="6" size="6" value="$color" /> #
		</td>
	</tr>
			
	<tr>
		<td style="padding:4px;">پر رنگ بودن لینک:</td>
		<td><input type="checkbox" name="bold" value="1" id="bold"{$boldchecked}><label for="bold"> <b>Bold</b></label></td>
	</tr>
	<tr>
		<td style="padding:4px;">آدرس سایت:</td>
		<td><input class="edit" value="$link" style="direction: ltr" type="text" size="45" name="link"></td>
	</tr>
	<tr>
		<td style="padding:4px;">ایمیل:</td>
		<td><input class="edit" value="$mail" style="direction: ltr" type="text "size="45" name="mail"></td>
	</tr>
 <tr>
			<td style="padding:4px;">محل قرارگیری:</td>
			<td><input type="text" name="posit" size="2" value="$posit" class="edit"></td>
		</tr>
		<tr>
			<td style="padding:4px;">توضیح لینک:</td>
			<td><textarea name="description" style="width:245px;height:50px;">$description</textarea></td>
		</tr>
		<tr>
		<td colspan="2"><div class="hr_line"></div></td>
	</tr>
	<tr>
		<td style="padding:4px;">&nbsp;</td>
		<td><input type="submit" class="buttons" value=" ذخیره تغییرات  "> <a href="$PHP_SELF?mod=obmen">بازگشت به لینکستان</a>
	</tr>
</table>
</td>
		<td background="engine/skins/images/tl_rb.gif"><img src="engine/skins/images/tl_rb.gif" width="6" height="1" border="0"></td>
	</tr>
	<tr>
		<td><img src="engine/skins/images/tl_lu.gif" width="4" height="6" border="0"></td>
		<td background="engine/skins/images/tl_ub.gif"><img src="engine/skins/images/tl_ub.gif" width="1" height="6" border="0"></td>
		<td><img src="engine/skins/images/tl_ru.gif" width="6" height="6" border="0"></td>
	</tr>
</table>
</div>
<input name="obmen-submit" value="true" type="hidden">
</form>
HTML;

		echo $colorPicker;
		echofooter();
	}
} elseif ($action == "sort") {
	foreach ($_POST["posit"] as $id => $posit) {
		if($posit != "") {
			$posi = intval($posit);
			$id = intval($id);
			$query = $db->query("UPDATE " . PREFIX . "_obmen SET posit=$posi WHERE id = $id");
		}
	}
msg("info", "اطلاعات", "لینک ها با موفقیت چیده شدند.", "$PHP_SELF?mod=obmen");

} elseif ($action == "add") {

	if( isset($_POST['obmen-submit']) AND ($_POST['obmen-submit'] == "true") ){
		$title = stripslashes($_POST['title']);
		$color = stripslashes($_POST['color']);
		$bold = stripslashes($_POST['bold']);
		$link = stripslashes($_POST['link']);
		$mail = stripslashes($_POST['mail']);
		$icq = stripslashes($_POST['icq']);
		$description = mysql_escape_string(substr(strip_tags(stripslashes($_REQUEST['description'])), 0, 350));
		$sql_insert = $db->query("INSERT INTO " . PREFIX . "_obmen (title, color, bold, link, icq, mail, description) values ('$title', '$color', '$bold', '$link', '$icq', '$mail', '$description')");
		msg("info", "اطلاعات", "لینک {$title} با موفقیت اضافه شد.", "$PHP_SELF?mod=obmen");
	} else {
		echoheader("obmen", 'Friends');
		echo <<<HTML
<form method="post" action="">
<div style="padding-top:5px;padding-bottom:2px;">
<table width="100%">
	<tr>
		<td width="4"><img src="engine/skins/images/tl_lo.gif" width="4" height="4" border="0"></td>
		<td background="engine/skins/images/tl_oo.gif"><img src="engine/skins/images/tl_oo.gif" width="1" height="4" border="0"></td>
		<td width="6"><img src="engine/skins/images/tl_ro.gif" width="6" height="4" border="0"></td>
	</tr>
	<tr>
		<td background="engine/skins/images/tl_lb.gif"><img src="engine/skins/images/tl_lb.gif" width="4" height="1" border="0"></td>
		<td style="padding:5px;" bgcolor="#FFFFFF">
<table width="100%">
	<tr>
		<td bgcolor="#EFEFEF" height="29" style="padding-right:10px;"><div class="navigation">اضافه کردن لینک جدید</div></td>
	</tr>
</table>
<div class="unterline"></div>
<table width="100%">
	<tr>
		<td width="260" style="padding:4px;">نام لینک:</td>
		<td><input class="edit" value="" type="text" size="45" name="title"></td>
	</tr>
	<tr>
		<td style="padding:4px;">رنگ لینک:</td>
		<td>
			<input name="color" id="colorSelector" maxlength="6" size="6" /> #
		</td>
	</tr>
			
	<tr>
		<td style="padding:4px;">پر رنگ بودن لینک:</td>
		<td><input type="checkbox" name="bold" value="1" id="bold"><label for="bold"> <b>Bold</b></label></td>
	</tr>
	<tr>
		<td style="padding:4px;">آدرس:</td>
		<td><input class="edit" style="text-align: left; direction: ltr;" value="http://" type="text" size="45" name="link"></td>
	</tr>
	<tr>
		<td style="padding:4px;">ایمیل:</td>
		<td><input class="edit" style="text-align: left; direction: ltr;" value="" type="text "size="45" name="mail"></td>
	</tr>
		<tr>
			<td style="padding:4px;">توضیح:</td>
			<td><textarea name="description" style="width:245px;height:70px;"></textarea></td>
		</tr>
		<tr>
		<td colspan="2"><div class="hr_line"></div></td>
	</tr>
	<tr>
		<td style="padding:4px;">&nbsp;</td>
		<td><input type="submit" class="buttons" value="اضافه کردن سایت">
	</tr>
</table>
</td>
		<td background="engine/skins/images/tl_rb.gif"><img src="engine/skins/images/tl_rb.gif" width="6" height="1" border="0"></td>
	</tr>
	<tr>
		<td><img src="engine/skins/images/tl_lu.gif" width="4" height="6" border="0"></td>
		<td background="engine/skins/images/tl_ub.gif"><img src="engine/skins/images/tl_ub.gif" width="1" height="6" border="0"></td>
		<td><img src="engine/skins/images/tl_ru.gif" width="6" height="6" border="0"></td>
	</tr>
</table>
</div>
<input name="obmen-submit" value="true" type="hidden">
</form>
HTML;

		echo $colorPicker;
		echofooter();
	}
} elseif ($action == "delete") {
	$db->query ("DELETE FROM " . PREFIX . "_obmen WHERE id = '{$_REQUEST['id']}'");
	msg("info", "اطلاعات", "این لینک با موفقیت حذف شد.", "$PHP_SELF?mod=obmen");

} else {

	echoheader("obmen", "Friend");
	echo <<<HTML
<script language="javascript" type="text/javascript">
<!--
	var ajax = new dle_ajax();

function check_rang( host, id ){
	ShowLoading("");

	\$.post("{$config['http_home_url']}engine/ajax/rangsite.php",{idsea:host},function(a){\$('#rang-'+id).fadeOut('normal',function(){\$(this).html(a).fadeIn();HideLoading("");});});

	return false;
}
//-->
</script>
<script type="text/javascript" src="engine/classes/js/menu.js"></script>
<script language="javascript" type="text/javascript">
<!--
function MenuBuild( m_id , led_action ){

var menu=new Array()
var lang_action = "";

menu[0]='<a onClick="document.location=\'$PHP_SELF?mod=obmen&action=edit&id=' + m_id + '\'; return(false)" href="#">ویرایش</a>';
menu[1]='<a onClick="javascript:confirmdelete(' + m_id + '); return(false)" href="#">حذف</a>';

return menu;
}
function confirmdelete(id){
	var agree=confirm("آیا واقعا میخواهید این لینک را حذف کنید؟");
	if (agree)
	 document.location="$PHP_SELF?mod=obmen&action=delete&id="+id;
}
//-->
</script>
HTML;
$result = $db->query("SELECT * FROM " . PREFIX . "_obmen ORDER BY posit ASC");
while($row = $db->get_row($result)) {
	$link = $row['link'];
	$id = $row['id'];
	$title = $row['title'];
	$color = $row['color'];
	$bold = $row['bold'];
	$mail = $row['mail'];
	$posit = $row['posit'];
	$icq = $row['icq'];
	$description = $row['description'];

	$che = parse_url("$link");
	$host = $che['host'];

	$entry .= <<<HTML
<tr>
<td style="padding:2px"><a href="{$link}" target="_blank">{$title}</a>
<td align=left width=250><div id="rang-{$id}"><input onclick="check_rang( '$host', '$id' ); return false;" class="edit" style="width:150px;" type="button" value="دریافت رتبه ای از سایت"></div></td>
<td align=left><input name='posit[$id]' value=$posit  class=edit size=2>&nbsp;</td>
<td width=60 align=left><a onClick="return dropdownmenu(this, event, MenuBuild('{$row['id']}', 'off'), '150px')" href="#"><img src="engine/skins/images/browser_action.gif" border="0"></a></td>
  </tr>
<tr><td background=engine/skins/images/mline.gif height=1 colspan=4></td></tr>
HTML;
}


if ($_POST['action'] == "send_notice") {
	
	$obmen_config = array();
	
	foreach($_POST as $name => $value){
		if($name == "action") continue;
		$obmen_config[$name] = $_POST[$name];
	}
	
	$f = fopen( ENGINE_DIR . "/data/obmen.php", 'w' );
	fwrite( $f, serialize($obmen_config) );
	fclose( $f );
	
}
$obmen_config = unserialize( @file_get_contents( ENGINE_DIR . "/data/obmen.php" ));

$text1 = $obmen_config['text']['1'];
$text2 = $obmen_config['text']['2'];
$showchecked1 = $obmen_config['showchecked']['1'];
$showchecked2 = $obmen_config['showchecked']['2'];
$showchecked3 = $obmen_config['showchecked']['3'];

if ( $showchecked1 == '1' ) $ifchecked1 = "checked";
if ( $showchecked2 == '1' ) $ifchecked2 = "checked";

	if ($showchecked3 == '1' ) $ifchecked3 = "checked";
	
	echo <<<HTML
<table width="100%">
	<tr>
		<td width="4"><img src="engine/skins/images/tl_lo.gif" width="4" height="4" border="0"></td>
		<td background="engine/skins/images/tl_oo.gif"><img src="engine/skins/images/tl_oo.gif" width="1" height="4" border="0"></td>
		<td width="6"><img src="engine/skins/images/tl_ro.gif" width="6" height="4" border="0"></td>
	</tr>
	<tr>
		<td background="engine/skins/images/tl_lb.gif"><img src="engine/skins/images/tl_lb.gif" width="4" height="1" border="0"></td>
		<td style="padding:5px;" bgcolor="#FFFFFF"><table width="100%">
	<tr>
		<td bgcolor="#EFEFEF" height="29" style="padding-right:10px;"><div class="navigation"><table width=100% class=navigation>
  <tr><td>آدرس سایت ها</td><td align=right style='font-size: 9px'></td></tr></table></div></td>
	</tr>
</table>
<div class="unterline"></div><form method="post" action="$PHP_SELF?mod=obmen&action=sort">
<table width=100% border=0 cellspacing=0 cellpadding=0>
HTML;
	
	if (! $entry) $entry = "<tr><td align=center height=30 colspan=5 align=center class=navigation>- هیچ سایتی در لینکستان وجود ندارد -</td></tr>";
	$row = $db->super_query("SELECT COUNT(*) as count FROM " . PREFIX . "_obmen");
	$sites = $row['count'];
	echo <<<HTML
$entry
</table>
 <div class="hr_line"></div><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
	<td class="navigation">تعداد سایت ها: <b>{$sites}</b></td>
	<td><div align="left">
	<input type='button' value=' اضافه کردن لینک جدید ' class='bbcodes' onclick="window.location='$PHP_SELF?mod=obmen&action=add'">
	<input type='button' value=' تنظیمات ' class='bbcodes' onclick="javascript:ShowOrHide('options')">
	<input type="submit" value='چیدن لینک ها' class='bbcodes'></div></td>
  </tr>
</table></form></td>
		<td background="engine/skins/images/tl_rb.gif"><img src="engine/skins/images/tl_rb.gif" width="6" height="1" border="0"></td>
	</tr>
	<tr>
		<td><img src="engine/skins/images/tl_lu.gif" width="4" height="6" border="0"></td>
		<td background="engine/skins/images/tl_ub.gif"><img src="engine/skins/images/tl_ub.gif" width="1" height="6" border="0"></td>
		<td><img src="engine/skins/images/tl_ru.gif" width="6" height="6" border="0"></td>
	</tr>
</table>


<div id="options" style="display:none;">
<form method="post" action="">
<input type="hidden" name="action" value="send_notice">
<div style="padding-top:5px;padding-bottom:2px;">
<table width="100%">
	<tr>
		<td width="4"><img src="engine/skins/images/tl_lo.gif" width="4" height="4" border="0"></td>
		<td background="engine/skins/images/tl_oo.gif"><img src="engine/skins/images/tl_oo.gif" width="1" height="4" border="0"></td>
		<td width="6"><img src="engine/skins/images/tl_ro.gif" width="6" height="4" border="0"></td>
	</tr>
	<tr>
		<td background="engine/skins/images/tl_lb.gif"><img src="engine/skins/images/tl_lb.gif" width="4" height="1" border="0"></td>
		<td style="padding:5px;" bgcolor="#FFFFFF">
<table width="100%">
	<tr>
		<td bgcolor="#EFEFEF" height="29" style="padding-right:10px;"><div class="navigation">تنظیمات</div></td>
	</tr>
</table>
<div class="unterline"></div>
<table width="100%">
		<tr><td width=200>متن در بالای لینکستان:<BR><input type="checkbox" {$ifchecked1} name="showchecked[1]" value="1" id="check1"><label for="check1">نمایش</label></td>
		<td><textarea name="text[1]" style="width:350px;height:70px;background-color:lightyellow;">{$text1}</textarea></td></tr>
		<tr><td width=200>متن در پایین لینکستان:<BR><input type="checkbox" {$ifchecked2} name="showchecked[2]" value="1" id="check2"><label for="check2">نمایش</label></td>
		<td><textarea name="text[2]" style="width:350px;height:70px;background-color:lightyellow;">{$text2}</textarea></td></tr>
		<tr><td width=200>متحرک:</td><td><input type="checkbox" {$ifchecked3} name="showchecked[3]" value="1" id="check3"><label for="check3">حرکت به سمت بالا</label></td></tr>
		<tr><td width=200><BR></td><td><BR><input type="submit" class="buttons" value="ذخیره تنظیمات" style="width:100px;"></td></tr>
	
</table>
</td>
		<td background="engine/skins/images/tl_rb.gif"><img src="engine/skins/images/tl_rb.gif" width="6" height="1" border="0"></td>
	</tr>
	<tr>
		<td><img src="engine/skins/images/tl_lu.gif" width="4" height="6" border="0"></td>
		<td background="engine/skins/images/tl_ub.gif"><img src="engine/skins/images/tl_ub.gif" width="1" height="6" border="0"></td>
		<td><img src="engine/skins/images/tl_ru.gif" width="6" height="6" border="0"></td>
	</tr>
</table>
</div>
</form></div>

HTML;

	echofooter();
}

?>