<?php

/* DCMS Special
 * Дата последнего редактирования 23.01.2016
 * Модифицировал densnet
 */

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user') as $inc) {
    require_once "../../sys/inc/$inc.php";
}

if (isset($_GET['public']) && $_GET['public'] == 'public') {
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' && !empty($_POST['id_post']) && !empty($_POST['us']) && isset($user)) {

        $id = intval($_POST['id_post']);
        $us = intval($_POST['us']);
        $wall = mysql_fetch_array(mysql_query("SELECT * FROM `public_wall` WHERE `id`='$id'"));
        $public = mysql_fetch_array(mysql_query("SELECT * FROM `public` WHERE `id`='" . $wall['id_public'] . "'"));
        $sql = mysql_result(mysql_query("SELECT COUNT(*) FROM `public_like` WHERE `id_user` = '$us' AND `public` = '" . $wall['id'] . "'"), 0);


        if ($sql == '0') {
            mysql_query("INSERT INTO `public_like` (`id_user`, `public`, `like`) values('$us', '" . $wall['id'] . "', '1')");
            $pmsg = '[url=/modules/public/index.php?id=' . $public['id'] . ']' . $wall['name'] . '[/url]';
            if ($public['id_user'] != $us) {
                mysql_query("INSERT INTO `public_wall` (`id_wall`, `link`, `ras`, `name`, `opis`, `id_pub`, `type`, `id_file`) values('$us', '" . $wall['link'] . "', '" . $wall['ras'] . "', '$pmsg', '" . $wall['opis'] . "', '" . $wall['id_pub'] . "', '" . $wall['type'] . "', '" . $wall['id_file'] . "')");
            }
            echo json_encode(array('result' => 'like'));
        } else {
            mysql_query("DELETE FROM `public_like` WHERE `id_user`='$us' AND `public`='" . $wall['id'] . "'");
            echo json_encode(array('result' => 'dislike'));
        }
    } else {
        header("location:/");
    }
}

if (isset($_GET['pub_foto']) && $_GET['pub_foto'] == 'pub_foto') {
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' && !empty($_POST['id_post']) && !empty($_POST['us']) && isset($user)) {

        $id = intval($_POST['id_post']);
        $us = intval($_POST['us']);
        $f = mysql_fetch_array(mysql_query("SELECT * FROM `public_foto` WHERE `id` = '$id'"));
        $sql = mysql_result(mysql_query("SELECT COUNT(*) FROM `public_like` WHERE `id_user` = '$us' AND `pub_foto` = '$id'"), 0);


        if ($sql == '0') {
            mysql_query("INSERT INTO `public_like` (`id_user`, `pub_foto`, `like`) values('$us', '$id', '1')");
            mysql_query("INSERT INTO `public_wall` (`id_wall`, `link`, `ras`, `name`, `id_ank`, `type`, `id_file`) values('$us', '/sys/public/foto/600/" . $f['id'] . ".jpg', 'jpg', '" . $f['name'] . "', '$us', 'public_foto', '$id')");
            echo json_encode(array('result' => 'like'));
        } else {
            mysql_query("DELETE FROM `public_like` WHERE `id_user`='$us' AND `pub_foto`='$id'");
            echo json_encode(array('result' => 'dislike'));
        }
    } else {
        header("location:/");
    }
}
?>