OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / BLOG.php
1 <<<<<<< HEAD
2 <?php\r
3 \r
4 /*\r
5  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
6  * Copyright (C) 2002-2012 The Nucleus Group\r
7  *\r
8  * This program is free software; you can redistribute it and/or\r
9  * modify it under the terms of the GNU General Public License\r
10  * as published by the Free Software Foundation; either version 2\r
11  * of the License, or (at your option) any later version.\r
12  * (see nucleus/documentation/index.html#license for more info)\r
13  */\r
14 /**\r
15  * A class representing a blog and containing functions to get that blog shown\r
16  * on the screen\r
17  *\r
18  * @license http://nucleuscms.org/license.txt GNU General Public License\r
19  * @copyright Copyright (C) 2002-2009 The Nucleus Group\r
20  * @version $Id: BLOG.php 1624 2012-01-09 11:36:20Z sakamocchi $\r
21  */\r
22 \r
23 if ( !function_exists('requestVar') ) exit;\r
24 require_once dirname(__FILE__) . '/ITEMACTIONS.php';\r
25 \r
26 class Blog\r
27 {\r
28         // blog id\r
29         public $blogid;\r
30         \r
31         // After creating an object of the blog class, contains true if the BLOG object is\r
32         // valid (the blog exists)\r
33         public $isValid;\r
34         \r
35         // associative array, containing all blogsettings (use the get/set functions instead)\r
36         private $settings;\r
37         \r
38         // ID of currently selected category\r
39         private $selectedcatid;\r
40         \r
41         /**\r
42          * Blog::_\construct()\r
43          * Creates a new BLOG object for the given blog\r
44          *\r
45          * @param       integer $id     blogid\r
46          * @return      void\r
47          */\r
48         public function __construct($id)\r
49         {\r
50                 global $catid;\r
51                 \r
52                 $this->blogid = (integer) $id;\r
53                 $this->readSettings();\r
54                 $this->setSelectedCategory($catid);\r
55                 return;\r
56         }\r
57         \r
58         /**\r
59          * Blog::readLog()\r
60          * Shows the given amount of items for this blog\r
61          *\r
62          * @param       string  $template       String representing the template _NAME_ (!)\r
63          * @param       integer $amountEntries  amount of entries to show\r
64          * @param       integer $startpos       offset from where items should be shown (e.g. 5 = start at fifth item)\r
65          * @return      integer amount of items shown\r
66          */\r
67         public function readLog($template, $amountEntries, $offset = 0, $startpos = 0)\r
68         {\r
69                 return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);\r
70         }\r
71         \r
72         /**\r
73          * Blog::showArchive()\r
74          * Shows an archive for a given month\r
75          *\r
76          * @param       integer $year           year\r
77          * @param       integer $month          month\r
78          * @param       string  $template       String representing the template name to be used\r
79          * @return      void\r
80          */\r
81         public function showArchive($templatename, $year, $month=0, $day=0)\r
82         {\r
83                 // create extra where clause for select query\r
84                 if ( $day == 0 && $month != 0 )\r
85                 {\r
86                         $timestamp_start = mktime(0,0,0,$month,1,$year);\r
87                         // also works when $month==12\r
88                         $timestamp_end = mktime(0,0,0,$month+1,1,$year);\r
89                 }\r
90                 elseif ( $month == 0 )\r
91                 {\r
92                         $timestamp_start = mktime(0,0,0,1,1,$year);\r
93                         // also works when $month==12\r
94                         $timestamp_end = mktime(0,0,0,12,31,$year);\r
95                 }\r
96                 else\r
97                 {\r
98                         $timestamp_start = mktime(0,0,0,$month,$day,$year);\r
99                         $timestamp_end = mktime(0,0,0,$month,$day+1,$year);\r
100                 }\r
101                 $extra_query = " and i.itime>=%s and i.itime<%s";\r
102                 $extra_query = sprintf($extra_query, DB::formatDateTime($timestamp_start), DB::formatDateTime($timestamp_end));\r
103                 \r
104                 $this->readLogAmount($templatename,0,$extra_query,'',1,1);\r
105                 return;\r
106         }\r
107         \r
108         /**\r
109          * Blog::setSelectedCategory()\r
110          * Sets the selected category by id (only when category exists)\r
111          * \r
112          * @param       integer $catid  ID for category\r
113          * @return      void\r
114          */\r
115         public function setSelectedCategory($catid)\r
116         {\r
117                 if ( $this->isValidCategory($catid) || (intval($catid) == 0) )\r
118                 {\r
119                         $this->selectedcatid = intval($catid);\r
120                 }\r
121                 return;\r
122         }\r
123         \r
124         /**\r
125          * Blog::setSelectedCategoryByName()\r
126          * Sets the selected category by name\r
127          * \r
128          * @param       string  $catname        name of category\r
129          * @return      void\r
130          */\r
131         public function setSelectedCategoryByName($catname)\r
132         {\r
133                 $this->setSelectedCategory($this->getCategoryIdFromName($catname));\r
134                 return;\r
135         }\r
136         \r
137         /**\r
138          * Blog::getSelectedCategory()\r
139          * Returns the selected category\r
140          * \r
141          * @param       void\r
142          * @return      integer\r
143          */\r
144         public function getSelectedCategory()\r
145         {\r
146                 return $this->selectedcatid;\r
147         }\r
148         \r
149         /**\r
150          * Shows the given amount of items for this blog\r
151          *\r
152          * @param       string  $template               string representing the template _NAME_ (!)\r
153          * @param       integer $amountEntries  amount of entries to show (0 = no limit)\r
154          * @param       string  $extraQuery             extra conditions to be added to the query\r
155          * @param       string  $highlight              contains a query that should be highlighted\r
156          * @param       integer $comments               1=show comments 0=don't show comments\r
157          * @param       integer $dateheads              1=show dateheads 0=don't show dateheads\r
158          * @param       integer $offset                 offset\r
159          * @return      integer amount of items shown\r
160          */\r
161         private function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0)\r
162         {\r
163                 $query = $this->getSqlBlog($extraQuery);\r
164                 \r
165                 if ( $amountEntries > 0 )\r
166                 {\r
167                         // $offset zou moeten worden:\r
168                         // (($startpos / $amountentries) + 1) * $offset ... later testen ...\r
169                         $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);\r
170                 }\r
171                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);\r
172         }\r
173         \r
174         /**\r
175          * Blog::showUsingQuery()\r
176          * Do the job for readLogAmmount\r
177          * \r
178          * @param       string  $templateName   template name\r
179          * @param       string  $query                  string for query\r
180          * @param       string  $highlight              string to be highlighted\r
181          * @param       integer $comments               the number of comments\r
182          * @param       boolean $dateheads              date header is needed or not\r
183          * @return      integer the number of rows as a result of mysql query\r
184          */\r
185         private function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1)\r
186         {\r
187                 global $CONF, $manager, $currentTemplateName;\r
188                 \r
189                 $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');\r
190                 if ( $lastVisit != 0 )\r
191                 {\r
192                         $lastVisit = $this->getCorrectTime($lastVisit);\r
193                 }\r
194                 \r
195                 // set templatename as global variable (so plugins can access it)\r
196                 $currentTemplateName = $templateName;\r
197                 $template =& $manager->getTemplate($templateName);\r
198                 \r
199                 // create parser object & action handler\r
200                 $handler = new ItemActions($this);\r
201                 $handler->setTemplate($template);\r
202                 $handler->setHighlight($highlight);\r
203                 $handler->setLastVisit($lastVisit);\r
204                 $handler->setShowComments($comments);\r
205                 \r
206                 $parser = new Parser($handler);\r
207                 \r
208                 // execute query\r
209                 $items = DB::getResult($query);\r
210                 \r
211                 // loop over all items\r
212                 $old_date = 0;\r
213                 foreach ( $items as $item )\r
214                 {\r
215                         // string timestamp -> unix timestamp\r
216                         $item['timestamp'] = strtotime($item['itime']);\r
217                         \r
218                         // action handler needs to know the item we're handling\r
219                         $handler->setCurrentItem($item);\r
220                         \r
221                         // add date header if needed\r
222                         if ( $dateheads )\r
223                         {\r
224                                 $new_date = date('dFY', $item['timestamp']);\r
225                                 if ( $new_date != $old_date )\r
226                                 {\r
227                                         // unless this is the first time, write date footer\r
228                                         $timestamp = $item['timestamp'];\r
229                                         if ( $old_date != 0 )\r
230                                         {\r
231                                                 $oldTS = strtotime($old_date);\r
232                                                 $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));\r
233                                                 \r
234                                                 if ( !in_array('DATE_FOOTER', $template) || empty($template['DATE_FOOTER']) )\r
235                                                 {\r
236                                                         $tmp_footer = '';\r
237                                                 }\r
238                                                 else\r
239                                                 {\r
240                                                         $tmp_footer = i18n::formatted_datetime($template['DATE_FOOTER'], $oldTS);\r
241                                                 }\r
242                                                 $parser->parse($tmp_footer);\r
243                                                 $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));\r
244                                         }\r
245                                         \r
246                                         $manager->notify('PreDateHead',array('blog' => &$this, 'timestamp' => $timestamp));\r
247                                         \r
248                                         // note, to use templatvars in the dateheader, the %-characters need to be doubled in\r
249                                         // order to be preserved by strftime\r
250                                         if ( !in_array('DATE_HEADER', $template) || empty($template['DATE_HEADER']) )\r
251                                         {\r
252                                                 $tmp_header = '';\r
253                                         }\r
254                                         else\r
255                                         {\r
256                                                 $tmp_header = i18n::formatted_datetime($template['DATE_HEADER'], $timestamp);\r
257                                         }\r
258                                         $parser->parse($tmp_header);\r
259                                         $manager->notify('PostDateHead',array('blog' => &$this, 'timestamp' => $timestamp));\r
260                                 }\r
261                                 $old_date = $new_date;\r
262                         }\r
263                         \r
264                         // parse item\r
265                         $parser->parse($template['ITEM_HEADER']);\r
266                         $manager->notify('PreItem', array('blog' => &$this, 'item' => &$item));\r
267                         $parser->parse($template['ITEM']);\r
268                         $manager->notify('PostItem', array('blog' => &$this, 'item' => &$item));\r
269                         $parser->parse($template['ITEM_FOOTER']);\r
270                 }\r
271                 \r
272                 $numrows = $items->rowCount();\r
273                 \r
274                 // add another date footer if there was at least one item\r
275                 if ( ($numrows > 0) && $dateheads )\r
276                 {\r
277                         $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));\r
278                         $parser->parse($template['DATE_FOOTER']);\r
279                         $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));\r
280                 }\r
281                 \r
282                 $items->closeCursor();\r
283                 return $numrows;\r
284         }\r
285         \r
286         /**\r
287          * Blog::showOneitem()\r
288          * Simplified function for showing only one item\r
289          * \r
290          * @param       integer $itemid         ID for item\r
291          * @param       array   $template       template for item\r
292          * @param       string  $highlight      string for highlight\r
293          * @return      integer 1\r
294          */\r
295         public function showOneitem($itemid, $template, $highlight)\r
296         {\r
297                 $extraQuery = ' and inumber=' . intval($itemid);\r
298                 \r
299                 return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);\r
300         }\r
301         \r
302         /**\r
303          * Blog::addItem()\r
304          * Adds an item to this blog\r
305          * \r
306          * @param       integer         $catid  ID for category\r
307          * @param       string          $title  ID for \r
308          * @param       string          $body   text for body\r
309          * @param       string          $more   text for more\r
310          * @param       integer         $blogid ID for blog\r
311          * @param       integer         $authorid       ID for author\r
312          * @param       timestamp       $timestamp      UNIX timestamp for post\r
313          * @param       boolean         $closed opened or closed\r
314          * @param       boolean         $draft  draft or not\r
315          * @param       boolean         $posted posted or not\r
316          * @return      integer ID for added item\r
317          */\r
318         function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft, $posted='1')\r
319         {\r
320                 global $manager;\r
321                 \r
322                 $blogid         = (integer) $blogid;\r
323                 $authorid       = (integer) $authorid;\r
324                 $title          = $title;\r
325                 $body           = $body;\r
326                 $more           = $more;\r
327                 $catid          = intval($catid);\r
328                 \r
329                 // convert newlines to <br />\r
330                 if ( $this->convertBreaks() )\r
331                 {\r
332                         $body = addBreaks($body);\r
333                         $more = addBreaks($more);\r
334                 }\r
335 \r
336                 if ( $closed != '1' )\r
337                 {\r
338                         $closed = '0';\r
339                 }\r
340                 if ( $draft != '0' )\r
341                 {\r
342                         $draft = '1';\r
343                 }\r
344                 \r
345                 if ( !$this->isValidCategory($catid) )\r
346                 {\r
347                         $catid = $this->getDefaultCategory();\r
348                 }\r
349                 \r
350                 $isFuture = 0;\r
351                 if ( $timestamp > $this->getCorrectTime() )\r
352                 {\r
353                         $isFuture = 1;\r
354                 }\r
355                 \r
356                 $timestamp = date('Y-m-d H:i:s',$timestamp);\r
357                 \r
358                 $manager->notify('PreAddItem',array('title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$this, 'authorid' => &$authorid, 'timestamp' => &$timestamp, 'closed' => &$closed, 'draft' => &$draft, 'catid' => &$catid));\r
359                 \r
360                 $ititle = DB::quoteValue($title);\r
361                 $ibody = DB::quoteValue($body);\r
362                 $imore = DB::quoteValue($more);\r
363                 $timestamp = DB::formatDateTime(strtotime($timestamp));\r
364                 \r
365                 $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)";\r
366                 $query = sprintf($query, sql_table('item'), $ititle, $ibody, $imore, $blogid, $authorid, $timestamp, $closed, $draft, $catid, $posted);\r
367                 DB::execute($query);\r
368                 $itemid = DB::getInsertId();\r
369                 \r
370                 $manager->notify('PostAddItem',array('itemid' => $itemid));\r
371                 \r
372                 if ( !$draft )\r
373                 {\r
374                         $this->updateUpdateFile();\r
375                 }\r
376                 // send notification mail\r
377                 if ( !$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem() )\r
378                 {\r
379                         $this->sendNewItemNotification($itemid, $title, $body);\r
380                 }\r
381                 return $itemid;\r
382         }\r
383         \r
384         /**\r
385          * Blog::sendNewItemNotification()\r
386          * Send a new item notification to the notification list\r
387          * \r
388          * @param       string  $itemid ID of the item\r
389          * @param       string  $title  title of the item\r
390          * @param       string  $body   body of the item\r
391          * @return      void\r
392          */\r
393         public function sendNewItemNotification($itemid, $title, $body)\r
394         {\r
395                 global $CONF, $member;\r
396                 \r
397                 $ascii = Entity::anchor_footnoting($body);\r
398                 \r
399                 $message = _NOTIFY_NI_MSG . " \n";\r
400                 $temp = parse_url($CONF['Self']);\r
401                 if ( $temp['scheme'] )\r
402                 {\r
403                         $message .= Link::create_item_link($itemid) . "\n\n";\r
404                 }\r
405                 else\r
406                 {\r
407                         $tempurl = $this->getURL();\r
408                         if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )\r
409                         {\r
410                                 $message .= $tempurl . '?itemid=' . $itemid . "\n\n";\r
411                         }\r
412                         else\r
413                         {\r
414                                 $message .= $tempurl . '/?itemid=' . $itemid . "\n\n";\r
415                         }\r
416                 }\r
417                 $message .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";\r
418                 $message .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";\r
419                 $message .= NOTIFICATION::get_mail_footer();\r
420                 \r
421                 $subject = $this->getName() . ': ' . _NOTIFY_NI_TITLE;\r
422                 \r
423                 $from = $member->getNotifyFromMailAddress();\r
424                 \r
425                 NOTIFICATION::mail($this->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());\r
426                 return;\r
427         }\r
428         \r
429         /**\r
430          * Blog::createNewCategory()\r
431          * Creates a new category for this blog\r
432          *\r
433          * @param       string  $catName                name of the new category. When empty, a name is generated automatically (starting with newcat)\r
434          * @param       string  $catDescription description of the new category. Defaults to 'New Category'\r
435          * @return      integer ID for new category on success. 0 on failure\r
436          */\r
437         public function createNewCategory($catName = '', $catDescription = _CREATED_NEW_CATEGORY_DESC)\r
438         {\r
439                 global $member, $manager;\r
440                 \r
441                 if ( !$member->blogAdminRights($this->blogid) )\r
442                 {\r
443                         return 0;\r
444                 }\r
445                 \r
446                 // generate\r
447                 if ( $catName == '' )\r
448                 {\r
449                         $catName = _CREATED_NEW_CATEGORY_NAME;\r
450                         $i = 1;\r
451                         \r
452                         $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);\r
453                         while ( $res->rowCount() > 0 )\r
454                         {\r
455                                 $i++;\r
456                                 $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);\r
457                         }\r
458                         \r
459                         $catName = $catName . $i;\r
460                 }\r
461                 \r
462                 $data = array(\r
463                         'blog'                  => &$this,\r
464                         'name'                  => &$catName,\r
465                         'description'   => $catDescription\r
466                 );\r
467                 $manager->notify('PreAddCategory', $data);\r
468                 \r
469                 $query = "INSERT INTO %s (cblog, cname, cdesc) VALUES (%d, %s, %s)";\r
470                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, DB::quoteValue($catName), DB::quoteValue($catDescription));\r
471                 DB::execute($query);\r
472                 $catid = DB::getInsertId();\r
473                 \r
474                 $data = array(\r
475                         'blog'                  => &$this,\r
476                         'name'                  => $catName,\r
477                         'description'   => $catDescription,\r
478                         'catid'                 => $catid\r
479                 );\r
480                 $manager->notify('PostAddCategory', $data);\r
481                 \r
482                 return $catid;\r
483         }\r
484         \r
485         /**\r
486          * Blog::search()\r
487          * Searches all months of this blog for the given query\r
488          *\r
489          * @param       string  $query                  search query\r
490          * @param       array   $template               template to be used (__NAME__ of the template)\r
491          * @param       integer $amountMonths   max amount of months to be search (0 = all)\r
492          * @param       integer $maxresults             max number of results to show\r
493          * @param       integer $startpos               offset\r
494          * @return      amount of hits found\r
495          */\r
496         public function search($query, $template, $amountMonths, $maxresults, $startpos) {\r
497                 global $CONF, $manager;\r
498                 \r
499                 $highlight      = '';\r
500                 $sqlquery       = $this->getSqlSearch($query, $amountMonths, $highlight);\r
501                 \r
502                 if ( $sqlquery == '' )\r
503                 {\r
504                         // no query -> show everything\r
505                         $extraquery = '';\r
506                         $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);\r
507                 }\r
508                 else\r
509                 {\r
510                         // add LIMIT to query (to split search results into pages)\r
511                         if ( intval($maxresults > 0) )\r
512                         {\r
513                                 $sqlquery .= ' LIMIT ' . intval($startpos) . ',' . intval($maxresults);\r
514                         }\r
515                         \r
516                         // show results\r
517                         $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);\r
518                         \r
519                         // when no results were found, show a message\r
520                         if ( $amountfound == 0 )\r
521                         {\r
522                                 $template =& $manager->getTemplate($template);\r
523                                 $vars = array(\r
524                                         'query'         => Entity::hsc($query),\r
525                                         'blogid'        => $this->blogid\r
526                                 );\r
527                                 echo Template::fill($template['SEARCH_NOTHINGFOUND'], $vars);\r
528                         }\r
529                 }\r
530                 return $amountfound;\r
531         }\r
532         \r
533         /**\r
534          * Blog::getSqlSearch()\r
535          * Returns an SQL query to use for a search query\r
536          * No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
537          *\r
538          * @param       string  $query                  search query\r
539          * @param       integer $amountMonths   amount of months to search back. Default = 0 = unlimited\r
540          * @param       string  $mode                   either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query\r
541          * @return      string  $highlight              words to highlight (out parameter)\r
542          * @return      string  either a full SQL query, or an empty string (if querystring empty)\r
543          */\r
544         public function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')\r
545         {\r
546                 $searchclass = new Search($query);\r
547                 \r
548                 $highlight       = $searchclass->inclusive;\r
549                 \r
550                 // if querystring is empty, return empty string\r
551                 if ( $searchclass->inclusive == '' )\r
552                 {\r
553                         return '';\r
554                 }\r
555                 \r
556                 $where  = $searchclass->boolean_sql_where('ititle,ibody,imore');\r
557                 $select = $searchclass->boolean_sql_select('ititle,ibody,imore');\r
558                 \r
559                 // get list of blogs to search\r
560                 $blogs          = $searchclass->blogs;  // array containing blogs that always need to be included\r
561                 $blogs[]        = $this->blogid;                // also search current blog (duh)\r
562                 $blogs          = array_unique($blogs); // remove duplicates\r
563                 $selectblogs = '';\r
564                 if ( count($blogs) > 0 )\r
565                 {\r
566                         $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';\r
567                 }\r
568                 \r
569                 if ( $mode == '' )\r
570                 {\r
571                         $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,\r
572                                 m.mname as author, m.mrealname as authorname, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl,\r
573                                 c.cname as category';\r
574                         \r
575                         if ( $select )\r
576                         {\r
577                                 $query .= ', '.$select. ' as score ';\r
578                         }\r
579                 }\r
580                 else\r
581                 {\r
582                         $query = 'SELECT COUNT(*) as result ';\r
583                 }\r
584                 \r
585                 $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'\r
586                                 . ' WHERE i.iauthor=m.mnumber'\r
587                                 . ' and i.icat=c.catid'\r
588                                 // exclude drafts\r
589                                 . ' and i.idraft=0'\r
590                                 . $selectblogs\r
591                                         // don't show future items\r
592                                 . ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime())\r
593                                 . ' and '.$where;\r
594                 \r
595                 // take into account amount of months to search\r
596                 if ( $amountMonths > 0 )\r
597                 {\r
598                         $localtime = getdate($this->getCorrectTime());\r
599                         $timestamp_start = mktime(0,0,0,$localtime['mon'] - $amountMonths,1,$localtime['year']);\r
600                         $query .= ' and i.itime>' . DB::formatDateTime($timestamp_start);\r
601                 }\r
602                 \r
603                 if ( $mode == '' )\r
604                 {\r
605                         if ( $select )\r
606                         {\r
607                                 $query .= ' ORDER BY score DESC';\r
608                         }\r
609                         else\r
610                         {\r
611                                 $query .= ' ORDER BY i.itime DESC ';\r
612                         }\r
613                 }\r
614                 \r
615                 return $query;\r
616         }\r
617         \r
618         /**\r
619          * Blog::getSqlBlog()\r
620          * Returns the SQL query that's normally used to display the blog items on the index type skins\r
621          * No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
622          *\r
623          * @param       string  $extraQuery     extra query string\r
624          * @param       string  $mode           either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query\r
625          * @return      string  either a full SQL query, or an empty string\r
626          */\r
627         public function getSqlBlog($extraQuery, $mode = '')\r
628         {\r
629                 if ( $mode == '' )\r
630                 {\r
631                         $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author,\r
632                                 m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail,\r
633                                 m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';\r
634                 }\r
635                 else\r
636                 {\r
637                         $query = 'SELECT COUNT(*) as result ';\r
638                 }\r
639                 \r
640                 $query  .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'\r
641                                 . ' WHERE i.iblog='.$this->blogid\r
642                                 . ' and i.iauthor=m.mnumber'\r
643                                 . ' and i.icat=c.catid'\r
644                                 . ' and i.idraft=0' // exclude drafts\r
645                                 . ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime()); // don't show future items\r
646                 \r
647                 if ( $this->selectedcatid )\r
648                 {\r
649                         $query .= ' and i.icat=' . $this->selectedcatid . ' ';\r
650                 }\r
651                 \r
652                 $query .= $extraQuery;\r
653                 \r
654                 if ( $mode == '' )\r
655                 {\r
656                         $query .= ' ORDER BY i.itime DESC';\r
657                 }\r
658                 return $query;\r
659         }\r
660         \r
661         /**\r
662          * Blog::showArchiveList()\r
663          * Shows the archivelist using the given template\r
664          * \r
665          * @param       string  $template       template name\r
666          * @param       string  $mode   year/month/day\r
667          * @param       integer $limit  limit of record count\r
668          * @return      void\r
669          */\r
670         public function showArchiveList($template, $mode = 'month', $limit = 0)\r
671         {\r
672                 global $CONF, $catid, $manager;\r
673                 \r
674                 if ( !isset ($linkparams) )\r
675                 {\r
676                         $linkparams = array();\r
677                 }\r
678                 \r
679                 if ( $catid )\r
680                 {\r
681                         $linkparams = array('catid' => $catid);\r
682                 }\r
683                 \r
684                 $template =& $manager->getTemplate($template);\r
685                 $data['blogid'] = $this->blogid;\r
686                 \r
687                 if ( !array_key_exists('ARCHIVELIST_HEADER', $template) || !$template['ARCHIVELIST_HEADER'] )\r
688                 {\r
689                         $tplt = '';\r
690                 }\r
691                 else\r
692                 {\r
693                         $tplt = $template['ARCHIVELIST_HEADER'];\r
694                 }\r
695                 \r
696                 echo Template::fill($tplt, $data);\r
697                 \r
698                 $query = 'SELECT itime, SUBSTRING(itime,1,4) AS Year, SUBSTRING(itime,6,2) AS Month, SUBSTRING(itime,9,2) AS Day'\r
699                                 . ' FROM '.sql_table('item')\r
700                                 . ' WHERE iblog=' . $this->blogid\r
701                                 . ' AND itime <=' . DB::formatDateTime($this->getCorrectTime()) // don't show future items!\r
702                                 . ' AND idraft=0'; // don't show draft items\r
703                 \r
704                 if ( $catid )\r
705                 {\r
706                         $query .= ' and icat=' . intval($catid);\r
707                 }\r
708                 \r
709                 $query .= ' GROUP BY Year';\r
710                 if ( $mode == 'month' || $mode == 'day' )\r
711                 {\r
712                         $query .= ', Month';\r
713                 }\r
714                 if ( $mode == 'day' )\r
715                 {\r
716                         $query .= ', Day';\r
717                 }\r
718                 \r
719                 $query .= ' ORDER BY itime DESC';\r
720                 \r
721                 if ( $limit > 0 )\r
722                 {\r
723                         $query .= ' LIMIT ' . intval($limit);\r
724                 }\r
725                 \r
726                 $res = DB::getResult($query);\r
727                 foreach ( $res as $current )\r
728                 {\r
729                         /* string time -> unix timestamp */\r
730                         $current['itime'] = strtotime($current['itime']);\r
731                         \r
732                         if ( $mode == 'day' )\r
733                         {\r
734                                 $archivedate = date('Y-m-d',$current['itime']);\r
735                                 $archive['day'] = date('d',$current['itime']);\r
736                                 $data['day'] = date('d',$current['itime']);\r
737                                 $data['month'] = date('m',$current['itime']);\r
738                                 $archive['month'] = $data['month'];\r
739                         }\r
740                         elseif ( $mode == 'year' )\r
741                         {\r
742                                 $archivedate = date('Y',$current['itime']);\r
743                                 $data['day'] = '';\r
744                                 $data['month'] = '';\r
745                                 $archive['day'] = '';\r
746                                 $archive['month'] = '';\r
747                         }\r
748                         else\r
749                         {\r
750                                 $archivedate = date('Y-m',$current['itime']);\r
751                                 $data['month'] = date('m',$current['itime']);\r
752                                 $archive['month'] = $data['month'];\r
753                                 $data['day'] = '';\r
754                                 $archive['day'] = '';\r
755                         }\r
756                         \r
757                         $data['year'] = date('Y',$current['itime']);\r
758                         $archive['year'] = $data['year'];\r
759                         $data['archivelink'] = Link::create_archive_link($this->blogid,$archivedate,$linkparams);\r
760                         \r
761                         $manager->notify('PreArchiveListItem', array('listitem' => &$data));\r
762                         \r
763                         $temp = Template::fill($template['ARCHIVELIST_LISTITEM'],$data);\r
764                         echo i18n::formatted_datetime($temp, $current['itime']);\r
765                         return;\r
766                 }\r
767                 \r
768                 $res->closeCursor();\r
769                 \r
770                 if ( !array_key_exists('ARCHIVELIST_FOOTER', $template) || !$template['ARCHIVELIST_FOOTER'] )\r
771                 {\r
772                         $tplt = '';\r
773                 }\r
774                 else\r
775                 {\r
776                         $tplt = $template['ARCHIVELIST_FOOTER'];\r
777                 }\r
778                 \r
779                 echo Template::fill($tplt, $data);\r
780                 return;\r
781         }\r
782         \r
783         /**\r
784          * Blog::showCategoryList()\r
785          * Shows the list of categories using a given template\r
786          * \r
787          * @param       string  $template       Template Name\r
788          * @return      void\r
789          */\r
790         public function showCategoryList($template)\r
791         {\r
792                 global $CONF, $archive, $archivelist, $manager;\r
793                 \r
794                 /*\r
795                  * determine arguments next to catids\r
796                  * I guess this can be done in a better way, but it works\r
797                  */\r
798                 $linkparams = array();\r
799                 if ( $archive )\r
800                 {\r
801                         $blogurl = Link::create_archive_link($this->blogid, $archive, '');\r
802                         $linkparams['blogid'] = $this->blogid;\r
803                         $linkparams['archive'] = $archive;\r
804                 }\r
805                 else if ( $archivelist )\r
806                 {\r
807                         $blogurl = Link::create_archivelist_link($this->blogid, '');\r
808                         $linkparams['archivelist'] = $archivelist;\r
809                 }\r
810                 else\r
811                 {\r
812                         $blogurl = Link::create_blogid_link($this->blogid, '');\r
813                         $linkparams['blogid'] = $this->blogid;\r
814                 }\r
815                 \r
816                 $template =& $manager->getTemplate($template);\r
817                 \r
818                 //: Change: Set nocatselected variable\r
819                 if ( $this->selectedcatid )\r
820                 {\r
821                         $nocatselected = 'no';\r
822                 }\r
823                 else\r
824                 {\r
825                         $nocatselected = 'yes';\r
826                 } \r
827                 \r
828                 $args = array(\r
829                         'blogid'        => $this->blogid,\r
830                         'blogurl'       => $blogurl,\r
831                         'self'          => $CONF['Self'],\r
832                         'catiscurrent'  => $nocatselected, // Change: Set catiscurrent template variable for header\r
833                         'currentcat'    => $nocatselected \r
834                 );\r
835                 \r
836                 /* output header of category list item */\r
837                 if ( !array_key_exists('CATLIST_HEADER', $template) || empty($template['CATLIST_HEADER']) )\r
838                 {\r
839                         echo Template::fill(NULL, $args);\r
840                 }\r
841                 else\r
842                 {\r
843                         echo Template::fill($template['CATLIST_HEADER'], $args);\r
844                 }\r
845                 \r
846                 $query = "SELECT catid, cdesc as catdesc, cname as catname FROM %s WHERE cblog=%d ORDER BY cname ASC;";\r
847                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid);\r
848                 $res = DB::getResult($query);\r
849                 \r
850                 foreach ( $res as $data )\r
851                 {\r
852                         $args = array(\r
853                                 'catid' => $data['catid'],\r
854                                 'name'  => $data['catname'],\r
855                                 'extra' => $linkparams\r
856                         );\r
857                         \r
858                         $data['blogid']         = $this->blogid;\r
859                         $data['blogurl']        = $blogurl;\r
860                         $data['catlink']        = Link::create_link('category', $args);\r
861                         $data['self']           = $CONF['Self'];\r
862                         \r
863                         // this gives catiscurrent = no when no category is selected.\r
864                         $data['catiscurrent'] = 'no';\r
865                         $data['currentcat'] = 'no';\r
866                         \r
867                         if ( $this->selectedcatid )\r
868                         {\r
869                                 if ( $this->selectedcatid == $data['catid'] )\r
870                                 {\r
871                                         $data['catiscurrent']   = 'yes';\r
872                                         $data['currentcat']             = 'yes';\r
873                                 }\r
874                         }\r
875                         else\r
876                         {\r
877                                 global $itemid;\r
878                                 if ( intval($itemid) && $manager->existsItem(intval($itemid), 0, 0) )\r
879                                 {\r
880                                         $iobj   =& $manager->getItem(intval($itemid), 0, 0);\r
881                                         $cid    = $iobj['catid'];\r
882                                         \r
883                                         if ( $cid == $data['catid'] )\r
884                                         {\r
885                                                 $data['catiscurrent']   = 'yes';\r
886                                                 $data['currentcat']             = 'yes';\r
887                                         }\r
888                                 }\r
889                         }\r
890                         \r
891                         $manager->notify('PreCategoryListItem', array('listitem' => &$data));\r
892                         \r
893                         if ( !array_key_exists('CATLIST_LISTITEM', $template) || empty($template['CATLIST_LISTITEM']))\r
894                         {\r
895                                 echo Template::fill(NULL, $data);\r
896                         }\r
897                         else\r
898                         {\r
899                                 echo Template::fill($template['CATLIST_LISTITEM'], $data);\r
900                         }\r
901                 }\r
902                 \r
903                 $res->closeCursor();\r
904                 \r
905                 $args = array(\r
906                         'blogid'                => $this->blogid,\r
907                         'blogurl'               => $blogurl,\r
908                         'self'                  => $CONF['Self'],\r
909                         'catiscurrent'  => $nocatselected, //: Change: Set catiscurrent template variable for footer\r
910                         'currentcat'    => $nocatselected\r
911                 );\r
912                 \r
913                 if ( !array_key_exists('CATLIST_FOOTER', $template) || empty($template['CATLIST_FOOTER']))\r
914                 {\r
915                         echo Template::fill(NULL, $args);\r
916                 }\r
917                 else\r
918                 {\r
919                         echo Template::fill($template['CATLIST_FOOTER'], $args);\r
920                 }\r
921                 \r
922                 return;\r
923         }\r
924         \r
925         /**\r
926          * Blog::showBlogList()\r
927          * Shows a list of all blogs in the system using a given template\r
928          * ordered by number, name, shortname or description\r
929          * in ascending or descending order\r
930          * \r
931          * @param       string  $template       tempalte name\r
932          * @param       string  $bnametype      bname/bshortname\r
933          * @param       string  $orderby        string for 'ORDER BY' SQL\r
934          * @param       string  $direction      ASC/DESC\r
935          * @return      void\r
936          */\r
937         public function showBlogList($template, $bnametype, $orderby, $direction)\r
938         {\r
939                 global $CONF, $manager;\r
940                 \r
941                 switch ( $orderby )\r
942                 {\r
943                         case 'number':\r
944                                 $orderby='bnumber';\r
945                                 break;\r
946                         case 'name':\r
947                                 $orderby='bname';\r
948                                 break;\r
949                         case 'shortname':\r
950                                 $orderby='bshortname';\r
951                                 break;\r
952                         case 'description':\r
953                                 $orderby='bdesc';\r
954                                 break;\r
955                         default:\r
956                                 $orderby='bnumber';\r
957                                 break;\r
958                 }\r
959                 \r
960                 $direction=strtolower($direction);\r
961                 switch ( $direction )\r
962                 {\r
963                         case 'asc':\r
964                                 $direction='ASC';\r
965                                 break;\r
966                         case 'desc':\r
967                                 $direction='DESC';\r
968                                 break;\r
969                         default:\r
970                                 $direction='ASC';\r
971                                 break;\r
972                 }\r
973                 \r
974                 $template =& $manager->getTemplate($template);\r
975                 \r
976                 if ( array_key_exists('BLOGLIST_HEADER', $template) && !empty($template['BLOGLIST_HEADER']) )\r
977                 {\r
978                         $vars = array(\r
979                                 'sitename'      => $CONF['SiteName'],\r
980                                 'siteurl'       => $CONF['IndexURL']\r
981                         );\r
982                         \r
983                         echo Template::fill($template['BLOGLIST_HEADER'], $vars);\r
984                 }\r
985                 \r
986                 if ( array_key_exists('BLOGLIST_LISTITEM', $template) && !empty($template['BLOGLIST_LISTITEM']) )\r
987                 {\r
988                         $query = 'SELECT bnumber, bname, bshortname, bdesc, burl FROM '.sql_table('blog').' ORDER BY '.$orderby.' '.$direction;\r
989                         $res = DB::getResult($query);\r
990                         \r
991                         foreach ( $res as $data )\r
992                         {\r
993                                 $list = array();\r
994                                 $list['bloglink'] = Link::create_blogid_link($data['bnumber']);\r
995                                 $list['blogdesc'] = $data['bdesc'];\r
996                                 $list['blogurl'] = $data['burl'];\r
997                                 \r
998                                 if ( $bnametype == 'shortname' )\r
999                                 {\r
1000                                         $list['blogname'] = $data['bshortname'];\r
1001                                 }\r
1002                                 else\r
1003                                 {\r
1004                                         /* all other cases */\r
1005                                         $list['blogname'] = $data['bname'];\r
1006                                 }\r
1007                                 \r
1008                                 $manager->notify('PreBlogListItem',array('listitem' => &$list));\r
1009                                 \r
1010                                 echo Template::fill($template['BLOGLIST_LISTITEM'], $list);\r
1011                         }\r
1012                         \r
1013                         $res->closeCursor();\r
1014                 }\r
1015                 \r
1016                 \r
1017                 if ( array_key_exists('BLOGLIST_FOOTER', $template) && !empty($template['BLOGLIST_FOOTER']) )\r
1018                 {\r
1019                         $vars = array(\r
1020                                 'sitename' => $CONF['SiteName'],\r
1021                                 'siteurl' => $CONF['IndexURL']\r
1022                         );\r
1023                         echo Template::fill($template['BLOGLIST_FOOTER']);\r
1024                 }\r
1025                 return;\r
1026         }\r
1027         \r
1028         /**\r
1029          * Blog::readSettings()\r
1030          * Read the blog settings\r
1031          * \r
1032          * @param       void\r
1033          * @return      void\r
1034          */\r
1035         public function readSettings()\r
1036         {\r
1037                 $query =  'SELECT * FROM %s WHERE bnumber=%d;';\r
1038                 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);\r
1039                 $res = DB::getResult($query);\r
1040                 \r
1041                 $this->isValid = ($res->rowCount() > 0);\r
1042                 if ( $this->isValid )\r
1043                 {\r
1044                         $this->settings = $res->fetch(PDO::FETCH_ASSOC);\r
1045                 }\r
1046                 return;\r
1047         }\r
1048         \r
1049         /**\r
1050          * Blog::writeSettings()\r
1051          * Write the blog settings\r
1052          */\r
1053         public function writeSettings()\r
1054         {\r
1055                 // (can't use floatval since not available prior to PHP 4.2)\r
1056                 $offset = $this->getTimeOffset();\r
1057                 if ( !is_float($offset) )\r
1058                 {\r
1059                         $offset = (integer) $offset;\r
1060                 }\r
1061                 \r
1062                 $query =  'UPDATE '.sql_table('blog')\r
1063                            . ' SET bname=' . DB::quoteValue($this->getName()) . ','\r
1064                            . '     bshortname='. DB::quoteValue($this->getShortName()) . ','\r
1065                            . '     bcomments='. intval($this->commentsEnabled()) . ','\r
1066                            . '     bmaxcomments=' . intval($this->getMaxComments()) . ','\r
1067                            . '     btimeoffset=' . $offset . ','\r
1068                            . '     bpublic=' . intval($this->isPublic()) . ','\r
1069                            . '     breqemail=' . intval($this->emailRequired()) . ','\r
1070                            . '     bconvertbreaks=' . intval($this->convertBreaks()) . ','\r
1071                            . '     ballowpast=' . intval($this->allowPastPosting()) . ','\r
1072                            . '     bnotify=' . DB::quoteValue($this->getNotifyAddress()) . ','\r
1073                            . '     bnotifytype=' . intval($this->getNotifyType()) . ','\r
1074                            . '     burl=' . DB::quoteValue($this->getURL()) . ','\r
1075                            . '     bupdate=' . DB::quoteValue($this->getUpdateFile()) . ','\r
1076                            . '     bdesc=' . DB::quoteValue($this->getDescription()) . ','\r
1077                            . '     bdefcat=' . intval($this->getDefaultCategory()) . ','\r
1078                            . '     bdefskin=' . intval($this->getDefaultSkin()) . ','\r
1079                            . '     bincludesearch=' . intval($this->getSearchable())\r
1080                            . ' WHERE bnumber=' . intval($this->blogid);\r
1081                 DB::execute($query);\r
1082                 return;\r
1083         }\r
1084         \r
1085         /**\r
1086          * Blog::updateUpdatefile()\r
1087          * Update the update file if requested\r
1088          * \r
1089          * @param       void\r
1090          * @return      void\r
1091          */\r
1092         public function updateUpdatefile()\r
1093         {\r
1094                 if ( $this->getUpdateFile() )\r
1095                 {\r
1096                         $f_update = fopen($this->getUpdateFile(), 'w');\r
1097                         fputs($f_update,$this->getCorrectTime());\r
1098                         fclose($f_update);\r
1099                 }\r
1100                 return;\r
1101         }\r
1102         \r
1103         /**\r
1104          * Blog::isValidCategory()\r
1105          * Check if a category with a given catid is valid\r
1106          * \r
1107          * @param       integer $catid  ID for category\r
1108          * @return      boolean exists or not\r
1109          */\r
1110         public function isValidCategory($catid)\r
1111         {\r
1112                 $query = 'SELECT * FROM %s WHERE cblog=%d and catid=%d;';\r
1113                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);\r
1114                 $res = DB::getResult($query);\r
1115                 return ($res->rowCount() != 0);\r
1116         }\r
1117         \r
1118         /**\r
1119          * Blog::getCategoryName()\r
1120          * Get the category name for a given catid\r
1121          * \r
1122          * @param       integer $catid  ID for category\r
1123          * @return      string  name of category\r
1124          */\r
1125         public function getCategoryName($catid)\r
1126         {\r
1127                 $query = 'SELECT cname FROM %s WHERE cblog=%d and catid=%d;';\r
1128                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);\r
1129                 $res = DB::getValue($query);\r
1130                 return $res;\r
1131         }\r
1132         \r
1133         /**\r
1134          * Blog::getCategoryDesc()\r
1135          * Get the category description for a given catid\r
1136          * \r
1137          * @param $catid\r
1138          *      category id\r
1139          */\r
1140         public function getCategoryDesc($catid)\r
1141         {\r
1142                 $query = 'SELECT cdesc FROM %s WHERE cblog=%d and catid=%d;';\r
1143                 $query = sprintf($querym, sql_table('category'), (integer) $this->blogid, (integer) $catid);\r
1144                 $res = DB::getValue();\r
1145                 return $res;\r
1146         }\r
1147         \r
1148         /**\r
1149          * Blog::getCategoryIdFromName\r
1150          * Get the category id for a given category name\r
1151          * \r
1152          * @param       string  $name   category name\r
1153          * @return      ID for category\r
1154          */\r
1155         public function getCategoryIdFromName($name)\r
1156         {\r
1157                 $query = 'SELECT catid FROM %s WHERE cblog=%d and cname=%s;';\r
1158                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, DB::quoteValue($name));\r
1159                 \r
1160                 $res = DB::getValue();\r
1161                 if ( !$res )\r
1162                 {\r
1163                         return $this->getDefaultCategory();\r
1164                 }\r
1165                 return $res;\r
1166         }\r
1167         \r
1168         /**\r
1169          * Blog::insertJavaScriptInfo()\r
1170          * Insert a javascript that includes information about the settings\r
1171          * of an author:  ConvertBreaks, MediaUrl and AuthorId\r
1172          * \r
1173          * @param       $authorid       id of the author\r
1174          */\r
1175         public function insertJavaScriptInfo($authorid = '')\r
1176         {\r
1177                 global $member, $CONF;\r
1178                 \r
1179                 if ( $authorid == '' )\r
1180                 {\r
1181                         $authorid = $member->getID();\r
1182                 }\r
1183                 \r
1184                 echo "<script type=\"text/javascript\">\n";\r
1185                 \r
1186                 if ( !$this->convertBreaks() )\r
1187                 {\r
1188                         echo "setConvertBreaks(false);\n";\r
1189                 }\r
1190                 else\r
1191                 {\r
1192                         echo "setConvertBreaks(true);\n";\r
1193                 }\r
1194                 echo "setMediaUrl('{$CONF['MediaURL']}');\n";\r
1195                 echo "setAuthorId('{$authorid}');\n";\r
1196                 echo "</script>\n";\r
1197                 return;\r
1198         }\r
1199         \r
1200         /**\r
1201          * Blog::setAllowPastPosting()\r
1202          * Set the the setting for allowing to publish postings in the past\r
1203          * \r
1204          * @param       boolean $val    new value for ballowpast\r
1205          * @return      void\r
1206          */\r
1207         public function setAllowPastPosting($val)\r
1208         {\r
1209                 $this->setSetting('ballowpast', $val);\r
1210                 return;\r
1211         }\r
1212         \r
1213         /**\r
1214          * Blog::allowPastPosting()\r
1215          * Get the the setting if it is allowed to publish postings in the past\r
1216          * [should be named as getAllowPastPosting()]\r
1217          * \r
1218          * @param       void\r
1219          * @return      boolean\r
1220          */\r
1221         public function allowPastPosting()\r
1222         {\r
1223                 return $this->getSetting('ballowpast');\r
1224         }\r
1225         \r
1226         /**\r
1227          * Blog::getCorrectTime()\r
1228          * \r
1229          * @param       integer $t\r
1230          * @return      integer\r
1231          */\r
1232         public function getCorrectTime($t=0)\r
1233         {\r
1234                 if ( $t == 0 )\r
1235                 {\r
1236                         $t = time();\r
1237                 }\r
1238                 return ($t + 3600 * $this->getTimeOffset());\r
1239         }\r
1240         \r
1241         /**\r
1242          * Blog::getName()\r
1243          * \r
1244          * @param       void\r
1245          * @return      string name of this weblog\r
1246          */\r
1247         public function getName()\r
1248         {\r
1249                 return $this->getSetting('bname');\r
1250         }\r
1251         \r
1252         /**\r
1253          * Blog::getShortName()\r
1254          * \r
1255          * @param       void\r
1256          * @return      string  short name of this weblog\r
1257          */\r
1258         public function getShortName()\r
1259         {\r
1260                 return $this->getSetting('bshortname');\r
1261         }\r
1262         \r
1263         /**\r
1264          * Blog::getMaxComments()\r
1265          * \r
1266          * @param       void\r
1267          * @return      integer maximum number of comments\r
1268          */\r
1269         public function getMaxComments()\r
1270         {\r
1271                 return $this->getSetting('bmaxcomments');\r
1272         }\r
1273         \r
1274         /**\r
1275          * Blog::getNotifyAddress()\r
1276          * \r
1277          * @param       void\r
1278          * @return      string  mail address for notifying\r
1279          */\r
1280         public function getNotifyAddress()\r
1281         {\r
1282                 return $this->getSetting('bnotify');\r
1283         }\r
1284         \r
1285         /**\r
1286          * Blog::getNotifyType()\r
1287          * \r
1288          * @param       void\r
1289          * @return      integer notifycation type\r
1290          */\r
1291         public function getNotifyType()\r
1292         {\r
1293                 return $this->getSetting('bnotifytype');\r
1294         }\r
1295         \r
1296         /**\r
1297          * Blog::notifyOnComment()\r
1298          * \r
1299          * @param       void\r
1300          * @return      boolean\r
1301          */\r
1302         public function notifyOnComment()\r
1303         {\r
1304                 $n = $this->getNotifyType();\r
1305                 return (($n != 0) && (($n % 3) == 0));\r
1306         }\r
1307         \r
1308         /**\r
1309          * Blog::notifyOnVote()\r
1310          * \r
1311          * @param       void\r
1312          * @return      boolean\r
1313          */\r
1314         public function notifyOnVote()\r
1315         {\r
1316                 $n = $this->getNotifyType();\r
1317                 return (($n != 0) && (($n % 5) == 0));\r
1318         }\r
1319         \r
1320         /**\r
1321          * Blog::notifyOnNewItem()\r
1322          * \r
1323          * @param       void\r
1324          * @return      boolean\r
1325          */\r
1326         public function notifyOnNewItem()\r
1327         {\r
1328                 $n = $this->getNotifyType();\r
1329                 return (($n != 0) && (($n % 7) == 0));\r
1330         }\r
1331         \r
1332         /**\r
1333          * Blog::setNotifyType()\r
1334          * \r
1335          * @param       integer $val\r
1336          * @return      void\r
1337          */\r
1338         public function setNotifyType($val)\r
1339         {\r
1340                 $this->setSetting('bnotifytype',$val);\r
1341                 return;\r
1342         }\r
1343         \r
1344         /**\r
1345          * Blog::getTimeOffset()\r
1346          * @param       void\r
1347          * @return      \r
1348          */\r
1349         public function getTimeOffset()\r
1350         {\r
1351                 return $this->getSetting('btimeoffset');\r
1352         }\r
1353         \r
1354         /**\r
1355          * Blog::commentsEnabled()\r
1356          * @param       void\r
1357          * @return      integer enabled or not\r
1358          */\r
1359         public function commentsEnabled()\r
1360         {\r
1361                 return $this->getSetting('bcomments');\r
1362         }\r
1363         \r
1364         /**\r
1365          * Blog::getURL()\r
1366          * @param       void\r
1367          * @return      string  URI for this weblog\r
1368          */\r
1369         public function getURL()\r
1370         {\r
1371                 return $this->getSetting('burl');\r
1372         }\r
1373         \r
1374         /**\r
1375          * Blog::getDefaultSkin()\r
1376          * @param       void\r
1377          * @return      name of skin as default for this weblog\r
1378          */\r
1379         public function getDefaultSkin()\r
1380         {\r
1381                 return $this->getSetting('bdefskin');\r
1382         }\r
1383         \r
1384         /**\r
1385          * Blog::getUpdateFile()\r
1386          * @param       void\r
1387          * @return      string  name of file to be updated when weblog is updated\r
1388          */\r
1389         public function getUpdateFile()\r
1390         {\r
1391                 return $this->getSetting('bupdate');\r
1392         }\r
1393         \r
1394         /**\r
1395          * Blog::getDescription()\r
1396          * @param       void\r
1397          * @return      string  description for this weblog\r
1398          */\r
1399         public function getDescription()\r
1400         {\r
1401                 return $this->getSetting('bdesc');\r
1402         }\r
1403         \r
1404         /**\r
1405          * Blog::isPublic()\r
1406          * @param       void\r
1407          * @return      integer publlic or not\r
1408          */\r
1409         public function isPublic()\r
1410         {\r
1411                 return $this->getSetting('bpublic');\r
1412         }\r
1413         \r
1414         /**\r
1415          * Blog::emailRequired()\r
1416          * @param       void\r
1417          * @return      integer email is required when posting comment or not\r
1418          */\r
1419         public function emailRequired()\r
1420         {\r
1421                 return $this->getSetting('breqemail');\r
1422         }\r
1423         \r
1424         /**\r
1425          * Blog::getSearchable()\r
1426          * @param       void\r
1427          * @return      integer searchable or not\r
1428          */\r
1429         public function getSearchable()\r
1430         {\r
1431                 return $this->getSetting('bincludesearch');\r
1432         }\r
1433         \r
1434         /**\r
1435          * Blog::getDefaultCategory()\r
1436          * @param       void\r
1437          * @return      ID for category as a default\r
1438          */\r
1439         public function getDefaultCategory()\r
1440         {\r
1441                 return $this->getSetting('bdefcat');\r
1442         }\r
1443         \r
1444         /**\r
1445          * Blog::setPublic()\r
1446          * @param       integer $val    allow comments by non-registered members or not\r
1447          * @return      void\r
1448          */\r
1449         public function setPublic($val)\r
1450         {\r
1451                 $this->setSetting('bpublic', $val);\r
1452                 return;\r
1453         }\r
1454         \r
1455         /**\r
1456          * Blog::setSearchable()\r
1457          * @param       integer $val    searchable from the other blogs or not\r
1458          * @return      void\r
1459          */\r
1460         public function setSearchable($val)\r
1461         {\r
1462                 $this->setSetting('bincludesearch', $val);\r
1463                 return;\r
1464         }\r
1465         \r
1466         /**\r
1467          * Blog::setDescription\r
1468          * @param       string  $val    description for this weblog\r
1469          * @return      void\r
1470          */\r
1471         public function setDescription($val)\r
1472         {\r
1473                 $this->setSetting('bdesc',$val);\r
1474                 return;\r
1475         }\r
1476         \r
1477         /**\r
1478          * Blog::setUpdateFile()\r
1479          * @param       string  $val    name of file to beupdated when weblog is updated\r
1480          * @return      \r
1481          */\r
1482         public function setUpdateFile($val)\r
1483         {\r
1484                 $this->setSetting('bupdate',$val);\r
1485                 return;\r
1486         }\r
1487         \r
1488         /**\r
1489          * Blog::setDefaultSkin()\r
1490          * @param       integer $val    ID for default skin to use when displaying this weblog\r
1491          * @return      void\r
1492          */\r
1493         public function setDefaultSkin($val)\r
1494         {\r
1495                 $this->setSetting('bdefskin', $val);\r
1496                 return;\r
1497         }\r
1498         \r
1499         /**\r
1500          * Blog::setURL()\r
1501          * @param       string  $val    URI for this weblog\r
1502          * @return      \r
1503          */\r
1504         public function setURL($val)\r
1505         {\r
1506                 $this->setSetting('burl', $val);\r
1507                 return;\r
1508         }\r
1509         \r
1510         /**\r
1511          * Blog::setName()\r
1512          * @param       string  $val    name of this weblog\r
1513          * @return      void\r
1514          */\r
1515         public function setName($val)\r
1516         {\r
1517                 $this->setSetting('bname', $val);\r
1518                 return;\r
1519         }\r
1520         \r
1521         /**\r
1522          * Blog::setShortName()\r
1523          * @param       string  $val    short name for this weblog\r
1524          * @return      void\r
1525          */\r
1526         public function setShortName($val)\r
1527         {\r
1528                 $this->setSetting('bshortname', $val);\r
1529                 return;\r
1530         }\r
1531         \r
1532         /**\r
1533          * Blog::setCommentsEnabled()\r
1534          * @param       integer $val    enabling posting comment or not\r
1535          * @return      void\r
1536          */\r
1537         public function setCommentsEnabled($val)\r
1538         {\r
1539                 $this->setSetting('bcomments',$val);\r
1540                 return;\r
1541         }\r
1542         \r
1543         /**\r
1544          * Blog::setMaxComments()\r
1545          * @param       integer $val    maximum number of comments for this weblog\r
1546          * @return      void\r
1547          */\r
1548         public function setMaxComments($val)\r
1549         {\r
1550                 $this->setSetting('bmaxcomments', $val);\r
1551                 return;\r
1552         }\r
1553         \r
1554         /**\r
1555          * Blog::setNotifyAddress()\r
1556          * @param       string  $val    email to be notified if weblog updated\r
1557          * @return      void\r
1558          */\r
1559         public function setNotifyAddress($val)\r
1560         {\r
1561                 $this->setSetting('bnotify', $val);\r
1562                 return;\r
1563         }\r
1564         \r
1565         /**\r
1566          * Blog::setEmailRequired()\r
1567          * @param       string  requiring comments with email or not from non member\r
1568          * @return      void\r
1569          */\r
1570         public function setEmailRequired($val)\r
1571         {\r
1572                 $this->setSetting('breqemail', $val);\r
1573                 return;\r
1574         }\r
1575         \r
1576         /**\r
1577          * Blog::setTimeOffset()\r
1578          * @param       integer $val    time offset\r
1579          * @return      void\r
1580          */\r
1581         public function setTimeOffset($val)\r
1582         {\r
1583                 // check validity of value\r
1584                 // 1. replace , by . (common mistake)\r
1585                 $val = str_replace(',','.',$val);\r
1586                 \r
1587                 // 2. cast to float or int\r
1588                 if ( is_numeric($val) && (i18n::strpos($val, '.5') === (i18n::strlen($val) - 2)) )\r
1589                 {\r
1590                         $val = (float) $val;\r
1591                 }\r
1592                 else\r
1593                 {\r
1594                         $val = (integer) $val;\r
1595                 }\r
1596                 \r
1597                 $this->setSetting('btimeoffset',$val);\r
1598                 return;\r
1599         }\r
1600         \r
1601         /**\r
1602          * Blog::setDefaultCategory()\r
1603          * @param       integer $val    ID for default category for this weblog\r
1604          * @return      \r
1605          */\r
1606         public function setDefaultCategory($val)\r
1607         {\r
1608                 $this->setSetting('bdefcat',$val);\r
1609                 return;\r
1610         }\r
1611         \r
1612         /**\r
1613          * Blog::getSetting()\r
1614          * @param       string  $key    key for setting of this weblog\r
1615          * @return      mixed   value for the setting\r
1616          */\r
1617         public function getSetting($key)\r
1618         {\r
1619                 return $this->settings[$key];\r
1620         }\r
1621         \r
1622         /**\r
1623          * Blog::setSetting()\r
1624          * @param       string  $key    key for setting of this weblog\r
1625          * @param       mixed   $value  value for the key\r
1626          * @return      \r
1627          */\r
1628         public function setSetting($key, $value)\r
1629         {\r
1630                 $this->settings[$key] = $value;\r
1631                 return;\r
1632         }\r
1633         \r
1634         /**\r
1635          * Blog::addTeamMember()\r
1636          * Tries to add a member to the team. \r
1637          * Returns false if the member was already on the team\r
1638          * \r
1639          * @param       integer $memberid       id for member\r
1640          * @param       boolean $admin  super-admin or not\r
1641          * @return      boolean Success/Fail\r
1642          */\r
1643         public function addTeamMember($memberid, $admin)\r
1644         {\r
1645                 global $manager;\r
1646                 \r
1647                 $memberid = intval($memberid);\r
1648                 $admin = intval($admin);\r
1649                 \r
1650                 // check if member is already a member\r
1651                 $tmem =& $manager->getMember($memberid);\r
1652                 \r
1653                 if ( $tmem->isTeamMember($this->blogid) )\r
1654                 {\r
1655                         return 0;\r
1656                 }\r
1657                 \r
1658                 $data = array(\r
1659                         'blog'          => &$this,\r
1660                         'member'        => &$tmem,\r
1661                         'admin'         => &$admin\r
1662                 );\r
1663                 $manager->notify('PreAddTeamMember', $data);\r
1664                 \r
1665                 // add to team\r
1666                 $query = "INSERT INTO %s (TMEMBER, TBLOG, TADMIN) VALUES (%d, %d, %d);";\r
1667                 $query = sprintf($query, sql_table('team'), (integer) $memberid, (integer) $this->blogid, (integer) $admin);\r
1668                 DB::execute($query);\r
1669                 \r
1670                 $data = array(\r
1671                         'blog'          => &$this,\r
1672                         'member'        => &$tmem,\r
1673                         'admin'         =>  $admin\r
1674                 );\r
1675                 $manager->notify('PostAddTeamMember', $data);\r
1676                 \r
1677                 $logMsg = sprintf(_TEAM_ADD_NEWTEAMMEMBER, $tmem->getDisplayName(), $memberid, $this->getName());\r
1678                 ActionLog::add(INFO, $logMsg);\r
1679                 \r
1680                 return 1;\r
1681         }\r
1682         \r
1683         /**\r
1684          * Blog::getID()\r
1685          * @param       void\r
1686          * @return      integer ID for this weblog\r
1687          */\r
1688         public function getID()\r
1689         {\r
1690                 return (integer) $this->blogid;\r
1691         }\r
1692         \r
1693         /**\r
1694          * Checks if a blog with a given shortname exists \r
1695          * Returns true if there is a blog with the given shortname (static)\r
1696          * \r
1697          * @param       string  $name           blog shortname\r
1698          * @return      boolean exists or not\r
1699          */\r
1700         public function exists($name)\r
1701         {\r
1702                 $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bshortname='. DB::quoteValue($name));\r
1703                 return ($r->rowCount() != 0);\r
1704         }\r
1705         \r
1706         /**\r
1707          * Checks if a blog with a given id exists \r
1708          * Returns true if there is a blog with the given ID (static)\r
1709          * \r
1710          * @param       integer $id     ID for searched weblog\r
1711          * @return      boolean exists or not\r
1712          */\r
1713         public function existsID($id)\r
1714         {\r
1715                 $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));\r
1716                 return ($r->rowCount() != 0);\r
1717         }\r
1718         \r
1719         /**\r
1720          * Blog::setFuturePost()\r
1721          * flag there is a future post pending\r
1722          * \r
1723          * @param       void\r
1724          * @return      void\r
1725          */\r
1726         public function setFuturePost()\r
1727         {\r
1728                 $query =  "UPDATE %s SET bfuturepost='1' WHERE bnumber=%d;";\r
1729                 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);\r
1730                 DB::execute($query);\r
1731                 return;\r
1732         }\r
1733         \r
1734         /**\r
1735          * Blog::clearFuturePost()\r
1736          * clear there is a future post pending\r
1737          * \r
1738          * @param       void\r
1739          * @return      void\r
1740          */\r
1741         public function clearFuturePost()\r
1742         {\r
1743                 $query =  "UPDATE %s SET bfuturepost='0' WHERE bnumber=%d;";\r
1744                 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);\r
1745                 DB::execute($query);\r
1746                 return;\r
1747         }\r
1748         \r
1749         /**\r
1750          * Blog::checkJustPosted()\r
1751          * check if we should throw justPosted event \r
1752          * \r
1753          * @param       void\r
1754          * @return      void\r
1755          */\r
1756         public function checkJustPosted()\r
1757         {\r
1758                 global $manager;\r
1759                 \r
1760                 if ( $this->settings['bfuturepost'] == 1 )\r
1761                 {\r
1762                         $query = "SELECT * FROM %s WHERE iposted=0 AND iblog=%d AND itime < NOW();";\r
1763                         $query = sprintf($query, sql_table('item'), (integer) $this->blogid);\r
1764                         \r
1765                         $result = DB::getResult($query);\r
1766                         if ( $result->rowCount() > 0 )\r
1767                         {\r
1768                                 // This $pinged is allow a plugin to tell other hook to the event that a ping is sent already\r
1769                                 // Note that the plugins's calling order is subject to thri order in the plugin list\r
1770                                 $pinged = FALSE;\r
1771                                 $manager->notify('JustPosted', array('blogid' => $this->blogid, 'pinged' => &$pinged));\r
1772                                 \r
1773                                 // clear all expired future posts\r
1774                                 $query = "UPDATE %s SET iposted='1' WHERE iblog=%d AND itime < NOW();";\r
1775                                 $query = spriintf($query, sql_table('item'), (integer) $this->blogid);\r
1776                                 DB::execute($query);\r
1777                                 \r
1778                                 // check to see any pending future post, clear the flag is none\r
1779                                 $query = "SELECT * FROM %s WHERE iposted=0 AND iblog=%d;";\r
1780                                 $query = sprintf($query, sql_table('item'), (integer) $this->blogid);\r
1781                                 \r
1782                                 $result = DB::getResult($query);\r
1783                                 if ( $result->rowCount() == 0 )\r
1784                                 {\r
1785                                         $this->clearFuturePost();\r
1786                                 }\r
1787                         }\r
1788                 }\r
1789                 return;\r
1790         }\r
1791         \r
1792         /**\r
1793          * Blog::readLogFromList()\r
1794          * Shows the given list of items for this blog\r
1795          *\r
1796          * @param       array   $itemarray      array of item numbers to be displayed\r
1797          * @param       string  $template       string representing the template _NAME_ (!)\r
1798          * @param       string  $highlight      contains a query that should be highlighted\r
1799          * @param       boolean $comments       1=show comments 0=don't show comments\r
1800          * @param       boolean $dateheads      1=show dateheads 0=don't show dateheads\r
1801          * @param       boolean $showDrafts     0=do not show drafts 1=show drafts\r
1802          * @param       boolean $showFuture     0=do not show future posts 1=show future posts\r
1803          * @return      integer amount of items shown\r
1804          */\r
1805         public function readLogFromList($itemarray, $template, $highlight = '', $comments = 1, $dateheads = 1,$showDrafts = 0, $showFuture = 0)\r
1806         {\r
1807                 $query = $this->getSqlItemList($itemarray,$showDrafts,$showFuture);\r
1808                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);\r
1809         }\r
1810         \r
1811         /**\r
1812          * Blog::getSqlItemList()\r
1813          * Returns the SQL query used to fill out templates for a list of items\r
1814          * No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
1815          *\r
1816          * @param       array   $itemarray      an array holding the item numbers of the items to be displayed\r
1817          * @param       integer $showDrafts     0=do not show drafts 1=show drafts\r
1818          * @param       integer $showFuture     0=do not show future posts 1=show future posts\r
1819          * @return      string  either a full SQL query, or an empty string\r
1820          */\r
1821         public function getSqlItemList($itemarray,$showDrafts = 0,$showFuture = 0)\r
1822         {\r
1823                 if ( !is_array($itemarray) )\r
1824                 {\r
1825                         return '';\r
1826                 }\r
1827                 \r
1828                 $showDrafts = intval($showDrafts);\r
1829                 $showFuture = intval($showFuture);\r
1830                 $items = array();\r
1831                 \r
1832                 foreach ( $itemarray as $value )\r
1833                 {\r
1834                         if ( intval($value) )\r
1835                         {\r
1836                                 $items[] = intval($value);\r
1837                         }\r
1838                 }\r
1839                 if ( !count($items) )\r
1840                 {\r
1841                         return '';\r
1842                 }\r
1843                 \r
1844                 $i = count($items);\r
1845                 $query = '';\r
1846                 foreach ( $items as $value )\r
1847                 {\r
1848                         $query .= '('\r
1849                                         .       'SELECT'\r
1850                                         .       ' i.inumber as itemid,'\r
1851                                         .       ' i.ititle as title,'\r
1852                                         .       ' i.ibody as body,'\r
1853                                         .       ' m.mname as author,'\r
1854                                         .       ' m.mrealname as authorname,'\r
1855                                         .       ' i.itime,'\r
1856                                         .       ' i.imore as more,'\r
1857                                         .       ' m.mnumber as authorid,'\r
1858                                         .       ' m.memail as authormail,'\r
1859                                         .       ' m.murl as authorurl,'\r
1860                                         .       ' c.cname as category,'\r
1861                                         .       ' i.icat as catid,'\r
1862                                         .       ' i.iclosed as closed';\r
1863                         \r
1864                         $query .= ' FROM '\r
1865                                         . sql_table('item') . ' as i, '\r
1866                                         . sql_table('member') . ' as m, '\r
1867                                         . sql_table('category') . ' as c'\r
1868                                         . ' WHERE'\r
1869                                     .    ' i.iblog='.$this->blogid\r
1870                                    . ' and i.iauthor=m.mnumber'\r
1871                                    . ' and i.icat=c.catid';\r
1872                         \r
1873                         // exclude drafts       \r
1874                         if ( !$showDrafts )\r
1875                         {\r
1876                                 $query .= ' and i.idraft=0';\r
1877                         }\r
1878                         if ( !$showFuture )\r
1879                         {\r
1880                                 // don't show future items\r
1881                                 $query .= ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime());\r
1882                         }\r
1883                         \r
1884                         $query .= ' and i.inumber='.intval($value);\r
1885                         $query .= ')';\r
1886                         $i--;\r
1887                         if ($i) $query .= ' UNION ';\r
1888                 }\r
1889                 \r
1890                 return $query;\r
1891         }\r
1892         \r
1893         /**\r
1894          * Blog::convertBreaks()\r
1895          * Get the the setting for the line break handling\r
1896          * [should be named as getConvertBreaks()]\r
1897          * \r
1898          * @deprecated\r
1899          * @param       void\r
1900          * @return      \r
1901          */\r
1902         public function convertBreaks()\r
1903         {\r
1904                 return $this->getSetting('bconvertbreaks');\r
1905         }\r
1906         \r
1907         /**\r
1908          * Set the the setting for the line break handling\r
1909          * \r
1910          * @deprecated\r
1911          * @param       boolean $val    new value for bconvertbreaks\r
1912          * @return      void\r
1913          */\r
1914         public function setConvertBreaks($val)\r
1915         {\r
1916                 $this->setSetting('bconvertbreaks', $val);\r
1917                 return;\r
1918         }\r
1919 }\r
1920 =======
1921 <?php
1922
1923 /*
1924  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
1925  * Copyright (C) 2002-2012 The Nucleus Group
1926  *
1927  * This program is free software; you can redistribute it and/or
1928  * modify it under the terms of the GNU General Public License
1929  * as published by the Free Software Foundation; either version 2
1930  * of the License, or (at your option) any later version.
1931  * (see nucleus/documentation/index.html#license for more info)
1932  */
1933 /**
1934  * A class representing a blog and containing functions to get that blog shown
1935  * on the screen
1936  *
1937  * @license http://nucleuscms.org/license.txt GNU General Public License
1938  * @copyright Copyright (C) 2002-2009 The Nucleus Group
1939  * @version $Id: BLOG.php 1624 2012-01-09 11:36:20Z sakamocchi $
1940  */
1941
1942 if ( !function_exists('requestVar') ) exit;
1943 require_once dirname(__FILE__) . '/ITEMACTIONS.php';
1944
1945 class Blog
1946 {
1947         // blog id
1948         public $blogid;
1949
1950         // After creating an object of the blog class, contains true if the BLOG object is
1951         // valid (the blog exists)
1952         public $isValid;
1953
1954         // associative array, containing all blogsettings (use the get/set functions instead)
1955         private $settings;
1956         
1957         // ID of currently selected category
1958         private $selectedcatid;
1959
1960         /**
1961          * Blog::_\construct()
1962          * Creates a new BLOG object for the given blog
1963          *
1964          * @param       integer $id     blogid
1965          * @return      void
1966          */
1967         public function __construct($id)
1968         {
1969                 global $catid;
1970                 
1971                 $this->blogid = (integer) $id;
1972                 $this->readSettings();
1973                 $this->setSelectedCategory($catid);
1974                 return;
1975         }
1976         
1977         /**
1978          * Blog::readLog()
1979          * Shows the given amount of items for this blog
1980          *
1981          * @param       string  $template       String representing the template _NAME_ (!)
1982          * @param       integer $amountEntries  amount of entries to show
1983          * @param       integer $startpos       offset from where items should be shown (e.g. 5 = start at fifth item)
1984          * @return      integer amount of items shown
1985          */
1986         public function readLog($template, $amountEntries, $offset = 0, $startpos = 0)
1987         {
1988                 return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);
1989         }
1990         
1991         /**
1992          * Blog::showArchive()
1993          * Shows an archive for a given month
1994          *
1995          * @param       integer $year           year
1996          * @param       integer $month          month
1997          * @param       string  $template       String representing the template name to be used
1998          * @return      void
1999          */
2000         public function showArchive($templatename, $year, $month=0, $day=0)
2001         {
2002                 // create extra where clause for select query
2003                 if ( $day == 0 && $month != 0 )
2004                 {
2005                         $timestamp_start = mktime(0,0,0,$month,1,$year);
2006                         // also works when $month==12
2007                         $timestamp_end = mktime(0,0,0,$month+1,1,$year);
2008                 }
2009                 elseif ( $month == 0 )
2010                 {
2011                         $timestamp_start = mktime(0,0,0,1,1,$year);
2012                         // also works when $month==12
2013                         $timestamp_end = mktime(0,0,0,12,31,$year);
2014                 }
2015                 else
2016                 {
2017                         $timestamp_start = mktime(0,0,0,$month,$day,$year);
2018                         $timestamp_end = mktime(0,0,0,$month,$day+1,$year);
2019                 }
2020                 $extra_query = " and i.itime>=%s and i.itime<%s";
2021                 $extra_query = sprintf($extra_query, DB::formatDateTime($timestamp_start), DB::formatDateTime($timestamp_end));
2022                 
2023                 $this->readLogAmount($templatename,0,$extra_query,'',1,1);
2024                 return;
2025         }
2026         
2027         /**
2028          * Blog::setSelectedCategory()
2029          * Sets the selected category by id (only when category exists)
2030          * 
2031          * @param       integer $catid  ID for category
2032          * @return      void
2033          */
2034         public function setSelectedCategory($catid)
2035         {
2036                 if ( $this->isValidCategory($catid) || (intval($catid) == 0) )
2037                 {
2038                         $this->selectedcatid = intval($catid);
2039                 }
2040                 return;
2041         }
2042         
2043         /**
2044          * Blog::setSelectedCategoryByName()
2045          * Sets the selected category by name
2046          * 
2047          * @param       string  $catname        name of category
2048          * @return      void
2049          */
2050         public function setSelectedCategoryByName($catname)
2051         {
2052                 $this->setSelectedCategory($this->getCategoryIdFromName($catname));
2053                 return;
2054         }
2055         
2056         /**
2057          * Blog::getSelectedCategory()
2058          * Returns the selected category
2059          * 
2060          * @param       void
2061          * @return      integer
2062          */
2063         public function getSelectedCategory()
2064         {
2065                 return $this->selectedcatid;
2066         }
2067         
2068         /**
2069          * Shows the given amount of items for this blog
2070          *
2071          * @param       string  $template               string representing the template _NAME_ (!)
2072          * @param       integer $amountEntries  amount of entries to show (0 = no limit)
2073          * @param       string  $extraQuery             extra conditions to be added to the query
2074          * @param       string  $highlight              contains a query that should be highlighted
2075          * @param       integer $comments               1=show comments 0=don't show comments
2076          * @param       integer $dateheads              1=show dateheads 0=don't show dateheads
2077          * @param       integer $offset                 offset
2078          * @return      integer amount of items shown
2079          */
2080         private function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0)
2081         {
2082                 $query = $this->getSqlBlog($extraQuery);
2083                 
2084                 if ( $amountEntries > 0 )
2085                 {
2086                         // $offset zou moeten worden:
2087                         // (($startpos / $amountentries) + 1) * $offset ... later testen ...
2088                         $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);
2089                 }
2090                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
2091         }
2092         
2093         /**
2094          * Blog::showUsingQuery()
2095          * Do the job for readLogAmmount
2096          * 
2097          * @param       string  $templateName   template name
2098          * @param       string  $query                  string for query
2099          * @param       string  $highlight              string to be highlighted
2100          * @param       integer $comments               the number of comments
2101          * @param       boolean $dateheads              date header is needed or not
2102          * @return      integer the number of rows as a result of mysql query
2103          */
2104         private function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1)
2105         {
2106                 global $CONF, $manager, $currentTemplateName;
2107                 
2108                 $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');
2109                 if ( $lastVisit != 0 )
2110                 {
2111                         $lastVisit = $this->getCorrectTime($lastVisit);
2112                 }
2113                 
2114                 // set templatename as global variable (so plugins can access it)
2115                 $currentTemplateName = $templateName;
2116                 $template =& $manager->getTemplate($templateName);
2117                 
2118                 // create parser object & action handler
2119                 $handler = new ItemActions($this);
2120                 $handler->setTemplate($template);
2121                 $handler->setHighlight($highlight);
2122                 $handler->setLastVisit($lastVisit);
2123                 $handler->setShowComments($comments);
2124                 
2125                 $parser = new Parser($handler);
2126                 
2127                 // execute query
2128                 $items = DB::getResult($query);
2129                 
2130                 // loop over all items
2131                 $old_date = 0;
2132                 foreach ( $items as $item )
2133                 {
2134                         // string timestamp -> unix timestamp
2135                         $item['timestamp'] = strtotime($item['itime']);
2136                         
2137                         // action handler needs to know the item we're handling
2138                         $handler->setCurrentItem($item);
2139                         
2140                         // add date header if needed
2141                         if ( $dateheads )
2142                         {
2143                                 $new_date = date('dFY', $item['timestamp']);
2144                                 if ( $new_date != $old_date )
2145                                 {
2146                                         // unless this is the first time, write date footer
2147                                         $timestamp = $item['timestamp'];
2148                                         if ( $old_date != 0 )
2149                                         {
2150                                                 $oldTS = strtotime($old_date);
2151                                                 $data = array('blog' => &$this, 'timestamp' => $oldTS);
2152                                                 $manager->notify('PreDateFoot', $data);
2153                                                 
2154                                                 if ( !in_array('DATE_FOOTER', $template) || empty($template['DATE_FOOTER']) )
2155                                                 {
2156                                                         $tmp_footer = '';
2157                                                 }
2158                                                 else
2159                                                 {
2160                                                         $tmp_footer = i18n::formatted_datetime($template['DATE_FOOTER'], $oldTS);
2161                                                 }
2162                                                 $parser->parse($tmp_footer);
2163                                                 $manager->notify('PostDateFoot', $data);
2164                                         }
2165                                         
2166                                         $data = array('blog' => &$this, 'timestamp' => $timestamp);
2167                                         $manager->notify('PreDateHead', $data);
2168                                         
2169                                         // note, to use templatvars in the dateheader, the %-characters need to be doubled in
2170                                         // order to be preserved by strftime
2171                                         if ( !in_array('DATE_HEADER', $template) || empty($template['DATE_HEADER']) )
2172                                         {
2173                                                 $tmp_header = '';
2174                                         }
2175                                         else
2176                                         {
2177                                                 $tmp_header = i18n::formatted_datetime($template['DATE_HEADER'], $timestamp);
2178                                         }
2179                                         $parser->parse($tmp_header);
2180                                         $manager->notify('PostDateHead', $data);
2181                                 }
2182                                 $old_date = $new_date;
2183                         }
2184                         
2185                         // parse item
2186                         $parser->parse($template['ITEM_HEADER']);
2187                         $data = array('blog' => &$this, 'item' => &$item);
2188                         $manager->notify('PreItem', $data);
2189                         $parser->parse($template['ITEM']);
2190                         $manager->notify('PostItem', $data);
2191                         $parser->parse($template['ITEM_FOOTER']);
2192                 }
2193                 
2194                 $numrows = $items->rowCount();
2195                 
2196                 // add another date footer if there was at least one item
2197                 if ( ($numrows > 0) && $dateheads )
2198                 {
2199                         $data = array('blog' => &$this, 'timestamp' => strtotime($old_date));
2200                         $manager->notify('PreDateFoot', $data);
2201                         $parser->parse($template['DATE_FOOTER']);
2202                         $manager->notify('PostDateFoot', $data);
2203                 }
2204                 
2205                 $items->closeCursor();
2206                 return $numrows;
2207         }
2208         
2209         /**
2210          * Blog::showOneitem()
2211          * Simplified function for showing only one item
2212          * 
2213          * @param       integer $itemid         ID for item
2214          * @param       array   $template       template for item
2215          * @param       string  $highlight      string for highlight
2216          * @return      integer 1
2217          */
2218         public function showOneitem($itemid, $template, $highlight)
2219         {
2220                 $extraQuery = ' and inumber=' . intval($itemid);
2221                 
2222                 return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);
2223         }
2224         
2225         /**
2226          * Blog::addItem()
2227          * Adds an item to this blog
2228          * 
2229          * @param       integer         $catid  ID for category
2230          * @param       string          $title  ID for 
2231          * @param       string          $body   text for body
2232          * @param       string          $more   text for more
2233          * @param       integer         $blogid ID for blog
2234          * @param       integer         $authorid       ID for author
2235          * @param       timestamp       $timestamp      UNIX timestamp for post
2236          * @param       boolean         $closed opened or closed
2237          * @param       boolean         $draft  draft or not
2238          * @param       boolean         $posted posted or not
2239          * @return      integer ID for added item
2240          */
2241         function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft, $posted='1')
2242         {
2243                 global $manager;
2244                 
2245                 $blogid         = (integer) $blogid;
2246                 $authorid       = (integer) $authorid;
2247                 $title          = $title;
2248                 $body           = $body;
2249                 $more           = $more;
2250                 $catid          = intval($catid);
2251                 
2252                 // convert newlines to <br />
2253                 if ( $this->convertBreaks() )
2254                 {
2255                         $body = addBreaks($body);
2256                         $more = addBreaks($more);
2257                 }
2258
2259                 if ( $closed != '1' )
2260                 {
2261                         $closed = '0';
2262                 }
2263                 if ( $draft != '0' )
2264                 {
2265                         $draft = '1';
2266                 }
2267                 
2268                 if ( !$this->isValidCategory($catid) )
2269                 {
2270                         $catid = $this->getDefaultCategory();
2271                 }
2272                 
2273                 $isFuture = 0;
2274                 if ( $timestamp > $this->getCorrectTime() )
2275                 {
2276                         $isFuture = 1;
2277                 }
2278                 
2279                 $timestamp = date('Y-m-d H:i:s',$timestamp);
2280
2281                 $data = array('title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => $this, 'authorid' => &$authorid, 'timestamp' => &$timestamp, 'closed' => &$closed, 'draft' => &$draft, 'catid' => &$catid);
2282                 $manager->notify('PreAddItem', $data);
2283                 
2284                 $ititle = DB::quoteValue($title);
2285                 $ibody = DB::quoteValue($body);
2286                 $imore = DB::quoteValue($more);
2287                 $timestamp = DB::formatDateTime(strtotime($timestamp));
2288                 
2289                 $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)";
2290                 $query = sprintf($query, sql_table('item'), $ititle, $ibody, $imore, $blogid, $authorid, $timestamp, $closed, $draft, $catid, $posted);
2291                 DB::execute($query);
2292                 $itemid = DB::getInsertId();
2293
2294                 $data = array('itemid' => $itemid);
2295                 $manager->notify('PostAddItem', $data);
2296                 
2297                 if ( !$draft )
2298                 {
2299                         $this->updateUpdateFile();
2300                 }
2301                 // send notification mail
2302                 if ( !$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem() )
2303                 {
2304                         $this->sendNewItemNotification($itemid, $title, $body);
2305                 }
2306                 return $itemid;
2307         }
2308         
2309         /**
2310          * Blog::sendNewItemNotification()
2311          * Send a new item notification to the notification list
2312          * 
2313          * @param       string  $itemid ID of the item
2314          * @param       string  $title  title of the item
2315          * @param       string  $body   body of the item
2316          * @return      void
2317          */
2318         public function sendNewItemNotification($itemid, $title, $body)
2319         {
2320                 global $CONF, $member;
2321                 
2322                 $ascii = Entity::anchor_footnoting($body);
2323                 
2324                 $message = _NOTIFY_NI_MSG . " \n";
2325                 $temp = parse_url($CONF['Self']);
2326                 if ( $temp['scheme'] )
2327                 {
2328                         $message .= Link::create_item_link($itemid) . "\n\n";
2329                 }
2330                 else
2331                 {
2332                         $tempurl = $this->getURL();
2333                         if ( i18n::substr($tempurl, -1) == '/' || i18n::substr($tempurl, -4) == '.php' )
2334                         {
2335                                 $message .= $tempurl . '?itemid=' . $itemid . "\n\n";
2336                         }
2337                         else
2338                         {
2339                                 $message .= $tempurl . '/?itemid=' . $itemid . "\n\n";
2340                         }
2341                 }
2342                 $message .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";
2343                 $message .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";
2344                 $message .= NOTIFICATION::get_mail_footer();
2345                 
2346                 $subject = $this->getName() . ': ' . _NOTIFY_NI_TITLE;
2347                 
2348                 $from = $member->getNotifyFromMailAddress();
2349                 
2350                 NOTIFICATION::mail($this->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());
2351                 return;
2352         }
2353         
2354         /**
2355          * Blog::createNewCategory()
2356          * Creates a new category for this blog
2357          *
2358          * @param       string  $catName                name of the new category. When empty, a name is generated automatically (starting with newcat)
2359          * @param       string  $catDescription description of the new category. Defaults to 'New Category'
2360          * @return      integer ID for new category on success. 0 on failure
2361          */
2362         public function createNewCategory($catName = '', $catDescription = _CREATED_NEW_CATEGORY_DESC)
2363         {
2364                 global $member, $manager;
2365                 
2366                 if ( !$member->blogAdminRights($this->blogid) )
2367                 {
2368                         return 0;
2369                 }
2370                 
2371                 // generate
2372                 if ( $catName == '' )
2373                 {
2374                         $catName = _CREATED_NEW_CATEGORY_NAME;
2375                         $i = 1;
2376                         
2377                         $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);
2378                         while ( $res->rowCount() > 0 )
2379                         {
2380                                 $i++;
2381                                 $res = DB::getResult('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->blogid);
2382                         }
2383                         
2384                         $catName = $catName . $i;
2385                 }
2386                 
2387                 $data = array(
2388                         'blog'                  => &$this,
2389                         'name'                  => &$catName,
2390                         'description'   => $catDescription
2391                 );
2392                 $manager->notify('PreAddCategory', $data);
2393                 
2394                 $query = "INSERT INTO %s (cblog, cname, cdesc) VALUES (%d, %s, %s)";
2395                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, DB::quoteValue($catName), DB::quoteValue($catDescription));
2396                 DB::execute($query);
2397                 $catid = DB::getInsertId();
2398                 
2399                 $data = array(
2400                         'blog'                  => &$this,
2401                         'name'                  => $catName,
2402                         'description'   => $catDescription,
2403                         'catid'                 => $catid
2404                 );
2405                 $manager->notify('PostAddCategory', $data);
2406                 
2407                 return $catid;
2408         }
2409         
2410         /**
2411          * Blog::search()
2412          * Searches all months of this blog for the given query
2413          *
2414          * @param       string  $query                  search query
2415          * @param       array   $template               template to be used (__NAME__ of the template)
2416          * @param       integer $amountMonths   max amount of months to be search (0 = all)
2417          * @param       integer $maxresults             max number of results to show
2418          * @param       integer $startpos               offset
2419          * @return      amount of hits found
2420          */
2421         public function search($query, $template, $amountMonths, $maxresults, $startpos) {
2422                 global $CONF, $manager;
2423                 
2424                 $highlight      = '';
2425                 $sqlquery       = $this->getSqlSearch($query, $amountMonths, $highlight);
2426                 
2427                 if ( $sqlquery == '' )
2428                 {
2429                         // no query -> show everything
2430                         $extraquery = '';
2431                         $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);
2432                 }
2433                 else
2434                 {
2435                         // add LIMIT to query (to split search results into pages)
2436                         if ( intval($maxresults > 0) )
2437                         {
2438                                 $sqlquery .= ' LIMIT ' . intval($startpos) . ',' . intval($maxresults);
2439                         }
2440                         
2441                         // show results
2442                         $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);
2443                         
2444                         // when no results were found, show a message
2445                         if ( $amountfound == 0 )
2446                         {
2447                                 $template =& $manager->getTemplate($template);
2448                                 $vars = array(
2449                                         'query'         => Entity::hsc($query),
2450                                         'blogid'        => $this->blogid
2451                                 );
2452                                 echo Template::fill($template['SEARCH_NOTHINGFOUND'], $vars);
2453                         }
2454                 }
2455                 return $amountfound;
2456         }
2457         
2458         /**
2459          * Blog::getSqlSearch()
2460          * Returns an SQL query to use for a search query
2461          * No LIMIT clause is added. (caller should add this if multiple pages are requested)
2462          *
2463          * @param       string  $query                  search query
2464          * @param       integer $amountMonths   amount of months to search back. Default = 0 = unlimited
2465          * @param       string  $mode                   either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
2466          * @return      string  $highlight              words to highlight (out parameter)
2467          * @return      string  either a full SQL query, or an empty string (if querystring empty)
2468          */
2469         public function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')
2470         {
2471                 $searchclass = new Search($query);
2472                 
2473                 $highlight       = $searchclass->inclusive;
2474                 
2475                 // if querystring is empty, return empty string
2476                 if ( $searchclass->inclusive == '' )
2477                 {
2478                         return '';
2479                 }
2480                 
2481                 $where  = $searchclass->boolean_sql_where('ititle,ibody,imore');
2482                 $select = $searchclass->boolean_sql_select('ititle,ibody,imore');
2483                 
2484                 // get list of blogs to search
2485                 $blogs          = $searchclass->blogs;  // array containing blogs that always need to be included
2486                 $blogs[]        = $this->blogid;                // also search current blog (duh)
2487                 $blogs          = array_unique($blogs); // remove duplicates
2488                 $selectblogs = '';
2489                 if ( count($blogs) > 0 )
2490                 {
2491                         $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';
2492                 }
2493                 
2494                 if ( $mode == '' )
2495                 {
2496                         $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,
2497                                 m.mname as author, m.mrealname as authorname, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl,
2498                                 c.cname as category';
2499                         
2500                         if ( $select )
2501                         {
2502                                 $query .= ', '.$select. ' as score ';
2503                         }
2504                 }
2505                 else
2506                 {
2507                         $query = 'SELECT COUNT(*) as result ';
2508                 }
2509                 
2510                 $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
2511                                 . ' WHERE i.iauthor=m.mnumber'
2512                                 . ' and i.icat=c.catid'
2513                                 // exclude drafts
2514                                 . ' and i.idraft=0'
2515                                 . $selectblogs
2516                                         // don't show future items
2517                                 . ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime())
2518                                 . ' and '.$where;
2519                 
2520                 // take into account amount of months to search
2521                 if ( $amountMonths > 0 )
2522                 {
2523                         $localtime = getdate($this->getCorrectTime());
2524                         $timestamp_start = mktime(0,0,0,$localtime['mon'] - $amountMonths,1,$localtime['year']);
2525                         $query .= ' and i.itime>' . DB::formatDateTime($timestamp_start);
2526                 }
2527                 
2528                 if ( $mode == '' )
2529                 {
2530                         if ( $select )
2531                         {
2532                                 $query .= ' ORDER BY score DESC';
2533                         }
2534                         else
2535                         {
2536                                 $query .= ' ORDER BY i.itime DESC ';
2537                         }
2538                 }
2539                 
2540                 return $query;
2541         }
2542         
2543         /**
2544          * Blog::getSqlBlog()
2545          * Returns the SQL query that's normally used to display the blog items on the index type skins
2546          * No LIMIT clause is added. (caller should add this if multiple pages are requested)
2547          *
2548          * @param       string  $extraQuery     extra query string
2549          * @param       string  $mode           either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
2550          * @return      string  either a full SQL query, or an empty string
2551          */
2552         public function getSqlBlog($extraQuery, $mode = '')
2553         {
2554                 if ( $mode == '' )
2555                 {
2556                         $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author,
2557                                 m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail,
2558                                 m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';
2559                 }
2560                 else
2561                 {
2562                         $query = 'SELECT COUNT(*) as result ';
2563                 }
2564                 
2565                 $query  .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'
2566                                 . ' WHERE i.iblog='.$this->blogid
2567                                 . ' and i.iauthor=m.mnumber'
2568                                 . ' and i.icat=c.catid'
2569                                 . ' and i.idraft=0' // exclude drafts
2570                                 . ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime()); // don't show future items
2571                 
2572                 if ( $this->selectedcatid )
2573                 {
2574                         $query .= ' and i.icat=' . $this->selectedcatid . ' ';
2575                 }
2576                 
2577                 $query .= $extraQuery;
2578                 
2579                 if ( $mode == '' )
2580                 {
2581                         $query .= ' ORDER BY i.itime DESC';
2582                 }
2583                 return $query;
2584         }
2585         
2586         /**
2587          * Blog::showArchiveList()
2588          * Shows the archivelist using the given template
2589          * 
2590          * @param       string  $template       template name
2591          * @param       string  $mode   year/month/day
2592          * @param       integer $limit  limit of record count
2593          * @return      void
2594          */
2595         public function showArchiveList($template, $mode = 'month', $limit = 0)
2596         {
2597                 global $CONF, $catid, $manager;
2598                 
2599                 if ( !isset ($linkparams) )
2600                 {
2601                         $linkparams = array();
2602                 }
2603                 
2604                 if ( $catid )
2605                 {
2606                         $linkparams = array('catid' => $catid);
2607                 }
2608                 
2609                 $template =& $manager->getTemplate($template);
2610                 $listitem['blogid'] = $this->blogid;
2611                 
2612                 if ( !array_key_exists('ARCHIVELIST_HEADER', $template) || !$template['ARCHIVELIST_HEADER'] )
2613                 {
2614                         $tplt = '';
2615                 }
2616                 else
2617                 {
2618                         $tplt = $template['ARCHIVELIST_HEADER'];
2619                 }
2620                 
2621                 echo Template::fill($tplt, $listitem);
2622                 
2623                 $query = 'SELECT itime, SUBSTRING(itime,1,4) AS Year, SUBSTRING(itime,6,2) AS Month, SUBSTRING(itime,9,2) AS Day'
2624                                 . ' FROM '.sql_table('item')
2625                                 . ' WHERE iblog=' . $this->blogid
2626                                 . ' AND itime <=' . DB::formatDateTime($this->getCorrectTime()) // don't show future items!
2627                                 . ' AND idraft=0'; // don't show draft items
2628                 
2629                 if ( $catid )
2630                 {
2631                         $query .= ' and icat=' . intval($catid);
2632                 }
2633                 
2634                 $query .= ' GROUP BY Year';
2635                 if ( $mode == 'month' || $mode == 'day' )
2636                 {
2637                         $query .= ', Month';
2638                 }
2639                 if ( $mode == 'day' )
2640                 {
2641                         $query .= ', Day';
2642                 }
2643                 
2644                 $query .= ' ORDER BY itime DESC';
2645                 
2646                 if ( $limit > 0 )
2647                 {
2648                         $query .= ' LIMIT ' . intval($limit);
2649                 }
2650                 
2651                 $res = DB::getResult($query);
2652                 foreach ( $res as $current )
2653                 {
2654                         /* string time -> unix timestamp */
2655                         $current['itime'] = strtotime($current['itime']);
2656                         
2657                         if ( $mode == 'day' )
2658                         {
2659                                 $archivedate = date('Y-m-d',$current['itime']);
2660                                 $archive['day'] = date('d',$current['itime']);
2661                                 $listitem['day'] = date('d',$current['itime']);
2662                                 $listitem['month'] = date('m',$current['itime']);
2663                                 $archive['month'] = $listitem['month'];
2664                         }
2665                         elseif ( $mode == 'year' )
2666                         {
2667                                 $archivedate = date('Y',$current['itime']);
2668                                 $listitem['day'] = '';
2669                                 $listitem['month'] = '';
2670                                 $archive['day'] = '';
2671                                 $archive['month'] = '';
2672                         }
2673                         else
2674                         {
2675                                 $archivedate = date('Y-m',$current['itime']);
2676                                 $listitem['month'] = date('m',$current['itime']);
2677                                 $archive['month'] = $listitem['month'];
2678                                 $listitem['day'] = '';
2679                                 $archive['day'] = '';
2680                         }
2681                         
2682                         $listitem['year'] = date('Y',$current['itime']);
2683                         $archive['year'] = $listitem['year'];
2684                         $listitem['archivelink'] = Link::create_archive_link($this->blogid,$archivedate,$linkparams);
2685
2686                         $data = array('listitem' => &$listitem);
2687                         $manager->notify('PreArchiveListItem', $data);
2688                         
2689                         $temp = Template::fill($template['ARCHIVELIST_LISTITEM'],$listitem);
2690                         echo i18n::formatted_datetime($temp, $current['itime']);
2691                         return;
2692                 }
2693                 
2694                 $res->closeCursor();
2695                 
2696                 if ( !array_key_exists('ARCHIVELIST_FOOTER', $template) || !$template['ARCHIVELIST_FOOTER'] )
2697                 {
2698                         $tplt = '';
2699                 }
2700                 else
2701                 {
2702                         $tplt = $template['ARCHIVELIST_FOOTER'];
2703                 }
2704                 
2705                 echo Template::fill($tplt, $listitem);
2706                 return;
2707         }
2708         
2709         /**
2710          * Blog::showCategoryList()
2711          * Shows the list of categories using a given template
2712          * 
2713          * @param       string  $template       Template Name
2714          * @return      void
2715          */
2716         public function showCategoryList($template)
2717         {
2718                 global $CONF, $archive, $archivelist, $manager;
2719                 
2720                 /*
2721                  * determine arguments next to catids
2722                  * I guess this can be done in a better way, but it works
2723                  */
2724                 $linkparams = array();
2725                 if ( $archive )
2726                 {
2727                         $blogurl = Link::create_archive_link($this->blogid, $archive, '');
2728                         $linkparams['blogid'] = $this->blogid;
2729                         $linkparams['archive'] = $archive;
2730                 }
2731                 else if ( $archivelist )
2732                 {
2733                         $blogurl = Link::create_archivelist_link($this->blogid, '');
2734                         $linkparams['archivelist'] = $archivelist;
2735                 }
2736                 else
2737                 {
2738                         $blogurl = Link::create_blogid_link($this->blogid, '');
2739                         $linkparams['blogid'] = $this->blogid;
2740                 }
2741                 
2742                 $template =& $manager->getTemplate($template);
2743                 
2744                 //: Change: Set nocatselected variable
2745                 if ( $this->selectedcatid )
2746                 {
2747                         $nocatselected = 'no';
2748                 }
2749                 else
2750                 {
2751                         $nocatselected = 'yes';
2752                 } 
2753                 
2754                 $args = array(
2755                         'blogid'        => $this->blogid,
2756                         'blogurl'       => $blogurl,
2757                         'self'          => $CONF['Self'],
2758                         'catiscurrent'  => $nocatselected, // Change: Set catiscurrent template variable for header
2759                         'currentcat'    => $nocatselected 
2760                 );
2761                 
2762                 /* output header of category list item */
2763                 if ( !array_key_exists('CATLIST_HEADER', $template) || empty($template['CATLIST_HEADER']) )
2764                 {
2765                         echo Template::fill(NULL, $args);
2766                 }
2767                 else
2768                 {
2769                         echo Template::fill($template['CATLIST_HEADER'], $args);
2770                 }
2771                 
2772                 $query = "SELECT catid, cdesc as catdesc, cname as catname FROM %s WHERE cblog=%d ORDER BY cname ASC;";
2773                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid);
2774                 $res = DB::getResult($query);
2775                 
2776                 foreach ( $res as $row )
2777                 {
2778                         $args = array(
2779                                 'catid' => $row['catid'],
2780                                 'name'  => $row['catname'],
2781                                 'extra' => $linkparams
2782                         );
2783                         
2784                         $row['blogid']          = $this->blogid;
2785                         $row['blogurl']         = $blogurl;
2786                         $row['catlink']         = Link::create_link('category', $args);
2787                         $row['self']            = $CONF['Self'];
2788                         
2789                         // this gives catiscurrent = no when no category is selected.
2790                         $row['catiscurrent'] = 'no';
2791                         $row['currentcat'] = 'no';
2792                         
2793                         if ( $this->selectedcatid )
2794                         {
2795                                 if ( $this->selectedcatid == $row['catid'] )
2796                                 {
2797                                         $row['catiscurrent']    = 'yes';
2798                                         $row['currentcat']              = 'yes';
2799                                 }
2800                         }
2801                         else
2802                         {
2803                                 global $itemid;
2804                                 if ( (integer) $itemid && $manager->existsItem((integer) $itemid, 0, 0) )
2805                                 {
2806                                         $iobj   =& $manager->getItem($itemid, 0, 0);
2807                                         $cid    = $iobj['catid'];
2808                                         
2809                                         if ( $cid == $row['catid'] )
2810                                         {
2811                                                 $row['catiscurrent']    = 'yes';
2812                                                 $row['currentcat']              = 'yes';
2813                                         }
2814                                 }
2815                         }
2816
2817                         $data = array('listitem' => &$row);
2818                         $manager->notify('PreCategoryListItem', $data);
2819                         
2820                         if ( !array_key_exists('CATLIST_LISTITEM', $template) || empty($template['CATLIST_LISTITEM']))
2821                         {
2822                                 echo Template::fill(NULL, $row);
2823                         }
2824                         else
2825                         {
2826                                 echo Template::fill($template['CATLIST_LISTITEM'], $row);
2827                         }
2828                 }
2829                 
2830                 $res->closeCursor();
2831                 
2832                 $args = array(
2833                         'blogid'                => $this->blogid,
2834                         'blogurl'               => $blogurl,
2835                         'self'                  => $CONF['Self'],
2836                         'catiscurrent'  => $nocatselected, //: Change: Set catiscurrent template variable for footer
2837                         'currentcat'    => $nocatselected
2838                 );
2839                 
2840                 if ( !array_key_exists('CATLIST_FOOTER', $template) || empty($template['CATLIST_FOOTER']))
2841                 {
2842                         echo Template::fill(NULL, $args);
2843                 }
2844                 else
2845                 {
2846                         echo Template::fill($template['CATLIST_FOOTER'], $args);
2847                 }
2848                 
2849                 return;
2850         }
2851         
2852         /**
2853          * Blog::showBlogList()
2854          * Shows a list of all blogs in the system using a given template
2855          * ordered by number, name, shortname or description
2856          * in ascending or descending order
2857          * 
2858          * @param       string  $template       tempalte name
2859          * @param       string  $bnametype      bname/bshortname
2860          * @param       string  $orderby        string for 'ORDER BY' SQL
2861          * @param       string  $direction      ASC/DESC
2862          * @return      void
2863          */
2864         static public function showBlogList($template, $bnametype, $orderby, $direction)
2865         {
2866                 global $CONF, $manager;
2867                 
2868                 switch ( $orderby )
2869                 {
2870                         case 'number':
2871                                 $orderby='bnumber';
2872                                 break;
2873                         case 'name':
2874                                 $orderby='bname';
2875                                 break;
2876                         case 'shortname':
2877                                 $orderby='bshortname';
2878                                 break;
2879                         case 'description':
2880                                 $orderby='bdesc';
2881                                 break;
2882                         default:
2883                                 $orderby='bnumber';
2884                                 break;
2885                 }
2886                 
2887                 $direction=strtolower($direction);
2888                 switch ( $direction )
2889                 {
2890                         case 'asc':
2891                                 $direction='ASC';
2892                                 break;
2893                         case 'desc':
2894                                 $direction='DESC';
2895                                 break;
2896                         default:
2897                                 $direction='ASC';
2898                                 break;
2899                 }
2900                 
2901                 $template =& $manager->getTemplate($template);
2902                 
2903                 if ( array_key_exists('BLOGLIST_HEADER', $template) && !empty($template['BLOGLIST_HEADER']) )
2904                 {
2905                         $vars = array(
2906                                 'sitename'      => $CONF['SiteName'],
2907                                 'siteurl'       => $CONF['IndexURL']
2908                         );
2909                         
2910                         echo Template::fill($template['BLOGLIST_HEADER'], $vars);
2911                 }
2912                 
2913                 if ( array_key_exists('BLOGLIST_LISTITEM', $template) && !empty($template['BLOGLIST_LISTITEM']) )
2914                 {
2915                         $query = 'SELECT bnumber, bname, bshortname, bdesc, burl FROM '.sql_table('blog').' ORDER BY '.$orderby.' '.$direction;
2916                         $res = DB::getResult($query);
2917                         
2918                         foreach ( $res as $row )
2919                         {
2920                                 $list = array();
2921                                 $list['bloglink'] = Link::create_blogid_link($row['bnumber']);
2922                                 $list['blogdesc'] = $row['bdesc'];
2923                                 $list['blogurl'] = $row['burl'];
2924                                 
2925                                 if ( $bnametype == 'shortname' )
2926                                 {
2927                                         $list['blogname'] = $row['bshortname'];
2928                                 }
2929                                 else
2930                                 {
2931                                         /* all other cases */
2932                                         $list['blogname'] = $row['bname'];
2933                                 }
2934
2935                                 $data = array('listitem' => &$list);
2936                                 $manager->notify('PreBlogListItem', $data);
2937                                 
2938                                 echo Template::fill($template['BLOGLIST_LISTITEM'], $list);
2939                         }
2940                         
2941                         $res->closeCursor();
2942                 }
2943                 
2944                 
2945                 if ( array_key_exists('BLOGLIST_FOOTER', $template) && !empty($template['BLOGLIST_FOOTER']) )
2946                 {
2947                         $vars = array(
2948                                 'sitename' => $CONF['SiteName'],
2949                                 'siteurl' => $CONF['IndexURL']
2950                         );
2951                         echo Template::fill($template['BLOGLIST_FOOTER']);
2952                 }
2953                 return;
2954         }
2955         
2956         /**
2957          * Blog::readSettings()
2958          * Read the blog settings
2959          * 
2960          * @param       void
2961          * @return      void
2962          */
2963         public function readSettings()
2964         {
2965                 $query =  'SELECT * FROM %s WHERE bnumber=%d;';
2966                 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);
2967                 $res = DB::getResult($query);
2968                 
2969                 $this->isValid = ($res->rowCount() > 0);
2970                 if ( $this->isValid )
2971                 {
2972                         $this->settings = $res->fetch(PDO::FETCH_ASSOC);
2973                 }
2974                 return;
2975         }
2976         
2977         /**
2978          * Blog::writeSettings()
2979          * Write the blog settings
2980          */
2981         public function writeSettings()
2982         {
2983                 // (can't use floatval since not available prior to PHP 4.2)
2984                 $offset = $this->getTimeOffset();
2985                 if ( !is_float($offset) )
2986                 {
2987                         $offset = (integer) $offset;
2988                 }
2989                 
2990                 $query =  'UPDATE '.sql_table('blog')
2991                            . ' SET bname=' . DB::quoteValue($this->getName()) . ','
2992                            . '     bshortname='. DB::quoteValue($this->getShortName()) . ','
2993                            . '     bcomments='. intval($this->commentsEnabled()) . ','
2994                            . '     bmaxcomments=' . intval($this->getMaxComments()) . ','
2995                            . '     btimeoffset=' . $offset . ','
2996                            . '     bpublic=' . intval($this->isPublic()) . ','
2997                            . '     breqemail=' . intval($this->emailRequired()) . ','
2998                            . '     bconvertbreaks=' . intval($this->convertBreaks()) . ','
2999                            . '     ballowpast=' . intval($this->allowPastPosting()) . ','
3000                            . '     bnotify=' . DB::quoteValue($this->getNotifyAddress()) . ','
3001                            . '     bnotifytype=' . intval($this->getNotifyType()) . ','
3002                            . '     burl=' . DB::quoteValue($this->getURL()) . ','
3003                            . '     bupdate=' . DB::quoteValue($this->getUpdateFile()) . ','
3004                            . '     bdesc=' . DB::quoteValue($this->getDescription()) . ','
3005                            . '     bdefcat=' . intval($this->getDefaultCategory()) . ','
3006                            . '     bdefskin=' . intval($this->getDefaultSkin()) . ','
3007                            . '     bincludesearch=' . intval($this->getSearchable())
3008                            . ' WHERE bnumber=' . intval($this->blogid);
3009                 DB::execute($query);
3010                 return;
3011         }
3012         
3013         /**
3014          * Blog::updateUpdatefile()
3015          * Update the update file if requested
3016          * 
3017          * @param       void
3018          * @return      void
3019          */
3020         public function updateUpdatefile()
3021         {
3022                 if ( $this->getUpdateFile() )
3023                 {
3024                         $f_update = fopen($this->getUpdateFile(), 'w');
3025                         fputs($f_update,$this->getCorrectTime());
3026                         fclose($f_update);
3027                 }
3028                 return;
3029         }
3030         
3031         /**
3032          * Blog::isValidCategory()
3033          * Check if a category with a given catid is valid
3034          * 
3035          * @param       integer $catid  ID for category
3036          * @return      boolean exists or not
3037          */
3038         public function isValidCategory($catid)
3039         {
3040                 $query = 'SELECT * FROM %s WHERE cblog=%d and catid=%d;';
3041                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);
3042                 $res = DB::getResult($query);
3043                 return ($res->rowCount() != 0);
3044         }
3045         
3046         /**
3047          * Blog::getCategoryName()
3048          * Get the category name for a given catid
3049          * 
3050          * @param       integer $catid  ID for category
3051          * @return      string  name of category
3052          */
3053         public function getCategoryName($catid)
3054         {
3055                 $query = 'SELECT cname FROM %s WHERE cblog=%d and catid=%d;';
3056                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);
3057                 $res = DB::getValue($query);
3058                 return $res;
3059         }
3060         
3061         /**
3062          * Blog::getCategoryDesc()
3063          * Get the category description for a given catid
3064          * 
3065          * @param $catid
3066          *      category id
3067          */
3068         public function getCategoryDesc($catid)
3069         {
3070                 $query = 'SELECT cdesc FROM %s WHERE cblog=%d and catid=%d;';
3071                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, (integer) $catid);
3072                 $res = DB::getValue($query);
3073                 return $res;
3074         }
3075         
3076         /**
3077          * Blog::getCategoryIdFromName
3078          * Get the category id for a given category name
3079          * 
3080          * @param       string  $name   category name
3081          * @return      ID for category
3082          */
3083         public function getCategoryIdFromName($name)
3084         {
3085                 $query = 'SELECT catid FROM %s WHERE cblog=%d and cname=%s;';
3086                 $query = sprintf($query, sql_table('category'), (integer) $this->blogid, DB::quoteValue($name));
3087                 
3088                 $res = DB::getValue();
3089                 if ( !$res )
3090                 {
3091                         return $this->getDefaultCategory();
3092                 }
3093                 return $res;
3094         }
3095         
3096         /**
3097          * Blog::insertJavaScriptInfo()
3098          * Insert a javascript that includes information about the settings
3099          * of an author:  ConvertBreaks, MediaUrl and AuthorId
3100          * 
3101          * @param       $authorid       id of the author
3102          */
3103         public function insertJavaScriptInfo($authorid = '')
3104         {
3105                 global $member, $CONF;
3106                 
3107                 if ( $authorid == '' )
3108                 {
3109                         $authorid = $member->getID();
3110                 }
3111                 
3112                 echo "<script type=\"text/javascript\">\n";
3113                 
3114                 if ( !$this->convertBreaks() )
3115                 {
3116                         echo "setConvertBreaks(false);\n";
3117                 }
3118                 else
3119                 {
3120                         echo "setConvertBreaks(true);\n";
3121                 }
3122                 echo "setMediaUrl('{$CONF['MediaURL']}');\n";
3123                 echo "setAuthorId('{$authorid}');\n";
3124                 echo "</script>\n";
3125                 return;
3126         }
3127         
3128         /**
3129          * Blog::setAllowPastPosting()
3130          * Set the the setting for allowing to publish postings in the past
3131          * 
3132          * @param       boolean $val    new value for ballowpast
3133          * @return      void
3134          */
3135         public function setAllowPastPosting($val)
3136         {
3137                 $this->setSetting('ballowpast', $val);
3138                 return;
3139         }
3140         
3141         /**
3142          * Blog::allowPastPosting()
3143          * Get the the setting if it is allowed to publish postings in the past
3144          * [should be named as getAllowPastPosting()]
3145          * 
3146          * @param       void
3147          * @return      boolean
3148          */
3149         public function allowPastPosting()
3150         {
3151                 return $this->getSetting('ballowpast');
3152         }
3153         
3154         /**
3155          * Blog::getCorrectTime()
3156          * 
3157          * @param       integer $t
3158          * @return      integer
3159          */
3160         public function getCorrectTime($t=0)
3161         {
3162                 if ( $t == 0 )
3163                 {
3164                         $t = time();
3165                 }
3166                 return ($t + 3600 * $this->getTimeOffset());
3167         }
3168         
3169         /**
3170          * Blog::getName()
3171          * 
3172          * @param       void
3173          * @return      string name of this weblog
3174          */
3175         public function getName()
3176         {
3177                 return $this->getSetting('bname');
3178         }
3179         
3180         /**
3181          * Blog::getShortName()
3182          * 
3183          * @param       void
3184          * @return      string  short name of this weblog
3185          */
3186         public function getShortName()
3187         {
3188                 return $this->getSetting('bshortname');
3189         }
3190         
3191         /**
3192          * Blog::getMaxComments()
3193          * 
3194          * @param       void
3195          * @return      integer maximum number of comments
3196          */
3197         public function getMaxComments()
3198         {
3199                 return $this->getSetting('bmaxcomments');
3200         }
3201         
3202         /**
3203          * Blog::getNotifyAddress()
3204          * 
3205          * @param       void
3206          * @return      string  mail address for notifying
3207          */
3208         public function getNotifyAddress()
3209         {
3210                 return $this->getSetting('bnotify');
3211         }
3212         
3213         /**
3214          * Blog::getNotifyType()
3215          * 
3216          * @param       void
3217          * @return      integer notifycation type
3218          */
3219         public function getNotifyType()
3220         {
3221                 return $this->getSetting('bnotifytype');
3222         }
3223         
3224         /**
3225          * Blog::notifyOnComment()
3226          * 
3227          * @param       void
3228          * @return      boolean
3229          */
3230         public function notifyOnComment()
3231         {
3232                 $n = $this->getNotifyType();
3233                 return (($n != 0) && (($n % 3) == 0));
3234         }
3235         
3236         /**
3237          * Blog::notifyOnVote()
3238          * 
3239          * @param       void
3240          * @return      boolean
3241          */
3242         public function notifyOnVote()
3243         {
3244                 $n = $this->getNotifyType();
3245                 return (($n != 0) && (($n % 5) == 0));
3246         }
3247         
3248         /**
3249          * Blog::notifyOnNewItem()
3250          * 
3251          * @param       void
3252          * @return      boolean
3253          */
3254         public function notifyOnNewItem()
3255         {
3256                 $n = $this->getNotifyType();
3257                 return (($n != 0) && (($n % 7) == 0));
3258         }
3259         
3260         /**
3261          * Blog::setNotifyType()
3262          * 
3263          * @param       integer $val
3264          * @return      void
3265          */
3266         public function setNotifyType($val)
3267         {
3268                 $this->setSetting('bnotifytype',$val);
3269                 return;
3270         }
3271         
3272         /**
3273          * Blog::getTimeOffset()
3274          * @param       void
3275          * @return      
3276          */
3277         public function getTimeOffset()
3278         {
3279                 return $this->getSetting('btimeoffset');
3280         }
3281         
3282         /**
3283          * Blog::commentsEnabled()
3284          * @param       void
3285          * @return      integer enabled or not
3286          */
3287         public function commentsEnabled()
3288         {
3289                 return $this->getSetting('bcomments');
3290         }
3291         
3292         /**
3293          * Blog::getURL()
3294          * @param       void
3295          * @return      string  URI for this weblog
3296          */
3297         public function getURL()
3298         {
3299                 return $this->getSetting('burl');
3300         }
3301         
3302         /**
3303          * Blog::getDefaultSkin()
3304          * @param       void
3305          * @return      name of skin as default for this weblog
3306          */
3307         public function getDefaultSkin()
3308         {
3309                 return $this->getSetting('bdefskin');
3310         }
3311         
3312         /**
3313          * Blog::getUpdateFile()
3314          * @param       void
3315          * @return      string  name of file to be updated when weblog is updated
3316          */
3317         public function getUpdateFile()
3318         {
3319                 return $this->getSetting('bupdate');
3320         }
3321         
3322         /**
3323          * Blog::getDescription()
3324          * @param       void
3325          * @return      string  description for this weblog
3326          */
3327         public function getDescription()
3328         {
3329                 return $this->getSetting('bdesc');
3330         }
3331         
3332         /**
3333          * Blog::isPublic()
3334          * @param       void
3335          * @return      integer publlic or not
3336          */
3337         public function isPublic()
3338         {
3339                 return $this->getSetting('bpublic');
3340         }
3341         
3342         /**
3343          * Blog::emailRequired()
3344          * @param       void
3345          * @return      integer email is required when posting comment or not
3346          */
3347         public function emailRequired()
3348         {
3349                 return $this->getSetting('breqemail');
3350         }
3351         
3352         /**
3353          * Blog::getSearchable()
3354          * @param       void
3355          * @return      integer searchable or not
3356          */
3357         public function getSearchable()
3358         {
3359                 return $this->getSetting('bincludesearch');
3360         }
3361         
3362         /**
3363          * Blog::getDefaultCategory()
3364          * @param       void
3365          * @return      ID for category as a default
3366          */
3367         public function getDefaultCategory()
3368         {
3369                 return $this->getSetting('bdefcat');
3370         }
3371         
3372         /**
3373          * Blog::setPublic()
3374          * @param       integer $val    allow comments by non-registered members or not
3375          * @return      void
3376          */
3377         public function setPublic($val)
3378         {
3379                 $this->setSetting('bpublic', $val);
3380                 return;
3381         }
3382         
3383         /**
3384          * Blog::setSearchable()
3385          * @param       integer $val    searchable from the other blogs or not
3386          * @return      void
3387          */
3388         public function setSearchable($val)
3389         {
3390                 $this->setSetting('bincludesearch', $val);
3391                 return;
3392         }
3393         
3394         /**
3395          * Blog::setDescription
3396          * @param       string  $val    description for this weblog
3397          * @return      void
3398          */
3399         public function setDescription($val)
3400         {
3401                 $this->setSetting('bdesc',$val);
3402                 return;
3403         }
3404         
3405         /**
3406          * Blog::setUpdateFile()
3407          * @param       string  $val    name of file to beupdated when weblog is updated
3408          * @return      
3409          */
3410         public function setUpdateFile($val)
3411         {
3412                 $this->setSetting('bupdate',$val);
3413                 return;
3414         }
3415         
3416         /**
3417          * Blog::setDefaultSkin()
3418          * @param       integer $val    ID for default skin to use when displaying this weblog
3419          * @return      void
3420          */
3421         public function setDefaultSkin($val)
3422         {
3423                 $this->setSetting('bdefskin', $val);
3424                 return;
3425         }
3426         
3427         /**
3428          * Blog::setURL()
3429          * @param       string  $val    URI for this weblog
3430          * @return      
3431          */
3432         public function setURL($val)
3433         {
3434                 $this->setSetting('burl', $val);
3435                 return;
3436         }
3437         
3438         /**
3439          * Blog::setName()
3440          * @param       string  $val    name of this weblog
3441          * @return      void
3442          */
3443         public function setName($val)
3444         {
3445                 $this->setSetting('bname', $val);
3446                 return;
3447         }
3448         
3449         /**
3450          * Blog::setShortName()
3451          * @param       string  $val    short name for this weblog
3452          * @return      void
3453          */
3454         public function setShortName($val)
3455         {
3456                 $this->setSetting('bshortname', $val);
3457                 return;
3458         }
3459         
3460         /**
3461          * Blog::setCommentsEnabled()
3462          * @param       integer $val    enabling posting comment or not
3463          * @return      void
3464          */
3465         public function setCommentsEnabled($val)
3466         {
3467                 $this->setSetting('bcomments',$val);
3468                 return;
3469         }
3470         
3471         /**
3472          * Blog::setMaxComments()
3473          * @param       integer $val    maximum number of comments for this weblog
3474          * @return      void
3475          */
3476         public function setMaxComments($val)
3477         {
3478                 $this->setSetting('bmaxcomments', $val);
3479                 return;
3480         }
3481         
3482         /**
3483          * Blog::setNotifyAddress()
3484          * @param       string  $val    email to be notified if weblog updated
3485          * @return      void
3486          */
3487         public function setNotifyAddress($val)
3488         {
3489                 $this->setSetting('bnotify', $val);
3490                 return;
3491         }
3492         
3493         /**
3494          * Blog::setEmailRequired()
3495          * @param       string  requiring comments with email or not from non member
3496          * @return      void
3497          */
3498         public function setEmailRequired($val)
3499         {
3500                 $this->setSetting('breqemail', $val);
3501                 return;
3502         }
3503         
3504         /**
3505          * Blog::setTimeOffset()
3506          * @param       integer $val    time offset
3507          * @return      void
3508          */
3509         public function setTimeOffset($val)
3510         {
3511                 // check validity of value
3512                 // 1. replace , by . (common mistake)
3513                 $val = str_replace(',','.',$val);
3514                 
3515                 // 2. cast to float or int
3516                 if ( is_numeric($val) && (i18n::strpos($val, '.5') === (i18n::strlen($val) - 2)) )
3517                 {
3518                         $val = (float) $val;
3519                 }
3520                 else
3521                 {
3522                         $val = (integer) $val;
3523                 }
3524                 
3525                 $this->setSetting('btimeoffset',$val);
3526                 return;
3527         }
3528         
3529         /**
3530          * Blog::setDefaultCategory()
3531          * @param       integer $val    ID for default category for this weblog
3532          * @return      
3533          */
3534         public function setDefaultCategory($val)
3535         {
3536                 $this->setSetting('bdefcat',$val);
3537                 return;
3538         }
3539         
3540         /**
3541          * Blog::getSetting()
3542          * @param       string  $key    key for setting of this weblog
3543          * @return      mixed   value for the setting
3544          */
3545         public function getSetting($key)
3546         {
3547                 return $this->settings[$key];
3548         }
3549         
3550         /**
3551          * Blog::setSetting()
3552          * @param       string  $key    key for setting of this weblog
3553          * @param       mixed   $value  value for the key
3554          * @return      
3555          */
3556         public function setSetting($key, $value)
3557         {
3558                 $this->settings[$key] = $value;
3559                 return;
3560         }
3561         
3562         /**
3563          * Blog::addTeamMember()
3564          * Tries to add a member to the team. 
3565          * Returns false if the member was already on the team
3566          * 
3567          * @param       integer $memberid       id for member
3568          * @param       boolean $admin  super-admin or not
3569          * @return      boolean Success/Fail
3570          */
3571         public function addTeamMember($memberid, $admin)
3572         {
3573                 global $manager;
3574                 
3575                 $memberid = intval($memberid);
3576                 $admin = intval($admin);
3577                 
3578                 // check if member is already a member
3579                 $tmem =& $manager->getMember($memberid);
3580                 
3581                 if ( $tmem->isTeamMember($this->blogid) )
3582                 {
3583                         return 0;
3584                 }
3585                 
3586                 $data = array(
3587                         'blog'          => &$this,
3588                         'member'        => &$tmem,
3589                         'admin'         => &$admin
3590                 );
3591                 $manager->notify('PreAddTeamMember', $data);
3592                 
3593                 // add to team
3594                 $query = "INSERT INTO %s (TMEMBER, TBLOG, TADMIN) VALUES (%d, %d, %d);";
3595                 $query = sprintf($query, sql_table('team'), (integer) $memberid, (integer) $this->blogid, (integer) $admin);
3596                 DB::execute($query);
3597                 
3598                 $data = array(
3599                         'blog'          => &$this,
3600                         'member'        => &$tmem,
3601                         'admin'         =>  $admin
3602                 );
3603                 $manager->notify('PostAddTeamMember', $data);
3604                 
3605                 $logMsg = sprintf(_TEAM_ADD_NEWTEAMMEMBER, $tmem->getDisplayName(), $memberid, $this->getName());
3606                 ActionLog::add(INFO, $logMsg);
3607                 
3608                 return 1;
3609         }
3610         
3611         /**
3612          * Blog::getID()
3613          * @param       void
3614          * @return      integer ID for this weblog
3615          */
3616         public function getID()
3617         {
3618                 return (integer) $this->blogid;
3619         }
3620         
3621         /**
3622          * Checks if a blog with a given shortname exists 
3623          * Returns true if there is a blog with the given shortname (static)
3624          * 
3625          * @param       string  $name           blog shortname
3626          * @return      boolean exists or not
3627          */
3628         public function exists($name)
3629         {
3630                 $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bshortname='. DB::quoteValue($name));
3631                 return ($r->rowCount() != 0);
3632         }
3633         
3634         /**
3635          * Checks if a blog with a given id exists 
3636          * Returns true if there is a blog with the given ID (static)
3637          * 
3638          * @param       integer $id     ID for searched weblog
3639          * @return      boolean exists or not
3640          */
3641         public function existsID($id)
3642         {
3643                 $r = DB::getResult('SELECT * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));
3644                 return ($r->rowCount() != 0);
3645         }
3646         
3647         /**
3648          * Blog::setFuturePost()
3649          * flag there is a future post pending
3650          * 
3651          * @param       void
3652          * @return      void
3653          */
3654         public function setFuturePost()
3655         {
3656                 $query =  "UPDATE %s SET bfuturepost='1' WHERE bnumber=%d;";
3657                 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);
3658                 DB::execute($query);
3659                 return;
3660         }
3661         
3662         /**
3663          * Blog::clearFuturePost()
3664          * clear there is a future post pending
3665          * 
3666          * @param       void
3667          * @return      void
3668          */
3669         public function clearFuturePost()
3670         {
3671                 $query =  "UPDATE %s SET bfuturepost='0' WHERE bnumber=%d;";
3672                 $query = sprintf($query, sql_table('blog'), (integer) $this->blogid);
3673                 DB::execute($query);
3674                 return;
3675         }
3676         
3677         /**
3678          * Blog::checkJustPosted()
3679          * check if we should throw justPosted event 
3680          * 
3681          * @param       void
3682          * @return      void
3683          */
3684         public function checkJustPosted()
3685         {
3686                 global $manager;
3687                 
3688                 if ( $this->settings['bfuturepost'] == 1 )
3689                 {
3690                         $query = "SELECT * FROM %s WHERE iposted=0 AND iblog=%d AND itime < NOW();";
3691                         $query = sprintf($query, sql_table('item'), (integer) $this->blogid);
3692                         
3693                         $result = DB::getResult($query);
3694                         if ( $result->rowCount() > 0 )
3695                         {
3696                                 // This $pinged is allow a plugin to tell other hook to the event that a ping is sent already
3697                                 // Note that the plugins's calling order is subject to thri order in the plugin list
3698                                 $pinged = FALSE;
3699                                 $data = array('blogid' => $this->blogid, 'pinged' => &$pinged);
3700                                 $manager->notify('JustPosted', $data);
3701                                 
3702                                 // clear all expired future posts
3703                                 $query = "UPDATE %s SET iposted='1' WHERE iblog=%d AND itime < NOW();";
3704                                 $query = spriintf($query, sql_table('item'), (integer) $this->blogid);
3705                                 DB::execute($query);
3706                                 
3707                                 // check to see any pending future post, clear the flag is none
3708                                 $query = "SELECT * FROM %s WHERE iposted=0 AND iblog=%d;";
3709                                 $query = sprintf($query, sql_table('item'), (integer) $this->blogid);
3710                                 
3711                                 $result = DB::getResult($query);
3712                                 if ( $result->rowCount() == 0 )
3713                                 {
3714                                         $this->clearFuturePost();
3715                                 }
3716                         }
3717                 }
3718                 return;
3719         }
3720         
3721         /**
3722          * Blog::readLogFromList()
3723          * Shows the given list of items for this blog
3724          *
3725          * @param       array   $itemarray      array of item numbers to be displayed
3726          * @param       string  $template       string representing the template _NAME_ (!)
3727          * @param       string  $highlight      contains a query that should be highlighted
3728          * @param       boolean $comments       1=show comments 0=don't show comments
3729          * @param       boolean $dateheads      1=show dateheads 0=don't show dateheads
3730          * @param       boolean $showDrafts     0=do not show drafts 1=show drafts
3731          * @param       boolean $showFuture     0=do not show future posts 1=show future posts
3732          * @return      integer amount of items shown
3733          */
3734         public function readLogFromList($itemarray, $template, $highlight = '', $comments = 1, $dateheads = 1,$showDrafts = 0, $showFuture = 0)
3735         {
3736                 $query = $this->getSqlItemList($itemarray,$showDrafts,$showFuture);
3737                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
3738         }
3739         
3740         /**
3741          * Blog::getSqlItemList()
3742          * Returns the SQL query used to fill out templates for a list of items
3743          * No LIMIT clause is added. (caller should add this if multiple pages are requested)
3744          *
3745          * @param       array   $itemarray      an array holding the item numbers of the items to be displayed
3746          * @param       integer $showDrafts     0=do not show drafts 1=show drafts
3747          * @param       integer $showFuture     0=do not show future posts 1=show future posts
3748          * @return      string  either a full SQL query, or an empty string
3749          */
3750         public function getSqlItemList($itemarray,$showDrafts = 0,$showFuture = 0)
3751         {
3752                 if ( !is_array($itemarray) )
3753                 {
3754                         return '';
3755                 }
3756                 
3757                 $showDrafts = intval($showDrafts);
3758                 $showFuture = intval($showFuture);
3759                 $items = array();
3760                 
3761                 foreach ( $itemarray as $value )
3762                 {
3763                         if ( intval($value) )
3764                         {
3765                                 $items[] = intval($value);
3766                         }
3767                 }
3768                 if ( !count($items) )
3769                 {
3770                         return '';
3771                 }
3772                 
3773                 $i = count($items);
3774                 $query = '';
3775                 foreach ( $items as $value )
3776                 {
3777                         $query .= '('
3778                                         .       'SELECT'
3779                                         .       ' i.inumber as itemid,'
3780                                         .       ' i.ititle as title,'
3781                                         .       ' i.ibody as body,'
3782                                         .       ' m.mname as author,'
3783                                         .       ' m.mrealname as authorname,'
3784                                         .       ' i.itime,'
3785                                         .       ' i.imore as more,'
3786                                         .       ' m.mnumber as authorid,'
3787                                         .       ' m.memail as authormail,'
3788                                         .       ' m.murl as authorurl,'
3789                                         .       ' c.cname as category,'
3790                                         .       ' i.icat as catid,'
3791                                         .       ' i.iclosed as closed';
3792                         
3793                         $query .= ' FROM '
3794                                         . sql_table('item') . ' as i, '
3795                                         . sql_table('member') . ' as m, '
3796                                         . sql_table('category') . ' as c'
3797                                         . ' WHERE'
3798                                     .    ' i.iblog='.$this->blogid
3799                                    . ' and i.iauthor=m.mnumber'
3800                                    . ' and i.icat=c.catid';
3801                         
3802                         // exclude drafts       
3803                         if ( !$showDrafts )
3804                         {
3805                                 $query .= ' and i.idraft=0';
3806                         }
3807                         if ( !$showFuture )
3808                         {
3809                                 // don't show future items
3810                                 $query .= ' and i.itime<=' . DB::formatDateTime($this->getCorrectTime());
3811                         }
3812                         
3813                         $query .= ' and i.inumber='.intval($value);
3814                         $query .= ')';
3815                         $i--;
3816                         if ($i) $query .= ' UNION ';
3817                 }
3818                 
3819                 return $query;
3820         }
3821         
3822         /**
3823          * Blog::convertBreaks()
3824          * Get the the setting for the line break handling
3825          * [should be named as getConvertBreaks()]
3826          * 
3827          * @deprecated
3828          * @param       void
3829          * @return      
3830          */
3831         public function convertBreaks()
3832         {
3833                 return $this->getSetting('bconvertbreaks');
3834         }
3835         
3836         /**
3837          * Set the the setting for the line break handling
3838          * 
3839          * @deprecated
3840          * @param       boolean $val    new value for bconvertbreaks
3841          * @return      void
3842          */
3843         public function setConvertBreaks($val)
3844         {
3845                 $this->setSetting('bconvertbreaks', $val);
3846                 return;
3847         }
3848 }
3849 >>>>>>> skinnable-master