<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');

InitGP(array('a'));
InitGP(array('cyid'), null, 2);

$colony = $db->get_one("SELECT c.*,cm.id AS ifcyer,cm.ifadmin FROM pw_colonys c LEFT JOIN pw_cmembers cm ON c.id=cm.colonyid AND cm.uid=" . pwEscape($winduid) . ' WHERE c.id=' . pwEscape($cyid));
empty($colony) && Showmsg('data_error');

if ($colony['cnimg']) {
	list($cnimg,$colony['imgtype']) = geturl("cn_img/$colony[cnimg]",'lf');
} else {
	$cnimg = $pwModeImg.'/nophoto.gif';
}
$ifadmin = ($colony['ifadmin'] == '1' || $colony['admin'] == $windid || $groupid == 3);

if (empty($a)) {

	InitGP(array('page'), null, 2);
	$page < 1 && $page = 1;
	$db_perpage = 10;
	$pages = numofpage($colony['albumnum'], $page, ceil($colony['albumnum']/$db_perpage), "{$basename}&q=$q&a=$a&");
	$album = array();
	$query = $db->query("SELECT aid,aname,photonum,lastphoto,private,lasttime FROM pw_cnalbum WHERE atype='1' AND ownerid=" . pwEscape($cyid) . ' ORDER BY aid DESC');
	while ($rt = $db->fetch_array($query)) {
		$rt['lasttime'] = get_date($rt['lasttime'],'Y-m-d');
		$rt['lastphoto'] = getphotourl($rt['lastphoto']);
		$album[] = $rt;
	}
	$atc_name = getLangInfo('o','group',false,1);
	$title = "$colony[cname] ($db_bbsurl/{$basename}q=group&cyid=$cyid)";
	$descrip = $colony['descrip'];


	require_once(R_P.'require/header.php');
	require_once PrintEot('m_galbum');
	footer();

} elseif ($a == 'album') {

	InitGP(array('aid','page'), null, 2);

	$page < 1 && $page = 1;
	$db_perpage = 20;
	$cnpho = array();
	$album = $db->get_one("SELECT aname,aintro,ownerid,photonum FROM pw_cnalbum WHERE atype='1' AND aid=" . pwEscape($aid));
	if (empty($album)) {
		Showmsg('data_error');
	}

	$query = $db->query("SELECT pid,path,ifthumb FROM pw_cnphoto WHERE aid=" . pwEscape($aid) . ' ORDER BY pid ' . pwLimit(($page - 1) * $db_perpage, $db_perpage));
	while ($rt = $db->fetch_array($query)) {
		$rt['path'] = getphotourl($rt['path'], $rt['ifthumb']);
		$cnpho[] = $rt;
	}
	require_once(R_P.'require/header.php');
	require_once PrintEot('m_galbum');footer();

} elseif ($a == 'view') {

	InitGP(array('pid'), null, 2);

	$photo = $db->get_one("SELECT p.pid,p.aid,p.pintro,p.path,p.uploader,p.uptime,p.hits,a.aname,a.atype, a.private,a.ownerid,a.photonum FROM pw_cnphoto p LEFT JOIN pw_cnalbum a ON p.aid=a.aid WHERE p.pid=" . pwEscape($pid));
	empty($photo) && Showmsg('data_error');
	if ($photo['private'] == 1) {

	}
	$photo['uptime'] = get_date($photo['uptime']);
	$photo['path'] = getphotourl($photo['path']);

	$num = $db->get_value("SELECT COUNT(*) AS sum FROM pw_cnphoto WHERE aid=" . pwEscape($photo['aid']) . ' AND pid<=' . pwEscape($pid));
	require_once(R_P.'require/header.php');
	require_once PrintEot('m_galbum');footer();

} elseif ($a == 'editphoto') {

	InitGP(array('pid'), null, 2);

	$photo = $db->get_one("SELECT p.aid,p.pintro,a.ownerid FROM pw_cnphoto p LEFT JOIN pw_cnalbum a ON p.aid=a.aid WHERE pid=" . pwEscape($pid));
	if (empty($photo)) {
		Showmsg('data_error');
	}
	if (empty($_POST['step'])) {

		$options = '';
		$query = $db->query("SELECT aid,aname FROM pw_cnalbum WHERE atype='1' AND ownerid=" . pwEscape($cyid) . ' ORDER BY aid DESC');
		while ($rt = $db->fetch_array($query)) {
			$options .= "<option value=\"$rt[aid]\"" . (($rt['aid'] == $photo['aid']) ? ' selected' : '') . ">$rt[aname]</option>";
		}
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_galbum');footer();

	} else {

		InitGP(array('pintro'),'P');
		InitGP(array('aid'), null, 2);
		!$aid && Showmsg('colony_albumclass');

		$pwSQL = array('pintro' => $pintro);

		$ischage = false;
		if ($aid != $photo['aid'] && $winduid == $db->get_value("SELECT ownerid FROM pw_cnalbum WHERE aid=" . pwEscape($aid))) {
			$pwSQL['aid'] = $aid;
			$ischage = true;
		}
		$db->update("UPDATE pw_cnphoto SET " . pwSqlSingle($pwSQL) . ' WHERE pid=' . pwEscape($pid));

		if ($ischage) {
			$phnum = array();
			$query = $db->query("SELECT aid,COUNT(*) AS sum FROM pw_cnphoto WHERE aid IN(" . pwImplode(array($aid,$photo['aid'])) . ') GROUP BY aid');
			while ($rt = $db->fetch_array($query)) {
				$phnum[$rt['aid']] = $rt['sum'];
			}
			$db->update("UPDATE pw_cnalbum SET " . pwSqlSingle(array('photonum' => $phnum[$aid] ? $phnum[$aid] : 0, 'lastpid' => implode(',',getLastPid($aid)))) . ' WHERE aid=' . pwEscape($aid));

			$db->update("UPDATE pw_cnalbum SET " . pwSqlSingle(array('photonum' => $phnum[$photo['aid']] ? $phnum[$photo['aid']] : 0, 'lastpid' => implode(',',getLastPid($photo['aid'])))) . ' WHERE aid=' . pwEscape($photo['aid']));
		}

		refreshto("{$basename}q=$q&a=view&cyid=$cyid&pid=$pid",'operate_success');
	}
} elseif ($a == 'upload') {

	InitGP(array('aid'));

	if (empty($_POST['step'])) {

		$extra_url = $options = '';
		$query = $db->query("SELECT aid,aname FROM pw_cnalbum WHERE atype='1' AND ownerid=" . pwEscape($cyid) . ' ORDER BY aid DESC');
		while ($rt = $db->fetch_array($query)) {
			$options .= "<option value=\"$rt[aid]\"" . (($aid && $rt['aid'] == $aid) ? ' selected' : '') . ">$rt[aname]</option>";
		}
		$aid && $extra_url = '&aid=' . $aid;
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_galbum');footer();

	} else {

		InitGP(array('pintro'),'P');
		!$aid && Showmsg('colony_albumclass');

		$rt = $db->get_one("SELECT photonum,ownerid,lastphoto FROM pw_cnalbum WHERE atype='1' AND aid=" . pwEscape($aid));
		if (empty($rt)) {
			Showmsg('undefined_action');
		} elseif ($cyid <> $rt['ownerid']) {
			Showmsg('colony_phototype');
		}
		$rt['photonum'] >= $o_maxphotonum && Showmsg('colony_photofull');

		require_once(R_P.'require/postfunc.php');
		$db_attachdir = $o_mkdir;
		!$cn_maxfilesize && $cn_maxfilesize = 1000;
		$db_uploadfiletype = array();
		$db_uploadfiletype['gif'] = $db_uploadfiletype['jpg'] = $db_uploadfiletype['jpeg'] = $db_uploadfiletype['bmp'] = $db_uploadfiletype['png'] = $o_maxfilesize;
		list($db_thumbw,$db_thumbh) = explode("\t",$db_athumbsize);
		$db_athumbsize = "100\t100";

		$uploaddb = UploadFile(randstr(4).$timestamp,'photo');
		pwFtpClose($ftp);

		!$uploaddb[0]['attachurl'] && Showmsg('colony_uploadnull');
		$fileuplodeurl = $uploaddb[0]['attachurl'];

		$lastpid = getLastPid($aid,4);
		$db->update("INSERT INTO pw_cnphoto SET " . pwSqlSingle(array(
			'aid'		=> $aid,
			'pintro'	=> $pintro,
			'path'		=> $fileuplodeurl,
			'uploader'	=> $windid,
			'uptime'	=> $timestamp,
			'ifthumb'	=> $uploaddb[0]['ifthumb']
		)));
		$pid = $db->insert_id();
		array_unshift($lastpid,$pid);

		$db->update("UPDATE pw_cnalbum SET photonum=photonum+1,lasttime=" . pwEscape($timestamp) . ',lastpid=' . pwEscape(implode(',',$lastpid)) . (!$rt['lastphoto'] ? ',lastphoto='.pwEscape($fileuplodeurl) : '') . " WHERE aid=" . pwEscape($aid));
		countPosts('+1');
		refreshto("{$basename}q=$q&a=view&cyid=$cyid&pid=$pid",'operate_success');
	}
} elseif ($a == 'delphoto') {

	define('AJAX','1');
	InitGP(array('pid'), null, 2);
	$photo = $db->get_one("SELECT cp.uploader,cp.path,ca.aid,ca.lastphoto,ca.lastpid FROM pw_cnphoto cp LEFT JOIN pw_cnalbum ca ON cp.aid=ca.aid WHERE cp.pid=" . pwEscape($pid) . " AND ca.atype='1' AND ca.ownerid=" . pwEscape($cyid));
	if (empty($photo) || (!$ifadmin && $photo['uploader'] != $windid)) {
		Showmsg('data_error');
	}
	$db->update("DELETE FROM pw_cnphoto WHERE pid=" . pwEscape($pid));

	$pwSQL = array();
	if ($photo['path'] == $photo['lastphoto']) {
		$pwSQL['lastphoto'] = $db->get_value("SELECT path FROM pw_cnphoto WHERE aid=" . pwEscape($photo['aid']) . " ORDER BY pid DESC LIMIT 1");
	}
	if (strpos(",$photo[lastpid],",",$pid,") !== false) {
		$pwSQL['lastpid'] = implode(',',getLastPid($photo['aid']));
	}
	$upsql = $pwSQL ? ',' . pwSqlSingle($pwSQL) : '';
	$db->update("UPDATE pw_cnalbum SET photonum=photonum-1{$upsql} WHERE aid=" . pwEscape($photo['aid']));

	pwDelatt($photo['path'], $db_ifftp);
	$lastpos = strrpos($photo['path'],'/') + 1;
	pwDelatt(substr($photo['path'], 0, $lastpos) . 's_' . substr($photo['path'], $lastpos), $db_ifftp);
	pwFtpClose($ftp);
	$affected_rows = delAppAction('photo',$pid)+1;
	countPosts("-$affected_rows");
	echo 'ok';ajax_footer();

} elseif ($a == 'delalbum') {

	define('AJAX', 1);
	InitGP(array('aid'), null, 2);
	!$ifadmin && Showmsg('undefined_action');
	$album = $db->get_one("SELECT * FROM pw_cnalbum WHERE aid=" . pwEscape($aid) . " AND atype='1'");

	if (empty($album) || $album['ownerid'] != $cyid) {
		Showmsg('data_error');
	}

	if (empty($_POST['step'])) {
		require_once PrintEot('m_ajax');
		ajax_footer();

	} else {

		$query = $db->query("SELECT pid,path,ifthumb FROM pw_cnphoto WHERE aid=" . pwEscape($aid));
		if (($num = $db->num_rows($query)) > 0) {
			$affected_rows = 0;
			while ($rt = $db->fetch_array($query)) {
				pwDelatt($rt['path'], $db_ifftp);
				if ($rt['ifthumb']) {
					$lastpos = strrpos($rt['path'],'/') + 1;
					pwDelatt(substr($rt['path'], 0, $lastpos) . 's_' . substr($rt['path'], $lastpos), $db_ifftp);
				}
				$affected_rows += delAppAction('photo',$rt['pid'])+1;
			}
			pwFtpClose($ftp);
			countPosts("-$affected_rows");
		}
		$db->update("DELETE FROM pw_cnphoto WHERE aid=" . pwEscape($aid));
		$db->update("DELETE FROM pw_cnalbum WHERE aid=" . pwEscape($aid));
		$db->update("UPDATE pw_colonys SET albumnum=albumnum-1 WHERE id=" . pwEscape($cyid));

		echo getLangInfo('msg','operate_success') . "\tjump\t{$basename}q=$q&cyid=$cyid";
		ajax_footer();
	}
} elseif ($a == 'edit') {

	InitGP(array('aid'), null, 2);
	empty($aid) && Showmsg('data_error');
	!$ifadmin && Showmsg('undefined_action');

	$rt = $db->get_one("SELECT aid,aname,aintro,private FROM pw_cnalbum WHERE aid=" . pwEscape($aid) . " AND atype='1' AND ownerid=" . pwEscape($cyid));
	if (empty($rt)) {
		Showmsg('data_error');
	}
	if (empty($_POST['step'])) {

		$extra_url = '&a=album&aid=' . $aid;
		${'select_'.$rt['private']} = 'selected';
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_galbum');footer();

	} else {

		InitGP(array('aname','aintro'),'P');
		InitGP(array('private'),'P',2);
		!$aname && Showmsg('colony_aname_empty');

		$db->update("UPDATE pw_cnalbum SET " . pwSqlSingle(array('aname' => $aname, 'aintro' => $aintro, 'private' => $private ? 1 : 0)) . ' WHERE aid=' . pwEscape($aid));

		refreshto("{$basename}q=$q&cyid=$cyid&a=album&aid=$aid",'operate_success');
	}
} elseif ($a == 'create') {

	!$ifadmin && Showmsg('undefined_action');

	if (empty($_POST['step'])) {

		$rt = array();
		$extra_url = '';
		$select_0 = 'selected';
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_galbum');footer();

	} else {

		InitGP(array('aname','aintro'),'P');
		InitGP(array('private'),'P',2);
		!$aname && Showmsg('colony_aname_empty');
		if ($o_albumnum2 > 0 && $o_albumnum2 <= $colony['albumnum']) {
			Showmsg('colony_album_num2');
		}
		if ($o_camoney) {
			require_once(R_P.'require/credit.php');
			if ($o_camoney > $credit->get($winduid,$o_moneytype)) {
				Showmsg('colony_moneylimit2');
			}
			$credit->addLog('hack_cyalbum',array($o_moneytype => -$o_camoney),array(
				'uid'		=> $winduid,
				'username'	=> $windid,
				'ip'		=> $onlineip,
				'aname'		=> $aname
			));
			$credit->set($winduid,$o_moneytype,-$o_camoney);
		}
		$db->update("INSERT INTO pw_cnalbum SET " . pwSqlSingle(array(
				'aname'		=> $aname,			'aintro'	=> $aintro,
				'atype'		=> 1,				'private'	=> $private ? 1 : 0,
				'ownerid'	=> $cyid,			'owner'		=> $colony['cname'],
				'lasttime'	=> $timestamp,		'crtime'	=> $timestamp
		)));
		$db->update("UPDATE pw_colonys SET albumnum=albumnum+1 WHERE id=" . pwEscape($cyid));

		refreshto("{$basename}q=$q&cyid=$cyid",'operate_success');
	}
}
?>