OSDN Git Service

FIX:$manager->notify()の第二引数に変数を渡すように修正。
[nucleus-jp/nucleus-next.git] / nucleus / libs / BLOG.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2012 The Nucleus Group
6  *
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)
12  */
13 /**
14  * A class representing a blog and containing functions to get that blog shown
15  * on the screen
16  *
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 $
20  */
21
22 if ( !function_exists('requestVar') ) exit;
23 require_once dirname(__FILE__) . '/ITEMACTIONS.php';
24
25 class Blog
26 {
27         // blog id
28         public $blogid;
29
30         // After creating an object of the blog class, contains true if the BLOG object is
31         // valid (the blog exists)
32         public $isValid;
33
34         // associative array, containing all blogsettings (use the get/set functions instead)
35         private $settings;
36         
37         // ID of currently selected category
38         private $selectedcatid;
39
40         /**
41          * Blog::_\construct()
42          * Creates a new BLOG object for the given blog
43          *
44          * @param       integer $id     blogid
45          * @return      void
46          */
47         public function __construct($id)
48         {
49                 global $catid;
50                 
51                 $this->blogid = (integer) $id;
52                 $this->readSettings();
53                 $this->setSelectedCategory($catid);
54                 return;
55         }
56         
57         /**
58          * Blog::readLog()
59          * Shows the given amount of items for this blog
60          *
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
65          */
66         public function readLog($template, $amountEntries, $offset = 0, $startpos = 0)
67         {
68                 return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);
69         }
70         
71         /**
72          * Blog::showArchive()
73          * Shows an archive for a given month
74          *
75          * @param       integer $year           year
76          * @param       integer $month          month
77          * @param       string  $template       String representing the template name to be used
78          * @return      void
79          */
80         public function showArchive($templatename, $year, $month=0, $day=0)
81         {
82                 // create extra where clause for select query
83                 if ( $day == 0 && $month != 0 )
84                 {
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);
88                 }
89                 elseif ( $month == 0 )
90                 {
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);
94                 }
95                 else
96                 {
97                         $timestamp_start = mktime(0,0,0,$month,$day,$year);
98                         $timestamp_end = mktime(0,0,0,$month,$day+1,$year);
99                 }
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));
102                 
103                 $this->readLogAmount($templatename,0,$extra_query,'',1,1);
104                 return;
105         }
106         
107         /**
108          * Blog::setSelectedCategory()
109          * Sets the selected category by id (only when category exists)
110          * 
111          * @param       integer $catid  ID for category
112          * @return      void
113          */
114         public function setSelectedCategory($catid)
115         {
116                 if ( $this->isValidCategory($catid) || (intval($catid) == 0) )
117                 {
118                         $this->selectedcatid = intval($catid);
119                 }
120                 return;
121         }
122         
123         /**
124          * Blog::setSelectedCategoryByName()
125          * Sets the selected category by name
126          * 
127          * @param       string  $catname        name of category
128          * @return      void
129          */
130         public function setSelectedCategoryByName($catname)
131         {
132                 $this->setSelectedCategory($this->getCategoryIdFromName($catname));
133                 return;
134         }
135         
136         /**
137          * Blog::getSelectedCategory()
138          * Returns the selected category
139          * 
140          * @param       void
141          * @return      integer
142          */
143         public function getSelectedCategory()
144         {
145                 return $this->selectedcatid;
146         }
147         
148         /**
149          * Shows the given amount of items for this blog
150          *
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
159          */
160         private function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0)
161         {
162                 $query = $this->getSqlBlog($extraQuery);
163                 
164                 if ( $amountEntries > 0 )
165                 {
166                         // $offset zou moeten worden:
167                         // (($startpos / $amountentries) + 1) * $offset ... later testen ...
168                         $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);
169                 }
170                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
171         }
172         
173         /**
174          * Blog::showUsingQuery()
175          * Do the job for readLogAmmount
176          * 
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
183          */
184         private function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1)
185         {
186                 global $CONF, $manager, $currentTemplateName;
187                 global $StartTime;
188                 
189                 $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');
190                 if ( $lastVisit != 0 )
191                 {
192                         $lastVisit = $this->getCorrectTime($lastVisit);
193                 }
194                 
195                 // set templatename as global variable (so plugins can access it)
196                 $currentTemplateName = $templateName;
197                 $template =& $manager->getTemplate($templateName);
198                 
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);
205                 
206                 $parser = new Parser($handler);
207                 
208                 // execute query
209                 $items = DB::getResult($query);
210                 
211                 // loop over all items
212                 $old_date = 0;
213                 foreach ( $items as $item )
214                 {
215                         // string timestamp -> unix timestamp
216                         $item['timestamp'] = strtotime($item['itime']);
217                         
218                         // action handler needs to know the item we're handling
219                         $handler->setCurrentItem($item);
220                         
221                         // add date header if needed
222                         if ( $dateheads )
223                         {
224                                 $new_date = date('dFY', $item['timestamp']);
225                                 if ( $new_date != $old_date )
226                                 {
227                                         // unless this is the first time, write date footer
228                                         $timestamp = $item['timestamp'];
229                                         if ( $old_date != 0 )
230                                         {
231                                                 $oldTS = strtotime($old_date);
232                                                 $data = array('blog' => $this, 'timestamp' => $oldTS);
233                                                 $manager->notify('PreDateFoot', $data);
234                                                 
235                                                 if ( !in_array('DATE_FOOTER', $template) || empty($template['DATE_FOOTER']) )
236                                                 {
237                                                         $tmp_footer = '';
238                                                 }
239                                                 else
240                                                 {
241                                                         $tmp_footer = i18n::formatted_datetime($template['DATE_FOOTER'], $oldTS);
242                                                 }
243                                                 $parser->parse($tmp_footer);
244                                                 $manager->notify('PostDateFoot', $data);
245                                         }
246                                         
247                                         $data = array('blog' => $this, 'timestamp' => $timestamp);
248                                         $manager->notify('PreDateHead', $data);
249                                         
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']) )
253                                         {
254                                                 $tmp_header = '';
255                                         }
256                                         else
257                                         {
258                                                 $tmp_header = i18n::formatted_datetime($template['DATE_HEADER'], $timestamp);
259                                         }
260                                         $parser->parse($tmp_header);
261                                         $manager->notify('PostDateHead', $data);
262                                 }
263                                 $old_date = $new_date;
264                         }
265                         
266                         // parse item
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']);
273                 }
274                 
275                 $numrows = $items->rowCount();
276                 
277                 // add another date footer if there was at least one item
278                 if ( ($numrows > 0) && $dateheads )
279                 {
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);
284                 }
285                 
286                 $items->closeCursor();
287                 return $numrows;
288         }
289         
290         /**
291          * Blog::showOneitem()
292          * Simplified function for showing only one item
293          * 
294          * @param       integer $itemid         ID for item
295          * @param       array   $template       template for item
296          * @param       string  $highlight      string for highlight
297          * @return      integer 1
298          */
299         public function showOneitem($itemid, $template, $highlight)
300         {
301                 $extraQuery = ' and inumber=' . intval($itemid);
302                 
303                 return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);
304         }
305         
306         /**
307          * Blog::addItem()
308          * Adds an item to this blog
309          * 
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
321          */
322         function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft, $posted='1')
323         {
324                 global $manager;
325                 
326                 $blogid         = (integer) $blogid;
327                 $authorid       = (integer) $authorid;
328                 $title          = $title;
329                 $body           = $body;
330                 $more           = $more;
331                 $catid          = intval($catid);
332                 
333                 // convert newlines to <br />
334                 if ( $this->convertBreaks() )
335                 {
336                         $body = addBreaks($body);
337                         $more = addBreaks($more);
338                 }
339
340                 if ( $closed != '1' )
341                 {
342                         $closed = '0';
343                 }
344                 if ( $draft != '0' )
345                 {
346                         $draft = '1';
347                 }
348                 
349                 if ( !$this->isValidCategory($catid) )
350                 {
351                         $catid = $this->getDefaultCategory();
352                 }
353                 
354                 $isFuture = 0;
355                 if ( $timestamp > $this->getCorrectTime() )
356                 {
357                         $isFuture = 1;
358                 }
359                 
360                 $timestamp = date('Y-m-d H:i:s',$timestamp);
361
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);
364                 
365                 $ititle = DB::quoteValue($title);
366                 $ibody = DB::quoteValue($body);
367                 $imore = DB::quoteValue($more);
368                 $timestamp = DB::formatDateTime(strtotime($timestamp));
369                 
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);
372                 DB::execute($query);
373                 $itemid = DB::getInsertId();
374
375                 $data = array('itemid' => $itemid);
376                 $manager->notify('PostAddItem', $data);
377                 
378                 if ( !$draft )
379                 {
380                         $this->updateUpdateFile();
381                 }
382                 // send notification mail
383                 if ( !$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem() )
384                 {
385                         $this->sendNewItemNotification($itemid, $title, $body);
386                 }
387                 return $itemid;
388         }
389         
390         /**
391          * Blog::sendNewItemNotification()
392          * Send a new item notification to the notification list
393          * 
394          * @param       string  $itemid ID of the item
395          * @param       string  $title  title of the item
396          * @param       string  $body   body of the item
397          * @return      void
398          */
399         public function sendNewItemNotification($itemid, $title, $body)
400         {
401                 global $CONF, $member;
402                 
403                 $ascii = Entity::anchor_footnoting($body);
404                 
405                 $message = _NOTIFY_NI_MSG . " \n";
406                 $temp = parse_url($CONF['Self']);
407                 if ( $temp['scheme'] )
408                 {
409                         $message .= Link::create_item_link($itemid) . "\n\n";
410                 }
411                 else
412                 {
413                         $tempurl = $this->getURL();
414                         if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )
415                         {
416                                 $message .= $tempurl . '?itemid=' . $itemid . "\n\n";
417                         }
418                         else
419                         {
420                                 $message .= $tempurl . '/?itemid=' . $itemid . "\n\n";
421                         }
422                 }
423                 $message .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";
424                 $message .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";
425                 $message .= NOTIFICATION::get_mail_footer();
426                 
427                 $subject = $this->getName() . ': ' . _NOTIFY_NI_TITLE;
428                 
429                 $from = $member->getNotifyFromMailAddress();
430                 
431                 NOTIFICATION::mail($this->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());
432                 return;
433         }
434         
435         /**
436          * Blog::createNewCategory()
437          * Creates a new category for this blog
438          *
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
442          */
443         public function createNewCategory($catName = '', $catDescription = _CREATED_NEW_CATEGORY_DESC)
444         {
445                 global $member, $manager;
446                 
447                 if ( !$member->blogAdminRights($this->blogid) )
448                 {
449                         return 0;
450                 }
451                 
452                 // generate
453                 if ( $catName == '' )
454                 {
455                         $catName = _CREATED_NEW_CATEGORY_NAME;
456                         $i = 1;
457                         
458                         $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);
459                         while ( $res->rowCount() > 0 )
460                         {
461                                 $i++;
462                                 $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);
463                         }
464                         
465                         $catName = $catName . $i;
466                 }
467                 
468                 $data = array(
469                         'blog'                  => &$this,
470                         'name'                  => &$catName,
471                         'description'   => $catDescription
472                 );
473                 $manager->notify('PreAddCategory', $data);
474                 
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));
477                 DB::execute($query);
478                 $catid = DB::getInsertId();
479                 
480                 $data = array(
481                         'blog'                  => &$this,
482                         'name'                  => $catName,
483                         'description'   => $catDescription,
484                         'catid'                 => $catid
485                 );
486                 $manager->notify('PostAddCategory', $data);
487                 
488                 return $catid;
489         }
490         
491         /**
492          * Blog::search()
493          * Searches all months of this blog for the given query
494          *
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
501          */
502         public function search($query, $template, $amountMonths, $maxresults, $startpos) {
503                 global $CONF, $manager;
504                 
505                 $highlight      = '';
506                 $sqlquery       = $this->getSqlSearch($query, $amountMonths, $highlight);
507                 
508                 if ( $sqlquery == '' )
509                 {
510                         // no query -> show everything
511                         $extraquery = '';
512                         $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);
513                 }
514                 else
515                 {
516                         // add LIMIT to query (to split search results into pages)
517                         if ( intval($maxresults > 0) )
518                         {
519                                 $sqlquery .= ' LIMIT ' . intval($startpos) . ',' . intval($maxresults);
520                         }
521                         
522                         // show results
523                         $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);
524                         
525                         // when no results were found, show a message
526                         if ( $amountfound == 0 )
527                         {
528                                 $template =& $manager->getTemplate($template);
529                                 $vars = array(
530                                         'query'         => Entity::hsc($query),
531                                         'blogid'        => $this->blogid
532                                 );
533                                 echo Template::fill($template['SEARCH_NOTHINGFOUND'], $vars);
534                         }
535                 }
536                 return $amountfound;
537         }
538         
539         /**
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)
543          *
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)
549          */
550         public function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')
551         {
552                 $searchclass = new Search($query);
553                 
554                 $highlight       = $searchclass->inclusive;
555                 
556                 // if querystring is empty, return empty string
557                 if ( $searchclass->inclusive == '' )
558                 {
559                         return '';
560                 }
561                 
562                 $where  = $searchclass->boolean_sql_where('ititle,ibody,imore');
563                 $select = $searchclass->boolean_sql_select('ititle,ibody,imore');
564                 
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
569                 $selectblogs = '';
570                 if ( count($blogs) > 0 )
571                 {
572                         $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';
573                 }
574                 
575                 if ( $mode == '' )
576                 {
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';
580                         
581                         if ( $select )
582                         {
583                                 $query .= ', '.$select. ' as score ';
584                         }
585                 }
586                 else
587                 {
588                         $query = 'SELECT COUNT(*) as result ';
589                 }
590                 
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'
594                                 // exclude drafts
595                                 . ' and i.idraft=0'
596                                 . $selectblogs
597                                         // don't show future items
598                                 . ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime())
599                                 . ' and '.$where;
600                 
601                 // take into account amount of months to search
602                 if ( $amountMonths > 0 )
603                 {
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);
607                 }
608                 
609                 if ( $mode == '' )
610                 {
611                         if ( $select )
612                         {
613                                 $query .= ' ORDER BY score DESC';
614                         }
615                         else
616                         {
617                                 $query .= ' ORDER BY i.itime DESC ';
618                         }
619                 }
620                 
621                 return $query;
622         }
623         
624         /**
625          * Blog::getSqlBlog()
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)
628          *
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
632          */
633         public function getSqlBlog($extraQuery, $mode = '')
634         {
635                 if ( $mode == '' )
636                 {
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';
640                 }
641                 else
642                 {
643                         $query = 'SELECT COUNT(*) as result ';
644                 }
645                 
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
652                 
653                 if ( $this->selectedcatid )
654                 {
655                         $query .= ' and i.icat=' . $this->selectedcatid . ' ';
656                 }
657                 
658                 $query .= $extraQuery;
659                 
660                 if ( $mode == '' )
661                 {
662                         $query .= ' ORDER BY i.itime DESC';
663                 }
664                 return $query;
665         }
666         
667         /**
668          * Blog::showArchiveList()
669          * Shows the archivelist using the given template
670          * 
671          * @param       string  $template       template name
672          * @param       string  $mode   year/month/day
673          * @param       integer $limit  limit of record count
674          * @return      void
675          */
676         public function showArchiveList($template, $mode = 'month', $limit = 0)
677         {
678                 global $CONF, $catid, $manager;
679                 
680                 if ( !isset ($linkparams) )
681                 {
682                         $linkparams = array();
683                 }
684                 
685                 if ( $catid )
686                 {
687                         $linkparams = array('catid' => $catid);
688                 }
689                 
690                 $template =& $manager->getTemplate($template);
691                 $listitem['blogid'] = $this->blogid;
692                 
693                 if ( !array_key_exists('ARCHIVELIST_HEADER', $template) || !$template['ARCHIVELIST_HEADER'] )
694                 {
695                         $tplt = '';
696                 }
697                 else
698                 {
699                         $tplt = $template['ARCHIVELIST_HEADER'];
700                 }
701                 
702                 echo Template::fill($tplt, $listitem);
703                 
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
709                 
710                 if ( $catid )
711                 {
712                         $query .= ' and icat=' . intval($catid);
713                 }
714                 
715                 $query .= ' GROUP BY Year';
716                 if ( $mode == 'month' || $mode == 'day' )
717                 {
718                         $query .= ', Month';
719                 }
720                 if ( $mode == 'day' )
721                 {
722                         $query .= ', Day';
723                 }
724                 
725                 $query .= ' ORDER BY itime DESC';
726                 
727                 if ( $limit > 0 )
728                 {
729                         $query .= ' LIMIT ' . intval($limit);
730                 }
731                 
732                 $res = DB::getResult($query);
733                 foreach ( $res as $current )
734                 {
735                         /* string time -> unix timestamp */
736                         $current['itime'] = strtotime($current['itime']);
737                         
738                         if ( $mode == 'day' )
739                         {
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'];
745                         }
746                         elseif ( $mode == 'year' )
747                         {
748                                 $archivedate = date('Y',$current['itime']);
749                                 $listitem['day'] = '';
750                                 $listitem['month'] = '';
751                                 $archive['day'] = '';
752                                 $archive['month'] = '';
753                         }
754                         else
755                         {
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'] = '';
761                         }
762                         
763                         $listitem['year'] = date('Y',$current['itime']);
764                         $archive['year'] = $listitem['year'];
765                         $listitem['archivelink'] = Link::create_archive_link($this->blogid,$archivedate,$linkparams);
766
767                         $data = array('listitem' => &$listitem);
768                         $manager->notify('PreArchiveListItem', $data);
769                         
770                         $temp = Template::fill($template['ARCHIVELIST_LISTITEM'],$listitem);
771                         echo i18n::formatted_datetime($temp, $current['itime']);
772                         return;
773                 }
774                 
775                 $res->closeCursor();
776                 
777                 if ( !array_key_exists('ARCHIVELIST_FOOTER', $template) || !$template['ARCHIVELIST_FOOTER'] )
778                 {
779                         $tplt = '';
780                 }
781                 else
782                 {
783                         $tplt = $template['ARCHIVELIST_FOOTER'];
784                 }
785                 
786                 echo Template::fill($tplt, $listitem);
787                 return;
788         }
789         
790         /**
791          * Blog::showCategoryList()
792          * Shows the list of categories using a given template
793          * 
794          * @param       string  $template       Template Name
795          * @return      void
796          */
797         public function showCategoryList($template)
798         {
799                 global $CONF, $archive, $archivelist, $manager;
800                 
801                 /*
802                  * determine arguments next to catids
803                  * I guess this can be done in a better way, but it works
804                  */
805                 $linkparams = array();
806                 if ( $archive )
807                 {
808                         $blogurl = Link::create_archive_link($this->blogid, $archive, '');
809                         $linkparams['blogid'] = $this->blogid;
810                         $linkparams['archive'] = $archive;
811                 }
812                 else if ( $archivelist )
813                 {
814                         $blogurl = Link::create_archivelist_link($this->blogid, '');
815                         $linkparams['archivelist'] = $archivelist;
816                 }
817                 else
818                 {
819                         $blogurl = Link::create_blogid_link($this->blogid, '');
820                         $linkparams['blogid'] = $this->blogid;
821                 }
822                 
823                 $template =& $manager->getTemplate($template);
824                 
825                 //: Change: Set nocatselected variable
826                 if ( $this->selectedcatid )
827                 {
828                         $nocatselected = 'no';
829                 }
830                 else
831                 {
832                         $nocatselected = 'yes';
833                 } 
834                 
835                 $args = array(
836                         'blogid'        => $this->blogid,
837                         'blogurl'       => $blogurl,
838                         'self'          => $CONF['Self'],
839                         'catiscurrent'  => $nocatselected, // Change: Set catiscurrent template variable for header
840                         'currentcat'    => $nocatselected 
841                 );
842                 
843                 /* output header of category list item */
844                 if ( !array_key_exists('CATLIST_HEADER', $template) || empty($template['CATLIST_HEADER']) )
845                 {
846                         echo Template::fill(NULL, $args);
847                 }
848                 else
849                 {
850                         echo Template::fill($template['CATLIST_HEADER'], $args);
851                 }
852                 
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);
856                 
857                 foreach ( $res as $row )
858                 {
859                         $args = array(
860                                 'catid' => $row['catid'],
861                                 'name'  => $row['catname'],
862                                 'extra' => $linkparams
863                         );
864                         
865                         $row['blogid']          = $this->blogid;
866                         $row['blogurl']         = $blogurl;
867                         $row['catlink']         = Link::create_link('category', $args);
868                         $row['self']            = $CONF['Self'];
869                         
870                         // this gives catiscurrent = no when no category is selected.
871                         $row['catiscurrent'] = 'no';
872                         $row['currentcat'] = 'no';
873                         
874                         if ( $this->selectedcatid )
875                         {
876                                 if ( $this->selectedcatid == $row['catid'] )
877                                 {
878                                         $row['catiscurrent']    = 'yes';
879                                         $row['currentcat']              = 'yes';
880                                 }
881                         }
882                         else
883                         {
884                                 global $itemid;
885                                 if ( (integer) $itemid && $manager->existsItem((integer) $itemid, 0, 0) )
886                                 {
887                                         $iobj   =& $manager->getItem($itemid, 0, 0);
888                                         $cid    = $iobj['catid'];
889                                         
890                                         if ( $cid == $row['catid'] )
891                                         {
892                                                 $row['catiscurrent']    = 'yes';
893                                                 $row['currentcat']              = 'yes';
894                                         }
895                                 }
896                         }
897
898                         $data = array('listitem' => &$row);
899                         $manager->notify('PreCategoryListItem', $data);
900                         
901                         if ( !array_key_exists('CATLIST_LISTITEM', $template) || empty($template['CATLIST_LISTITEM']))
902                         {
903                                 echo Template::fill(NULL, $row);
904                         }
905                         else
906                         {
907                                 echo Template::fill($template['CATLIST_LISTITEM'], $row);
908                         }
909                 }
910                 
911                 $res->closeCursor();
912                 
913                 $args = array(
914                         'blogid'                => $this->blogid,
915                         'blogurl'               => $blogurl,
916                         'self'                  => $CONF['Self'],
917                         'catiscurrent'  => $nocatselected, //: Change: Set catiscurrent template variable for footer
918                         'currentcat'    => $nocatselected
919                 );
920                 
921                 if ( !array_key_exists('CATLIST_FOOTER', $template) || empty($template['CATLIST_FOOTER']))
922                 {
923                         echo Template::fill(NULL, $args);
924                 }
925                 else
926                 {
927                         echo Template::fill($template['CATLIST_FOOTER'], $args);
928                 }
929                 
930                 return;
931         }
932         
933         /**
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
938          * 
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
943          * @return      void
944          */
945         public function showBlogList($template, $bnametype, $orderby, $direction)
946         {
947                 global $CONF, $manager;
948                 
949                 switch ( $orderby )
950                 {
951                         case 'number':
952                                 $orderby='bnumber';
953                                 break;
954                         case 'name':
955                                 $orderby='bname';
956                                 break;
957                         case 'shortname':
958                                 $orderby='bshortname';
959                                 break;
960                         case 'description':
961                                 $orderby='bdesc';
962                                 break;
963                         default:
964                                 $orderby='bnumber';
965                                 break;
966                 }
967                 
968                 $direction=strtolower($direction);
969                 switch ( $direction )
970                 {
971                         case 'asc':
972                                 $direction='ASC';
973                                 break;
974                         case 'desc':
975                                 $direction='DESC';
976                                 break;
977                         default:
978                                 $direction='ASC';
979                                 break;
980                 }
981                 
982                 $template =& $manager->getTemplate($template);
983                 
984                 if ( array_key_exists('BLOGLIST_HEADER', $template) && !empty($template['BLOGLIST_HEADER']) )
985                 {
986                         $vars = array(
987                                 'sitename'      => $CONF['SiteName'],
988                                 'siteurl'       => $CONF['IndexURL']
989                         );
990                         
991                         echo Template::fill($template['BLOGLIST_HEADER'], $vars);
992                 }
993                 
994                 if ( array_key_exists('BLOGLIST_LISTITEM', $template) && !empty($template['BLOGLIST_LISTITEM']) )
995                 {
996                         $query = 'SELECT bnumber, bname, bshortname, bdesc, burl FROM '.sql_table('blog').' ORDER BY '.$orderby.' '.$direction;
997                         $res = DB::getResult($query);
998                         
999                         foreach ( $res as $row )
1000                         {
1001                                 $list = array();
1002                                 $list['bloglink'] = Link::create_blogid_link($row['bnumber']);
1003                                 $list['blogdesc'] = $row['bdesc'];
1004                                 $list['blogurl'] = $row['burl'];
1005                                 
1006                                 if ( $bnametype == 'shortname' )
1007                                 {
1008                                         $list['blogname'] = $row['bshortname'];
1009                                 }
1010                                 else
1011                                 {
1012                                         /* all other cases */
1013                                         $list['blogname'] = $row['bname'];
1014                                 }
1015
1016                                 $data = array('listitem' => &$list);
1017                                 $manager->notify('PreBlogListItem', $data);
1018                                 
1019                                 echo Template::fill($template['BLOGLIST_LISTITEM'], $list);
1020                         }
1021                         
1022                         $res->closeCursor();
1023                 }
1024                 
1025                 
1026                 if ( array_key_exists('BLOGLIST_FOOTER', $template) && !empty($template['BLOGLIST_FOOTER']) )
1027                 {
1028                         $vars = array(
1029                                 'sitename' => $CONF['SiteName'],
1030                                 'siteurl' => $CONF['IndexURL']
1031                         );
1032                         echo Template::fill($template['BLOGLIST_FOOTER']);
1033                 }
1034                 return;
1035         }
1036         
1037         /**
1038          * Blog::readSettings()
1039          * Read the blog settings
1040          * 
1041          * @param       void
1042          * @return      void
1043          */
1044         public function readSettings()
1045         {
1046                 $query =  'SELECT * FROM %s WHERE bnumber=%d;';
1047                 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);
1048                 $res = DB::getResult($query);
1049                 
1050                 $this->isValid = ($res->rowCount() > 0);
1051                 if ( $this->isValid )
1052                 {
1053                         $this->settings = $res->fetch(PDO::FETCH_ASSOC);
1054                 }
1055                 return;
1056         }
1057         
1058         /**
1059          * Blog::writeSettings()
1060          * Write the blog settings
1061          */
1062         public function writeSettings()
1063         {
1064                 // (can't use floatval since not available prior to PHP 4.2)
1065                 $offset = $this->getTimeOffset();
1066                 if ( !is_float($offset) )
1067                 {
1068                         $offset = (integer) $offset;
1069                 }
1070                 
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);
1091                 return;
1092         }
1093         
1094         /**
1095          * Blog::updateUpdatefile()
1096          * Update the update file if requested
1097          * 
1098          * @param       void
1099          * @return      void
1100          */
1101         public function updateUpdatefile()
1102         {
1103                 if ( $this->getUpdateFile() )
1104                 {
1105                         $f_update = fopen($this->getUpdateFile(), 'w');
1106                         fputs($f_update,$this->getCorrectTime());
1107                         fclose($f_update);
1108                 }
1109                 return;
1110         }
1111         
1112         /**
1113          * Blog::isValidCategory()
1114          * Check if a category with a given catid is valid
1115          * 
1116          * @param       integer $catid  ID for category
1117          * @return      boolean exists or not
1118          */
1119         public function isValidCategory($catid)
1120         {
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);
1125         }
1126         
1127         /**
1128          * Blog::getCategoryName()
1129          * Get the category name for a given catid
1130          * 
1131          * @param       integer $catid  ID for category
1132          * @return      string  name of category
1133          */
1134         public function getCategoryName($catid)
1135         {
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);
1139                 return $res;
1140         }
1141         
1142         /**
1143          * Blog::getCategoryDesc()
1144          * Get the category description for a given catid
1145          * 
1146          * @param $catid
1147          *      category id
1148          */
1149         public function getCategoryDesc($catid)
1150         {
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);
1154                 return $res;
1155         }
1156         
1157         /**
1158          * Blog::getCategoryIdFromName
1159          * Get the category id for a given category name
1160          * 
1161          * @param       string  $name   category name
1162          * @return      ID for category
1163          */
1164         public function getCategoryIdFromName($name)
1165         {
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));
1168                 
1169                 $res = DB::getValue();
1170                 if ( !$res )
1171                 {
1172                         return $this->getDefaultCategory();
1173                 }
1174                 return $res;
1175         }
1176         
1177         /**
1178          * Blog::insertJavaScriptInfo()
1179          * Insert a javascript that includes information about the settings
1180          * of an author:  ConvertBreaks, MediaUrl and AuthorId
1181          * 
1182          * @param       $authorid       id of the author
1183          */
1184         public function insertJavaScriptInfo($authorid = '')
1185         {
1186                 global $member, $CONF;
1187                 
1188                 if ( $authorid == '' )
1189                 {
1190                         $authorid = $member->getID();
1191                 }
1192                 
1193                 echo "<script type=\"text/javascript\">\n";
1194                 
1195                 if ( !$this->convertBreaks() )
1196                 {
1197                         echo "setConvertBreaks(false);\n";
1198                 }
1199                 else
1200                 {
1201                         echo "setConvertBreaks(true);\n";
1202                 }
1203                 echo "setMediaUrl('{$CONF['MediaURL']}');\n";
1204                 echo "setAuthorId('{$authorid}');\n";
1205                 echo "</script>\n";
1206                 return;
1207         }
1208         
1209         /**
1210          * Blog::setAllowPastPosting()
1211          * Set the the setting for allowing to publish postings in the past
1212          * 
1213          * @param       boolean $val    new value for ballowpast
1214          * @return      void
1215          */
1216         public function setAllowPastPosting($val)
1217         {
1218                 $this->setSetting('ballowpast', $val);
1219                 return;
1220         }
1221         
1222         /**
1223          * Blog::allowPastPosting()
1224          * Get the the setting if it is allowed to publish postings in the past
1225          * [should be named as getAllowPastPosting()]
1226          * 
1227          * @param       void
1228          * @return      boolean
1229          */
1230         public function allowPastPosting()
1231         {
1232                 return $this->getSetting('ballowpast');
1233         }
1234         
1235         /**
1236          * Blog::getCorrectTime()
1237          * 
1238          * @param       integer $t
1239          * @return      integer
1240          */
1241         public function getCorrectTime($t=0)
1242         {
1243                 if ( $t == 0 )
1244                 {
1245                         $t = time();
1246                 }
1247                 return ($t + 3600 * $this->getTimeOffset());
1248         }
1249         
1250         /**
1251          * Blog::getName()
1252          * 
1253          * @param       void
1254          * @return      string name of this weblog
1255          */
1256         public function getName()
1257         {
1258                 return $this->getSetting('bname');
1259         }
1260         
1261         /**
1262          * Blog::getShortName()
1263          * 
1264          * @param       void
1265          * @return      string  short name of this weblog
1266          */
1267         public function getShortName()
1268         {
1269                 return $this->getSetting('bshortname');
1270         }
1271         
1272         /**
1273          * Blog::getMaxComments()
1274          * 
1275          * @param       void
1276          * @return      integer maximum number of comments
1277          */
1278         public function getMaxComments()
1279         {
1280                 return $this->getSetting('bmaxcomments');
1281         }
1282         
1283         /**
1284          * Blog::getNotifyAddress()
1285          * 
1286          * @param       void
1287          * @return      string  mail address for notifying
1288          */
1289         public function getNotifyAddress()
1290         {
1291                 return $this->getSetting('bnotify');
1292         }
1293         
1294         /**
1295          * Blog::getNotifyType()
1296          * 
1297          * @param       void
1298          * @return      integer notifycation type
1299          */
1300         public function getNotifyType()
1301         {
1302                 return $this->getSetting('bnotifytype');
1303         }
1304         
1305         /**
1306          * Blog::notifyOnComment()
1307          * 
1308          * @param       void
1309          * @return      boolean
1310          */
1311         public function notifyOnComment()
1312         {
1313                 $n = $this->getNotifyType();
1314                 return (($n != 0) && (($n % 3) == 0));
1315         }
1316         
1317         /**
1318          * Blog::notifyOnVote()
1319          * 
1320          * @param       void
1321          * @return      boolean
1322          */
1323         public function notifyOnVote()
1324         {
1325                 $n = $this->getNotifyType();
1326                 return (($n != 0) && (($n % 5) == 0));
1327         }
1328         
1329         /**
1330          * Blog::notifyOnNewItem()
1331          * 
1332          * @param       void
1333          * @return      boolean
1334          */
1335         public function notifyOnNewItem()
1336         {
1337                 $n = $this->getNotifyType();
1338                 return (($n != 0) && (($n % 7) == 0));
1339         }
1340         
1341         /**
1342          * Blog::setNotifyType()
1343          * 
1344          * @param       integer $val
1345          * @return      void
1346          */
1347         public function setNotifyType($val)
1348         {
1349                 $this->setSetting('bnotifytype',$val);
1350                 return;
1351         }
1352         
1353         /**
1354          * Blog::getTimeOffset()
1355          * @param       void
1356          * @return      
1357          */
1358         public function getTimeOffset()
1359         {
1360                 return $this->getSetting('btimeoffset');
1361         }
1362         
1363         /**
1364          * Blog::commentsEnabled()
1365          * @param       void
1366          * @return      integer enabled or not
1367          */
1368         public function commentsEnabled()
1369         {
1370                 return $this->getSetting('bcomments');
1371         }
1372         
1373         /**
1374          * Blog::getURL()
1375          * @param       void
1376          * @return      string  URI for this weblog
1377          */
1378         public function getURL()
1379         {
1380                 return $this->getSetting('burl');
1381         }
1382         
1383         /**
1384          * Blog::getDefaultSkin()
1385          * @param       void
1386          * @return      name of skin as default for this weblog
1387          */
1388         public function getDefaultSkin()
1389         {
1390                 return $this->getSetting('bdefskin');
1391         }
1392         
1393         /**
1394          * Blog::getUpdateFile()
1395          * @param       void
1396          * @return      string  name of file to be updated when weblog is updated
1397          */
1398         public function getUpdateFile()
1399         {
1400                 return $this->getSetting('bupdate');
1401         }
1402         
1403         /**
1404          * Blog::getDescription()
1405          * @param       void
1406          * @return      string  description for this weblog
1407          */
1408         public function getDescription()
1409         {
1410                 return $this->getSetting('bdesc');
1411         }
1412         
1413         /**
1414          * Blog::isPublic()
1415          * @param       void
1416          * @return      integer publlic or not
1417          */
1418         public function isPublic()
1419         {
1420                 return $this->getSetting('bpublic');
1421         }
1422         
1423         /**
1424          * Blog::emailRequired()
1425          * @param       void
1426          * @return      integer email is required when posting comment or not
1427          */
1428         public function emailRequired()
1429         {
1430                 return $this->getSetting('breqemail');
1431         }
1432         
1433         /**
1434          * Blog::getSearchable()
1435          * @param       void
1436          * @return      integer searchable or not
1437          */
1438         public function getSearchable()
1439         {
1440                 return $this->getSetting('bincludesearch');
1441         }
1442         
1443         /**
1444          * Blog::getDefaultCategory()
1445          * @param       void
1446          * @return      ID for category as a default
1447          */
1448         public function getDefaultCategory()
1449         {
1450                 return $this->getSetting('bdefcat');
1451         }
1452         
1453         /**
1454          * Blog::setPublic()
1455          * @param       integer $val    allow comments by non-registered members or not
1456          * @return      void
1457          */
1458         public function setPublic($val)
1459         {
1460                 $this->setSetting('bpublic', $val);
1461                 return;
1462         }
1463         
1464         /**
1465          * Blog::setSearchable()
1466          * @param       integer $val    searchable from the other blogs or not
1467          * @return      void
1468          */
1469         public function setSearchable($val)
1470         {
1471                 $this->setSetting('bincludesearch', $val);
1472                 return;
1473         }
1474         
1475         /**
1476          * Blog::setDescription
1477          * @param       string  $val    description for this weblog
1478          * @return      void
1479          */
1480         public function setDescription($val)
1481         {
1482                 $this->setSetting('bdesc',$val);
1483                 return;
1484         }
1485         
1486         /**
1487          * Blog::setUpdateFile()
1488          * @param       string  $val    name of file to beupdated when weblog is updated
1489          * @return      
1490          */
1491         public function setUpdateFile($val)
1492         {
1493                 $this->setSetting('bupdate',$val);
1494                 return;
1495         }
1496         
1497         /**
1498          * Blog::setDefaultSkin()
1499          * @param       integer $val    ID for default skin to use when displaying this weblog
1500          * @return      void
1501          */
1502         public function setDefaultSkin($val)
1503         {
1504                 $this->setSetting('bdefskin', $val);
1505                 return;
1506         }
1507         
1508         /**
1509          * Blog::setURL()
1510          * @param       string  $val    URI for this weblog
1511          * @return      
1512          */
1513         public function setURL($val)
1514         {
1515                 $this->setSetting('burl', $val);
1516                 return;
1517         }
1518         
1519         /**
1520          * Blog::setName()
1521          * @param       string  $val    name of this weblog
1522          * @return      void
1523          */
1524         public function setName($val)
1525         {
1526                 $this->setSetting('bname', $val);
1527                 return;
1528         }
1529         
1530         /**
1531          * Blog::setShortName()
1532          * @param       string  $val    short name for this weblog
1533          * @return      void
1534          */
1535         public function setShortName($val)
1536         {
1537                 $this->setSetting('bshortname', $val);
1538                 return;
1539         }
1540         
1541         /**
1542          * Blog::setCommentsEnabled()
1543          * @param       integer $val    enabling posting comment or not
1544          * @return      void
1545          */
1546         public function setCommentsEnabled($val)
1547         {
1548                 $this->setSetting('bcomments',$val);
1549                 return;
1550         }
1551         
1552         /**
1553          * Blog::setMaxComments()
1554          * @param       integer $val    maximum number of comments for this weblog
1555          * @return      void
1556          */
1557         public function setMaxComments($val)
1558         {
1559                 $this->setSetting('bmaxcomments', $val);
1560                 return;
1561         }
1562         
1563         /**
1564          * Blog::setNotifyAddress()
1565          * @param       string  $val    email to be notified if weblog updated
1566          * @return      void
1567          */
1568         public function setNotifyAddress($val)
1569         {
1570                 $this->setSetting('bnotify', $val);
1571                 return;
1572         }
1573         
1574         /**
1575          * Blog::setEmailRequired()
1576          * @param       string  requiring comments with email or not from non member
1577          * @return      void
1578          */
1579         public function setEmailRequired($val)
1580         {
1581                 $this->setSetting('breqemail', $val);
1582                 return;
1583         }
1584         
1585         /**
1586          * Blog::setTimeOffset()
1587          * @param       integer $val    time offset
1588          * @return      void
1589          */
1590         public function setTimeOffset($val)
1591         {
1592                 // check validity of value
1593                 // 1. replace , by . (common mistake)
1594                 $val = str_replace(',','.',$val);
1595                 
1596                 // 2. cast to float or int
1597                 if ( is_numeric($val) && (i18n::strpos($val, '.5') === (i18n::strlen($val) - 2)) )
1598                 {
1599                         $val = (float) $val;
1600                 }
1601                 else
1602                 {
1603                         $val = (integer) $val;
1604                 }
1605                 
1606                 $this->setSetting('btimeoffset',$val);
1607                 return;
1608         }
1609         
1610         /**
1611          * Blog::setDefaultCategory()
1612          * @param       integer $val    ID for default category for this weblog
1613          * @return      
1614          */
1615         public function setDefaultCategory($val)
1616         {
1617                 $this->setSetting('bdefcat',$val);
1618                 return;
1619         }
1620         
1621         /**
1622          * Blog::getSetting()
1623          * @param       string  $key    key for setting of this weblog
1624          * @return      mixed   value for the setting
1625          */
1626         public function getSetting($key)
1627         {
1628                 return $this->settings[$key];
1629         }
1630         
1631         /**
1632          * Blog::setSetting()
1633          * @param       string  $key    key for setting of this weblog
1634          * @param       mixed   $value  value for the key
1635          * @return      
1636          */
1637         public function setSetting($key, $value)
1638         {
1639                 $this->settings[$key] = $value;
1640                 return;
1641         }
1642         
1643         /**
1644          * Blog::addTeamMember()
1645          * Tries to add a member to the team. 
1646          * Returns false if the member was already on the team
1647          * 
1648          * @param       integer $memberid       id for member
1649          * @param       boolean $admin  super-admin or not
1650          * @return      boolean Success/Fail
1651          */
1652         public function addTeamMember($memberid, $admin)
1653         {
1654                 global $manager;
1655                 
1656                 $memberid = intval($memberid);
1657                 $admin = intval($admin);
1658                 
1659                 // check if member is already a member
1660                 $tmem =& $manager->getMember($memberid);
1661                 
1662                 if ( $tmem->isTeamMember($this->blogid) )
1663                 {
1664                         return 0;
1665                 }
1666                 
1667                 $data = array(
1668                         'blog'          => &$this,
1669                         'member'        => &$tmem,
1670                         'admin'         => &$admin
1671                 );
1672                 $manager->notify('PreAddTeamMember', $data);
1673                 
1674                 // add to team
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);
1678                 
1679                 $data = array(
1680                         'blog'          => &$this,
1681                         'member'        => &$tmem,
1682                         'admin'         =>  $admin
1683                 );
1684                 $manager->notify('PostAddTeamMember', $data);
1685                 
1686                 $logMsg = sprintf(_TEAM_ADD_NEWTEAMMEMBER, $tmem->getDisplayName(), $memberid, $this->getName());
1687                 ActionLog::add(INFO, $logMsg);
1688                 
1689                 return 1;
1690         }
1691         
1692         /**
1693          * Blog::getID()
1694          * @param       void
1695          * @return      integer ID for this weblog
1696          */
1697         public function getID()
1698         {
1699                 return (integer) $this->blogid;
1700         }
1701         
1702         /**
1703          * Checks if a blog with a given shortname exists 
1704          * Returns true if there is a blog with the given shortname (static)
1705          * 
1706          * @param       string  $name           blog shortname
1707          * @return      boolean exists or not
1708          */
1709         public function exists($name)
1710         {
1711                 $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bshortname='. DB::quoteValue($name));
1712                 return ($r->rowCount() != 0);
1713         }
1714         
1715         /**
1716          * Checks if a blog with a given id exists 
1717          * Returns true if there is a blog with the given ID (static)
1718          * 
1719          * @param       integer $id     ID for searched weblog
1720          * @return      boolean exists or not
1721          */
1722         public function existsID($id)
1723         {
1724                 $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));
1725                 return ($r->rowCount() != 0);
1726         }
1727         
1728         /**
1729          * Blog::setFuturePost()
1730          * flag there is a future post pending
1731          * 
1732          * @param       void
1733          * @return      void
1734          */
1735         public function setFuturePost()
1736         {
1737                 $query =  "UPDATE %s SET bfuturepost='1' WHERE bnumber=%d;";
1738                 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);
1739                 DB::execute($query);
1740                 return;
1741         }
1742         
1743         /**
1744          * Blog::clearFuturePost()
1745          * clear there is a future post pending
1746          * 
1747          * @param       void
1748          * @return      void
1749          */
1750         public function clearFuturePost()
1751         {
1752                 $query =  "UPDATE %s SET bfuturepost='0' WHERE bnumber=%d;";
1753                 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);
1754                 DB::execute($query);
1755                 return;
1756         }
1757         
1758         /**
1759          * Blog::checkJustPosted()
1760          * check if we should throw justPosted event 
1761          * 
1762          * @param       void
1763          * @return      void
1764          */
1765         public function checkJustPosted()
1766         {
1767                 global $manager;
1768                 
1769                 if ( $this->settings['bfuturepost'] == 1 )
1770                 {
1771                         $query = "SELECT * FROM %s WHERE iposted=0 AND iblog=%d AND itime < NOW();";
1772                         $query = sprintf($query, sql_table('item'), (integer) $this->blogid);
1773                         
1774                         $result = DB::getResult($query);
1775                         if ( $result->rowCount() > 0 )
1776                         {
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
1779                                 $pinged = FALSE;
1780                                 $data = array('blogid' => $this->blogid, 'pinged' => &$pinged);
1781                                 $manager->notify('JustPosted', $data);
1782                                 
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);
1787                                 
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);
1791                                 
1792                                 $result = DB::getResult($query);
1793                                 if ( $result->rowCount() == 0 )
1794                                 {
1795                                         $this->clearFuturePost();
1796                                 }
1797                         }
1798                 }
1799                 return;
1800         }
1801         
1802         /**
1803          * Blog::readLogFromList()
1804          * Shows the given list of items for this blog
1805          *
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
1814          */
1815         public function readLogFromList($itemarray, $template, $highlight = '', $comments = 1, $dateheads = 1,$showDrafts = 0, $showFuture = 0)
1816         {
1817                 $query = $this->getSqlItemList($itemarray,$showDrafts,$showFuture);
1818                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
1819         }
1820         
1821         /**
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)
1825          *
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
1830          */
1831         public function getSqlItemList($itemarray,$showDrafts = 0,$showFuture = 0)
1832         {
1833                 if ( !is_array($itemarray) )
1834                 {
1835                         return '';
1836                 }
1837                 
1838                 $showDrafts = intval($showDrafts);
1839                 $showFuture = intval($showFuture);
1840                 $items = array();
1841                 
1842                 foreach ( $itemarray as $value )
1843                 {
1844                         if ( intval($value) )
1845                         {
1846                                 $items[] = intval($value);
1847                         }
1848                 }
1849                 if ( !count($items) )
1850                 {
1851                         return '';
1852                 }
1853                 
1854                 $i = count($items);
1855                 $query = '';
1856                 foreach ( $items as $value )
1857                 {
1858                         $query .= '('
1859                                         .       'SELECT'
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,'
1865                                         .       ' i.itime,'
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';
1873                         
1874                         $query .= ' FROM '
1875                                         . sql_table('item') . ' as i, '
1876                                         . sql_table('member') . ' as m, '
1877                                         . sql_table('category') . ' as c'
1878                                         . ' WHERE'
1879                                     .    ' i.iblog='.$this->blogid
1880                                    . ' and i.iauthor=m.mnumber'
1881                                    . ' and i.icat=c.catid';
1882                         
1883                         // exclude drafts       
1884                         if ( !$showDrafts )
1885                         {
1886                                 $query .= ' and i.idraft=0';
1887                         }
1888                         if ( !$showFuture )
1889                         {
1890                                 // don't show future items
1891                                 $query .= ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime());
1892                         }
1893                         
1894                         $query .= ' and i.inumber='.intval($value);
1895                         $query .= ')';
1896                         $i--;
1897                         if ($i) $query .= ' UNION ';
1898                 }
1899                 
1900                 return $query;
1901         }
1902         
1903         /**
1904          * Blog::convertBreaks()
1905          * Get the the setting for the line break handling
1906          * [should be named as getConvertBreaks()]
1907          * 
1908          * @deprecated
1909          * @param       void
1910          * @return      
1911          */
1912         public function convertBreaks()
1913         {
1914                 return $this->getSetting('bconvertbreaks');
1915         }
1916         
1917         /**
1918          * Set the the setting for the line break handling
1919          * 
1920          * @deprecated
1921          * @param       boolean $val    new value for bconvertbreaks
1922          * @return      void
1923          */
1924         public function setConvertBreaks($val)
1925         {
1926                 $this->setSetting('bconvertbreaks', $val);
1927                 return;
1928         }
1929 }