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