$GLOBALS['uploadpath'] = $lb_path . "/upload/";
//connect to the database
mysql_connect($lb_host, $lb_user, $lb_pass) or die("Unfortunately I couldn't connect to the database. <br />" . mysql_error());
mysql_select_db($lb_data) or die("Unfortunately I couldn't work with this database. <br />" . mysql_error());
//make all those clever functions available
include "loudblog/inc/functions.php";
$settings = getsettings();
//getting data from postings-table
$dosql = "SELECT * FROM {$GLOBALS['prefix']}lb_postings WHERE ";
//posting must be "live" to be displayed
$dosql .= "status='3' ";
//posting must not be published in the future
$dosql .= "AND posted < NOW() ";
//if category is set, filter postings which doesn't fit
if (isset($_GET['cat'])) {
    $getcat = getcategoryid(urlencode($_GET['cat']));
    $dosql .= "AND (category1_id = {$getcat}  ";
    $dosql .= "OR category2_id = {$getcat} ";
    $dosql .= "OR category3_id = {$getcat} ";
    $dosql .= "OR category4_id = {$getcat}) ";
}
//limiting number
$dosql .= "ORDER BY posted DESC LIMIT 0, {$settings['rss_postings']};";
$result = mysql_query($dosql) or die(mysql_error());
$i = 0;
while ($rows[$i] = mysql_fetch_assoc($result)) {
    $i += 1;
}
$lasttime = strtotime($rows[0]['posted']);
unset($rows[$i]);
//Ready to rock'n'roll? Let's start building the feed!
function if_category($content)
{
    //parse content only if a (certain) category or no category is being shown
    $att = getattributes($content);
    $return = "";
    $nocat = true;
    //checking the url for a category list request
    if (isset($_GET['cat'])) {
        if (!isset($att['category'])) {
            $return = fullparse(stripcontainer($content));
        } else {
            $att['category'] = htmlentities($att['category'], ENT_QUOTES, "UTF-8");
            if (getcategoryidshort($_GET['cat']) == getcategoryid($att['category'])) {
                $return = fullparse(stripcontainer($content));
            }
        }
        $nocat = false;
    }
    //checking url for single posting
    if (isset($_GET['id'])) {
        //checking if category is available
        $dosql = "SELECT category1_id, category2_id, category3_id, category4_id \n              FROM " . $GLOBALS['prefix'] . "lb_postings \n              WHERE id='" . $_GET['id'] . "';";
        $result = mysql_query($dosql) or die(mysql_error());
        $row = mysql_fetch_assoc($result);
        $show = false;
        foreach ($row as $c => $id) {
            if (!isset($att['category'])) {
                if ($id != 0) {
                    $return = fullparse(stripcontainer($content));
                    $nocat = false;
                    break;
                }
            } else {
                $att['category'] = htmlentities($att['category'], ENT_QUOTES, "UTF-8");
                if ($id == getcategoryid($att['category'])) {
                    $return = fullparse(stripcontainer($content));
                    $nocat = false;
                    break;
                }
            }
        }
    }
    //the "no category" option at the end
    if ($nocat and isset($att['category']) and $att['category'] == "false") {
        $return = fullparse(stripcontainer($content));
    }
    return trim($return);
}
function loop_postings($content)
{
    //returns a certain number of postings
    global $currentid;
    global $postings;
    $att = getattributes($content);
    $content = stripcontainer($content);
    //possible attributes and default-values
    if (isset($att['number'])) {
        $loops = $att['number'];
    } else {
        $loops = 5;
    }
    if (isset($att['order'])) {
        $order = strtoupper($att['order']);
    } else {
        $order = "DESC";
    }
    if (isset($att['forceloop'])) {
        $forceloop = $att['forceloop'];
    } else {
        $forceloop = false;
    }
    if (isset($att['paging'])) {
        $paging = $att['paging'];
    } else {
        $paging = true;
    }
    $return = "";
    //offset / splitting into pages
    if (isset($_GET['page']) and $paging == "true") {
        $start = $loops * ($_GET['page'] - 1);
    } else {
        $start = 0;
    }
    //no request from url? show us a loop of postings!
    if (!isset($_GET['id']) or $forceloop) {
        //getting data from postings-table
        $dosql = "SELECT * FROM " . $GLOBALS['prefix'] . "lb_postings WHERE ";
        //posting must be "live" to be displayed
        $dosql .= "status='3' ";
        //posting must not be published in the future
        $dosql .= "AND posted < NOW() ";
        //if category is set, filter postings which doesn't fit
        if (isset($_GET['cat'])) {
            $dosql .= "AND (category1_id = " . getcategoryid($_GET['cat']) . " ";
            $dosql .= "OR category2_id = " . getcategoryid($_GET['cat']) . " ";
            $dosql .= "OR category3_id = " . getcategoryid($_GET['cat']) . " ";
            $dosql .= "OR category4_id = " . getcategoryid($_GET['cat']) . ") ";
        }
        //order and paging
        $dosql .= "ORDER BY posted " . $order . " LIMIT " . $start . ", " . $loops . ";";
        //get data from MySQL
        $result = mysql_query($dosql) or die(mysql_error());
        $i = 0;
        //use all results!
        while ($temp = mysql_fetch_assoc($result)) {
            $i += 1;
            $currentid = $temp['id'];
            $postings[$currentid] = $temp;
            $return .= fullparse($content);
        }
    } else {
        //ah, we want to show a single posting with a given id? no problem!
        //getting data from postings-table
        $dosql = "SELECT * FROM " . $GLOBALS['prefix'] . "lb_postings \n              WHERE id='" . $_GET['id'] . "' \n              AND posted < NOW() AND status='3';";
        $result = mysql_query($dosql) or die(mysql_error());
        $temp = mysql_fetch_assoc($result);
        $currentid = $temp['id'];
        $postings[$currentid] = $temp;
        $return .= fullparse($content);
    }
    return trim($return);
}