<?php
if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( 'init.php' );
}

include('sessioninc.php');

$type='profilepics';

if (isset($_REQUEST['type']) && $_REQUEST['type'] == 'gallery') {
	$type=$_REQUEST['type'];
}

$userid = $_REQUEST['id'];

$username = $osDB->getOne('select username from ! where id = ?',array( USER_TABLE, $userid) );

if ($type == 'profilepics') {
	$search=' and (album_id is null or album_id = 0)';
} else {
	$search=' and  album_id > 0 ';

	$useralbums =& $osDB->getAll('select id, name, passwd from ! where username = ? ', array(USERALBUMS_TABLE, $username) );

	if (count($useralbums) > 0) {
		$albumpasswd = 0;

		foreach ($useralbums as $k => $row) {
			if ($row['passwd'] != '') {
				$albumpasswd++;
				$useralbums[$k]['password']='';
			}
		}

		$useralbums = array_merge(array(array('id'=>'999','name'=>'general')), $useralbums);

		$t->assign('albumpasswd', $albumpasswd);

		$t->assign('useralbums', $useralbums);

		unset($useralbums);
	}
}

$t->assign('type',$type);

$album_passwd = $_REQUEST['album_passwd'];

if ($type == 'gallery') {
	$album_id = ($_REQUEST['album_id'] !='')?$_REQUEST['album_id']:'999';
}
if ($album_id != '') {

	/* First check if the user opted to allow membrs in the buddy list to view */

	$buddy_view = $osDB->getOne('select choice_value from ! where userid=? and choice_name=?', array(USER_CHOICES_TABLE, $userid, 'allow_buddy_view_album') );
	$hotlist_view = $osDB->getOne('select choice_value from ! where userid=? and choice_name=?', array(USER_CHOICES_TABLE, $userid, 'allow_hotlist_view_album') );

	if ($buddy_view == '1' or $buddy_view == '' or !isset($buddy_view) ) {

		$in_buddy_list = $osDB->getOne('select id from ! where username = ? and ref_username = ? and act = ?', array(BUDDY_BAN_TABLE, $username, $_SESSION['UserName'], 'F') );

	}

	if ($hotlist_view == '1' or $hotlist_view == '' or !isset($hotlist_view) ) {

		$in_hot_list = $osDB->getOne('select id from ! where username = ? and ref_username = ? and act = ?', array(BUDDY_BAN_TABLE, $username, $_SESSION['UserName'], 'H') );

	}

	if ( ($in_buddy_list == '' or !isset($in_buddy_list) or $in_buddy_list < 0 or $buddy_view == '0' or !isset($buddy_view)) and ($in_hot_list == '' or !isset($in_hot_list) or $in_hot_list < 0 or $hotlist_view == '0' or !isset($hotlist_view)) ) {

		if ($album_id != '999') {
			$pwd = $osDB->getOne('select passwd from ! where username = ? and  id = ? ', array(USERALBUMS_TABLE, $username, $album_id) );

			if ($pwd != md5($album_passwd) && $userid != $_SESSION['UserId']) {

				$err = INVALID_PASSWORD;

				$album_id = '';
			}
		}
	}

}

if ($type == 'gallery') {
	$t->assign('pics',$osDB->getAll('select picno from ! where userid = ? and album_id =?',array( USER_SNAP_TABLE, $userid, $album_id) ));
} else {
	$t->assign('pics',$osDB->getAll('select picno from ! where userid = ? '.$search,array( USER_SNAP_TABLE, $userid)) );
}

$t->assign('username',$username);

$t->assign('userid',$userid);

$t->assign('error_message', get_lang('errormsgs',$err));

$t->assign('err',$err);

$t->assign('album_id', $album_id);

$t->assign('lang',$lang);

if ( $config['use_profilepopups'] == 'Y' ) {

	$t->display( 'userpicgallery.tpl' );

} else {

	$t->assign( 'rendered_page', $t->fetch( 'userpicgallery.tpl' ) );

	$t->display ( 'index.tpl' );
}


?>