<?
include 'config.php';
include 'func.php';
head();
aut();
ban();
////////////////////////////////////////
####################Запись онлайна##################
mysql_query("UPDATE `profile` SET `online` = 'dr' WHERE `id` = '".$id_session."'");
####################################################

$day = date('j');
$month = date('n');
$kol = mysql_result(mysql_query("SELECT count(*) FROM `profile` WHERE `d` = '".$day."' and `m` = '".$month."'"), 0);
$my = mysql_result(mysql_query("SELECT count(*) FROM `dr` WHERE `to_user` = '".$id_session."'"), 0);
$mynew = mysql_result(mysql_query("SELECT count(*) FROM `dr` WHERE `to_user` = '".$id_session."' AND `status` = '1'"), 0);
$my2 = mysql_result(mysql_query("SELECT count(*) FROM `dr` WHERE `ot_user` = '".$id_session."'"), 0);

echo head . "<b>Дни рождения:</b>" . div;   
switch($_GET['act']){
default:


if($kol!=0) {
echo link1 . '<a href="dr.php?act=list2">Меня поздравили</a> ['.$my.'] + '.$mynew.'<br/ >';
echo '<a href="dr.php?act=list">Я поздравил(а)</a> ['.$my2.']' . div;

echo link2 . 'В этот день родились:' . div;

$result = mysql_query("SELECT id, login, d, m, g, sex FROM profile WHERE d = '".$day."' AND m = '".$month."'");
while($a = mysql_fetch_assoc($result)) {
    
    echo link2 . user($a['id'])." <img src='images/dr.gif' alt=''/> (".age($a['d'], $a['m'], $a['g']) .")<br/ ><br/ >";
    echo '<a href="dr.php?act=happy&id='.$a['id'].'">Поздравить</a></div>';
}


} else {
    
    echo link1 . 'Список пуст...' . div;
}

break;

case 'happy':

$mypresent = mysql_fetch_assoc(mysql_query("SELECT * FROM `dr` WHERE `time` > '".$time."' LIMIT 1"));



$id = num($_GET['id']);
$user = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$id."'"));

    if ( empty($user['id']) ) {
        
        echo link1 . 'Пользователь не найден' . div;
        navig();
        foot();
        exit();
    }
    
    mysql_query ("Select * from profile WHERE (d = '".$day."') and (m = '".$month."') and (id = '".$id."')");
    if(mysql_affected_rows()==0) {
        
        echo link1 . 'У данного пользователя не сегодня День рождения' . div;
        navig();
        foot();
        exit();
        }
    
        if ( $user['id'] == $id_session ) {
        
        echo link1 . 'Вы не можете себя поздравить' . div;
        navig();
        foot();
        exit();
    }
    
echo link2 . '<form action="dr.php?act=1&id='.$id.'" method="post" name="form">';
echo 'Текст поздравления:<br/ ><textarea id="tarea" name="msg" cols="30" rows="5" style="width: 99%;"></textarea><br/>';
echo '<input name="submit" type="submit" class="button" value="Далее" /></form>' . div; 
break;

case '1':

$mypresent = mysql_fetch_assoc(mysql_query("SELECT * FROM `dr` WHERE `time` > '".$time."' LIMIT 1"));


$_SESSION['happy_msg'] = html($_POST['msg']);
$_SESSION['happy_user'] = num($_GET['id']);

$id = num($_SESSION['happy_user']);
$user = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$id."'"));

    if ( empty($user['id']) ) {
        
        echo link1 . 'Пользователь не найден' . div;
        navig();
        foot();
        exit();
    }
    
    mysql_query ("Select * from profile WHERE (d = '".$day."') and (m = '".$month."') and (id = '".$id."')");
    if(mysql_affected_rows()==0) {
        
        echo link1 . 'У данного пользователя не сегодня День рождения' . div;
        navig();
        foot();
        exit();
        }
    
        if ( $user['id'] == $id_session ) {
        
        echo link1 . 'Вы не можете себя поздравить' . div;
        navig();
        foot();
        exit();
    }
    
    if ( empty($_SESSION['happy_msg']) ) {
        
        echo link1 . 'Отсутствует текст поздравления' . div;
        navig();
        foot();
        exit();
    }
    
echo link1 . 'Выберите подарок' . div;

for( $i=1; $i<=21; $i++ ) {
    
    echo link2 . "<a href='dr.php?act=2&id=".$i."'><img src='dr/".$i.".png' width='40' height='40' alt=''/>". div;
    
}

break;

case '2':

$mypresent = mysql_fetch_assoc(mysql_query("SELECT * FROM `dr` WHERE `time` > '".$time."' LIMIT 1"));



$_SESSION['happy_present'] = num($_GET['id']);
$id = num($_SESSION['happy_user']);
$user = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$id."'"));

    if ( empty($user['id']) ) {
        
        echo link2 . 'Пользователь не найден' . div;
        navig();
        foot();
        exit();
    }
    
    if ( empty($_SESSION['happy_msg']) ) {
        
        echo link2 . 'Отсуствывает текст поздравления' . div;
        navig();
        foot();
        exit();
    }
    
    mysql_query ("Select * from profile WHERE (d = '".$day."') and (m = '".$month."') and (id = '".$id."')");
    if(mysql_affected_rows()==0) {
        
        echo link2 . 'У данного пользователя не сегодня День рождения' . div;
        navig();
        foot();
        exit();
        }
    
        if ( $user['id'] == $id_session ) {
        
        echo link2 . 'Вы не можете себя поздравить' . div;
        navig();
        foot();
        exit();
    }
    
echo link1 . 'Завершающий этап' . div;

echo link2 . '<img src="dr/'.num($_GET['id']).'.png" alt=""/>' . div;

echo link2 . html($_SESSION['happy_msg']) . div;

//echo link1 . 'Поздравляем пользователя: '.user(html($_SESSION['happy_user'])).'<br/ >';


echo link1 . '<a href="dr.php?act=3">Поздравить</a>' . div;

break;

case '3':
$mypresent = mysql_fetch_assoc(mysql_query("SELECT * FROM `dr` WHERE `time` > '".$time."' LIMIT 1"));



$user = num($_SESSION['happy_user']);
$msg = html($_SESSION['happy_msg']);
$present = num($_SESSION['happy_present']);

$result = mysql_query("INSERT INTO `dr` SET `msg` = '".$msg."', `to_user` = '".$user."', `ot_user` = '".$id_session."',`status` = '1', `date` = '".time()."', `present` = '".$present."', `time` = '". ( time() + 600)."'");

if ( $result != NULL )
echo link1 . 'Вы успешно поздравили '.user($user) . div;
else
echo link1 . 'Возникла ошибка' . div;

echo link2 . '<a href="dr.php">В дни рождения</a>' . div;

break;


case 'list':
function present ($str) {
    for( $i=1; $i<=21; $i++ ) {
    
    if ($str == $i) $result = "<img src='dr/".$i.".png' width='40' height='40' alt=''/>";
    
}
return $result;
    
}
echo link1 . '<b>Мои поздравления:</b>' . div;
$all = mysql_result(mysql_query("SELECT count(*) FROM `dr` WHERE `ot_user` = '".$id_session."' AND `status` = '1'"), 0);
mysql_query("UPDATE `dr` SET `status` = '0' WHERE `to_user` = '".$id_session."' AND `md` = 'present'");
if($all){
    $onpage = 5;
    $allpage = ceil($all/$onpage);
    $this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
    if($this_page < 1 || $this_page > $allpage)$this_page = 1;
    $start = $this_page*$onpage - $onpage;
    
    $result = mysql_query("SELECT * FROM `dr` WHERE `ot_user` = '".$id_session."' ORDER BY `id` DESC limit ".$start.",".$onpage."");
    while($a = mysql_fetch_assoc($result)){
        echo link2 . '<b>Поздравил(а):</b> '.user($a['ot_user']).' | '.date2($a['date']).'<br/>';
        echo '<b>Текст поздравления:</b><br/ >';
        echo ''.smile($a['msg']).'<br/>';
        echo '<b>Подарок:</b><br/>'.present($a['present']).'' . div;
    }
    echo link1 . '<b>Страницы:</b> ';
    navigation($allpage,"dr.php?p={p}",1,1);
    echo div;
} else {
    echo link2 . 'Поздравлений нет' . div;
}
break;

case 'list2':
function present ($str) {
    for( $i=1; $i<=21; $i++ ) {
    
    if ($str == $i) $result = "<img src='dr/".$i.".png' width='40' height='40' alt=''/>";
    
}
return $result;
    
}
echo link1 . '<b>Мои поздравления:</b>' . div;
$all = mysql_result(mysql_query("SELECT count(*) FROM `dr` WHERE `to_user` = '".$id_session."' AND `status` = '1'"), 0);


if($all){
    $onpage = 5;
    $allpage = ceil($all/$onpage);
    $this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
    if($this_page < 1 || $this_page > $allpage)$this_page = 1;
    $start = $this_page*$onpage - $onpage;
    
    $result = mysql_query("SELECT * FROM `dr` WHERE `to_user` = '".$id_session."' AND `status` = '1' ORDER BY `id` DESC limit ".$start.",".$onpage."");
    while($a = mysql_fetch_assoc($result)){
        echo link2 . '<b>Поздравил(а):</b> '.user($a['ot_user']).' | '.date2($a['date']).'<br/>';
        echo '<b>Текст поздравления:</b><br/ >';
        echo ''.smile($a['msg']).'<br/>';
        echo '<b>Подарок:</b><br/>'.present($a['present']).'' . div;
    }
    echo link1 . '<b>Страницы:</b> ';
    navigation($allpage,"dr.php?p={p}",1,1);
    echo div;
} else {
    echo link2 . 'Поздравлений нет' .div;
}
break;

}
/////////////////////////////////////////

navig();
foot();
?>