4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2012 The Nucleus Group
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * (see nucleus/documentation/index.html#license for more info)
14 * A class representing a blog and containing functions to get that blog shown
17 * @license http://nucleuscms.org/license.txt GNU General Public License
18 * @copyright Copyright (C) 2002-2009 The Nucleus Group
19 * @version $Id: BLOG.php 1624 2012-01-09 11:36:20Z sakamocchi $
22 if ( !function_exists('requestVar') ) exit;
23 require_once dirname(__FILE__) . '/ITEMACTIONS.php';
30 // After creating an object of the blog class, contains true if the BLOG object is
31 // valid (the blog exists)
34 // associative array, containing all blogsettings (use the get/set functions instead)
37 // ID of currently selected category
38 private $selectedcatid;
42 * Creates a new BLOG object for the given blog
44 * @param integer $id blogid
47 public function __construct($id)
51 $this->blogid = (integer) $id;
52 $this->readSettings();
53 $this->setSelectedCategory($catid);
59 * Shows the given amount of items for this blog
61 * @param string $template String representing the template _NAME_ (!)
62 * @param integer $amountEntries amount of entries to show
63 * @param integer $startpos offset from where items should be shown (e.g. 5 = start at fifth item)
64 * @return integer amount of items shown
66 public function readLog($template, $amountEntries, $offset = 0, $startpos = 0)
68 return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);
73 * Shows an archive for a given month
75 * @param integer $year year
76 * @param integer $month month
77 * @param string $template String representing the template name to be used
80 public function showArchive($templatename, $year, $month=0, $day=0)
82 // create extra where clause for select query
83 if ( $day == 0 && $month != 0 )
85 $timestamp_start = mktime(0,0,0,$month,1,$year);
86 // also works when $month==12
87 $timestamp_end = mktime(0,0,0,$month+1,1,$year);
89 elseif ( $month == 0 )
91 $timestamp_start = mktime(0,0,0,1,1,$year);
92 // also works when $month==12
93 $timestamp_end = mktime(0,0,0,12,31,$year);
97 $timestamp_start = mktime(0,0,0,$month,$day,$year);
98 $timestamp_end = mktime(0,0,0,$month,$day+1,$year);
100 $extra_query = " and i.itime>=%s and i.itime<%s";
101 $extra_query = sprintf($extra_query, DB::formatDateTime($timestamp_start), DB::formatDateTime($timestamp_end));
103 $this->readLogAmount($templatename,0,$extra_query,'',1,1);
108 * Blog::setSelectedCategory()
109 * Sets the selected category by id (only when category exists)
111 * @param integer $catid ID for category
114 public function setSelectedCategory($catid)
116 if ( $this->isValidCategory($catid) || (intval($catid) == 0) )
118 $this->selectedcatid = intval($catid);
124 * Blog::setSelectedCategoryByName()
125 * Sets the selected category by name
127 * @param string $catname name of category
130 public function setSelectedCategoryByName($catname)
132 $this->setSelectedCategory($this->getCategoryIdFromName($catname));
137 * Blog::getSelectedCategory()
138 * Returns the selected category
143 public function getSelectedCategory()
145 return $this->selectedcatid;
149 * Shows the given amount of items for this blog
151 * @param string $template string representing the template _NAME_ (!)
152 * @param integer $amountEntries amount of entries to show (0 = no limit)
153 * @param string $extraQuery extra conditions to be added to the query
154 * @param string $highlight contains a query that should be highlighted
155 * @param integer $comments 1=show comments 0=don't show comments
156 * @param integer $dateheads 1=show dateheads 0=don't show dateheads
157 * @param integer $offset offset
158 * @return integer amount of items shown
160 private function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0)
162 $query = $this->getSqlBlog($extraQuery);
164 if ( $amountEntries > 0 )
166 // $offset zou moeten worden:
167 // (($startpos / $amountentries) + 1) * $offset ... later testen ...
168 $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);
170 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
174 * Blog::showUsingQuery()
175 * Do the job for readLogAmmount
177 * @param string $templateName template name
178 * @param string $query string for query
179 * @param string $highlight string to be highlighted
180 * @param integer $comments the number of comments
181 * @param boolean $dateheads date header is needed or not
182 * @return integer the number of rows as a result of mysql query
184 private function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1)
186 global $CONF, $manager, $currentTemplateName;
189 $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');
190 if ( $lastVisit != 0 )
192 $lastVisit = $this->getCorrectTime($lastVisit);
195 // set templatename as global variable (so plugins can access it)
196 $currentTemplateName = $templateName;
197 $template =& $manager->getTemplate($templateName);
199 // create parser object & action handler
200 $handler = new ItemActions($this);
201 $handler->setTemplate($template);
202 $handler->setHighlight($highlight);
203 $handler->setLastVisit($lastVisit);
204 $handler->setShowComments($comments);
206 $parser = new Parser($handler);
209 $items = DB::getResult($query);
211 // loop over all items
213 foreach ( $items as $item )
215 // string timestamp -> unix timestamp
216 $item['timestamp'] = strtotime($item['itime']);
218 // action handler needs to know the item we're handling
219 $handler->setCurrentItem($item);
221 // add date header if needed
224 $new_date = date('dFY', $item['timestamp']);
225 if ( $new_date != $old_date )
227 // unless this is the first time, write date footer
228 $timestamp = $item['timestamp'];
229 if ( $old_date != 0 )
231 $oldTS = strtotime($old_date);
232 $data = array('blog' => $this, 'timestamp' => $oldTS);
233 $manager->notify('PreDateFoot', $data);
235 if ( !in_array('DATE_FOOTER', $template) || empty($template['DATE_FOOTER']) )
241 $tmp_footer = i18n::formatted_datetime($template['DATE_FOOTER'], $oldTS);
243 $parser->parse($tmp_footer);
244 $manager->notify('PostDateFoot', $data);
247 $data = array('blog' => $this, 'timestamp' => $timestamp);
248 $manager->notify('PreDateHead', $data);
250 // note, to use templatvars in the dateheader, the %-characters need to be doubled in
251 // order to be preserved by strftime
252 if ( !in_array('DATE_HEADER', $template) || empty($template['DATE_HEADER']) )
258 $tmp_header = i18n::formatted_datetime($template['DATE_HEADER'], $timestamp);
260 $parser->parse($tmp_header);
261 $manager->notify('PostDateHead', $data);
263 $old_date = $new_date;
267 $parser->parse($template['ITEM_HEADER']);
268 $data = array('blog' => $this, 'item' => &$item);
269 $manager->notify('PreItem', $data);
270 $parser->parse($template['ITEM']);
271 $manager->notify('PostItem', $data);
272 $parser->parse($template['ITEM_FOOTER']);
275 $numrows = $items->rowCount();
277 // add another date footer if there was at least one item
278 if ( ($numrows > 0) && $dateheads )
280 $data = array('blog' => $this, 'timestamp' => strtotime($old_date));
281 $manager->notify('PreDateFoot', $data);
282 $parser->parse($template['DATE_FOOTER']);
283 $manager->notify('PostDateFoot', $data);
286 $items->closeCursor();
291 * Blog::showOneitem()
292 * Simplified function for showing only one item
294 * @param integer $itemid ID for item
295 * @param array $template template for item
296 * @param string $highlight string for highlight
299 public function showOneitem($itemid, $template, $highlight)
301 $extraQuery = ' and inumber=' . intval($itemid);
303 return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);
308 * Adds an item to this blog
310 * @param integer $catid ID for category
311 * @param string $title ID for
312 * @param string $body text for body
313 * @param string $more text for more
314 * @param integer $blogid ID for blog
315 * @param integer $authorid ID for author
316 * @param timestamp $timestamp UNIX timestamp for post
317 * @param boolean $closed opened or closed
318 * @param boolean $draft draft or not
319 * @param boolean $posted posted or not
320 * @return integer ID for added item
322 function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft, $posted='1')
326 $blogid = (integer) $blogid;
327 $authorid = (integer) $authorid;
331 $catid = intval($catid);
333 // convert newlines to <br />
334 if ( $this->convertBreaks() )
336 $body = addBreaks($body);
337 $more = addBreaks($more);
340 if ( $closed != '1' )
349 if ( !$this->isValidCategory($catid) )
351 $catid = $this->getDefaultCategory();
355 if ( $timestamp > $this->getCorrectTime() )
360 $timestamp = date('Y-m-d H:i:s',$timestamp);
362 $data = array('title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => $this, 'authorid' => &$authorid, 'timestamp' => &$timestamp, 'closed' => &$closed, 'draft' => &$draft, 'catid' => &$catid);
363 $manager->notify('PreAddItem', $data);
365 $ititle = DB::quoteValue($title);
366 $ibody = DB::quoteValue($body);
367 $imore = DB::quoteValue($more);
368 $timestamp = DB::formatDateTime(strtotime($timestamp));
370 $query = "INSERT INTO %s (ITITLE, IBODY, IMORE, IBLOG, IAUTHOR, ITIME, ICLOSED, IDRAFT, ICAT, IPOSTED) VALUES (%s, %s, %s, %d, %d, %s, %s, %s, %s, %s)";
371 $query = sprintf($query, sql_table('item'), $ititle, $ibody, $imore, $blogid, $authorid, $timestamp, $closed, $draft, $catid, $posted);
373 $itemid = DB::getInsertId();
375 $data = array('itemid' => $itemid);
376 $manager->notify('PostAddItem', $data);
380 $this->updateUpdateFile();
382 // send notification mail
383 if ( !$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem() )
385 $this->sendNewItemNotification($itemid, $title, $body);
391 * Blog::sendNewItemNotification()
392 * Send a new item notification to the notification list
394 * @param string $itemid ID of the item
395 * @param string $title title of the item
396 * @param string $body body of the item
399 public function sendNewItemNotification($itemid, $title, $body)
401 global $CONF, $member;
403 $ascii = Entity::anchor_footnoting($body);
405 $message = _NOTIFY_NI_MSG . " \n";
406 $temp = parse_url($CONF['Self']);
407 if ( $temp['scheme'] )
409 $message .= Link::create_item_link($itemid) . "\n\n";
413 $tempurl = $this->getURL();
414 if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )
416 $message .= $tempurl . '?itemid=' . $itemid . "\n\n";
420 $message .= $tempurl . '/?itemid=' . $itemid . "\n\n";
423 $message .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";
424 $message .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";
425 $message .= NOTIFICATION::get_mail_footer();
427 $subject = $this->getName() . ': ' . _NOTIFY_NI_TITLE;
429 $from = $member->getNotifyFromMailAddress();
431 NOTIFICATION::mail($this->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());
436 * Blog::createNewCategory()
437 * Creates a new category for this blog
439 * @param string $catName name of the new category. When empty, a name is generated automatically (starting with newcat)
440 * @param string $catDescription description of the new category. Defaults to 'New Category'
441 * @return integer ID for new category on success. 0 on failure
443 public function createNewCategory($catName = '', $catDescription = _CREATED_NEW_CATEGORY_DESC)
445 global $member, $manager;
447 if ( !$member->blogAdminRights($this->blogid) )
453 if ( $catName == '' )
455 $catName = _CREATED_NEW_CATEGORY_NAME;
458 $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);
459 while ( $res->rowCount() > 0 )
462 $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);
465 $catName = $catName . $i;
471 'description' => $catDescription
473 $manager->notify('PreAddCategory', $data);
475 $query = "INSERT INTO %s (cblog, cname, cdesc) VALUES (%d, %s, %s)";
476 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, DB::quoteValue($catName), DB::quoteValue($catDescription));
478 $catid = DB::getInsertId();
483 'description' => $catDescription,
486 $manager->notify('PostAddCategory', $data);
493 * Searches all months of this blog for the given query
495 * @param string $query search query
496 * @param array $template template to be used (__NAME__ of the template)
497 * @param integer $amountMonths max amount of months to be search (0 = all)
498 * @param integer $maxresults max number of results to show
499 * @param integer $startpos offset
500 * @return amount of hits found
502 public function search($query, $template, $amountMonths, $maxresults, $startpos) {
503 global $CONF, $manager;
506 $sqlquery = $this->getSqlSearch($query, $amountMonths, $highlight);
508 if ( $sqlquery == '' )
510 // no query -> show everything
512 $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);
516 // add LIMIT to query (to split search results into pages)
517 if ( intval($maxresults > 0) )
519 $sqlquery .= ' LIMIT ' . intval($startpos) . ',' . intval($maxresults);
523 $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);
525 // when no results were found, show a message
526 if ( $amountfound == 0 )
528 $template =& $manager->getTemplate($template);
530 'query' => Entity::hsc($query),
531 'blogid' => $this->blogid
533 echo Template::fill($template['SEARCH_NOTHINGFOUND'], $vars);
540 * Blog::getSqlSearch()
541 * Returns an SQL query to use for a search query
542 * No LIMIT clause is added. (caller should add this if multiple pages are requested)
544 * @param string $query search query
545 * @param integer $amountMonths amount of months to search back. Default = 0 = unlimited
546 * @param string $mode either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
547 * @return string $highlight words to highlight (out parameter)
548 * @return string either a full SQL query, or an empty string (if querystring empty)
550 public function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')
552 $searchclass = new Search($query);
554 $highlight = $searchclass->inclusive;
556 // if querystring is empty, return empty string
557 if ( $searchclass->inclusive == '' )
562 $where = $searchclass->boolean_sql_where('ititle,ibody,imore');
563 $select = $searchclass->boolean_sql_select('ititle,ibody,imore');
565 // get list of blogs to search
566 $blogs = $searchclass->blogs; // array containing blogs that always need to be included
567 $blogs[] = $this->blogid; // also search current blog (duh)
568 $blogs = array_unique($blogs); // remove duplicates
570 if ( count($blogs) > 0 )
572 $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';
577 $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, i.itime, i.imore as more, i.icat as catid, i.iclosed as closed,
578 m.mname as author, m.mrealname as authorname, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl,
579 c.cname as category';
583 $query .= ', '.$select. ' as score ';
588 $query = 'SELECT COUNT(*) as result ';
591 $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
592 . ' WHERE i.iauthor=m.mnumber'
593 . ' and i.icat=c.catid'
597 // don't show future items
598 . ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime())
601 // take into account amount of months to search
602 if ( $amountMonths > 0 )
604 $localtime = getdate($this->getCorrectTime());
605 $timestamp_start = mktime(0,0,0,$localtime['mon'] - $amountMonths,1,$localtime['year']);
606 $query .= ' and i.itime>' . DB::formatDateTime($timestamp_start);
613 $query .= ' ORDER BY score DESC';
617 $query .= ' ORDER BY i.itime DESC ';
626 * Returns the SQL query that's normally used to display the blog items on the index type skins
627 * No LIMIT clause is added. (caller should add this if multiple pages are requested)
629 * @param string $extraQuery extra query string
630 * @param string $mode either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
631 * @return string either a full SQL query, or an empty string
633 public function getSqlBlog($extraQuery, $mode = '')
637 $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author,
638 m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail,
639 m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';
643 $query = 'SELECT COUNT(*) as result ';
646 $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
647 . ' WHERE i.iblog='.$this->blogid
648 . ' and i.iauthor=m.mnumber'
649 . ' and i.icat=c.catid'
650 . ' and i.idraft=0' // exclude drafts
651 . ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime()); // don't show future items
653 if ( $this->selectedcatid )
655 $query .= ' and i.icat=' . $this->selectedcatid . ' ';
658 $query .= $extraQuery;
662 $query .= ' ORDER BY i.itime DESC';
668 * Blog::showArchiveList()
669 * Shows the archivelist using the given template
671 * @param string $template template name
672 * @param string $mode year/month/day
673 * @param integer $limit limit of record count
676 public function showArchiveList($template, $mode = 'month', $limit = 0)
678 global $CONF, $catid, $manager;
680 if ( !isset ($linkparams) )
682 $linkparams = array();
687 $linkparams = array('catid' => $catid);
690 $template =& $manager->getTemplate($template);
691 $listitem['blogid'] = $this->blogid;
693 if ( !array_key_exists('ARCHIVELIST_HEADER', $template) || !$template['ARCHIVELIST_HEADER'] )
699 $tplt = $template['ARCHIVELIST_HEADER'];
702 echo Template::fill($tplt, $listitem);
704 $query = 'SELECT itime, SUBSTRING(itime,1,4) AS Year, SUBSTRING(itime,6,2) AS Month, SUBSTRING(itime,9,2) AS Day'
705 . ' FROM '.sql_table('item')
706 . ' WHERE iblog=' . $this->blogid
707 . ' AND itime <=' . DB::formatDateTime($this->getCorrectTime()) // don't show future items!
708 . ' AND idraft=0'; // don't show draft items
712 $query .= ' and icat=' . intval($catid);
715 $query .= ' GROUP BY Year';
716 if ( $mode == 'month' || $mode == 'day' )
720 if ( $mode == 'day' )
725 $query .= ' ORDER BY itime DESC';
729 $query .= ' LIMIT ' . intval($limit);
732 $res = DB::getResult($query);
733 foreach ( $res as $current )
735 /* string time -> unix timestamp */
736 $current['itime'] = strtotime($current['itime']);
738 if ( $mode == 'day' )
740 $archivedate = date('Y-m-d',$current['itime']);
741 $archive['day'] = date('d',$current['itime']);
742 $listitem['day'] = date('d',$current['itime']);
743 $listitem['month'] = date('m',$current['itime']);
744 $archive['month'] = $listitem['month'];
746 elseif ( $mode == 'year' )
748 $archivedate = date('Y',$current['itime']);
749 $listitem['day'] = '';
750 $listitem['month'] = '';
751 $archive['day'] = '';
752 $archive['month'] = '';
756 $archivedate = date('Y-m',$current['itime']);
757 $listitem['month'] = date('m',$current['itime']);
758 $archive['month'] = $listitem['month'];
759 $listitem['day'] = '';
760 $archive['day'] = '';
763 $listitem['year'] = date('Y',$current['itime']);
764 $archive['year'] = $listitem['year'];
765 $listitem['archivelink'] = Link::create_archive_link($this->blogid,$archivedate,$linkparams);
767 $data = array('listitem' => &$listitem);
768 $manager->notify('PreArchiveListItem', $data);
770 $temp = Template::fill($template['ARCHIVELIST_LISTITEM'],$listitem);
771 echo i18n::formatted_datetime($temp, $current['itime']);
777 if ( !array_key_exists('ARCHIVELIST_FOOTER', $template) || !$template['ARCHIVELIST_FOOTER'] )
783 $tplt = $template['ARCHIVELIST_FOOTER'];
786 echo Template::fill($tplt, $listitem);
791 * Blog::showCategoryList()
792 * Shows the list of categories using a given template
794 * @param string $template Template Name
797 public function showCategoryList($template)
799 global $CONF, $archive, $archivelist, $manager;
802 * determine arguments next to catids
803 * I guess this can be done in a better way, but it works
805 $linkparams = array();
808 $blogurl = Link::create_archive_link($this->blogid, $archive, '');
809 $linkparams['blogid'] = $this->blogid;
810 $linkparams['archive'] = $archive;
812 else if ( $archivelist )
814 $blogurl = Link::create_archivelist_link($this->blogid, '');
815 $linkparams['archivelist'] = $archivelist;
819 $blogurl = Link::create_blogid_link($this->blogid, '');
820 $linkparams['blogid'] = $this->blogid;
823 $template =& $manager->getTemplate($template);
825 //: Change: Set nocatselected variable
826 if ( $this->selectedcatid )
828 $nocatselected = 'no';
832 $nocatselected = 'yes';
836 'blogid' => $this->blogid,
837 'blogurl' => $blogurl,
838 'self' => $CONF['Self'],
839 'catiscurrent' => $nocatselected, // Change: Set catiscurrent template variable for header
840 'currentcat' => $nocatselected
843 /* output header of category list item */
844 if ( !array_key_exists('CATLIST_HEADER', $template) || empty($template['CATLIST_HEADER']) )
846 echo Template::fill(NULL, $args);
850 echo Template::fill($template['CATLIST_HEADER'], $args);
853 $query = "SELECT catid, cdesc as catdesc, cname as catname FROM %s WHERE cblog=%d ORDER BY cname ASC;";
854 $query = sprintf($query, sql_table('category'), (integer) $this->blogid);
855 $res = DB::getResult($query);
857 foreach ( $res as $row )
860 'catid' => $row['catid'],
861 'name' => $row['catname'],
862 'extra' => $linkparams
865 $row['blogid'] = $this->blogid;
866 $row['blogurl'] = $blogurl;
867 $row['catlink'] = Link::create_link('category', $args);
868 $row['self'] = $CONF['Self'];
870 // this gives catiscurrent = no when no category is selected.
871 $row['catiscurrent'] = 'no';
872 $row['currentcat'] = 'no';
874 if ( $this->selectedcatid )
876 if ( $this->selectedcatid == $row['catid'] )
878 $row['catiscurrent'] = 'yes';
879 $row['currentcat'] = 'yes';
885 if ( (integer) $itemid && $manager->existsItem((integer) $itemid, 0, 0) )
887 $iobj =& $manager->getItem($itemid, 0, 0);
888 $cid = $iobj['catid'];
890 if ( $cid == $row['catid'] )
892 $row['catiscurrent'] = 'yes';
893 $row['currentcat'] = 'yes';
898 $data = array('listitem' => &$row);
899 $manager->notify('PreCategoryListItem', $data);
901 if ( !array_key_exists('CATLIST_LISTITEM', $template) || empty($template['CATLIST_LISTITEM']))
903 echo Template::fill(NULL, $row);
907 echo Template::fill($template['CATLIST_LISTITEM'], $row);
914 'blogid' => $this->blogid,
915 'blogurl' => $blogurl,
916 'self' => $CONF['Self'],
917 'catiscurrent' => $nocatselected, //: Change: Set catiscurrent template variable for footer
918 'currentcat' => $nocatselected
921 if ( !array_key_exists('CATLIST_FOOTER', $template) || empty($template['CATLIST_FOOTER']))
923 echo Template::fill(NULL, $args);
927 echo Template::fill($template['CATLIST_FOOTER'], $args);
934 * Blog::showBlogList()
935 * Shows a list of all blogs in the system using a given template
936 * ordered by number, name, shortname or description
937 * in ascending or descending order
939 * @param string $template tempalte name
940 * @param string $bnametype bname/bshortname
941 * @param string $orderby string for 'ORDER BY' SQL
942 * @param string $direction ASC/DESC
945 public function showBlogList($template, $bnametype, $orderby, $direction)
947 global $CONF, $manager;
958 $orderby='bshortname';
968 $direction=strtolower($direction);
969 switch ( $direction )
982 $template =& $manager->getTemplate($template);
984 if ( array_key_exists('BLOGLIST_HEADER', $template) && !empty($template['BLOGLIST_HEADER']) )
987 'sitename' => $CONF['SiteName'],
988 'siteurl' => $CONF['IndexURL']
991 echo Template::fill($template['BLOGLIST_HEADER'], $vars);
994 if ( array_key_exists('BLOGLIST_LISTITEM', $template) && !empty($template['BLOGLIST_LISTITEM']) )
996 $query = 'SELECT bnumber, bname, bshortname, bdesc, burl FROM '.sql_table('blog').' ORDER BY '.$orderby.' '.$direction;
997 $res = DB::getResult($query);
999 foreach ( $res as $row )
1002 $list['bloglink'] = Link::create_blogid_link($row['bnumber']);
1003 $list['blogdesc'] = $row['bdesc'];
1004 $list['blogurl'] = $row['burl'];
1006 if ( $bnametype == 'shortname' )
1008 $list['blogname'] = $row['bshortname'];
1012 /* all other cases */
1013 $list['blogname'] = $row['bname'];
1016 $data = array('listitem' => &$list);
1017 $manager->notify('PreBlogListItem', $data);
1019 echo Template::fill($template['BLOGLIST_LISTITEM'], $list);
1022 $res->closeCursor();
1026 if ( array_key_exists('BLOGLIST_FOOTER', $template) && !empty($template['BLOGLIST_FOOTER']) )
1029 'sitename' => $CONF['SiteName'],
1030 'siteurl' => $CONF['IndexURL']
1032 echo Template::fill($template['BLOGLIST_FOOTER']);
1038 * Blog::readSettings()
1039 * Read the blog settings
1044 public function readSettings()
1046 $query = 'SELECT * FROM %s WHERE bnumber=%d;';
1047 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);
1048 $res = DB::getResult($query);
1050 $this->isValid = ($res->rowCount() > 0);
1051 if ( $this->isValid )
1053 $this->settings = $res->fetch(PDO::FETCH_ASSOC);
1059 * Blog::writeSettings()
1060 * Write the blog settings
1062 public function writeSettings()
1064 // (can't use floatval since not available prior to PHP 4.2)
1065 $offset = $this->getTimeOffset();
1066 if ( !is_float($offset) )
1068 $offset = (integer) $offset;
1071 $query = 'UPDATE '.sql_table('blog')
1072 . ' SET bname=' . DB::quoteValue($this->getName()) . ','
1073 . ' bshortname='. DB::quoteValue($this->getShortName()) . ','
1074 . ' bcomments='. intval($this->commentsEnabled()) . ','
1075 . ' bmaxcomments=' . intval($this->getMaxComments()) . ','
1076 . ' btimeoffset=' . $offset . ','
1077 . ' bpublic=' . intval($this->isPublic()) . ','
1078 . ' breqemail=' . intval($this->emailRequired()) . ','
1079 . ' bconvertbreaks=' . intval($this->convertBreaks()) . ','
1080 . ' ballowpast=' . intval($this->allowPastPosting()) . ','
1081 . ' bnotify=' . DB::quoteValue($this->getNotifyAddress()) . ','
1082 . ' bnotifytype=' . intval($this->getNotifyType()) . ','
1083 . ' burl=' . DB::quoteValue($this->getURL()) . ','
1084 . ' bupdate=' . DB::quoteValue($this->getUpdateFile()) . ','
1085 . ' bdesc=' . DB::quoteValue($this->getDescription()) . ','
1086 . ' bdefcat=' . intval($this->getDefaultCategory()) . ','
1087 . ' bdefskin=' . intval($this->getDefaultSkin()) . ','
1088 . ' bincludesearch=' . intval($this->getSearchable())
1089 . ' WHERE bnumber=' . intval($this->blogid);
1090 DB::execute($query);
1095 * Blog::updateUpdatefile()
1096 * Update the update file if requested
1101 public function updateUpdatefile()
1103 if ( $this->getUpdateFile() )
1105 $f_update = fopen($this->getUpdateFile(), 'w');
1106 fputs($f_update,$this->getCorrectTime());
1113 * Blog::isValidCategory()
1114 * Check if a category with a given catid is valid
1116 * @param integer $catid ID for category
1117 * @return boolean exists or not
1119 public function isValidCategory($catid)
1121 $query = 'SELECT * FROM %s WHERE cblog=%d and catid=%d;';
1122 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);
1123 $res = DB::getResult($query);
1124 return ($res->rowCount() != 0);
1128 * Blog::getCategoryName()
1129 * Get the category name for a given catid
1131 * @param integer $catid ID for category
1132 * @return string name of category
1134 public function getCategoryName($catid)
1136 $query = 'SELECT cname FROM %s WHERE cblog=%d and catid=%d;';
1137 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);
1138 $res = DB::getValue($query);
1143 * Blog::getCategoryDesc()
1144 * Get the category description for a given catid
1149 public function getCategoryDesc($catid)
1151 $query = 'SELECT cdesc FROM %s WHERE cblog=%d and catid=%d;';
1152 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);
1153 $res = DB::getValue($query);
1158 * Blog::getCategoryIdFromName
1159 * Get the category id for a given category name
1161 * @param string $name category name
1162 * @return ID for category
1164 public function getCategoryIdFromName($name)
1166 $query = 'SELECT catid FROM %s WHERE cblog=%d and cname=%s;';
1167 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, DB::quoteValue($name));
1169 $res = DB::getValue();
1172 return $this->getDefaultCategory();
1178 * Blog::insertJavaScriptInfo()
1179 * Insert a javascript that includes information about the settings
1180 * of an author: ConvertBreaks, MediaUrl and AuthorId
1182 * @param $authorid id of the author
1184 public function insertJavaScriptInfo($authorid = '')
1186 global $member, $CONF;
1188 if ( $authorid == '' )
1190 $authorid = $member->getID();
1193 echo "<script type=\"text/javascript\">\n";
1195 if ( !$this->convertBreaks() )
1197 echo "setConvertBreaks(false);\n";
1201 echo "setConvertBreaks(true);\n";
1203 echo "setMediaUrl('{$CONF['MediaURL']}');\n";
1204 echo "setAuthorId('{$authorid}');\n";
1210 * Blog::setAllowPastPosting()
1211 * Set the the setting for allowing to publish postings in the past
1213 * @param boolean $val new value for ballowpast
1216 public function setAllowPastPosting($val)
1218 $this->setSetting('ballowpast', $val);
1223 * Blog::allowPastPosting()
1224 * Get the the setting if it is allowed to publish postings in the past
1225 * [should be named as getAllowPastPosting()]
1230 public function allowPastPosting()
1232 return $this->getSetting('ballowpast');
1236 * Blog::getCorrectTime()
1241 public function getCorrectTime($t=0)
1247 return ($t + 3600 * $this->getTimeOffset());
1254 * @return string name of this weblog
1256 public function getName()
1258 return $this->getSetting('bname');
1262 * Blog::getShortName()
1265 * @return string short name of this weblog
1267 public function getShortName()
1269 return $this->getSetting('bshortname');
1273 * Blog::getMaxComments()
1276 * @return integer maximum number of comments
1278 public function getMaxComments()
1280 return $this->getSetting('bmaxcomments');
1284 * Blog::getNotifyAddress()
1287 * @return string mail address for notifying
1289 public function getNotifyAddress()
1291 return $this->getSetting('bnotify');
1295 * Blog::getNotifyType()
1298 * @return integer notifycation type
1300 public function getNotifyType()
1302 return $this->getSetting('bnotifytype');
1306 * Blog::notifyOnComment()
1311 public function notifyOnComment()
1313 $n = $this->getNotifyType();
1314 return (($n != 0) && (($n % 3) == 0));
1318 * Blog::notifyOnVote()
1323 public function notifyOnVote()
1325 $n = $this->getNotifyType();
1326 return (($n != 0) && (($n % 5) == 0));
1330 * Blog::notifyOnNewItem()
1335 public function notifyOnNewItem()
1337 $n = $this->getNotifyType();
1338 return (($n != 0) && (($n % 7) == 0));
1342 * Blog::setNotifyType()
1344 * @param integer $val
1347 public function setNotifyType($val)
1349 $this->setSetting('bnotifytype',$val);
1354 * Blog::getTimeOffset()
1358 public function getTimeOffset()
1360 return $this->getSetting('btimeoffset');
1364 * Blog::commentsEnabled()
1366 * @return integer enabled or not
1368 public function commentsEnabled()
1370 return $this->getSetting('bcomments');
1376 * @return string URI for this weblog
1378 public function getURL()
1380 return $this->getSetting('burl');
1384 * Blog::getDefaultSkin()
1386 * @return name of skin as default for this weblog
1388 public function getDefaultSkin()
1390 return $this->getSetting('bdefskin');
1394 * Blog::getUpdateFile()
1396 * @return string name of file to be updated when weblog is updated
1398 public function getUpdateFile()
1400 return $this->getSetting('bupdate');
1404 * Blog::getDescription()
1406 * @return string description for this weblog
1408 public function getDescription()
1410 return $this->getSetting('bdesc');
1416 * @return integer publlic or not
1418 public function isPublic()
1420 return $this->getSetting('bpublic');
1424 * Blog::emailRequired()
1426 * @return integer email is required when posting comment or not
1428 public function emailRequired()
1430 return $this->getSetting('breqemail');
1434 * Blog::getSearchable()
1436 * @return integer searchable or not
1438 public function getSearchable()
1440 return $this->getSetting('bincludesearch');
1444 * Blog::getDefaultCategory()
1446 * @return ID for category as a default
1448 public function getDefaultCategory()
1450 return $this->getSetting('bdefcat');
1455 * @param integer $val allow comments by non-registered members or not
1458 public function setPublic($val)
1460 $this->setSetting('bpublic', $val);
1465 * Blog::setSearchable()
1466 * @param integer $val searchable from the other blogs or not
1469 public function setSearchable($val)
1471 $this->setSetting('bincludesearch', $val);
1476 * Blog::setDescription
1477 * @param string $val description for this weblog
1480 public function setDescription($val)
1482 $this->setSetting('bdesc',$val);
1487 * Blog::setUpdateFile()
1488 * @param string $val name of file to beupdated when weblog is updated
1491 public function setUpdateFile($val)
1493 $this->setSetting('bupdate',$val);
1498 * Blog::setDefaultSkin()
1499 * @param integer $val ID for default skin to use when displaying this weblog
1502 public function setDefaultSkin($val)
1504 $this->setSetting('bdefskin', $val);
1510 * @param string $val URI for this weblog
1513 public function setURL($val)
1515 $this->setSetting('burl', $val);
1521 * @param string $val name of this weblog
1524 public function setName($val)
1526 $this->setSetting('bname', $val);
1531 * Blog::setShortName()
1532 * @param string $val short name for this weblog
1535 public function setShortName($val)
1537 $this->setSetting('bshortname', $val);
1542 * Blog::setCommentsEnabled()
1543 * @param integer $val enabling posting comment or not
1546 public function setCommentsEnabled($val)
1548 $this->setSetting('bcomments',$val);
1553 * Blog::setMaxComments()
1554 * @param integer $val maximum number of comments for this weblog
1557 public function setMaxComments($val)
1559 $this->setSetting('bmaxcomments', $val);
1564 * Blog::setNotifyAddress()
1565 * @param string $val email to be notified if weblog updated
1568 public function setNotifyAddress($val)
1570 $this->setSetting('bnotify', $val);
1575 * Blog::setEmailRequired()
1576 * @param string requiring comments with email or not from non member
1579 public function setEmailRequired($val)
1581 $this->setSetting('breqemail', $val);
1586 * Blog::setTimeOffset()
1587 * @param integer $val time offset
1590 public function setTimeOffset($val)
1592 // check validity of value
1593 // 1. replace , by . (common mistake)
1594 $val = str_replace(',','.',$val);
1596 // 2. cast to float or int
1597 if ( is_numeric($val) && (i18n::strpos($val, '.5') === (i18n::strlen($val) - 2)) )
1599 $val = (float) $val;
1603 $val = (integer) $val;
1606 $this->setSetting('btimeoffset',$val);
1611 * Blog::setDefaultCategory()
1612 * @param integer $val ID for default category for this weblog
1615 public function setDefaultCategory($val)
1617 $this->setSetting('bdefcat',$val);
1622 * Blog::getSetting()
1623 * @param string $key key for setting of this weblog
1624 * @return mixed value for the setting
1626 public function getSetting($key)
1628 return $this->settings[$key];
1632 * Blog::setSetting()
1633 * @param string $key key for setting of this weblog
1634 * @param mixed $value value for the key
1637 public function setSetting($key, $value)
1639 $this->settings[$key] = $value;
1644 * Blog::addTeamMember()
1645 * Tries to add a member to the team.
1646 * Returns false if the member was already on the team
1648 * @param integer $memberid id for member
1649 * @param boolean $admin super-admin or not
1650 * @return boolean Success/Fail
1652 public function addTeamMember($memberid, $admin)
1656 $memberid = intval($memberid);
1657 $admin = intval($admin);
1659 // check if member is already a member
1660 $tmem =& $manager->getMember($memberid);
1662 if ( $tmem->isTeamMember($this->blogid) )
1672 $manager->notify('PreAddTeamMember', $data);
1675 $query = "INSERT INTO %s (TMEMBER, TBLOG, TADMIN) VALUES (%d, %d, %d);";
1676 $query = sprintf($query, sql_table('team'), (integer) $memberid, (integer) $this->blogid, (integer) $admin);
1677 DB::execute($query);
1684 $manager->notify('PostAddTeamMember', $data);
1686 $logMsg = sprintf(_TEAM_ADD_NEWTEAMMEMBER, $tmem->getDisplayName(), $memberid, $this->getName());
1687 ActionLog::add(INFO, $logMsg);
1695 * @return integer ID for this weblog
1697 public function getID()
1699 return (integer) $this->blogid;
1703 * Checks if a blog with a given shortname exists
1704 * Returns true if there is a blog with the given shortname (static)
1706 * @param string $name blog shortname
1707 * @return boolean exists or not
1709 public function exists($name)
1711 $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bshortname='. DB::quoteValue($name));
1712 return ($r->rowCount() != 0);
1716 * Checks if a blog with a given id exists
1717 * Returns true if there is a blog with the given ID (static)
1719 * @param integer $id ID for searched weblog
1720 * @return boolean exists or not
1722 public function existsID($id)
1724 $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));
1725 return ($r->rowCount() != 0);
1729 * Blog::setFuturePost()
1730 * flag there is a future post pending
1735 public function setFuturePost()
1737 $query = "UPDATE %s SET bfuturepost='1' WHERE bnumber=%d;";
1738 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);
1739 DB::execute($query);
1744 * Blog::clearFuturePost()
1745 * clear there is a future post pending
1750 public function clearFuturePost()
1752 $query = "UPDATE %s SET bfuturepost='0' WHERE bnumber=%d;";
1753 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);
1754 DB::execute($query);
1759 * Blog::checkJustPosted()
1760 * check if we should throw justPosted event
1765 public function checkJustPosted()
1769 if ( $this->settings['bfuturepost'] == 1 )
1771 $query = "SELECT * FROM %s WHERE iposted=0 AND iblog=%d AND itime < NOW();";
1772 $query = sprintf($query, sql_table('item'), (integer) $this->blogid);
1774 $result = DB::getResult($query);
1775 if ( $result->rowCount() > 0 )
1777 // This $pinged is allow a plugin to tell other hook to the event that a ping is sent already
1778 // Note that the plugins's calling order is subject to thri order in the plugin list
1780 $data = array('blogid' => $this->blogid, 'pinged' => &$pinged);
1781 $manager->notify('JustPosted', $data);
1783 // clear all expired future posts
1784 $query = "UPDATE %s SET iposted='1' WHERE iblog=%d AND itime < NOW();";
1785 $query = spriintf($query, sql_table('item'), (integer) $this->blogid);
1786 DB::execute($query);
1788 // check to see any pending future post, clear the flag is none
1789 $query = "SELECT * FROM %s WHERE iposted=0 AND iblog=%d;";
1790 $query = sprintf($query, sql_table('item'), (integer) $this->blogid);
1792 $result = DB::getResult($query);
1793 if ( $result->rowCount() == 0 )
1795 $this->clearFuturePost();
1803 * Blog::readLogFromList()
1804 * Shows the given list of items for this blog
1806 * @param array $itemarray array of item numbers to be displayed
1807 * @param string $template string representing the template _NAME_ (!)
1808 * @param string $highlight contains a query that should be highlighted
1809 * @param boolean $comments 1=show comments 0=don't show comments
1810 * @param boolean $dateheads 1=show dateheads 0=don't show dateheads
1811 * @param boolean $showDrafts 0=do not show drafts 1=show drafts
1812 * @param boolean $showFuture 0=do not show future posts 1=show future posts
1813 * @return integer amount of items shown
1815 public function readLogFromList($itemarray, $template, $highlight = '', $comments = 1, $dateheads = 1,$showDrafts = 0, $showFuture = 0)
1817 $query = $this->getSqlItemList($itemarray,$showDrafts,$showFuture);
1818 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
1822 * Blog::getSqlItemList()
1823 * Returns the SQL query used to fill out templates for a list of items
1824 * No LIMIT clause is added. (caller should add this if multiple pages are requested)
1826 * @param array $itemarray an array holding the item numbers of the items to be displayed
1827 * @param integer $showDrafts 0=do not show drafts 1=show drafts
1828 * @param integer $showFuture 0=do not show future posts 1=show future posts
1829 * @return string either a full SQL query, or an empty string
1831 public function getSqlItemList($itemarray,$showDrafts = 0,$showFuture = 0)
1833 if ( !is_array($itemarray) )
1838 $showDrafts = intval($showDrafts);
1839 $showFuture = intval($showFuture);
1842 foreach ( $itemarray as $value )
1844 if ( intval($value) )
1846 $items[] = intval($value);
1849 if ( !count($items) )
1856 foreach ( $items as $value )
1860 . ' i.inumber as itemid,'
1861 . ' i.ititle as title,'
1862 . ' i.ibody as body,'
1863 . ' m.mname as author,'
1864 . ' m.mrealname as authorname,'
1866 . ' i.imore as more,'
1867 . ' m.mnumber as authorid,'
1868 . ' m.memail as authormail,'
1869 . ' m.murl as authorurl,'
1870 . ' c.cname as category,'
1871 . ' i.icat as catid,'
1872 . ' i.iclosed as closed';
1875 . sql_table('item') . ' as i, '
1876 . sql_table('member') . ' as m, '
1877 . sql_table('category') . ' as c'
1879 . ' i.iblog='.$this->blogid
1880 . ' and i.iauthor=m.mnumber'
1881 . ' and i.icat=c.catid';
1886 $query .= ' and i.idraft=0';
1890 // don't show future items
1891 $query .= ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime());
1894 $query .= ' and i.inumber='.intval($value);
1897 if ($i) $query .= ' UNION ';
1904 * Blog::convertBreaks()
1905 * Get the the setting for the line break handling
1906 * [should be named as getConvertBreaks()]
1912 public function convertBreaks()
1914 return $this->getSetting('bconvertbreaks');
1918 * Set the the setting for the line break handling
1921 * @param boolean $val new value for bconvertbreaks
1924 public function setConvertBreaks($val)
1926 $this->setSetting('bconvertbreaks', $val);