/**
 * Gets latest comments for news articles and pages
 *
 * @param int $number how many comments you want.
 * @param string $type 	"all" for all latest comments for all news articles and all pages
 * 											"news" for the lastest comments of one specific news article
 * 											"page" for the lastest comments of one specific page
 * @param int $itemID the ID of the element to get the comments for if $type != "all"
 */
function getLatestZenpageComments($number, $type = "all", $itemID = "")
{
    $itemID = sanitize_numeric($itemID);
    $number = sanitize_numeric($number);
    $checkauth = zp_loggedin();
    if ($type == 'all' || $type == 'news') {
        $newspasswordcheck = "";
        if (zp_loggedin(MANAGE_ALL_NEWS_RIGHTS)) {
            $newsshow = '';
        } else {
            $newsshow = 'news.show=1 AND';
            $newscheck = query_full_array("SELECT * FROM " . prefix('news') . " ORDER BY date");
            foreach ($newscheck as $articlecheck) {
                $obj = new ZenpageNews($articlecheck['titlelink']);
                if ($obj->inProtectedCategory()) {
                    if ($checkauth && $obj->isMyItem(LIST_RIGHTS)) {
                        $newsshow = '';
                    } else {
                        $excludenews = " AND id != " . $articlecheck['id'];
                        $newspasswordcheck = $newspasswordcheck . $excludenews;
                    }
                }
            }
        }
    }
    if ($type == 'all' || $type == 'page') {
        $pagepasswordcheck = "";
        if (zp_loggedin(MANAGE_ALL_PAGES_RIGHTS)) {
            $pagesshow = '';
        } else {
            $pagesshow = 'pages.show=1 AND';
            $pagescheck = query_full_array("SELECT * FROM " . prefix('pages') . " ORDER BY date");
            foreach ($pagescheck as $pagecheck) {
                $obj = new ZenpagePage($pagecheck['titlelink']);
                if ($obj->isProtected()) {
                    if ($checkauth && $obj->isMyItem(LIST_RIGHTS)) {
                        $pagesshow = '';
                    } else {
                        $excludepages = " AND pages.id != " . $pagecheck['id'];
                        $pagepasswordcheck = $pagepasswordcheck . $excludepages;
                    }
                }
            }
        }
    }
    switch ($type) {
        case "news":
            $whereNews = " WHERE {$newsshow} news.id = " . $itemID . " AND c.ownerid = news.id AND c.type = 'news' AND c.private = 0 AND c.inmoderation = 0" . $newspasswordcheck;
            break;
        case "page":
            $wherePages = " WHERE {$pagesshow} pages.id = " . $itemID . " AND c.ownerid = pages.id AND c.type = 'pages' AND c.private = 0 AND c.inmoderation = 0" . $pagepasswordcheck;
            break;
        case "all":
            $whereNews = " WHERE {$newsshow} c.ownerid = news.id AND c.type = 'news' AND c.private = 0 AND c.inmoderation = 0" . $newspasswordcheck;
            $wherePages = " WHERE {$pagesshow} c.ownerid = pages.id AND c.type = 'pages' AND c.private = 0 AND c.inmoderation = 0" . $pagepasswordcheck;
            break;
    }
    $comments_news = array();
    $comments_pages = array();
    if ($type == "all" or $type == "news") {
        $comments_news = query_full_array("SELECT c.id, c.name, c.type, c.website," . " c.date, c.anon, c.comment, news.title, news.titlelink FROM " . prefix('comments') . " AS c, " . prefix('news') . " AS news " . $whereNews . " ORDER BY c.id DESC LIMIT {$number}");
    }
    if ($type == "all" or $type == "page") {
        $comments_pages = query_full_array($sql = "SELECT c.id, c.name, c.type, c.website," . " c.date, c.anon, c.comment, pages.title, pages.titlelink FROM " . prefix('comments') . " AS c, " . prefix('pages') . " AS pages " . $wherePages . " ORDER BY c.id DESC LIMIT {$number}");
    }
    $comments = array();
    foreach ($comments_news as $comment) {
        $comments[$comment['id']] = $comment;
    }
    foreach ($comments_pages as $comment) {
        $comments[$comment['id']] = $comment;
    }
    krsort($comments);
    return array_slice($comments, 0, $number);
}
/**
 * Gets to the Zenpage news articles
 *
 * @param  string $changefreq One of the supported changefrequence values regarding sitemap.org. Default is empty or wrong is "daily".
 * @return string
 */
function getSitemapZenpageNewsArticles()
{
    global $_zp_zenpage, $sitemap_number;
    //not splitted into several sitemaps yet
    if ($sitemap_number == 1) {
        $data = '';
        $sitemap_locales = generateLanguageList();
        $changefreq = getOption('sitemap_changefreq_news');
        $articles = $_zp_zenpage->getArticles('', 'published', true, "date", "desc");
        if ($articles) {
            $data .= sitemap_echonl('<?xml version="1.0" encoding="UTF-8"?>');
            $data .= sitemap_echonl('<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
            foreach ($articles as $article) {
                $articleobj = new ZenpageNews($article['titlelink']);
                $date = substr($articleobj->getDatetime(), 0, 10);
                $lastchange = '';
                if (!is_null($articleobj->getLastchange())) {
                    $lastchange = substr($articleobj->getLastchange(), 0, 10);
                }
                if ($date > $lastchange && !empty($lastchangedate)) {
                    $date = $lastchange;
                }
                if (!$articleobj->inProtectedCategory()) {
                    switch (SITEMAP_LOCALE_TYPE) {
                        case 1:
                            foreach ($sitemap_locales as $locale) {
                                $url = seo_locale::localePath(true, $locale) . '/' . _NEWS_ . '/' . urlencode($articleobj->getTitlelink());
                                $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $url . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $changefreq . "</changefreq>\n\t\t<priority>0.9</priority>\n\t</url>");
                            }
                            break;
                        case 2:
                            foreach ($sitemap_locales as $locale) {
                                $url = rewrite_path(_NEWS_ . '/' . urlencode($articleobj->getTitlelink()), '?p=news&amp;title=' . urlencode($articleobj->getTitlelink()), dynamic_locale::fullHostPath($locale));
                                $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $url . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $changefreq . "</changefreq>\n\t\t<priority>0.9</priority>\n\t</url>");
                            }
                            break;
                        default:
                            $url = rewrite_path(_NEWS_ . '/' . urlencode($articleobj->getTitlelink()), '?p=news&amp;title=' . urlencode($articleobj->getTitlelink()), FULLWEBPATH);
                            $data .= sitemap_echonl("\t<url>\n\t\t<loc>" . $url . "</loc>\n\t\t<lastmod>" . $date . "</lastmod>\n\t\t<changefreq>" . $changefreq . "</changefreq>\n\t\t<priority>0.9</priority>\n\t</url>");
                            break;
                    }
                }
            }
            $data .= sitemap_echonl('</urlset>');
            // End off the <urlset> tag
        }
        return $data;
    }
}