<?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 = $db->get_array($db->query("SELECT * FROM `public_wall` WHERE `id`='$id'"));
        $public = $db->get_array($db->query("SELECT * FROM `public` WHERE `id`='" . $wall['id_public'] . "'"));
        $sql = $db->super_query("SELECT COUNT(*) as count FROM `public_like` WHERE `id_user` = '$us' AND `public` = '" . $wall['id'] . "'");
        $sql = $sql['count'];

        if ($sql == '0') {
            $db->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) {
                $db->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 {
            $db->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 = $db->get_array($db->query("SELECT * FROM `public_foto` WHERE `id` = '$id'"));
        $sql = $db->super_query("SELECT COUNT(*) as count FROM `public_like` WHERE `id_user` = '$us' AND `pub_foto` = '$id'");
        $sql = $sql['count'];

        if ($sql == '0') {
            $db->query("INSERT INTO `public_like` (`id_user`, `pub_foto`, `like`) values('$us', '$id', '1')");
            $db->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 {
            $db->query("DELETE FROM `public_like` WHERE `id_user`='$us' AND `pub_foto`='$id'");
            echo json_encode(array('result' => 'dislike'));
        }
    } else {
        header("location:/");
    }
}
?>