OSDN Git Service

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