<?php
if (!defined('entry')) {
    define('entry', true);
}
/* ===========================

  Sorbet CMS - A PHP based tumblelog CMS forked from Gelato CMS

  Sorbet CMS is a free software licensed under the GPL 3.0

  =========================== */

require_once '../entry.php';
global $user, $conf, $tumble, $util;

$comment = new comments();
$template = new plantillas("admin");
$isAdmin = $user->isAdmin();
$isEdition = isset($_GET["edit"]);
$commentId = ($isEdition) ? $_GET["edit"] : null;

if (isset($_POST["btnAdd"])) {
    unset($_POST["btnAdd"]);

    $_POST["username"] = strip_tags($_POST["username"]);
    $_POST["email"] =  strip_tags($_POST["email"]);
    $_POST["web"] = strip_tags($_POST["web"]);
    $_POST["content"] = $util->removeBadTags($_POST["content"], true);

    if (isset($_POST["id_comment"])) {
        if ($isAdmin) {
            if ($comment->modifyComment($_POST, $_POST["id_comment"])) {
                header("Location: comments.php?modified=true");
                die();
            } else {
                header("Location: comments.php?modified=false");
                die();
            }
        }
    } else {
        $comment->generateCookie($_POST);
        $_POST["spam"] = ($comment->isSpam($_POST)) ? "1" : "0";
        $_POST["ip_user"] = $_SERVER["REMOTE_ADDR"];

        $strEnd=($conf->urlFriendly) ? "/" : "";
        if ($comment->addComment($_POST)) {
            header("Location: ".$conf->urlSorbet.($conf->urlFriendly?"/post/":"/index.php?post=").$_POST["id_post"].$strEnd);
            die();
        } else {
            header("Location: ".$conf->urlSorbet.($conf->urlFriendly?"/post/":"/index.php?post=").$_POST["id_post"].$strEnd);
            die();
        }
    }
}

if ($isAdmin) {
    if (isset($_GET["delete"])) {
        if ($comment->deleteComment($_GET['delete'])) {
            header("Location: comments.php?deleted=true");
            die();
        } else {
            header("Location: comments.php?deleted=false");
            die();
        }
    } ?>
	<!DOCTYPE html>
	<html>
	<head>
		<title><?php echo __("comments")?> - Sorbet CMS</title>
		<meta charset="utf-8" />
		<meta name="generator" content="Sorbet CMS <?= $util->version(); ?>" />
		<link rel="shortcut icon" href="<?php echo $conf->urlSorbet; ?>/images/favicon.ico" />
		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlSorbet; ?>/admin/scripts/jquery.js"></script>
		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlSorbet; ?>/admin/scripts/tools.js"></script>
		<script language="javascript" type="text/javascript">
		$(document).ready(function(){
			$("#divMessages").fadeOut(5000,function(){
				$("#divMessages").css({display:"none"});
			});
		});
		</script>
		<style type="text/css" media="screen">
			@import "<?php echo $conf->urlSorbet; ?>/admin/css/style.css";
		</style>
	</head>

	<body>
		<div id="div-process" style="display:none;"><?=__("Processing request&hellip;"); ?></div>
		<div id="cont">
			<div id="head">
				<h1><a href="<?php echo $conf->urlSorbet; ?>/admin/index.php" title="<?=__("home")?> - Sorbet CMS">Sorbet CMS</a></h1>
				<ul id="nav">
					<li><a href="<?php echo $conf->urlSorbet; ?>/" title="<?=__("Take me to the tumblelog")?>"><?=__("Back to the Tumblelog")?></a></li>
			  	</ul>
			</div>
			<div id="main">

				<div class="box">
					<ul class="menu manage">
						<h3><?=__("Manage comments")?></h3>
						<li><a href="index.php"><?=__("Posts")?></a></li>
						<li <?php if (isset($_GET["spam"])) {
        ?> class="selected" <?php
    } ?>><a href="comments.php?spam=true"><?=__("Spam")?></a></li>
						<li <?php if (!isset($_GET["spam"])) {
        ?> class="selected" <?php
    } ?> ><a href="comments.php"><?php echo ($isEdition) ? __("Edit") : __("List"); ?></a></li>
					</ul>
					<p>&nbsp;</p>
<?php
                    if (isset($_GET["deleted"])) {
                        if ($_GET["deleted"]=="true") {
                            echo "<div class=\"exito\" id=\"divMessages\">".__("The comment has been eliminated successfully.")."</div>";
                        }
                        if ($_GET["deleted"]=="false") {
                            echo "<div class=\"error\" id=\"divMessages\">".__("The post has NOT been eliminated.")."</div>";
                        }
                    }

    if (isset($_GET["modified"])) {
        if ($_GET["modified"]=="true") {
            echo "<div class=\"exito\" id=\"divMessages\">".__("The comment has been modified successfully.")."</div>";
        }
        if ($_GET["modified"]=="false") {
            echo "<div class=\"error\" id=\"divMessages\">".__("The post has NOT been modified.")."</div>";
        }
    } ?>
					<div class="tabla">
<?php
                        if ($isEdition) {
                            $row = $comment->getComment($_GET["edit"]);
                            $date = strtotime($row["comment_date"]);

                            $input = array("{User}", "{Email}", "{Web}", "{Comment}", "{Id_Post}", "{Date_Added}", "{Id_Comment}", "{Form_Action}");
                            $output = array($row["username"], $row["email"], $row["web"], $row["content"], $row["id_post"], $date, $row["id_comment"], $conf->urlSorbet."/admin/comments.php");

                            $template->cargarPlantilla($input, $output, "template_comment_post");
                            $template->mostrarPlantilla();
                        } else {
                            if (isset($_GET["page"]) && is_numeric($_GET["page"])) {
                                $page_num = $_GET["page"];
                            } else {
                                $page_num = null;
                            }

                            $limit=$conf->postLimit;

                            if (isset($page_num) && is_numeric($page_num) && $page_num>0) {
                                $from = (($page_num-1) * $limit);
                            } else {
                                $from = 0;
                            }

                            if (isset($_GET["spam"]) && $_GET["spam"]=="true") {
                                $sp = "1";
                            } else {
                                $sp = null;
                            }

                            $rs = $comment->getComments(null, $limit, $from, $sp);

                            if ($db->contarRegistros()>0) {
                                while ($rowComment = $rs->fetch()) {
                                    $commentAuthor = ($rowComment["web"]=="") ? $rowComment["username"]." | ".$rowComment["email"]  : "<a href=\"".$rowComment["web"]."\" rel=\"external\">".$rowComment["username"]."</a> | ".$rowComment["email"];

                                    $input = array("{Permalink}", "{URL_Tumble}", "{Id_Comment}", "{Comment_Author}", "{Comment}");
                                    $output = array($conf->urlSorbet."/index.php/post/".$rowComment["id_post"]."#comment-".$rowComment["id_comment"], $conf->urlSorbet, $rowComment["id_comment"], $commentAuthor, $rowComment["content"]);

                                    $template->cargarPlantilla($input, $output, "template_comment");
                                    $template->mostrarPlantilla();
                                }

                                $p = new pagination;
                                $p->items($comment->countComments());
                                $p->limit($limit);
                                $p->currentPage(isset($page_num) ? $page_num : 1);
                                $p->show();
                            }
                        } ?>
					</div>

					<div class="footer-box">&nbsp;</div>
				</div>
			</div>
			<div id="foot">
				Sorbet CMS - PHP Tumblelog Content Management System.
			</div>
		</div>
	</body>
	</html>
<?php

} else {
    header("Location: ".$conf->urlSorbet."/login.php");
}
