OSDN Git Service

Merge branch 'skinnable-master' of sakamocchi@git.sourceforge.jp:/gitroot/nucleus...
[nucleus-jp/nucleus-next.git] / nucleus / libs / BLOG.php
index dca9da4..faf5bbe 100644 (file)
-<?php
-
-/*
- * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
- * Copyright (C) 2002-2009 The Nucleus Group
- *
- * This program 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 2
- * of the License, or (at your option) any later version.
- * (see nucleus/documentation/index.html#license for more info)
- */
-/**
- * A class representing a blog and containing functions to get that blog shown
- * on the screen
- *
- * @license http://nucleuscms.org/license.txt GNU General Public License
- * @copyright Copyright (C) 2002-2009 The Nucleus Group
- * @version $Id: BLOG.php 1624 2012-01-09 11:36:20Z sakamocchi $
- */
-
-if ( !function_exists('requestVar') ) exit;
-require_once dirname(__FILE__) . '/ITEMACTIONS.php';
-
-class BLOG {
-
-       // blog id
-       var $blogid;
-
-       // ID of currently selected category
-       var $selectedcatid;
-
-       // After creating an object of the blog class, contains true if the BLOG object is
-       // valid (the blog exists)
-       var $isValid;
-
-       // associative array, containing all blogsettings (use the get/set functions instead)
-       var $settings;
-
-       /**
-        * Creates a new BLOG object for the given blog
-        *
-        * @param $id blogid
-        */
-       function BLOG($id) {
-               $this->blogid = intval($id);
-               $this->readSettings();
-
-               // try to set catid
-               // (the parse functions in SKIN.php will override this, so it's mainly useless)
-               global $catid;
-               $this->setSelectedCategory($catid);
-       }
-
-       /**
-        * Shows the given amount of items for this blog
-        *
-        * @param $template
-        *              String representing the template _NAME_ (!)
-        * @param $amountEntries
-        *              amount of entries to show
-        * @param $startpos
-        *              offset from where items should be shown (e.g. 5 = start at fifth item)
-        * @returns int
-        *              amount of items shown
-        */
-       function readLog($template, $amountEntries, $offset = 0, $startpos = 0) {
-               return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);
-       }
-
-       /**
-        * BLOG::showArchive()
-        * Shows an archive for a given month
-        *
-        * @param integer       $year           year
-        * @param integer       $month          month
-        * @param string        $template       String representing the template name to be used
-        * @return void
-        * 
-        */
-       function showArchive($templatename, $year, $month=0, $day=0)
-       {
-               // create extra where clause for select query
-               if ( $day == 0 && $month != 0 )
-               {
-                       $timestamp_start = mktime(0,0,0,$month,1,$year);
-                       // also works when $month==12
-                       $timestamp_end = mktime(0,0,0,$month+1,1,$year);
-               }
-               elseif ( $month == 0 )
-               {
-                       $timestamp_start = mktime(0,0,0,1,1,$year);
-                       // also works when $month==12
-                       $timestamp_end = mktime(0,0,0,12,31,$year);
-               }
-               else
-               {
-                       $timestamp_start = mktime(0,0,0,$month,$day,$year);
-                       $timestamp_end = mktime(0,0,0,$month,$day+1,$year);
-               }
-               $extra_query = " and i.itime>='%s' and i.itime<'%s'";
-               $extra_query = sprintf($extra_query, i18n::formatted_datetime('mysql', $timestamp_start), i18n::formatted_datetime('mysql', $timestamp_end));
-               
-               $this->readLogAmount($templatename,0,$extra_query,'',1,1);
-               return;
-       }
-
-       /**
-        * Sets the selected category by id (only when category exists)
-        */
-       function setSelectedCategory($catid) {
-               if ($this->isValidCategory($catid) || (intval($catid) == 0))
-                       $this->selectedcatid = intval($catid);
-       }
-
-       /**
-        * Sets the selected category by name
-        */
-       function setSelectedCategoryByName($catname) {
-               $this->setSelectedCategory($this->getCategoryIdFromName($catname));
-       }
-
-       /**
-        * Returns the selected category
-        */
-       function getSelectedCategory() {
-               return $this->selectedcatid;
-       }
-
-       /**
-        * Shows the given amount of items for this blog
-        *
-        * @param $template
-        *              String representing the template _NAME_ (!)
-        * @param $amountEntries
-        *              amount of entries to show (0 = no limit)
-        * @param $extraQuery
-        *              extra conditions to be added to the query
-        * @param $highlight
-        *              contains a query that should be highlighted
-        * @param $comments
-        *              1=show comments 0=don't show comments
-        * @param $dateheads
-        *              1=show dateheads 0=don't show dateheads
-        * @param $offset
-        *              offset
-        * @returns int
-        *              amount of items shown
-        */
-       function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0) {
-
-               $query = $this->getSqlBlog($extraQuery);
-
-               if ($amountEntries > 0) {
-                               // $offset zou moeten worden:
-                               // (($startpos / $amountentries) + 1) * $offset ... later testen ...
-                          $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);
-               }
-               return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
-       }
-
-       /**
-        * BLOG::showUsingQuery()
-        * Do the job for readLogAmmount
-        * 
-        * @param       string          $templateName   template name
-        * @param       string          $query                  string for query
-        * @param       string          $highlight              string to be highlighted
-        * @param       integer $comments               the number of comments
-        * @param       boolean $dateheads              date header is needed or not
-        * @return      integer the number of rows as a result of mysql query
-        * 
-        */     
-       function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1)
-       {
-               global $CONF, $manager, $currentTemplateName;
-               
-               $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');
-               if ( $lastVisit != 0 )
-               {
-                       $lastVisit = $this->getCorrectTime($lastVisit);
-               }
-               
-               // set templatename as global variable (so plugins can access it)
-               $currentTemplateName = $templateName;
-               $template =& $manager->getTemplate($templateName);
-               
-               // create parser object & action handler
-               $actions = new ITEMACTIONS($this);
-               $parser = new PARSER($actions->getDefinedActions(),$actions);
-               $actions->setTemplate($template);
-               $actions->setHighlight($highlight);
-               $actions->setLastVisit($lastVisit);
-               $actions->setParser($parser);
-               $actions->setShowComments($comments);
-               
-               // execute query
-               $items = sql_query($query);
-               
-               // loop over all items
-               $old_date = 0;
-               while ( $item = sql_fetch_object($items) )
-               {
-                       $item->timestamp = strtotime($item->itime);     // string timestamp -> unix timestamp
-                       
-                       // action handler needs to know the item we're handling
-                       $actions->setCurrentItem($item);
-                       
-                       // add date header if needed
-                       if ( $dateheads )
-                       {
-                               $new_date = date('dFY',$item->timestamp);
-                               if ( $new_date != $old_date )
-                               {
-                                       // unless this is the first time, write date footer
-                                       $timestamp = $item->timestamp;
-                                       if ( $old_date != 0 )
-                                       {
-                                               $oldTS = strtotime($old_date);
-                                               $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));
-                                               $tmp_footer = i18n::strftime(isset($template['DATE_FOOTER'])?$template['DATE_FOOTER']:'', $oldTS);
-                                               $parser->parse($tmp_footer);
-                                               $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));
-                                       }
-                                       $manager->notify('PreDateHead',array('blog' => &$this, 'timestamp' => $timestamp));
-                                       // note, to use templatvars in the dateheader, the %-characters need to be doubled in
-                                       // order to be preserved by strftime
-                                       $tmp_header = i18n::strftime((isset($template['DATE_HEADER']) ? $template['DATE_HEADER'] : null), $timestamp);
-                                       $parser->parse($tmp_header);
-                                       $manager->notify('PostDateHead',array('blog' => &$this, 'timestamp' => $timestamp));
-                               }
-                               $old_date = $new_date;
-                       }
-                       
-                       // parse item
-                       $parser->parse($template['ITEM_HEADER']);
-                       $manager->notify('PreItem', array('blog' => &$this, 'item' => &$item));
-                       $parser->parse($template['ITEM']);
-                       $manager->notify('PostItem', array('blog' => &$this, 'item' => &$item));
-                       $parser->parse($template['ITEM_FOOTER']);
-               }
-               
-               $numrows = sql_num_rows($items);
-               
-               // add another date footer if there was at least one item
-               if ( ($numrows > 0) && $dateheads )
-               {
-                       $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));
-                       $parser->parse($template['DATE_FOOTER']);
-                       $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));
-               }
-               
-               sql_free_result($items);
-               return $numrows;
-       }
-       
-       /**
-        * Simplified function for showing only one item
-        */
-       function showOneitem($itemid, $template, $highlight) {
-               $extraQuery = ' and inumber=' . intval($itemid);
-
-               return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);
-       }
-
-
-       /**
-        * BLOG::addItem()
-        * Adds an item to this blog
-        * 
-        * @param       Integer $catid  ID for category
-        * @param       String  $title  ID for 
-        * @param       String  $body   text for body
-        * @param       String  $more   text for more
-        * @param       Integer $blogid ID for blog
-        * @param       Integer $authorid       ID for author
-        * @param       Timestamp       $timestamp      UNIX timestamp for post
-        * @param       Boolean $closed opened or closed
-        * @param       Boolean $draft  draft or not
-        * @param       Boolean $posted posted or not
-        * @return
-        */
-       function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft, $posted='1')
-       {
-               global $manager;
-               
-               $blogid         = intval($blogid);
-               $authorid       = intval($authorid);
-               $title          = $title;
-               $body           = $body;
-               $more           = $more;
-               $catid          = intval($catid);
-               
-               // convert newlines to <br />
-               if ( $this->convertBreaks() )
-               {
-                       $body = addBreaks($body);
-                       $more = addBreaks($more);
-               }
-
-               if ( $closed != '1' )
-               {
-                       $closed = '0';
-               }
-               if ( $draft != '0' )
-               {
-                       $draft = '1';
-               }
-               
-               if ( !$this->isValidCategory($catid) )
-               {
-                       $catid = $this->getDefaultCategory();
-               }
-               
-               if ( $timestamp > $this->getCorrectTime() )
-               {
-                       $isFuture = 1;
-               }
-               
-               $timestamp = date('Y-m-d H:i:s',$timestamp);
-               
-               $manager->notify('PreAddItem',array('title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$this, 'authorid' => &$authorid, 'timestamp' => &$timestamp, 'closed' => &$closed, 'draft' => &$draft, 'catid' => &$catid));
-               
-               $ititle = sql_real_escape_string($title);
-               $ibody = sql_real_escape_string($body);
-               $imore = sql_real_escape_string($more);
-               
-               $query = "INSERT INTO %s (ITITLE, IBODY, IMORE, IBLOG, IAUTHOR, ITIME, ICLOSED, IDRAFT, ICAT, IPOSTED) VALUES ('%s', '%s', '%s', %d, %d, '%s', %s, %s, %s, %s)";
-               $query = sprintf($query, sql_table('item'), $ititle, $ibody, $imore, $blogid, $authorid, $timestamp, $closed, $draft, $catid, $posted);
-               sql_query($query);
-               $itemid = sql_insert_id();
-               
-               $manager->notify('PostAddItem',array('itemid' => $itemid));
-               
-               if ( !$draft )
-               {
-                       $this->updateUpdateFile();
-               }
-               // send notification mail
-               if ( !$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem() )
-               {
-                       $this->sendNewItemNotification($itemid, $title, $body);
-               }
-               return $itemid;
-       }
-       
-       /**
-        * BLOG::sendNewItemNotification()
-        * Send a new item notification to the notification list
-        * 
-        * @param String        $itemid ID of the item
-        * @param String        $title  title of the item
-        * @param String        $body   body of the item
-        * @return      Void
-        */
-       function sendNewItemNotification($itemid, $title, $body)
-       {
-               global $CONF, $member;
-               
-               $ascii = ENTITY::anchor_footnoting($body);
-               
-               $message = _NOTIFY_NI_MSG . " \n";
-               $temp = parse_url($CONF['Self']);
-               if ( $temp['scheme'] )
-               {
-                       $message .= LINK::create_item_link($itemid) . "\n\n";
-               }
-               else
-               {
-                       $tempurl = $this->getURL();
-                       if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )
-                       {
-                               $message .= $tempurl . '?itemid=' . $itemid . "\n\n";
-                       }
-                       else
-                       {
-                               $message .= $tempurl . '/?itemid=' . $itemid . "\n\n";
-                       }
-               }
-               $message .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";
-               $message .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";
-               $message .= NOTIFICATION::get_mail_footer();
-               
-               $subject = $this->getName() . ': ' . _NOTIFY_NI_TITLE;
-               
-               $from = $member->getNotifyFromMailAddress();
-               
-               NOTIFICATION::mail($this->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());
-               return;
-       }
-       
-       /**
-        * BLOG::createNewCategory()
-        * Creates a new category for this blog
-        *
-        * @param String        $catName        name of the new category. When empty, a name is generated automatically (starting with newcat)
-        * @param String        $catDescription description of the new category. Defaults to 'New Category'
-        * @returns     Integer the new category-id in case of success. 0 on failure
-        */
-       function createNewCategory($catName = '', $catDescription = _CREATED_NEW_CATEGORY_DESC)
-       {
-               global $member, $manager;
-               
-               if ( $member->blogAdminRights($this->getID()) )
-               {
-                       // generate
-                       if ( $catName == '' )
-                       {
-                               $catName = _CREATED_NEW_CATEGORY_NAME;
-                               $i = 1;
-                               
-                               $res = sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID());
-                               while ( sql_num_rows($res) > 0 )
-                               {
-                                       $i++;
-                                       $res = sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID());
-                               }
-                               
-                               $catName = $catName . $i;
-                       }
-                       
-                       $manager->notify(
-                               'PreAddCategory',
-                               array(
-                                       'blog' => &$this,
-                                       'name' => &$catName,
-                                       'description' => $catDescription
-                               )
-                       );
-                       
-                       $query = "INSERT INTO %s (cblog, cname, cdesc) VALUES (%d, '%s', '%s')";
-                       $query = sprintf($query, sql_table('category'), (integer) $this->getID(), sql_real_escape_string($catName), sql_real_escape_string($catDescription));
-                       sql_query($query);
-                       $catid = sql_insert_id();
-                       
-                       $manager->notify(
-                               'PostAddCategory',
-                               array(
-                                       'blog' => &$this,
-                                       'name' => $catName,
-                                       'description' => $catDescription,
-                                       'catid' => $catid
-                               )
-                       );
-                       
-                       return $catid;
-               }
-               return 0;
-       }
-       
-       /**
-        * Searches all months of this blog for the given query
-        *
-        * @param $query
-        *              search query
-        * @param $template
-        *              template to be used (__NAME__ of the template)
-        * @param $amountMonths
-        *              max amount of months to be search (0 = all)
-        * @param $maxresults
-        *              max number of results to show
-        * @param $startpos
-        *              offset
-        * @returns
-        *              amount of hits found
-        */
-       function search($query, $template, $amountMonths, $maxresults, $startpos) {
-               global $CONF, $manager;
-
-               $highlight      = '';
-               $sqlquery       = $this->getSqlSearch($query, $amountMonths, $highlight);
-
-               if ($sqlquery == '')
-               {
-                       // no query -> show everything
-                       $extraquery = '';
-                       $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);
-               } else {
-
-                       // add LIMIT to query (to split search results into pages)
-                       if (intval($maxresults > 0))
-                               $sqlquery .= ' LIMIT ' . intval($startpos).',' . intval($maxresults);
-
-                       // show results
-                       $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);
-
-                       // when no results were found, show a message
-                       if ($amountfound == 0)
-                       {
-                               $template =& $manager->getTemplate($template);
-                               $vars = array(
-                                       'query'         => ENTITY::hsc($query),
-                                       'blogid'        => $this->getID()
-                               );
-                               echo TEMPLATE::fill($template['SEARCH_NOTHINGFOUND'],$vars);
-                       }
-               }
-
-               return $amountfound;
-       }
-
-       /**
-        * BLOG::getSqlSearch()
-        * Returns an SQL query to use for a search query
-        * No LIMIT clause is added. (caller should add this if multiple pages are requested)
-        *
-        * @param string        $query  search query
-        * @param       integer $amountMonths   amount of months to search back. Default = 0 = unlimited
-        * @param       string  $mode   either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
-        * @return      string  $highlight      words to highlight (out parameter)
-        * @return      string  either a full SQL query, or an empty string (if querystring empty)
-        */
-       function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')
-       {
-               $searchclass = new SEARCH($query);
-               
-               $highlight       = $searchclass->inclusive;
-               
-               // if querystring is empty, return empty string
-               if ( $searchclass->inclusive == '' )
-               {
-                       return '';
-               }
-               
-               $where  = $searchclass->boolean_sql_where('ititle, ibody, imore');
-               $select = $searchclass->boolean_sql_select('ititle, ibody, imore');
-               
-               // get list of blogs to search
-               $blogs                  = $searchclass->blogs;  // array containing blogs that always need to be included
-               $blogs[]                = $this->getID();                       // also search current blog (duh)
-               $blogs                  = array_unique($blogs); // remove duplicates
-               $selectblogs    = '';
-               
-               if ( count($blogs) > 0 )
-               {
-                       $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';
-               }
-               
-               if ( $mode == '' )
-               {
-                       $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, i.itime, i.imore as more, i.icat as catid, i.iclosed as closed,
-                               m.mname as author, m.mrealname as authorname, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl,
-                               c.cname as category';
-                       
-                       if ( $select )
-                       {
-                               $query .= ', '.$select. ' as score ';
-                       }
-               }
-               else
-               {
-                       $query = 'SELECT COUNT(*) as result ';
-               }
-               
-               $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
-                               . ' WHERE i.iauthor=m.mnumber'
-                               . ' and i.icat=c.catid'
-                               // exclude drafts
-                               . ' and i.idraft=0'
-                               . $selectblogs
-                               // don't show future items
-                               . ' and i.itime<="' . i18n::formatted_datetime('mysql', $this->getCorrectTime()) . '"'
-                               . ' and '.$where;
-
-               // take into account amount of months to search
-               if ( $amountMonths > 0 )
-               {
-                       $localtime = getdate($this->getCorrectTime());
-                       $timestamp_start = mktime(0,0,0,$localtime['mon'] - $amountMonths,1,$localtime['year']);
-                       $query .= ' and i.itime>"' . i18n::formatted_datetime('mysql', $timestamp_start) . '"';
-               }
-               
-               if ( $mode == '' )
-               {
-                       if ( $select )
-                       {
-                               $query .= ' ORDER BY score DESC';
-                       }
-                       else
-                       {
-                               $query .= ' ORDER BY i.itime DESC ';
-                       }
-               }
-               return $query;
-       }
-       
-       /**
-        * BLOG::getSqlBlog()
-        * Returns the SQL query that's normally used to display the blog items on the index type skins
-        * No LIMIT clause is added. (caller should add this if multiple pages are requested)
-        *
-        * @param       string  $extraQuery     extra query string
-        * @param       string  $mode                   either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
-        * @return      string  either a full SQL query, or an empty string
-        */
-       function getSqlBlog($extraQuery, $mode = '')
-       {
-               if ( $mode == '' )
-               {
-                       $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author,
-                               m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail,
-                               m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';
-               }
-               else
-               {
-                       $query = 'SELECT COUNT(*) as result ';
-               }
-               
-               $query  .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
-                               . ' WHERE i.iblog='.$this->blogid
-                               . ' and i.iauthor=m.mnumber'
-                               . ' and i.icat=c.catid'
-                               // exclude drafts
-                               . ' and i.idraft=0'
-                               // don't show future items
-                               . ' and i.itime<="' . i18n::formatted_datetime('mysql', $this->getCorrectTime()) . '"';
-               
-               if ( $this->getSelectedCategory() )
-               {
-                       $query .= ' and i.icat=' . $this->getSelectedCategory() . ' ';
-               }
-               
-               $query .= $extraQuery;
-               
-               if ( $mode == '' )
-               {
-                       $query .= ' ORDER BY i.itime DESC';
-               }
-               return $query;
-       }
-       
-       /**
-        * BLOG::showArchiveList()
-        * Shows the archivelist using the given template
-        * 
-        * @param       String  $template       template name
-        * @param       String  $mode   year/month/day
-        * @param       Integer $limit  limit of record count
-        * @return      Void
-        */
-       function showArchiveList($template, $mode = 'month', $limit = 0)
-       {
-               global $CONF, $catid, $manager;
-               
-               if ( !isset ($linkparams) )
-               {
-                       $linkparams = array();
-               }
-               
-               if ( $catid )
-               {
-                       $linkparams = array('catid' => $catid);
-               }
-               
-               $template =& $manager->getTemplate($template);
-               $data['blogid'] = $this->getID();
-               
-               if ( !array_key_exists('ARCHIVELIST_HEADER', $template) || !$template['ARCHIVELIST_HEADER'] )
-               {
-                       $tplt = '';
-               }
-               else
-               {
-                       $tplt = $template['ARCHIVELIST_HEADER'];
-               }
-               
-               echo TEMPLATE::fill($tplt, $data);
-               
-               $query = 'SELECT itime, SUBSTRING(itime,1,4) AS Year, SUBSTRING(itime,6,2) AS Month, SUBSTRING(itime,9,2) AS Day'
-                               . ' FROM '.sql_table('item')
-                               . ' WHERE iblog=' . $this->getID()
-                               // don't show future items!
-                               . ' AND itime <="' . i18n::formatted_datetime('mysql', $this->getCorrectTime()) . '"'
-                               // don't show draft items
-                               . ' AND idraft=0';
-               
-               if ( $catid )
-               {
-                       $query .= ' and icat=' . intval($catid);
-               }
-               
-               $query .= ' GROUP BY Year';
-               if ( $mode == 'month' || $mode == 'day' )
-               {
-                       $query .= ', Month';
-               }
-               if ( $mode == 'day' )
-               {
-                       $query .= ', Day';
-               }
-               
-               $query .= ' ORDER BY itime DESC';
-               
-               if ( $limit > 0 )
-               {
-                       $query .= ' LIMIT ' . intval($limit);
-               }
-               
-               $res = sql_query($query);
-               while ( $current = sql_fetch_object($res) )
-               {
-                       /* string time -> unix timestamp */
-                       $current->itime = strtotime($current->itime);
-                       
-                       if ( $mode == 'day' )
-                       {
-                               $archivedate = date('Y-m-d',$current->itime);
-                               $archive['day'] = date('d',$current->itime);
-                               $data['day'] = date('d',$current->itime);
-                               $data['month'] = date('m',$current->itime);
-                               $archive['month'] = $data['month'];
-                       }
-                       elseif ( $mode == 'year' )
-                       {
-                               $archivedate = date('Y',$current->itime);
-                               $data['day'] = '';
-                               $data['month'] = '';
-                               $archive['day'] = '';
-                               $archive['month'] = '';
-                       }
-                       else
-                       {
-                               $archivedate = date('Y-m',$current->itime);
-                               $data['month'] = date('m',$current->itime);
-                               $archive['month'] = $data['month'];
-                               $data['day'] = '';
-                               $archive['day'] = '';
-                       }
-                       
-                       $data['year'] = date('Y',$current->itime);
-                       $archive['year'] = $data['year'];
-                       $data['archivelink'] = LINK::create_archive_link($this->getID(),$archivedate,$linkparams);
-                       
-                       $manager->notify(
-                               'PreArchiveListItem',
-                               array(
-                                       'listitem' => &$data
-                               )
-                       );
-                       
-                       $temp = TEMPLATE::fill($template['ARCHIVELIST_LISTITEM'],$data);
-                       echo i18n::strftime($temp,$current->itime);
-                       return;
-               }
-               
-               sql_free_result($res);
-               
-               if ( !array_key_exists('ARCHIVELIST_FOOTER', $template) || !$template['ARCHIVELIST_FOOTER'] )
-               {
-                       $tplt = '';
-               }
-               else
-               {
-                       $tplt = $template['ARCHIVELIST_FOOTER'];
-               }
-               
-               echo TEMPLATE::fill($tplt, $data);
-               return;
-       }
-       
-       /**
-        * BLOG::showCategoryList()
-        * Shows the list of categories using a given template
-        * 
-        * @param       String  $template       Template Name
-        * @return      Void
-        */
-       function showCategoryList($template)
-       {
-               global $CONF, $manager;
-               
-               /*
-                * determine arguments next to catids
-                * I guess this can be done in a better way, but it works
-                */
-               global $archive, $archivelist;
-               
-               $linkparams = array();
-               if ( $archive )
-               {
-                       $blogurl = LINK::create_archive_link($this->getID(), $archive, '');
-                       $linkparams['blogid'] = $this->getID();
-                       $linkparams['archive'] = $archive;
-               }
-               else if ( $archivelist )
-               {
-                       $blogurl = LINK::create_archivelist_link($this->getID(), '');
-                       $linkparams['archivelist'] = $archivelist;
-               }
-               else
-               {
-                       $blogurl = LINK::create_blogid_link($this->getID(), '');
-                       $linkparams['blogid'] = $this->getID();
-               }
-               
-               $template =& $manager->getTemplate($template);
-               
-               //: Change: Set nocatselected variable
-               if ( $this->getSelectedCategory() )
-               {
-                       $nocatselected = 'no';
-               }
-               else
-               {
-                       $nocatselected = 'yes';
-               } 
-               
-               echo TEMPLATE::fill((isset($template['CATLIST_HEADER']) ? $template['CATLIST_HEADER'] : null),
-                       array(
-                               'blogid' => $this->getID(),
-                               'blogurl' => $blogurl,
-                               'self' => $CONF['Self'],
-                               //: Change: Set catiscurrent template variable for header
-                               'catiscurrent' => $nocatselected,
-                               'currentcat' => $nocatselected 
-                       ));
-               
-               $query = 'SELECT catid, cdesc as catdesc, cname as catname FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' ORDER BY cname ASC';
-               $res = sql_query($query);
-               
-               while ( $data = sql_fetch_assoc($res) )
-               {
-                       $data['blogid'] = $this->getID();
-                       $data['blogurl'] = $blogurl;
-                       $data['catlink'] = LINK::create_link(
-                               'category',
-                               array(
-                                       'catid' => $data['catid'],
-                                       'name' => $data['catname'],
-                                       'extra' => $linkparams
-                               ));
-                       $data['self'] = $CONF['Self'];
-                       
-                       //catiscurrent
-                       //: Change: Bugfix for catiscurrent logic so it gives catiscurrent = no when no category is selected.
-                       $data['catiscurrent'] = 'no';
-                       $data['currentcat'] = 'no'; 
-                       if ( $this->getSelectedCategory() )
-                       {
-                               if ( $this->getSelectedCategory() == $data['catid'] )
-                               {
-                                       $data['catiscurrent'] = 'yes';
-                                       $data['currentcat'] = 'yes';
-                               }
-                       }
-                       else
-                       {
-                               global $itemid;
-                               if ( intval($itemid) && $manager->existsItem(intval($itemid),0,0) )
-                               {
-                                       $iobj =& $manager->getItem(intval($itemid),0,0);
-                                       $cid = $iobj['catid'];
-                                       if ( $cid == $data['catid'] )
-                                       {
-                                               $data['catiscurrent'] = 'yes';
-                                               $data['currentcat'] = 'yes';
-                                       }
-                               }
-                       }
-                       
-                       $manager->notify(
-                               'PreCategoryListItem',
-                               array(
-                                       'listitem' => &$data
-                               )
-                       );
-                       
-                       echo TEMPLATE::fill((isset($template['CATLIST_LISTITEM']) ? $template['CATLIST_LISTITEM'] : null), $data);
-               }
-               
-               sql_free_result($res);
-               
-               echo TEMPLATE::fill((isset($template['CATLIST_FOOTER']) ? $template['CATLIST_FOOTER'] : null),
-                       array(
-                               'blogid' => $this->getID(),
-                               'blogurl' => $blogurl,
-                               'self' => $CONF['Self'],
-                               //: Change: Set catiscurrent template variable for footer
-                               'catiscurrent' => $nocatselected,
-                               'currentcat' => $nocatselected  
-                       ));
-               return;
-       }
-       
-       /**
-        * BLOG::showBlogList()
-        * Shows a list of all blogs in the system using a given template
-        * ordered by number, name, shortname or description
-        * in ascending or descending order
-        * 
-        * @param       String  $template       tempalte name
-        * @param       String  $bnametype      bname/bshortname
-        * @param       String  $orderby        string for 'ORDER BY' SQL
-        * @param       String  $direction      ASC/DESC
-        * @return      Void
-        */
-       function showBlogList($template, $bnametype, $orderby, $direction)
-       {
-               global $CONF, $manager;
-               
-               switch ( $orderby )
-               {
-                       case 'number':
-                               $orderby='bnumber';
-                               break;
-                       case 'name':
-                               $orderby='bname';
-                               break;
-                       case 'shortname':
-                               $orderby='bshortname';
-                               break;
-                       case 'description':
-                               $orderby='bdesc';
-                               break;
-                       default:
-                               $orderby='bnumber';
-                               break;
-               }
-               
-               $direction=strtolower($direction);
-               switch ( $direction )
-               {
-                       case 'asc':
-                               $direction='ASC';
-                               break;
-                       case 'desc':
-                               $direction='DESC';
-                               break;
-                       default:
-                               $direction='ASC';
-                               break;
-               }
-               
-               $template =& $manager->getTemplate($template);
-               
-               echo TEMPLATE::fill((isset($template['BLOGLIST_HEADER']) ? $template['BLOGLIST_HEADER'] : null),
-                       array(
-                               'sitename' => $CONF['SiteName'],
-                               'siteurl' => $CONF['IndexURL']
-                       )
-               );
-               
-               $query = 'SELECT bnumber, bname, bshortname, bdesc, burl FROM '.sql_table('blog').' ORDER BY '.$orderby.' '.$direction;
-               $res = sql_query($query);
-               
-               while ( $data = sql_fetch_assoc($res) )
-               {
-                       $list = array();
-                       $list['bloglink'] = LINK::create_blogid_link($data['bnumber']);
-                       $list['blogdesc'] = $data['bdesc'];
-                       $list['blogurl'] = $data['burl'];
-                       
-                       if ( $bnametype == 'shortname' )
-                       {
-                               $list['blogname'] = $data['bshortname'];
-                       }
-                       else
-                       {
-                               /* all other cases */
-                               $list['blogname'] = $data['bname'];
-                       }
-                       
-                       $manager->notify(
-                               'PreBlogListItem',
-                               array(
-                                       'listitem' => &$list
-                               )
-                       );
-                       
-                       echo TEMPLATE::fill((isset($template['BLOGLIST_LISTITEM']) ? $template['BLOGLIST_LISTITEM'] : null), $list);
-               }
-               
-               sql_free_result($res);
-               
-               echo TEMPLATE::fill((isset($template['BLOGLIST_FOOTER']) ? $template['BLOGLIST_FOOTER'] : null),
-                       array(
-                               'sitename' => $CONF['SiteName'],
-                               'siteurl' => $CONF['IndexURL']
-                       )
-               );
-               return;
-       }
-       
-       /**
-         * Read the blog settings
-         */
-       function readSettings() {
-               $query =  'SELECT *'
-                          . ' FROM '.sql_table('blog')
-                          . ' WHERE bnumber=' . $this->blogid;
-               $res = sql_query($query);
-
-               $this->isValid = (sql_num_rows($res) > 0);
-               if (!$this->isValid)
-                       return;
-
-               $this->settings = sql_fetch_assoc($res);
-       }
-
-       /**
-         * Write the blog settings
-         */
-       function writeSettings() {
-
-               // (can't use floatval since not available prior to PHP 4.2)
-               $offset = $this->getTimeOffset();
-               if (!is_float($offset))
-                       $offset = intval($offset);
-
-               $query =  'UPDATE '.sql_table('blog')
-                          . " SET bname='" . sql_real_escape_string($this->getName()) . "',"
-                          . "     bshortname='". sql_real_escape_string($this->getShortName()) . "',"
-                          . "     bcomments=". intval($this->commentsEnabled()) . ","
-                          . "     bmaxcomments=" . intval($this->getMaxComments()) . ","
-                          . "     btimeoffset=" . $offset . ","
-                          . "     bpublic=" . intval($this->isPublic()) . ","
-                          . "     breqemail=" . intval($this->emailRequired()) . ","
-                          . "     bconvertbreaks=" . intval($this->convertBreaks()) . ","
-                          . "     ballowpast=" . intval($this->allowPastPosting()) . ","
-                          . "     bnotify='" . sql_real_escape_string($this->getNotifyAddress()) . "',"
-                          . "     bnotifytype=" . intval($this->getNotifyType()) . ","
-                          . "     burl='" . sql_real_escape_string($this->getURL()) . "',"
-                          . "     bupdate='" . sql_real_escape_string($this->getUpdateFile()) . "',"
-                          . "     bdesc='" . sql_real_escape_string($this->getDescription()) . "',"
-                          . "     bdefcat=" . intval($this->getDefaultCategory()) . ","
-                          . "     bdefskin=" . intval($this->getDefaultSkin()) . ","
-                          . "     bincludesearch=" . intval($this->getSearchable())
-                          . " WHERE bnumber=" . intval($this->getID());
-               sql_query($query);
-
-       }
-
-       /**
-         * Update the update file if requested
-         */    
-       function updateUpdatefile() {
-                if ($this->getUpdateFile()) {
-                       $f_update = fopen($this->getUpdateFile(),'w');
-                       fputs($f_update,$this->getCorrectTime());
-                       fclose($f_update);
-                }
-
-       }
-
-       /**
-         * Check if a category with a given catid is valid
-         * 
-         * @param $catid
-         *     category id
-         */
-       function isValidCategory($catid) {
-               $query = 'SELECT * FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' and catid=' . intval($catid);
-               $res = sql_query($query);
-               return (sql_num_rows($res) != 0);
-       }
-
-       /**
-         * Get the category name for a given catid
-         * 
-         * @param $catid
-         *     category id
-         */
-       function getCategoryName($catid) {
-               $res = sql_query('SELECT cname FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));
-               $o = sql_fetch_object($res);
-               return $o->cname;
-       }
-
-       /**
-         * Get the category description for a given catid
-         * 
-         * @param $catid
-         *     category id
-         */
-       function getCategoryDesc($catid) {
-               $res = sql_query('SELECT cdesc FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));
-               $o = sql_fetch_object($res);
-               return $o->cdesc;
-       }
-
-       /**
-         * Get the category id for a given category name
-         * 
-         * @param $name
-         *     category name
-         */
-       function getCategoryIdFromName($name) {
-               $res = sql_query('SELECT catid FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and cname="' . sql_real_escape_string($name) . '"');
-               if (sql_num_rows($res) > 0) {
-                       $o = sql_fetch_object($res);
-                       return $o->catid;
-               } else {
-                       return $this->getDefaultCategory();
-               }
-       }
-
-       /**
-         * Get the the setting for the line break handling
-         * [should be named as getConvertBreaks()]
-         */
-       function convertBreaks() {
-               return $this->getSetting('bconvertbreaks');
-       }
-       
-       /**
-         * Set the the setting for the line break handling
-         * 
-         * @param $val
-         *     new value for bconvertbreaks
-         */
-       function setConvertBreaks($val) {
-               $this->setSetting('bconvertbreaks',$val);
-       }
-
-       /**
-         * Insert a javascript that includes information about the settings
-         * of an author:  ConvertBreaks, MediaUrl and AuthorId
-         * 
-         * @param $authorid
-         *     id of the author
-         */    
-       function insertJavaScriptInfo($authorid = '') {
-               global $member, $CONF;
-
-               if ($authorid == '')
-                       $authorid = $member->getID();
-
-               ?>
-               <script type="text/javascript">
-                       setConvertBreaks(<?php echo  $this->convertBreaks() ? 'true' : 'false' ?>);
-                       setMediaUrl("<?php echo $CONF['MediaURL']?>");
-                       setAuthorId(<?php echo $authorid?>);
-               </script><?php  
-       }
-
-       /**
-         * Set the the setting for allowing to publish postings in the past
-         * 
-         * @param $val
-         *     new value for ballowpast
-         */
-       function setAllowPastPosting($val) {
-               $this->setSetting('ballowpast',$val);
-       }
-       
-       /**
-         * Get the the setting if it is allowed to publish postings in the past
-         * [should be named as getAllowPastPosting()]
-         */
-       function allowPastPosting() {
-               return $this->getSetting('ballowpast');
-       }
-
-       function getCorrectTime($t=0) {
-               if ($t == 0) $t = time();
-               return ($t + 3600 * $this->getTimeOffset());
-       }
-
-       function getName() {
-               return $this->getSetting('bname');
-       }
-
-       function getShortName() {
-               return $this->getSetting('bshortname');
-       }
-
-       function getMaxComments() {
-               return $this->getSetting('bmaxcomments');
-       }
-
-       function getNotifyAddress() {
-               return $this->getSetting('bnotify');
-       }
-
-       function getNotifyType() {
-               return $this->getSetting('bnotifytype');
-       }
-
-       function notifyOnComment() {
-               $n = $this->getNotifyType();
-               return (($n != 0) && (($n % 3) == 0));
-       }
-
-       function notifyOnVote() {
-               $n = $this->getNotifyType();
-               return (($n != 0) && (($n % 5) == 0));
-       }
-
-       function notifyOnNewItem() {
-               $n = $this->getNotifyType();
-               return (($n != 0) && (($n % 7) == 0));
-       }
-
-       function setNotifyType($val) {
-               $this->setSetting('bnotifytype',$val);
-       }
-
-
-       function getTimeOffset() {
-               return $this->getSetting('btimeoffset');
-       }
-
-       function commentsEnabled() {
-               return $this->getSetting('bcomments');
-       }
-
-       function getURL() {
-               return $this->getSetting('burl');
-       }
-
-       function getDefaultSkin() {
-               return $this->getSetting('bdefskin');
-       }
-
-       function getUpdateFile() {
-               return $this->getSetting('bupdate');
-       }
-
-       function getDescription() {
-               return $this->getSetting('bdesc');
-       }
-
-       function isPublic() {
-               return $this->getSetting('bpublic');
-       }
-
-       function emailRequired() {
-               return $this->getSetting('breqemail');
-       }
-
-       function getSearchable() {
-               return $this->getSetting('bincludesearch');
-       }
-
-       function getDefaultCategory() {
-               return $this->getSetting('bdefcat');
-       }
-
-       function setPublic($val) {
-               $this->setSetting('bpublic',$val);
-       }
-
-       function setSearchable($val) {
-               $this->setSetting('bincludesearch',$val);
-       }
-
-       function setDescription($val) {
-               $this->setSetting('bdesc',$val);
-       }
-
-       function setUpdateFile($val) {
-               $this->setSetting('bupdate',$val);
-       }
-
-       function setDefaultSkin($val) {
-               $this->setSetting('bdefskin',$val);
-       }
-
-       function setURL($val) {
-               $this->setSetting('burl',$val);
-       }
-
-       function setName($val) {
-               $this->setSetting('bname',$val);
-       }
-
-       function setShortName($val) {
-               $this->setSetting('bshortname',$val);
-       }
-
-       function setCommentsEnabled($val) {
-               $this->setSetting('bcomments',$val);
-       }
-
-       function setMaxComments($val) {
-               $this->setSetting('bmaxcomments',$val);
-       }
-
-       function setNotifyAddress($val) {
-               $this->setSetting('bnotify',$val);
-       }
-
-       function setEmailRequired($val) {
-               $this->setSetting('breqemail',$val);
-       }
-
-       function setTimeOffset($val) {
-               // check validity of value
-               // 1. replace , by . (common mistake)
-               $val = str_replace(',','.',$val);
-               // 2. cast to float or int
-               if (is_numeric($val) && strstr($val,'.5')) {
-                       $val = (float) $val;
-               } else {
-                       $val = intval($val);
-               }
-
-               $this->setSetting('btimeoffset',$val);
-       }
-
-       function setDefaultCategory($val) {
-               $this->setSetting('bdefcat',$val);
-       }
-
-       function getSetting($key) {
-               return $this->settings[$key];
-       }
-
-       function setSetting($key,$value) {
-               $this->settings[$key] = $value;
-       }
-
-       /**
-        * BLOG::addTeamMember()
-        * Tries to add a member to the team. 
-        * Returns false if the member was already on the team
-        * 
-        * @param       Integer $memberid       id for member
-        * @param       Boolean $admin  super-admin or not
-        * @return      Boolean Success/Fail
-        */
-       function addTeamMember($memberid, $admin)
-       {
-               global $manager;
-               
-               $memberid = intval($memberid);
-               $admin = intval($admin);
-               
-               // check if member is already a member
-               $tmem = MEMBER::createFromID($memberid);
-               
-               if ( $tmem->isTeamMember($this->getID()) )
-               {
-                       return 0;
-               }
-               
-               $manager->notify(
-                       'PreAddTeamMember',
-                       array(
-                               'blog' => &$this,
-                               'member' => &$tmem,
-                               'admin' => &$admin
-                       )
-               );
-               
-               // add to team
-               $query = "INSERT INTO %s (TMEMBER, TBLOG, TADMIN) ' . 'VALUES (%d, %d, %d)";
-               $query = sprintf($query, sql_table('team'), $memberid, $this->getID(), $admin);
-               sql_query($query);
-
-               $manager->notify(
-                       'PostAddTeamMember',
-                       array(
-                               'blog' => &$this,
-                               'member' => &$tmem,
-                               'admin' => $admin
-                       )
-               );
-               
-               $logMsg = sprintf(_TEAM_ADD_NEWTEAMMEMBER, $tmem->getDisplayName(), $memberid, $this->getName());
-               ACTIONLOG::add(INFO, $logMsg);
-               
-               return 1;
-       }
-
-       function getID() {
-               return (integer) $this->blogid;
-       }
-
-       /**
-         * Checks if a blog with a given shortname exists 
-         * Returns true if there is a blog with the given shortname (static)
-         * 
-         * @param $name
-         *     blog shortname
-         */
-       function exists($name) {
-               $r = sql_query('select * FROM '.sql_table('blog').' WHERE bshortname="'.sql_real_escape_string($name).'"');
-               return (sql_num_rows($r) != 0);
-       }
-
-       /**
-         * Checks if a blog with a given id exists 
-         * Returns true if there is a blog with the given ID (static)
-         * 
-         * @param $id
-         *     blog id
-         */
-       function existsID($id) {
-               $r = sql_query('select * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));
-               return (sql_num_rows($r) != 0);
-       }
-
-       /**
-         * flag there is a future post pending 
-         */
-       function setFuturePost() {
-               $query =  'UPDATE '.sql_table('blog')
-                           . " SET bfuturepost='1' WHERE bnumber=" . $this->getID();
-               sql_query($query);
-       }
-
-       /**
-         * clear there is a future post pending 
-         */
-       function clearFuturePost() {
-               $query =  'UPDATE '.sql_table('blog')
-                          . " SET bfuturepost='0' WHERE bnumber=" . $this->getID();
-               sql_query($query);
-       }
-
-       /**
-         * check if we should throw justPosted event 
-         */
-       function checkJustPosted() {
-               global $manager;
-
-               if ($this->settings['bfuturepost'] == 1) {
-                       $blogid = $this->getID();
-                       $result = sql_query("SELECT * FROM " . sql_table('item')
-                                 . " WHERE iposted=0 AND iblog=" . $blogid . " AND itime<NOW()");
-                       if (sql_num_rows($result) > 0) {
-                               // This $pinged is allow a plugin to tell other hook to the event that a ping is sent already
-                               // Note that the plugins's calling order is subject to thri order in the plugin list
-                               $pinged = false;
-                               $manager->notify(
-                                               'JustPosted',
-                                               array('blogid' => $blogid,
-                                               'pinged' => &$pinged
-                                               )
-                               );
-
-                               // clear all expired future posts
-                               sql_query("UPDATE " . sql_table('item') . " SET iposted='1' WHERE iblog=" . $blogid . " AND itime<NOW()");
-
-                               // check to see any pending future post, clear the flag is none
-                               $result = sql_query("SELECT * FROM " . sql_table('item')
-                                         . " WHERE iposted=0 AND iblog=" . $blogid);
-                               if (sql_num_rows($result) == 0) {
-                                       $this->clearFuturePost();
-                               }
-                       }
-               }
-       }
-
-       /**
-        * Shows the given list of items for this blog
-        *
-        * @param $itemarray
-        *              array of item numbers to be displayed
-        * @param $template
-        *              String representing the template _NAME_ (!)
-        * @param $highlight
-        *              contains a query that should be highlighted
-        * @param $comments
-        *              1=show comments 0=don't show comments
-        * @param $dateheads
-        *              1=show dateheads 0=don't show dateheads
-        * @param $showDrafts
-        *              0=do not show drafts 1=show drafts
-        * @param $showFuture
-        *              0=do not show future posts 1=show future posts
-        * @returns int
-        *              amount of items shown
-        */
-       function readLogFromList($itemarray, $template, $highlight = '', $comments = 1, $dateheads = 1,$showDrafts = 0, $showFuture = 0) {
-
-               $query = $this->getSqlItemList($itemarray,$showDrafts,$showFuture);
-
-               return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
-       }
-
-       /**
-        * BLOG::getSqlItemList()
-        * Returns the SQL query used to fill out templates for a list of items
-        * No LIMIT clause is added. (caller should add this if multiple pages are requested)
-        *
-        * @param       array   $itemarray              an array holding the item numbers of the items to be displayed
-        * @param integer       $showDrafts     0=do not show drafts 1=show drafts
-        * @param integer       $showFuture     0=do not show future posts 1=show future posts
-        * @return      string  either a full SQL query, or an empty string
-        * 
-        */
-       function getSqlItemList($itemarray,$showDrafts = 0,$showFuture = 0)
-       {
-               if ( !is_array($itemarray) )
-               {
-                       return '';
-               }
-               
-               $showDrafts = intval($showDrafts);
-               $showFuture = intval($showFuture);
-               $items = array();
-               
-               foreach ( $itemarray as $value )
-               {
-                       if ( intval($value) )
-                       {
-                               $items[] = intval($value);
-                       }
-               }
-               if ( !count($items) )
-               {
-                       return '';
-               }
-               
-               $i = count($items);
-               $query = '';
-               foreach ( $items as $value )
-               {
-                       $query .= '('
-                                       .       'SELECT'
-                                       .       ' i.inumber as itemid,'
-                                       .       ' i.ititle as title,'
-                                       .       ' i.ibody as body,'
-                                       .       ' m.mname as author,'
-                                       .       ' m.mrealname as authorname,'
-                                       .       ' i.itime,'
-                                       .       ' i.imore as more,'
-                                       .       ' m.mnumber as authorid,'
-                                       .       ' m.memail as authormail,'
-                                       .       ' m.murl as authorurl,'
-                                       .       ' c.cname as category,'
-                                       .       ' i.icat as catid,'
-                                       .       ' i.iclosed as closed';
-                       
-                       $query .= ' FROM '
-                                       . sql_table('item') . ' as i, '
-                                       . sql_table('member') . ' as m, '
-                                       . sql_table('category') . ' as c'
-                                       . ' WHERE'
-                                   .    ' i.iblog='.$this->blogid
-                                  . ' and i.iauthor=m.mnumber'
-                                  . ' and i.icat=c.catid';
-                       
-                       // exclude drafts       
-                       if ( !$showDrafts )
-                       {
-                               $query .= ' and i.idraft=0';
-                       }
-                       if ( !$showFuture )
-                       {
-                               // don't show future items
-                               $query .= ' and i.itime<="' . i18n::formatted_datetime('mysql', $this->getCorrectTime()) . '"';
-                       }
-                       
-                       $query .= ' and i.inumber='.intval($value);
-                       $query .= ')';
-                       $i--;
-                       if ($i) $query .= ' UNION ';
-               }
-               
-               return $query;
-       }
-}
+<?php\r
+\r
+/*\r
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
+ * Copyright (C) 2002-2012 The Nucleus Group\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * (see nucleus/documentation/index.html#license for more info)\r
+ */\r
+/**\r
+ * A class representing a blog and containing functions to get that blog shown\r
+ * on the screen\r
+ *\r
+ * @license http://nucleuscms.org/license.txt GNU General Public License\r
+ * @copyright Copyright (C) 2002-2009 The Nucleus Group\r
+ * @version $Id: BLOG.php 1624 2012-01-09 11:36:20Z sakamocchi $\r
+ */\r
+\r
+if ( !function_exists('requestVar') ) exit;\r
+require_once dirname(__FILE__) . '/ITEMACTIONS.php';\r
+\r
+class Blog\r
+{\r
+       // blog id\r
+       public $blogid;\r
+       \r
+       // After creating an object of the blog class, contains true if the BLOG object is\r
+       // valid (the blog exists)\r
+       public $isValid;\r
+       \r
+       // associative array, containing all blogsettings (use the get/set functions instead)\r
+       private $settings;\r
+       \r
+       // ID of currently selected category\r
+       private $selectedcatid;\r
+       \r
+       /**\r
+        * Blog::_\construct()\r
+        * Creates a new BLOG object for the given blog\r
+        *\r
+        * @param       integer $id     blogid\r
+        * @return      void\r
+        */\r
+       public function __construct($id)\r
+       {\r
+               global $catid;\r
+               \r
+               $this->blogid = (integer) $id;\r
+               $this->readSettings();\r
+               $this->setSelectedCategory($catid);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::readLog()\r
+        * Shows the given amount of items for this blog\r
+        *\r
+        * @param       string  $template       String representing the template _NAME_ (!)\r
+        * @param       integer $amountEntries  amount of entries to show\r
+        * @param       integer $startpos       offset from where items should be shown (e.g. 5 = start at fifth item)\r
+        * @return      integer amount of items shown\r
+        */\r
+       public function readLog($template, $amountEntries, $offset = 0, $startpos = 0)\r
+       {\r
+               return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);\r
+       }\r
+       \r
+       /**\r
+        * Blog::showArchive()\r
+        * Shows an archive for a given month\r
+        *\r
+        * @param       integer $year           year\r
+        * @param       integer $month          month\r
+        * @param       string  $template       String representing the template name to be used\r
+        * @return      void\r
+        */\r
+       public function showArchive($templatename, $year, $month=0, $day=0)\r
+       {\r
+               // create extra where clause for select query\r
+               if ( $day == 0 && $month != 0 )\r
+               {\r
+                       $timestamp_start = mktime(0,0,0,$month,1,$year);\r
+                       // also works when $month==12\r
+                       $timestamp_end = mktime(0,0,0,$month+1,1,$year);\r
+               }\r
+               elseif ( $month == 0 )\r
+               {\r
+                       $timestamp_start = mktime(0,0,0,1,1,$year);\r
+                       // also works when $month==12\r
+                       $timestamp_end = mktime(0,0,0,12,31,$year);\r
+               }\r
+               else\r
+               {\r
+                       $timestamp_start = mktime(0,0,0,$month,$day,$year);\r
+                       $timestamp_end = mktime(0,0,0,$month,$day+1,$year);\r
+               }\r
+               $extra_query = " and i.itime>=%s and i.itime<%s";\r
+               $extra_query = sprintf($extra_query, DB::formatDateTime($timestamp_start), DB::formatDateTime($timestamp_end));\r
+               \r
+               $this->readLogAmount($templatename,0,$extra_query,'',1,1);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setSelectedCategory()\r
+        * Sets the selected category by id (only when category exists)\r
+        * \r
+        * @param       integer $catid  ID for category\r
+        * @return      void\r
+        */\r
+       public function setSelectedCategory($catid)\r
+       {\r
+               if ( $this->isValidCategory($catid) || (intval($catid) == 0) )\r
+               {\r
+                       $this->selectedcatid = intval($catid);\r
+               }\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setSelectedCategoryByName()\r
+        * Sets the selected category by name\r
+        * \r
+        * @param       string  $catname        name of category\r
+        * @return      void\r
+        */\r
+       public function setSelectedCategoryByName($catname)\r
+       {\r
+               $this->setSelectedCategory($this->getCategoryIdFromName($catname));\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::getSelectedCategory()\r
+        * Returns the selected category\r
+        * \r
+        * @param       void\r
+        * @return      integer\r
+        */\r
+       public function getSelectedCategory()\r
+       {\r
+               return $this->selectedcatid;\r
+       }\r
+       \r
+       /**\r
+        * Shows the given amount of items for this blog\r
+        *\r
+        * @param       string  $template               string representing the template _NAME_ (!)\r
+        * @param       integer $amountEntries  amount of entries to show (0 = no limit)\r
+        * @param       string  $extraQuery             extra conditions to be added to the query\r
+        * @param       string  $highlight              contains a query that should be highlighted\r
+        * @param       integer $comments               1=show comments 0=don't show comments\r
+        * @param       integer $dateheads              1=show dateheads 0=don't show dateheads\r
+        * @param       integer $offset                 offset\r
+        * @return      integer amount of items shown\r
+        */\r
+       private function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0)\r
+       {\r
+               $query = $this->getSqlBlog($extraQuery);\r
+               \r
+               if ( $amountEntries > 0 )\r
+               {\r
+                       // $offset zou moeten worden:\r
+                       // (($startpos / $amountentries) + 1) * $offset ... later testen ...\r
+                       $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);\r
+               }\r
+               return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);\r
+       }\r
+       \r
+       /**\r
+        * Blog::showUsingQuery()\r
+        * Do the job for readLogAmmount\r
+        * \r
+        * @param       string  $templateName   template name\r
+        * @param       string  $query                  string for query\r
+        * @param       string  $highlight              string to be highlighted\r
+        * @param       integer $comments               the number of comments\r
+        * @param       boolean $dateheads              date header is needed or not\r
+        * @return      integer the number of rows as a result of mysql query\r
+        */\r
+       private function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1)\r
+       {\r
+               global $CONF, $manager, $currentTemplateName;\r
+               \r
+               $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');\r
+               if ( $lastVisit != 0 )\r
+               {\r
+                       $lastVisit = $this->getCorrectTime($lastVisit);\r
+               }\r
+               \r
+               // set templatename as global variable (so plugins can access it)\r
+               $currentTemplateName = $templateName;\r
+               $template =& $manager->getTemplate($templateName);\r
+               \r
+               // create parser object & action handler\r
+               $handler = new ItemActions($this);\r
+               $handler->setTemplate($template);\r
+               $handler->setHighlight($highlight);\r
+               $handler->setLastVisit($lastVisit);\r
+               $handler->setShowComments($comments);\r
+               \r
+               $parser = new Parser($handler);\r
+               \r
+               // execute query\r
+               $items = DB::getResult($query);\r
+               \r
+               // loop over all items\r
+               $old_date = 0;\r
+               foreach ( $items as $item )\r
+               {\r
+                       // string timestamp -> unix timestamp\r
+                       $item['timestamp'] = strtotime($item['itime']);\r
+                       \r
+                       // action handler needs to know the item we're handling\r
+                       $handler->setCurrentItem($item);\r
+                       \r
+                       // add date header if needed\r
+                       if ( $dateheads )\r
+                       {\r
+                               $new_date = date('dFY', $item['timestamp']);\r
+                               if ( $new_date != $old_date )\r
+                               {\r
+                                       // unless this is the first time, write date footer\r
+                                       $timestamp = $item['timestamp'];\r
+                                       if ( $old_date != 0 )\r
+                                       {\r
+                                               $oldTS = strtotime($old_date);\r
+                                               $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));\r
+                                               \r
+                                               if ( !in_array('DATE_FOOTER', $template) || empty($template['DATE_FOOTER']) )\r
+                                               {\r
+                                                       $tmp_footer = '';\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       $tmp_footer = i18n::formatted_datetime($template['DATE_FOOTER'], $oldTS);\r
+                                               }\r
+                                               $parser->parse($tmp_footer);\r
+                                               $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));\r
+                                       }\r
+                                       \r
+                                       $manager->notify('PreDateHead',array('blog' => &$this, 'timestamp' => $timestamp));\r
+                                       \r
+                                       // note, to use templatvars in the dateheader, the %-characters need to be doubled in\r
+                                       // order to be preserved by strftime\r
+                                       if ( !in_array('DATE_HEADER', $template) || empty($template['DATE_HEADER']) )\r
+                                       {\r
+                                               $tmp_header = '';\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               $tmp_header = i18n::formatted_datetime($template['DATE_HEADER'], $timestamp);\r
+                                       }\r
+                                       $parser->parse($tmp_header);\r
+                                       $manager->notify('PostDateHead',array('blog' => &$this, 'timestamp' => $timestamp));\r
+                               }\r
+                               $old_date = $new_date;\r
+                       }\r
+                       \r
+                       // parse item\r
+                       $parser->parse($template['ITEM_HEADER']);\r
+                       $manager->notify('PreItem', array('blog' => &$this, 'item' => &$item));\r
+                       $parser->parse($template['ITEM']);\r
+                       $manager->notify('PostItem', array('blog' => &$this, 'item' => &$item));\r
+                       $parser->parse($template['ITEM_FOOTER']);\r
+               }\r
+               \r
+               $numrows = $items->rowCount();\r
+               \r
+               // add another date footer if there was at least one item\r
+               if ( ($numrows > 0) && $dateheads )\r
+               {\r
+                       $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));\r
+                       $parser->parse($template['DATE_FOOTER']);\r
+                       $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));\r
+               }\r
+               \r
+               $items->closeCursor();\r
+               return $numrows;\r
+       }\r
+       \r
+       /**\r
+        * Blog::showOneitem()\r
+        * Simplified function for showing only one item\r
+        * \r
+        * @param       integer $itemid         ID for item\r
+        * @param       array   $template       template for item\r
+        * @param       string  $highlight      string for highlight\r
+        * @return      integer 1\r
+        */\r
+       public function showOneitem($itemid, $template, $highlight)\r
+       {\r
+               $extraQuery = ' and inumber=' . intval($itemid);\r
+               \r
+               return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);\r
+       }\r
+       \r
+       /**\r
+        * Blog::addItem()\r
+        * Adds an item to this blog\r
+        * \r
+        * @param       integer         $catid  ID for category\r
+        * @param       string          $title  ID for \r
+        * @param       string          $body   text for body\r
+        * @param       string          $more   text for more\r
+        * @param       integer         $blogid ID for blog\r
+        * @param       integer         $authorid       ID for author\r
+        * @param       timestamp       $timestamp      UNIX timestamp for post\r
+        * @param       boolean         $closed opened or closed\r
+        * @param       boolean         $draft  draft or not\r
+        * @param       boolean         $posted posted or not\r
+        * @return      integer ID for added item\r
+        */\r
+       function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft, $posted='1')\r
+       {\r
+               global $manager;\r
+               \r
+               $blogid         = (integer) $blogid;\r
+               $authorid       = (integer) $authorid;\r
+               $title          = $title;\r
+               $body           = $body;\r
+               $more           = $more;\r
+               $catid          = intval($catid);\r
+               \r
+               // convert newlines to <br />\r
+               if ( $this->convertBreaks() )\r
+               {\r
+                       $body = addBreaks($body);\r
+                       $more = addBreaks($more);\r
+               }\r
+\r
+               if ( $closed != '1' )\r
+               {\r
+                       $closed = '0';\r
+               }\r
+               if ( $draft != '0' )\r
+               {\r
+                       $draft = '1';\r
+               }\r
+               \r
+               if ( !$this->isValidCategory($catid) )\r
+               {\r
+                       $catid = $this->getDefaultCategory();\r
+               }\r
+               \r
+               $isFuture = 0;\r
+               if ( $timestamp > $this->getCorrectTime() )\r
+               {\r
+                       $isFuture = 1;\r
+               }\r
+               \r
+               $timestamp = date('Y-m-d H:i:s',$timestamp);\r
+               \r
+               $manager->notify('PreAddItem',array('title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$this, 'authorid' => &$authorid, 'timestamp' => &$timestamp, 'closed' => &$closed, 'draft' => &$draft, 'catid' => &$catid));\r
+               \r
+               $ititle = DB::quoteValue($title);\r
+               $ibody = DB::quoteValue($body);\r
+               $imore = DB::quoteValue($more);\r
+               $timestamp = DB::formatDateTime(strtotime($timestamp));\r
+               \r
+               $query = "INSERT INTO %s (ITITLE, IBODY, IMORE, IBLOG, IAUTHOR, ITIME, ICLOSED, IDRAFT, ICAT, IPOSTED) VALUES (%s, %s, %s, %d, %d, %s, %s, %s, %s, %s)";\r
+               $query = sprintf($query, sql_table('item'), $ititle, $ibody, $imore, $blogid, $authorid, $timestamp, $closed, $draft, $catid, $posted);\r
+               DB::execute($query);\r
+               $itemid = DB::getInsertId();\r
+               \r
+               $manager->notify('PostAddItem',array('itemid' => $itemid));\r
+               \r
+               if ( !$draft )\r
+               {\r
+                       $this->updateUpdateFile();\r
+               }\r
+               // send notification mail\r
+               if ( !$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem() )\r
+               {\r
+                       $this->sendNewItemNotification($itemid, $title, $body);\r
+               }\r
+               return $itemid;\r
+       }\r
+       \r
+       /**\r
+        * Blog::sendNewItemNotification()\r
+        * Send a new item notification to the notification list\r
+        * \r
+        * @param       string  $itemid ID of the item\r
+        * @param       string  $title  title of the item\r
+        * @param       string  $body   body of the item\r
+        * @return      void\r
+        */\r
+       public function sendNewItemNotification($itemid, $title, $body)\r
+       {\r
+               global $CONF, $member;\r
+               \r
+               $ascii = Entity::anchor_footnoting($body);\r
+               \r
+               $message = _NOTIFY_NI_MSG . " \n";\r
+               $temp = parse_url($CONF['Self']);\r
+               if ( $temp['scheme'] )\r
+               {\r
+                       $message .= Link::create_item_link($itemid) . "\n\n";\r
+               }\r
+               else\r
+               {\r
+                       $tempurl = $this->getURL();\r
+                       if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )\r
+                       {\r
+                               $message .= $tempurl . '?itemid=' . $itemid . "\n\n";\r
+                       }\r
+                       else\r
+                       {\r
+                               $message .= $tempurl . '/?itemid=' . $itemid . "\n\n";\r
+                       }\r
+               }\r
+               $message .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";\r
+               $message .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";\r
+               $message .= NOTIFICATION::get_mail_footer();\r
+               \r
+               $subject = $this->getName() . ': ' . _NOTIFY_NI_TITLE;\r
+               \r
+               $from = $member->getNotifyFromMailAddress();\r
+               \r
+               NOTIFICATION::mail($this->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::createNewCategory()\r
+        * Creates a new category for this blog\r
+        *\r
+        * @param       string  $catName                name of the new category. When empty, a name is generated automatically (starting with newcat)\r
+        * @param       string  $catDescription description of the new category. Defaults to 'New Category'\r
+        * @return      integer ID for new category on success. 0 on failure\r
+        */\r
+       public function createNewCategory($catName = '', $catDescription = _CREATED_NEW_CATEGORY_DESC)\r
+       {\r
+               global $member, $manager;\r
+               \r
+               if ( !$member->blogAdminRights($this->blogid) )\r
+               {\r
+                       return 0;\r
+               }\r
+               \r
+               // generate\r
+               if ( $catName == '' )\r
+               {\r
+                       $catName = _CREATED_NEW_CATEGORY_NAME;\r
+                       $i = 1;\r
+                       \r
+                       $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);\r
+                       while ( $res->rowCount() > 0 )\r
+                       {\r
+                               $i++;\r
+                               $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);\r
+                       }\r
+                       \r
+                       $catName = $catName . $i;\r
+               }\r
+               \r
+               $data = array(\r
+                       'blog'                  => &$this,\r
+                       'name'                  => &$catName,\r
+                       'description'   => $catDescription\r
+               );\r
+               $manager->notify('PreAddCategory', $data);\r
+               \r
+               $query = "INSERT INTO %s (cblog, cname, cdesc) VALUES (%d, %s, %s)";\r
+               $query = sprintf($query, sql_table('category'), (integer) $this->blogid, DB::quoteValue($catName), DB::quoteValue($catDescription));\r
+               DB::execute($query);\r
+               $catid = DB::getInsertId();\r
+               \r
+               $data = array(\r
+                       'blog'                  => &$this,\r
+                       'name'                  => $catName,\r
+                       'description'   => $catDescription,\r
+                       'catid'                 => $catid\r
+               );\r
+               $manager->notify('PostAddCategory', $data);\r
+               \r
+               return $catid;\r
+       }\r
+       \r
+       /**\r
+        * Blog::search()\r
+        * Searches all months of this blog for the given query\r
+        *\r
+        * @param       string  $query                  search query\r
+        * @param       array   $template               template to be used (__NAME__ of the template)\r
+        * @param       integer $amountMonths   max amount of months to be search (0 = all)\r
+        * @param       integer $maxresults             max number of results to show\r
+        * @param       integer $startpos               offset\r
+        * @return      amount of hits found\r
+        */\r
+       public function search($query, $template, $amountMonths, $maxresults, $startpos) {\r
+               global $CONF, $manager;\r
+               \r
+               $highlight      = '';\r
+               $sqlquery       = $this->getSqlSearch($query, $amountMonths, $highlight);\r
+               \r
+               if ( $sqlquery == '' )\r
+               {\r
+                       // no query -> show everything\r
+                       $extraquery = '';\r
+                       $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);\r
+               }\r
+               else\r
+               {\r
+                       // add LIMIT to query (to split search results into pages)\r
+                       if ( intval($maxresults > 0) )\r
+                       {\r
+                               $sqlquery .= ' LIMIT ' . intval($startpos) . ',' . intval($maxresults);\r
+                       }\r
+                       \r
+                       // show results\r
+                       $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);\r
+                       \r
+                       // when no results were found, show a message\r
+                       if ( $amountfound == 0 )\r
+                       {\r
+                               $template =& $manager->getTemplate($template);\r
+                               $vars = array(\r
+                                       'query'         => Entity::hsc($query),\r
+                                       'blogid'        => $this->blogid\r
+                               );\r
+                               echo Template::fill($template['SEARCH_NOTHINGFOUND'], $vars);\r
+                       }\r
+               }\r
+               return $amountfound;\r
+       }\r
+       \r
+       /**\r
+        * Blog::getSqlSearch()\r
+        * Returns an SQL query to use for a search query\r
+        * No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
+        *\r
+        * @param       string  $query                  search query\r
+        * @param       integer $amountMonths   amount of months to search back. Default = 0 = unlimited\r
+        * @param       string  $mode                   either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query\r
+        * @return      string  $highlight              words to highlight (out parameter)\r
+        * @return      string  either a full SQL query, or an empty string (if querystring empty)\r
+        */\r
+       public function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')\r
+       {\r
+               $searchclass = new Search($query);\r
+               \r
+               $highlight       = $searchclass->inclusive;\r
+               \r
+               // if querystring is empty, return empty string\r
+               if ( $searchclass->inclusive == '' )\r
+               {\r
+                       return '';\r
+               }\r
+               \r
+               $where  = $searchclass->boolean_sql_where('ititle,ibody,imore');\r
+               $select = $searchclass->boolean_sql_select('ititle,ibody,imore');\r
+               \r
+               // get list of blogs to search\r
+               $blogs          = $searchclass->blogs;  // array containing blogs that always need to be included\r
+               $blogs[]        = $this->blogid;                // also search current blog (duh)\r
+               $blogs          = array_unique($blogs); // remove duplicates\r
+               $selectblogs = '';\r
+               if ( count($blogs) > 0 )\r
+               {\r
+                       $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';\r
+               }\r
+               \r
+               if ( $mode == '' )\r
+               {\r
+                       $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, i.itime, i.imore as more, i.icat as catid, i.iclosed as closed,\r
+                               m.mname as author, m.mrealname as authorname, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl,\r
+                               c.cname as category';\r
+                       \r
+                       if ( $select )\r
+                       {\r
+                               $query .= ', '.$select. ' as score ';\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       $query = 'SELECT COUNT(*) as result ';\r
+               }\r
+               \r
+               $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'\r
+                               . ' WHERE i.iauthor=m.mnumber'\r
+                               . ' and i.icat=c.catid'\r
+                               // exclude drafts\r
+                               . ' and i.idraft=0'\r
+                               . $selectblogs\r
+                                       // don't show future items\r
+                               . ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime())\r
+                               . ' and '.$where;\r
+               \r
+               // take into account amount of months to search\r
+               if ( $amountMonths > 0 )\r
+               {\r
+                       $localtime = getdate($this->getCorrectTime());\r
+                       $timestamp_start = mktime(0,0,0,$localtime['mon'] - $amountMonths,1,$localtime['year']);\r
+                       $query .= ' and i.itime>' . DB::formatDateTime($timestamp_start);\r
+               }\r
+               \r
+               if ( $mode == '' )\r
+               {\r
+                       if ( $select )\r
+                       {\r
+                               $query .= ' ORDER BY score DESC';\r
+                       }\r
+                       else\r
+                       {\r
+                               $query .= ' ORDER BY i.itime DESC ';\r
+                       }\r
+               }\r
+               \r
+               return $query;\r
+       }\r
+       \r
+       /**\r
+        * Blog::getSqlBlog()\r
+        * Returns the SQL query that's normally used to display the blog items on the index type skins\r
+        * No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
+        *\r
+        * @param       string  $extraQuery     extra query string\r
+        * @param       string  $mode           either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query\r
+        * @return      string  either a full SQL query, or an empty string\r
+        */\r
+       public function getSqlBlog($extraQuery, $mode = '')\r
+       {\r
+               if ( $mode == '' )\r
+               {\r
+                       $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author,\r
+                               m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail,\r
+                               m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';\r
+               }\r
+               else\r
+               {\r
+                       $query = 'SELECT COUNT(*) as result ';\r
+               }\r
+               \r
+               $query  .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'\r
+                               . ' WHERE i.iblog='.$this->blogid\r
+                               . ' and i.iauthor=m.mnumber'\r
+                               . ' and i.icat=c.catid'\r
+                               . ' and i.idraft=0' // exclude drafts\r
+                               . ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime()); // don't show future items\r
+               \r
+               if ( $this->selectedcatid )\r
+               {\r
+                       $query .= ' and i.icat=' . $this->selectedcatid . ' ';\r
+               }\r
+               \r
+               $query .= $extraQuery;\r
+               \r
+               if ( $mode == '' )\r
+               {\r
+                       $query .= ' ORDER BY i.itime DESC';\r
+               }\r
+               return $query;\r
+       }\r
+       \r
+       /**\r
+        * Blog::showArchiveList()\r
+        * Shows the archivelist using the given template\r
+        * \r
+        * @param       string  $template       template name\r
+        * @param       string  $mode   year/month/day\r
+        * @param       integer $limit  limit of record count\r
+        * @return      void\r
+        */\r
+       public function showArchiveList($template, $mode = 'month', $limit = 0)\r
+       {\r
+               global $CONF, $catid, $manager;\r
+               \r
+               if ( !isset ($linkparams) )\r
+               {\r
+                       $linkparams = array();\r
+               }\r
+               \r
+               if ( $catid )\r
+               {\r
+                       $linkparams = array('catid' => $catid);\r
+               }\r
+               \r
+               $template =& $manager->getTemplate($template);\r
+               $data['blogid'] = $this->blogid;\r
+               \r
+               if ( !array_key_exists('ARCHIVELIST_HEADER', $template) || !$template['ARCHIVELIST_HEADER'] )\r
+               {\r
+                       $tplt = '';\r
+               }\r
+               else\r
+               {\r
+                       $tplt = $template['ARCHIVELIST_HEADER'];\r
+               }\r
+               \r
+               echo Template::fill($tplt, $data);\r
+               \r
+               $query = 'SELECT itime, SUBSTRING(itime,1,4) AS Year, SUBSTRING(itime,6,2) AS Month, SUBSTRING(itime,9,2) AS Day'\r
+                               . ' FROM '.sql_table('item')\r
+                               . ' WHERE iblog=' . $this->blogid\r
+                               . ' AND itime <=' . DB::formatDateTime($this->getCorrectTime()) // don't show future items!\r
+                               . ' AND idraft=0'; // don't show draft items\r
+               \r
+               if ( $catid )\r
+               {\r
+                       $query .= ' and icat=' . intval($catid);\r
+               }\r
+               \r
+               $query .= ' GROUP BY Year';\r
+               if ( $mode == 'month' || $mode == 'day' )\r
+               {\r
+                       $query .= ', Month';\r
+               }\r
+               if ( $mode == 'day' )\r
+               {\r
+                       $query .= ', Day';\r
+               }\r
+               \r
+               $query .= ' ORDER BY itime DESC';\r
+               \r
+               if ( $limit > 0 )\r
+               {\r
+                       $query .= ' LIMIT ' . intval($limit);\r
+               }\r
+               \r
+               $res = DB::getResult($query);\r
+               foreach ( $res as $current )\r
+               {\r
+                       /* string time -> unix timestamp */\r
+                       $current['itime'] = strtotime($current['itime']);\r
+                       \r
+                       if ( $mode == 'day' )\r
+                       {\r
+                               $archivedate = date('Y-m-d',$current['itime']);\r
+                               $archive['day'] = date('d',$current['itime']);\r
+                               $data['day'] = date('d',$current['itime']);\r
+                               $data['month'] = date('m',$current['itime']);\r
+                               $archive['month'] = $data['month'];\r
+                       }\r
+                       elseif ( $mode == 'year' )\r
+                       {\r
+                               $archivedate = date('Y',$current['itime']);\r
+                               $data['day'] = '';\r
+                               $data['month'] = '';\r
+                               $archive['day'] = '';\r
+                               $archive['month'] = '';\r
+                       }\r
+                       else\r
+                       {\r
+                               $archivedate = date('Y-m',$current['itime']);\r
+                               $data['month'] = date('m',$current['itime']);\r
+                               $archive['month'] = $data['month'];\r
+                               $data['day'] = '';\r
+                               $archive['day'] = '';\r
+                       }\r
+                       \r
+                       $data['year'] = date('Y',$current['itime']);\r
+                       $archive['year'] = $data['year'];\r
+                       $data['archivelink'] = Link::create_archive_link($this->blogid,$archivedate,$linkparams);\r
+                       \r
+                       $manager->notify('PreArchiveListItem', array('listitem' => &$data));\r
+                       \r
+                       $temp = Template::fill($template['ARCHIVELIST_LISTITEM'],$data);\r
+                       echo i18n::formatted_datetime($temp, $current['itime']);\r
+                       return;\r
+               }\r
+               \r
+               $res->closeCursor();\r
+               \r
+               if ( !array_key_exists('ARCHIVELIST_FOOTER', $template) || !$template['ARCHIVELIST_FOOTER'] )\r
+               {\r
+                       $tplt = '';\r
+               }\r
+               else\r
+               {\r
+                       $tplt = $template['ARCHIVELIST_FOOTER'];\r
+               }\r
+               \r
+               echo Template::fill($tplt, $data);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::showCategoryList()\r
+        * Shows the list of categories using a given template\r
+        * \r
+        * @param       string  $template       Template Name\r
+        * @return      void\r
+        */\r
+       public function showCategoryList($template)\r
+       {\r
+               global $CONF, $archive, $archivelist, $manager;\r
+               \r
+               /*\r
+                * determine arguments next to catids\r
+                * I guess this can be done in a better way, but it works\r
+                */\r
+               $linkparams = array();\r
+               if ( $archive )\r
+               {\r
+                       $blogurl = Link::create_archive_link($this->blogid, $archive, '');\r
+                       $linkparams['blogid'] = $this->blogid;\r
+                       $linkparams['archive'] = $archive;\r
+               }\r
+               else if ( $archivelist )\r
+               {\r
+                       $blogurl = Link::create_archivelist_link($this->blogid, '');\r
+                       $linkparams['archivelist'] = $archivelist;\r
+               }\r
+               else\r
+               {\r
+                       $blogurl = Link::create_blogid_link($this->blogid, '');\r
+                       $linkparams['blogid'] = $this->blogid;\r
+               }\r
+               \r
+               $template =& $manager->getTemplate($template);\r
+               \r
+               //: Change: Set nocatselected variable\r
+               if ( $this->selectedcatid )\r
+               {\r
+                       $nocatselected = 'no';\r
+               }\r
+               else\r
+               {\r
+                       $nocatselected = 'yes';\r
+               } \r
+               \r
+               $args = array(\r
+                       'blogid'        => $this->blogid,\r
+                       'blogurl'       => $blogurl,\r
+                       'self'          => $CONF['Self'],\r
+                       'catiscurrent'  => $nocatselected, // Change: Set catiscurrent template variable for header\r
+                       'currentcat'    => $nocatselected \r
+               );\r
+               \r
+               /* output header of category list item */\r
+               if ( !array_key_exists('CATLIST_HEADER', $template) || empty($template['CATLIST_HEADER']) )\r
+               {\r
+                       echo Template::fill(NULL, $args);\r
+               }\r
+               else\r
+               {\r
+                       echo Template::fill($template['CATLIST_HEADER'], $args);\r
+               }\r
+               \r
+               $query = "SELECT catid, cdesc as catdesc, cname as catname FROM %s WHERE cblog=%d ORDER BY cname ASC;";\r
+               $query = sprintf($query, sql_table('category'), (integer) $this->blogid);\r
+               $res = DB::getResult($query);\r
+               \r
+               foreach ( $res as $data )\r
+               {\r
+                       $args = array(\r
+                               'catid' => $data['catid'],\r
+                               'name'  => $data['catname'],\r
+                               'extra' => $linkparams\r
+                       );\r
+                       \r
+                       $data['blogid']         = $this->blogid;\r
+                       $data['blogurl']        = $blogurl;\r
+                       $data['catlink']        = Link::create_link('category', $args);\r
+                       $data['self']           = $CONF['Self'];\r
+                       \r
+                       // this gives catiscurrent = no when no category is selected.\r
+                       $data['catiscurrent'] = 'no';\r
+                       $data['currentcat'] = 'no';\r
+                       \r
+                       if ( $this->selectedcatid )\r
+                       {\r
+                               if ( $this->selectedcatid == $data['catid'] )\r
+                               {\r
+                                       $data['catiscurrent']   = 'yes';\r
+                                       $data['currentcat']             = 'yes';\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               global $itemid;\r
+                               if ( (integer) $itemid && $manager->existsItem((integer) $itemid, 0, 0) )\r
+                               {\r
+                                       $iobj   =& $manager->getItem($itemid, 0, 0);\r
+                                       $cid    = $iobj['catid'];\r
+                                       \r
+                                       if ( $cid == $data['catid'] )\r
+                                       {\r
+                                               $data['catiscurrent']   = 'yes';\r
+                                               $data['currentcat']             = 'yes';\r
+                                       }\r
+                               }\r
+                       }\r
+                       \r
+                       $manager->notify('PreCategoryListItem', array('listitem' => &$data));\r
+                       \r
+                       if ( !array_key_exists('CATLIST_LISTITEM', $template) || empty($template['CATLIST_LISTITEM']))\r
+                       {\r
+                               echo Template::fill(NULL, $data);\r
+                       }\r
+                       else\r
+                       {\r
+                               echo Template::fill($template['CATLIST_LISTITEM'], $data);\r
+                       }\r
+               }\r
+               \r
+               $res->closeCursor();\r
+               \r
+               $args = array(\r
+                       'blogid'                => $this->blogid,\r
+                       'blogurl'               => $blogurl,\r
+                       'self'                  => $CONF['Self'],\r
+                       'catiscurrent'  => $nocatselected, //: Change: Set catiscurrent template variable for footer\r
+                       'currentcat'    => $nocatselected\r
+               );\r
+               \r
+               if ( !array_key_exists('CATLIST_FOOTER', $template) || empty($template['CATLIST_FOOTER']))\r
+               {\r
+                       echo Template::fill(NULL, $args);\r
+               }\r
+               else\r
+               {\r
+                       echo Template::fill($template['CATLIST_FOOTER'], $args);\r
+               }\r
+               \r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::showBlogList()\r
+        * Shows a list of all blogs in the system using a given template\r
+        * ordered by number, name, shortname or description\r
+        * in ascending or descending order\r
+        * \r
+        * @param       string  $template       tempalte name\r
+        * @param       string  $bnametype      bname/bshortname\r
+        * @param       string  $orderby        string for 'ORDER BY' SQL\r
+        * @param       string  $direction      ASC/DESC\r
+        * @return      void\r
+        */\r
+       public function showBlogList($template, $bnametype, $orderby, $direction)\r
+       {\r
+               global $CONF, $manager;\r
+               \r
+               switch ( $orderby )\r
+               {\r
+                       case 'number':\r
+                               $orderby='bnumber';\r
+                               break;\r
+                       case 'name':\r
+                               $orderby='bname';\r
+                               break;\r
+                       case 'shortname':\r
+                               $orderby='bshortname';\r
+                               break;\r
+                       case 'description':\r
+                               $orderby='bdesc';\r
+                               break;\r
+                       default:\r
+                               $orderby='bnumber';\r
+                               break;\r
+               }\r
+               \r
+               $direction=strtolower($direction);\r
+               switch ( $direction )\r
+               {\r
+                       case 'asc':\r
+                               $direction='ASC';\r
+                               break;\r
+                       case 'desc':\r
+                               $direction='DESC';\r
+                               break;\r
+                       default:\r
+                               $direction='ASC';\r
+                               break;\r
+               }\r
+               \r
+               $template =& $manager->getTemplate($template);\r
+               \r
+               if ( array_key_exists('BLOGLIST_HEADER', $template) && !empty($template['BLOGLIST_HEADER']) )\r
+               {\r
+                       $vars = array(\r
+                               'sitename'      => $CONF['SiteName'],\r
+                               'siteurl'       => $CONF['IndexURL']\r
+                       );\r
+                       \r
+                       echo Template::fill($template['BLOGLIST_HEADER'], $vars);\r
+               }\r
+               \r
+               if ( array_key_exists('BLOGLIST_LISTITEM', $template) && !empty($template['BLOGLIST_LISTITEM']) )\r
+               {\r
+                       $query = 'SELECT bnumber, bname, bshortname, bdesc, burl FROM '.sql_table('blog').' ORDER BY '.$orderby.' '.$direction;\r
+                       $res = DB::getResult($query);\r
+                       \r
+                       foreach ( $res as $data )\r
+                       {\r
+                               $list = array();\r
+                               $list['bloglink'] = Link::create_blogid_link($data['bnumber']);\r
+                               $list['blogdesc'] = $data['bdesc'];\r
+                               $list['blogurl'] = $data['burl'];\r
+                               \r
+                               if ( $bnametype == 'shortname' )\r
+                               {\r
+                                       $list['blogname'] = $data['bshortname'];\r
+                               }\r
+                               else\r
+                               {\r
+                                       /* all other cases */\r
+                                       $list['blogname'] = $data['bname'];\r
+                               }\r
+                               \r
+                               $manager->notify('PreBlogListItem',array('listitem' => &$list));\r
+                               \r
+                               echo Template::fill($template['BLOGLIST_LISTITEM'], $list);\r
+                       }\r
+                       \r
+                       $res->closeCursor();\r
+               }\r
+               \r
+               \r
+               if ( array_key_exists('BLOGLIST_FOOTER', $template) && !empty($template['BLOGLIST_FOOTER']) )\r
+               {\r
+                       $vars = array(\r
+                               'sitename' => $CONF['SiteName'],\r
+                               'siteurl' => $CONF['IndexURL']\r
+                       );\r
+                       echo Template::fill($template['BLOGLIST_FOOTER']);\r
+               }\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::readSettings()\r
+        * Read the blog settings\r
+        * \r
+        * @param       void\r
+        * @return      void\r
+        */\r
+       public function readSettings()\r
+       {\r
+               $query =  'SELECT * FROM %s WHERE bnumber=%d;';\r
+               $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);\r
+               $res = DB::getResult($query);\r
+               \r
+               $this->isValid = ($res->rowCount() > 0);\r
+               if ( $this->isValid )\r
+               {\r
+                       $this->settings = $res->fetch(PDO::FETCH_ASSOC);\r
+               }\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::writeSettings()\r
+        * Write the blog settings\r
+        */\r
+       public function writeSettings()\r
+       {\r
+               // (can't use floatval since not available prior to PHP 4.2)\r
+               $offset = $this->getTimeOffset();\r
+               if ( !is_float($offset) )\r
+               {\r
+                       $offset = (integer) $offset;\r
+               }\r
+               \r
+               $query =  'UPDATE '.sql_table('blog')\r
+                          . ' SET bname=' . DB::quoteValue($this->getName()) . ','\r
+                          . '     bshortname='. DB::quoteValue($this->getShortName()) . ','\r
+                          . '     bcomments='. intval($this->commentsEnabled()) . ','\r
+                          . '     bmaxcomments=' . intval($this->getMaxComments()) . ','\r
+                          . '     btimeoffset=' . $offset . ','\r
+                          . '     bpublic=' . intval($this->isPublic()) . ','\r
+                          . '     breqemail=' . intval($this->emailRequired()) . ','\r
+                          . '     bconvertbreaks=' . intval($this->convertBreaks()) . ','\r
+                          . '     ballowpast=' . intval($this->allowPastPosting()) . ','\r
+                          . '     bnotify=' . DB::quoteValue($this->getNotifyAddress()) . ','\r
+                          . '     bnotifytype=' . intval($this->getNotifyType()) . ','\r
+                          . '     burl=' . DB::quoteValue($this->getURL()) . ','\r
+                          . '     bupdate=' . DB::quoteValue($this->getUpdateFile()) . ','\r
+                          . '     bdesc=' . DB::quoteValue($this->getDescription()) . ','\r
+                          . '     bdefcat=' . intval($this->getDefaultCategory()) . ','\r
+                          . '     bdefskin=' . intval($this->getDefaultSkin()) . ','\r
+                          . '     bincludesearch=' . intval($this->getSearchable())\r
+                          . ' WHERE bnumber=' . intval($this->blogid);\r
+               DB::execute($query);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::updateUpdatefile()\r
+        * Update the update file if requested\r
+        * \r
+        * @param       void\r
+        * @return      void\r
+        */\r
+       public function updateUpdatefile()\r
+       {\r
+               if ( $this->getUpdateFile() )\r
+               {\r
+                       $f_update = fopen($this->getUpdateFile(), 'w');\r
+                       fputs($f_update,$this->getCorrectTime());\r
+                       fclose($f_update);\r
+               }\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::isValidCategory()\r
+        * Check if a category with a given catid is valid\r
+        * \r
+        * @param       integer $catid  ID for category\r
+        * @return      boolean exists or not\r
+        */\r
+       public function isValidCategory($catid)\r
+       {\r
+               $query = 'SELECT * FROM %s WHERE cblog=%d and catid=%d;';\r
+               $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);\r
+               $res = DB::getResult($query);\r
+               return ($res->rowCount() != 0);\r
+       }\r
+       \r
+       /**\r
+        * Blog::getCategoryName()\r
+        * Get the category name for a given catid\r
+        * \r
+        * @param       integer $catid  ID for category\r
+        * @return      string  name of category\r
+        */\r
+       public function getCategoryName($catid)\r
+       {\r
+               $query = 'SELECT cname FROM %s WHERE cblog=%d and catid=%d;';\r
+               $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);\r
+               $res = DB::getValue($query);\r
+               return $res;\r
+       }\r
+       \r
+       /**\r
+        * Blog::getCategoryDesc()\r
+        * Get the category description for a given catid\r
+        * \r
+        * @param $catid\r
+        *      category id\r
+        */\r
+       public function getCategoryDesc($catid)\r
+       {\r
+               $query = 'SELECT cdesc FROM %s WHERE cblog=%d and catid=%d;';\r
+               $query = sprintf($querym, sql_table('category'), (integer) $this->blogid, (integer) $catid);\r
+               $res = DB::getValue();\r
+               return $res;\r
+       }\r
+       \r
+       /**\r
+        * Blog::getCategoryIdFromName\r
+        * Get the category id for a given category name\r
+        * \r
+        * @param       string  $name   category name\r
+        * @return      ID for category\r
+        */\r
+       public function getCategoryIdFromName($name)\r
+       {\r
+               $query = 'SELECT catid FROM %s WHERE cblog=%d and cname=%s;';\r
+               $query = sprintf($query, sql_table('category'), (integer) $this->blogid, DB::quoteValue($name));\r
+               \r
+               $res = DB::getValue();\r
+               if ( !$res )\r
+               {\r
+                       return $this->getDefaultCategory();\r
+               }\r
+               return $res;\r
+       }\r
+       \r
+       /**\r
+        * Blog::insertJavaScriptInfo()\r
+        * Insert a javascript that includes information about the settings\r
+        * of an author:  ConvertBreaks, MediaUrl and AuthorId\r
+        * \r
+        * @param       $authorid       id of the author\r
+        */\r
+       public function insertJavaScriptInfo($authorid = '')\r
+       {\r
+               global $member, $CONF;\r
+               \r
+               if ( $authorid == '' )\r
+               {\r
+                       $authorid = $member->getID();\r
+               }\r
+               \r
+               echo "<script type=\"text/javascript\">\n";\r
+               \r
+               if ( !$this->convertBreaks() )\r
+               {\r
+                       echo "setConvertBreaks(false);\n";\r
+               }\r
+               else\r
+               {\r
+                       echo "setConvertBreaks(true);\n";\r
+               }\r
+               echo "setMediaUrl('{$CONF['MediaURL']}');\n";\r
+               echo "setAuthorId('{$authorid}');\n";\r
+               echo "</script>\n";\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setAllowPastPosting()\r
+        * Set the the setting for allowing to publish postings in the past\r
+        * \r
+        * @param       boolean $val    new value for ballowpast\r
+        * @return      void\r
+        */\r
+       public function setAllowPastPosting($val)\r
+       {\r
+               $this->setSetting('ballowpast', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::allowPastPosting()\r
+        * Get the the setting if it is allowed to publish postings in the past\r
+        * [should be named as getAllowPastPosting()]\r
+        * \r
+        * @param       void\r
+        * @return      boolean\r
+        */\r
+       public function allowPastPosting()\r
+       {\r
+               return $this->getSetting('ballowpast');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getCorrectTime()\r
+        * \r
+        * @param       integer $t\r
+        * @return      integer\r
+        */\r
+       public function getCorrectTime($t=0)\r
+       {\r
+               if ( $t == 0 )\r
+               {\r
+                       $t = time();\r
+               }\r
+               return ($t + 3600 * $this->getTimeOffset());\r
+       }\r
+       \r
+       /**\r
+        * Blog::getName()\r
+        * \r
+        * @param       void\r
+        * @return      string name of this weblog\r
+        */\r
+       public function getName()\r
+       {\r
+               return $this->getSetting('bname');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getShortName()\r
+        * \r
+        * @param       void\r
+        * @return      string  short name of this weblog\r
+        */\r
+       public function getShortName()\r
+       {\r
+               return $this->getSetting('bshortname');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getMaxComments()\r
+        * \r
+        * @param       void\r
+        * @return      integer maximum number of comments\r
+        */\r
+       public function getMaxComments()\r
+       {\r
+               return $this->getSetting('bmaxcomments');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getNotifyAddress()\r
+        * \r
+        * @param       void\r
+        * @return      string  mail address for notifying\r
+        */\r
+       public function getNotifyAddress()\r
+       {\r
+               return $this->getSetting('bnotify');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getNotifyType()\r
+        * \r
+        * @param       void\r
+        * @return      integer notifycation type\r
+        */\r
+       public function getNotifyType()\r
+       {\r
+               return $this->getSetting('bnotifytype');\r
+       }\r
+       \r
+       /**\r
+        * Blog::notifyOnComment()\r
+        * \r
+        * @param       void\r
+        * @return      boolean\r
+        */\r
+       public function notifyOnComment()\r
+       {\r
+               $n = $this->getNotifyType();\r
+               return (($n != 0) && (($n % 3) == 0));\r
+       }\r
+       \r
+       /**\r
+        * Blog::notifyOnVote()\r
+        * \r
+        * @param       void\r
+        * @return      boolean\r
+        */\r
+       public function notifyOnVote()\r
+       {\r
+               $n = $this->getNotifyType();\r
+               return (($n != 0) && (($n % 5) == 0));\r
+       }\r
+       \r
+       /**\r
+        * Blog::notifyOnNewItem()\r
+        * \r
+        * @param       void\r
+        * @return      boolean\r
+        */\r
+       public function notifyOnNewItem()\r
+       {\r
+               $n = $this->getNotifyType();\r
+               return (($n != 0) && (($n % 7) == 0));\r
+       }\r
+       \r
+       /**\r
+        * Blog::setNotifyType()\r
+        * \r
+        * @param       integer $val\r
+        * @return      void\r
+        */\r
+       public function setNotifyType($val)\r
+       {\r
+               $this->setSetting('bnotifytype',$val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::getTimeOffset()\r
+        * @param       void\r
+        * @return      \r
+        */\r
+       public function getTimeOffset()\r
+       {\r
+               return $this->getSetting('btimeoffset');\r
+       }\r
+       \r
+       /**\r
+        * Blog::commentsEnabled()\r
+        * @param       void\r
+        * @return      integer enabled or not\r
+        */\r
+       public function commentsEnabled()\r
+       {\r
+               return $this->getSetting('bcomments');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getURL()\r
+        * @param       void\r
+        * @return      string  URI for this weblog\r
+        */\r
+       public function getURL()\r
+       {\r
+               return $this->getSetting('burl');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getDefaultSkin()\r
+        * @param       void\r
+        * @return      name of skin as default for this weblog\r
+        */\r
+       public function getDefaultSkin()\r
+       {\r
+               return $this->getSetting('bdefskin');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getUpdateFile()\r
+        * @param       void\r
+        * @return      string  name of file to be updated when weblog is updated\r
+        */\r
+       public function getUpdateFile()\r
+       {\r
+               return $this->getSetting('bupdate');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getDescription()\r
+        * @param       void\r
+        * @return      string  description for this weblog\r
+        */\r
+       public function getDescription()\r
+       {\r
+               return $this->getSetting('bdesc');\r
+       }\r
+       \r
+       /**\r
+        * Blog::isPublic()\r
+        * @param       void\r
+        * @return      integer publlic or not\r
+        */\r
+       public function isPublic()\r
+       {\r
+               return $this->getSetting('bpublic');\r
+       }\r
+       \r
+       /**\r
+        * Blog::emailRequired()\r
+        * @param       void\r
+        * @return      integer email is required when posting comment or not\r
+        */\r
+       public function emailRequired()\r
+       {\r
+               return $this->getSetting('breqemail');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getSearchable()\r
+        * @param       void\r
+        * @return      integer searchable or not\r
+        */\r
+       public function getSearchable()\r
+       {\r
+               return $this->getSetting('bincludesearch');\r
+       }\r
+       \r
+       /**\r
+        * Blog::getDefaultCategory()\r
+        * @param       void\r
+        * @return      ID for category as a default\r
+        */\r
+       public function getDefaultCategory()\r
+       {\r
+               return $this->getSetting('bdefcat');\r
+       }\r
+       \r
+       /**\r
+        * Blog::setPublic()\r
+        * @param       integer $val    allow comments by non-registered members or not\r
+        * @return      void\r
+        */\r
+       public function setPublic($val)\r
+       {\r
+               $this->setSetting('bpublic', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setSearchable()\r
+        * @param       integer $val    searchable from the other blogs or not\r
+        * @return      void\r
+        */\r
+       public function setSearchable($val)\r
+       {\r
+               $this->setSetting('bincludesearch', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setDescription\r
+        * @param       string  $val    description for this weblog\r
+        * @return      void\r
+        */\r
+       public function setDescription($val)\r
+       {\r
+               $this->setSetting('bdesc',$val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setUpdateFile()\r
+        * @param       string  $val    name of file to beupdated when weblog is updated\r
+        * @return      \r
+        */\r
+       public function setUpdateFile($val)\r
+       {\r
+               $this->setSetting('bupdate',$val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setDefaultSkin()\r
+        * @param       integer $val    ID for default skin to use when displaying this weblog\r
+        * @return      void\r
+        */\r
+       public function setDefaultSkin($val)\r
+       {\r
+               $this->setSetting('bdefskin', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setURL()\r
+        * @param       string  $val    URI for this weblog\r
+        * @return      \r
+        */\r
+       public function setURL($val)\r
+       {\r
+               $this->setSetting('burl', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setName()\r
+        * @param       string  $val    name of this weblog\r
+        * @return      void\r
+        */\r
+       public function setName($val)\r
+       {\r
+               $this->setSetting('bname', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setShortName()\r
+        * @param       string  $val    short name for this weblog\r
+        * @return      void\r
+        */\r
+       public function setShortName($val)\r
+       {\r
+               $this->setSetting('bshortname', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setCommentsEnabled()\r
+        * @param       integer $val    enabling posting comment or not\r
+        * @return      void\r
+        */\r
+       public function setCommentsEnabled($val)\r
+       {\r
+               $this->setSetting('bcomments',$val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setMaxComments()\r
+        * @param       integer $val    maximum number of comments for this weblog\r
+        * @return      void\r
+        */\r
+       public function setMaxComments($val)\r
+       {\r
+               $this->setSetting('bmaxcomments', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setNotifyAddress()\r
+        * @param       string  $val    email to be notified if weblog updated\r
+        * @return      void\r
+        */\r
+       public function setNotifyAddress($val)\r
+       {\r
+               $this->setSetting('bnotify', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setEmailRequired()\r
+        * @param       string  requiring comments with email or not from non member\r
+        * @return      void\r
+        */\r
+       public function setEmailRequired($val)\r
+       {\r
+               $this->setSetting('breqemail', $val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setTimeOffset()\r
+        * @param       integer $val    time offset\r
+        * @return      void\r
+        */\r
+       public function setTimeOffset($val)\r
+       {\r
+               // check validity of value\r
+               // 1. replace , by . (common mistake)\r
+               $val = str_replace(',','.',$val);\r
+               \r
+               // 2. cast to float or int\r
+               if ( is_numeric($val) && (i18n::strpos($val, '.5') === (i18n::strlen($val) - 2)) )\r
+               {\r
+                       $val = (float) $val;\r
+               }\r
+               else\r
+               {\r
+                       $val = (integer) $val;\r
+               }\r
+               \r
+               $this->setSetting('btimeoffset',$val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::setDefaultCategory()\r
+        * @param       integer $val    ID for default category for this weblog\r
+        * @return      \r
+        */\r
+       public function setDefaultCategory($val)\r
+       {\r
+               $this->setSetting('bdefcat',$val);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::getSetting()\r
+        * @param       string  $key    key for setting of this weblog\r
+        * @return      mixed   value for the setting\r
+        */\r
+       public function getSetting($key)\r
+       {\r
+               return $this->settings[$key];\r
+       }\r
+       \r
+       /**\r
+        * Blog::setSetting()\r
+        * @param       string  $key    key for setting of this weblog\r
+        * @param       mixed   $value  value for the key\r
+        * @return      \r
+        */\r
+       public function setSetting($key, $value)\r
+       {\r
+               $this->settings[$key] = $value;\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::addTeamMember()\r
+        * Tries to add a member to the team. \r
+        * Returns false if the member was already on the team\r
+        * \r
+        * @param       integer $memberid       id for member\r
+        * @param       boolean $admin  super-admin or not\r
+        * @return      boolean Success/Fail\r
+        */\r
+       public function addTeamMember($memberid, $admin)\r
+       {\r
+               global $manager;\r
+               \r
+               $memberid = intval($memberid);\r
+               $admin = intval($admin);\r
+               \r
+               // check if member is already a member\r
+               $tmem =& $manager->getMember($memberid);\r
+               \r
+               if ( $tmem->isTeamMember($this->blogid) )\r
+               {\r
+                       return 0;\r
+               }\r
+               \r
+               $data = array(\r
+                       'blog'          => &$this,\r
+                       'member'        => &$tmem,\r
+                       'admin'         => &$admin\r
+               );\r
+               $manager->notify('PreAddTeamMember', $data);\r
+               \r
+               // add to team\r
+               $query = "INSERT INTO %s (TMEMBER, TBLOG, TADMIN) VALUES (%d, %d, %d);";\r
+               $query = sprintf($query, sql_table('team'), (integer) $memberid, (integer) $this->blogid, (integer) $admin);\r
+               DB::execute($query);\r
+               \r
+               $data = array(\r
+                       'blog'          => &$this,\r
+                       'member'        => &$tmem,\r
+                       'admin'         =>  $admin\r
+               );\r
+               $manager->notify('PostAddTeamMember', $data);\r
+               \r
+               $logMsg = sprintf(_TEAM_ADD_NEWTEAMMEMBER, $tmem->getDisplayName(), $memberid, $this->getName());\r
+               ActionLog::add(INFO, $logMsg);\r
+               \r
+               return 1;\r
+       }\r
+       \r
+       /**\r
+        * Blog::getID()\r
+        * @param       void\r
+        * @return      integer ID for this weblog\r
+        */\r
+       public function getID()\r
+       {\r
+               return (integer) $this->blogid;\r
+       }\r
+       \r
+       /**\r
+        * Checks if a blog with a given shortname exists \r
+        * Returns true if there is a blog with the given shortname (static)\r
+        * \r
+        * @param       string  $name           blog shortname\r
+        * @return      boolean exists or not\r
+        */\r
+       public function exists($name)\r
+       {\r
+               $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bshortname='. DB::quoteValue($name));\r
+               return ($r->rowCount() != 0);\r
+       }\r
+       \r
+       /**\r
+        * Checks if a blog with a given id exists \r
+        * Returns true if there is a blog with the given ID (static)\r
+        * \r
+        * @param       integer $id     ID for searched weblog\r
+        * @return      boolean exists or not\r
+        */\r
+       public function existsID($id)\r
+       {\r
+               $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));\r
+               return ($r->rowCount() != 0);\r
+       }\r
+       \r
+       /**\r
+        * Blog::setFuturePost()\r
+        * flag there is a future post pending\r
+        * \r
+        * @param       void\r
+        * @return      void\r
+        */\r
+       public function setFuturePost()\r
+       {\r
+               $query =  "UPDATE %s SET bfuturepost='1' WHERE bnumber=%d;";\r
+               $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);\r
+               DB::execute($query);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::clearFuturePost()\r
+        * clear there is a future post pending\r
+        * \r
+        * @param       void\r
+        * @return      void\r
+        */\r
+       public function clearFuturePost()\r
+       {\r
+               $query =  "UPDATE %s SET bfuturepost='0' WHERE bnumber=%d;";\r
+               $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);\r
+               DB::execute($query);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::checkJustPosted()\r
+        * check if we should throw justPosted event \r
+        * \r
+        * @param       void\r
+        * @return      void\r
+        */\r
+       public function checkJustPosted()\r
+       {\r
+               global $manager;\r
+               \r
+               if ( $this->settings['bfuturepost'] == 1 )\r
+               {\r
+                       $query = "SELECT * FROM %s WHERE iposted=0 AND iblog=%d AND itime < NOW();";\r
+                       $query = sprintf($query, sql_table('item'), (integer) $this->blogid);\r
+                       \r
+                       $result = DB::getResult($query);\r
+                       if ( $result->rowCount() > 0 )\r
+                       {\r
+                               // This $pinged is allow a plugin to tell other hook to the event that a ping is sent already\r
+                               // Note that the plugins's calling order is subject to thri order in the plugin list\r
+                               $pinged = FALSE;\r
+                               $manager->notify('JustPosted', array('blogid' => $this->blogid, 'pinged' => &$pinged));\r
+                               \r
+                               // clear all expired future posts\r
+                               $query = "UPDATE %s SET iposted='1' WHERE iblog=%d AND itime < NOW();";\r
+                               $query = spriintf($query, sql_table('item'), (integer) $this->blogid);\r
+                               DB::execute($query);\r
+                               \r
+                               // check to see any pending future post, clear the flag is none\r
+                               $query = "SELECT * FROM %s WHERE iposted=0 AND iblog=%d;";\r
+                               $query = sprintf($query, sql_table('item'), (integer) $this->blogid);\r
+                               \r
+                               $result = DB::getResult($query);\r
+                               if ( $result->rowCount() == 0 )\r
+                               {\r
+                                       $this->clearFuturePost();\r
+                               }\r
+                       }\r
+               }\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Blog::readLogFromList()\r
+        * Shows the given list of items for this blog\r
+        *\r
+        * @param       array   $itemarray      array of item numbers to be displayed\r
+        * @param       string  $template       string representing the template _NAME_ (!)\r
+        * @param       string  $highlight      contains a query that should be highlighted\r
+        * @param       boolean $comments       1=show comments 0=don't show comments\r
+        * @param       boolean $dateheads      1=show dateheads 0=don't show dateheads\r
+        * @param       boolean $showDrafts     0=do not show drafts 1=show drafts\r
+        * @param       boolean $showFuture     0=do not show future posts 1=show future posts\r
+        * @return      integer amount of items shown\r
+        */\r
+       public function readLogFromList($itemarray, $template, $highlight = '', $comments = 1, $dateheads = 1,$showDrafts = 0, $showFuture = 0)\r
+       {\r
+               $query = $this->getSqlItemList($itemarray,$showDrafts,$showFuture);\r
+               return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);\r
+       }\r
+       \r
+       /**\r
+        * Blog::getSqlItemList()\r
+        * Returns the SQL query used to fill out templates for a list of items\r
+        * No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
+        *\r
+        * @param       array   $itemarray      an array holding the item numbers of the items to be displayed\r
+        * @param       integer $showDrafts     0=do not show drafts 1=show drafts\r
+        * @param       integer $showFuture     0=do not show future posts 1=show future posts\r
+        * @return      string  either a full SQL query, or an empty string\r
+        */\r
+       public function getSqlItemList($itemarray,$showDrafts = 0,$showFuture = 0)\r
+       {\r
+               if ( !is_array($itemarray) )\r
+               {\r
+                       return '';\r
+               }\r
+               \r
+               $showDrafts = intval($showDrafts);\r
+               $showFuture = intval($showFuture);\r
+               $items = array();\r
+               \r
+               foreach ( $itemarray as $value )\r
+               {\r
+                       if ( intval($value) )\r
+                       {\r
+                               $items[] = intval($value);\r
+                       }\r
+               }\r
+               if ( !count($items) )\r
+               {\r
+                       return '';\r
+               }\r
+               \r
+               $i = count($items);\r
+               $query = '';\r
+               foreach ( $items as $value )\r
+               {\r
+                       $query .= '('\r
+                                       .       'SELECT'\r
+                                       .       ' i.inumber as itemid,'\r
+                                       .       ' i.ititle as title,'\r
+                                       .       ' i.ibody as body,'\r
+                                       .       ' m.mname as author,'\r
+                                       .       ' m.mrealname as authorname,'\r
+                                       .       ' i.itime,'\r
+                                       .       ' i.imore as more,'\r
+                                       .       ' m.mnumber as authorid,'\r
+                                       .       ' m.memail as authormail,'\r
+                                       .       ' m.murl as authorurl,'\r
+                                       .       ' c.cname as category,'\r
+                                       .       ' i.icat as catid,'\r
+                                       .       ' i.iclosed as closed';\r
+                       \r
+                       $query .= ' FROM '\r
+                                       . sql_table('item') . ' as i, '\r
+                                       . sql_table('member') . ' as m, '\r
+                                       . sql_table('category') . ' as c'\r
+                                       . ' WHERE'\r
+                                   .    ' i.iblog='.$this->blogid\r
+                                  . ' and i.iauthor=m.mnumber'\r
+                                  . ' and i.icat=c.catid';\r
+                       \r
+                       // exclude drafts       \r
+                       if ( !$showDrafts )\r
+                       {\r
+                               $query .= ' and i.idraft=0';\r
+                       }\r
+                       if ( !$showFuture )\r
+                       {\r
+                               // don't show future items\r
+                               $query .= ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime());\r
+                       }\r
+                       \r
+                       $query .= ' and i.inumber='.intval($value);\r
+                       $query .= ')';\r
+                       $i--;\r
+                       if ($i) $query .= ' UNION ';\r
+               }\r
+               \r
+               return $query;\r
+       }\r
+       \r
+       /**\r
+        * Blog::convertBreaks()\r
+        * Get the the setting for the line break handling\r
+        * [should be named as getConvertBreaks()]\r
+        * \r
+        * @deprecated\r
+        * @param       void\r
+        * @return      \r
+        */\r
+       public function convertBreaks()\r
+       {\r
+               return $this->getSetting('bconvertbreaks');\r
+       }\r
+       \r
+       /**\r
+        * Set the the setting for the line break handling\r
+        * \r
+        * @deprecated\r
+        * @param       boolean $val    new value for bconvertbreaks\r
+        * @return      void\r
+        */\r
+       public function setConvertBreaks($val)\r
+       {\r
+               $this->setSetting('bconvertbreaks', $val);\r
+               return;\r
+       }\r
+}\r