<?php

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

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

  Sorbet CMS is a free software licensed under the GPL 3.0

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

if (!defined('entry')) {
    define('entry', true);
}

// Received a valid request
require_once "entry.php";
global $user, $tumble, $conf;

$theme = new themes;

$util_class = new util();
        // Our first approach to MVC... our second? visit http://www.flavorphp.com

        if (isset($_SERVER['PATH_INFO'])) {
            $param_url = explode("/", $_SERVER['PATH_INFO']);
        }

        if (isset($_GET["post"])) {
            $id_post = $_GET["post"];
            if (!is_numeric($id_post) || $id_post < 1) {
                header("Location: index.php");
            }
        } else {
            if (isset($param_url[1]) && $param_url[1]=="post") {
                $id_post = (isset($param_url[2])) ? ((is_numeric($param_url[2])) ? $param_url[2] : null) : null;
            } else {
                $id_post = null;
            }
        }

        $theme->set('id_post', $id_post);
        $theme->set('error', '');

        if (isset($_GET["page"])) {
            $page_num = $_GET["page"];
        } else {
            if (isset($param_url[1]) && $param_url[1]=="page") {
                $page_num = (isset($param_url[2])) ? ((is_numeric($param_url[2])) ? $param_url[2] : null) : null;
            } else {
                $page_num = null;
            }
        }

        $feed_url = $conf->urlSorbet.($conf->urlFriendly?"/rss/":"/rss.php");



        $sorbet_includes = "<meta name=\"generator\" content=\"sorbet ".$util_class -> codeName()." (".$util_class -> version().")\" />\n";
        $sorbet_includes .= "\t<link rel=\"shortcut icon\" href=\"".$conf->urlSorbet."/images/favicon.ico\" />";

        $page_title = $conf->title;
        $page_title_divisor = " - "; // it should be set in configuration
        $page_title_len = 50; // it should be set in configuration
        if ($id_post) {
            $register = $tumble->getPost($id_post);
            if (empty($register["title"])) {
                if (!empty($register["description"])) {
                    $page_title_data = $util_class->trimString($register["description"], $page_title_len);
                } else {
                    $page_title_data =  $util_class->type2Text($register["type"]);
                }
            } else {
                $page_title_data = $register["title"];
            }
            if (!empty($page_title_data)) {
                $page_title .= $page_title_divisor.stripslashes($page_title_data);
            }
        }

        $trigger->call('sorbet_includes');
        $theme->set('rssFeed', $feed_url);
        $theme->set('Sorbet_includes', $sorbet_includes);
        $theme->set('Title', $conf->title);
        $theme->set('Page_Title', $page_title);
        $theme->set('Description', $conf->description);
        $theme->set('URL_Tumble', $conf->urlSorbet);
        $theme->set('Template_name', $conf->template);
        $theme->set('allowComments', $conf->allowComments);

        $theme->set('isAuthenticated', $user->isAuthenticated());
        if ($user->isAuthenticated()) {
            $theme->set('User', $_SESSION["user_login"]);
            $theme->set('URL_Tumble', $conf->urlSorbet);
        }

        $rows = array();
        if (!$id_post) {
            $limit=$conf->postLimit;

            if (isset($page_num) && is_numeric($page_num) && $page_num>0) { // Is defined the page and is numeric?
                    $from = (($page_num-1) * $limit);
            } else {
                $from = 0;
            }

            $rs = $tumble->getPosts($limit, $from);

            if ($db->contarRegistros()>0) {
                $dateTmp = null;
                while ($register = $rs->fetch()) {
                    $formatedDate = gmdate("M d Y", strtotime($register["date"]) + $util_class->transform_offset($conf->offsetTime));
                    if ($dateTmp != null && $formatedDate == $dateTmp) {
                        $formatedDate = "";
                    } else {
                        $dateTmp = $formatedDate;
                    }

                    $permalink = $tumble->getPermalink($register["id_post"]);

                    $conversation = $register["description"];

                    $register["title"] = stripslashes($register["title"]);
                    $register["description"] = stripslashes($register["description"]);

                    $row['Date_Added'] = $formatedDate;
                    $row['Permalink'] = $permalink;
                    $row['postType'] = $util_class->type2Text($register["type"]);

                    switch ($register['type']) {
                                        case "1":
                                            $row['Title'] = $register["title"];
                                            $row['Body'] = $register["description"];
                                            break;
                                            case "2":
                                            $fileName = "uploads/".$util_class->getFileName($register["url"]);
                                            $x = @getimagesize($fileName);
                                                if ($x[0] > 500) {
                                                    $photoPath = $conf->urlSorbet."/classes/imgsize.php?w=500&img=".$register["url"];
                                                } else {
                                                    $photoPath = str_replace("../", $conf->urlSorbet."/", $register["url"]);
                                                }

                                                $effect = " href=\"".str_replace("../", $conf->urlSorbet."/", $register["url"])."\" rel=\"lightbox\"";

                                                $row['PhotoURL'] = $photoPath;
                                                $row['PhotoAlt'] = strip_tags($register["description"]);
                                                $row['Caption'] = $register["description"];
                                                $row['Effect'] = $effect;
                                                break;
                                        case "3":
                                                $row['Quote'] = $register["description"];
                                                $row['Source'] = $register["title"];
                                                break;
                                        case "4":
                                                if ($conf->shorten_links) {
                                                    $register["url"] = $util_class->_file_get_contents("http://api.abbrr.com/api.php?out=link&url=".$register["url"]);
                                                }
                                                $register["title"] = ($register["title"]=="")? $register["url"] : $register["title"];

                                                $row['URL'] = $register["url"];
                                                $row['Name'] = $register["title"];
                                                $row['Description'] = $register["description"];
                                                break;
                                                case "5":
                                                $row['Title'] = $register["title"];
                                                $row['Conversation'] = $tumble->formatConversation($conversation);
                                                break;
                                        case "6":
                                                $row['Video'] = $tumble->getVideoPlayer($register["url"]);
                                                $row['Caption'] = $register["description"];
                                                break;
                                        case "7":
                                                $row['Mp3'] = $tumble->getMp3Player($register["url"]);
                                                $row['Caption'] = $register["description"];
                                                break;
                                }

                    $comment = new comments();
                    $noComments = $comment->countComments($register["id_post"]);

                    $user = new user();
                    $username = $user->getUserByID($register["id_user"]);

                    $row['User'] = $username["name"];
                    $row['Comments_Number'] = $noComments;

                    $rows[] = $row;
                }

                $trigger->call('post_content');
                $theme->set('rows', $rows);

                $p = new pagination;
                $p->Items($tumble->getPostsNumber());
                $p->limit($limit);
                if ($conf->urlFriendly) {
                    $p->urlFriendly('[...]');
                    $p->target($conf->urlSorbet."/page/[...]");
                } else {
                    $p->target($conf->urlSorbet);
                }

                $p->currentPage(isset($page_num) ? $page_num : 1);
                $theme->set('pagination', $p->getPagination());
            } else {
                $theme->set('error', 'No posts in this tumblelog.');
            }
        } else {
            $register = $tumble->getPost($id_post);

            $formatedDate = gmdate("M d Y", strtotime($register["date"]) + $util_class->transform_offset($conf->offsetTime));
            $permalink = $tumble->getPermalink($register["id_post"]);

            $conversation = $register["description"];

            $register["description"] = $register["description"];

            $register["title"] = stripslashes($register["title"]);
            $register["description"] = stripslashes($register["description"]);

            $row['Date_Added'] = $formatedDate;
            $row['Permalink'] = $permalink;
            $row['postType'] = $util_class->type2Text($register["type"]);

            switch ($register['type']) {
                        case "1":
                                $row['Title'] = $register["title"];
                                $row['Body'] = $register["description"];
                                break;
                        case "2":
                                $fileName = "uploads/".$util_class->getFileName($register["url"]);

                                $x = @getimagesize($fileName);
                                if ($x[0] > 500) {
                                    $photoPath = $conf->urlSorbet."/classes/imgsize.php?w=500&img=".$register["url"];
                                } else {
                                    $photoPath = str_replace("../", $conf->urlSorbet."/", $register["url"]);
                                }

                                $effect = " href=\"".str_replace("../", $conf->urlSorbet."/", $register["url"])."\" rel=\"lightbox\"";

                                $row['PhotoURL'] = $photoPath;
                                $row['PhotoAlt'] = strip_tags($register["description"]);
                                $row['Caption'] = $register["description"];
                                $row['Effect'] = $effect;
                                break;
                        case "3":
                                $row['Quote'] = $register["description"];
                                $row['Source'] = $register["title"];
                                break;
                        case "4":
                            if ($conf->shorten_links) {
                                $register["url"] = $util_class->_file_get_contents("http://api.abbrr.com/api.php?out=link&url=".$register["url"]);
                            }
                                $row['URL'] = $register["url"];
                                $row['Name'] = $register["title"];
                                $row['Description'] = $register["description"];
                                break;
                        case "5":
                                $row['Title'] = $register["title"];
                                $row['Conversation'] = $tumble->formatConversation($conversation);
                                break;
                        case "6":
                                $row['Video'] = $tumble->getVideoPlayer($register["url"]);
                                $row['Caption'] = $register["description"];
                                break;
                        case "7":
                                $row['Mp3'] = $tumble->getMp3Player($register["url"]);
                                $row['Caption'] = $register["description"];
                                break;
                }

            $user = new user();
            $username = $user->getUserByID($register["id_user"]);

            $row["User"] = $username["name"];

            if (empty($register["title"])) {
                if (!empty($register["description"])) {
                    $postTitle = $util_class->trimString($register["description"]);
                } else {
                    $postTitle =  $util_class->type2Text($register["type"]);
                }
            } else {
                $postTitle = $register["title"];
            }

            $row["Post_Title"] = $postTitle;

            $comment = new comments();
            $row["Comments_Number"] = $comment->countComments($register["id_post"]);

            if ($conf->allowComments) {
                $rsComments = $comment->getComments($register["id_post"]);
                $comments = array();
                while ($rowComment = $rsComments->fetch()) {
                    $commentAuthor = ($rowComment["web"]=="") ? $rowComment["username"] : "<a href=\"".$rowComment["web"]."\" rel=\"external\">".$rowComment["username"]."</a>";

                    $answers['Id_Comment'] = $rowComment["id_comment"];
                    $answers['Comment_Author'] = $commentAuthor;
                    $answers['Date'] = gmdate("d.m.y", strtotime($rowComment["comment_date"]) + $util_class->transform_offset($conf->offsetTime));
                    $answers['Comment'] = nl2br($rowComment["content"]);

                    $comments[] = $answers;
                }
                $theme->set('comments', $comments);

                $whois['User_Cookie'] = isset($_COOKIE['cookie_sor_user'])?$_COOKIE['cookie_sor_user']:'';
                $whois['Email_Cookie'] = isset($_COOKIE['cookie_sor_email'])?$_COOKIE['cookie_sor_email']:'';
                $whois['Web_Cookie'] = isset($_COOKIE['cookie_sor_web'])?$_COOKIE['cookie_sor_web']:'';
                $whois['Id_Post'] = $register["id_post"];

                $theme->set('Date_Added', time());
                $theme->set('Form_Action', $conf->urlSorbet."/admin/comments.php");
                $theme->set('whois', $whois);
            }

            $rows[] = $row;

            $trigger->call('post_content');
            $theme->set('rows', $rows);
        }

        $theme->set('URL_Tumble', $conf->urlSorbet);
        $theme->display(Absolute_Path.'themes/'.$conf->template.'/index.htm');
