OSDN Git Service

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