OSDN Git Service

fixed url
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / 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-2005 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   * A class representing a blog and containing functions to get that blog shown\r
14   * on the screen \r
15   *\r
16   * $Id: BLOG.php,v 1.3 2005-03-12 06:19:05 kimitake Exp $\r
17   * $NucleusJP$\r
18   */\r
19 class BLOG {\r
20         \r
21         // blog id\r
22         var $blogid;\r
23         \r
24         // ID of currently selected category\r
25         var $selectedcatid;\r
26         \r
27         // After creating an object of the blog class, contains true if the BLOG object is \r
28         // valid (the blog exists)\r
29         var $isValid;\r
30         \r
31         // associative array, containing all blogsettings (use the get/set functions instead)\r
32         var $settings;\r
33         \r
34         /**\r
35          * Creates a new BLOG object for the given blog\r
36          *\r
37          * @param $id blogid\r
38          */\r
39         function BLOG($id) {\r
40                 $this->blogid = intval($id);\r
41                 $this->readSettings();\r
42 \r
43                 // try to set catid \r
44                 // (the parse functions in SKIN.php will override this, so it's mainly useless)\r
45                 global $catid;\r
46                 $this->setSelectedCategory($catid);\r
47         }\r
48 \r
49         /**\r
50          * Shows the given amount of items for this blog\r
51          *\r
52          * @param $template\r
53          *              String representing the template _NAME_ (!)\r
54          * @param $amountEntries \r
55          *              amount of entries to show\r
56          * @param $startpos\r
57          *              offset from where items should be shown (e.g. 5 = start at fifth item)\r
58          * @returns int\r
59          *              amount of items shown\r
60          */\r
61         function readLog($template, $amountEntries, $offset = 0, $startpos = 0) {\r
62                 return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);\r
63         }\r
64 \r
65         /**\r
66          * Shows an archive for a given month\r
67          *\r
68          * @param $year\r
69          *              year\r
70          * @param $month\r
71          *              month \r
72          * @param $template\r
73          *              String representing the template name to be used\r
74          */\r
75         function showArchive($templatename, $year, $month, $day=0) {\r
76                 \r
77                 // create extra where clause for select query\r
78                 if ($day == 0) {\r
79                         $timestamp_start = mktime(0,0,0,$month,1,$year);\r
80                         $timestamp_end = mktime(0,0,0,$month+1,1,$year);  // also works when $month==12\r
81                 } else {\r
82                         $timestamp_start = mktime(0,0,0,$month,$day,$year);\r
83                         $timestamp_end = mktime(0,0,0,$month,$day+1,$year);  \r
84                 }\r
85                 $extra_query = ' and i.itime>=' . mysqldate($timestamp_start)\r
86                              . ' and i.itime<' . mysqldate($timestamp_end);\r
87                 \r
88         \r
89                 $this->readLogAmount($templatename,0,$extra_query,'',1,1);\r
90 \r
91         }\r
92 \r
93 \r
94         // sets/gets current category (only when category exists)\r
95         function setSelectedCategory($catid) {\r
96                 if ($this->isValidCategory($catid) || (intval($catid) == 0)) \r
97                         $this->selectedcatid = intval($catid);\r
98         }\r
99         \r
100         function setSelectedCategoryByName($catname) {\r
101                 $this->setSelectedCategory($this->getCategoryIdFromName($catname));\r
102         }\r
103 \r
104         function getSelectedCategory() { \r
105                 return $this->selectedcatid; \r
106         }\r
107 \r
108         /**\r
109          * Shows the given amount of items for this blog\r
110          *\r
111          * @param $template\r
112          *              String representing the template _NAME_ (!)\r
113          * @param $amountEntries\r
114          *              amount of entries to show (0 = no limit)\r
115          * @param $extraQuery\r
116          *              extra conditions to be added to the query\r
117          * @param $highlight\r
118          *              contains a query that should be highlighted\r
119          * @param $comments\r
120          *              1=show comments 0=don't show comments\r
121          * @param $dateheads\r
122          *              1=show dateheads 0=don't show dateheads\r
123          * @param $offset\r
124          *              offset\r
125          * @returns int\r
126          *              amount of items shown\r
127          */\r
128         function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0) {\r
129 \r
130                 $query = $this->getSqlBlog($extraQuery);\r
131                 \r
132                 if ($amountEntries > 0) {\r
133                         // $offset zou moeten worden:\r
134                         // (($startpos / $amountentries) + 1) * $offset ... later testen ...\r
135                        $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);\r
136                 }\r
137                 return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);\r
138         }\r
139 \r
140         function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1) {\r
141                 global $CONF, $manager;\r
142 \r
143                 $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');\r
144                 if ($lastVisit != 0)\r
145                         $lastVisit = $this->getCorrectTime($lastVisit);\r
146 \r
147                 // set templatename as global variable (so plugins can access it)\r
148                 global $currentTemplateName;\r
149                 $currentTemplateName = $templateName;\r
150                 \r
151                 $template =& $manager->getTemplate($templateName);\r
152                 \r
153                 // create parser object & action handler\r
154                 $actions =& new ITEMACTIONS($this);\r
155                 $parser =& new PARSER($actions->getDefinedActions(),$actions);\r
156                 $actions->setTemplate($template);\r
157                 $actions->setHighlight($highlight);\r
158                 $actions->setLastVisit($lastVisit);\r
159                 $actions->setParser($parser);\r
160                 $actions->setShowComments($comments);\r
161 \r
162                 // execute query\r
163                 $items = sql_query($query);\r
164                 \r
165                 // loop over all items\r
166                 while ($item = mysql_fetch_object($items)) {\r
167                         \r
168                         $item->timestamp = strtotime($item->itime);     // string timestamp -> unix timestamp\r
169                 \r
170                         // action handler needs to know the item we're handling\r
171                         $actions->setCurrentItem($item);\r
172                         \r
173                         // add date header if needed\r
174                         if ($dateheads) {\r
175                                 $new_date = date('dFY',$item->timestamp);\r
176                                 if ($new_date != $old_date) {\r
177                                         // unless this is the first time, write date footer\r
178                                         $timestamp = $item->timestamp;\r
179                                         if ($old_date != 0) {\r
180                                                 $oldTS = strtotime($old_date);\r
181                                                 $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));\r
182                                                 $parser->parse(strftime($template['DATE_FOOTER'], $oldTS));\r
183                                                 $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));                                                \r
184                                         }\r
185                                         $manager->notify('PreDateHead',array('blog' => &$this, 'timestamp' => $timestamp));\r
186                                         // note, to use templatvars in the dateheader, the %-characters need to be doubled in\r
187                                         // order to be preserved by strftime\r
188                                         $parser->parse(strftime($template['DATE_HEADER'],$timestamp));\r
189                                         $manager->notify('PostDateHead',array('blog' => &$this, 'timestamp' => $timestamp));                                    \r
190                                 }\r
191                                 $old_date = $new_date;\r
192                         }\r
193                         \r
194                         // parse item \r
195                         $parser->parse($template['ITEM_HEADER']);\r
196                         $manager->notify('PreItem', array('blog' => &$this, 'item' => &$item));                 \r
197                         $parser->parse($template['ITEM']);                      \r
198                         $manager->notify('PostItem', array('blog' => &$this, 'item' => &$item));                        \r
199                         $parser->parse($template['ITEM_FOOTER']);\r
200 \r
201                 }\r
202                 \r
203                 $numrows = mysql_num_rows($items);\r
204                 \r
205                 // add another date footer if there was at least one item\r
206                 if (($numrows > 0) && $dateheads) {\r
207                         $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));           \r
208                         $parser->parse($template['DATE_FOOTER']);\r
209                         $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));\r
210                 }\r
211                 \r
212                 mysql_free_result($items);      // free memory\r
213                 \r
214                 return $numrows;\r
215                 \r
216         }        \r
217         \r
218         function showOneitem($itemid, $template, $highlight) {\r
219                 $extraQuery = ' and inumber=' . intval($itemid);\r
220                 \r
221                 return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);\r
222         }\r
223         \r
224 \r
225         /**\r
226           * Adds an item to this blog\r
227           */\r
228         function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft) {\r
229                 global $manager;\r
230 \r
231                 $blogid         = intval($blogid);\r
232                 $authorid       = intval($authorid);\r
233                 $title          = $title;\r
234                 $body           = $body;\r
235                 $more           = $more;\r
236                 $catid          = intval($catid);\r
237 \r
238                 // convert newlines to <br />\r
239                 if ($this->convertBreaks()) {\r
240                         $body = addBreaks($body);\r
241                         $more = addBreaks($more);\r
242                 }\r
243                 \r
244                 if ($closed != '1')     $closed = '0';\r
245                 if ($draft != '0') $draft = '1';\r
246 \r
247                 if (!$this->isValidCategory($catid))\r
248                         $catid = $this->getDefaultCategory();\r
249                 \r
250                 if ($timestamp > $this->getCorrectTime())\r
251                         $isFuture = 1;\r
252 \r
253                 $timestamp = date('Y-m-d H:i:s',$timestamp);\r
254 \r
255                 $manager->notify('PreAddItem',array('title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$this, 'authorid' => &$authorid, 'timestamp' => &$timestamp, 'closed' => &$closed, 'draft' => &$draft, 'catid' => &$catid));\r
256 \r
257                 $title = addslashes($title);\r
258                 $body = addslashes($body);\r
259                 $more = addslashes($more);\r
260 \r
261                 $query = 'INSERT INTO '.sql_table('item').' (ITITLE, IBODY, IMORE, IBLOG, IAUTHOR, ITIME, ICLOSED, IDRAFT, ICAT) '\r
262                        . "VALUES ('$title', '$body', '$more', $blogid, $authorid, '$timestamp', $closed, $draft, $catid)";\r
263                 sql_query($query);  \r
264                 $itemid = mysql_insert_id();\r
265                 \r
266                 $manager->notify('PostAddItem',array('itemid' => $itemid));\r
267                 \r
268                 if (!$draft) \r
269                         $this->updateUpdateFile();\r
270                 \r
271                 // send notification mail\r
272                 if (!$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem()) \r
273                         $this->sendNewItemNotification($itemid, stripslashes($title), stripslashes($body));\r
274                 \r
275                 return $itemid;\r
276         }\r
277         \r
278         function sendNewItemNotification($itemid, $title, $body) {\r
279                 global $CONF, $member;\r
280                 \r
281                 // create text version of html post\r
282                 $ascii = toAscii($body);\r
283 \r
284                 $mailto_msg = _NOTIFY_NI_MSG . " \n";\r
285                 $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $itemid . "\n\n";\r
286                 $mailto_msg .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";\r
287                 $mailto_msg .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";\r
288                 $mailto_msg .= getMailFooter();\r
289                 \r
290                 $mailto_title = $this->getName() . ': ' . _NOTIFY_NI_TITLE;\r
291                         \r
292                 $frommail = $member->getNotifyFromMailAddress();\r
293 \r
294                 $notify =& new NOTIFICATION($this->getNotifyAddress());\r
295                 $notify->notify($mailto_title, $mailto_msg , $frommail);\r
296                                 \r
297 \r
298 \r
299         }\r
300 \r
301         \r
302         /**\r
303           * Creates a new category for this blog\r
304           *\r
305           * @param $catName\r
306           *             name of the new category. When empty, a name is generated automatically \r
307           *             (starting with newcat)\r
308           * @param $catDescription\r
309           *             description of the new category. Defaults to 'New Category'\r
310           *\r
311           * @returns \r
312           *             the new category-id in case of success. \r
313           *             0 on failure\r
314           */\r
315         function createNewCategory($catName = '', $catDescription = 'New category') {\r
316                 global $member, $manager;\r
317 \r
318                 if ($member->blogAdminRights($this->getID())) {\r
319                         // generate \r
320                         if ($catName == '')\r
321                         {\r
322                                 $catName = 'newcat';\r
323                                 $i = 1;\r
324                                 while(mysql_num_rows(sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID())) > 0) \r
325                                         $i++;\r
326                                 $catName = $catName . $i;\r
327                         }\r
328                         \r
329                         $manager->notify(\r
330                                 'PreAddCategory',\r
331                                 array(\r
332                                         'blog' => &$this,\r
333                                         'name' => &$catName,\r
334                                         'description' => $catDescription\r
335                                 )\r
336                         );\r
337                         \r
338                         $query = 'INSERT INTO '.sql_table('category').' (cblog, cname, cdesc) VALUES (' . $this->getID() . ", '" . addslashes($catName) . "', '" . addslashes($catDescription) . "')";\r
339                         sql_query($query);\r
340                         $catid = mysql_insert_id();\r
341                         \r
342                         $manager->notify(\r
343                                 'PostAddCategory',\r
344                                 array(\r
345                                         'blog' => &$this,\r
346                                         'name' => $catName,\r
347                                         'description' => $catDescription,\r
348                                         'catid' => $catid\r
349                                 )\r
350                         );                      \r
351                         \r
352                         return $catid;\r
353                 } else {\r
354                         return 0;\r
355                 }\r
356         \r
357         }       \r
358 \r
359 \r
360         /**\r
361          * Searches all months of this blog for the given query\r
362          *\r
363          * @param $query\r
364          *              search query\r
365          * @param $template\r
366          *              template to be used (__NAME__ of the template)\r
367          * @param $amountMonths\r
368          *              max amount of months to be search (0 = all)\r
369          * @param $maxresults\r
370          *              max number of results to show\r
371          * @param $startpos\r
372          *              offset\r
373          * @returns\r
374          *              amount of hits found\r
375          */\r
376         function search($query, $template, $amountMonths, $maxresults, $startpos) {\r
377         global $CONF, $manager;\r
378 \r
379                 $highlight      = '';\r
380                 $sqlquery       = $this->getSqlSearch($query, $amountMonths, $highlight);\r
381                 \r
382                 if ($sqlquery == '')\r
383                 {\r
384                         // no query -> show everything\r
385             $extraquery = '';\r
386                     $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);\r
387                 } else {\r
388                 \r
389                         // add LIMIT to query (to split search results into pages)\r
390             if (intval($maxresults > 0)) \r
391                     $sqlquery .= ' LIMIT ' . intval($startpos).',' . intval($maxresults);\r
392 \r
393                         // show results\r
394                     $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);\r
395                     \r
396                         // when no results were found, show a message \r
397                 if ($amountfound == 0) \r
398                 {\r
399                         $template =& $manager->getTemplate($template);\r
400                         $vars = array(\r
401                                 'query'         => htmlspecialchars($query),\r
402                                 'blogid'        => $this->getID()\r
403                         );\r
404                         echo TEMPLATE::fill($template['SEARCH_NOTHINGFOUND'],$vars);\r
405                 }\r
406         }\r
407         \r
408                 return $amountfound;\r
409         }\r
410         \r
411         /**\r
412          * Returns an SQL query to use for a search query\r
413          *\r
414          * @param $query\r
415          *              search query\r
416          * @param $amountMonths\r
417          *              amount of months to search back. Default = 0 = unlimited\r
418          * @param $mode\r
419          *              either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query\r
420          * @returns $highlight\r
421          *              words to highlight (out parameter)\r
422          * @returns \r
423          *              either a full SQL query, or an empty string (if querystring empty)\r
424          * @note\r
425          *              No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
426          */\r
427         function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')\r
428         {\r
429         $searchclass =& new SEARCH($query);\r
430         \r
431         $highlight        = $searchclass->inclusive;\r
432         \r
433         // if querystring is empty, return empty string\r
434         if ($searchclass->inclusive == '') \r
435                 return '';\r
436             \r
437            \r
438                 $where  = $searchclass->boolean_sql_where('ititle,ibody,imore');\r
439                 $select = $searchclass->boolean_sql_select('ititle,ibody,imore');\r
440 \r
441                 // get list of blogs to search\r
442                 $blogs          = $searchclass->blogs;          // array containing blogs that always need to be included\r
443                 $blogs[]        = $this->getID();                       // also search current blog (duh)\r
444                 $blogs          = array_unique($blogs);         // remove duplicates\r
445                 $selectblogs = '';\r
446                 if (count($blogs) > 0)\r
447                         $selectblogs = ' and i.iblog in (' . implode(',', $blogs) . ')';\r
448 \r
449                 if ($mode == '') \r
450                 {\r
451                         $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author, m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';\r
452                         if ($select) \r
453                                 $query .= ', '.$select. ' as score ';\r
454                 } else {\r
455                         $query = 'SELECT COUNT(*) as result ';\r
456                 }\r
457                         \r
458                 $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'\r
459                            . ' WHERE i.iauthor=m.mnumber'\r
460                            . ' and i.icat=c.catid'\r
461                            . ' and i.idraft=0'  // exclude drafts\r
462                            . $selectblogs\r
463                                         // don't show future items\r
464                            . ' and i.itime<=' . mysqldate($this->getCorrectTime())\r
465                            . ' and '.$where;\r
466 \r
467                 // take into account amount of months to search\r
468                 if ($amountMonths > 0) \r
469                 {\r
470                         $localtime = getdate($this->getCorrectTime());\r
471                         $timestamp_start = mktime(0,0,0,$localtime['mon'] - $amountMonths,1,$localtime['year']);\r
472                         $query .= ' and i.itime>' . mysqldate($timestamp_start);\r
473                 }\r
474 \r
475                 if ($mode == '')\r
476                 {\r
477                         if ($select) \r
478                                 $query .= ' ORDER BY score DESC';\r
479                         else \r
480                                 $query .= ' ORDER BY i.itime DESC ';\r
481                 }\r
482 \r
483                 return $query;          \r
484         }\r
485         \r
486         /**\r
487          * Returns the SQL query that's normally used to display the blog items on the index type skins\r
488          *\r
489          * @param $mode\r
490          *              either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query\r
491          * @returns \r
492          *              either a full SQL query, or an empty string \r
493          * @note\r
494          *              No LIMIT clause is added. (caller should add this if multiple pages are requested)\r
495          */\r
496         function getSqlBlog($extraQuery, $mode = '')\r
497         {\r
498                 if ($mode == '')\r
499                         $query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body, m.mname as author, m.mrealname as authorname, i.itime, i.imore as more, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed';\r
500                 else\r
501                         $query = 'SELECT COUNT(*) as result ';\r
502                 \r
503                 $query .= ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, '.sql_table('category').' as c'\r
504                        . ' WHERE i.iblog='.$this->blogid\r
505                        . ' and i.iauthor=m.mnumber'\r
506                        . ' and i.icat=c.catid'\r
507                        . ' and i.idraft=0'      // exclude drafts\r
508                                         // don't show future items\r
509                        . ' and i.itime<=' . mysqldate($this->getCorrectTime());\r
510 \r
511                 if ($this->getSelectedCategory())\r
512                         $query .= ' and i.icat=' . $this->getSelectedCategory() . ' ';\r
513 \r
514 \r
515                 $query .= $extraQuery;\r
516                 \r
517                 if ($mode == '')\r
518                         $query .= ' ORDER BY i.itime DESC';\r
519                        \r
520                 return $query;\r
521         }\r
522 \r
523         /**\r
524           * Shows the archivelist using the given template\r
525           */\r
526         function showArchiveList($template, $mode = 'month', $limit = 0) {\r
527                 global $CONF, $catid, $manager;\r
528 \r
529                 if ($catid) \r
530                         $linkparams = array('catid' => $catid);\r
531                 \r
532                 $template =& $manager->getTemplate($template);\r
533                 $data['blogid'] = $this->getID();\r
534 \r
535                 echo TEMPLATE::fill($template['ARCHIVELIST_HEADER'],$data);\r
536 \r
537                 $query = 'SELECT itime, SUBSTRING(itime,1,4) AS Year, SUBSTRING(itime,6,2) AS Month, SUBSTRING(itime,9,2) as Day FROM '.sql_table('item')\r
538                 . ' WHERE iblog=' . $this->getID()\r
539                 . ' and itime <=' . mysqldate($this->getCorrectTime())  // don't show future items!\r
540                 . ' and idraft=0'; // don't show draft items\r
541                 \r
542                 if ($catid)\r
543                         $query .= ' and icat=' . intval($catid);\r
544                 \r
545                 $query .= ' GROUP BY Year, Month';\r
546                 if ($mode == 'day')\r
547                         $query .= ', Day';\r
548                 \r
549                         \r
550                 $query .= ' ORDER BY itime DESC';                               \r
551                 \r
552                 if ($limit > 0) \r
553                         $query .= ' LIMIT ' . intval($limit);\r
554                 \r
555                 $res = sql_query($query);\r
556 \r
557                 while ($current = mysql_fetch_object($res)) {\r
558                         $current->itime = strtotime($current->itime);   // string time -> unix timestamp\r
559                 \r
560                         if ($mode == 'day') {\r
561                                 $archivedate = date('Y-m-d',$current->itime);\r
562                                 $archive['day'] = date('d',$current->itime);\r
563                         } else {\r
564                                 $archivedate = date('Y-m',$current->itime);                     \r
565                         }\r
566                         $data['month'] = date('m',$current->itime);\r
567                         $data['year'] = date('Y',$current->itime);\r
568                         $data['archivelink'] = createArchiveLink($this->getID(),$archivedate,$linkparams);\r
569 \r
570                         $temp = TEMPLATE::fill($template['ARCHIVELIST_LISTITEM'],$data);\r
571                         echo strftime($temp,$current->itime);\r
572 \r
573                 }\r
574                 \r
575                 mysql_free_result($res);\r
576 \r
577                 echo TEMPLATE::fill($template['ARCHIVELIST_FOOTER'],$data);\r
578         }\r
579         \r
580 \r
581         /**\r
582           * Shows the list of categories using a given template\r
583           */\r
584         function showCategoryList($template) {\r
585                 global $CONF, $manager;\r
586                 \r
587                 // determine arguments next to catids\r
588                 // I guess this can be done in a better way, but it works\r
589                 global $archive, $archivelist;\r
590                 \r
591                 $linkparams = array();\r
592                 if ($archive) {\r
593                         $blogurl = createArchiveLink($this->getID(), $archive, '');\r
594                         $linkparams['blogid'] = $this->getID();\r
595                         $linkparams['archive'] = $archive;\r
596                 } else if ($archivelist) {\r
597                         $blogurl = createArchiveListLink($this->getID(), '');\r
598                         $linkparams['archivelist'] = $archivelist;\r
599                 } else {\r
600                         $blogurl = createBlogidLink($this->getID(), '');\r
601                         $linkparams['blogid'] = $this->getID();\r
602                 } \r
603                         \r
604                 //$blogurl = $this->getURL() . $qargs;\r
605                 $blogurl = createBlogLink($this->getURL(), $linkparams);\r
606 \r
607                 $template =& $manager->getTemplate($template);\r
608 \r
609                 echo TEMPLATE::fill($template['CATLIST_HEADER'],\r
610                                                         array(\r
611                                                                 'blogid' => $this->getID(),\r
612                                                                 'blogurl' => $blogurl,\r
613                                                                 'self' => $CONF['Self']\r
614                                                         ));\r
615 \r
616                 $query = 'SELECT catid, cdesc as catdesc, cname as catname FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' ORDER BY cname ASC';\r
617                 $res = sql_query($query);\r
618 \r
619 \r
620                 while ($data = mysql_fetch_assoc($res)) {\r
621                         $data['blogid'] = $this->getID();       \r
622                         $data['blogurl'] = $blogurl;\r
623                         $data['catlink'] = createCategoryLink($data['catid'], $linkparams);\r
624                         $data['self'] = $CONF['Self'];\r
625 \r
626                         $temp = TEMPLATE::fill($template['CATLIST_LISTITEM'],$data);\r
627                         echo strftime($temp,$current->itime);\r
628 \r
629                 }\r
630                 \r
631                 mysql_free_result($res);\r
632 \r
633                 echo TEMPLATE::fill($template['CATLIST_FOOTER'],\r
634                                                         array(\r
635                                                                 'blogid' => $this->getID(),\r
636                                                                 'blogurl' => $blogurl,\r
637                                                                 'self' => $CONF['Self']                                                         \r
638                                                         ));\r
639         }\r
640 \r
641         /**\r
642           * Blogsettings functions\r
643           */\r
644           \r
645         function readSettings() {\r
646                 $query =  'SELECT *'\r
647                        . ' FROM '.sql_table('blog')\r
648                        . ' WHERE bnumber=' . $this->blogid;\r
649                 $res = sql_query($query);\r
650                 \r
651                 $this->isValid = (mysql_num_rows($res) > 0);\r
652                 if (!$this->isValid)\r
653                         return;\r
654                 \r
655                 $this->settings = mysql_fetch_assoc($res);\r
656         }\r
657         \r
658         function writeSettings() {\r
659         \r
660                 // (can't use floatval since not available prior to PHP 4.2)\r
661                 $offset = $this->getTimeOffset();\r
662                 if (!is_float($offset))\r
663                         $offset = intval($offset);\r
664                 \r
665                 $query =  'UPDATE '.sql_table('blog')\r
666                        . " SET bname='" . addslashes($this->getName()) . "',"\r
667                        . "     bshortname='". addslashes($this->getShortName()) . "',"\r
668                        . "     bcomments=". intval($this->commentsEnabled()) . ","\r
669                        . "     bmaxcomments=" . intval($this->getMaxComments()) . ","\r
670                        . "     btimeoffset=" . $offset . ","\r
671                        . "     bpublic=" . intval($this->isPublic()) . ","\r
672                        . "     bsendping=" . intval($this->pingUserland()) . ","\r
673                        . "     bconvertbreaks=" . intval($this->convertBreaks()) . ","\r
674                        . "     ballowpast=" . intval($this->allowPastPosting()) . ","                      \r
675                        . "     bnotify='" . addslashes($this->getNotifyAddress()) . "',"\r
676                        . "     bnotifytype=" . intval($this->getNotifyType()) . ","                    \r
677                        . "     burl='" . addslashes($this->getURL()) . "',"\r
678                        . "     bupdate='" . addslashes($this->getUpdateFile()) . "',"\r
679                        . "     bdesc='" . addslashes($this->getDescription()) . "',"\r
680                        . "     bdefcat=" . intval($this->getDefaultCategory()) . ","                   \r
681                        . "     bdefskin=" . intval($this->getDefaultSkin()) . ","\r
682                        . "     bincludesearch=" . intval($this->getSearchable())\r
683                        . " WHERE bnumber=" . intval($this->getID());\r
684                 sql_query($query);\r
685 \r
686         }\r
687         \r
688 \r
689                 \r
690         // update update file if requested\r
691         function updateUpdatefile() {\r
692                  if ($this->getUpdateFile()) {\r
693                         $f_update = fopen($this->getUpdateFile(),'w');\r
694                         fputs($f_update,$this->getCorrectTime());\r
695                         fclose($f_update);\r
696                  }\r
697 \r
698         }\r
699         \r
700         /**\r
701           * Sends a XML-RPC ping message to Userland, so the weblog can\r
702           * show up in the weblogs.com updates-list\r
703           */\r
704         function sendUserlandPing() {\r
705                 global $php_errormsg;\r
706                 \r
707                  if ($this->pingUserland()) {\r
708                           // testmessage for adding an item\r
709                           $message = new xmlrpcmsg('weblogUpdates.ping',array(\r
710                                         new xmlrpcval($this->getName(),'string'),       \r
711                                         new xmlrpcval($this->getURL(),'string')\r
712                           ));\r
713                           \r
714                           $c = new xmlrpc_client('/RPC2', 'rpc.weblogs.com', 80);\r
715 \r
716                           // $c->setDebug(1);\r
717 \r
718                           $r = $c->send($message,15); // 15 seconds timeout...\r
719                           \r
720                           if (($r == 0) && ($r->errno || $r->errstring)) {\r
721                                 return 'Error ' . $r->errno . ' : ' . $r->errstring;\r
722                           } elseif (($r == 0) && ($php_errormsg)) {\r
723                                 return 'PHP Error: ' . $php_errormsg;\r
724                           } elseif ($r == 0) {\r
725                                 return 'Error while trying to send ping. Sorry about that.';\r
726                           } elseif ($r->faultCode() != 0) {\r
727                                 return 'Error: ' . $r->faultString();\r
728                           } else {\r
729                                   $r = $r->value();     // get response struct\r
730                                   // get values\r
731                                   $flerror = $r->structmem('flerror');\r
732                                   $flerror = $flerror->scalarval();\r
733  \r
734                                   \r
735                                   $message = $r->structmem('message');\r
736                                   $message = $message->scalarval();\r
737 \r
738                                   if ($flerror != 0)\r
739                                         return 'Error (flerror=1): ' . $message;\r
740                                   else\r
741                                         return 'Success: ' . $message;\r
742                           }                       \r
743                  }\r
744         }\r
745         \r
746         function isValidCategory($catid) {\r
747                 $query = 'SELECT * FROM '.sql_table('category').' WHERE cblog=' . $this->getID() . ' and catid=' . intval($catid);\r
748                 return (mysql_num_rows(mysql_query($query)) != 0);\r
749         }\r
750         \r
751         function getCategoryName($catid) {\r
752                 $res = mysql_query('SELECT cname FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));\r
753                 $o = mysql_fetch_object($res);\r
754                 return $o->cname;\r
755         }\r
756         \r
757         function getCategoryDesc($catid) {\r
758                 $res = mysql_query('SELECT cdesc FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and catid=' . intval($catid));\r
759                 $o = mysql_fetch_object($res);\r
760                 return $o->cdesc;\r
761         }\r
762 \r
763         function getCategoryIdFromName($name) {\r
764                 $res = mysql_query('SELECT catid FROM '.sql_table('category').' WHERE cblog='.$this->getID().' and cname="' . addslashes($name) . '"');\r
765                 if (mysql_num_rows($res) > 0) {\r
766                         $o = mysql_fetch_object($res);\r
767                         return $o->catid;       \r
768                 } else {\r
769                         return $this->getDefaultCategory();\r
770                 }\r
771         }\r
772         \r
773         function pingUserland() {\r
774                 return $this->getSetting('bsendping');\r
775         }\r
776         \r
777         function setPingUserland($val) {\r
778                 $this->setSetting('bsendping',$val);\r
779         }\r
780         \r
781         function convertBreaks() {\r
782                 return $this->getSetting('bconvertbreaks');\r
783         }\r
784         \r
785         function insertJavaScriptInfo($authorid = '') {\r
786                 global $member, $CONF;\r
787                 \r
788                 if ($authorid == '')    \r
789                         $authorid = $member->getID();\r
790                         \r
791                 ?>\r
792                 <script type="text/javascript">\r
793                         setConvertBreaks(<?php echo  $this->convertBreaks() ? 'true' : 'false' ?>);\r
794                         setMediaUrl("<?php echo $CONF['MediaURL']?>");\r
795                         setAuthorId(<?php echo $authorid?>);\r
796                 </script><?php  }\r
797         \r
798         function setConvertBreaks($val) {\r
799                 $this->setSetting('bconvertbreaks',$val);\r
800         }\r
801         function setAllowPastPosting($val) {\r
802                 $this->setSetting('ballowpast',$val);\r
803         }               \r
804         function allowPastPosting() {\r
805                 return $this->getSetting('ballowpast');\r
806         }\r
807         \r
808         function getCorrectTime($t=0) {\r
809                 if ($t == 0) $t = time();\r
810                 return ($t + 3600 * $this->getTimeOffset());\r
811         }\r
812         \r
813         function getName() {\r
814                 return $this->getSetting('bname');\r
815         }\r
816         \r
817         function getShortName() {\r
818                 return $this->getSetting('bshortname');\r
819         }\r
820         \r
821         function getMaxComments() {\r
822                 return $this->getSetting('bmaxcomments');\r
823         }\r
824         \r
825         function getNotifyAddress() {\r
826                 return $this->getSetting('bnotify');\r
827         }\r
828         \r
829         function getNotifyType() {\r
830                 return $this->getSetting('bnotifytype');\r
831         }\r
832         \r
833         function notifyOnComment() {\r
834                 $n = $this->getNotifyType();\r
835                 return (($n != 0) && (($n % 3) == 0));\r
836         }\r
837 \r
838         function notifyOnVote() {\r
839                 $n = $this->getNotifyType();\r
840                 return (($n != 0) && (($n % 5) == 0));\r
841         }\r
842 \r
843         function notifyOnNewItem() {\r
844                 $n = $this->getNotifyType();\r
845                 return (($n != 0) && (($n % 7) == 0));\r
846         }\r
847         \r
848         function setNotifyType($val) {\r
849                 $this->setSetting('bnotifytype',$val);  \r
850         }\r
851         \r
852 \r
853         function getTimeOffset() {\r
854                 return $this->getSetting('btimeoffset');\r
855         }\r
856         \r
857         function commentsEnabled() {\r
858                 return $this->getSetting('bcomments');\r
859         }\r
860         \r
861         function getURL() {\r
862                 return $this->getSetting('burl');\r
863         }\r
864         \r
865         function getDefaultSkin() {\r
866                 return $this->getSetting('bdefskin');\r
867         }\r
868         \r
869         function getUpdateFile() {\r
870                 return $this->getSetting('bupdate');\r
871         }\r
872         \r
873         function getDescription() {\r
874                 return $this->getSetting('bdesc');\r
875         }\r
876         \r
877         function isPublic() {\r
878                 return $this->getSetting('bpublic');\r
879         }\r
880         \r
881         function getSearchable() {\r
882                 return $this->getSetting('bincludesearch');\r
883         }\r
884 \r
885         function getDefaultCategory() {\r
886                 return $this->getSetting('bdefcat');\r
887         }\r
888         \r
889         function setPublic($val) {\r
890                 $this->setSetting('bpublic',$val);\r
891         }\r
892 \r
893         function setSearchable($val) {\r
894                 $this->setSetting('bincludesearch',$val);\r
895         }\r
896 \r
897         function setDescription($val) {\r
898                 $this->setSetting('bdesc',$val);\r
899         }\r
900 \r
901         function setUpdateFile($val) {\r
902                 $this->setSetting('bupdate',$val);\r
903         }\r
904         \r
905         function setDefaultSkin($val) {\r
906                 $this->setSetting('bdefskin',$val);\r
907         }\r
908         \r
909         function setURL($val) {\r
910                 $this->setSetting('burl',$val);\r
911         }\r
912         \r
913         function setName($val) {\r
914                 $this->setSetting('bname',$val);\r
915         }\r
916         \r
917         function setShortName($val) {\r
918                 $this->setSetting('bshortname',$val);\r
919         }\r
920         \r
921         function setCommentsEnabled($val) { \r
922                 $this->setSetting('bcomments',$val);\r
923         }\r
924         \r
925         function setMaxComments($val) {\r
926                 $this->setSetting('bmaxcomments',$val);\r
927         }\r
928         \r
929         function setNotifyAddress($val) {\r
930                 $this->setSetting('bnotify',$val);\r
931         }\r
932         \r
933         function setTimeOffset($val) {\r
934                 // check validity of value\r
935                 // 1. replace , by . (common mistake)\r
936                 $val = str_replace(',','.',$val);\r
937                 // 2. cast to float or int\r
938                 if (is_numeric($val) && strstr($val,'.5')) {\r
939                         $val = (float) $val;\r
940                 } else {\r
941                         $val = intval($val);\r
942                 }\r
943         \r
944                 $this->setSetting('btimeoffset',$val);\r
945         }\r
946         \r
947         function setDefaultCategory($val) {\r
948                 $this->setSetting('bdefcat',$val);\r
949         }\r
950 \r
951         function getSetting($key) {\r
952                 return $this->settings[$key];\r
953         }\r
954         \r
955         function setSetting($key,$value) {\r
956                 $this->settings[$key] = $value;\r
957         }\r
958         \r
959         \r
960         // tries to add a member to the team. Returns false if the member was already on\r
961         // the team\r
962         function addTeamMember($memberid, $admin) {\r
963                 global $manager;\r
964                 \r
965                 $memberid = intval($memberid);\r
966                 $admin = intval($admin);\r
967                 \r
968                 // check if member is already a member\r
969                 $tmem = MEMBER::createFromID($memberid);\r
970                 \r
971                 if ($tmem->isTeamMember($this->getID()))\r
972                         return 0;\r
973                 \r
974                 $manager->notify(\r
975                         'PreAddTeamMember',\r
976                         array(\r
977                                 'blog' => &$this,\r
978                                 'member' => &$tmem,\r
979                                 'admin' => &$admin\r
980                         )\r
981                 );\r
982                 \r
983                 // add to team\r
984                 $query = 'INSERT INTO '.sql_table('team').' (TMEMBER, TBLOG, TADMIN) '\r
985                        . 'VALUES (' . $memberid .', '.$this->getID().', "'.$admin.'")';\r
986                 sql_query($query);\r
987                 \r
988                 $manager->notify(\r
989                         'PostAddTeamMember',\r
990                         array(\r
991                                 'blog' => &$this,\r
992                                 'member' => &$tmem,\r
993                                 'admin' => $admin\r
994                         )\r
995                         \r
996                 );\r
997                 \r
998                 ACTIONLOG::add(INFO, 'Added ' . $tmem->getDisplayName() . ' (ID=' .\r
999                                $memberid .') to the team of blog "' . $this->getName() . '"');\r
1000                 \r
1001                 return 1;\r
1002         }\r
1003         \r
1004         function getID() {\r
1005                 return $this->blogid;\r
1006         }\r
1007         \r
1008         // returns true if there is a blog with the given shortname (static)\r
1009         function exists($name) {\r
1010                 $r = sql_query('select * FROM '.sql_table('blog').' WHERE bshortname="'.addslashes($name).'"');\r
1011                 return (mysql_num_rows($r) != 0);\r
1012         }\r
1013 \r
1014         // returns true if there is a blog with the given ID (static)\r
1015         function existsID($id) {\r
1016                 $r = sql_query('select * FROM '.sql_table('blog').' WHERE bnumber='.intval($id));\r
1017                 return (mysql_num_rows($r) != 0);\r
1018         }       \r
1019         \r
1020         \r
1021 }\r
1022 \r
1023 /**\r
1024   * This class is used when parsing item templates\r
1025   */\r
1026 class ITEMACTIONS extends BaseActions {\r
1027 \r
1028         // contains an assoc array with parameters that need to be included when\r
1029         // generating links to items/archives/... (e.g. catid)\r
1030         var $linkparams;\r
1031         \r
1032         // true when the current user is a blog admin (and thus allowed to edit all items)\r
1033         var $allowEditAll;\r
1034         \r
1035         // timestamp of last visit\r
1036         var $lastVisit;\r
1037         \r
1038         // item currently being handled (mysql result object, see BLOG::showUsingQuery)\r
1039         var $currentItem;\r
1040         \r
1041         // reference to the blog currently being displayed\r
1042         var $blog;\r
1043         \r
1044         // associative array with template info (part name => contents)\r
1045         var $template;\r
1046         \r
1047         // true when comments need to be displayed\r
1048         var $showComments;\r
1049         \r
1050         function ITEMACTIONS(&$blog) {\r
1051                 // call constructor of superclass first\r
1052                 $this->BaseActions();   \r
1053                 \r
1054                 // extra parameters for created links\r
1055                 global $catid;\r
1056                 if ($catid)\r
1057                         $this->linkparams = array('catid' => $catid);\r
1058                         \r
1059                 // check if member is blog admin (and thus allowed to edit all items)\r
1060                 global $member;\r
1061                 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));\r
1062                 $this->setBlog($blog);\r
1063         }\r
1064 \r
1065         function getDefinedActions() {\r
1066                 return array(\r
1067                         'blogid',\r
1068                         'title',\r
1069                         'body',\r
1070                         'more',\r
1071                         'smartbody',\r
1072                         'itemid',\r
1073                         'morelink',\r
1074                         'category',\r
1075                         'categorylink',\r
1076                         'author',\r
1077                         'authorid',\r
1078                         'authorlink',\r
1079                         'catid',\r
1080                         'karma',\r
1081                         'date',\r
1082                         'time',\r
1083                         'query',\r
1084                         'itemlink',\r
1085                         'blogurl',\r
1086                         'closed',\r
1087                         'syndicate_title',\r
1088                         'syndicate_description',\r
1089                         'karmaposlink',\r
1090                         'karmaneglink',\r
1091                         'new',\r
1092                         'image',\r
1093                         'popup',\r
1094                         'media',\r
1095                         'daylink',\r
1096                         'query',\r
1097                         'include',\r
1098                         'phpinclude',\r
1099                         'parsedinclude',\r
1100                         'skinfile',\r
1101                         'set',\r
1102                         'plugin',\r
1103                         'edit',\r
1104                         'editlink',\r
1105                         'editpopupcode',\r
1106                         'comments',\r
1107                         'relevance'\r
1108                 );\r
1109         }\r
1110         \r
1111 \r
1112         \r
1113         function setLastVisit($lastVisit) {             $this->lastVisit = $lastVisit; }\r
1114         function setParser(&$parser) {                  $this->parser =& $parser; }\r
1115         function setCurrentItem(&$item) {               $this->currentItem =& $item; }\r
1116         function setBlog(&$blog) {                              $this->blog =& $blog; }\r
1117         function setTemplate($template) {               $this->template =& $template; }\r
1118         function setShowComments($val) {                $this->showComments = $val; }\r
1119         \r
1120         // methods used by parser to insert content\r
1121         \r
1122         function parse_blogid() {               echo $this->blog->getID();      }\r
1123         function parse_body() {                 $this->highlightAndParse($this->currentItem->body); }\r
1124         function parse_title() {                $this->highlightAndParse($this->currentItem->title); }\r
1125         function parse_more() {                 $this->highlightAndParse($this->currentItem->more); }   \r
1126         function parse_itemid() {               echo $this->currentItem->itemid; }              \r
1127         function parse_category() {             echo $this->currentItem->category; }                            \r
1128         function parse_categorylink() { echo createCategoryLink($this->currentItem->catid); }                           \r
1129         function parse_catid() {                echo $this->currentItem->catid; }                                       \r
1130         function parse_authorid() {             echo $this->currentItem->authorid; }\r
1131         function parse_authorlink() {   echo createMemberLink($this->currentItem->authorid, $this->linkparams); }       \r
1132         function parse_query() {                echo $this->strHighlight; }\r
1133         function parse_itemlink() {             echo createItemLink($this->currentItem->itemid, $this->linkparams); }\r
1134         function parse_blogurl() {              echo $this->blog->getURL(); }\r
1135         function parse_closed() {               echo $this->currentItem->closed; }\r
1136         function parse_relevance() {    echo round($this->currentItem->score,2);}\r
1137         \r
1138         function parse_karma($type = 'totalscore') {\r
1139                 global $manager;\r
1140 \r
1141                 // get karma object\r
1142                 $karma =& $manager->getKarma($this->currentItem->itemid);\r
1143                 \r
1144                 switch($type) {\r
1145                         case 'pos':\r
1146                                 echo $karma->getNbPosVotes();\r
1147                                 break;\r
1148                         case 'neg':\r
1149                                 echo $karma->getNbNegVotes();\r
1150                                 break;                          \r
1151                         case 'votes':\r
1152                                 echo $karma->getNbOfVotes();\r
1153                                 break;          \r
1154                         case 'posp':\r
1155                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;\r
1156                                 echo number_format($percentage,2), '%';                         \r
1157                                 break;                          \r
1158                         case 'negp':\r
1159                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;\r
1160                                 echo number_format($percentage,2), '%';                         \r
1161                                 break;                          \r
1162                         case 'totalscore':\r
1163                         default:\r
1164                                 echo $karma->getTotalScore();\r
1165                                 break;\r
1166                 }\r
1167                 \r
1168         }                                               \r
1169 \r
1170         function parse_author($which = '') {            \r
1171                 switch($which)\r
1172                 {\r
1173                         case 'realname':\r
1174                                 echo $this->currentItem->authorname;\r
1175                                 break;\r
1176                         case 'id':\r
1177                                 echo $this->currentItem->authorid;\r
1178                                 break;\r
1179                         case 'email':\r
1180                                 echo $this->currentItem->authormail;\r
1181                                 break;\r
1182                         case 'url':\r
1183                                 echo $this->currentItem->authorurl;\r
1184                                 break;\r
1185                         case 'name':\r
1186                         default:\r
1187                                 echo $this->currentItem->author;\r
1188                 }\r
1189         }               \r
1190         \r
1191         function parse_smartbody() {\r
1192                 if (!$this->currentItem->more) {\r
1193                         $this->highlightAndParse($this->currentItem->body);\r
1194                 } else {\r
1195                         $this->highlightAndParse($this->currentItem->more);             \r
1196                 }\r
1197         }\r
1198 \r
1199         function parse_morelink() {             \r
1200                 if ($this->currentItem->more)\r
1201                         $this->parser->parse($this->template['MORELINK']);\r
1202         }                       \r
1203         \r
1204         function parse_date($format = '') {\r
1205                 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE']);\r
1206         }\r
1207         \r
1208         /**\r
1209           * @param format optional strftime format\r
1210           */\r
1211         function parse_time($format = '') {                     \r
1212                 echo strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp); \r
1213         }\r
1214         \r
1215         /**\r
1216           * @param maxLength optional maximum length\r
1217           */\r
1218         function parse_syndicate_title($maxLength = 100) { \r
1219                 $syndicated = strip_tags($this->currentItem->title);\r
1220                 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'));\r
1221         }\r
1222         \r
1223         /**\r
1224           * @param maxLength optional maximum length\r
1225           */\r
1226         function parse_syndicate_description($maxLength = 250, $addHighlight = 0) { \r
1227                 $syndicated = strip_tags($this->currentItem->body);\r
1228                 if ($addHighlight) {\r
1229                     echo $this->highlightAndParse(htmlspecialchars(shorten($syndicated,$maxLength,'...')));\r
1230                 } else {\r
1231                 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'));\r
1232                 }\r
1233         }\r
1234         \r
1235         function parse_karmaposlink($text = '') { \r
1236                 global $CONF;\r
1237                 $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid='.$this->currentItem->itemid; \r
1238                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;\r
1239         }\r
1240         \r
1241         function parse_karmaneglink($text = '') { \r
1242                 global $CONF;\r
1243                 $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem->itemid; \r
1244                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;\r
1245         }\r
1246         \r
1247         function parse_new() {  \r
1248                 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))\r
1249                         echo $this->template['NEW'];\r
1250         }\r
1251         \r
1252         function parse_image() {\r
1253                 // image/popup calls have arguments separated by |\r
1254                 $args = func_get_args();\r
1255                 $args = explode('|',implode($args,', '));\r
1256                 call_user_func_array(array(&$this,'createImageCode'),$args);\r
1257         }\r
1258         function parse_popup() {\r
1259                 // image/popup calls have arguments separated by |\r
1260                 $args = func_get_args();\r
1261                 $args = explode('|',implode($args,', '));\r
1262                 call_user_func_array(array(&$this,'createPopupCode'),$args);\r
1263         }\r
1264         function parse_media() {\r
1265                 // image/popup calls have arguments separated by |\r
1266                 $args = func_get_args();\r
1267                 $args = explode('|',implode($args,', '));\r
1268                 call_user_func_array(array(&$this,'createMediaCode'),$args);\r
1269         }\r
1270         \r
1271         function parse_daylink() {              \r
1272                 echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);\r
1273         }\r
1274         \r
1275         function parse_comments($maxToShow = 0) {\r
1276                 if ($maxToShow == 0)\r
1277                         $maxToShow = $this->blog->getMaxComments();\r
1278                         \r
1279                 // add comments\r
1280                 if ($this->showComments && $this->blog->commentsEnabled()) {\r
1281                         $comments =& new COMMENTS($this->currentItem->itemid);\r
1282                         $comments->setItemActions($this);\r
1283                         $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight); \r
1284                 }\r
1285         }\r
1286 \r
1287         /**\r
1288           * Executes a plugin templatevar\r
1289           *\r
1290           * @param pluginName name of plugin (without the NP_)\r
1291           * \r
1292           * extra parameters can be added\r
1293           */\r
1294         function parse_plugin($pluginName) {\r
1295                 global $manager;\r
1296                 \r
1297                 // only continue when the plugin is really installed\r
1298                 if (!$manager->pluginInstalled('NP_' . $pluginName))\r
1299                         return;\r
1300                 \r
1301                 $plugin =& $manager->getPlugin('NP_' . $pluginName);\r
1302                 if (!$plugin) return;\r
1303 \r
1304                 // get arguments\r
1305                 $params = func_get_args();\r
1306                 \r
1307                 // remove plugin name \r
1308                 array_shift($params);\r
1309                 \r
1310                 // add item reference (array_unshift didn't work)\r
1311                 $params = array_merge(array(&$this->currentItem),$params);\r
1312 \r
1313                 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);\r
1314         }\r
1315         \r
1316         function parse_edit() {\r
1317                 global $member, $CONF;\r
1318                 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {\r
1319                         $this->parser->parse($this->template['EDITLINK']);\r
1320                 }\r
1321         }\r
1322         \r
1323         function parse_editlink() {\r
1324                 global $CONF;\r
1325                 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&amp;itemid=',$this->currentItem->itemid;\r
1326         }\r
1327         \r
1328         function parse_editpopupcode() {\r
1329                 echo "if (event &amp;&amp; event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=yes,width=600,height=500,left=10,top=10,status=yes,resizable=yes');winbm.focus();return false;";\r
1330         }\r
1331         \r
1332         // helper functions\r
1333 \r
1334         /**\r
1335           * Parses highlighted text, with limited actions only (to prevent not fully trusted team members\r
1336           * from hacking your weblog. \r
1337           */\r
1338         function highlightAndParse(&$data) {\r
1339                 // allow only a limited subset of actions (do not allow includes etc, they might be evil)\r
1340                 $this->parser->actions = array('image','media','popup');\r
1341                 $this->parser->parse($this->highlight($data));\r
1342                 $this->parser->actions = $this->getDefinedActions();\r
1343         }\r
1344         \r
1345         function createPopupCode($filename, $width, $height, $text = '') {\r
1346                 global $CONF;\r
1347         \r
1348                 // select private collection when no collection given\r
1349                 if (!strstr($filename,'/')) {\r
1350                         $filename = $this->currentItem->authorid . '/' . $filename;\r
1351                 }\r
1352                 \r
1353                 $windowwidth = $width;\r
1354                 $windowheight = $height;\r
1355                 \r
1356                 $vars['rawpopuplink']   = $CONF['Self'] . "?imagepopup=" . htmlspecialchars($filename) . "&amp;width=$width&amp;height=$height&amp;imagetext=" . urlencode(htmlspecialchars($text));\r
1357                 $vars['popupcode']              = "window.open(this.href,'imagepopup','status=no,toolbar=no,scrollbars=no,resizable=yes,width=$windowwidth,height=$windowheight');return false;";\r
1358                 $vars['popuptext']              = htmlspecialchars($text);\r
1359                 $vars['popuplink']              = '<a href="' . $vars['rawpopuplink']. '" onclick="'. $vars['popupcode'].'" >' . $vars['popuptext'] . '</a>';\r
1360                 $vars['width']                  = $width;\r
1361                 $vars['height']                 = $height;\r
1362                 $vars['text']                   = $text;\r
1363                 \r
1364                 echo TEMPLATE::fill($this->template['POPUP_CODE'],$vars);\r
1365         }\r
1366         \r
1367         function createImageCode($filename, $width, $height, $text = '') {\r
1368                 global $CONF;\r
1369                 \r
1370                 // select private collection when no collection given\r
1371                 if (!strstr($filename,'/')) {\r
1372                         $filename = $this->currentItem->authorid . '/' . $filename;\r
1373                 }\r
1374                 \r
1375                 $windowwidth = $width;\r
1376                 $windowheight = $height;\r
1377                 \r
1378                 $vars['link']                   = htmlspecialchars($CONF['MediaURL']. $filename);\r
1379                 $vars['text']                   = htmlspecialchars($text);\r
1380                 $vars['image'] = '<img src="' . $vars['link'] . '" width="' . $width . '" height="' . $height . '" alt="' . $vars['text'] . '" title="' . $vars['text'] . '" />';\r
1381                 $vars['width']                  = $width;\r
1382                 $vars['height']                 = $height;\r
1383                                 \r
1384                 \r
1385                 \r
1386                 echo TEMPLATE::fill($this->template['IMAGE_CODE'],$vars);;\r
1387 \r
1388         }\r
1389         \r
1390         function createMediaCode($filename, $text = '') {\r
1391                 global $CONF;\r
1392                 \r
1393                 // select private collection when no collection given\r
1394                 if (!strstr($filename,'/')) {\r
1395                         $filename = $this->currentItem->authorid . '/' . $filename;\r
1396                 }               \r
1397 \r
1398                 $vars['link']                   = htmlspecialchars($CONF['MediaURL'] . $filename);\r
1399                 $vars['text']                   = htmlspecialchars($text);\r
1400                 $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';\r
1401 \r
1402                 echo TEMPLATE::fill($this->template['MEDIA_CODE'],$vars);;\r
1403         }\r
1404         \r
1405         \r
1406 }\r
1407 \r
1408 \r
1409 // used for mail notification (html -> text)\r
1410 \r
1411 function toAscii($html) {\r
1412         // strip off most tags \r
1413         $html = strip_tags($html,'<a>');\r
1414         $to_replace = "/<a[^>]*href=[\"\']([^\"^']*)[\"\'][^>]*>([^<]*)<\/a>/i"; \r
1415         _links_init(); \r
1416         $ascii = preg_replace_callback ($to_replace, '_links_add', $html); \r
1417         $ascii .= "\n\n" . _links_list(); \r
1418         return strip_tags($ascii);\r
1419 }\r
1420 \r
1421 function _links_init() { \r
1422    global $tmp_links; \r
1423    $tmp_links = array(); \r
1424\r
1425 \r
1426 function _links_add($match) { \r
1427    global $tmp_links; \r
1428    array_push($tmp_links, $match[1]); \r
1429    return $match[2] . ' [' . sizeof($tmp_links) .']'; \r
1430\r
1431 \r
1432 function _links_list() { \r
1433    global $tmp_links; \r
1434    $output = ''; \r
1435    $i = 1; \r
1436    foreach ($tmp_links as $current) { \r
1437       $output .= "[$i] $current\n"; \r
1438       $i++; \r
1439    } \r
1440    return $output; \r
1441\r
1442 \r
1443  \r
1444 ?>\r