b2evolution PHP Cross Reference Blogging Systems

Source: /inc/collections/model/_blogcache.class.php - 456 lines - 12888 bytes - Summary - Text - Print

Description: This file implements the BlogCache class. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**
   3   * This file implements the BlogCache class.
   4   *
   5   * This file is part of the evoCore framework - {@link http://evocore.net/}
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
  10   *
  11   * {@internal License choice
  12   * - If you have received this file as part of a package, please find the license.txt file in
  13   *   the same folder or the closest folder above for complete license terms.
  14   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  15   *   then you must choose one of the following licenses before using the file:
  16   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  17   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  18   * }}
  19   *
  20   * {@internal Open Source relicensing agreement:
  21   * Daniel HAHLER grants Francois PLANQUE the right to license
  22   * Daniel HAHLER's contributions to this file and the b2evolution project
  23   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  24   * }}
  25   *
  26   * @package evocore
  27   *
  28   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  29   * @author blueyed: Daniel HAHLER.
  30   * @author fplanque: Francois PLANQUE
  31   *
  32   * @version $Id: _blogcache.class.php 6136 2014-03-08 07:59:48Z manuel $
  33   */
  34  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  35  
  36  load_class( '_core/model/dataobjects/_dataobjectcache.class.php', 'DataObjectCache' );
  37  
  38  /**
  39   * Blog Cache Class
  40   *
  41   * @package evocore
  42   */
  43  class BlogCache extends DataObjectCache
  44  {
  45      /**
  46       * Cache by absolute siteurl
  47       * @var array
  48       */
  49      var $cache_siteurl_abs = array();
  50  
  51      /**
  52       * Cache by urlname
  53       * @var array
  54       */
  55      var $cache_urlname = array();
  56  
  57      /**
  58       * Constructor
  59       */
  60  	function BlogCache()
  61      {
  62          parent::DataObjectCache( 'Blog', false, 'T_blogs', 'blog_', 'blog_ID', NULL, '',
  63              /* TRANS: "None" select option */ T_('No blog'), 0 );
  64      }
  65  
  66  
  67      /**
  68       * Add object to cache, handling our own indices.
  69       *
  70       * @param Blog
  71       * @return boolean True on add, false if already existing.
  72       */
  73  	function add( & $Blog )
  74      {
  75          if( ! empty($Blog->siteurl) && preg_match( '~^https?://~', $Blog->siteurl ) )
  76          { // absolute siteurl
  77              $this->cache_siteurl_abs[ $Blog->siteurl ] = & $Blog;
  78          }
  79  
  80          $this->cache_urlname[ $Blog->urlname ] = & $Blog;
  81  
  82          return parent::add( $Blog );
  83      }
  84  
  85  
  86      /**
  87       * Get an object from cache by its url ("siteurl")
  88       *
  89       * Load the cache if necessary
  90       *
  91       * This gets used in /index_multi.php to detect blogs according to the requested HostWithPath
  92       *
  93       * @todo fp> de-factorize. cleanup. make efficient. split access types.
  94       *
  95       * @param string URL of blog to load (should be the whole requested URL/path, e.g. "http://mr.example.com/permalink")
  96       * @param boolean false if you want to return false on error
  97       * @return Blog A Blog object on success, false on failure (may also halt!)
  98       */
  99      function & get_by_url( $req_url, $halt_on_error = true )
 100      {
 101          global $DB, $Debuglog, $baseurl, $basedomain;
 102  
 103          foreach( array_keys($this->cache_siteurl_abs) as $siteurl_abs )
 104          {
 105              if( strpos( $req_url, $siteurl ) === 0 )
 106              { // found in cache
 107                  return $this->cache_siteurl_abs[$siteurl_abs];
 108              }
 109          }
 110  
 111          // Load just the requested object:
 112          $Debuglog->add( "Loading <strong>$this->objtype($req_url)</strong> into cache", 'dataobjects' );
 113  
 114          $req_url_wo_proto = substr( $req_url, strpos( $req_url, '://' ) ); // req_url without protocol, so it matches http and https below
 115  
 116          $sql = 'SELECT *
 117                FROM T_blogs
 118               WHERE ( blog_access_type = "absolute"
 119                       AND ( '.$DB->quote('http'.$req_url_wo_proto).' LIKE CONCAT( blog_siteurl, "%" )
 120                           OR '.$DB->quote('https'.$req_url_wo_proto).' LIKE CONCAT( blog_siteurl, "%" ) ) )
 121                  OR ( blog_access_type = "subdom"
 122                       AND '.$DB->quote($req_url_wo_proto).' LIKE CONCAT( "://", blog_urlname, ".'.$basedomain.'/%" ) )';
 123  
 124          // Match stubs like "http://base/url/STUB?param=1" on $baseurl
 125          /*
 126          if( preg_match( "#^$baseurl([^/?]+)#", $req_url, $match ) )
 127          {
 128              $sql .= "\n OR ( blog_access_type = 'stub' AND blog_stub = ".$DB->quote($match[1])." )";
 129          }
 130          */
 131  
 132          $row = $DB->get_row( $sql, OBJECT, 0, 'Blog::get_by_url()' );
 133  
 134          if( empty( $row ) )
 135          { // Requested object does not exist
 136              if( $halt_on_error ) debug_die( "Requested $this->objtype does not exist!" );
 137  
 138              $r = false;
 139              return $r; // we return by reference!
 140          }
 141  
 142          $Blog = new Blog( $row );
 143          $this->add( $Blog );
 144  
 145          return $Blog;
 146      }
 147  
 148  
 149      /**
 150       * Get a blog from cache by its URL name.
 151       *
 152       * Load the object into cache, if necessary.
 153       *
 154       * @param string URL name of object to load
 155       * @param boolean false if you want to return false on error
 156       * @return Blog|false A Blog object on success, false on failure (may also halt!)
 157       */
 158      function & get_by_urlname( $req_urlname, $halt_on_error = true )
 159      {
 160          global $DB, $Debuglog;
 161  
 162          if( isset($this->cache_urlname[$req_urlname]) )
 163          {
 164              return $this->cache_urlname[$req_urlname];
 165          }
 166  
 167          // Load just the requested object:
 168          $Debuglog->add( "Loading <strong>$this->objtype($req_urlname)</strong> into cache", 'dataobjects' );
 169          $sql = "
 170                  SELECT *
 171                    FROM $this->dbtablename
 172                   WHERE blog_urlname = ".$DB->quote($req_urlname);
 173          $row = $DB->get_row( $sql );
 174  
 175          if( empty( $row ) )
 176          { // Requested object does not exist
 177              if( $halt_on_error ) debug_die( "Requested $this->objtype does not exist!" );
 178              $r = false;
 179              return $r;
 180          }
 181  
 182          $Blog = new Blog( $row );
 183          $this->add( $Blog );
 184  
 185          return $Blog;
 186      }
 187  
 188  
 189      /**
 190       * Load the cache **extensively**
 191       */
 192  	function load_all( $order_by = '', $order_dir = '' )
 193      {
 194          global $Settings;
 195  
 196          if( $order_by == '' )
 197          {    // Use default value from settings
 198              $order_by = $Settings->get('blogs_order_by');
 199          }
 200  
 201          if( $order_dir == '' )
 202          {    // Use default value from settings
 203              $order_dir = $Settings->get('blogs_order_dir');
 204          }
 205  
 206          // Save order
 207          $saved_order = $this->order_by;
 208  
 209          $this->order_by = gen_order_clause( $order_by, $order_dir, $this->dbprefix, $this->dbIDname );
 210  
 211          parent::load_all();
 212  
 213          // Restore
 214          $this->order_by = $saved_order;
 215      }
 216  
 217  
 218      /**
 219       * Load a list of public blogs into the cache
 220       *
 221       * @param string
 222       * @return array of IDs
 223       */
 224  	function load_public( $order_by = '', $order_dir = '' )
 225      {
 226          global $DB, $Settings, $Debuglog;
 227  
 228          $Debuglog->add( "Loading <strong>$this->objtype(public)</strong> into cache", 'dataobjects' );
 229  
 230          if( $order_by == '' )
 231          {    // Use default value from settings
 232              $order_by = $Settings->get('blogs_order_by');
 233          }
 234  
 235          if( $order_dir == '' )
 236          {    // Use default value from settings
 237              $order_dir = $Settings->get('blogs_order_dir');
 238          }
 239  
 240          $SQL = new SQL();
 241          $SQL->SELECT( '*' );
 242          $SQL->FROM( $this->dbtablename );
 243          $SQL->WHERE( 'blog_in_bloglist <> 0' );
 244          $SQL->ORDER_BY( gen_order_clause( $order_by, $order_dir, 'blog_', 'blog_ID' ) );
 245  
 246          foreach( $DB->get_results( $SQL->get(), OBJECT, 'Load public blog list' ) as $row )
 247          {    // Instantiate a custom object
 248              $this->instantiate( $row );
 249          }
 250  
 251          return $DB->get_col( NULL, 0 );
 252      }
 253  
 254  
 255      /**
 256       * Load a list of blogs owner by specific ID into the cache
 257       *
 258       * @param integer
 259       * @param string
 260       * @return array of IDs
 261       */
 262  	function load_owner_blogs( $owner_ID, $order_by = '', $order_dir = '' )
 263      {
 264          global $DB, $Settings, $Debuglog;
 265  
 266          $Debuglog->add( "Loading <strong>$this->objtype(owner={$owner_ID})</strong> into cache", 'dataobjects' );
 267  
 268          if( $order_by == '' )
 269          {    // Use default value from settings
 270              $order_by = $Settings->get('blogs_order_by');
 271          }
 272  
 273          if( $order_dir == '' )
 274          {    // Use default value from settings
 275              $order_dir = $Settings->get('blogs_order_dir');
 276          }
 277  
 278          $SQL = new SQL();
 279          $SQL->SELECT( '*' );
 280          $SQL->FROM( $this->dbtablename );
 281          $SQL->WHERE( 'blog_owner_user_ID = '.$DB->quote($owner_ID) );
 282          $SQL->ORDER_BY( gen_order_clause( $order_by, $order_dir, 'blog_', 'blog_ID' ) );
 283  
 284          foreach( $DB->get_results( $SQL->get(), OBJECT, 'Load owner blog list' ) as $row )
 285          {    // Instantiate a custom object
 286              $this->instantiate( $row );
 287          }
 288  
 289          return $DB->get_col( NULL, 0 );
 290      }
 291  
 292  
 293      /**
 294       * Load blogs a user has permissions for.
 295       *
 296       * @param string permission: 'member' (default), 'browse' (files)
 297       * @param string
 298       * @param integer user ID
 299       * @return array The blog IDs
 300       */
 301  	function load_user_blogs( $permname = 'blog_ismember', $permlevel = 'view', $user_ID = NULL, $order_by = '', $order_dir = '', $limit = NULL )
 302      {
 303          global $DB, $Settings, $Debuglog;
 304  
 305          $Debuglog->add( "Loading <strong>$this->objtype(permission: $permname)</strong> into cache", 'dataobjects' );
 306  
 307          if( $order_by == '' )
 308          {    // Use default value from settings
 309              $order_by = $Settings->get('blogs_order_by');
 310          }
 311  
 312          if( $order_dir == '' )
 313          {    // Use default value from settings
 314              $order_dir = $Settings->get('blogs_order_dir');
 315          }
 316  
 317          if( is_null($user_ID) )
 318          {
 319              global $current_User;
 320              $user_ID = $current_User->ID;
 321              $for_User = $current_User;
 322          }
 323          else
 324          {
 325              $UserCache = & get_UserCache();
 326              $for_User = & $UserCache->get_by_ID( $user_ID );
 327          }
 328          $for_User->get_Group();// ensure Group is set
 329  
 330          $Group = $for_User->Group;
 331          // First check if we have a global access perm:
 332           if( $Group->check_perm( 'blogs', $permlevel ) )
 333          { // If group grants a global permission:
 334              $this->load_all( $order_by, $order_dir );
 335              return $this->get_ID_array();
 336          }
 337  
 338          // Note: We only JOIN in the advanced perms if any given blog has them enabled,
 339          // otherwise they are ignored!
 340          $sql = "SELECT DISTINCT T_blogs.*
 341                    FROM T_blogs LEFT JOIN T_coll_user_perms ON (blog_advanced_perms <> 0
 342                                                                                                    AND blog_ID = bloguser_blog_ID
 343                                                                                                    AND bloguser_user_ID = {$user_ID} )
 344                             LEFT JOIN T_coll_group_perms ON (blog_advanced_perms <> 0
 345                                                                                        AND blog_ID = bloggroup_blog_ID
 346                                                                                        AND bloggroup_group_ID = {$Group->ID} )
 347                   WHERE ";
 348  
 349          if( $permname != 'blog_admin' )
 350          {    // Only the admin perm is not convered by being the owner of the blog:
 351              $sql .= "blog_owner_user_ID = {$user_ID} ";
 352          }
 353  
 354          switch( $permname )
 355          {
 356              case 'blog_ismember':
 357                  $sql .= "OR bloguser_ismember <> 0
 358                                   OR bloggroup_ismember <> 0";
 359                  break;
 360  
 361              case 'blog_post_statuses':
 362                  $sql .= "OR bloguser_perm_poststatuses <> ''
 363                                 OR bloggroup_perm_poststatuses <> ''";
 364                  break;
 365  
 366              case 'blog_comments':
 367                  // user needs to have permission for at least one kind of comments
 368                  $sql .= "OR bloguser_perm_cmtstatuses <> ''
 369                          OR bloggroup_perm_cmtstatuses <> ''";
 370                  break;
 371  
 372              case 'stats':
 373                  $permname = 'blog_properties';    // TEMP
 374              case 'blog_cats':
 375              case 'blog_properties':
 376              case 'blog_admin':
 377              case 'blog_media_browse':
 378                  $short_permname = substr( $permname, 5 );
 379                  $sql .= "OR bloguser_perm_{$short_permname} <> 0
 380                                   OR bloggroup_perm_{$short_permname} <> 0";
 381                  break;
 382  
 383              default:
 384                  debug_die( 'BlogCache::load_user_blogs() : Unsupported perm ['.$permname.']!' );
 385          }
 386  
 387          $sql .= " ORDER BY ".gen_order_clause( $order_by, $order_dir, $this->dbprefix, $this->dbIDname );
 388  
 389          if( $limit )
 390          {
 391              $sql .= " LIMIT {$limit}";
 392          }
 393  
 394          foreach( $DB->get_results( $sql, OBJECT, 'Load user blog list' ) as $row )
 395          {
 396              // Instantiate a custom object
 397              $this->instantiate( $row );
 398          }
 399  
 400          return $DB->get_col( NULL, 0 );
 401      }
 402  
 403  
 404      /**
 405       * Returns form option list with cache contents
 406       *
 407       * Loads the whole cache!
 408       *
 409       * @param integer selected ID
 410       * @param boolean provide a choice for "none" with ID 0
 411       * @param string Callback method name
 412       * @return string HTML tags <option>
 413       */
 414  	function get_option_list( $default = 0, $allow_none = false, $method = 'get_name' )
 415      {
 416          // We force a full load!
 417          $this->load_all();
 418  
 419          return parent::get_option_list( $default, $allow_none, $method );
 420      }
 421  
 422  
 423      /**
 424       * Returns form option list with cache contents
 425       *
 426       * Loads the blogs with forums type!
 427       *
 428       * @param integer selected ID
 429       * @param boolean provide a choice for "none" with ID 0
 430       * @param string Callback method name
 431       * @return string HTML tags <option>
 432       */
 433  	function get_option_list_forums( $default = 0, $allow_none = false, $method = 'get_name' )
 434      {
 435          // Load only blogs with type 'forum'
 436          $this->load_where( 'blog_type = "forum"' );
 437  
 438          return parent::get_option_list( $default, $allow_none, $method );
 439      }
 440  
 441  
 442      /**
 443       * Returns form option list with cache contents
 444       *
 445       * @param integer selected ID
 446       * @param boolean provide a choice for "none" with ID 0
 447       * @param string Callback method name
 448       * @return string HTML tags <option>
 449       */
 450  	function get_option_list_parent( $default = 0, $allow_none = false, $method = 'get_name' )
 451      {
 452          return parent::get_option_list( $default, $allow_none, $method );
 453      }
 454  }
 455  
 456  ?>

title

Description

title

Description

title

Description

title

title

Body