<?php

include './core.php';

if( !isset( $user ) ) access_error();

if( isset( $_REQUEST['view'] ) )
{
    $picture = $MySQL->query_first( sprintf("SELECT * FROM user_pictures WHERE accountid = %d", intval( $_REQUEST['view'] ) ));
    $picture['imgurl'] = combine_path( $settings['user_gallery_dir'], $picture['accountid'].'.'.$picture['extension'] );
    $picture['imgsize'] = @getimagesize( $picture['imgurl'] );
    $Smarty->assign( 'picture', $picture );

    $Smarty->display( 'user_gallery_view.tpl' );

    die();
}
elseif( isset( $_POST['send_form'] ) )
{
    $form_errors = array();

    if( !isset( $_FILES['file'] ) )
        $form_errors[] = form_error( 'uncomplete' );
    else
    {
        $file_extension = strtolower( substr( strrchr( $_FILES['file']['name'], "." ), 1 ) );
		if( !in_array( $file_extension, array( "jpg", "jpeg", "gif" ) ) )
            $form_errors[] = form_error( 'no_picture' );
        if( $_FILES['file']['size'] > intval( $settings['max_user_picture_size'] ))
        {
            $form_errors[] = form_error( 'toobig_user_picture' );
        }
    }

    if( count( $form_errors ) > 0 )
    {
        $Smarty->assign( 'form_errors', $form_errors );
    }
    else
    {
    	$old_picture = $MySQL->query_first( sprintf("SELECT * FROM user_pictures WHERE accountid = %d",$user->ID ));
        if( $old_picture['accountid'] > 0 )
        {
        	@unlink( combine_path( $settings['user_gallery_dir'], $user->ID.".".$old_picture['extension'] ) );
            $MySQL->query( sprintf("UPDATE user_pictures SET extension = '%s' WHERE accountid = %d", me_escape($file_extension), $user->ID ));
        }
        else
        	$MySQL->query( sprintf("INSERT INTO user_pictures (accountid, extension) VALUES (%d, '%s')", $user->ID, me_escape($file_extension) ));

        $imgpath = combine_path( $settings['user_gallery_dir'], $user->ID.'.'.$file_extension );

        move_uploaded_file( $_FILES['file']['tmp_name'], $imgpath );
        @chmod ( $imgpath, 0777 );
    }
}
elseif( isset( $_POST['delete_picture'] ) )
{
    $old_picture = $MySQL->query_first( sprintf("SELECT * FROM user_pictures WHERE accountid = %d", $user->ID ));
    if( $old_picture['accountid'] > 0 )
    {
        @unlink( combine_path( $settings['user_gallery_dir'], $user->ID.".".$old_picture['extension'] ) );
        $MySQL->query( sprintf("DELETE FROM user_pictures WHERE accountid = %d", $user->ID ));
    }
}

// start page navigation

if( !isset( $_REQUEST['page_num'] ) || $_REQUEST['page_num'] < 1 )
	$page_num = 1;
else
	$page_num = intval( $_REQUEST['page_num'] );

$Smarty->assign( 'page_num', $page_num );

list( $picture_count ) = $MySQL->query_first( "SELECT COUNT(*) FROM user_pictures" );
$pages = ceil( $picture_count / intval( $settings['accounts_perpage'] ) );

$Smarty->assign( 'pages', $pages );

$page_navigation = make_page_navigation( "user_gallery.php?".SID, $page_num, $pages, 2 );
$Smarty->assign( 'page_navigation', $page_navigation );

// end page navigation

if( isset( $_POST['save_comments'] ) && $user->can_access( 'user_gallery_comments' ) )
{
    $list = $MySQL->query( sprintf("SELECT user_pictures.accountid FROM user_pictures, server.accounts WHERE user_pictures.activation = 1 && user_pictures.accountid = server.accounts.accountid ORDER BY server.accounts.name LIMIT %d, %d", ( intval( $settings['accounts_perpage'] ) * ( $page_num - 1 ) ), $settings['accounts_perpage'] ));
	while( $row = $MySQL->fetch_array( $list ) )
	{
        $MySQL->query( sprintf("UPDATE user_pictures SET comment = '%s' WHERE accountid = %d", me_escape( $_POST[$row['accountid']] ), $row['accountid'] ));
	}
}

$pictures_res = $MySQL->query( sprintf("SELECT user_pictures.*, server.accounts.name AS picture_owner FROM user_pictures, server.accounts WHERE user_pictures.activation = 1 && user_pictures.accountid = server.accounts.accountid ORDER BY server.accounts.name LIMIT %d, %d", ( intval( $settings['accounts_perpage'] ) * ( $page_num - 1 ) ), $settings['accounts_perpage'] ));
$pictures = array();
while( $row = $MySQL->fetch_array( $pictures_res ) )
{
    $row['imgurl'] = combine_path( $settings['user_gallery_dir'], $row['accountid'].'.'.$row['extension'] );
    $row['imgsize'] = @getimagesize( $row['imgurl'] );

    $pictures[] = $row;
}
$Smarty->assign( 'pictures', $pictures );

$page['content'] = $Smarty->fetch( 'user_gallery.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>