<?
include 'config.php';
include 'func.php';
head();
aut();
ban();
####################Запись онлайна##################
mysql_query("UPDATE `profile` SET `online` = 'friends' WHERE `id` = '".$id_session."'");
####################################################
echo head . "Мои друзья" . div; 
switch ( $_GET['mod'] ) {
    
    default:
             if ( $_GET['msg'] == 'add_yes') {
        echo link1 . 'Вы успешно добавили друга' . div;
    }
    
    if ( $_GET['msg'] == 'add_error' ) {
        echo link1 . 'Возникла ошибка' . div;
    }
    
    if ( $_GET['msg'] == 'delete_yes' ) {
        echo link1 . 'Вы успешно удалили друга' . div;
    }
    if ( $_GET['msg'] == 'delete_error' ) {
        echo link1 . 'Ошибка при удалении друга' . div;
    }    
    echo ' <div class="link2"><a href="friends.php?mod=search">Добавить друга</a><br/ >';
    echo ' <a href="friends.php?mod=friends">Мои друзья</a><br/ >';
    echo ' <a href="friends.php?mod=online">Мои друзья</a> [Сейчас на сайте]<br/ >';
    echo ' <a href="friends.php?mod=friends2">Я в друзьях</a>';
    break;
    
    
    
    
    case 'search':
    if (isset($_POST['submit'])) {
        
        $id = html($_POST['id']);
        
        
        
        
        if ( empty($id) ) {
            
            echo 'Вы ничего не ввели<br/ >';
            
        } else {
        
        
        
        if ( is_numeric($id) ) 
            $result = mysql_query("SELECT * FROM `profile` WHERE `id` = '".$id."';");
            else
            $result = mysql_query("SELECT * FROM `profile` WHERE `login` LIKE '%".html(urldecode($id))."%';");
            $total = mysql_result(mysql_query('SELECT FOUND_ROWS()'), 0);
            if ( $total == 0 ) {
                echo 'Совпадений не найдено<br/ >';
            
            } else { 
            
            
            while($res = mysql_fetch_assoc($result)) {
                
                    mysql_query ("Select * from friends WHERE (user_1 = '".$id_session."') and (user_2 = '".$res['id']."')");
                    if(mysql_affected_rows()!=0) $status = '[в друзьях]'; else $status;
                echo '<div class="link1"> '.user($res['id']).' [<a href="friends.php?mod=add&user='.$res['id'].'">Добавить</a>] '.$status.'</div>';
                
            }
            
            
            }
            
            }
        
        
    }
    echo div . katalog;
    echo 'Введите ID или логин:<br/ >';
    echo "<form method=\"post\" action=\"friends.php?mod=search\">";
    echo "<input name=\"id\" maxlength=\"25\"/>";
    echo "<input name=\"submit\" type=\"submit\" class=\"button\"  value=\"Поиск\"/></form>" . div;    
    
    break;
    
    
    case 'add':
    
    $id = num($_GET['user']);
    
    $user = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$id."'"));
    
    if ( empty($user['id']) ) {
        
        echo link2 . 'Пользователь не найден' . div;
        navig();
        foot();
        exit();
    }
    
    if ( $user['id'] == $id_session ) {
        
        echo link2 . 'Вы не можете себя добавить' . div;
        navig();
        foot();
        exit();
    }  
    
      
    mysql_query ("Select * from friends WHERE (user_1 = '".$id_session."') and (user_2 = '".$user['id']."')");
    if(mysql_affected_rows()!=0) {
        echo link2 . 'Пользователь '.$user['login'].' уже есть у вас в друзьях' . div;
        navig();
        foot();
        exit();
    } 
    
    if ($user['private_friend'] == 1 AND $user['id'] != $id_session) $err = 'Вы не можете добавить в друзья пользователя ' . $usinf['login'];

if ($err) { 
        echo link1 . 'Ошибка, ' . $err . div;
    }else {   
    
    $result = mysql_query("INSERT INTO `friends` SET  `user_1` = '".$id_session."', `user_2` = '".$user['id']."', `date` = '".time()."'");

    $msg = 'Пользователь <b>'.$login_session.'</b> добавил(а) вас в друзья.';
    mysql_query("INSERT INTO `uvedom` SET  `user_to` = '".$user['id']."', `user_ot` = '".$id_session."', `msg` = '".$msg."', `date` = '".time()."', `md` = 'friend', `status` = '1'");
    $msg1 = '<b>'.user($id_session).'</b> добавил в друзья <b>'.user($user['id']).'</b>';
    mysql_query("INSERT INTO `jurnal` SET  `msg` = '".$msg1."', `user` = '".$id_session."', `date` = '".time()."'");
    }
    
    if ( $result != NULL )
        echo rew('friends.php?msg=add_yes');
        else
        echo rew('friends.php?msg=add_error');
        
    
    break;
    
    case 'friends':

    
    $id = num($_GET['id']);
    
    if ( empty($id) ) 
    $us = $id_session;
    else
    $us = $id;
    
    $usinfo = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$us."'"));
    
    if ($usinfo['private_friends'] == 1 AND $usinfo['id'] != $id_session) $err = 'Вы не можете просматривать список друзей пользователя' . $usinf['login'];
    
    if ($us['private_friends'] == 2 OR $user['id'] != $id_session) {
    $sql = mysql_query("SELECT * FROM `friends` WHERE `user_1` = $usinfo[id] AND `user_2` = $id_session;");
    if(mysql_num_rows($sql) ==0) $err = 'Пользователю '.$usinfo['login'].' могут писать только его/ее друзья.';
    }
    
    if ($err) {
        echo link1 . $err . div;
    } else {
    
    $all = mysql_result(mysql_query("SELECT count(*) FROM `friends` WHERE `user_1` = '".$us."'"), 0);
    
    if ( $all > 0 ) {
    
    $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 `friends` WHERE `user_1` = '".$us."' order by id desc limit ".num($start).",".num($onpage)."");
    while($res = mysql_fetch_assoc($result)) {
        $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$res['user_2']."'"));
        
        echo link2;
        if($us['upload_ava']){
            $ava = '<img src="resize.php?img=upload_ava/'.$us['upload_ava'].'&width=80&height=0" alt=""/>';
        } else {
            $ava = '<img src="resize.php?img=noava.png&width=80&height=0" alt=""/>';
            }
        echo $ava . '<br/ >';
        
        if ($us['time'] > (time()-600))
        echo 'Online<br/ >';
        else
        echo '';
        echo user($res['user_2']);
         mysql_query ("Select * from friends WHERE (user1 = '".$id_session."') and (user_2 = '".$us['id']."')");
    if(mysql_affected_rows()!=0) 
        echo  '<br/><br/><a href="friends.php?mod=delete&id='.$res['user_2'].'">Удалить из друзей</a></div>';
        else
        echo '</div>';
    }
echo link1 . '<b>Страницы:</b> ';
if ( empty($id) ) 
navigation($allpage,"friends.php?mod=friends&p={p}",1,1);
else
navigation($allpage,"friends.php?mod=friends&id=$id&p={p}",1,1);
echo div;


    } else {
        
        echo 'Список друзей пуст';
        
    }
    
    }
    
    break;
    
case 'friends2':
    

    $id = num($_GET['id']);
    
    if ( empty($id) ) 
    $us = $id_session;
    else
    $us = $id;
    $all = mysql_result(mysql_query("SELECT count(*) FROM `friends` WHERE `user_2` = '".$id_session."'"), 0);
    
    if ( $all > 0 ) {
    
    $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 `friends` WHERE `user_2` = '".$us."' order by id desc limit ".num($start).",".num($onpage)."");
    while($res = mysql_fetch_assoc($result)) {
        $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$res['user_1']."'"));
        
        if (!empty($us['id'])) {
        if($us['upload_ava']){
            $ava = '<div class="katalog"><img src="resize.php?img=upload_ava/'.$us['upload_ava'].'&width=80&height=0" alt=""/>';
        } else {
            $ava = '<img src="resize.php?img=noava.png&width=80&height=0" alt=""/>';
            }
        echo $ava . '<br/ >';
        
        if ($us['time'] > (time()-600))
        echo 'Online<br/ >';
        else
        echo '';
        echo user($res['user_1']) .' [<a href="friends.php?mod=delete&id='.$res['user_2'].'">x</a>]</div>';
        }
    }
echo link1 . '<b>Страницы:</b> ';
if ( empty($id) ) 
navigation($allpage,"friends.php?mod=friends2&p={p}",1,1);
else
navigation($allpage,"friends.php?mod=friends2&id=$id&p={p}",1,1);
echo div;


    } else {
        
        echo 'Список друзей пуст';
        
    }
    
    
    
    break;
    
    
       case 'delete':
    $id = num($_GET['id']);
    $user = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$id."'"));

    
        mysql_query ("Select * from friends WHERE (user1 = '".$id_session."') and (user_2 = '".$user['id']."')");
    if(mysql_affected_rows()==0) {
        echo link2 . 'Пользователь не состоит у вас в друзьях' . div;
        navig();
        foot();
        exit();
    }   
    if (mysql_query("DELETE FROM friends WHERE (user_1 = '".$id_session."') and (user_2 = '".$user['id']."')"))
        echo rew('friends.php?msg=delete_yes');
        else
        echo rew('friends.php?msg=delete_error');
    
    break;
    
    case 'online':

    
    $id = num($_GET['id']);
    
    if ( empty($id) ) 
    $us = $id_session;
    else
    $us = $id;
    $all = mysql_result(mysql_query("SELECT count(*) FROM `friends` WHERE `user_1` = '".$id_session."'"), 0);
    


echo $k_post;                        
    if ( $all > 0 ) {
    
    
    $result = mysql_query("SELECT DISTINCT `friends`.* FROM `friends`

                        LEFT JOIN `profile` 

                        ON

                        (`friends`.`user_2` = `profile`.`id`)

                        WHERE

                        `friends`.`user_1` = '$us'

                        AND

                        `profile`.`time` > '" . (time()-600) . "'

                        ORDER BY `profile`.`time`");

    while($res = mysql_fetch_assoc($result)) {
        $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$res['user_2']."'"));
        
        
        if($us['upload_ava']){
            $ava = '<div class="katalog"><img src="resize.php?img=upload_ava/'.$us['upload_ava'].'&width=80&height=0" alt=""/>';
        } else {
            $ava = '<img src="resize.php?img=noava.png&width=80&height=0" alt=""/>';
            }
        echo $ava . '<br/ >';
        
        if ($us['time'] > (time()-600))
        echo 'Online<br/ >';
        else
        echo '';
        echo user($res['user_2']) .' [<a href="friends.php?mod=delete&id='.$res['user_2'].'">x</a>]</div>';
        
    }




    } else {
        
        echo 'Список друзей пуст';
        
    }
    
    break;
    
    case 'usfriends':
        $id = num($_GET['id']);
    
    if ( empty($id) ) 
    $us = $id_session;
    else
    $us = $id;
    $all = mysql_result(mysql_query("SELECT count(*) FROM `friends` WHERE `user_2` = '".$id_session."'"), 0);
    
    if ( $all > 0 ) {
    
    $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 `friends` WHERE `user_2` = '".$us."' order by id desc limit ".num($start).",".num($onpage)."");
    while($res = mysql_fetch_assoc($result)) {
        $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$res['user_1']."'"));
        
        if($us['upload_ava']){
            $ava = '<div class="katalog"><img src="resize.php?img=upload_ava/'.$us['upload_ava'].'&width=80&height=0" alt=""/>';
        } else {
            $ava = '<img src="resize.php?img=noava.png&width=80&height=0" alt=""/>';
            }
        echo $ava . '<br/ >';
        
        if ($us['time'] > (time()-600))
        echo 'Online<br/ >';
        else
        echo '';
        echo user($res['user_1']) .' [<a href="friends.php?mod=delete&id='.$res['user_2'].'">x</a>]</div>';
    }
        echo link1 . '<b>Страницы:</b> ';
        if ( empty($id) ) 
        navigation($allpage,"friends.php?mod=friends2&p={p}",1,1);
        else
        navigation($allpage,"stena.php?mod=friends2&id=$id&p={p}",1,1);
        echo div;


    } else {
        
        echo 'Список друзей пуст';
        
    }
    break;
    
}


navig();
foot();
?>