OSDN Git Service

FIX: sprintf()に与える引数の記述のミス
[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-2009 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         var $blogid;
29
30         // ID of currently selected category
31         var $selectedcatid;
32
33         // After creating an object of the blog class, contains true if the BLOG object is
34         // valid (the blog exists)
35         var $isValid;
36
37         // associative array, containing all blogsettings (use the get/set functions instead)
38         var $settings;
39
40         /**
41          * Creates a new BLOG object for the given blog
42          *
43          * @param $id blogid
44          */
45         function BLOG($id) {
46                 $this->blogid = intval($id);
47                 $this->readSettings();
48
49                 // try to set catid
50                 // (the parse functions in SKIN.php will override this, so it's mainly useless)
51                 global $catid;
52                 $this->setSelectedCategory($catid);
53         }
54
55         /**
56          * Shows the given amount of items for this blog
57          *
58          * @param $template
59          *              String representing the template _NAME_ (!)
60          * @param $amountEntries
61          *              amount of entries to show
62          * @param $startpos
63          *              offset from where items should be shown (e.g. 5 = start at fifth item)
64          * @returns int
65          *              amount of items shown
66          */
67         function readLog($template, $amountEntries, $offset = 0, $startpos = 0) {
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          */
81         function showArchive($templatename, $year, $month=0, $day=0)
82         {
83                 // create extra where clause for select query
84                 if ( $day == 0 && $month != 0 )
85                 {
86                         $timestamp_start = mktime(0,0,0,$month,1,$year);
87                         // also works when $month==12
88                         $timestamp_end = mktime(0,0,0,$month+1,1,$year);
89                 }
90                 elseif ( $month == 0 )
91                 {
92                         $timestamp_start = mktime(0,0,0,1,1,$year);
93                         // also works when $month==12
94                         $timestamp_end = mktime(0,0,0,12,31,$year);
95                 }
96                 else
97                 {
98                         $timestamp_start = mktime(0,0,0,$month,$day,$year);
99                         $timestamp_end = mktime(0,0,0,$month,$day+1,$year);
100                 }
101                 $extra_query = " and i.itime>='%s' and i.itime<'%s'";
102                 $extra_query = sprintf($extra_query, i18n::formatted_datetime('mysql', $timestamp_start), i18n::formatted_datetime('mysql', $timestamp_end));
103                 
104                 $this->readLogAmount($templatename,0,$extra_query,'',1,1);
105                 return;
106         }
107
108         /**
109          * Sets the selected category by id (only when category exists)
110          */
111         function setSelectedCategory($catid) {
112                 if ($this->isValidCategory($catid) || (intval($catid) == 0))
113                         $this->selectedcatid = intval($catid);
114         }
115
116         /**
117          * Sets the selected category by name
118          */
119         function setSelectedCategoryByName($catname) {
120                 $this->setSelectedCategory($this->getCategoryIdFromName($catname));
121         }
122
123         /**
124          * Returns the selected category
125          */
126         function getSelectedCategory() {
127                 return $this->selectedcatid;
128         }
129
130         /**
131          * Shows the given amount of items for this blog
132          *
133          * @param $template
134          *              String representing the template _NAME_ (!)
135          * @param $amountEntries
136          *              amount of entries to show (0 = no limit)
137          * @param $extraQuery
138          *              extra conditions to be added to the query
139          * @param $highlight
140          *              contains a query that should be highlighted
141          * @param $comments
142          *              1=show comments 0=don't show comments
143          * @param $dateheads
144          *              1=show dateheads 0=don't show dateheads
145          * @param $offset
146          *              offset
147          * @returns int
148          *              amount of items shown
149          */
150         function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0) {
151
152                 $query = $this->getSqlBlog($extraQuery);
153
154                 if ($amountEntries > 0) {
155                                 // $offset zou moeten worden:
156                                 // (($startpos / $amountentries) + 1) * $offset ... later testen ...
157                            $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);
158                 }
159                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
160         }
161
162         /**
163          * BLOG::showUsingQuery()
164          * Do the job for readLogAmmount
165          * 
166          * @param       string          $templateName   template name
167          * @param       string          $query                  string for query
168          * @param       string          $highlight              string to be highlighted
169          * @param       integer $comments               the number of comments
170          * @param       boolean $dateheads              date header is needed or not
171          * @return      integer the number of rows as a result of mysql query
172          * 
173          */     
174         function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1)
175         {
176                 global $CONF, $manager, $currentTemplateName;
177                 
178                 $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');
179                 if ( $lastVisit != 0 )
180                 {
181                         $lastVisit = $this->getCorrectTime($lastVisit);
182                 }
183                 
184                 // set templatename as global variable (so plugins can access it)
185                 $currentTemplateName = $templateName;
186                 $template =& $manager->getTemplate($templateName);
187                 
188                 // create parser object & action handler
189                 $actions = new ITEMACTIONS($this);
190                 $parser = new PARSER($actions->getDefinedActions(),$actions);
191                 $actions->setTemplate($template);
192                 $actions->setHighlight($highlight);
193                 $actions->setLastVisit($lastVisit);
194                 $actions->setParser($parser);
195                 $actions->setShowComments($comments);
196                 
197                 // execute query
198                 $items = sql_query($query);
199                 
200                 // loop over all items
201                 $old_date = 0;
202                 while ( $item = sql_fetch_object($items) )
203                 {
204                         $item->timestamp = strtotime($item->itime);     // string timestamp -> unix timestamp
205                         
206                         // action handler needs to know the item we're handling
207                         $actions->setCurrentItem($item);
208                         
209                         // add date header if needed
210                         if ( $dateheads )
211                         {
212                                 $new_date = date('dFY',$item->timestamp);
213                                 if ( $new_date != $old_date )
214                                 {
215                                         // unless this is the first time, write date footer
216                                         $timestamp = $item->timestamp;
217                                         if ( $old_date != 0 )
218                                         {
219                                                 $oldTS = strtotime($old_date);
220                                                 $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));
221                                                 $tmp_footer = i18n::strftime(isset($template['DATE_FOOTER'])?$template['DATE_FOOTER']:'', $oldTS);
222                                                 $parser->parse($tmp_footer);
223                                                 $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));
224                                         }
225                                         $manager->notify('PreDateHead',array('blog' => &$this, 'timestamp' => $timestamp));
226                                         // note, to use templatvars in the dateheader, the %-characters need to be doubled in
227                                         // order to be preserved by strftime
228                                         $tmp_header = i18n::strftime((isset($template['DATE_HEADER']) ? $template['DATE_HEADER'] : null), $timestamp);
229                                         $parser->parse($tmp_header);
230                                         $manager->notify('PostDateHead',array('blog' => &$this, 'timestamp' => $timestamp));
231                                 }
232                                 $old_date = $new_date;
233                         }
234                         
235                         // parse item
236                         $parser->parse($template['ITEM_HEADER']);
237                         $manager->notify('PreItem', array('blog' => &$this, 'item' => &$item));
238                         $parser->parse($template['ITEM']);
239                         $manager->notify('PostItem', array('blog' => &$this, 'item' => &$item));
240                         $parser->parse($template['ITEM_FOOTER']);
241                 }
242                 
243                 $numrows = sql_num_rows($items);
244                 
245                 // add another date footer if there was at least one item
246                 if ( ($numrows > 0) && $dateheads )
247                 {
248                         $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));
249                         $parser->parse($template['DATE_FOOTER']);
250                         $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));
251                 }
252                 
253                 sql_free_result($items);
254                 return $numrows;
255         }
256         
257         /**
258          * Simplified function for showing only one item
259          */
260         function showOneitem($itemid, $template, $highlight) {
261                 $extraQuery = ' and inumber=' . intval($itemid);
262
263                 return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);
264         }
265
266
267         /**
268          * BLOG::addItem()
269          * Adds an item to this blog
270          * 
271          * @param       Integer $catid  ID for category
272          * @param       String  $title  ID for 
273          * @param       String  $body   text for body
274          * @param       String  $more   text for more
275          * @param       Integer $blogid ID for blog
276          * @param       Integer $authorid       ID for author
277          * @param       Timestamp       $timestamp      UNIX timestamp for post
278          * @param       Boolean $closed opened or closed
279          * @param       Boolean $draft  draft or not
280          * @param       Boolean $posted posted or not
281          * @return
282          */
283         function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft, $posted='1')
284         {
285                 global $manager;
286                 
287                 $blogid         = intval($blogid);
288                 $authorid       = intval($authorid);
289                 $title          = $title;
290                 $body           = $body;
291                 $more           = $more;
292                 $catid          = intval($catid);
293                 
294                 // convert newlines to <br />
295                 if ( $this->convertBreaks() )
296                 {
297                         $body = addBreaks($body);
298                         $more = addBreaks($more);
299                 }
300
301                 if ( $closed != '1' )
302                 {
303                         $closed = '0';
304                 }
305                 if ( $draft != '0' )
306                 {
307                         $draft = '1';
308                 }
309                 
310                 if ( !$this->isValidCategory($catid) )
311                 {
312                         $catid = $this->getDefaultCategory();
313                 }
314                 
315                 if ( $timestamp > $this->getCorrectTime() )
316                 {
317                         $isFuture = 1;
318                 }
319                 
320                 $timestamp = date('Y-m-d H:i:s',$timestamp);
321                 
322                 $manager->notify('PreAddItem',array('title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$this, 'authorid' => &$authorid, 'timestamp' => &$timestamp, 'closed' => &$closed, 'draft' => &$draft, 'catid' => &$catid));
323                 
324                 $ititle = sql_real_escape_string($title);
325                 $ibody = sql_real_escape_string($body);
326                 $imore = sql_real_escape_string($more);
327                 
328                 $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)";
329                 $query = sprintf($query, sql_table('item'), $ititle, $ibody, $imore, $blogid, $authorid, $timestamp, $closed, $draft, $catid, $posted);
330                 sql_query($query);
331                 $itemid = sql_insert_id();
332                 
333                 $manager->notify('PostAddItem',array('itemid' => $itemid));
334                 
335                 if ( !$draft )
336                 {
337                         $this->updateUpdateFile();
338                 }
339                 // send notification mail
340                 if ( !$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem() )
341                 {
342                         $this->sendNewItemNotification($itemid, $title, $body);
343                 }
344                 return $itemid;
345         }
346         
347         /**
348          * BLOG::sendNewItemNotification()
349          * Send a new item notification to the notification list
350          * 
351          * @param String        $itemid ID of the item
352          * @param String        $title  title of the item
353          * @param String        $body   body of the item
354          * @return      Void
355          */
356         function sendNewItemNotification($itemid, $title, $body)
357         {
358                 global $CONF, $member;
359                 
360                 $ascii = ENTITY::anchor_footnoting($body);
361                 
362                 $message = _NOTIFY_NI_MSG . " \n";
363                 $temp = parse_url($CONF['Self']);
364                 if ( $temp['scheme'] )
365                 {
366                         $message .= LINK::create_item_link($itemid) . "\n\n";
367                 }
368                 else
369                 {
370                         $tempurl = $this->getURL();
371                         if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )
372                         {
373                                 $message .= $tempurl . '?itemid=' . $itemid . "\n\n";
374                         }
375                         else
376                         {
377                                 $message .= $tempurl . '/?itemid=' . $itemid . "\n\n";
378                         }
379                 }
380                 $message .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";
381                 $message .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";
382                 $message .= NOTIFICATION::get_mail_footer();
383                 
384                 $subject = $this->getName() . ': ' . _NOTIFY_NI_TITLE;
385                 
386                 $from = $member->getNotifyFromMailAddress();
387                 
388                 NOTIFICATION::mail($this->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());
389                 return;
390         }
391         
392         /**
393          * BLOG::createNewCategory()
394          * Creates a new category for this blog
395          *
396          * @param String        $catName        name of the new category. When empty, a name is generated automatically (starting with newcat)
397          * @param String        $catDescription description of the new category. Defaults to 'New Category'
398          * @returns     Integer the new category-id in case of success. 0 on failure
399          */
400         function createNewCategory($catName = '', $catDescription = _CREATED_NEW_CATEGORY_DESC)
401         {
402                 global $member, $manager;
403                 
404                 if ( $member->blogAdminRights($this->getID()) )
405                 {
406                         // generate
407                         if ( $catName == '' )
408                         {
409                                 $catName = _CREATED_NEW_CATEGORY_NAME;
410                                 $i = 1;
411                                 
412                                 $res = sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID());
413                                 while ( sql_num_rows($res) > 0 )
414                                 {
415                                         $i++;
416                                         $res = sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID());
417                                 }
418                                 
419                                 $catName = $catName . $i;
420                         }
421                         
422                         $manager->notify(
423                                 'PreAddCategory',
424                                 array(
425                                         'blog' => &$this,
426                                         'name' => &$catName,
427                                         'description' => $catDescription
428                                 )
429                         );
430                         
431                         $query = "INSERT INTO %s (cblog, cname, cdesc) VALUES (%d, '%s', '%s')";
432                         $query = sprintf($query, sql_table('category'), (integer) $this->getID(), sql_real_escape_string($catName), sql_real_escape_string($catDescription));
433                         sql_query($query);
434                         $catid = sql_insert_id();
435                         
436                         $manager->notify(
437                                 'PostAddCategory',
438                                 array(
439                                         'blog' => &$this,
440                                         'name' => $catName,
441                                         'description' => $catDescription,
442                                         'catid' => $catid
443                                 )
444                         );
445                         
446                         return $catid;
447                 }
448                 return 0;
449         }
450         
451         /**
452          * Searches all months of this blog for the given query
453          *
454          * @param $query
455          *              search query
456          * @param $template
457          *              template to be used (__NAME__ of the template)
458          * @param $amountMonths
459          *              max amount of months to be search (0 = all)
460          * @param $maxresults
461          *              max number of results to show
462          * @param $startpos
463          *              offset
464          * @returns
465          *              amount of hits found
466          */
467         function search($query, $template, $amountMonths, $maxresults, $startpos) {
468                 global $CONF, $manager;
469
470                 $highlight      = '';
471                 $sqlquery       = $this->getSqlSearch($query, $amountMonths, $highlight);
472
473                 if ($sqlquery == '')
474                 {
475                         // no query -> show everything
476                         $extraquery = '';
477                         $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);
478                 } else {
479
480                         // add LIMIT to query (to split search results into pages)
481                         if (intval($maxresults > 0))
482                                 $sqlquery .= ' LIMIT ' . intval($startpos).',' . intval($maxresults);
483
484                         // show results
485                         $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);
486
487                         // when no results were found, show a message
488                         if ($amountfound == 0)
489                         {
490                                 $template =& $manager->getTemplate($template);
491                                 $vars = array(
492                                         'query'         => ENTITY::hsc($query),
493                                         'blogid'        => $this->getID()
494                                 );
495                                 echo TEMPLATE::fill($template['SEARCH_NOTHINGFOUND'],$vars);
496                         }
497                 }
498
499                 return $amountfound;
500         }
501
502         /**
503          * BLOG::getSqlSearch()
504          * Returns an SQL query to use for a search query
505          * No LIMIT clause is added. (caller should add this if multiple pages are requested)
506          *
507          * @param string        $query  search query
508          * @param       integer $amountMonths   amount of months to search back. Default = 0 = unlimited
509          * @param       string  $mode   either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
510          * @return      string  $highlight      words to highlight (out parameter)
511          * @return      string  either a full SQL query, or an empty string (if querystring empty)
512          */
513         function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')
514         {
515                 $searchclass = new SEARCH($query);
516                 
517                 $highlight       = $searchclass->inclusive;
518                 
519                 // if querystring is empty, return empty string
520                 if ( $searchclass->inclusive == '' )
521                 {
522                         return '';
523                 }
524                 
525                 $where  = $searchclass->boolean_sql_where('ititle, ibody, imore');
526                 $select = $searchclass->boolean_sql_select('ititle, ibody, imore');
527                 
528                 // get list of blogs to search
529                 $blogs                  = $searchclass->blogs;  // array containing blogs that always need to be included
530                 $blogs[]                = $this->getID();                       // also search current blog (duh)
531                 $blogs                  = array_unique($blogs); // remove duplicates
532                 $selectblogs    = '';
533                 
534                 if ( count($blogs) > 0 )
535                 {
536                         $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';
537                 }
538                 
539                 if ( $mode == '' )
540                 {
541                         $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,
542                                 m.mname as author, m.mrealname as authorname, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl,
543                                 c.cname as category';
544                         
545                         if ( $select )
546                         {
547                                 $query .= ', '.$select. ' as score ';
548                         }
549                 }
550                 else
551                 {
552                         $query = 'SELECT COUNT(*) as result ';
553                 }
554                 
555                 $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
556                                 . ' WHERE i.iauthor=m.mnumber'
557                                 . ' and i.icat=c.catid'
558                                 // exclude drafts
559                                 . ' and i.idraft=0'
560                                 . $selectblogs
561                                 // don't show future items
562                                 . ' and i.itime<="' . i18n::formatted_datetime('mysql', $this->getCorrectTime()) . '"'
563                                 . ' and '.$where;
564
565                 // take into account amount of months to search
566                 if ( $amountMonths > 0 )
567                 {
568                         $localtime = getdate($this->getCorrectTime());
569                         $timestamp_start = mktime(0,0,0,$localtime['mon'] - $amountMonths,1,$localtime['year']);
570                         $query .= ' and i.itime>"' . i18n::formatted_datetime('mysql', $timestamp_start) . '"';
571                 }
572                 
573                 if ( $mode == '' )
574                 {
575                         if ( $select )
576                         {
577                                 $query .= ' ORDER BY score DESC';
578                         }
579                         else
580                         {
581                                 $query .= ' ORDER BY i.itime DESC ';
582                         }
583                 }
584                 return $query;
585         }
586         
587         /**
588          * BLOG::getSqlBlog()
589          * Returns the SQL query that's normally used to display the blog items on the index type skins
590          * No LIMIT clause is added. (caller should add this if multiple pages are requested)
591          *
592          * @param       string  $extraQuery     extra query string
593          * @param       string  $mode                   either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
594          * @return      string  either a full SQL query, or an empty string
595          */
596         function getSqlBlog($extraQuery, $mode = '')
597         {
598                 if ( $mode == '' )
599                 {
600                         $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author,
601                                 m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail,
602                                 m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';
603                 }
604                 else
605                 {
606                         $query = 'SELECT COUNT(*) as result ';
607                 }
608                 
609                 $query  .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
610                                 . ' WHERE i.iblog='.$this->blogid
611                                 . ' and i.iauthor=m.mnumber'
612                                 . ' and i.icat=c.catid'
613                                 // exclude drafts
614                                 . ' and i.idraft=0'
615                                 // don't show future items
616                                 . ' and i.itime<="' . i18n::formatted_datetime('mysql', $this->getCorrectTime()) . '"';
617                 
618                 if ( $this->getSelectedCategory() )
619                 {
620                         $query .= ' and i.icat=' . $this->getSelectedCategory() . ' ';
621                 }
622                 
623                 $query .= $extraQuery;
624                 
625                 if ( $mode == '' )
626                 {
627                         $query .= ' ORDER BY i.itime DESC';
628                 }
629                 return $query;
630         }
631         
632         /**
633          * BLOG::showArchiveList()
634          * Shows the archivelist using the given template
635          * 
636          * @param       String  $template       template name
637          * @param       String  $mode   year/month/day
638          * @param       Integer $limit  limit of record count
639          * @return      Void
640          */
641         function showArchiveList($template, $mode = 'month', $limit = 0)
642         {
643                 global $CONF, $catid, $manager;
644                 
645                 if ( !isset ($linkparams) )
646                 {
647                         $linkparams = array();
648                 }
649                 
650                 if ( $catid )
651                 {
652                         $linkparams = array('catid' => $catid);
653                 }
654                 
655                 $template =& $manager->getTemplate($template);
656                 $data['blogid'] = $this->getID();
657                 
658                 if ( !array_key_exists('ARCHIVELIST_HEADER', $template) || !$template['ARCHIVELIST_HEADER'] )
659                 {
660                         $tplt = '';
661                 }
662                 else
663                 {
664                         $tplt = $template['ARCHIVELIST_HEADER'];
665                 }
666                 
667                 echo TEMPLATE::fill($tplt, $data);
668                 
669                 $query = 'SELECT itime, SUBSTRING(itime,1,4) AS Year, SUBSTRING(itime,6,2) AS Month, SUBSTRING(itime,9,2) AS Day'
670                                 . ' FROM '.sql_table('item')
671                                 . ' WHERE iblog=' . $this->getID()
672                                 // don't show future items!
673                                 . ' AND itime <="' . i18n::formatted_datetime('mysql', $this->getCorrectTime()) . '"'
674                                 // don't show draft items
675                                 . ' AND idraft=0';
676                 
677                 if ( $catid )
678                 {
679                         $query .= ' and icat=' . intval($catid);
680                 }
681                 
682                 $query .= ' GROUP BY Year';
683                 if ( $mode == 'month' || $mode == 'day' )
684                 {
685                         $query .= ', Month';
686                 }
687                 if ( $mode == 'day' )
688                 {
689                         $query .= ', Day';
690                 }
691                 
692                 $query .= ' ORDER BY itime DESC';
693                 
694                 if ( $limit > 0 )
695                 {
696                         $query .= ' LIMIT ' . intval($limit);
697                 }
698                 
699                 $res = sql_query($query);
700                 while ( $current = sql_fetch_object($res) )
701                 {
702                         /* string time -> unix timestamp */
703                         $current->itime = strtotime($current->itime);
704                         
705                         if ( $mode == 'day' )
706                         {
707                                 $archivedate = date('Y-m-d',$current->itime);
708                                 $archive['day'] = date('d',$current->itime);
709                                 $data['day'] = date('d',$current->itime);
710                                 $data['month'] = date('m',$current->itime);
711                                 $archive['month'] = $data['month'];
712                         }
713                         elseif ( $mode == 'year' )
714                         {
715                                 $archivedate = date('Y',$current->itime);
716                                 $data['day'] = '';
717                                 $data['month'] = '';
718                                 $archive['day'] = '';
719                                 $archive['month'] = '';
720                         }
721                         else
722                         {
723                                 $archivedate = date('Y-m',$current->itime);
724                                 $data['month'] = date('m',$current->itime);
725                                 $archive['month'] = $data['month'];
726                                 $data['day'] = '';
727                                 $archive['day'] = '';
728                         }
729                         
730                         $data['year'] = date('Y',$current->itime);
731                         $archive['year'] = $data['year'];
732                         $data['archivelink'] = LINK::create_archive_link($this->getID(),$archivedate,$linkparams);
733                         
734                         $manager->notify(
735                                 'PreArchiveListItem',
736                                 array(
737                                         'listitem' => &$data
738                                 )
739                         );
740                         
741                         $temp = TEMPLATE::fill($template['ARCHIVELIST_LISTITEM'],$data);
742                         echo i18n::strftime($temp,$current->itime);
743                         return;
744                 }
745                 
746                 sql_free_result($res);
747                 
748                 if ( !array_key_exists('ARCHIVELIST_FOOTER', $template) || !$template['ARCHIVELIST_FOOTER'] )
749                 {
750                         $tplt = '';
751                 }
752                 else
753                 {
754                         $tplt = $template['ARCHIVELIST_FOOTER'];
755                 }
756                 
757                 echo TEMPLATE::fill($tplt, $data);
758                 return;
759         }
760         
761         /**
762          * BLOG::showCategoryList()
763          * Shows the list of categories using a given template
764          * 
765          * @param       String  $template       Template Name
766          * @return      Void
767          */
768         function showCategoryList($template)
769         {
770                 global $CONF, $manager;
771                 
772                 /*
773                  * determine arguments next to catids
774                  * I guess this can be done in a better way, but it works
775                  */
776                 global $archive, $archivelist;
777                 
778                 $linkparams = array();
779                 if ( $archive )
780                 {
781                         $blogurl = LINK::create_archive_link($this->getID(), $archive, '');
782                         $linkparams['blogid'] = $this->getID();
783                         $linkparams['archive'] = $archive;
784                 }
785                 else if ( $archivelist )
786                 {
787                         $blogurl = LINK::create_archivelist_link($this->getID(), '');
788                         $linkparams['archivelist'] = $archivelist;
789                 }
790                 else
791                 {
792                         $blogurl = LINK::create_blogid_link($this->getID(), '');
793                         $linkparams['blogid'] = $this->getID();
794                 }
795                 
796                 $template =& $manager->getTemplate($template);
797                 
798                 //: Change: Set nocatselected variable
799                 if ( $this->getSelectedCategory() )
800                 {
801                         $nocatselected = 'no';
802                 }
803                 else
804                 {
805                         $nocatselected = 'yes';
806                 } 
807                 
808                 echo TEMPLATE::fill((isset($template['CATLIST_HEADER']) ? $template['CATLIST_HEADER'] : null),
809                         array(
810                                 'blogid' => $this->getID(),
811                                 'blogurl' => $blogurl,
812                                 'self' => $CONF['Self'],
813                                 //: Change: Set catiscurrent template variable for header
814                                 'catiscurrent' => $nocatselected,
815                                 'currentcat' => $nocatselected 
816                         ));
817                 
818                 $query = 'SELECT catid, cdesc as catdesc, cname as catname FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' ORDER BY cname ASC';
819                 $res = sql_query($query);
820                 
821                 while ( $data = sql_fetch_assoc($res) )
822                 {
823                         $data['blogid'] = $this->getID();
824                         $data['blogurl'] = $blogurl;
825                         $data['catlink'] = LINK::create_link(
826                                 'category',
827                                 array(
828                                         'catid' => $data['catid'],
829                                         'name' => $data['catname'],
830                                         'extra' => $linkparams
831                                 ));
832                         $data['self'] = $CONF['Self'];
833                         
834                         //catiscurrent
835                         //: Change: Bugfix for catiscurrent logic so it gives catiscurrent = no when no category is selected.
836                         $data['catiscurrent'] = 'no';
837                         $data['currentcat'] = 'no'; 
838                         if ( $this->getSelectedCategory() )
839                         {
840                                 if ( $this->getSelectedCategory() == $data['catid'] )
841                                 {
842                                         $data['catiscurrent'] = 'yes';
843                                         $data['currentcat'] = 'yes';
844                                 }
845                         }
846                         else
847                         {
848                                 global $itemid;
849                                 if ( intval($itemid) && $manager->existsItem(intval($itemid),0,0) )
850                                 {
851                                         $iobj =& $manager->getItem(intval($itemid),0,0);
852                                         $cid = $iobj['catid'];
853                                         if ( $cid == $data['catid'] )
854                                         {
855                                                 $data['catiscurrent'] = 'yes';
856                                                 $data['currentcat'] = 'yes';
857                                         }
858                                 }
859                         }
860                         
861                         $manager->notify(
862                                 'PreCategoryListItem',
863                                 array(
864                                         'listitem' => &$data
865                                 )
866                         );
867                         
868                         echo TEMPLATE::fill((isset($template['CATLIST_LISTITEM']) ? $template['CATLIST_LISTITEM'] : null), $data);
869                 }
870                 
871                 sql_free_result($res);
872                 
873                 echo TEMPLATE::fill((isset($template['CATLIST_FOOTER']) ? $template['CATLIST_FOOTER'] : null),
874                         array(
875                                 'blogid' => $this->getID(),
876                                 'blogurl' => $blogurl,
877                                 'self' => $CONF['Self'],
878                                 //: Change: Set catiscurrent template variable for footer
879                                 'catiscurrent' => $nocatselected,
880                                 'currentcat' => $nocatselected  
881                         ));
882                 return;
883         }
884         
885         /**
886          * BLOG::showBlogList()
887          * Shows a list of all blogs in the system using a given template
888          * ordered by number, name, shortname or description
889          * in ascending or descending order
890          * 
891          * @param       String  $template       tempalte name
892          * @param       String  $bnametype      bname/bshortname
893          * @param       String  $orderby        string for 'ORDER BY' SQL
894          * @param       String  $direction      ASC/DESC
895          * @return      Void
896          */
897         function showBlogList($template, $bnametype, $orderby, $direction)
898         {
899                 global $CONF, $manager;
900                 
901                 switch ( $orderby )
902                 {
903                         case 'number':
904                                 $orderby='bnumber';
905                                 break;
906                         case 'name':
907                                 $orderby='bname';
908                                 break;
909                         case 'shortname':
910                                 $orderby='bshortname';
911                                 break;
912                         case 'description':
913                                 $orderby='bdesc';
914                                 break;
915                         default:
916                                 $orderby='bnumber';
917                                 break;
918                 }
919                 
920                 $direction=strtolower($direction);
921                 switch ( $direction )
922                 {
923                         case 'asc':
924                                 $direction='ASC';
925                                 break;
926                         case 'desc':
927                                 $direction='DESC';
928                                 break;
929                         default:
930                                 $direction='ASC';
931                                 break;
932                 }
933                 
934                 $template =& $manager->getTemplate($template);
935                 
936                 echo TEMPLATE::fill((isset($template['BLOGLIST_HEADER']) ? $template['BLOGLIST_HEADER'] : null),
937                         array(
938                                 'sitename' => $CONF['SiteName'],
939                                 'siteurl' => $CONF['IndexURL']
940                         )
941                 );
942                 
943                 $query = 'SELECT bnumber, bname, bshortname, bdesc, burl FROM '.sql_table('blog').' ORDER BY '.$orderby.' '.$direction;
944                 $res = sql_query($query);
945                 
946                 while ( $data = sql_fetch_assoc($res) )
947                 {
948                         $list = array();
949                         $list['bloglink'] = LINK::create_blogid_link($data['bnumber']);
950                         $list['blogdesc'] = $data['bdesc'];
951                         $list['blogurl'] = $data['burl'];
952                         
953                         if ( $bnametype == 'shortname' )
954                         {
955                                 $list['blogname'] = $data['bshortname'];
956                         }
957                         else
958                         {
959                                 /* all other cases */
960                                 $list['blogname'] = $data['bname'];
961                         }
962                         
963                         $manager->notify(
964                                 'PreBlogListItem',
965                                 array(
966                                         'listitem' => &$list
967                                 )
968                         );
969                         
970                         echo TEMPLATE::fill((isset($template['BLOGLIST_LISTITEM']) ? $template['BLOGLIST_LISTITEM'] : null), $list);
971                 }
972                 
973                 sql_free_result($res);
974                 
975                 echo TEMPLATE::fill((isset($template['BLOGLIST_FOOTER']) ? $template['BLOGLIST_FOOTER'] : null),
976                         array(
977                                 'sitename' => $CONF['SiteName'],
978                                 'siteurl' => $CONF['IndexURL']
979                         )
980                 );
981                 return;
982         }
983         
984         /**
985           * Read the blog settings
986           */
987         function readSettings() {
988                 $query =  'SELECT *'
989                            . ' FROM '.sql_table('blog')
990                            . ' WHERE bnumber=' . $this->blogid;
991                 $res = sql_query($query);
992
993                 $this->isValid = (sql_num_rows($res) > 0);
994                 if (!$this->isValid)
995                         return;
996
997                 $this->settings = sql_fetch_assoc($res);
998         }
999
1000         /**
1001           * Write the blog settings
1002           */
1003         function writeSettings() {
1004
1005                 // (can't use floatval since not available prior to PHP 4.2)
1006                 $offset = $this->getTimeOffset();
1007                 if (!is_float($offset))
1008                         $offset = intval($offset);
1009
1010                 $query =  'UPDATE '.sql_table('blog')
1011                            . " SET bname='" . sql_real_escape_string($this->getName()) . "',"
1012                            . "     bshortname='". sql_real_escape_string($this->getShortName()) . "',"
1013                            . "     bcomments=". intval($this->commentsEnabled()) . ","
1014                            . "     bmaxcomments=" . intval($this->getMaxComments()) . ","
1015                            . "     btimeoffset=" . $offset . ","
1016                            . "     bpublic=" . intval($this->isPublic()) . ","
1017                            . "     breqemail=" . intval($this->emailRequired()) . ","
1018                            . "     bconvertbreaks=" . intval($this->convertBreaks()) . ","
1019                            . "     ballowpast=" . intval($this->allowPastPosting()) . ","
1020                            . "     bnotify='" . sql_real_escape_string($this->getNotifyAddress()) . "',"
1021                            . "     bnotifytype=" . intval($this->getNotifyType()) . ","
1022                            . "     burl='" . sql_real_escape_string($this->getURL()) . "',"
1023                            . "     bupdate='" . sql_real_escape_string($this->getUpdateFile()) . "',"
1024                            . "     bdesc='" . sql_real_escape_string($this->getDescription()) . "',"
1025                            . "     bdefcat=" . intval($this->getDefaultCategory()) . ","
1026                            . "     bdefskin=" . intval($this->getDefaultSkin()) . ","
1027                            . "     bincludesearch=" . intval($this->getSearchable())
1028                            . " WHERE bnumber=" . intval($this->getID());
1029                 sql_query($query);
1030
1031         }
1032
1033         /**
1034           * Update the update file if requested
1035           */    
1036         function updateUpdatefile() {
1037                  if ($this->getUpdateFile()) {
1038                         $f_update = fopen($this->getUpdateFile(),'w');
1039                         fputs($f_update,$this->getCorrectTime());
1040                         fclose($f_update);
1041                  }
1042
1043         }
1044
1045         /**
1046           * Check if a category with a given catid is valid
1047           * 
1048           * @param $catid
1049           *     category id
1050           */
1051         function isValidCategory($catid) {
1052                 $query = 'SELECT * FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' and catid=' . intval($catid);
1053                 $res = sql_query($query);
1054                 return (sql_num_rows($res) != 0);
1055         }
1056
1057         /**
1058           * Get the category name for a given catid
1059           * 
1060           * @param $catid
1061           *     category id
1062           */
1063         function getCategoryName($catid) {
1064                 $res = sql_query('SELECT cname FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));
1065                 $o = sql_fetch_object($res);
1066                 return $o->cname;
1067         }
1068
1069         /**
1070           * Get the category description for a given catid
1071           * 
1072           * @param $catid
1073           *     category id
1074           */
1075         function getCategoryDesc($catid) {
1076                 $res = sql_query('SELECT cdesc FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));
1077                 $o = sql_fetch_object($res);
1078                 return $o->cdesc;
1079         }
1080
1081         /**
1082           * Get the category id for a given category name
1083           * 
1084           * @param $name
1085           *     category name
1086           */
1087         function getCategoryIdFromName($name) {
1088                 $res = sql_query('SELECT catid FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and cname="' . sql_real_escape_string($name) . '"');
1089                 if (sql_num_rows($res) > 0) {
1090                         $o = sql_fetch_object($res);
1091                         return $o->catid;
1092                 } else {
1093                         return $this->getDefaultCategory();
1094                 }
1095         }
1096
1097         /**
1098           * Get the the setting for the line break handling
1099           * [should be named as getConvertBreaks()]
1100           */
1101         function convertBreaks() {
1102                 return $this->getSetting('bconvertbreaks');
1103         }
1104         
1105         /**
1106           * Set the the setting for the line break handling
1107           * 
1108           * @param $val
1109           *     new value for bconvertbreaks
1110           */
1111         function setConvertBreaks($val) {
1112                 $this->setSetting('bconvertbreaks',$val);
1113         }
1114
1115         /**
1116           * Insert a javascript that includes information about the settings
1117           * of an author:  ConvertBreaks, MediaUrl and AuthorId
1118           * 
1119           * @param $authorid
1120           *     id of the author
1121           */    
1122         function insertJavaScriptInfo($authorid = '') {
1123                 global $member, $CONF;
1124
1125                 if ($authorid == '')
1126                         $authorid = $member->getID();
1127
1128                 ?>
1129                 <script type="text/javascript">
1130                         setConvertBreaks(<?php echo  $this->convertBreaks() ? 'true' : 'false' ?>);
1131                         setMediaUrl("<?php echo $CONF['MediaURL']?>");
1132                         setAuthorId(<?php echo $authorid?>);
1133                 </script><?php  
1134         }
1135
1136         /**
1137           * Set the the setting for allowing to publish postings in the past
1138           * 
1139           * @param $val
1140           *     new value for ballowpast
1141           */
1142         function setAllowPastPosting($val) {
1143                 $this->setSetting('ballowpast',$val);
1144         }
1145         
1146         /**
1147           * Get the the setting if it is allowed to publish postings in the past
1148           * [should be named as getAllowPastPosting()]
1149           */
1150         function allowPastPosting() {
1151                 return $this->getSetting('ballowpast');
1152         }
1153
1154         function getCorrectTime($t=0) {
1155                 if ($t == 0) $t = time();
1156                 return ($t + 3600 * $this->getTimeOffset());
1157         }
1158
1159         function getName() {
1160                 return $this->getSetting('bname');
1161         }
1162
1163         function getShortName() {
1164                 return $this->getSetting('bshortname');
1165         }
1166
1167         function getMaxComments() {
1168                 return $this->getSetting('bmaxcomments');
1169         }
1170
1171         function getNotifyAddress() {
1172                 return $this->getSetting('bnotify');
1173         }
1174
1175         function getNotifyType() {
1176                 return $this->getSetting('bnotifytype');
1177         }
1178
1179         function notifyOnComment() {
1180                 $n = $this->getNotifyType();
1181                 return (($n != 0) && (($n % 3) == 0));
1182         }
1183
1184         function notifyOnVote() {
1185                 $n = $this->getNotifyType();
1186                 return (($n != 0) && (($n % 5) == 0));
1187         }
1188
1189         function notifyOnNewItem() {
1190                 $n = $this->getNotifyType();
1191                 return (($n != 0) && (($n % 7) == 0));
1192         }
1193
1194         function setNotifyType($val) {
1195                 $this->setSetting('bnotifytype',$val);
1196         }
1197
1198
1199         function getTimeOffset() {
1200                 return $this->getSetting('btimeoffset');
1201         }
1202
1203         function commentsEnabled() {
1204                 return $this->getSetting('bcomments');
1205         }
1206
1207         function getURL() {
1208                 return $this->getSetting('burl');
1209         }
1210
1211         function getDefaultSkin() {
1212                 return $this->getSetting('bdefskin');
1213         }
1214
1215         function getUpdateFile() {
1216                 return $this->getSetting('bupdate');
1217         }
1218
1219         function getDescription() {
1220                 return $this->getSetting('bdesc');
1221         }
1222
1223         function isPublic() {
1224                 return $this->getSetting('bpublic');
1225         }
1226
1227         function emailRequired() {
1228                 return $this->getSetting('breqemail');
1229         }
1230
1231         function getSearchable() {
1232                 return $this->getSetting('bincludesearch');
1233         }
1234
1235         function getDefaultCategory() {
1236                 return $this->getSetting('bdefcat');
1237         }
1238
1239         function setPublic($val) {
1240                 $this->setSetting('bpublic',$val);
1241         }
1242
1243         function setSearchable($val) {
1244                 $this->setSetting('bincludesearch',$val);
1245         }
1246
1247         function setDescription($val) {
1248                 $this->setSetting('bdesc',$val);
1249         }
1250
1251         function setUpdateFile($val) {
1252                 $this->setSetting('bupdate',$val);
1253         }
1254
1255         function setDefaultSkin($val) {
1256                 $this->setSetting('bdefskin',$val);
1257         }
1258
1259         function setURL($val) {
1260                 $this->setSetting('burl',$val);
1261         }
1262
1263         function setName($val) {
1264                 $this->setSetting('bname',$val);
1265         }
1266
1267         function setShortName($val) {
1268                 $this->setSetting('bshortname',$val);
1269         }
1270
1271         function setCommentsEnabled($val) {
1272                 $this->setSetting('bcomments',$val);
1273         }
1274
1275         function setMaxComments($val) {
1276                 $this->setSetting('bmaxcomments',$val);
1277         }
1278
1279         function setNotifyAddress($val) {
1280                 $this->setSetting('bnotify',$val);
1281         }
1282
1283         function setEmailRequired($val) {
1284                 $this->setSetting('breqemail',$val);
1285         }
1286
1287         function setTimeOffset($val) {
1288                 // check validity of value
1289                 // 1. replace , by . (common mistake)
1290                 $val = str_replace(',','.',$val);
1291                 // 2. cast to float or int
1292                 if (is_numeric($val) && strstr($val,'.5')) {
1293                         $val = (float) $val;
1294                 } else {
1295                         $val = intval($val);
1296                 }
1297
1298                 $this->setSetting('btimeoffset',$val);
1299         }
1300
1301         function setDefaultCategory($val) {
1302                 $this->setSetting('bdefcat',$val);
1303         }
1304
1305         function getSetting($key) {
1306                 return $this->settings[$key];
1307         }
1308
1309         function setSetting($key,$value) {
1310                 $this->settings[$key] = $value;
1311         }
1312
1313         /**
1314          * BLOG::addTeamMember()
1315          * Tries to add a member to the team. 
1316          * Returns false if the member was already on the team
1317          * 
1318          * @param       Integer $memberid       id for member
1319          * @param       Boolean $admin  super-admin or not
1320          * @return      Boolean Success/Fail
1321          */
1322         function addTeamMember($memberid, $admin)
1323         {
1324                 global $manager;
1325                 
1326                 $memberid = intval($memberid);
1327                 $admin = intval($admin);
1328                 
1329                 // check if member is already a member
1330                 $tmem = MEMBER::createFromID($memberid);
1331                 
1332                 if ( $tmem->isTeamMember($this->getID()) )
1333                 {
1334                         return 0;
1335                 }
1336                 
1337                 $manager->notify(
1338                         'PreAddTeamMember',
1339                         array(
1340                                 'blog' => &$this,
1341                                 'member' => &$tmem,
1342                                 'admin' => &$admin
1343                         )
1344                 );
1345                 
1346                 // add to team
1347                 $query = "INSERT INTO %s (TMEMBER, TBLOG, TADMIN) ' . 'VALUES (%d, %d, %d)";
1348                 $query = sprintf($query, sql_table('team'), $memberid, $this->getID(), $admin);
1349                 sql_query($query);
1350
1351                 $manager->notify(
1352                         'PostAddTeamMember',
1353                         array(
1354                                 'blog' => &$this,
1355                                 'member' => &$tmem,
1356                                 'admin' => $admin
1357                         )
1358                 );
1359                 
1360                 $logMsg = sprintf(_TEAM_ADD_NEWTEAMMEMBER, $tmem->getDisplayName(), $memberid, $this->getName());
1361                 ACTIONLOG::add(INFO, $logMsg);
1362                 
1363                 return 1;
1364         }
1365
1366         function getID() {
1367                 return (integer) $this->blogid;
1368         }
1369
1370         /**
1371           * Checks if a blog with a given shortname exists 
1372           * Returns true if there is a blog with the given shortname (static)
1373           * 
1374           * @param $name
1375           *     blog shortname
1376           */
1377         function exists($name) {
1378                 $r = sql_query('select * FROM '.sql_table('blog').' WHERE bshortname="'.sql_real_escape_string($name).'"');
1379                 return (sql_num_rows($r) != 0);
1380         }
1381
1382         /**
1383           * Checks if a blog with a given id exists 
1384           * Returns true if there is a blog with the given ID (static)
1385           * 
1386           * @param $id
1387           *     blog id
1388           */
1389         function existsID($id) {
1390                 $r = sql_query('select * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));
1391                 return (sql_num_rows($r) != 0);
1392         }
1393
1394         /**
1395           * flag there is a future post pending 
1396           */
1397         function setFuturePost() {
1398                 $query =  'UPDATE '.sql_table('blog')
1399                             . " SET bfuturepost='1' WHERE bnumber=" . $this->getID();
1400                 sql_query($query);
1401         }
1402
1403         /**
1404           * clear there is a future post pending 
1405           */
1406         function clearFuturePost() {
1407                 $query =  'UPDATE '.sql_table('blog')
1408                            . " SET bfuturepost='0' WHERE bnumber=" . $this->getID();
1409                 sql_query($query);
1410         }
1411
1412         /**
1413           * check if we should throw justPosted event 
1414           */
1415         function checkJustPosted() {
1416                 global $manager;
1417
1418                 if ($this->settings['bfuturepost'] == 1) {
1419                         $blogid = $this->getID();
1420                         $result = sql_query("SELECT * FROM " . sql_table('item')
1421                                   . " WHERE iposted=0 AND iblog=" . $blogid . " AND itime<NOW()");
1422                         if (sql_num_rows($result) > 0) {
1423                                 // This $pinged is allow a plugin to tell other hook to the event that a ping is sent already
1424                                 // Note that the plugins's calling order is subject to thri order in the plugin list
1425                                 $pinged = false;
1426                                 $manager->notify(
1427                                                 'JustPosted',
1428                                                 array('blogid' => $blogid,
1429                                                 'pinged' => &$pinged
1430                                                 )
1431                                 );
1432
1433                                 // clear all expired future posts
1434                                 sql_query("UPDATE " . sql_table('item') . " SET iposted='1' WHERE iblog=" . $blogid . " AND itime<NOW()");
1435
1436                                 // check to see any pending future post, clear the flag is none
1437                                 $result = sql_query("SELECT * FROM " . sql_table('item')
1438                                           . " WHERE iposted=0 AND iblog=" . $blogid);
1439                                 if (sql_num_rows($result) == 0) {
1440                                         $this->clearFuturePost();
1441                                 }
1442                         }
1443                 }
1444         }
1445
1446         /**
1447          * Shows the given list of items for this blog
1448          *
1449          * @param $itemarray
1450          *              array of item numbers to be displayed
1451          * @param $template
1452          *              String representing the template _NAME_ (!)
1453          * @param $highlight
1454          *              contains a query that should be highlighted
1455          * @param $comments
1456          *              1=show comments 0=don't show comments
1457          * @param $dateheads
1458          *              1=show dateheads 0=don't show dateheads
1459          * @param $showDrafts
1460          *              0=do not show drafts 1=show drafts
1461          * @param $showFuture
1462          *              0=do not show future posts 1=show future posts
1463          * @returns int
1464          *              amount of items shown
1465          */
1466         function readLogFromList($itemarray, $template, $highlight = '', $comments = 1, $dateheads = 1,$showDrafts = 0, $showFuture = 0) {
1467
1468                 $query = $this->getSqlItemList($itemarray,$showDrafts,$showFuture);
1469
1470                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
1471         }
1472
1473         /**
1474          * BLOG::getSqlItemList()
1475          * Returns the SQL query used to fill out templates for a list of items
1476          * No LIMIT clause is added. (caller should add this if multiple pages are requested)
1477          *
1478          * @param       array   $itemarray              an array holding the item numbers of the items to be displayed
1479          * @param integer       $showDrafts     0=do not show drafts 1=show drafts
1480          * @param integer       $showFuture     0=do not show future posts 1=show future posts
1481          * @return      string  either a full SQL query, or an empty string
1482          * 
1483          */
1484         function getSqlItemList($itemarray,$showDrafts = 0,$showFuture = 0)
1485         {
1486                 if ( !is_array($itemarray) )
1487                 {
1488                         return '';
1489                 }
1490                 
1491                 $showDrafts = intval($showDrafts);
1492                 $showFuture = intval($showFuture);
1493                 $items = array();
1494                 
1495                 foreach ( $itemarray as $value )
1496                 {
1497                         if ( intval($value) )
1498                         {
1499                                 $items[] = intval($value);
1500                         }
1501                 }
1502                 if ( !count($items) )
1503                 {
1504                         return '';
1505                 }
1506                 
1507                 $i = count($items);
1508                 $query = '';
1509                 foreach ( $items as $value )
1510                 {
1511                         $query .= '('
1512                                         .       'SELECT'
1513                                         .       ' i.inumber as itemid,'
1514                                         .       ' i.ititle as title,'
1515                                         .       ' i.ibody as body,'
1516                                         .       ' m.mname as author,'
1517                                         .       ' m.mrealname as authorname,'
1518                                         .       ' i.itime,'
1519                                         .       ' i.imore as more,'
1520                                         .       ' m.mnumber as authorid,'
1521                                         .       ' m.memail as authormail,'
1522                                         .       ' m.murl as authorurl,'
1523                                         .       ' c.cname as category,'
1524                                         .       ' i.icat as catid,'
1525                                         .       ' i.iclosed as closed';
1526                         
1527                         $query .= ' FROM '
1528                                         . sql_table('item') . ' as i, '
1529                                         . sql_table('member') . ' as m, '
1530                                         . sql_table('category') . ' as c'
1531                                         . ' WHERE'
1532                                     .    ' i.iblog='.$this->blogid
1533                                    . ' and i.iauthor=m.mnumber'
1534                                    . ' and i.icat=c.catid';
1535                         
1536                         // exclude drafts       
1537                         if ( !$showDrafts )
1538                         {
1539                                 $query .= ' and i.idraft=0';
1540                         }
1541                         if ( !$showFuture )
1542                         {
1543                                 // don't show future items
1544                                 $query .= ' and i.itime<="' . i18n::formatted_datetime('mysql', $this->getCorrectTime()) . '"';
1545                         }
1546                         
1547                         $query .= ' and i.inumber='.intval($value);
1548                         $query .= ')';
1549                         $i--;
1550                         if ($i) $query .= ' UNION ';
1551                 }
1552                 
1553                 return $query;
1554         }
1555 }