<?php
defined('IN_HS') or exit('Access Denied');
require HS_ROOT.'/module/'.$mod.'/include/common.inc.php';

sajax_init();
sajax_export("codedocheck");
sajax_handle_client_request();
//$GLOBALS['sajax_debug_mode'] = 1;

if(!$_userid) header('location:/member/login.php?forward='.urlencode($PHP_URL));
if($CHA['channeldomain'] && strpos($PHP_URL, $CHA['channeldomain'])!==false)
{
   header('Location:'.$HS['siteurl'].$CHA['channeldir'].'/myitem.php?'.$PHP_QUERYSTRING);
   exit;
}
$CHA['enablecontribute'] or showmessage($LANG['current_channel_not_allow_submit_picture'], 'goback');
//if(!$_userid) showmessage('请登陆后再发布', '/member/login.php?forward='.urlencode($PHP_URL));



require_once HS_ROOT.'/include/formselect.func.php';
require HS_ROOT.'/include/tree.class.php';
$tree = new tree();

$enableaddalways = false;
$GROUP = cache_read('member_group.php');
$gid[] = $_groupid;
$gids = array_merge($_arrgroupid, $gid);
foreach($gids as $gid)
{
	if($GROUP[$gid]['enableaddalways'])
	{
		$enableaddalways = true;
		break;
	}
}

$pictureid = isset($pictureid) ? intval($pictureid) : 0;
$catid = isset($catid) ? intval($catid) : 0;
if($catid) $CAT = cache_read('category_'.$catid.'.php');

$category_select = category_select('catid', $LANG['select_category'], $catid, 'id="catid"');
$pagesize = $HS['pagesize'] ? $HS['pagesize'] : 20;
$referer = isset($referer) ? $referer : $PHP_REFERER;
$action = isset($action) ? $action : 'manage';
if(!isset($checkcodestr)) $checkcodestr = '';
if ($_groupid ==1)
{
$status =3;
}else{
$status =1;
}
switch($action){

case 'add':

	if($dosubmit)
	{
		checkcode($checkcodestr, $MOD['check_code'], $PHP_REFERER);
		if(!$catid)	showmessage($LANG['sorry_select_parent_category'], 'goback');
		if($catid < 3 || $catid > 4)	showmessage("你没有权限在此栏目发布", 'goback');
		if(empty($title)) showmessage($LANG['sorry_title_not_null'], 'goback');
		if(empty($author)) showmessage("请填写游戏中昵称", 'goback');
		if(empty($copyfrom)) showmessage("请填写所在服务器", 'goback');
		if($CAT['child'] && !$CAT['enableadd']) showmessage($LANG['appoint_category_not_allowed_to_add_picture'], 'goback');
		if(empty($pictureurls)) showmessage($LANG['picture_url_not_null'], 'goback');
		if(empty($content))	showmessage("图片说明不能为空", 'goback');
		if($CAT['arrgroupid_add'] && strpos($CAT['arrgroupid_add'], "$_groupid") === false) showmessage($LANG['not_allowed_to_add_by_your_group'], 'goback');

		$picurls = explode("\n", $pictureurls);
		$tmp_picurls = array();
		foreach($picurls as $picurl)
		{
			$t = '';
			if(strpos($picurl, "|") === false)
			{
				$t = $picurl;
			}
			else
			{
				$s = explode("|", $picurl);
				$t = $s[1];
			}
			$tmp_picurls[] = $t;
		}
		$tmp_allpicurls = explode("\n", $allpictureurls);
		$tmp_array = array_diff($tmp_allpicurls, $tmp_picurls);
		if(!empty($tmp_array))
		{
			foreach($tmp_array as $v)
			{
				if(strpos($v, "://")) continue;
				$fileurl = trim(HS_ROOT.'/'.$CHA['channeldir'].'/'.$MOD['upload_dir'].'/'.$v);
				@unlink($fileurl);
				@unlink(dirname($fileurl).'/thumb_'.basename($fileurl));
			}
		}

		$tmp_picurls[] = $t;
		
    if(strpos($pictureurls, "://"))
    {
    $thumb = $pictureurls;
    }else{
    $thumb = '/'.$HS['uploaddir'].'/'.$CHA['channeldir'].'/'.$MOD['upload_dir'].'/'.$pictureurls;
    }
		
		
		$inputstring=new_htmlspecialchars(array('title'=>$title,'thumb'=>$thumb,'pictureurls'=>$pictureurls));
		extract($inputstring,EXTR_OVERWRITE);
		$content = str_safe($content);
		$addtime = $PHP_TIME;

		$urlruleid = $CAT['ishtml'] ? $CAT['item_html_urlruleid'] : $CAT['item_php_urlruleid'];



		$query = "INSERT INTO ".channel_table('picture', $channelid)."(catid,typeid,title,introduce,thumb,status,islink,author,copyfrom,pictureurls,username,addtime,editor,edittime,ishtml,urlruleid,htmldir,prefix) VALUES('$catid','$typeid','$title','$content','$thumb','$status','0','$author','$copyfrom','$pictureurls','$_username','$addtime','$_username','$addtime','$CAT[ishtml]', '$urlruleid','$CAT[item_htmldir]','$CAT[item_prefix]')";
		$db->query($query);
		$pictureid = $db->insert_id();

		require HS_ROOT.'/include/attachment.class.php';
		$att = new attachment;
		$att->attachment($pictureid, $channelid, $catid);
		$att->add($introduce);


		$linkurl = item_url('url', $catid, $CAT['ishtml'], $CAT['item_html_urlruleid'], $CAT['item_htmldir'], $CAT['item_prefix'], $pictureid, $addtime);
		$db->query("UPDATE ".channel_table('picture', $channelid)." SET linkurl='$linkurl' WHERE pictureid=$pictureid ");

		showmessage($LANG['picture_post_success'], $referer);
	}
	else
	{

		$type_select = type_select('typeid', $LANG['type']);
		$picture_province = isset($province) ? trim(urldecode($province)) : $HS['province'];
		//$picture_city = isset($city) ? trim(urldecode($city)) : $HS['city'];
		//$picture_area = isset($area) ? trim(urldecode($area)) : $HS['area'];
		//$disabled = $CHA['enablecheck'] && !$enableaddalways;
	}
break;

case 'edit':

    $pictureid or showmessage($LANG['picture_id_not_null'], 'goback');
    if($dosubmit)
	{
		checkcode($checkcodestr, $MOD['check_code'], $PHP_REFERER);
		if(!$catid)	showmessage($LANG['sorry_select_parent_category'], 'goback');
		if($catid < 3 || $catid > 4)	showmessage("你没有权限在此栏目发布", 'goback');
		if(empty($title)) showmessage($LANG['sorry_title_not_null'], 'goback');
		if(empty($author)) showmessage("请填写游戏中昵称", 'goback');
		if(empty($copyfrom)) showmessage("请填写所在服务器", 'goback');
		if($CAT['child'] && !$CAT['enableadd']) showmessage($LANG['appoint_category_not_allowed_to_add_picture'], 'goback');
		if(empty($pictureurls)) showmessage($LANG['picture_url_not_null'], 'goback');
		if(empty($content))	showmessage("图片说明不能为空", 'goback');
		if($CAT['arrgroupid_add'] && strpos($CAT['arrgroupid_add'], "$_groupid") === false) showmessage($LANG['not_allowed_to_add_by_your_group'], 'goback');
		$picurls = explode("\n", $pictureurls);
		$tmp_picurls = array();
		foreach($picurls as $picurl)
		{
			$t = '';
			if(strpos($picurl, "|") === false)
			{
				$t = $picurl;
			}
			else
			{
				$s = explode("|", $picurl);
				$t = $s[1];
			}
			$tmp_picurls[] = $t;
		}
		$tmp_allpicurls = explode("\n", $allpictureurls);
		$tmp_array = array_diff($tmp_allpicurls, $tmp_picurls);
		if(!empty($tmp_array))
		{
			foreach($tmp_array as $v)
			{
				if(strpos($v, "://")) continue;
				$fileurl = trim(HS_ROOT.'/'.$CHA['channeldir'].'/'.$MOD['upload_dir'].'/'.$v);
				@unlink($fileurl);
				@unlink(dirname($fileurl).'/thumb_'.basename($fileurl));
			}
		}
		$inputstring = new_htmlspecialchars(array('title'=>$title,'thumb'=>$thumb,'pictureurls'=>$pictureurls));
		extract($inputstring,EXTR_OVERWRITE);
		$content = str_safe($content);


    if(strpos($pictureurls, "://"))
    {
    $thumb = $pictureurls;
    }else{
    $thumb = '/'.$HS['uploaddir'].'/'.$CHA['channeldir'].'/'.$MOD['upload_dir'].'/'.$pictureurls;
    }
    
		$query = "UPDATE ".channel_table('picture', $channelid)." SET catid='$catid',typeid='$typeid',title='$title',author='$author',copyfrom='$copyfrom',thumb='$thumb',status='$status',introduce='$content',pictureurls='$pictureurls',editor='$_username',edittime='$PHP_TIME' WHERE pictureid=$pictureid  AND username='$_username'";
		$db->query($query);

		showmessage($LANG['picture_edit_success'], $referer);
	}
	else
	{
		$r = $db->get_one("SELECT * FROM ".channel_table('picture', $channelid)." WHERE pictureid=$pictureid AND username='$_username'");
		if(!$r['pictureid']) showmessage($LANG['picture_not_exist_or_no_permission'], 'goback');
		@extract($r);
		$type_select = type_select('typeid', $LANG['type'], $typeid);
		$category_select = category_select('catid', $LANG['select_category'], $catid, 'id="catid"');
		$pictureurls = trim($pictureurls);
		$picurls = explode("\n", $pictureurls);
		$allpictureurls = array();
		foreach($picurls as $picurl)
		{
			$t = '';
			if(strpos($picurl, "|") === false)
			{
				$t = $picurl;
			}
			else
			{
				$s = explode("|", $picurl);
				$t = $s[1];
			}
			$allpictureurls[] = $t;
		}
		$allpictureurls = implode("\n", $allpictureurls);
	}
break;

case 'delete':

	$pictureid or showmessage($LANG['picture_id_not_null'], ' goback'); 
	$db->query("DELETE FROM ".channel_table('picture', $channelid)." WHERE pictureid=$pictureid AND username='$_username' AND status!=3 ");
	if($db->affected_rows()>0)
	{
		showmessage($LANG['picture_delete_success'],$referer);
	}
	else
	{
		showmessage($LANG['picture_delete_fail'], 'goback');
	}
break;

case 'manage':


	if($catid && !array_key_exists($catid, $CATEGORY)) $catid = 0;
	if(!isset($page))
	{
		$page=1;
		$offset=0;
	}
	else
	{
		$offset=($page-1)*$pagesize;
	}


	$sql = '';

	if($catid)
	{
		$sql .=  $CATEGORY[$catid]['child'] ? " and catid in({$CATEGORY[$catid]['arrchildid']}) " : " and catid = $catid ";
	}

	$query="SELECT COUNT(pictureid) as num FROM ".channel_table('picture', $channelid)." WHERE status='$status' AND username='$_username' $sql ";
	$result=$db->query($query);
	$r=$db->fetch_array($result);
	$number=$r["num"];

	$pages=phppages($number, $page, $pagesize);
	$pictures = array();

	$query="SELECT * FROM ".channel_table('picture', $channelid)." WHERE username='$_username' $sql ORDER BY status ASC LIMIT $offset,$pagesize ";
	$result=$db->query($query);
	if($db->num_rows($result)>0)
	{
		while($r=$db->fetch_array($result))
		{
			$r['linkurl'] = linkurl($r['linkurl']);
			$r['title'] = str_cut($r['title'], 46, '...');
			$r['title'] = style($r['title'], $r['style']);
			$r['adddate']=date("Y-m-d H:i:s",$r['addtime']);
			$r['checktime']=date("Y-m-d H:i:s",$r['checktime']);			
			$r['catname'] = $CATEGORY[$r['catid']]['catname'];
			$r['catlinkurl'] = $CATEGORY[$r['catid']]['linkurl'];
			$r['type'] = $r['typeid'] ? show_type($channelid, $r['typeid']) : '';
			$pictures[]=$r;
		}
	}
break;
}

$head['title'] = $LANG['my_pictrue_manage'];
$head['keywords'] = $HS['seo_keywords'];
$head['description'] = $HS['seo_description'];
include template('picture', 'myitem');

function codedocheck($chkcode)
{
	global $session;
	if($chkcode == '') return "nocode";
	if(!is_object($session)) $session = new hs_session();
	if(strtolower($_SESSION['checkcode']) != strtolower($chkcode))
	{
	  unset($_SESSION['checkcode']);
	  return "errcode";
	}else{
   return "true";
  }
}

?>