b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (CVS HEAD) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _blogcache.class.php

Documentation is available at _blogcache.class.php

  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-2010 by Francois PLANQUE - {@link http://fplanque.net/}
  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,v 1.12 2010/02/08 17:52:09 efy-yury Exp $
  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'));
  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.         ifempty($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.         foreacharray_keys($this->cache_siteurl_absas $siteurl_abs )
  104.         {
  105.             ifstrpos$req_url$siteurl === )
  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_urlstrpos$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$sqlOBJECT0'Blog::get_by_url()' );
  133.  
  134.         ifempty$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|falseA 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.         ifisset($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.         ifempty$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 a list of public blogs into the cache
  191.      *
  192.      * @param string 
  193.      * @return array of IDs
  194.      */
  195.     function load_public$order_by 'ID' )
  196.     {
  197.         global $DB$Debuglog;
  198.  
  199.         $Debuglog->add"Loading <strong>$this->objtype(public)</strong> into cache"'dataobjects' );
  200.  
  201.         $sql "SELECT *
  202.                   FROM {$this->dbtablename}
  203.                  WHERE blog_in_bloglist <> 0
  204.                  ORDER BY {$this->dbprefix}{$order_by}";
  205.  
  206.         foreach( $DB->get_results$sqlOBJECT'Load public blog list' as $row )
  207.         {
  208.             // Instantiate a custom object
  209.             $this->instantiate$row );
  210.         }
  211.  
  212.         return $DB->get_colNULL);
  213.     }
  214.  
  215.  
  216.     /**
  217.      * Load a list of blogs owner by specific ID into the cache
  218.      *
  219.      * @param integer
  220.      * @param string
  221.      * @return array of IDs
  222.      */
  223.     function load_owner_blogs( $owner_ID, $order_by = 'ID' )
  224.     {
  225.         global $DB, $Debuglog;
  226.  
  227.         $Debuglog->add"Loading <strong>$this->objtype(owner={$owner_ID})</strong> into cache", 'dataobjects' );
  228.  
  229.         $sql = "SELECT *
  230.                   FROM {$this->dbtablename}
  231.                  WHERE blog_owner_user_ID = {$owner_ID}
  232.                  ORDER BY {$this->dbprefix}{$order_by}";
  233.  
  234.         foreach( $DB->get_results$sqlOBJECT'Load owner blog list' as $row )
  235.         {
  236.             // Instantiate a custom object
  237.             $this->instantiate$row );
  238.         }
  239.  
  240.         return $DB->get_colNULL);
  241.     }
  242.  
  243.  
  244.     /**
  245.      * Load blogs a user has permissions for.
  246.      *
  247.      * @param string permission: 'member' (default), 'browse' (files)
  248.      * @param string
  249.      * @param integer user ID
  250.      * @return array The blog IDs
  251.      */
  252.     function load_user_blogs( $permname = 'blog_ismember', $permlevel = 'view', $user_ID = NULL, $order_by = 'ID', $limit = NULL )
  253.     {
  254.         global $DB, $Debuglog;
  255.  
  256.         $Debuglog->add"Loading <strong>$this->objtype(permission: $permname)</strong> into cache", 'dataobjects' );
  257.  
  258.         if( is_null($user_ID) )
  259.         {
  260.             global $current_User;
  261.             $user_ID = $current_User->ID;
  262.             $for_User $current_User;
  263.         }
  264.         else
  265.         {
  266.             $UserCache = & get_UserCache();
  267.             $for_User = & $UserCache->get_by_ID$user_ID );
  268.         }
  269.         $for_User->get_Group();// ensure Group is set
  270.  
  271.         $Group $for_User->Group;
  272.         // First check if we have a global access perm:
  273.          if$Group->check_perm'blogs'$permlevel ) )
  274.         { // If group grants a global permission:
  275.             $this->load_all();
  276.             return $this->get_ID_array();
  277.         }
  278.  
  279.         // Note: We only JOIN in the advanced perms if any given blog has them enabled,
  280.         // otherwise they are ignored!
  281.         $sql = "SELECT DISTINCT T_blogs.*
  282.                   FROM T_blogs LEFT JOIN T_coll_user_perms ON (blog_advanced_perms <> 0
  283.                                                                                                   AND blog_ID = bloguser_blog_ID
  284.                                                                                                   AND bloguser_user_ID = {$user_ID} )
  285.                            LEFT JOIN T_coll_group_perms ON (blog_advanced_perms <> 0
  286.                                                                                       AND blog_ID = bloggroup_blog_ID
  287.                                                                                       AND bloggroup_group_ID = {$Group->ID} )
  288.                  WHERE ";
  289.  
  290.         if( $permname != 'blog_admin' )
  291.         {    // Only the admin perm is not convered by being the owner of the blog:
  292.             $sql .= "blog_owner_user_ID = {$user_ID} ";
  293.         }
  294.  
  295.         switch( $permname )
  296.         {
  297.             case 'blog_ismember':
  298.                 $sql .= "OR bloguser_ismember <> 0
  299.                                  OR bloggroup_ismember <> 0";
  300.                 break;
  301.  
  302.             case 'blog_post_statuses':
  303.                 $sql .= "OR bloguser_perm_poststatuses <> ''
  304.                                OR bloggroup_perm_poststatuses <> ''";
  305.                 break;
  306.  
  307.             case 'stats':
  308.                 $permname = 'blog_properties';    // TEMP
  309.             case 'blog_cats':
  310.             case 'blog_properties':
  311.             case 'blog_admin':
  312.             case 'blog_comments':
  313.             case 'blog_media_browse':
  314.                 $short_permname = substr( $permname, 5 );
  315.                 $sql .= "OR bloguser_perm_{$short_permname} <> 0
  316.                                  OR bloggroup_perm_{$short_permname} <> 0";
  317.                 break;
  318.  
  319.             default:
  320.                 debug_die( 'BlogCache::load_user_blogs() : Unsupported perm ['.$permname.']!' );
  321.         }
  322.  
  323.         $sql .= " ORDER BY {$this->dbprefix}{$order_by}";
  324.  
  325.         if( $limit )
  326.         {
  327.             $sql .= " LIMIT {$limit}";
  328.         }
  329.  
  330.         foreach( $DB->get_results$sqlOBJECT'Load user blog list' as $row )
  331.         {
  332.             // Instantiate a custom object
  333.             $this->instantiate$row );
  334.         }
  335.  
  336.         return $DB->get_colNULL);
  337.     }
  338.  
  339.  
  340.     /**
  341.      * Returns form option list with cache contents
  342.      *
  343.      * Loads the whole cache!
  344.      *
  345.      * @param integer selected ID
  346.      * @param boolean provide a choice for "none" with ID 0
  347.      */
  348.     function get_option_list( $default = 0, $allow_none = false, $method = 'get_name' )
  349.     {
  350.         // We force a full load!
  351.         $this->load_all();
  352.  
  353.         return parent::get_option_list$default$allow_none$method );
  354.     }
  355. }
  356.  
  357.  
  358. /*
  359.  * $Log: _blogcache.class.php,v $
  360.  * Revision 1.12  2010/02/08 17:52:09  efy-yury
  361.  * copyright 2009 -> 2010
  362.  *
  363.  * Revision 1.11  2009/09/26 12:00:42  tblue246
  364.  * Minor/coding style
  365.  *
  366.  * Revision 1.10  2009/09/25 07:32:52  efy-cantor
  367.  * replace get_cache to get_*cache
  368.  *
  369.  * Revision 1.9  2009/09/14 12:43:05  efy-arrin
  370.  * Included the ClassName in load_class() call with proper UpperCase
  371.  *
  372.  * Revision 1.8  2009/09/05 18:17:40  tblue246
  373.  * DataObjectCache/BlogCache::get_option_list(): Back again... Allow custom value for "None" option and use 0 for BlogCache.
  374.  *
  375.  * Revision 1.6  2009/09/03 15:51:52  tblue246
  376.  * Doc, "refix", use "0" instead of an empty string for the "No blog" option.
  377.  *
  378.  * Revision 1.5  2009/03/08 23:57:42  fplanque
  379.  * 2009
  380.  *
  381.  * Revision 1.4  2008/03/04 22:27:43  blueyed
  382.  * MFB: Fix SQL injection through requested URL (commented out?!); fix indent
  383.  *
  384.  * Revision 1.3  2008/01/21 09:35:26  fplanque
  385.  * (c) 2008
  386.  *
  387.  * Revision 1.2  2007/12/20 12:01:56  yabs
  388.  * bug fix
  389.  *
  390.  * Revision 1.1  2007/06/25 10:59:32  fplanque
  391.  * MODULES (refactored MVC)
  392.  *
  393.  * Revision 1.25  2007/05/31 03:02:23  fplanque
  394.  * Advanced perms now disabled by default (simpler interface).
  395.  * Except when upgrading.
  396.  * Enable advanced perms in blog settings -> features
  397.  *
  398.  * Revision 1.24  2007/05/30 01:18:56  fplanque
  399.  * blog owner gets all permissions except advanced/admin settings
  400.  *
  401.  * Revision 1.23  2007/05/29 01:17:20  fplanque
  402.  * advanced admin blog settings are now restricted by a special permission
  403.  *
  404.  * Revision 1.22  2007/05/09 01:58:57  fplanque
  405.  * Widget to display other blogs from same owner
  406.  *
  407.  * Revision 1.21  2007/05/09 01:00:24  fplanque
  408.  * optimized querying for blog lists
  409.  *
  410.  * Revision 1.20  2007/04/26 00:11:05  fplanque
  411.  * (c) 2007
  412.  *
  413.  * Revision 1.19  2007/03/25 15:07:38  fplanque
  414.  * multiblog fixes
  415.  *
  416.  * Revision 1.18  2006/12/17 23:44:35  fplanque
  417.  * minor cleanup
  418.  *
  419.  * Revision 1.17  2006/12/07 23:13:10  fplanque
  420.  * @var needs to have only one argument: the variable type
  421.  * Otherwise, I can't code!
  422.  *
  423.  * Revision 1.16  2006/12/06 18:04:23  fplanque
  424.  * doc
  425.  *
  426.  * Revision 1.15  2006/12/05 01:35:27  blueyed
  427.  * Hooray for less complexity and the 8th param for DataObjectCache()
  428.  *
  429.  * Revision 1.14  2006/12/05 00:34:39  blueyed
  430.  * Implemented custom "None" option text in DataObjectCache; Added for $ItemStatusCache, $GroupCache, UserCache and BlogCache; Added custom text for Item::priority_options()
  431.  *
  432.  * Revision 1.13  2006/11/24 18:27:23  blueyed
  433.  * Fixed link to b2evo CVS browsing interface in file docblocks
  434.  */

Documentation generated on Sat, 06 Mar 2010 04:02:24 +0100 by phpDocumentor 1.4.2. This site is hosted and maintained by Daniel HAHLER (Contact).