<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');
include_once(D_P.'data/bbscache/o_config.php');
require_once(R_P.'require/showimg.php');

InitGP(array('a','u'));
$a_key = 'index';
$u = $u ? $u : $winduid;
if ($u && $u != $winduid) {
	$userdb = $db->get_one("SELECT username,icon FROM pw_members WHERE uid=".pwEscape($u));
	if (empty($userdb)) {
		$errorname = '';
		Showmsg('user_not_exists');
	}
	$thisbase = $basename .'u='.$u.'&';
	list($faceimg) = showfacedesign($userdb['icon'],1);
	$username = $userdb['username'];
} else {
	$username = $windid;
	list($faceimg) = showfacedesign($winddb['icon'],1);
	$thisbase = $basename;
}

if (empty($a)) {

	InitGP(array('page'),'GP',2);
	$page < 1 && $page = 1;
	$db_perpage = 10;
	$sum = $db->get_value("SELECT COUNT(*) AS sum FROM pw_cnalbum WHERE atype='0' AND ownerid=" . pwEscape($u));

	$lastpid = array();
	$albumdb = array();
	$smphoto = array();
	$query = $db->query("SELECT aid,aname,photonum,ownerid,owner,lastphoto,lastpid,lasttime FROM pw_cnalbum WHERE atype='0' AND ownerid=" . pwEscape($u) . ' ORDER BY aid DESC' . pwLimit(($page - 1) * $db_perpage, $db_perpage));
	while ($rt = $db->fetch_array($query)) {
		$rt['lasttime']		= get_date($rt['lasttime']);
		$rt['lastphoto']	= getphotourl($rt['lastphoto']);
		if ($rt['lastpid']) {
			$lastpid = array_merge($lastpid,explode(',',$rt['lastpid']));
		}
		$albumdb[] =  $rt;
	}
	if ($lastpid) {
		$query = $db->query('SELECT pid,aid,path FROM pw_cnphoto ORDER BY pid DESC LIMIT 6');
		while ($rt = $db->fetch_array($query)) {
			$rt['path']	= getphotourl($rt['path']);
			$smphoto[$rt['aid']][] = $rt;
		}
	}
	require_once(R_P.'require/header.php');
	require_once PrintEot('m_photos');footer();

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

	$a_key = 'friend';
	InitGP(array('page'),'GP',2);
	$page < 1 && $page = 1;
	$db_perpage = 10;
	$sum = $db->get_value("SELECT COUNT(*) AS sum FROM pw_friends f LEFT JOIN pw_cnalbum c ON c.atype='0' AND f.friendid=c.ownerid WHERE f.uid=" . pwEscape($winduid) . ' AND f.uid!=f.friendid AND c.aid IS NOT NULL');

	$lastpid = array();
	$albumdb = array();
	$smphoto = array();
	$query = $db->query("SELECT c.aid,c.aname,c.photonum,c.ownerid,c.owner,c.lastphoto,c.lastpid,c.lasttime FROM pw_friends f LEFT JOIN pw_cnalbum c ON c.atype='0' AND c.ownerid=f.friendid WHERE f.uid=" . pwEscape($winduid) . ' AND f.uid!=f.friendid AND c.aid IS NOT NULL ORDER BY c.aid DESC' . pwLimit(($page - 1) * $db_perpage, $db_perpage));
	while ($rt = $db->fetch_array($query)) {
		$rt['lasttime']		= get_date($rt['lasttime']);
		$rt['lastphoto']	= getphotourl($rt['lastphoto']);
		if ($rt['lastpid']) {
			$lastpid = array_merge($lastpid,explode(',',$rt['lastpid']));
		}
		$albumdb[] =  $rt;
	}
	if ($lastpid) {
		$query = $db->query("SELECT pid,aid,path FROM pw_cnphoto WHERE pid IN(" . pwImplode($lastpid) . ') ORDER BY pid DESC');
		while ($rt = $db->fetch_array($query)) {
			$rt['path']	= getphotourl($rt['path']);
			$smphoto[$rt['aid']][] = $rt;
		}
	}
	require_once(R_P.'require/header.php');
	require_once PrintEot('m_photos');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,private,ownerid,owner,photonum FROM pw_cnalbum WHERE atype='0' AND aid=" . pwEscape($aid));
	empty($album) && Showmsg('data_error');

	if ($album['ownerid'] <> $winduid && $album['private'] == 1 && !isFriend($album['ownerid'],$winduid)) {
		Showmsg('mode_o_photos_private');
	}
	$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_photos');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,p.c_num,a.aname,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) . " AND a.atype='0'");
	empty($photo) && Showmsg('data_error');

	if ($photo['ownerid'] <> $winduid && $photo['private'] == 1 && !isFriend($photo['ownerid'],$winduid)) {
		Showmsg('mode_o_photos_private');
	}
	$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_photos');footer();

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

	define('AJAX',1);
	InitGP(array('pid'), null, 2);
	$pid = $db->get_value("SELECT MIN(b.pid) AS pid FROM pw_cnphoto a LEFT JOIN pw_cnphoto b ON a.aid=b.aid AND a.pid<b.pid WHERE a.pid=" . pwEscape($pid));
	echo "ok\t$pid";
	ajax_footer();

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

	define('AJAX',1);
	InitGP(array('pid'), null, 2);
	$pid = $db->get_value("SELECT MAX(b.pid) AS pid FROM pw_cnphoto a LEFT JOIN pw_cnphoto b ON a.aid=b.aid AND a.pid>b.pid WHERE a.pid=" . pwEscape($pid));
	echo "ok\t$pid";
	ajax_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) || $photo['ownerid'] <> $winduid) {
		Showmsg('data_error');
	}
	if (empty($_POST['step'])) {

		$options = '';
		$query = $db->query("SELECT aid,aname FROM pw_cnalbum WHERE atype='0' AND ownerid=" . pwEscape($winduid) . ' 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_photos');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("{$thisbase}q=$q&a=view&pid=$pid",'operate_success');
	}
} elseif ($a == 'delphoto') {

	define('AJAX','1');
	InitGP(array('pid'), null, 2);
	$photo = $db->get_one("SELECT 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='0' AND ca.ownerid=" . pwEscape($winduid));

	if (empty($photo)) {
		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);
	echo 'ok';ajax_footer();

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

	$a_key = 'upload';
	InitGP(array('aid'));

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

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

	} else {

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

		$rt = $db->get_one("SELECT aname,photonum,ownerid,private,lastphoto FROM pw_cnalbum WHERE atype='0' AND aid=" . pwEscape($aid));
		if (empty($rt)) {
			Showmsg('undefined_action');
		} elseif ($winduid != $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($winduid,'photo');
		pwFtpClose($ftp);
		$photoNum = 0;$photos = array();
		foreach ($uploaddb as $value) {
			if ($value['attachurl']) {
				$photoNum++;
				$fileuplodeurl = $value['attachurl'];
				$photos[] = array(
					'aid'		=> $aid,
					'pintro'	=> $pintro[$value['id']],
					'path'		=> $value['attachurl'],
					'uploader'	=> $windid,
					'uptime'	=> $timestamp,
					'ifthumb'	=> $value['ifthumb']
				);
			}
		}

		empty($photoNum) && Showmsg('colony_uploadnull');
		$db->update("INSERT INTO pw_cnphoto (aid,pintro,path,uploader,uptime,ifthumb) VALUES " . pwSqlMulti($photos));
		$pid = $db->insert_id();
		$lastpid = getLastPid($aid,4);
		array_unshift($lastpid,$pid);

		if (!$rt['private']) {
			$feedText = "[url=$db_bbsurl/{$thisbase}q=photos&a=album&aid=$aid]{$rt[aname]}[/url]\n";
			foreach ($photos as $value) {
				$feedText .= "[url=$db_bbsurl/{$thisbase}q=photos&a=view&pid=$pid][img]".getphotourl($value['path'], $value['ifthumb'])."[/img][/url]&nbsp;";
			}

			pwAddFeed($winduid,'photo',$pid,array('num'=>$photoNum,'text'=>$feedText));
		}

		$db->update("UPDATE pw_cnalbum SET photonum=photonum+".pwEscape($photoNum,false).",lasttime=" . pwEscape($timestamp,false) . ',lastpid=' . pwEscape(implode(',',$lastpid)) . (!$rt['lastphoto'] ? ',lastphoto='.pwEscape($fileuplodeurl) : '') . " WHERE aid=" . pwEscape($aid));

		refreshto("{$thisbase}q=$q&a=view&pid=$pid",'operate_success');
	}
} elseif ($a == 'create') {

	$a_key = 'create';
	if (empty($_POST['step'])) {

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

	} else {

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

		if ($o_albumnum2 > 0 && $o_albumnum2 <= $db->get_value("SELECT COUNT(*) AS count FROM pw_cnalbum WHERE atype='0' AND ownerid=" . pwEscape($winduid))) {
			Showmsg('colony_album_num2');
		}
		if ($o_camoney) {
			require_once R_P.'require/credit.php';
			if ($o_camoney > $credit->get($winduid,$o_moneytype)) {
				$moneyname = $credit->cType[$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'		=> 0,				'private'	=> $private ? 1 : 0,
				'ownerid'	=> $winduid,		'owner'		=> $windid,
				'lasttime'	=> $timestamp,		'crtime'	=> $timestamp
		)));
		refreshto("{$thisbase}q=$q",'operate_success');
	}
} elseif ($a == 'delalbum') {

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

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

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

		require_once PrintEot('m_ajax');
		ajax_footer();

	} else {

		$query = $db->query("SELECT path,ifthumb FROM pw_cnphoto WHERE aid=" . pwEscape($aid));
		if ($db->num_rows($query) > 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);
				}
			}
			pwFtpClose($ftp);
		}
		$db->update("DELETE FROM pw_cnphoto WHERE aid=" . pwEscape($aid));
		$db->update("DELETE FROM pw_cnalbum WHERE aid=" . pwEscape($aid));

		echo getLangInfo('msg','operate_success') . "\tjump\t{$thisbase}q=$q";
		ajax_footer();
	}
} elseif ($a == 'editalbum') {
	define('AJAX', 1);
	InitGP(array('aid'));
	empty($aid) && Showmsg('data_error');

	$rt = $db->get_one("SELECT aid,aname,aintro,atype,private,ownerid FROM pw_cnalbum WHERE aid=" . pwEscape($aid));
	if (empty($rt) || $rt['atype'] <> 0 || $rt['ownerid'] <> $winduid) {
		Showmsg('data_error');
	}
	if (empty($_POST['step'])) {
		${'select_'.$rt['private']} = 'selected';
		require_once PrintEot('m_ajax');ajax_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("{$thisbase}q=$q",'operate_success');
	}
}
?>