OSDN Git Service

merged 3.3 beta1
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / BLOG.php
index d4e401c..3b9b7bd 100755 (executable)
-<?php\r
-\r
-/*\r
- * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
- * Copyright (C) 2002-2005 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-2005 The Nucleus Group\r
- * @version $Id: BLOG.php,v 1.4 2005-08-13 07:31:04 kimitake Exp $\r
- * $NucleusJP: BLOG.php,v 1.3 2005/03/12 06:19:05 kimitake Exp $\r
- */\r
-class BLOG {\r
-       \r
-       // blog id\r
-       var $blogid;\r
-       \r
-       // ID of currently selected category\r
-       var $selectedcatid;\r
-       \r
-       // After creating an object of the blog class, contains true if the BLOG object is \r
-       // valid (the blog exists)\r
-       var $isValid;\r
-       \r
-       // associative array, containing all blogsettings (use the get/set functions instead)\r
-       var $settings;\r
-       \r
-       /**\r
-        * Creates a new BLOG object for the given blog\r
-        *\r
-        * @param $id blogid\r
-        */\r
-       function BLOG($id) {\r
-               $this->blogid = intval($id);\r
-               $this->readSettings();\r
-\r
-               // try to set catid \r
-               // (the parse functions in SKIN.php will override this, so it's mainly useless)\r
-               global $catid;\r
-               $this->setSelectedCategory($catid);\r
-       }\r
-\r
-       /**\r
-        * Shows the given amount of items for this blog\r
-        *\r
-        * @param $template\r
-        *              String representing the template _NAME_ (!)\r
-        * @param $amountEntries \r
-        *              amount of entries to show\r
-        * @param $startpos\r
-        *              offset from where items should be shown (e.g. 5 = start at fifth item)\r
-        * @returns int\r
-        *              amount of items shown\r
-        */\r
-       function readLog($template, $amountEntries, $offset = 0, $startpos = 0) {\r
-               return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);\r
-       }\r
-\r
-       /**\r
-        * Shows an archive for a given month\r
-        *\r
-        * @param $year\r
-        *              year\r
-        * @param $month\r
-        *              month \r
-        * @param $template\r
-        *              String representing the template name to be used\r
-        */\r
-       function showArchive($templatename, $year, $month, $day=0) {\r
-               \r
-               // create extra where clause for select query\r
-               if ($day == 0) {\r
-                       $timestamp_start = mktime(0,0,0,$month,1,$year);\r
-                       $timestamp_end = mktime(0,0,0,$month+1,1,$year);  // also works when $month==12\r
-               } else {\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>=' . mysqldate($timestamp_start)\r
-                            . ' and i.itime<' . mysqldate($timestamp_end);\r
-               \r
-       \r
-               $this->readLogAmount($templatename,0,$extra_query,'',1,1);\r
-\r
-       }\r
-\r
-\r
-       // sets/gets current category (only when category exists)\r
-       function setSelectedCategory($catid) {\r
-               if ($this->isValidCategory($catid) || (intval($catid) == 0)) \r
-                       $this->selectedcatid = intval($catid);\r
-       }\r
-       \r
-       function setSelectedCategoryByName($catname) {\r
-               $this->setSelectedCategory($this->getCategoryIdFromName($catname));\r
-       }\r
-\r
-       function getSelectedCategory() { \r
-               return $this->selectedcatid; \r
-       }\r
-\r
-       /**\r
-        * Shows the given amount of items for this blog\r
-        *\r
-        * @param $template\r
-        *              String representing the template _NAME_ (!)\r
-        * @param $amountEntries\r
-        *              amount of entries to show (0 = no limit)\r
-        * @param $extraQuery\r
-        *              extra conditions to be added to the query\r
-        * @param $highlight\r
-        *              contains a query that should be highlighted\r
-        * @param $comments\r
-        *              1=show comments 0=don't show comments\r
-        * @param $dateheads\r
-        *              1=show dateheads 0=don't show dateheads\r
-        * @param $offset\r
-        *              offset\r
-        * @returns int\r
-        *              amount of items shown\r
-        */\r
-       function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0) {\r
-\r
-               $query = $this->getSqlBlog($extraQuery);\r
-               \r
-               if ($amountEntries > 0) {\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
-       function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1) {\r
-               global $CONF, $manager;\r
-\r
-               $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');\r
-               if ($lastVisit != 0)\r
-                       $lastVisit = $this->getCorrectTime($lastVisit);\r
-\r
-               // set templatename as global variable (so plugins can access it)\r
-               global $currentTemplateName;\r
-               $currentTemplateName = $templateName;\r
-               \r
-               $template =& $manager->getTemplate($templateName);\r
-               \r
-               // create parser object & action handler\r
-               $actions =& new ITEMACTIONS($this);\r
-               $parser =& new PARSER($actions->getDefinedActions(),$actions);\r
-               $actions->setTemplate($template);\r
-               $actions->setHighlight($highlight);\r
-               $actions->setLastVisit($lastVisit);\r
-               $actions->setParser($parser);\r
-               $actions->setShowComments($comments);\r
-\r
-               // execute query\r
-               $items = sql_query($query);\r
-               \r
-               // loop over all items\r
-               while ($item = mysql_fetch_object($items)) {\r
-                       \r
-                       $item->timestamp = strtotime($item->itime);     // string timestamp -> unix timestamp\r
-               \r
-                       // action handler needs to know the item we're handling\r
-                       $actions->setCurrentItem($item);\r
-                       \r
-                       // add date header if needed\r
-                       if ($dateheads) {\r
-                               $new_date = date('dFY',$item->timestamp);\r
-                               if ($new_date != $old_date) {\r
-                                       // unless this is the first time, write date footer\r
-                                       $timestamp = $item->timestamp;\r
-                                       if ($old_date != 0) {\r
-                                               $oldTS = strtotime($old_date);\r
-                                               $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));\r
-                                               $parser->parse(strftime($template['DATE_FOOTER'], $oldTS));\r
-                                               $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));                                                \r
-                                       }\r
-                                       $manager->notify('PreDateHead',array('blog' => &$this, 'timestamp' => $timestamp));\r
-                                       // note, to use templatvars in the dateheader, the %-characters need to be doubled in\r
-                                       // order to be preserved by strftime\r
-                                       $parser->parse(strftime($template['DATE_HEADER'],$timestamp));\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
-               \r
-               $numrows = mysql_num_rows($items);\r
-               \r
-               // add another date footer if there was at least one item\r
-               if (($numrows > 0) && $dateheads) {\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
-               mysql_free_result($items);      // free memory\r
-               \r
-               return $numrows;\r
-               \r
-       }        \r
-       \r
-       function showOneitem($itemid, $template, $highlight) {\r
-               $extraQuery = ' and inumber=' . intval($itemid);\r
-               \r
-               return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);\r
-       }\r
-       \r
-\r
-       /**\r
-         * Adds an item to this blog\r
-         */\r
-       function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft) {\r
-               global $manager;\r
-\r
-               $blogid         = intval($blogid);\r
-               $authorid       = intval($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
-                       $body = addBreaks($body);\r
-                       $more = addBreaks($more);\r
-               }\r
-               \r
-               if ($closed != '1')     $closed = '0';\r
-               if ($draft != '0') $draft = '1';\r
-\r
-               if (!$this->isValidCategory($catid))\r
-                       $catid = $this->getDefaultCategory();\r
-               \r
-               if ($timestamp > $this->getCorrectTime())\r
-                       $isFuture = 1;\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
-               $title = addslashes($title);\r
-               $body = addslashes($body);\r
-               $more = addslashes($more);\r
-\r
-               $query = 'INSERT INTO '.sql_table('item').' (ITITLE, IBODY, IMORE, IBLOG, IAUTHOR, ITIME, ICLOSED, IDRAFT, ICAT) '\r
-                      . "VALUES ('$title', '$body', '$more', $blogid, $authorid, '$timestamp', $closed, $draft, $catid)";\r
-               sql_query($query);  \r
-               $itemid = mysql_insert_id();\r
-               \r
-               $manager->notify('PostAddItem',array('itemid' => $itemid));\r
-               \r
-               if (!$draft) \r
-                       $this->updateUpdateFile();\r
-               \r
-               // send notification mail\r
-               if (!$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem()) \r
-                       $this->sendNewItemNotification($itemid, stripslashes($title), stripslashes($body));\r
-               \r
-               return $itemid;\r
-       }\r
-       \r
-       function sendNewItemNotification($itemid, $title, $body) {\r
-               global $CONF, $member;\r
-               \r
-               // create text version of html post\r
-               $ascii = toAscii($body);\r
-\r
-               $mailto_msg = _NOTIFY_NI_MSG . " \n";\r
-               $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $itemid . "\n\n";\r
-               $mailto_msg .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";\r
-               $mailto_msg .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";\r
-               $mailto_msg .= getMailFooter();\r
-               \r
-               $mailto_title = $this->getName() . ': ' . _NOTIFY_NI_TITLE;\r
-                       \r
-               $frommail = $member->getNotifyFromMailAddress();\r
-\r
-               $notify =& new NOTIFICATION($this->getNotifyAddress());\r
-               $notify->notify($mailto_title, $mailto_msg , $frommail);\r
-                               \r
-\r
-\r
-       }\r
-\r
-       \r
-       /**\r
-         * Creates a new category for this blog\r
-         *\r
-         * @param $catName\r
-         *             name of the new category. When empty, a name is generated automatically \r
-         *             (starting with newcat)\r
-         * @param $catDescription\r
-         *             description of the new category. Defaults to 'New Category'\r
-         *\r
-         * @returns \r
-         *             the new category-id in case of success. \r
-         *             0 on failure\r
-         */\r
-       function createNewCategory($catName = '', $catDescription = 'New category') {\r
-               global $member, $manager;\r
-\r
-               if ($member->blogAdminRights($this->getID())) {\r
-                       // generate \r
-                       if ($catName == '')\r
-                       {\r
-                               $catName = 'newcat';\r
-                               $i = 1;\r
-                               while(mysql_num_rows(sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID())) > 0) \r
-                                       $i++;\r
-                               $catName = $catName . $i;\r
-                       }\r
-                       \r
-                       $manager->notify(\r
-                               'PreAddCategory',\r
-                               array(\r
-                                       'blog' => &$this,\r
-                                       'name' => &$catName,\r
-                                       'description' => $catDescription\r
-                               )\r
-                       );\r
-                       \r
-                       $query = 'INSERT INTO '.sql_table('category').' (cblog, cname, cdesc) VALUES (' . $this->getID() . ", '" . addslashes($catName) . "', '" . addslashes($catDescription) . "')";\r
-                       sql_query($query);\r
-                       $catid = mysql_insert_id();\r
-                       \r
-                       $manager->notify(\r
-                               'PostAddCategory',\r
-                               array(\r
-                                       'blog' => &$this,\r
-                                       'name' => $catName,\r
-                                       'description' => $catDescription,\r
-                                       'catid' => $catid\r
-                               )\r
-                       );                      \r
-                       \r
-                       return $catid;\r
-               } else {\r
-                       return 0;\r
-               }\r
-       \r
-       }       \r
-\r
-\r
-       /**\r
-        * Searches all months of this blog for the given query\r
-        *\r
-        * @param $query\r
-        *              search query\r
-        * @param $template\r
-        *              template to be used (__NAME__ of the template)\r
-        * @param $amountMonths\r
-        *              max amount of months to be search (0 = all)\r
-        * @param $maxresults\r
-        *              max number of results to show\r
-        * @param $startpos\r
-        *              offset\r
-        * @returns\r
-        *              amount of hits found\r
-        */\r
-       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
-               } else {\r
-               \r
-                       // add LIMIT to query (to split search results into pages)\r
-            if (intval($maxresults > 0)) \r
-                   $sqlquery .= ' LIMIT ' . intval($startpos).',' . intval($maxresults);\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'         => htmlspecialchars($query),\r
-                               'blogid'        => $this->getID()\r
-                       );\r
-                       echo TEMPLATE::fill($template['SEARCH_NOTHINGFOUND'],$vars);\r
-               }\r
-        }\r
-        \r
-               return $amountfound;\r
-       }\r
-       \r
-       /**\r
-        * Returns an SQL query to use for a search query\r
-        *\r
-        * @param $query\r
-        *              search query\r
-        * @param $amountMonths\r
-        *              amount of months to search back. Default = 0 = unlimited\r
-        * @param $mode\r
-        *              either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query\r
-        * @returns $highlight\r
-        *              words to highlight (out parameter)\r
-        * @returns \r
-        *              either a full SQL query, or an empty string (if querystring empty)\r
-        * @note\r
-        *              No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
-        */\r
-       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
-               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->getID();                       // also search current blog (duh)\r
-               $blogs          = array_unique($blogs);         // remove duplicates\r
-               $selectblogs = '';\r
-               if (count($blogs) > 0)\r
-                       $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';\r
-\r
-               if ($mode == '') \r
-               {\r
-                       $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';\r
-                       if ($select) \r
-                               $query .= ', '.$select. ' as score ';\r
-               } else {\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
-                          . ' and i.idraft=0'  // exclude drafts\r
-                          . $selectblogs\r
-                                       // don't show future items\r
-                          . ' and i.itime<=' . mysqldate($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>' . mysqldate($timestamp_start);\r
-               }\r
-\r
-               if ($mode == '')\r
-               {\r
-                       if ($select) \r
-                               $query .= ' ORDER BY score DESC';\r
-                       else \r
-                               $query .= ' ORDER BY i.itime DESC ';\r
-               }\r
-\r
-               return $query;          \r
-       }\r
-       \r
-       /**\r
-        * Returns the SQL query that's normally used to display the blog items on the index type skins\r
-        *\r
-        * @param $mode\r
-        *              either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query\r
-        * @returns \r
-        *              either a full SQL query, or an empty string \r
-        * @note\r
-        *              No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
-        */\r
-       function getSqlBlog($extraQuery, $mode = '')\r
-       {\r
-               if ($mode == '')\r
-                       $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';\r
-               else\r
-                       $query = 'SELECT COUNT(*) as result ';\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
-                                       // don't show future items\r
-                      . ' and i.itime<=' . mysqldate($this->getCorrectTime());\r
-\r
-               if ($this->getSelectedCategory())\r
-                       $query .= ' and i.icat=' . $this->getSelectedCategory() . ' ';\r
-\r
-\r
-               $query .= $extraQuery;\r
-               \r
-               if ($mode == '')\r
-                       $query .= ' ORDER BY i.itime DESC';\r
-                      \r
-               return $query;\r
-       }\r
-\r
-       /**\r
-         * Shows the archivelist using the given template\r
-         */\r
-       function showArchiveList($template, $mode = 'month', $limit = 0) {\r
-               global $CONF, $catid, $manager;\r
-\r
-               if ($catid) \r
-                       $linkparams = array('catid' => $catid);\r
-               \r
-               $template =& $manager->getTemplate($template);\r
-               $data['blogid'] = $this->getID();\r
-\r
-               echo TEMPLATE::fill($template['ARCHIVELIST_HEADER'],$data);\r
-\r
-               $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')\r
-               . ' WHERE iblog=' . $this->getID()\r
-               . ' and itime <=' . mysqldate($this->getCorrectTime())  // don't show future items!\r
-               . ' and idraft=0'; // don't show draft items\r
-               \r
-               if ($catid)\r
-                       $query .= ' and icat=' . intval($catid);\r
-               \r
-               $query .= ' GROUP BY Year, Month';\r
-               if ($mode == 'day')\r
-                       $query .= ', Day';\r
-               \r
-                       \r
-               $query .= ' ORDER BY itime DESC';                               \r
-               \r
-               if ($limit > 0) \r
-                       $query .= ' LIMIT ' . intval($limit);\r
-               \r
-               $res = sql_query($query);\r
-\r
-               while ($current = mysql_fetch_object($res)) {\r
-                       $current->itime = strtotime($current->itime);   // string time -> unix timestamp\r
-               \r
-                       if ($mode == 'day') {\r
-                               $archivedate = date('Y-m-d',$current->itime);\r
-                               $archive['day'] = date('d',$current->itime);\r
-                       } else {\r
-                               $archivedate = date('Y-m',$current->itime);                     \r
-                       }\r
-                       $data['month'] = date('m',$current->itime);\r
-                       $data['year'] = date('Y',$current->itime);\r
-                       $data['archivelink'] = createArchiveLink($this->getID(),$archivedate,$linkparams);\r
-\r
-                       $temp = TEMPLATE::fill($template['ARCHIVELIST_LISTITEM'],$data);\r
-                       echo strftime($temp,$current->itime);\r
-\r
-               }\r
-               \r
-               mysql_free_result($res);\r
-\r
-               echo TEMPLATE::fill($template['ARCHIVELIST_FOOTER'],$data);\r
-       }\r
-       \r
-\r
-       /**\r
-         * Shows the list of categories using a given template\r
-         */\r
-       function showCategoryList($template) {\r
-               global $CONF, $manager;\r
-               \r
-               // determine arguments next to catids\r
-               // I guess this can be done in a better way, but it works\r
-               global $archive, $archivelist;\r
-               \r
-               $linkparams = array();\r
-               if ($archive) {\r
-                       $blogurl = createArchiveLink($this->getID(), $archive, '');\r
-                       $linkparams['blogid'] = $this->getID();\r
-                       $linkparams['archive'] = $archive;\r
-               } else if ($archivelist) {\r
-                       $blogurl = createArchiveListLink($this->getID(), '');\r
-                       $linkparams['archivelist'] = $archivelist;\r
-               } else {\r
-                       $blogurl = createBlogidLink($this->getID(), '');\r
-                       $linkparams['blogid'] = $this->getID();\r
-               } \r
-                       \r
-               //$blogurl = $this->getURL() . $qargs;\r
-               $blogurl = createBlogLink($this->getURL(), $linkparams);\r
-\r
-               $template =& $manager->getTemplate($template);\r
-\r
-               echo TEMPLATE::fill($template['CATLIST_HEADER'],\r
-                                                       array(\r
-                                                               'blogid' => $this->getID(),\r
-                                                               'blogurl' => $blogurl,\r
-                                                               'self' => $CONF['Self']\r
-                                                       ));\r
-\r
-               $query = 'SELECT catid, cdesc as catdesc, cname as catname FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' ORDER BY cname ASC';\r
-               $res = sql_query($query);\r
-\r
-\r
-               while ($data = mysql_fetch_assoc($res)) {\r
-                       $data['blogid'] = $this->getID();       \r
-                       $data['blogurl'] = $blogurl;\r
-                       $data['catlink'] = createCategoryLink($data['catid'], $linkparams);\r
-                       $data['self'] = $CONF['Self'];\r
-\r
-                       $temp = TEMPLATE::fill($template['CATLIST_LISTITEM'],$data);\r
-                       echo strftime($temp,$current->itime);\r
-\r
-               }\r
-               \r
-               mysql_free_result($res);\r
-\r
-               echo TEMPLATE::fill($template['CATLIST_FOOTER'],\r
-                                                       array(\r
-                                                               'blogid' => $this->getID(),\r
-                                                               'blogurl' => $blogurl,\r
-                                                               'self' => $CONF['Self']                                                         \r
-                                                       ));\r
-       }\r
-\r
-       /**\r
-         * Blogsettings functions\r
-         */\r
-         \r
-       function readSettings() {\r
-               $query =  'SELECT *'\r
-                      . ' FROM '.sql_table('blog')\r
-                      . ' WHERE bnumber=' . $this->blogid;\r
-               $res = sql_query($query);\r
-               \r
-               $this->isValid = (mysql_num_rows($res) > 0);\r
-               if (!$this->isValid)\r
-                       return;\r
-               \r
-               $this->settings = mysql_fetch_assoc($res);\r
-       }\r
-       \r
-       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
-                       $offset = intval($offset);\r
-               \r
-               $query =  'UPDATE '.sql_table('blog')\r
-                      . " SET bname='" . addslashes($this->getName()) . "',"\r
-                      . "     bshortname='". addslashes($this->getShortName()) . "',"\r
-                      . "     bcomments=". intval($this->commentsEnabled()) . ","\r
-                      . "     bmaxcomments=" . intval($this->getMaxComments()) . ","\r
-                      . "     btimeoffset=" . $offset . ","\r
-                      . "     bpublic=" . intval($this->isPublic()) . ","\r
-                      . "     bsendping=" . intval($this->pingUserland()) . ","\r
-                      . "     bconvertbreaks=" . intval($this->convertBreaks()) . ","\r
-                      . "     ballowpast=" . intval($this->allowPastPosting()) . ","                      \r
-                      . "     bnotify='" . addslashes($this->getNotifyAddress()) . "',"\r
-                      . "     bnotifytype=" . intval($this->getNotifyType()) . ","                    \r
-                      . "     burl='" . addslashes($this->getURL()) . "',"\r
-                      . "     bupdate='" . addslashes($this->getUpdateFile()) . "',"\r
-                      . "     bdesc='" . addslashes($this->getDescription()) . "',"\r
-                      . "     bdefcat=" . intval($this->getDefaultCategory()) . ","                   \r
-                      . "     bdefskin=" . intval($this->getDefaultSkin()) . ","\r
-                      . "     bincludesearch=" . intval($this->getSearchable())\r
-                      . " WHERE bnumber=" . intval($this->getID());\r
-               sql_query($query);\r
-\r
-       }\r
-       \r
-\r
-               \r
-       // update update file if requested\r
-       function updateUpdatefile() {\r
-                if ($this->getUpdateFile()) {\r
-                       $f_update = fopen($this->getUpdateFile(),'w');\r
-                       fputs($f_update,$this->getCorrectTime());\r
-                       fclose($f_update);\r
-                }\r
-\r
-       }\r
-       \r
-       /**\r
-         * Sends a XML-RPC ping message to Userland, so the weblog can\r
-         * show up in the weblogs.com updates-list\r
-         */\r
-       function sendUserlandPing() {\r
-               global $php_errormsg;\r
-               \r
-                if ($this->pingUserland()) {\r
-                         // testmessage for adding an item\r
-                         $message = new xmlrpcmsg('weblogUpdates.ping',array(\r
-                                       new xmlrpcval($this->getName(),'string'),       \r
-                                       new xmlrpcval($this->getURL(),'string')\r
-                         ));\r
-                         \r
-                         $c = new xmlrpc_client('/RPC2', 'rpc.weblogs.com', 80);\r
-\r
-                         // $c->setDebug(1);\r
-\r
-                         $r = $c->send($message,15); // 15 seconds timeout...\r
-                         \r
-                         if (($r == 0) && ($r->errno || $r->errstring)) {\r
-                               return 'Error ' . $r->errno . ' : ' . $r->errstring;\r
-                         } elseif (($r == 0) && ($php_errormsg)) {\r
-                               return 'PHP Error: ' . $php_errormsg;\r
-                         } elseif ($r == 0) {\r
-                               return 'Error while trying to send ping. Sorry about that.';\r
-                         } elseif ($r->faultCode() != 0) {\r
-                               return 'Error: ' . $r->faultString();\r
-                         } else {\r
-                                 $r = $r->value();     // get response struct\r
-                                 // get values\r
-                                 $flerror = $r->structmem('flerror');\r
-                                 $flerror = $flerror->scalarval();\r
\r
-                                 \r
-                                 $message = $r->structmem('message');\r
-                                 $message = $message->scalarval();\r
-\r
-                                 if ($flerror != 0)\r
-                                       return 'Error (flerror=1): ' . $message;\r
-                                 else\r
-                                       return 'Success: ' . $message;\r
-                         }                       \r
-                }\r
-       }\r
-       \r
-       function isValidCategory($catid) {\r
-               $query = 'SELECT * FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' and catid=' . intval($catid);\r
-               return (mysql_num_rows(mysql_query($query)) != 0);\r
-       }\r
-       \r
-       function getCategoryName($catid) {\r
-               $res = mysql_query('SELECT cname FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));\r
-               $o = mysql_fetch_object($res);\r
-               return $o->cname;\r
-       }\r
-       \r
-       function getCategoryDesc($catid) {\r
-               $res = mysql_query('SELECT cdesc FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));\r
-               $o = mysql_fetch_object($res);\r
-               return $o->cdesc;\r
-       }\r
-\r
-       function getCategoryIdFromName($name) {\r
-               $res = mysql_query('SELECT catid FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and cname="' . addslashes($name) . '"');\r
-               if (mysql_num_rows($res) > 0) {\r
-                       $o = mysql_fetch_object($res);\r
-                       return $o->catid;       \r
-               } else {\r
-                       return $this->getDefaultCategory();\r
-               }\r
-       }\r
-       \r
-       function pingUserland() {\r
-               return $this->getSetting('bsendping');\r
-       }\r
-       \r
-       function setPingUserland($val) {\r
-               $this->setSetting('bsendping',$val);\r
-       }\r
-       \r
-       function convertBreaks() {\r
-               return $this->getSetting('bconvertbreaks');\r
-       }\r
-       \r
-       function insertJavaScriptInfo($authorid = '') {\r
-               global $member, $CONF;\r
-               \r
-               if ($authorid == '')    \r
-                       $authorid = $member->getID();\r
-                       \r
-               ?>\r
-               <script type="text/javascript">\r
-                       setConvertBreaks(<?php echo  $this->convertBreaks() ? 'true' : 'false' ?>);\r
-                       setMediaUrl("<?php echo $CONF['MediaURL']?>");\r
-                       setAuthorId(<?php echo $authorid?>);\r
-               </script><?php  }\r
-       \r
-       function setConvertBreaks($val) {\r
-               $this->setSetting('bconvertbreaks',$val);\r
-       }\r
-       function setAllowPastPosting($val) {\r
-               $this->setSetting('ballowpast',$val);\r
-       }               \r
-       function allowPastPosting() {\r
-               return $this->getSetting('ballowpast');\r
-       }\r
-       \r
-       function getCorrectTime($t=0) {\r
-               if ($t == 0) $t = time();\r
-               return ($t + 3600 * $this->getTimeOffset());\r
-       }\r
-       \r
-       function getName() {\r
-               return $this->getSetting('bname');\r
-       }\r
-       \r
-       function getShortName() {\r
-               return $this->getSetting('bshortname');\r
-       }\r
-       \r
-       function getMaxComments() {\r
-               return $this->getSetting('bmaxcomments');\r
-       }\r
-       \r
-       function getNotifyAddress() {\r
-               return $this->getSetting('bnotify');\r
-       }\r
-       \r
-       function getNotifyType() {\r
-               return $this->getSetting('bnotifytype');\r
-       }\r
-       \r
-       function notifyOnComment() {\r
-               $n = $this->getNotifyType();\r
-               return (($n != 0) && (($n % 3) == 0));\r
-       }\r
-\r
-       function notifyOnVote() {\r
-               $n = $this->getNotifyType();\r
-               return (($n != 0) && (($n % 5) == 0));\r
-       }\r
-\r
-       function notifyOnNewItem() {\r
-               $n = $this->getNotifyType();\r
-               return (($n != 0) && (($n % 7) == 0));\r
-       }\r
-       \r
-       function setNotifyType($val) {\r
-               $this->setSetting('bnotifytype',$val);  \r
-       }\r
-       \r
-\r
-       function getTimeOffset() {\r
-               return $this->getSetting('btimeoffset');\r
-       }\r
-       \r
-       function commentsEnabled() {\r
-               return $this->getSetting('bcomments');\r
-       }\r
-       \r
-       function getURL() {\r
-               return $this->getSetting('burl');\r
-       }\r
-       \r
-       function getDefaultSkin() {\r
-               return $this->getSetting('bdefskin');\r
-       }\r
-       \r
-       function getUpdateFile() {\r
-               return $this->getSetting('bupdate');\r
-       }\r
-       \r
-       function getDescription() {\r
-               return $this->getSetting('bdesc');\r
-       }\r
-       \r
-       function isPublic() {\r
-               return $this->getSetting('bpublic');\r
-       }\r
-       \r
-       function getSearchable() {\r
-               return $this->getSetting('bincludesearch');\r
-       }\r
-\r
-       function getDefaultCategory() {\r
-               return $this->getSetting('bdefcat');\r
-       }\r
-       \r
-       function setPublic($val) {\r
-               $this->setSetting('bpublic',$val);\r
-       }\r
-\r
-       function setSearchable($val) {\r
-               $this->setSetting('bincludesearch',$val);\r
-       }\r
-\r
-       function setDescription($val) {\r
-               $this->setSetting('bdesc',$val);\r
-       }\r
-\r
-       function setUpdateFile($val) {\r
-               $this->setSetting('bupdate',$val);\r
-       }\r
-       \r
-       function setDefaultSkin($val) {\r
-               $this->setSetting('bdefskin',$val);\r
-       }\r
-       \r
-       function setURL($val) {\r
-               $this->setSetting('burl',$val);\r
-       }\r
-       \r
-       function setName($val) {\r
-               $this->setSetting('bname',$val);\r
-       }\r
-       \r
-       function setShortName($val) {\r
-               $this->setSetting('bshortname',$val);\r
-       }\r
-       \r
-       function setCommentsEnabled($val) { \r
-               $this->setSetting('bcomments',$val);\r
-       }\r
-       \r
-       function setMaxComments($val) {\r
-               $this->setSetting('bmaxcomments',$val);\r
-       }\r
-       \r
-       function setNotifyAddress($val) {\r
-               $this->setSetting('bnotify',$val);\r
-       }\r
-       \r
-       function setTimeOffset($val) {\r
-               // check validity of value\r
-               // 1. replace , by . (common mistake)\r
-               $val = str_replace(',','.',$val);\r
-               // 2. cast to float or int\r
-               if (is_numeric($val) && strstr($val,'.5')) {\r
-                       $val = (float) $val;\r
-               } else {\r
-                       $val = intval($val);\r
-               }\r
-       \r
-               $this->setSetting('btimeoffset',$val);\r
-       }\r
-       \r
-       function setDefaultCategory($val) {\r
-               $this->setSetting('bdefcat',$val);\r
-       }\r
-\r
-       function getSetting($key) {\r
-               return $this->settings[$key];\r
-       }\r
-       \r
-       function setSetting($key,$value) {\r
-               $this->settings[$key] = $value;\r
-       }\r
-       \r
-       \r
-       // tries to add a member to the team. Returns false if the member was already on\r
-       // the team\r
-       function addTeamMember($memberid, $admin) {\r
-               global $manager;\r
-               \r
-               $memberid = intval($memberid);\r
-               $admin = intval($admin);\r
-               \r
-               // check if member is already a member\r
-               $tmem = MEMBER::createFromID($memberid);\r
-               \r
-               if ($tmem->isTeamMember($this->getID()))\r
-                       return 0;\r
-               \r
-               $manager->notify(\r
-                       'PreAddTeamMember',\r
-                       array(\r
-                               'blog' => &$this,\r
-                               'member' => &$tmem,\r
-                               'admin' => &$admin\r
-                       )\r
-               );\r
-               \r
-               // add to team\r
-               $query = 'INSERT INTO '.sql_table('team').' (TMEMBER, TBLOG, TADMIN) '\r
-                      . 'VALUES (' . $memberid .', '.$this->getID().', "'.$admin.'")';\r
-               sql_query($query);\r
-               \r
-               $manager->notify(\r
-                       'PostAddTeamMember',\r
-                       array(\r
-                               'blog' => &$this,\r
-                               'member' => &$tmem,\r
-                               'admin' => $admin\r
-                       )\r
-                       \r
-               );\r
-               \r
-               ACTIONLOG::add(INFO, 'Added ' . $tmem->getDisplayName() . ' (ID=' .\r
-                              $memberid .') to the team of blog "' . $this->getName() . '"');\r
-               \r
-               return 1;\r
-       }\r
-       \r
-       function getID() {\r
-               return $this->blogid;\r
-       }\r
-       \r
-       // returns true if there is a blog with the given shortname (static)\r
-       function exists($name) {\r
-               $r = sql_query('select * FROM '.sql_table('blog').' WHERE bshortname="'.addslashes($name).'"');\r
-               return (mysql_num_rows($r) != 0);\r
-       }\r
-\r
-       // returns true if there is a blog with the given ID (static)\r
-       function existsID($id) {\r
-               $r = sql_query('select * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));\r
-               return (mysql_num_rows($r) != 0);\r
-       }       \r
-       \r
-       \r
-}\r
-\r
-/**\r
-  * This class is used when parsing item templates\r
-  */\r
-class ITEMACTIONS extends BaseActions {\r
-\r
-       // contains an assoc array with parameters that need to be included when\r
-       // generating links to items/archives/... (e.g. catid)\r
-       var $linkparams;\r
-       \r
-       // true when the current user is a blog admin (and thus allowed to edit all items)\r
-       var $allowEditAll;\r
-       \r
-       // timestamp of last visit\r
-       var $lastVisit;\r
-       \r
-       // item currently being handled (mysql result object, see BLOG::showUsingQuery)\r
-       var $currentItem;\r
-       \r
-       // reference to the blog currently being displayed\r
-       var $blog;\r
-       \r
-       // associative array with template info (part name => contents)\r
-       var $template;\r
-       \r
-       // true when comments need to be displayed\r
-       var $showComments;\r
-       \r
-       function ITEMACTIONS(&$blog) {\r
-               // call constructor of superclass first\r
-               $this->BaseActions();   \r
-               \r
-               // extra parameters for created links\r
-               global $catid;\r
-               if ($catid)\r
-                       $this->linkparams = array('catid' => $catid);\r
-                       \r
-               // check if member is blog admin (and thus allowed to edit all items)\r
-               global $member;\r
-               $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));\r
-               $this->setBlog($blog);\r
-       }\r
-\r
-       function getDefinedActions() {\r
-               return array(\r
-                       'blogid',\r
-                       'title',\r
-                       'body',\r
-                       'more',\r
-                       'smartbody',\r
-                       'itemid',\r
-                       'morelink',\r
-                       'category',\r
-                       'categorylink',\r
-                       'author',\r
-                       'authorid',\r
-                       'authorlink',\r
-                       'catid',\r
-                       'karma',\r
-                       'date',\r
-                       'time',\r
-                       'query',\r
-                       'itemlink',\r
-                       'blogurl',\r
-                       'closed',\r
-                       'syndicate_title',\r
-                       'syndicate_description',\r
-                       'karmaposlink',\r
-                       'karmaneglink',\r
-                       'new',\r
-                       'image',\r
-                       'popup',\r
-                       'media',\r
-                       'daylink',\r
-                       'query',\r
-                       'include',\r
-                       'phpinclude',\r
-                       'parsedinclude',\r
-                       'skinfile',\r
-                       'set',\r
-                       'plugin',\r
-                       'edit',\r
-                       'editlink',\r
-                       'editpopupcode',\r
-                       'comments',\r
-                       'relevance'\r
-               );\r
-       }\r
-       \r
-\r
-       \r
-       function setLastVisit($lastVisit) {             $this->lastVisit = $lastVisit; }\r
-       function setParser(&$parser) {                  $this->parser =& $parser; }\r
-       function setCurrentItem(&$item) {               $this->currentItem =& $item; }\r
-       function setBlog(&$blog) {                              $this->blog =& $blog; }\r
-       function setTemplate($template) {               $this->template =& $template; }\r
-       function setShowComments($val) {                $this->showComments = $val; }\r
-       \r
-       // methods used by parser to insert content\r
-       \r
-       function parse_blogid() {               echo $this->blog->getID();      }\r
-       function parse_body() {                 $this->highlightAndParse($this->currentItem->body); }\r
-       function parse_title() {                $this->highlightAndParse($this->currentItem->title); }\r
-       function parse_more() {                 $this->highlightAndParse($this->currentItem->more); }   \r
-       function parse_itemid() {               echo $this->currentItem->itemid; }              \r
-       function parse_category() {             echo $this->currentItem->category; }                            \r
-       function parse_categorylink() { echo createCategoryLink($this->currentItem->catid); }                           \r
-       function parse_catid() {                echo $this->currentItem->catid; }                                       \r
-       function parse_authorid() {             echo $this->currentItem->authorid; }\r
-       function parse_authorlink() {   echo createMemberLink($this->currentItem->authorid, $this->linkparams); }       \r
-       function parse_query() {                echo $this->strHighlight; }\r
-       function parse_itemlink() {             echo createItemLink($this->currentItem->itemid, $this->linkparams); }\r
-       function parse_blogurl() {              echo $this->blog->getURL(); }\r
-       function parse_closed() {               echo $this->currentItem->closed; }\r
-       function parse_relevance() {    echo round($this->currentItem->score,2);}\r
-       \r
-       function parse_karma($type = 'totalscore') {\r
-               global $manager;\r
-\r
-               // get karma object\r
-               $karma =& $manager->getKarma($this->currentItem->itemid);\r
-               \r
-               switch($type) {\r
-                       case 'pos':\r
-                               echo $karma->getNbPosVotes();\r
-                               break;\r
-                       case 'neg':\r
-                               echo $karma->getNbNegVotes();\r
-                               break;                          \r
-                       case 'votes':\r
-                               echo $karma->getNbOfVotes();\r
-                               break;          \r
-                       case 'posp':\r
-                               $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;\r
-                               echo number_format($percentage,2), '%';                         \r
-                               break;                          \r
-                       case 'negp':\r
-                               $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;\r
-                               echo number_format($percentage,2), '%';                         \r
-                               break;                          \r
-                       case 'totalscore':\r
-                       default:\r
-                               echo $karma->getTotalScore();\r
-                               break;\r
-               }\r
-               \r
-       }                                               \r
-\r
-       function parse_author($which = '') {            \r
-               switch($which)\r
-               {\r
-                       case 'realname':\r
-                               echo $this->currentItem->authorname;\r
-                               break;\r
-                       case 'id':\r
-                               echo $this->currentItem->authorid;\r
-                               break;\r
-                       case 'email':\r
-                               echo $this->currentItem->authormail;\r
-                               break;\r
-                       case 'url':\r
-                               echo $this->currentItem->authorurl;\r
-                               break;\r
-                       case 'name':\r
-                       default:\r
-                               echo $this->currentItem->author;\r
-               }\r
-       }               \r
-       \r
-       function parse_smartbody() {\r
-               if (!$this->currentItem->more) {\r
-                       $this->highlightAndParse($this->currentItem->body);\r
-               } else {\r
-                       $this->highlightAndParse($this->currentItem->more);             \r
-               }\r
-       }\r
-\r
-       function parse_morelink() {             \r
-               if ($this->currentItem->more)\r
-                       $this->parser->parse($this->template['MORELINK']);\r
-       }                       \r
-       \r
-       function parse_date($format = '') {\r
-               echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE']);\r
-       }\r
-       \r
-       /**\r
-         * @param format optional strftime format\r
-         */\r
-       function parse_time($format = '') {                     \r
-               echo strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp); \r
-       }\r
-       \r
-       /**\r
-         * @param maxLength optional maximum length\r
-         */\r
-       function parse_syndicate_title($maxLength = 100) { \r
-               $syndicated = strip_tags($this->currentItem->title);\r
-               echo htmlspecialchars(shorten($syndicated,$maxLength,'...'));\r
-       }\r
-       \r
-       /**\r
-         * @param maxLength optional maximum length\r
-         */\r
-       function parse_syndicate_description($maxLength = 250, $addHighlight = 0) { \r
-               $syndicated = strip_tags($this->currentItem->body);\r
-               if ($addHighlight) {\r
-                   echo $this->highlightAndParse(htmlspecialchars(shorten($syndicated,$maxLength,'...')));\r
-               } else {\r
-               echo htmlspecialchars(shorten($syndicated,$maxLength,'...'));\r
-               }\r
-       }\r
-       \r
-       function parse_karmaposlink($text = '') { \r
-               global $CONF;\r
-               $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid='.$this->currentItem->itemid; \r
-               echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;\r
-       }\r
-       \r
-       function parse_karmaneglink($text = '') { \r
-               global $CONF;\r
-               $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem->itemid; \r
-               echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;\r
-       }\r
-       \r
-       function parse_new() {  \r
-               if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))\r
-                       echo $this->template['NEW'];\r
-       }\r
-       \r
-       function parse_image() {\r
-               // image/popup calls have arguments separated by |\r
-               $args = func_get_args();\r
-               $args = explode('|',implode($args,', '));\r
-               call_user_func_array(array(&$this,'createImageCode'),$args);\r
-       }\r
-       function parse_popup() {\r
-               // image/popup calls have arguments separated by |\r
-               $args = func_get_args();\r
-               $args = explode('|',implode($args,', '));\r
-               call_user_func_array(array(&$this,'createPopupCode'),$args);\r
-       }\r
-       function parse_media() {\r
-               // image/popup calls have arguments separated by |\r
-               $args = func_get_args();\r
-               $args = explode('|',implode($args,', '));\r
-               call_user_func_array(array(&$this,'createMediaCode'),$args);\r
-       }\r
-       \r
-       function parse_daylink() {              \r
-               echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);\r
-       }\r
-       \r
-       function parse_comments($maxToShow = 0) {\r
-               if ($maxToShow == 0)\r
-                       $maxToShow = $this->blog->getMaxComments();\r
-                       \r
-               // add comments\r
-               if ($this->showComments && $this->blog->commentsEnabled()) {\r
-                       $comments =& new COMMENTS($this->currentItem->itemid);\r
-                       $comments->setItemActions($this);\r
-                       $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight); \r
-               }\r
-       }\r
-\r
-       /**\r
-         * Executes a plugin templatevar\r
-         *\r
-         * @param pluginName name of plugin (without the NP_)\r
-         * \r
-         * extra parameters can be added\r
-         */\r
-       function parse_plugin($pluginName) {\r
-               global $manager;\r
-               \r
-               // only continue when the plugin is really installed\r
-               if (!$manager->pluginInstalled('NP_' . $pluginName))\r
-                       return;\r
-               \r
-               $plugin =& $manager->getPlugin('NP_' . $pluginName);\r
-               if (!$plugin) return;\r
-\r
-               // get arguments\r
-               $params = func_get_args();\r
-               \r
-               // remove plugin name \r
-               array_shift($params);\r
-               \r
-               // add item reference (array_unshift didn't work)\r
-               $params = array_merge(array(&$this->currentItem),$params);\r
-\r
-               call_user_func_array(array(&$plugin,'doTemplateVar'), $params);\r
-       }\r
-       \r
-       function parse_edit() {\r
-               global $member, $CONF;\r
-               if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {\r
-                       $this->parser->parse($this->template['EDITLINK']);\r
-               }\r
-       }\r
-       \r
-       function parse_editlink() {\r
-               global $CONF;\r
-               echo $CONF['AdminURL'],'bookmarklet.php?action=edit&amp;itemid=',$this->currentItem->itemid;\r
-       }\r
-       \r
-       function parse_editpopupcode() {\r
-               echo "if (event &amp;&amp; event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=yes,width=600,height=500,left=10,top=10,status=yes,resizable=yes');winbm.focus();return false;";\r
-       }\r
-       \r
-       // helper functions\r
-\r
-       /**\r
-         * Parses highlighted text, with limited actions only (to prevent not fully trusted team members\r
-         * from hacking your weblog. \r
-         */\r
-       function highlightAndParse(&$data) {\r
-               // allow only a limited subset of actions (do not allow includes etc, they might be evil)\r
-               $this->parser->actions = array('image','media','popup');\r
-               $this->parser->parse($this->highlight($data));\r
-               $this->parser->actions = $this->getDefinedActions();\r
-       }\r
-       \r
-       function createPopupCode($filename, $width, $height, $text = '') {\r
-               global $CONF;\r
-       \r
-               // select private collection when no collection given\r
-               if (!strstr($filename,'/')) {\r
-                       $filename = $this->currentItem->authorid . '/' . $filename;\r
-               }\r
-               \r
-               $windowwidth = $width;\r
-               $windowheight = $height;\r
-               \r
-               $vars['rawpopuplink']   = $CONF['Self'] . "?imagepopup=" . htmlspecialchars($filename) . "&amp;width=$width&amp;height=$height&amp;imagetext=" . urlencode(htmlspecialchars($text));\r
-               $vars['popupcode']              = "window.open(this.href,'imagepopup','status=no,toolbar=no,scrollbars=no,resizable=yes,width=$windowwidth,height=$windowheight');return false;";\r
-               $vars['popuptext']              = htmlspecialchars($text);\r
-               $vars['popuplink']              = '<a href="' . $vars['rawpopuplink']. '" onclick="'. $vars['popupcode'].'" >' . $vars['popuptext'] . '</a>';\r
-               $vars['width']                  = $width;\r
-               $vars['height']                 = $height;\r
-               $vars['text']                   = $text;\r
-               \r
-               echo TEMPLATE::fill($this->template['POPUP_CODE'],$vars);\r
-       }\r
-       \r
-       function createImageCode($filename, $width, $height, $text = '') {\r
-               global $CONF;\r
-               \r
-               // select private collection when no collection given\r
-               if (!strstr($filename,'/')) {\r
-                       $filename = $this->currentItem->authorid . '/' . $filename;\r
-               }\r
-               \r
-               $windowwidth = $width;\r
-               $windowheight = $height;\r
-               \r
-               $vars['link']                   = htmlspecialchars($CONF['MediaURL']. $filename);\r
-               $vars['text']                   = htmlspecialchars($text);\r
-               $vars['image'] = '<img src="' . $vars['link'] . '" width="' . $width . '" height="' . $height . '" alt="' . $vars['text'] . '" title="' . $vars['text'] . '" />';\r
-               $vars['width']                  = $width;\r
-               $vars['height']                 = $height;\r
-                               \r
-               \r
-               \r
-               echo TEMPLATE::fill($this->template['IMAGE_CODE'],$vars);;\r
-\r
-       }\r
-       \r
-       function createMediaCode($filename, $text = '') {\r
-               global $CONF;\r
-               \r
-               // select private collection when no collection given\r
-               if (!strstr($filename,'/')) {\r
-                       $filename = $this->currentItem->authorid . '/' . $filename;\r
-               }               \r
-\r
-               $vars['link']                   = htmlspecialchars($CONF['MediaURL'] . $filename);\r
-               $vars['text']                   = htmlspecialchars($text);\r
-               $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';\r
-\r
-               echo TEMPLATE::fill($this->template['MEDIA_CODE'],$vars);;\r
-       }\r
-       \r
-       \r
-}\r
-\r
-\r
-// used for mail notification (html -> text)\r
-\r
-function toAscii($html) {\r
-       // strip off most tags \r
-       $html = strip_tags($html,'<a>');\r
-       $to_replace = "/<a[^>]*href=[\"\']([^\"^']*)[\"\'][^>]*>([^<]*)<\/a>/i"; \r
-       _links_init(); \r
-       $ascii = preg_replace_callback ($to_replace, '_links_add', $html); \r
-       $ascii .= "\n\n" . _links_list(); \r
-       return strip_tags($ascii);\r
-}\r
-\r
-function _links_init() { \r
-   global $tmp_links; \r
-   $tmp_links = array(); \r
-} \r
-\r
-function _links_add($match) { \r
-   global $tmp_links; \r
-   array_push($tmp_links, $match[1]); \r
-   return $match[2] . ' [' . sizeof($tmp_links) .']'; \r
-} \r
-\r
-function _links_list() { \r
-   global $tmp_links; \r
-   $output = ''; \r
-   $i = 1; \r
-   foreach ($tmp_links as $current) { \r
-      $output .= "[$i] $current\n"; \r
-      $i++; \r
-   } \r
-   return $output; \r
-} \r
-\r
\r
-?>\r
+<?php
+
+/*
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
+ * Copyright (C) 2002-2006 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-2006 The Nucleus Group
+ * @version $Id: BLOG.php,v 1.5 2006-07-17 20:03:44 kimitake Exp $
+ * $NucleusJP: BLOG.php,v 1.4 2005/08/13 07:31:04 kimitake Exp $
+ */
+
+// temporary: dirt way to separe class ITEMACTIONS from BLOG
+require_once $DIR_LIBS . '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);
+       }
+
+       /**
+        * Shows an archive for a given month
+        *
+        * @param $year
+        *              year
+        * @param $month
+        *              month
+        * @param $template
+        *              String representing the template name to be used
+        */
+       function showArchive($templatename, $year, $month, $day=0) {
+
+               // create extra where clause for select query
+               if ($day == 0) {
+                       $timestamp_start = mktime(0,0,0,$month,1,$year);
+                       $timestamp_end = mktime(0,0,0,$month+1,1,$year);  // also works when $month==12
+               } 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>=' . mysqldate($timestamp_start)
+                                        . ' and i.itime<' . mysqldate($timestamp_end);
+
+
+               $this->readLogAmount($templatename,0,$extra_query,'',1,1);
+
+       }
+
+
+       // sets/gets current category (only when category exists)
+       function setSelectedCategory($catid) {
+               if ($this->isValidCategory($catid) || (intval($catid) == 0))
+                       $this->selectedcatid = intval($catid);
+       }
+
+       function setSelectedCategoryByName($catname) {
+               $this->setSelectedCategory($this->getCategoryIdFromName($catname));
+       }
+
+       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);
+       }
+
+       function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1) {
+               global $CONF, $manager;
+
+               $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');
+               if ($lastVisit != 0)
+                       $lastVisit = $this->getCorrectTime($lastVisit);
+
+               // set templatename as global variable (so plugins can access it)
+               global $currentTemplateName;
+               $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
+               while ($item = mysql_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
+                       $old_date = 0;
+                       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 = strftime($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 = 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 = mysql_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)));
+               }
+
+               mysql_free_result($items);      // free memory
+
+               return $numrows;
+
+       }
+
+       function showOneitem($itemid, $template, $highlight) {
+               $extraQuery = ' and inumber=' . intval($itemid);
+
+               return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);
+       }
+
+
+       /**
+         * Adds an item to this blog
+         */
+       function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft) {
+               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));
+
+               $title = addslashes($title);
+               $body = addslashes($body);
+               $more = addslashes($more);
+
+               $query = 'INSERT INTO '.sql_table('item').' (ITITLE, IBODY, IMORE, IBLOG, IAUTHOR, ITIME, ICLOSED, IDRAFT, ICAT) '
+                          . "VALUES ('$title', '$body', '$more', $blogid, $authorid, '$timestamp', $closed, $draft, $catid)";
+               sql_query($query);
+               $itemid = mysql_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, stripslashes($title), stripslashes($body));
+
+               return $itemid;
+       }
+
+       function sendNewItemNotification($itemid, $title, $body) {
+               global $CONF, $member;
+
+               // create text version of html post
+               $ascii = toAscii($body);
+
+               $mailto_msg = _NOTIFY_NI_MSG . " \n";
+               $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $itemid . "\n\n";
+               $mailto_msg .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";
+               $mailto_msg .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";
+               $mailto_msg .= getMailFooter();
+
+               $mailto_title = $this->getName() . ': ' . _NOTIFY_NI_TITLE;
+
+               $frommail = $member->getNotifyFromMailAddress();
+
+               $notify =& new NOTIFICATION($this->getNotifyAddress());
+               $notify->notify($mailto_title, $mailto_msg , $frommail);
+
+
+
+       }
+
+
+       /**
+         * Creates a new category for this blog
+         *
+         * @param $catName
+         *             name of the new category. When empty, a name is generated automatically
+         *             (starting with newcat)
+         * @param $catDescription
+         *             description of the new category. Defaults to 'New Category'
+         *
+         * @returns
+         *             the new category-id in case of success.
+         *             0 on failure
+         */
+       function createNewCategory($catName = '', $catDescription = 'New category') {
+               global $member, $manager;
+
+               if ($member->blogAdminRights($this->getID())) {
+                       // generate
+                       if ($catName == '')
+                       {
+                               $catName = 'newcat';
+                               $i = 1;
+
+                               $res = sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID());
+                               while (mysql_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 '.sql_table('category').' (cblog, cname, cdesc) VALUES (' . $this->getID() . ", '" . addslashes($catName) . "', '" . addslashes($catDescription) . "')";
+                       sql_query($query);
+                       $catid = mysql_insert_id();
+
+                       $manager->notify(
+                               'PostAddCategory',
+                               array(
+                                       'blog' => &$this,
+                                       'name' => $catName,
+                                       'description' => $catDescription,
+                                       'catid' => $catid
+                               )
+                       );
+
+                       return $catid;
+               } else {
+                       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'         => htmlspecialchars($query),
+                                       'blogid'        => $this->getID()
+                               );
+                               echo TEMPLATE::fill($template['SEARCH_NOTHINGFOUND'],$vars);
+                       }
+               }
+
+               return $amountfound;
+       }
+
+       /**
+        * Returns an SQL query to use for a search query
+        *
+        * @param $query
+        *              search query
+        * @param $amountMonths
+        *              amount of months to search back. Default = 0 = unlimited
+        * @param $mode
+        *              either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
+        * @returns $highlight
+        *              words to highlight (out parameter)
+        * @returns
+        *              either a full SQL query, or an empty string (if querystring empty)
+        * @note
+        *              No LIMIT clause is added. (caller should add this if multiple pages are requested)
+        */
+       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, 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';
+                       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'
+                          . ' and i.idraft=0'  // exclude drafts
+                          . $selectblogs
+                                       // don't show future items
+                          . ' and i.itime<=' . mysqldate($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>' . mysqldate($timestamp_start);
+               }
+
+               if ($mode == '')
+               {
+                       if ($select)
+                               $query .= ' ORDER BY score DESC';
+                       else
+                               $query .= ' ORDER BY i.itime DESC ';
+               }
+
+               return $query;
+       }
+
+       /**
+        * Returns the SQL query that's normally used to display the blog items on the index type skins
+        *
+        * @param $mode
+        *              either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
+        * @returns
+        *              either a full SQL query, or an empty string
+        * @note
+        *              No LIMIT clause is added. (caller should add this if multiple pages are requested)
+        */
+       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'
+                          . ' and i.idraft=0'  // exclude drafts
+                                       // don't show future items
+                          . ' and i.itime<=' . mysqldate($this->getCorrectTime());
+
+               if ($this->getSelectedCategory())
+                       $query .= ' and i.icat=' . $this->getSelectedCategory() . ' ';
+
+
+               $query .= $extraQuery;
+
+               if ($mode == '')
+                       $query .= ' ORDER BY i.itime DESC';
+
+               return $query;
+       }
+
+       /**
+         * Shows the archivelist using the given template
+         */
+       function showArchiveList($template, $mode = 'month', $limit = 0) {
+               global $CONF, $catid, $manager;
+
+               if ($catid)
+                       $linkparams = array('catid' => $catid);
+
+               $template =& $manager->getTemplate($template);
+               $data['blogid'] = $this->getID();
+
+               echo TEMPLATE::fill($template['ARCHIVELIST_HEADER'],$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()
+               . ' and itime <=' . mysqldate($this->getCorrectTime())  // don't show future items!
+               . ' and idraft=0'; // don't show draft items
+
+               if ($catid)
+                       $query .= ' and icat=' . intval($catid);
+
+               $query .= ' GROUP BY Year, Month';
+               if ($mode == 'day')
+                       $query .= ', Day';
+
+
+               $query .= ' ORDER BY itime DESC';
+
+               if ($limit > 0)
+                       $query .= ' LIMIT ' . intval($limit);
+
+               $res = sql_query($query);
+
+               while ($current = mysql_fetch_object($res)) {
+                       $current->itime = strtotime($current->itime);   // string time -> unix timestamp
+
+                       if ($mode == 'day') {
+                               $archivedate = date('Y-m-d',$current->itime);
+                               $archive['day'] = date('d',$current->itime);
+                       } else {
+                               $archivedate = date('Y-m',$current->itime);
+                       }
+                       $data['month'] = date('m',$current->itime);
+                       $data['year'] = date('Y',$current->itime);
+                       $data['archivelink'] = createArchiveLink($this->getID(),$archivedate,$linkparams);
+
+                       $temp = TEMPLATE::fill($template['ARCHIVELIST_LISTITEM'],$data);
+                       echo strftime($temp,$current->itime);
+
+               }
+
+               mysql_free_result($res);
+
+               echo TEMPLATE::fill($template['ARCHIVELIST_FOOTER'],$data);
+       }
+
+
+       /**
+         * Shows the list of categories using a given template
+         */
+       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 = createArchiveLink($this->getID(), $archive, '');
+                       $linkparams['blogid'] = $this->getID();
+                       $linkparams['archive'] = $archive;
+               } else if ($archivelist) {
+                       $blogurl = createArchiveListLink($this->getID(), '');
+                       $linkparams['archivelist'] = $archivelist;
+               } else {
+                       $blogurl = createBlogidLink($this->getID(), '');
+                       $linkparams['blogid'] = $this->getID();
+               }
+
+               //$blogurl = $this->getURL() . $qargs;
+               $blogurl = createBlogLink($this->getURL(), $linkparams);
+
+               $template =& $manager->getTemplate($template);
+
+               echo TEMPLATE::fill((isset($template['CATLIST_HEADER']) ? $template['CATLIST_HEADER'] : null),
+                                                       array(
+                                                               'blogid' => $this->getID(),
+                                                               'blogurl' => $blogurl,
+                                                               'self' => $CONF['Self']
+                                                       ));
+
+               $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 = mysql_fetch_assoc($res)) {
+                       $data['blogid'] = $this->getID();
+                       $data['blogurl'] = $blogurl;
+                       $data['catlink'] = createLink(
+                                                               'category',
+                                                               array(
+                                                                       'catid' => $data['catid'],
+                                                                       'name' => $data['catname'],
+                                                                       'extra' => $linkparams
+                                                               )
+                                                          );
+                       $data['self'] = $CONF['Self'];
+
+                       echo TEMPLATE::fill((isset($template['CATLIST_LISTITEM']) ? $template['CATLIST_LISTITEM'] : null), $data);
+                       //$temp = TEMPLATE::fill((isset($template['CATLIST_LISTITEM']) ? $template['CATLIST_LISTITEM'] : null), $data);
+                       //echo strftime($temp, $current->itime);
+
+               }
+
+               mysql_free_result($res);
+
+               echo TEMPLATE::fill((isset($template['CATLIST_FOOTER']) ? $template['CATLIST_FOOTER'] : null),
+                                                       array(
+                                                               'blogid' => $this->getID(),
+                                                               'blogurl' => $blogurl,
+                                                               'self' => $CONF['Self']
+                                                       ));
+       }
+
+       /**
+         * Blogsettings functions
+         */
+
+       function readSettings() {
+               $query =  'SELECT *'
+                          . ' FROM '.sql_table('blog')
+                          . ' WHERE bnumber=' . $this->blogid;
+               $res = sql_query($query);
+
+               $this->isValid = (mysql_num_rows($res) > 0);
+               if (!$this->isValid)
+                       return;
+
+               $this->settings = mysql_fetch_assoc($res);
+       }
+
+       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='" . addslashes($this->getName()) . "',"
+                          . "     bshortname='". addslashes($this->getShortName()) . "',"
+                          . "     bcomments=". intval($this->commentsEnabled()) . ","
+                          . "     bmaxcomments=" . intval($this->getMaxComments()) . ","
+                          . "     btimeoffset=" . $offset . ","
+                          . "     bpublic=" . intval($this->isPublic()) . ","
+                          . "     breqemail=" . intval($this->emailRequired()) . ","
+                          . "     bsendping=" . intval($this->pingUserland()) . ","
+                          . "     bconvertbreaks=" . intval($this->convertBreaks()) . ","
+                          . "     ballowpast=" . intval($this->allowPastPosting()) . ","
+                          . "     bnotify='" . addslashes($this->getNotifyAddress()) . "',"
+                          . "     bnotifytype=" . intval($this->getNotifyType()) . ","
+                          . "     burl='" . addslashes($this->getURL()) . "',"
+                          . "     bupdate='" . addslashes($this->getUpdateFile()) . "',"
+                          . "     bdesc='" . addslashes($this->getDescription()) . "',"
+                          . "     bdefcat=" . intval($this->getDefaultCategory()) . ","
+                          . "     bdefskin=" . intval($this->getDefaultSkin()) . ","
+                          . "     bincludesearch=" . intval($this->getSearchable())
+                          . " WHERE bnumber=" . intval($this->getID());
+               sql_query($query);
+
+       }
+
+
+
+       // update update file if requested
+       function updateUpdatefile() {
+                if ($this->getUpdateFile()) {
+                       $f_update = fopen($this->getUpdateFile(),'w');
+                       fputs($f_update,$this->getCorrectTime());
+                       fclose($f_update);
+                }
+
+       }
+
+       /**
+         * Sends a XML-RPC ping message to Userland, so the weblog can
+         * show up in the weblogs.com updates-list
+         */
+       function sendUserlandPing() {
+               global $php_errormsg;
+
+                if ($this->pingUserland()) {
+                         // testmessage for adding an item
+                         $message = new xmlrpcmsg('weblogUpdates.ping',array(
+                                       new xmlrpcval($this->getName(),'string'),
+                                       new xmlrpcval($this->getURL(),'string')
+                         ));
+
+                         $c = new xmlrpc_client('/RPC2', 'rpc.weblogs.com', 80);
+
+                         // $c->setDebug(1);
+
+                         $r = $c->send($message,15); // 15 seconds timeout...
+
+                         if (($r == 0) && ($r->errno || $r->errstring)) {
+                               return 'Error ' . $r->errno . ' : ' . $r->errstring;
+                         } elseif (($r == 0) && ($php_errormsg)) {
+                               return 'PHP Error: ' . $php_errormsg;
+                         } elseif ($r == 0) {
+                               return 'Error while trying to send ping. Sorry about that.';
+                         } elseif ($r->faultCode() != 0) {
+                               return 'Error: ' . $r->faultString();
+                         } else {
+                                 $r = $r->value();     // get response struct
+                                 // get values
+                                 $flerror = $r->structmem('flerror');
+                                 $flerror = $flerror->scalarval();
+
+
+                                 $message = $r->structmem('message');
+                                 $message = $message->scalarval();
+
+                                 if ($flerror != 0)
+                                       return 'Error (flerror=1): ' . $message;
+                                 else
+                                       return 'Success: ' . $message;
+                         }
+                }
+       }
+
+       function isValidCategory($catid) {
+               $query = 'SELECT * FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' and catid=' . intval($catid);
+               $res = mysql_query($query);
+               return (mysql_num_rows($res) != 0);
+       }
+
+       function getCategoryName($catid) {
+               $res = mysql_query('SELECT cname FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));
+               $o = mysql_fetch_object($res);
+               return $o->cname;
+       }
+
+       function getCategoryDesc($catid) {
+               $res = mysql_query('SELECT cdesc FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));
+               $o = mysql_fetch_object($res);
+               return $o->cdesc;
+       }
+
+       function getCategoryIdFromName($name) {
+               $res = mysql_query('SELECT catid FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and cname="' . addslashes($name) . '"');
+               if (mysql_num_rows($res) > 0) {
+                       $o = mysql_fetch_object($res);
+                       return $o->catid;
+               } else {
+                       return $this->getDefaultCategory();
+               }
+       }
+
+       function pingUserland() {
+               return $this->getSetting('bsendping');
+       }
+
+       function setPingUserland($val) {
+               $this->setSetting('bsendping',$val);
+       }
+
+       function convertBreaks() {
+               return $this->getSetting('bconvertbreaks');
+       }
+
+       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  }
+
+       function setConvertBreaks($val) {
+               $this->setSetting('bconvertbreaks',$val);
+       }
+       function setAllowPastPosting($val) {
+               $this->setSetting('ballowpast',$val);
+       }
+       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;
+       }
+
+
+       // tries to add a member to the team. Returns false if the member was already on
+       // the team
+       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 '.sql_table('team').' (TMEMBER, TBLOG, TADMIN) '
+                          . 'VALUES (' . $memberid .', '.$this->getID().', "'.$admin.'")';
+               sql_query($query);
+
+               $manager->notify(
+                       'PostAddTeamMember',
+                       array(
+                               'blog' => &$this,
+                               'member' => &$tmem,
+                               'admin' => $admin
+                       )
+
+               );
+
+               ACTIONLOG::add(INFO, 'Added ' . $tmem->getDisplayName() . ' (ID=' .
+                                          $memberid .') to the team of blog "' . $this->getName() . '"');
+
+               return 1;
+       }
+
+       function getID() {
+               return $this->blogid;
+       }
+
+       // returns true if there is a blog with the given shortname (static)
+       function exists($name) {
+               $r = sql_query('select * FROM '.sql_table('blog').' WHERE bshortname="'.addslashes($name).'"');
+               return (mysql_num_rows($r) != 0);
+       }
+
+       // returns true if there is a blog with the given ID (static)
+       function existsID($id) {
+               $r = sql_query('select * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));
+               return (mysql_num_rows($r) != 0);
+       }
+
+
+}
+
+?>