Файл: present.php
Строк: 229
<?php
/*************************************************
 *    apwa.ru       present.php
 *                 -------------
 *  Anv@r.all 2011 // Модуль подарков
 ************************************************/
 
define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
$userdata = session_pagestart($user_ip, PAGE_GUESTBOOK);
init_userprefs($userdata);
if ( empty($HTTP_GET_VARS[POST_USERS_URL]) || $HTTP_GET_VARS[POST_USERS_URL] == ANONYMOUS )
{
    message_die(GENERAL_MESSAGE, $lang['No_user_id_specified']);
}
$profiledata = get_userdata($HTTP_GET_VARS[POST_USERS_URL]);
if (!$profiledata)
{
    message_die(GENERAL_MESSAGE, $lang['No_user_id_specified']);
}
/*
if (isset($HTTP_GET_VARS['present'])) 
{    
    $user_id = intval($HTTP_GET_VARS[POST_USERS_URL]);    
    if ( $userdata['user_points'] < $points_config['present_points'] )
    {
        message_die(GENERAL_MESSAGE, $lang['Not_enough_points']);
    }
    if ( $userdata['user_id'] == $user_id )
    {
        message_die(GENERAL_MESSAGE, $lang['Error_user_donate']);
    }    
    $sql = "SELECT username 
        FROM " . USERS_TABLE . " 
        WHERE user_id = '$user_id'";
    if ( !$result = $db->sql_query($sql) )
    {
        message_die(GENERAL_ERROR, 'Could not obtain user information', '', __LINE__, __FILE__, $sql);
    }
    if ( !$row = $db->sql_fetchrow($result) )
    {
        message_die(GENERAL_MESSAGE, $lang['No_user_id_specified']);
    }
    $username = $row['username'];
    
    if ( isset($HTTP_POST_VARS['submit']) || isset($HTTP_POST_VARS['submitimg']) )
    {
            $filename = ( !empty($HTTP_POST_VARS['select_img']) ) ? htmlspecialchars($HTTP_POST_VARS['select_img']) : '';    
            $category = ( !empty($HTTP_POST_VARS['category']) ) ? htmlspecialchars($HTTP_POST_VARS['category']) : '';
            $message = trim(htmlspecialchars($HTTP_POST_VARS['message']));
            $no_del = ( !empty($HTTP_POST_VARS['nodel']) ) ? 1 : 0;
            if ( $no_del == '1' )
            {
                $sql = "UPDATE " . USERS_TABLE . "
                    SET user_points = user_points - " . $points_config['present_points'] . "
                    WHERE user_id = " . $userdata['user_id'];
                if ( !($result = $db->sql_query($sql)) )
                {
                    message_die(GENERAL_ERROR, 'Could not update users table', '', __LINE__, __FILE__, $sql);
                }
            }
            $filename = phpbb_ltrim(basename($filename), "'");
            $category = phpbb_ltrim(basename($category), "'");
            
        if ($filename == "" || $category == "")
            {
                message_die(GENERAL_MESSAGE, $lang['No_image_present']);
            }         
            $sql = "INSERT INTO " . PRESENTS_TABLE . " (present_img, present_user_id, donate_user_id, donate_username, donate_time, message, nodel)
            VALUES ('" . str_replace("'", "''", $category . '/' . $filename) . "', " . $user_id . ", " . $userdata['user_id'] . ", '" . $userdata['username'] . "', '" . time() . "', '" . str_replace("'", "''", $message) . "', '" . $no_del . "')";
        if( !($result = $db->sql_query($sql) ) )
        {
            message_die(GENERAL_ERROR, 'Could not insert into present table', '', __LINE__, __FILE__, $sql);
        }
        $present_points = ($no_del == '1') ? $points_config['present_points'] : $no_del;
        message_die(GENERAL_MESSAGE, $lang['A_gift_sent'] . '<br/>' . sprintf($lang['Deducted_from_the_account'], $present_points, $points_config['points_name']) . '<br/><a href=' . append_sid("profile.$phpEx?mode=viewprofile&u=$user_id") . '>' . $lang['Return_profile'] . '</a>');
    }
        else
        {                                   
            $category = ( !empty($HTTP_POST_VARS['category_img']) ) ? htmlspecialchars($HTTP_POST_VARS['category_img']) : '';
            $dir = @opendir('images/present/');
            $images = array();
            while( $file = @readdir($dir) )
            {
                if( $file != '.' && $file != '..' && !is_file('images/present/' . $file) && !is_link('images/present/' . $file) )
                {
                    $sub_dir = @opendir('images/present/' . $file);
                    $row_count = 0;
                    $col_count = 0;
                    while( $sub_file = @readdir($sub_dir) )
                    {
                        if( preg_match('/(.gif$|.png$|.jpg|.jpeg)$/is', $sub_file) )
                        {
                            $images[$file][$row_count][$col_count] = $sub_file; 
                            $name[$file][$row_count][$col_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)..*$/', '1', $sub_file)));
                            $col_count++;
                            if( $col_count == 5 )
                            {
                               $row_count++;
                               $col_count = 0;
                            }
                        }
                    }
                 }
             }
             @closedir($dir);
             @ksort($images);
             @reset($images);
             if( empty($category) )
             {
                 list($category, ) = each($images);
             }
             @reset($images);
        
                 $page_title = 'Подарки';             
             include($phpbb_root_path . 'includes/page_header.'.$phpEx);
    
             $template->set_filenames(array(
                 'body' => 'shop/present.tpl')
             );    
    
             $s_categories = '<select name="category_img">';
             while( list($key) = each($images) )
             {
                 $selected = ( $key == $category ) ? ' selected="selected"' : '';
                 if( count($images[$key]) )
                 {
                     $s_categories .= '<option value="' . $key . '"' . $selected . '>' . ucfirst($key) . '</option>';
                 }
             }
             $s_categories .= '</select>';
             $s_colspan = 0;
             for($i = 0; $i < count($images[$category]); $i++)
             {
                 $template->assign_block_vars("images_row", array());
                 $s_colspan = max($s_colspan, count($images[$category][$i]));
                 for($j = 0; $j < count($images[$category][$i]); $j++)
                 {
                     $template->assign_block_vars('images_row.column', array(
                         "IMAGE" => 'images/present/' . $category . '/' . $images[$category][$i][$j], 
                         "NAME" => $name[$category][$i][$j],
                         "S_OPTIONS" => $images[$category][$i][$j])
                     );
                 }
             }
    
             $s_hidden_vars = '<input type="hidden" name="sid" value="' . $session_id . '" /><input type="hidden" name="category" value="' . $category . '" />';
    
             $template->assign_vars(array(
                 'L_SELECT' => $lang['Select'], 
                 'L_CATEGORY' => $lang['Select_category'], 
                 'L_FOREVER' => $lang['To_forever'],
                 'L_VALUE' => sprintf($lang['Value'], $points_config['present_points'], $points_config['points_name']),
                 'S_CATEGORY_SELECT' => $s_categories,                  
                 'S_COLSPAN' => $s_colspan,                  
                 'S_PROFILE_ACTION' => append_sid("present.$phpEx?u=$user_id&present"), 
                 'S_HIDDEN_FIELDS' => $s_hidden_vars)
             );
                 $template->pparse('body');
             include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
    }
}
*/
$start     = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
$sql = "SELECT * FROM " . PRESENTS_TABLE . "
        WHERE present_user_id = " . $profiledata['user_id'] . "
        ORDER BY donate_time DESC LIMIT " . $start . ", " . $board_config['posts_per_page'];        
if( !($result = $db->sql_query($sql)))
{
    message_die(GENERAL_ERROR, 'Could not present', '', __LINE__, __FILE__, $sql);
}
$present_rows = array();
if ($row = $db->sql_fetchrow($result))
{
     do
     {
     $present_rows[] = $row;        
     }    
     while ($row = $db->sql_fetchrow($result));
     $db->sql_freeresult($result);
}
else 
{ 
     $message = 'Пока ни кто ни чего не подарил.<br/> Сделай это первей всех!'; 
} 
$sql =  "SELECT count(*) AS total FROM " . PRESENTS_TABLE . " WHERE present_user_id = " . $profiledata['user_id'];
if($result = $db->sql_query($sql))
{
    $row = $db->sql_fetchrow($result);
    $num = $row['total'];           
}
$db->sql_freeresult($result);    
$pagination = generate_pagination("present.$phpEx?" . POST_USERS_URL . "=" . $profiledata['user_id'], $num, $board_config['posts_per_page'], $start);        
$page_title = 'Подарки "' . $profiledata['username'] . '"';
include($phpbb_root_path . 'includes/page_header.'.$phpEx); 
$template->set_filenames(array( 
        'body' => 'present_body.tpl') 
); 
$user = '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . '='  . $profiledata['user_id']) . '">' . $profiledata['username'] . '</a>';          
$template->assign_vars(array(
        'DONATE' => append_sid('profile.php?mode=money&' . POST_USERS_URL . '='  . $profiledata['user_id'] . '&present'),
        'MESSAGE' => $message,
        'USER' => $user,
    'PAGINATION' => $pagination)
);    
        
for($i = 0; $i < count($present_rows); $i++)
{    
        $time_del = mktime()-($points_config['delete_time']*86400);
        if ( $present_rows[$i]['nodel'] == '0' && $present_rows[$i]['donate_time'] < $time_del )
    {
                $present_id = $present_rows[$i]['present_id'];
                $sql = "DELETE FROM " . PRESENTS_TABLE . "
                        WHERE present_id = $present_id";
                if( !$db->sql_query($sql) )
                {
                         message_die(GENERAL_ERROR, 'Could not delete', '', __LINE__, __FILE__, $sql);
                }
    } 
        $poster_id = $present_rows[$i]['donate_user_id'];
        $message = $present_rows[$i]['message'];
        $date = create_date($board_config['default_dateformat'], $present_rows[$i]['donate_time'], $board_config['board_timezone']);
        $poster = '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . '='  . $present_rows[$i]['donate_user_id']) . '">' . $present_rows[$i]['donate_username'] . '</a>';          
        $present_img = "<img src='images/present/" . $present_rows[$i]['present_img'] . "' alt='" . $present_rows[$i]['present_img'] . "'>";
    $message = preg_replace('#(<)([/]?.*?)(>)#is', "<\2>", $message);
    $message = ($message != '') ? '<div class="row1">' . str_replace("n", "n<br />n", $message) . '</div>' : '';
    $row_class = ( !($i % 2) ) ? 'row_easy' : 'row_hard';
    $template->assign_block_vars('donaterow', array(
            'ROW_CLASS' => $row_class,
            'PRESENT_IMG' => $present_img,         
            'USER' => $poster,
            'MESSAGE' => $message,
            'DATE' => $date)
    );
}               
$template->pparse('body'); 
include($phpbb_root_path . 'includes/page_tail.'.$phpEx); 
?>