OSDN Git Service

add show 'BLOGLIST_HEADER' and 'BLOGLIST_FOOTER'
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / BLOG.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2007 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-2007 The Nucleus Group
19  * @version $Id: BLOG.php,v 1.9 2007-02-17 04:39:29 shizuki Exp $
20  * $NucleusJP: BLOG.php,v 1.8 2007/02/07 12:32:25 shizuki Exp $
21  */
22
23 // temporary: dirt way to separe class ITEMACTIONS from BLOG
24 require_once $DIR_LIBS . 'ITEMACTIONS.php';
25
26 class BLOG {
27
28         // blog id
29         var $blogid;
30
31         // ID of currently selected category
32         var $selectedcatid;
33
34         // After creating an object of the blog class, contains true if the BLOG object is
35         // valid (the blog exists)
36         var $isValid;
37
38         // associative array, containing all blogsettings (use the get/set functions instead)
39         var $settings;
40
41         /**
42          * Creates a new BLOG object for the given blog
43          *
44          * @param $id blogid
45          */
46         function BLOG($id) {
47                 $this->blogid = intval($id);
48                 $this->readSettings();
49
50                 // try to set catid
51                 // (the parse functions in SKIN.php will override this, so it's mainly useless)
52                 global $catid;
53                 $this->setSelectedCategory($catid);
54         }
55
56         /**
57          * Shows the given amount of items for this blog
58          *
59          * @param $template
60          *              String representing the template _NAME_ (!)
61          * @param $amountEntries
62          *              amount of entries to show
63          * @param $startpos
64          *              offset from where items should be shown (e.g. 5 = start at fifth item)
65          * @returns int
66          *              amount of items shown
67          */
68         function readLog($template, $amountEntries, $offset = 0, $startpos = 0) {
69                 return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);
70         }
71
72         /**
73          * Shows an archive for a given month
74          *
75          * @param $year
76          *              year
77          * @param $month
78          *              month
79          * @param $template
80          *              String representing the template name to be used
81          */
82         function showArchive($templatename, $year, $month, $day=0) {
83
84                 // create extra where clause for select query
85                 if ($day == 0) {
86                         $timestamp_start = mktime(0,0,0,$month,1,$year);
87                         $timestamp_end = mktime(0,0,0,$month+1,1,$year);  // also works when $month==12
88                 } else {
89                         $timestamp_start = mktime(0,0,0,$month,$day,$year);
90                         $timestamp_end = mktime(0,0,0,$month,$day+1,$year);
91                 }
92                 $extra_query = ' and i.itime>=' . mysqldate($timestamp_start)
93                                          . ' and i.itime<' . mysqldate($timestamp_end);
94
95
96                 $this->readLogAmount($templatename,0,$extra_query,'',1,1);
97
98         }
99
100
101         // sets/gets current category (only when category exists)
102         function setSelectedCategory($catid) {
103                 if ($this->isValidCategory($catid) || (intval($catid) == 0))
104                         $this->selectedcatid = intval($catid);
105         }
106
107         function setSelectedCategoryByName($catname) {
108                 $this->setSelectedCategory($this->getCategoryIdFromName($catname));
109         }
110
111         function getSelectedCategory() {
112                 return $this->selectedcatid;
113         }
114
115         /**
116          * Shows the given amount of items for this blog
117          *
118          * @param $template
119          *              String representing the template _NAME_ (!)
120          * @param $amountEntries
121          *              amount of entries to show (0 = no limit)
122          * @param $extraQuery
123          *              extra conditions to be added to the query
124          * @param $highlight
125          *              contains a query that should be highlighted
126          * @param $comments
127          *              1=show comments 0=don't show comments
128          * @param $dateheads
129          *              1=show dateheads 0=don't show dateheads
130          * @param $offset
131          *              offset
132          * @returns int
133          *              amount of items shown
134          */
135         function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0) {
136
137                 $query = $this->getSqlBlog($extraQuery);
138
139                 if ($amountEntries > 0) {
140                                 // $offset zou moeten worden:
141                                 // (($startpos / $amountentries) + 1) * $offset ... later testen ...
142                            $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);
143                 }
144                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
145         }
146
147         function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1) {
148                 global $CONF, $manager;
149
150                 $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');
151                 if ($lastVisit != 0)
152                         $lastVisit = $this->getCorrectTime($lastVisit);
153
154                 // set templatename as global variable (so plugins can access it)
155                 global $currentTemplateName;
156                 $currentTemplateName = $templateName;
157
158                 $template =& $manager->getTemplate($templateName);
159
160                 // create parser object & action handler
161                 $actions =& new ITEMACTIONS($this);
162                 $parser =& new PARSER($actions->getDefinedActions(),$actions);
163                 $actions->setTemplate($template);
164                 $actions->setHighlight($highlight);
165                 $actions->setLastVisit($lastVisit);
166                 $actions->setParser($parser);
167                 $actions->setShowComments($comments);
168
169                 // execute query
170                 $items = sql_query($query);
171
172                 // loop over all items
173                 while ($item = mysql_fetch_object($items)) {
174
175                         $item->timestamp = strtotime($item->itime);     // string timestamp -> unix timestamp
176
177                         // action handler needs to know the item we're handling
178                         $actions->setCurrentItem($item);
179
180                         // add date header if needed
181                         $old_date = 0;
182                         if ($dateheads) {
183                                 $new_date = date('dFY',$item->timestamp);
184                                 if ($new_date != $old_date) {
185                                         // unless this is the first time, write date footer
186                                         $timestamp = $item->timestamp;
187                                         if ($old_date != 0) {
188                                                 $oldTS = strtotime($old_date);
189                                                 $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));
190                                                 $tmp_footer = strftime($template['DATE_FOOTER'], $oldTS);
191                                                 $parser->parse($tmp_footer);
192                                                 $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));
193                                         }
194                                         $manager->notify('PreDateHead',array('blog' => &$this, 'timestamp' => $timestamp));
195                                         // note, to use templatvars in the dateheader, the %-characters need to be doubled in
196                                         // order to be preserved by strftime
197                                         $tmp_header = strftime((isset($template['DATE_HEADER']) ? $template['DATE_HEADER'] : null), $timestamp);
198                                         $parser->parse($tmp_header);
199                                         $manager->notify('PostDateHead',array('blog' => &$this, 'timestamp' => $timestamp));
200                                 }
201                                 $old_date = $new_date;
202                         }
203
204                         // parse item
205                         $parser->parse($template['ITEM_HEADER']);
206                         $manager->notify('PreItem', array('blog' => &$this, 'item' => &$item));
207                         $parser->parse($template['ITEM']);
208                         $manager->notify('PostItem', array('blog' => &$this, 'item' => &$item));
209                         $parser->parse($template['ITEM_FOOTER']);
210
211                 }
212
213                 $numrows = mysql_num_rows($items);
214
215                 // add another date footer if there was at least one item
216                 if (($numrows > 0) && $dateheads) {
217                         $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));
218                         $parser->parse($template['DATE_FOOTER']);
219                         $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));
220                 }
221
222                 mysql_free_result($items);      // free memory
223
224                 return $numrows;
225
226         }
227
228         function showOneitem($itemid, $template, $highlight) {
229                 $extraQuery = ' and inumber=' . intval($itemid);
230
231                 return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);
232         }
233
234
235         /**
236           * Adds an item to this blog
237           */
238         function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft) {
239                 global $manager;
240
241                 $blogid         = intval($blogid);
242                 $authorid       = intval($authorid);
243                 $title          = $title;
244                 $body           = $body;
245                 $more           = $more;
246                 $catid          = intval($catid);
247
248                 // convert newlines to <br />
249                 if ($this->convertBreaks()) {
250                         $body = addBreaks($body);
251                         $more = addBreaks($more);
252                 }
253
254                 if ($closed != '1')     $closed = '0';
255                 if ($draft != '0') $draft = '1';
256
257                 if (!$this->isValidCategory($catid))
258                         $catid = $this->getDefaultCategory();
259
260                 if ($timestamp > $this->getCorrectTime())
261                         $isFuture = 1;
262
263                 $timestamp = date('Y-m-d H:i:s',$timestamp);
264
265                 $manager->notify('PreAddItem',array('title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$this, 'authorid' => &$authorid, 'timestamp' => &$timestamp, 'closed' => &$closed, 'draft' => &$draft, 'catid' => &$catid));
266
267                 $title = addslashes($title);
268                 $body = addslashes($body);
269                 $more = addslashes($more);
270
271                 $query = 'INSERT INTO '.sql_table('item').' (ITITLE, IBODY, IMORE, IBLOG, IAUTHOR, ITIME, ICLOSED, IDRAFT, ICAT) '
272                            . "VALUES ('$title', '$body', '$more', $blogid, $authorid, '$timestamp', $closed, $draft, $catid)";
273                 sql_query($query);
274                 $itemid = mysql_insert_id();
275
276                 $manager->notify('PostAddItem',array('itemid' => $itemid));
277
278                 if (!$draft)
279                         $this->updateUpdateFile();
280
281                 // send notification mail
282                 if (!$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem())
283                         $this->sendNewItemNotification($itemid, stripslashes($title), stripslashes($body));
284
285                 return $itemid;
286         }
287
288         function sendNewItemNotification($itemid, $title, $body) {
289                 global $CONF, $member;
290
291                 // create text version of html post
292                 $ascii = toAscii($body);
293
294                 $mailto_msg = _NOTIFY_NI_MSG . " \n";
295                 $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $itemid . "\n\n";
296                 $mailto_msg .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";
297                 $mailto_msg .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";
298                 $mailto_msg .= getMailFooter();
299
300                 $mailto_title = $this->getName() . ': ' . _NOTIFY_NI_TITLE;
301
302                 $frommail = $member->getNotifyFromMailAddress();
303
304                 $notify =& new NOTIFICATION($this->getNotifyAddress());
305                 $notify->notify($mailto_title, $mailto_msg , $frommail);
306
307
308
309         }
310
311
312         /**
313           * Creates a new category for this blog
314           *
315           * @param $catName
316           *             name of the new category. When empty, a name is generated automatically
317           *             (starting with newcat)
318           * @param $catDescription
319           *             description of the new category. Defaults to 'New Category'
320           *
321           * @returns
322           *             the new category-id in case of success.
323           *             0 on failure
324           */
325         function createNewCategory($catName = '', $catDescription = 'New category') {
326                 global $member, $manager;
327
328                 if ($member->blogAdminRights($this->getID())) {
329                         // generate
330                         if ($catName == '')
331                         {
332                                 $catName = 'newcat';
333                                 $i = 1;
334
335                                 $res = sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID());
336                                 while (mysql_num_rows($res) > 0)
337                                 {
338                                         $i++;
339                                         $res = sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID());
340                                 }
341
342                                 $catName = $catName . $i;
343                         }
344
345                         $manager->notify(
346                                 'PreAddCategory',
347                                 array(
348                                         'blog' => &$this,
349                                         'name' => &$catName,
350                                         'description' => $catDescription
351                                 )
352                         );
353
354                         $query = 'INSERT INTO '.sql_table('category').' (cblog, cname, cdesc) VALUES (' . $this->getID() . ", '" . addslashes($catName) . "', '" . addslashes($catDescription) . "')";
355                         sql_query($query);
356                         $catid = mysql_insert_id();
357
358                         $manager->notify(
359                                 'PostAddCategory',
360                                 array(
361                                         'blog' => &$this,
362                                         'name' => $catName,
363                                         'description' => $catDescription,
364                                         'catid' => $catid
365                                 )
366                         );
367
368                         return $catid;
369                 } else {
370                         return 0;
371                 }
372
373         }
374
375
376         /**
377          * Searches all months of this blog for the given query
378          *
379          * @param $query
380          *              search query
381          * @param $template
382          *              template to be used (__NAME__ of the template)
383          * @param $amountMonths
384          *              max amount of months to be search (0 = all)
385          * @param $maxresults
386          *              max number of results to show
387          * @param $startpos
388          *              offset
389          * @returns
390          *              amount of hits found
391          */
392         function search($query, $template, $amountMonths, $maxresults, $startpos) {
393                 global $CONF, $manager;
394
395                 $highlight      = '';
396                 $sqlquery       = $this->getSqlSearch($query, $amountMonths, $highlight);
397
398                 if ($sqlquery == '')
399                 {
400                         // no query -> show everything
401                         $extraquery = '';
402                         $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);
403                 } else {
404
405                         // add LIMIT to query (to split search results into pages)
406                         if (intval($maxresults > 0))
407                                 $sqlquery .= ' LIMIT ' . intval($startpos).',' . intval($maxresults);
408
409                         // show results
410                         $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);
411
412                         // when no results were found, show a message
413                         if ($amountfound == 0)
414                         {
415                                 $template =& $manager->getTemplate($template);
416                                 $vars = array(
417                                         'query'         => htmlspecialchars($query),
418                                         'blogid'        => $this->getID()
419                                 );
420                                 echo TEMPLATE::fill($template['SEARCH_NOTHINGFOUND'],$vars);
421                         }
422                 }
423
424                 return $amountfound;
425         }
426
427         /**
428          * Returns an SQL query to use for a search query
429          *
430          * @param $query
431          *              search query
432          * @param $amountMonths
433          *              amount of months to search back. Default = 0 = unlimited
434          * @param $mode
435          *              either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
436          * @returns $highlight
437          *              words to highlight (out parameter)
438          * @returns
439          *              either a full SQL query, or an empty string (if querystring empty)
440          * @note
441          *              No LIMIT clause is added. (caller should add this if multiple pages are requested)
442          */
443         function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')
444         {
445                 $searchclass =& new SEARCH($query);
446
447                 $highlight        = $searchclass->inclusive;
448
449                 // if querystring is empty, return empty string
450                 if ($searchclass->inclusive == '')
451                         return '';
452
453
454                 $where  = $searchclass->boolean_sql_where('ititle,ibody,imore');
455                 $select = $searchclass->boolean_sql_select('ititle,ibody,imore');
456
457                 // get list of blogs to search
458                 $blogs          = $searchclass->blogs;          // array containing blogs that always need to be included
459                 $blogs[]        = $this->getID();                       // also search current blog (duh)
460                 $blogs          = array_unique($blogs);         // remove duplicates
461                 $selectblogs = '';
462                 if (count($blogs) > 0)
463                         $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';
464
465                 if ($mode == '')
466                 {
467                         $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author, m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';
468                         if ($select)
469                                 $query .= ', '.$select. ' as score ';
470                 } else {
471                         $query = 'SELECT COUNT(*) as result ';
472                 }
473
474                 $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
475                            . ' WHERE i.iauthor=m.mnumber'
476                            . ' and i.icat=c.catid'
477                            . ' and i.idraft=0'  // exclude drafts
478                            . $selectblogs
479                                         // don't show future items
480                            . ' and i.itime<=' . mysqldate($this->getCorrectTime())
481                            . ' and '.$where;
482
483                 // take into account amount of months to search
484                 if ($amountMonths > 0)
485                 {
486                         $localtime = getdate($this->getCorrectTime());
487                         $timestamp_start = mktime(0,0,0,$localtime['mon'] - $amountMonths,1,$localtime['year']);
488                         $query .= ' and i.itime>' . mysqldate($timestamp_start);
489                 }
490
491                 if ($mode == '')
492                 {
493                         if ($select)
494                                 $query .= ' ORDER BY score DESC';
495                         else
496                                 $query .= ' ORDER BY i.itime DESC ';
497                 }
498
499                 return $query;
500         }
501
502         /**
503          * Returns the SQL query that's normally used to display the blog items on the index type skins
504          *
505          * @param $mode
506          *              either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
507          * @returns
508          *              either a full SQL query, or an empty string
509          * @note
510          *              No LIMIT clause is added. (caller should add this if multiple pages are requested)
511          */
512         function getSqlBlog($extraQuery, $mode = '')
513         {
514                 if ($mode == '')
515                         $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author, m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';
516                 else
517                         $query = 'SELECT COUNT(*) as result ';
518
519                 $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
520                            . ' WHERE i.iblog='.$this->blogid
521                            . ' and i.iauthor=m.mnumber'
522                            . ' and i.icat=c.catid'
523                            . ' and i.idraft=0'  // exclude drafts
524                                         // don't show future items
525                            . ' and i.itime<=' . mysqldate($this->getCorrectTime());
526
527                 if ($this->getSelectedCategory())
528                         $query .= ' and i.icat=' . $this->getSelectedCategory() . ' ';
529
530
531                 $query .= $extraQuery;
532
533                 if ($mode == '')
534                         $query .= ' ORDER BY i.itime DESC';
535
536                 return $query;
537         }
538
539         /**
540           * Shows the archivelist using the given template
541           */
542         function showArchiveList($template, $mode = 'month', $limit = 0) {
543                 global $CONF, $catid, $manager;
544
545                 if ($catid)
546                         $linkparams = array('catid' => $catid);
547
548                 $template =& $manager->getTemplate($template);
549                 $data['blogid'] = $this->getID();
550
551                 echo TEMPLATE::fill($template['ARCHIVELIST_HEADER'],$data);
552
553                 $query = 'SELECT itime, SUBSTRING(itime,1,4) AS Year, SUBSTRING(itime,6,2) AS Month, SUBSTRING(itime,9,2) as Day FROM '.sql_table('item')
554                 . ' WHERE iblog=' . $this->getID()
555                 . ' and itime <=' . mysqldate($this->getCorrectTime())  // don't show future items!
556                 . ' and idraft=0'; // don't show draft items
557
558                 if ($catid)
559                         $query .= ' and icat=' . intval($catid);
560
561                 $query .= ' GROUP BY Year, Month';
562                 if ($mode == 'day')
563                         $query .= ', Day';
564
565
566                 $query .= ' ORDER BY itime DESC';
567
568                 if ($limit > 0)
569                         $query .= ' LIMIT ' . intval($limit);
570
571                 $res = sql_query($query);
572
573                 while ($current = mysql_fetch_object($res)) {
574                         $current->itime = strtotime($current->itime);   // string time -> unix timestamp
575
576                         if ($mode == 'day') {
577                                 $archivedate = date('Y-m-d',$current->itime);
578                                 $archive['day'] = date('d',$current->itime);
579                         } else {
580                                 $archivedate = date('Y-m',$current->itime);
581                         }
582                         $data['month'] = date('m',$current->itime);
583                         $data['year'] = date('Y',$current->itime);
584                         $data['archivelink'] = createArchiveLink($this->getID(),$archivedate,$linkparams);
585
586                         $temp = TEMPLATE::fill($template['ARCHIVELIST_LISTITEM'],$data);
587                         echo strftime($temp,$current->itime);
588
589                 }
590
591                 mysql_free_result($res);
592
593                 echo TEMPLATE::fill($template['ARCHIVELIST_FOOTER'],$data);
594         }
595
596
597         /**
598           * Shows the list of categories using a given template
599           */
600         function showCategoryList($template) {
601                 global $CONF, $manager;
602
603                 // determine arguments next to catids
604                 // I guess this can be done in a better way, but it works
605                 global $archive, $archivelist;
606
607                 $linkparams = array();
608                 if ($archive) {
609                         $blogurl = createArchiveLink($this->getID(), $archive, '');
610                         $linkparams['blogid'] = $this->getID();
611                         $linkparams['archive'] = $archive;
612                 } else if ($archivelist) {
613                         $blogurl = createArchiveListLink($this->getID(), '');
614                         $linkparams['archivelist'] = $archivelist;
615                 } else {
616                         $blogurl = createBlogidLink($this->getID(), '');
617                         $linkparams['blogid'] = $this->getID();
618                 }
619
620                 //$blogurl = $this->getURL() . $qargs;
621                 //$blogurl = createBlogLink($this->getURL(), $linkparams);
622
623                 $template =& $manager->getTemplate($template);
624
625                 echo TEMPLATE::fill((isset($template['CATLIST_HEADER']) ? $template['CATLIST_HEADER'] : null),
626                                                         array(
627                                                                 'blogid' => $this->getID(),
628                                                                 'blogurl' => $blogurl,
629                                                                 'self' => $CONF['Self']
630                                                         ));
631
632                 $query = 'SELECT catid, cdesc as catdesc, cname as catname FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' ORDER BY cname ASC';
633                 $res = sql_query($query);
634
635
636                 while ($data = mysql_fetch_assoc($res)) {
637                         $data['blogid'] = $this->getID();
638                         $data['blogurl'] = $blogurl;
639                         $data['catlink'] = createLink(
640                                                                 'category',
641                                                                 array(
642                                                                         'catid' => $data['catid'],
643                                                                         'name' => $data['catname'],
644                                                                         'extra' => $linkparams
645                                                                 )
646                                                            );
647                         $data['self'] = $CONF['Self'];
648
649                         echo TEMPLATE::fill((isset($template['CATLIST_LISTITEM']) ? $template['CATLIST_LISTITEM'] : null), $data);
650                         //$temp = TEMPLATE::fill((isset($template['CATLIST_LISTITEM']) ? $template['CATLIST_LISTITEM'] : null), $data);
651                         //echo strftime($temp, $current->itime);
652
653                 }
654
655                 mysql_free_result($res);
656
657                 echo TEMPLATE::fill((isset($template['CATLIST_FOOTER']) ? $template['CATLIST_FOOTER'] : null),
658                                                         array(
659                                                                 'blogid' => $this->getID(),
660                                                                 'blogurl' => $blogurl,
661                                                                 'self' => $CONF['Self']
662                                                         ));
663         }
664         
665         /**
666           * Shows a list of all blogs in the system using a given template
667           */
668         function showBlogList($template, $bnametype) {
669                 global $CONF, $manager;
670                 
671                 $template =& $manager->getTemplate($template);
672                 
673                 echo TEMPLATE::fill((isset($template['BLOGLIST_HEADER']) ? $template['BLOGLIST_HEADER'] : null),
674                                                         array(
675                                                                 'sitename' => $CONF['SiteName'],
676                                                                 'siteurl' => $CONF['IndexURL']
677                                                         ));
678                 
679                 $query = 'SELECT bnumber, bname, bshortname, bdesc, burl FROM '.sql_table('blog').' ORDER BY bnumber ASC';
680                 $res = sql_query($query);
681                 
682                 while ($data = mysql_fetch_assoc($res)) {
683                 
684                         $list = array();
685                 
686                         $list['bloglink'] = createLink('blog', array('blogid' => $data['bnumber']));
687                 
688                         $list['blogdesc'] = $data['bdesc'];
689                         
690                         if ($bnametype=='shortname') {
691                                 $list['blogname'] = $data['bshortname'];
692                         }
693                         else { // all other cases
694                                 $list['blogname'] = $data['bname'];
695                         }
696                         
697                         echo TEMPLATE::fill((isset($template['BLOGLIST_LISTITEM']) ? $template['BLOGLIST_LISTITEM'] : null), $list);
698                         
699                 }
700                 
701                 mysql_free_result($res);
702                 
703                 echo TEMPLATE::fill((isset($template['BLOGLIST_FOOTER']) ? $template['BLOGLIST_FOOTER'] : null),
704                                                         array(
705                                                                 'sitename' => $CONF['SiteName'],
706                                                                 'siteurl' => $CONF['IndexURL']
707                                                         ));
708
709         }
710
711         /**
712           * Blogsettings functions
713           */
714
715         function readSettings() {
716                 $query =  'SELECT *'
717                            . ' FROM '.sql_table('blog')
718                            . ' WHERE bnumber=' . $this->blogid;
719                 $res = sql_query($query);
720
721                 $this->isValid = (mysql_num_rows($res) > 0);
722                 if (!$this->isValid)
723                         return;
724
725                 $this->settings = mysql_fetch_assoc($res);
726         }
727
728         function writeSettings() {
729
730                 // (can't use floatval since not available prior to PHP 4.2)
731                 $offset = $this->getTimeOffset();
732                 if (!is_float($offset))
733                         $offset = intval($offset);
734
735                 $query =  'UPDATE '.sql_table('blog')
736                            . " SET bname='" . addslashes($this->getName()) . "',"
737                            . "     bshortname='". addslashes($this->getShortName()) . "',"
738                            . "     bcomments=". intval($this->commentsEnabled()) . ","
739                            . "     bmaxcomments=" . intval($this->getMaxComments()) . ","
740                            . "     btimeoffset=" . $offset . ","
741                            . "     bpublic=" . intval($this->isPublic()) . ","
742                            . "     breqemail=" . intval($this->emailRequired()) . ","
743                            . "     bsendping=" . intval($this->pingUserland()) . ","
744                            . "     bconvertbreaks=" . intval($this->convertBreaks()) . ","
745                            . "     ballowpast=" . intval($this->allowPastPosting()) . ","
746                            . "     bnotify='" . addslashes($this->getNotifyAddress()) . "',"
747                            . "     bnotifytype=" . intval($this->getNotifyType()) . ","
748                            . "     burl='" . addslashes($this->getURL()) . "',"
749                            . "     bupdate='" . addslashes($this->getUpdateFile()) . "',"
750                            . "     bdesc='" . addslashes($this->getDescription()) . "',"
751                            . "     bdefcat=" . intval($this->getDefaultCategory()) . ","
752                            . "     bdefskin=" . intval($this->getDefaultSkin()) . ","
753                            . "     bincludesearch=" . intval($this->getSearchable())
754                            . " WHERE bnumber=" . intval($this->getID());
755                 sql_query($query);
756
757         }
758
759
760
761         // update update file if requested
762         function updateUpdatefile() {
763                  if ($this->getUpdateFile()) {
764                         $f_update = fopen($this->getUpdateFile(),'w');
765                         fputs($f_update,$this->getCorrectTime());
766                         fclose($f_update);
767                  }
768
769         }
770
771         /**
772           * Sends a XML-RPC ping message to Userland, so the weblog can
773           * show up in the weblogs.com updates-list
774           */
775         function sendUserlandPing() {
776                 global $php_errormsg;
777
778                  if ($this->pingUserland()) {
779                           // testmessage for adding an item
780                           $message = new xmlrpcmsg('weblogUpdates.ping',array(
781                                         new xmlrpcval($this->getName(),'string'),
782                                         new xmlrpcval($this->getURL(),'string')
783                           ));
784
785                           $c = new xmlrpc_client('/RPC2', 'rpc.weblogs.com', 80);
786
787                           // $c->setDebug(1);
788
789                           $r = $c->send($message,15); // 15 seconds timeout...
790
791                           if (($r == 0) && ($r->errno || $r->errstring)) {
792                                 return 'Error ' . $r->errno . ' : ' . $r->errstring;
793                           } elseif (($r == 0) && ($php_errormsg)) {
794                                 return 'PHP Error: ' . $php_errormsg;
795                           } elseif ($r == 0) {
796                                 return 'Error while trying to send ping. Sorry about that.';
797                           } elseif ($r->faultCode() != 0) {
798                                 return 'Error: ' . $r->faultString();
799                           } else {
800                                   $r = $r->value();     // get response struct
801                                   // get values
802                                   $flerror = $r->structmem('flerror');
803                                   $flerror = $flerror->scalarval();
804
805
806                                   $message = $r->structmem('message');
807                                   $message = $message->scalarval();
808
809                                   if ($flerror != 0)
810                                         return 'Error (flerror=1): ' . $message;
811                                   else
812                                         return 'Success: ' . $message;
813                           }
814                  }
815         }
816
817         function isValidCategory($catid) {
818                 $query = 'SELECT * FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' and catid=' . intval($catid);
819                 $res = mysql_query($query);
820                 return (mysql_num_rows($res) != 0);
821         }
822
823         function getCategoryName($catid) {
824                 $res = mysql_query('SELECT cname FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));
825                 $o = mysql_fetch_object($res);
826                 return $o->cname;
827         }
828
829         function getCategoryDesc($catid) {
830                 $res = mysql_query('SELECT cdesc FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));
831                 $o = mysql_fetch_object($res);
832                 return $o->cdesc;
833         }
834
835         function getCategoryIdFromName($name) {
836                 $res = mysql_query('SELECT catid FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and cname="' . addslashes($name) . '"');
837                 if (mysql_num_rows($res) > 0) {
838                         $o = mysql_fetch_object($res);
839                         return $o->catid;
840                 } else {
841                         return $this->getDefaultCategory();
842                 }
843         }
844
845         function pingUserland() {
846                 return $this->getSetting('bsendping');
847         }
848
849         function setPingUserland($val) {
850                 $this->setSetting('bsendping',$val);
851         }
852
853         function convertBreaks() {
854                 return $this->getSetting('bconvertbreaks');
855         }
856
857         function insertJavaScriptInfo($authorid = '') {
858                 global $member, $CONF;
859
860                 if ($authorid == '')
861                         $authorid = $member->getID();
862
863                 ?>
864                 <script type="text/javascript">
865                         setConvertBreaks(<?php echo  $this->convertBreaks() ? 'true' : 'false' ?>);
866                         setMediaUrl("<?php echo $CONF['MediaURL']?>");
867                         setAuthorId(<?php echo $authorid?>);
868                 </script><?php  }
869
870         function setConvertBreaks($val) {
871                 $this->setSetting('bconvertbreaks',$val);
872         }
873         function setAllowPastPosting($val) {
874                 $this->setSetting('ballowpast',$val);
875         }
876         function allowPastPosting() {
877                 return $this->getSetting('ballowpast');
878         }
879
880         function getCorrectTime($t=0) {
881                 if ($t == 0) $t = time();
882                 return ($t + 3600 * $this->getTimeOffset());
883         }
884
885         function getName() {
886                 return $this->getSetting('bname');
887         }
888
889         function getShortName() {
890                 return $this->getSetting('bshortname');
891         }
892
893         function getMaxComments() {
894                 return $this->getSetting('bmaxcomments');
895         }
896
897         function getNotifyAddress() {
898                 return $this->getSetting('bnotify');
899         }
900
901         function getNotifyType() {
902                 return $this->getSetting('bnotifytype');
903         }
904
905         function notifyOnComment() {
906                 $n = $this->getNotifyType();
907                 return (($n != 0) && (($n % 3) == 0));
908         }
909
910         function notifyOnVote() {
911                 $n = $this->getNotifyType();
912                 return (($n != 0) && (($n % 5) == 0));
913         }
914
915         function notifyOnNewItem() {
916                 $n = $this->getNotifyType();
917                 return (($n != 0) && (($n % 7) == 0));
918         }
919
920         function setNotifyType($val) {
921                 $this->setSetting('bnotifytype',$val);
922         }
923
924
925         function getTimeOffset() {
926                 return $this->getSetting('btimeoffset');
927         }
928
929         function commentsEnabled() {
930                 return $this->getSetting('bcomments');
931         }
932
933         function getURL() {
934                 return $this->getSetting('burl');
935         }
936
937         function getDefaultSkin() {
938                 return $this->getSetting('bdefskin');
939         }
940
941         function getUpdateFile() {
942                 return $this->getSetting('bupdate');
943         }
944
945         function getDescription() {
946                 return $this->getSetting('bdesc');
947         }
948
949         function isPublic() {
950                 return $this->getSetting('bpublic');
951         }
952
953         function emailRequired() {
954                 return $this->getSetting('breqemail');
955         }
956
957         function getSearchable() {
958                 return $this->getSetting('bincludesearch');
959         }
960
961         function getDefaultCategory() {
962                 return $this->getSetting('bdefcat');
963         }
964
965         function setPublic($val) {
966                 $this->setSetting('bpublic',$val);
967         }
968
969         function setSearchable($val) {
970                 $this->setSetting('bincludesearch',$val);
971         }
972
973         function setDescription($val) {
974                 $this->setSetting('bdesc',$val);
975         }
976
977         function setUpdateFile($val) {
978                 $this->setSetting('bupdate',$val);
979         }
980
981         function setDefaultSkin($val) {
982                 $this->setSetting('bdefskin',$val);
983         }
984
985         function setURL($val) {
986                 $this->setSetting('burl',$val);
987         }
988
989         function setName($val) {
990                 $this->setSetting('bname',$val);
991         }
992
993         function setShortName($val) {
994                 $this->setSetting('bshortname',$val);
995         }
996
997         function setCommentsEnabled($val) {
998                 $this->setSetting('bcomments',$val);
999         }
1000
1001         function setMaxComments($val) {
1002                 $this->setSetting('bmaxcomments',$val);
1003         }
1004
1005         function setNotifyAddress($val) {
1006                 $this->setSetting('bnotify',$val);
1007         }
1008
1009         function setEmailRequired($val) {
1010                 $this->setSetting('breqemail',$val);
1011         }
1012
1013         function setTimeOffset($val) {
1014                 // check validity of value
1015                 // 1. replace , by . (common mistake)
1016                 $val = str_replace(',','.',$val);
1017                 // 2. cast to float or int
1018                 if (is_numeric($val) && strstr($val,'.5')) {
1019                         $val = (float) $val;
1020                 } else {
1021                         $val = intval($val);
1022                 }
1023
1024                 $this->setSetting('btimeoffset',$val);
1025         }
1026
1027         function setDefaultCategory($val) {
1028                 $this->setSetting('bdefcat',$val);
1029         }
1030
1031         function getSetting($key) {
1032                 return $this->settings[$key];
1033         }
1034
1035         function setSetting($key,$value) {
1036                 $this->settings[$key] = $value;
1037         }
1038
1039
1040         // tries to add a member to the team. Returns false if the member was already on
1041         // the team
1042         function addTeamMember($memberid, $admin) {
1043                 global $manager;
1044
1045                 $memberid = intval($memberid);
1046                 $admin = intval($admin);
1047
1048                 // check if member is already a member
1049                 $tmem = MEMBER::createFromID($memberid);
1050
1051                 if ($tmem->isTeamMember($this->getID()))
1052                         return 0;
1053
1054                 $manager->notify(
1055                         'PreAddTeamMember',
1056                         array(
1057                                 'blog' => &$this,
1058                                 'member' => &$tmem,
1059                                 'admin' => &$admin
1060                         )
1061                 );
1062
1063                 // add to team
1064                 $query = 'INSERT INTO '.sql_table('team').' (TMEMBER, TBLOG, TADMIN) '
1065                            . 'VALUES (' . $memberid .', '.$this->getID().', "'.$admin.'")';
1066                 sql_query($query);
1067
1068                 $manager->notify(
1069                         'PostAddTeamMember',
1070                         array(
1071                                 'blog' => &$this,
1072                                 'member' => &$tmem,
1073                                 'admin' => $admin
1074                         )
1075
1076                 );
1077
1078                 ACTIONLOG::add(INFO, 'Added ' . $tmem->getDisplayName() . ' (ID=' .
1079                                            $memberid .') to the team of blog "' . $this->getName() . '"');
1080
1081                 return 1;
1082         }
1083
1084         function getID() {
1085                 return intVal($this->blogid);
1086         }
1087
1088         // returns true if there is a blog with the given shortname (static)
1089         function exists($name) {
1090                 $r = sql_query('select * FROM '.sql_table('blog').' WHERE bshortname="'.addslashes($name).'"');
1091                 return (mysql_num_rows($r) != 0);
1092         }
1093
1094         // returns true if there is a blog with the given ID (static)
1095         function existsID($id) {
1096                 $r = sql_query('select * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));
1097                 return (mysql_num_rows($r) != 0);
1098         }
1099
1100
1101 }
1102
1103 ?>