<?php
/**
 * This file is part of
 * pragmaMx - Web Content Management System.
 * Copyright by pragmaMx Developer Team - http://www.pragmamx.org
 *
 * pragmaMx is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * $Revision: 1.23.2.2.2.6 $
 * $Author: tora60 $
 * $Date: 2009/03/23 13:20:16 $
 *
 * this file based on:
 * php-nuke Web Portal System - http://phpnuke.org/
 * Thatware - http://thatware.org/
 */

/**
 * rechte Bloecke anzeigen?
 * 0 = nein, 1 = ja
 */
$index = 0;
$keinfuss = 1;

defined('mxMainFileLoaded') or die('access denied');

$module_name = basename(dirname(__FILE__));
mxGetLangfile($module_name);

function showpage()
{
    global $prefix, $sitename, $module_name, $pagetitle;
    $pid = (empty($_REQUEST['pid'])) ? 0 : intval($_REQUEST['pid']);
    $result = sql_query("SELECT * FROM ${prefix}_pages WHERE pid=" . intval($pid));
    $mypage = sql_fetch_assoc($result);
    if (!$mypage['active'] && !MX_IS_ADMIN) {
        mxErrorScreen("Sorry... This page doesn't exist.");
    }
    if ($mypage['active']) {
        $adminlink = (MX_IS_ADMIN) ? "<p>[&nbsp;<a href=\"admin.php?op=content_edit&amp;pid=$pid\">" . _EDIT . "</a> | <a href=\"admin.php?op=content_change_status&amp;pid=$pid&amp;active=1\">" . _DEACTIVATE . "</a> | <a href=\"admin.php?op=content_delete&amp;pid=$pid\">" . _DELETE . "</a>&nbsp;]</p>" : "";
    } else {
        $adminlink = (MX_IS_ADMIN) ? "<p>[&nbsp;<a href=\"admin.php?op=content_edit&amp;pid=$pid\">" . _EDIT . "</a> | <a href=\"admin.php?op=content_change_status&amp;pid=$pid&amp;active=0\">" . _ACTIVATE . "</a> | <a href=\"admin.php?op=content_delete&amp;pid=$pid\">" . _DELETE . "</a>&nbsp;]</p>" : "";
    }
    $date = formatTimestamp($mypage['date']); # explode(" ", $mypage['date']);
    $page = (empty($_REQUEST['page']) || $_REQUEST['page'] < 1) ? 1 : intval($_REQUEST['page']);
    $contentpages = explode("<!--pagebreak-->", $mypage['text']);
    $content_toview = '';
    $pageno = 0;
    foreach($contentpages as $i => $content) {
        $content = trim($content);
        if (empty($content)) {
            continue;
        }
        $pageno++;
        if ($page >= $pageno) {
            $content_toview = $content;
        }
    }
    if ($pageno && $page > $pageno) {
        $page = $pageno;
    }
    if ($page == 1) {
        sql_query("UPDATE ${prefix}_pages SET counter=counter+1 WHERE pid=" . intval($pid));
    }

    $pager = '';
    $start = '';
    $ende = '';
    $counter = 1;
    $link = 'modules.php?name=' . $module_name . '&amp;pid=' . $pid . '&amp;page=%d';
    if ($page > 3 && $pageno > 6) {
        $start = '<a href="' . sprintf($link, 1) . '" title="' . _GOTOPAGEFIRST . '">1&nbsp;<span style="font-size: 120%;">&laquo;</span></a>&nbsp;..&nbsp;';
        if ($page >= $pageno - 3) {
            $counter = $page - ($page - $pageno + 4);
        } else {
            $counter = $page - 1;
        }
    }
    // Schleife durch Seitenzahlen
    while ($counter <= $pageno) {
        if (($counter > 5) && ($counter > $page + 2) && ($counter < $pageno) && $pageno > 6) {
            $ende = '&nbsp;..&nbsp;<a href="' . sprintf($link, $pageno) . '" title="' . _GOTOPAGELAST . '"><span style="font-size: 120%;">&raquo;</span>&nbsp;' . $pageno . ' </a>';
            break;
        } else if ($counter == $page) {
            $part[] = '<span class="current" title="' . _PAGE . '&nbsp;' . $page . ' ' . _OF . ' ' . $pageno . '">' . $counter . '</span>';
        } else {
            $part[] = '<a href="' . sprintf($link, $counter) . '" title="' . _GOTOPAGE . ' ' . $counter . '">' . $counter . '</a>';
        }
        $counter++;
    }
    if (isset($part)) {
        $pager = '
        <span class="counter">' . _PAGE . '&nbsp;' . $page . '&nbsp;' . _OF . '&nbsp;' . $pageno . '
        </span>' . $start . implode($part) . $ende . '
        ';
    }

    $pagetitle = $mypage['title'] . (($page < 2) ? '' : ', ' . _PAGE . ' ' . $page);
    include("header.php");
    OpenTable();
    echo '<h2 class="title">' . $mypage['title'] . '</h2>';
    if ($mypage['subtitle']) {
        echo '<h3 class="option">' . $mypage['subtitle'] . '</h3>';
    }
    if ($pageno > 1) {
        echo '<div class="pagination align-right">' . $pager . '</div>';
    }
    if ($page == 1) {
        echo '<div class="content">' . $mypage['page_header'] . '</div>';
    }
    echo '<div class="content">' . $content_toview . '</div>';
    if ($page == $pageno) {
        echo '<div class="content">' . $mypage['page_footer'] . '</div>';
    }
    if ($pageno > 1) {
        echo '<div class="pagination align-right">' . $pager . "</div>";
    }

    if ($page == $pageno) {
        if ($mypage['signature']) echo "<div align=\"right\">" . $mypage['signature'] . "</div>";
	if ($keinfuss == 1) {
        echo "<p align=\"right\"><font class=\"tiny\">
        " . _PUBLISHEDON . ": $date ($mypage[counter] " . _READS . ")
        <br />
        " . _COPYRIGHT . " $sitename
        </font></p>
        ";
	}
        echo "
        <p align=\"center\">
        <a href=\"modules.php?name=$module_name&amp;pa=printpage&amp;pid=$pid\">" . mxCreateImage('modules/' . $module_name . '/images/print.gif', _PRINTER) . "</a>";
        if (mxModuleAllowed('Recommend_Us')) {
            echo "&nbsp;&nbsp;<a href=\"modules.php?name=" . $module_name . "&amp;file=friend&amp;pid=$pid\" rel=\"nofollow\">" . mxCreateImage('modules/' . $module_name . '/images/friend.gif', _FRIEND) . "</a>";
        }
        echo '</p>';
    }
    $page_back = ($mypage['cid'] > 0) ? '&amp;pa=list_pages_categories&amp;cid=' . $mypage['cid'] . '" title="' . _CONT_GOBACKCAT : '" title="' . _CONT_GOBACKMAIN;
    echo '<p align="center">[ <a href="modules.php?name=Content' . $page_back . '"> ' . _CONT_GOBACK . '</a> ]<br /><br /></p>';
    echo $adminlink;
    CloseTable();
    include("footer.php");
}

/**
 * Ab hier Aenderungen fuer Contentdruck
 */
function PrintContPage()
{
    global $site_logo, $sitename, $datetime, $prefix, $module_name;
    $pid = (empty($_REQUEST['pid'])) ? 0 : intval($_REQUEST['pid']);

    if (!MX_IS_ADMIN && (empty($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], PMX_HOME_URL) === false)) {
        $target = PMX_HOME_URL . '/modules.php?name=' . $module_name . '&pid=' . $pid;
        @header('HTTP/1.1 301 Moved Permanently');
        @header('Status: 301 Moved Permanently');
        @header('Location: ' . $target);
    }

    $result = sql_query("SELECT title, text, subtitle, page_header, page_footer FROM ${prefix}_pages WHERE pid=" . intval($pid));
    list($title, $text, $subtitle, $page_header, $page_footer) = sql_fetch_row($result);
    /**
     * Sicherstellen, dass Seitentitel und Untertitel keine Tags enthalten und Sonderzeichen nicht zerstueckelt werden
     */
    $title = strip_tags(str_replace('&nbsp;', ' ', $title));
    $subtitle = strip_tags(str_replace('&nbsp;', ' ', $subtitle));
    echo "
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
	\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html><head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" . _CHARSET . "\" />
<meta http-equiv=\"content-language\" content=\"" . _DOC_LANGUAGE . "\" />
<meta name=\"robots\" content=\"NOINDEX, NOFOLLOW\" />
<title>" . $title . "</title>
<link rel=\"stylesheet\" type=\"text/css\" href=\"layout/style/style.css.php?t=" . MX_THEME . "\" />
</head>
<body class=\"allone\">
<center><table border=\"0\"><tr><td>
<table border=\"0\" width=\"640\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#000000\"><tr><td>
<table border=\"0\" width=\"640\" cellpadding=\"20\" cellspacing=\"1\" bgcolor=\"#FFFFFF\"><tr><td>
<center>";
    if (file_exists("images/$site_logo"))
        echo mxCreateImage('images/' . $site_logo, $sitename) . '<br /><br />';
    if ($title)
        echo "<div class=\"content\"><b>$title</b></div><br />";
    echo "</center>";
    if ($subtitle)
        echo "<div class=\"content\">$subtitle</div><br />";
    if ($page_header)
        echo "<div class=\"content\">$page_header</div><br />";
    if ($text)
        echo "<div class=\"content\">$text</div><br />";
    if ($page_footer)
        echo "<div class=\"content\">$page_footer</div><br /><br /><center><p>" . _COPYRIGHT . " $sitename </p></center>";
    echo "</td></tr></table></td></tr></table>
<br /><br /><center>
<font class=\"content\">
" . _COMESFROM . " <a href=\"" . PMX_HOME_URL . "/\">$sitename</a><br /><br />
" . _THEURL . "<br />
<a href=\"" . PMX_HOME_URL . "/modules.php?name=" . $module_name . "&amp;pid=$pid\">" . PMX_HOME_URL . "/modules.php?name=" . $module_name . "&amp;pid=$pid</a>
</font></center>
</td></tr></table></center>
</body>
</html>
";
}

function list_pages()
{
    global $prefix, $sitename, $multilingual, $module_name, $pagetitle;
    $qry = "SELECT c.cid, c.title, c.description, Count(p.pid) AS pcount
            FROM ${prefix}_pages_categories AS c LEFT JOIN ${prefix}_pages AS p ON c.cid = p.cid
            WHERE (((p.active)=1))
            GROUP BY c.cid, c.title, c.description
            HAVING (((Count(p.pid))>0))
            ORDER BY c.title";
    $result = sql_query($qry);
    while (list($cid, $title, $description, $pcount) = sql_fetch_row($result)) {
        $lines[] = "<tr valign=\"top\"><td><a href=\"modules.php?name=" . $module_name . "&amp;cid=" . $cid . "\">" . strip_tags($title) . "</a></td><td align=\"left\">" . strip_tags($description) . "</td><td align=\"center\">(" . $pcount . ")</td></tr>";
    }
    $result = sql_query("SELECT pid, title, subtitle, clanguage FROM ${prefix}_pages WHERE active=1 AND cid < 1 ORDER BY date DESC");
    while (list($pid, $title, $subtitle, $clanguage) = sql_fetch_row($result)) {
        $the_lang = ($multilingual) ? mxContentLanguageFlag($clanguage) : "<strong><big>&middot;</big></strong>";
        $subtitle = (empty($subtitle)) ? "" : "<br /><span class=\"tiny\">(" . strip_tags($subtitle) . ")</span>";
        $adminlink = (MX_IS_ADMIN) ? "[&nbsp;<a href=\"admin.php?op=content_edit&amp;pid=$pid\">" . _EDIT . "</a> | <a href=\"admin.php?op=content_change_status&amp;pid=$pid&amp;active=1\">" . _DEACTIVATE . "</a> | <a href=\"admin.php?op=content_delete&amp;pid=$pid\">" . _DELETE . "</a>&nbsp;]" : "&nbsp;";
        $lines2[] = "<tr valign=\"top\"><td>$the_lang</td><td><a href=\"modules.php?name=$module_name&amp;pid=$pid\">" . strip_tags($title) . "</a>" . $subtitle . "</td><td align=\"right\">" . $adminlink . "</td></tr>";
    }
    if (MX_IS_ADMIN) {
        $result = sql_query("SELECT pid, cid, title, subtitle, clanguage FROM ${prefix}_pages WHERE active='0' ORDER BY date DESC");
        while (list($pid, $cid, $title, $subtitle, $clanguage) = sql_fetch_row($result)) {
            $the_lang = ($multilingual) ? mxContentLanguageFlag($clanguage) : "<strong><big>&middot;</big></strong>";
            $subtitle = (empty($subtitle)) ? "" : "<br /><span class=\"tiny\">(" . strip_tags($subtitle) . ")</span>";
            $adminlink = "[&nbsp;<a href=\"admin.php?op=content_edit&amp;pid=$pid\">" . _EDIT . "</a> | <a href=\"admin.php?op=content_change_status&amp;pid=$pid&amp;active=0\">" . _ACTIVATE . "</a> | <a href=\"admin.php?op=content_delete&amp;pid=$pid\">" . _DELETE . "</a>&nbsp;]";
            $lines3[] = "<tr valign=\"top\"><td>$the_lang</td><td><a href=\"modules.php?name=$module_name&amp;pid=$pid\">" . strip_tags($title) . "</a>" . $subtitle . "</td><td align=\"right\">" . $adminlink . "</td></tr>";
        }
    }
    $xlist = 0;
    $pagetitle = _CONTENTCATEGORIES;
    include("header.php");
    if (isset($lines)) {
        OpenTable();
        echo "<center><div class=\"title\">" . _CONTENTCATEGORIES . "</div><br /><br />";
        echo "<table border=\"0\" align=\"center\" width=\"95%\">";
        echo implode("\n", $lines);
        echo "</table></center>";
        CloseTable();
        echo "<br />";
        $xlist++;
    }
    if (isset($lines2)) {
        OpenTable();
        echo "<center><div class=\"title\">" . _NONCLASSCONT . "</div><br /><br />";
        echo "<table border=\"0\" align=\"center\" width=\"95%\">";
        echo implode("\n", $lines2);
        echo "</table></center>";
        CloseTable();
        echo "<br />";
        $xlist++;
    }
    if (isset($lines3)) {
        OpenTable();
        echo "<center><div class=\"title\">" . _YOURADMINLIST . "</div><br /><br />";
        echo "<table border=\"0\" align=\"center\" width=\"95%\">";
        echo implode("\n", $lines3);
        echo "</table></center>";
        CloseTable();
        echo "<br />";
        $xlist++;
    }
    if (empty($xlist)) {
        title(_NOCONTENT);
    }
    include("footer.php");
}

function list_pages_categories()
{
    global $prefix, $sitename, $multilingual, $module_name, $pagetitle;

    $cid = (empty($_REQUEST['cid'])) ? 0 : intval($_REQUEST['cid']);

    $qry = "SELECT c.title, c.description FROM ${prefix}_pages_categories AS c WHERE (((c.cid)=" . intval($cid) . "))";
    $result = sql_query($qry);
    list($c_title, $c_description) = sql_fetch_row($result);

    $result = sql_query("SELECT pid, title, subtitle, clanguage FROM ${prefix}_pages WHERE active='1' AND cid='" . intval($cid) . "' ORDER BY date DESC");
    while (list($pid, $title, $subtitle, $clanguage) = sql_fetch_row($result)) {
        $the_lang = ($multilingual) ? mxContentLanguageFlag($clanguage) : "<strong><big>&middot;</big></strong>";
        $subtitle = (empty($subtitle)) ? "" : "<br /><span class=\"tiny\">(" . $subtitle . ")</span>";
        $adminlink = (MX_IS_ADMIN) ? "[&nbsp;<a href=\"admin.php?op=content_edit&amp;pid=$pid\">" . _EDIT . "</a> | <a href=\"admin.php?op=content_change_status&amp;pid=$pid&amp;active=1\">" . _DEACTIVATE . "</a> | <a href=\"admin.php?op=content_delete&amp;pid=$pid\">" . _DELETE . "</a>&nbsp;]" : "&nbsp;";
        $lines2[] = "<tr valign=\"top\"><td>$the_lang</td><td><a href=\"modules.php?name=$module_name&amp;pid=$pid\">" . strip_tags($title) . "</a>" . $subtitle . "</td><td align=\"right\">" . $adminlink . "</td></tr>";
    }

    $pagetitle = _PAGESLIST . ', ' . $c_title;
    include("header.php");
    title(_PAGESLIST . ' ' . $sitename);
    OpenTable();
    echo "<p>" . _LISTOFCONTENT . " <strong>" . $c_title . "</strong></p>";
    if ($c_description) {
        echo "<p>" . $c_description . "</p>";
    }
    $out = '';
    $result = sql_query("SELECT pid, title, subtitle, clanguage FROM ${prefix}_pages WHERE active='1' AND cid='" . intval($cid) . "' ORDER BY date");
    while (list($pid, $title, $subtitle, $clanguage) = sql_fetch_row($result)) {
        if ($multilingual) {
            $the_lang = mxContentLanguageFlag($clanguage);
        } else {
            $the_lang = "";
        }
        $subtitle = (empty($subtitle)) ? "" : "<br /><span class=\"tiny\">(" . $subtitle . ")</span>";
        if (MX_IS_ADMIN) {
            $out .= "<li> $the_lang <a href=\"modules.php?name=$module_name&amp;pid=$pid\">$title</a> $subtitle <span class=\"tiny\">[&nbsp;<a href=\"admin.php?op=content_edit&amp;pid=$pid\">" . _EDIT . "</a> | <a href=\"admin.php?op=content_change_status&amp;pid=$pid&amp;active=1\">" . _DEACTIVATE . "</a> | <a href=\"admin.php?op=content_delete&amp;pid=$pid\">" . _DELETE . "</a>&nbsp;]</span></li>";
        } else {
            $out .= "<li> $the_lang <a href=\"modules.php?name=$module_name&amp;pid=$pid\">$title</a> $subtitle</li>";
        }
    }
    if ($out) {
        echo "<br /><ul>" . $out . "</ul>";
    }

    if (MX_IS_ADMIN) {
        $out = '';
        $result = sql_query("SELECT pid, title, subtitle, clanguage FROM ${prefix}_pages WHERE active='0' AND cid='" . intval($cid) . "' ORDER BY date");
        while (list($pid, $title, $subtitle, $clanguage) = sql_fetch_row($result)) {
            if ($multilingual) {
                $the_lang = mxContentLanguageFlag($clanguage);
            } else {
                $the_lang = "";
            }
            $subtitle = (empty($subtitle)) ? "" : "<br /><span class=\"tiny\">(" . $subtitle . ")</span>";
            $out .= "<li> $the_lang <a href=\"modules.php?name=$module_name&amp;pid=$pid\">$title</a> $subtitle <span class=\"tiny\">[&nbsp;<a href=\"admin.php?op=content_edit&amp;pid=$pid\">" . _EDIT . "</a> | <a href=\"admin.php?op=content_change_status&amp;pid=$pid&amp;active=0\">" . _ACTIVATE . "</a> | <a href=\"admin.php?op=content_delete&amp;pid=$pid\">" . _DELETE . "</a>&nbsp;]</span></li>";
        }
        if ($out) {
            echo "<br /><br /><center><b>" . _YOURADMINLIST . "</b></center>";
            echo "<ul>" . $out . "</ul>";
        }
    }
    echo '<p class="align-center">[ <a href="modules.php?name=Content" title="' . _CONT_GOBACKMAIN . '">' . _CONT_GOBACK . '</a> ]</p>';
    CloseTable();

    include("footer.php");
}

function mxContentLanguageFlag($language) {
    static $img;
    
    if (!$language) {
        $language = $GLOBALS['currentlang'];
        $alt = _ALL;
    } else {
        $alt = mxGetLanguageString($language);
    }
    if (!isset($img[$alt])) {
        $img[$alt] = mxCreateImage(PMX_IMAGE_PATH . 'language/flag-' . $language . '.png', $alt, 0, 'hspace="3"');
    }
    return $img[$alt];
}

if (empty($_REQUEST['pa'])) {
    if (!empty($_REQUEST['pid'])) {
        $pa = 'showpage';
    } else if (!empty($_REQUEST['cid'])) {
        $pa = 'list_pages_categories';
    } else {
        $pa = '';
    }
} else {
    $pa = $_REQUEST['pa'];
}
switch ($pa) {
    case "showpage":
        showpage();
        break;

    case "printpage":
        PrintContPage();
        break;

    case "list_pages_categories":
        list_pages_categories();
        break;

    default:
        list_pages();
        break;
}
