[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

title

Body

[close]

/nucleus/libs/ -> BLOG.php (source)

   1  <?php
   2  
   3  /*
   4   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
   5   * Copyright (C) 2002-2007 The Nucleus Group
   6   *
   7   * This program is free software; you can redistribute it and/or
   8   * modify it under the terms of the GNU General Public License
   9   * as published by the Free Software Foundation; either version 2
  10   * of the License, or (at your option) any later version.
  11   * (see nucleus/documentation/index.html#license for more info)
  12   */
  13  /**
  14   * A class representing a blog and containing functions to get that blog shown
  15   * on the screen
  16   *
  17   * @license http://nucleuscms.org/license.txt GNU General Public License
  18   * @copyright Copyright (C) 2002-2007 The Nucleus Group
  19   * @version $Id: BLOG.php 1171 2007-06-14 03:36:49Z ehui $
  20   */
  21  
  22  if ( !function_exists('requestVar') ) exit;
  23  require_once dirname(__FILE__) . '/ITEMACTIONS.php';
  24  
  25  class BLOG {
  26  
  27      // blog id
  28      var $blogid;
  29  
  30      // ID of currently selected category
  31      var $selectedcatid;
  32  
  33      // After creating an object of the blog class, contains true if the BLOG object is
  34      // valid (the blog exists)
  35      var $isValid;
  36  
  37      // associative array, containing all blogsettings (use the get/set functions instead)
  38      var $settings;
  39  
  40      /**
  41       * Creates a new BLOG object for the given blog
  42       *
  43       * @param $id blogid
  44       */
  45  	function BLOG($id) {
  46          $this->blogid = intval($id);
  47          $this->readSettings();
  48  
  49          // try to set catid
  50          // (the parse functions in SKIN.php will override this, so it's mainly useless)
  51          global $catid;
  52          $this->setSelectedCategory($catid);
  53      }
  54  
  55      /**
  56       * Shows the given amount of items for this blog
  57       *
  58       * @param $template
  59       *        String representing the template _NAME_ (!)
  60       * @param $amountEntries
  61       *        amount of entries to show
  62       * @param $startpos
  63       *        offset from where items should be shown (e.g. 5 = start at fifth item)
  64       * @returns int
  65       *        amount of items shown
  66       */
  67  	function readLog($template, $amountEntries, $offset = 0, $startpos = 0) {
  68          return $this->readLogAmount($template,$amountEntries,'','',1,1,$offset, $startpos);
  69      }
  70  
  71      /**
  72       * Shows an archive for a given month
  73       *
  74       * @param $year
  75       *        year
  76       * @param $month
  77       *        month
  78       * @param $template
  79       *        String representing the template name to be used
  80       */
  81  	function showArchive($templatename, $year, $month, $day=0) {
  82  
  83          // create extra where clause for select query
  84          if ($day == 0) {
  85              $timestamp_start = mktime(0,0,0,$month,1,$year);
  86              $timestamp_end = mktime(0,0,0,$month+1,1,$year);  // also works when $month==12
  87          } else {
  88              $timestamp_start = mktime(0,0,0,$month,$day,$year);
  89              $timestamp_end = mktime(0,0,0,$month,$day+1,$year);
  90          }
  91          $extra_query = ' and i.itime>=' . mysqldate($timestamp_start)
  92                       . ' and i.itime<' . mysqldate($timestamp_end);
  93  
  94  
  95          $this->readLogAmount($templatename,0,$extra_query,'',1,1);
  96  
  97      }
  98  
  99  
 100      // sets/gets current category (only when category exists)
 101  	function setSelectedCategory($catid) {
 102          if ($this->isValidCategory($catid) || (intval($catid) == 0))
 103              $this->selectedcatid = intval($catid);
 104      }
 105  
 106  	function setSelectedCategoryByName($catname) {
 107          $this->setSelectedCategory($this->getCategoryIdFromName($catname));
 108      }
 109  
 110  	function getSelectedCategory() {
 111          return $this->selectedcatid;
 112      }
 113  
 114      /**
 115       * Shows the given amount of items for this blog
 116       *
 117       * @param $template
 118       *        String representing the template _NAME_ (!)
 119       * @param $amountEntries
 120       *        amount of entries to show (0 = no limit)
 121       * @param $extraQuery
 122       *        extra conditions to be added to the query
 123       * @param $highlight
 124       *        contains a query that should be highlighted
 125       * @param $comments
 126       *        1=show comments 0=don't show comments
 127       * @param $dateheads
 128       *        1=show dateheads 0=don't show dateheads
 129       * @param $offset
 130       *        offset
 131       * @returns int
 132       *        amount of items shown
 133       */
 134  	function readLogAmount($template, $amountEntries, $extraQuery, $highlight, $comments, $dateheads, $offset = 0, $startpos = 0) {
 135  
 136          $query = $this->getSqlBlog($extraQuery);
 137  
 138          if ($amountEntries > 0) {
 139                  // $offset zou moeten worden:
 140                  // (($startpos / $amountentries) + 1) * $offset ... later testen ...
 141                 $query .= ' LIMIT ' . intval($startpos + $offset).',' . intval($amountEntries);
 142          }
 143          return $this->showUsingQuery($template, $query, $highlight, $comments, $dateheads);
 144      }
 145  
 146  	function showUsingQuery($templateName, $query, $highlight = '', $comments = 0, $dateheads = 1) {
 147          global $CONF, $manager;
 148  
 149          $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');
 150          if ($lastVisit != 0)
 151              $lastVisit = $this->getCorrectTime($lastVisit);
 152  
 153          // set templatename as global variable (so plugins can access it)
 154          global $currentTemplateName;
 155          $currentTemplateName = $templateName;
 156  
 157          $template =& $manager->getTemplate($templateName);
 158  
 159          // create parser object & action handler
 160          $actions =& new ITEMACTIONS($this);
 161          $parser =& new PARSER($actions->getDefinedActions(),$actions);
 162          $actions->setTemplate($template);
 163          $actions->setHighlight($highlight);
 164          $actions->setLastVisit($lastVisit);
 165          $actions->setParser($parser);
 166          $actions->setShowComments($comments);
 167  
 168          // execute query
 169          $items = sql_query($query);
 170  
 171          // loop over all items
 172          $old_date = 0;
 173          while ($item = mysql_fetch_object($items)) {
 174  
 175              $item->timestamp = strtotime($item->itime);    // string timestamp -> unix timestamp
 176  
 177              // action handler needs to know the item we're handling
 178              $actions->setCurrentItem($item);
 179  
 180              // add date header if needed
 181              if ($dateheads) {
 182                  $new_date = date('dFY',$item->timestamp);
 183                  if ($new_date != $old_date) {
 184                      // unless this is the first time, write date footer
 185                      $timestamp = $item->timestamp;
 186                      if ($old_date != 0) {
 187                          $oldTS = strtotime($old_date);
 188                          $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));
 189                          $tmp_footer = strftime($template['DATE_FOOTER'], $oldTS);
 190                          $parser->parse($tmp_footer);
 191                          $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => $oldTS));
 192                      }
 193                      $manager->notify('PreDateHead',array('blog' => &$this, 'timestamp' => $timestamp));
 194                      // note, to use templatvars in the dateheader, the %-characters need to be doubled in
 195                      // order to be preserved by strftime
 196                      $tmp_header = strftime((isset($template['DATE_HEADER']) ? $template['DATE_HEADER'] : null), $timestamp);
 197                      $parser->parse($tmp_header);
 198                      $manager->notify('PostDateHead',array('blog' => &$this, 'timestamp' => $timestamp));
 199                  }
 200                  $old_date = $new_date;
 201              }
 202  
 203              // parse item
 204              $parser->parse($template['ITEM_HEADER']);
 205              $manager->notify('PreItem', array('blog' => &$this, 'item' => &$item));
 206              $parser->parse($template['ITEM']);
 207              $manager->notify('PostItem', array('blog' => &$this, 'item' => &$item));
 208              $parser->parse($template['ITEM_FOOTER']);
 209  
 210          }
 211  
 212          $numrows = mysql_num_rows($items);
 213  
 214          // add another date footer if there was at least one item
 215          if (($numrows > 0) && $dateheads) {
 216              $manager->notify('PreDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));
 217              $parser->parse($template['DATE_FOOTER']);
 218              $manager->notify('PostDateFoot',array('blog' => &$this, 'timestamp' => strtotime($old_date)));
 219          }
 220  
 221          mysql_free_result($items);    // free memory
 222  
 223          return $numrows;
 224  
 225      }
 226  
 227  	function showOneitem($itemid, $template, $highlight) {
 228          $extraQuery = ' and inumber=' . intval($itemid);
 229  
 230          return $this->readLogAmount($template, 1, $extraQuery, $highlight, 0, 0);
 231      }
 232  
 233  
 234      /**
 235        * Adds an item to this blog
 236        */
 237  	function additem($catid, $title, $body, $more, $blogid, $authorid, $timestamp, $closed, $draft, $posted='1') {
 238          global $manager;
 239  
 240          $blogid     = intval($blogid);
 241          $authorid    = intval($authorid);
 242          $title        = $title;
 243          $body        = $body;
 244          $more        = $more;
 245          $catid        = intval($catid);
 246  
 247          // convert newlines to <br />
 248          if ($this->convertBreaks()) {
 249              $body = addBreaks($body);
 250              $more = addBreaks($more);
 251          }
 252  
 253          if ($closed != '1') $closed = '0';
 254          if ($draft != '0') $draft = '1';
 255  
 256          if (!$this->isValidCategory($catid))
 257              $catid = $this->getDefaultCategory();
 258  
 259          if ($timestamp > $this->getCorrectTime())
 260              $isFuture = 1;
 261  
 262          $timestamp = date('Y-m-d H:i:s',$timestamp);
 263  
 264          $manager->notify('PreAddItem',array('title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$this, 'authorid' => &$authorid, 'timestamp' => &$timestamp, 'closed' => &$closed, 'draft' => &$draft, 'catid' => &$catid));
 265  
 266          $title = addslashes($title);
 267          $body = addslashes($body);
 268          $more = addslashes($more);
 269  
 270          $query = 'INSERT INTO '.sql_table('item').' (ITITLE, IBODY, IMORE, IBLOG, IAUTHOR, ITIME, ICLOSED, IDRAFT, ICAT, IPOSTED) '
 271                 . "VALUES ('$title', '$body', '$more', $blogid, $authorid, '$timestamp', $closed, $draft, $catid, $posted)";
 272          sql_query($query);
 273          $itemid = mysql_insert_id();
 274  
 275          $manager->notify('PostAddItem',array('itemid' => $itemid));
 276  
 277          if (!$draft)
 278              $this->updateUpdateFile();
 279  
 280          // send notification mail
 281          if (!$draft && !$isFuture && $this->getNotifyAddress() && $this->notifyOnNewItem())
 282              $this->sendNewItemNotification($itemid, stripslashes($title), stripslashes($body));
 283  
 284          return $itemid;
 285      }
 286  
 287  	function sendNewItemNotification($itemid, $title, $body) {
 288          global $CONF, $member;
 289  
 290          // create text version of html post
 291          $ascii = toAscii($body);
 292  
 293          $mailto_msg = _NOTIFY_NI_MSG . " \n";
 294  //        $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $itemid . "\n\n";
 295          $temp = parse_url($CONF['Self']);
 296          if ($temp['scheme']) {
 297              $mailto_msg .= createItemLink($itemid) . "\n\n";
 298          } else {
 299              $tempurl = $this->getURL();
 300              if (substr($tempurl, -1) == '/' || substr($tempurl, -4) == '.php') {
 301                  $mailto_msg .= $tempurl . '?itemid=' . $itemid . "\n\n";
 302              } else {
 303                  $mailto_msg .= $tempurl . '/?itemid=' . $itemid . "\n\n";
 304              }
 305          }
 306          $mailto_msg .= _NOTIFY_TITLE . ' ' . strip_tags($title) . "\n";
 307          $mailto_msg .= _NOTIFY_CONTENTS . "\n " . $ascii . "\n";
 308          $mailto_msg .= getMailFooter();
 309  
 310          $mailto_title = $this->getName() . ': ' . _NOTIFY_NI_TITLE;
 311  
 312          $frommail = $member->getNotifyFromMailAddress();
 313  
 314          $notify =& new NOTIFICATION($this->getNotifyAddress());
 315          $notify->notify($mailto_title, $mailto_msg , $frommail);
 316  
 317  
 318  
 319      }
 320  
 321  
 322      /**
 323        * Creates a new category for this blog
 324        *
 325        * @param $catName
 326        *        name of the new category. When empty, a name is generated automatically
 327        *        (starting with newcat)
 328        * @param $catDescription
 329        *        description of the new category. Defaults to 'New Category'
 330        *
 331        * @returns
 332        *        the new category-id in case of success.
 333        *        0 on failure
 334        */
 335  	function createNewCategory($catName = '', $catDescription = 'New category') {
 336          global $member, $manager;
 337  
 338          if ($member->blogAdminRights($this->getID())) {
 339              // generate
 340              if ($catName == '')
 341              {
 342                  $catName = 'newcat';
 343                  $i = 1;
 344  
 345                  $res = sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID());
 346                  while (mysql_num_rows($res) > 0)
 347                  {
 348                      $i++;
 349                      $res = sql_query('SELECT * FROM '.sql_table('category')." WHERE cname='".$catName.$i."' and cblog=".$this->getID());
 350                  }
 351  
 352                  $catName = $catName . $i;
 353              }
 354  
 355              $manager->notify(
 356                  'PreAddCategory',
 357                  array(
 358                      'blog' => &$this,
 359                      'name' => &$catName,
 360                      'description' => $catDescription
 361                  )
 362              );
 363  
 364              $query = 'INSERT INTO '.sql_table('category').' (cblog, cname, cdesc) VALUES (' . $this->getID() . ", '" . addslashes($catName) . "', '" . addslashes($catDescription) . "')";
 365              sql_query($query);
 366              $catid = mysql_insert_id();
 367  
 368              $manager->notify(
 369                  'PostAddCategory',
 370                  array(
 371                      'blog' => &$this,
 372                      'name' => $catName,
 373                      'description' => $catDescription,
 374                      'catid' => $catid
 375                  )
 376              );
 377  
 378              return $catid;
 379          } else {
 380              return 0;
 381          }
 382  
 383      }
 384  
 385  
 386      /**
 387       * Searches all months of this blog for the given query
 388       *
 389       * @param $query
 390       *        search query
 391       * @param $template
 392       *        template to be used (__NAME__ of the template)
 393       * @param $amountMonths
 394       *        max amount of months to be search (0 = all)
 395       * @param $maxresults
 396       *        max number of results to show
 397       * @param $startpos
 398       *        offset
 399       * @returns
 400       *        amount of hits found
 401       */
 402  	function search($query, $template, $amountMonths, $maxresults, $startpos) {
 403          global $CONF, $manager;
 404  
 405          $highlight     = '';
 406          $sqlquery    = $this->getSqlSearch($query, $amountMonths, $highlight);
 407  
 408          if ($sqlquery == '')
 409          {
 410              // no query -> show everything
 411              $extraquery = '';
 412              $amountfound = $this->readLogAmount($template, $maxresults, $extraQuery, $query, 1, 1);
 413          } else {
 414  
 415              // add LIMIT to query (to split search results into pages)
 416              if (intval($maxresults > 0))
 417                  $sqlquery .= ' LIMIT ' . intval($startpos).',' . intval($maxresults);
 418  
 419              // show results
 420              $amountfound = $this->showUsingQuery($template, $sqlquery, $highlight, 1, 1);
 421  
 422              // when no results were found, show a message
 423              if ($amountfound == 0)
 424              {
 425                  $template =& $manager->getTemplate($template);
 426                  $vars = array(
 427                      'query'        => htmlspecialchars($query),
 428                      'blogid'    => $this->getID()
 429                  );
 430                  echo TEMPLATE::fill($template['SEARCH_NOTHINGFOUND'],$vars);
 431              }
 432          }
 433  
 434          return $amountfound;
 435      }
 436  
 437      /**
 438       * Returns an SQL query to use for a search query
 439       *
 440       * @param $query
 441       *        search query
 442       * @param $amountMonths
 443       *        amount of months to search back. Default = 0 = unlimited
 444       * @param $mode
 445       *        either empty, or 'count'. In this case, the query will be a SELECT COUNT(*) query
 446       * @returns $highlight
 447       *        words to highlight (out parameter)
 448       * @returns
 449       *        either a full SQL query, or an empty string (if querystring empty)
 450       * @note
 451       *        No LIMIT clause is added. (caller should add this if multiple pages are requested)
 452       */
 453  	function getSqlSearch($query, $amountMonths = 0, &$highlight, $mode = '')
 454      {
 455          $searchclass =& new SEARCH($query);
 456  
 457          $highlight      = $searchclass->inclusive;
 458  
 459          // if querystring is empty, return empty string
 460          if ($searchclass->inclusive == '')
 461              return '';
 462  
 463  
 464          $where  = $searchclass->boolean_sql_where('ititle,ibody,imore');
 465          $select =