[ Index ] |
|
Code source de b2evolution 2.1.0-beta |
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-2007 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.1 2007/06/25 10:59:32 fplanque 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'); 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 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 = '".$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 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( $sql, OBJECT, 'Load public blog list' ) as $row ) 207 { 208 // Instantiate a custom object 209 $this->instantiate( $row ); 210 } 211 212 return $DB->get_col( NULL, 0 ); 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( $sql, OBJECT, 'Load owner blog list' ) as $row ) 235 { 236 // Instantiate a custom object 237 $this->instantiate( $row ); 238 } 239 240 return $DB->get_col( NULL, 0 ); 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 $Group = $current_User->Group; 263 } 264 else 265 { 266 $UserCache = & get_Cache( 'UserCache' ); 267 $for_User = & $UserCache->get_by_ID( $user_ID ); 268 $Group = $for_User->Group; 269 } 270 271 // First check if we have a global access perm: 272 if( $Group->check_perm( 'blogs', $permlevel ) ) 273 { // If group grants a global permission: 274 $this->load_all(); 275 return $this->get_ID_array(); 276 } 277 278 // Note: We only JOIN in the advanced perms if any given blog has them enabled, 279 // otherwise they are ignored! 280 $sql = "SELECT DISTINCT T_blogs.* 281 FROM T_blogs LEFT JOIN T_coll_user_perms ON (blog_advanced_perms <> 0 282 AND blog_ID = bloguser_blog_ID 283 AND bloguser_user_ID = {$user_ID} ) 284 LEFT JOIN T_coll_group_perms ON (blog_advanced_perms <> 0 285 AND blog_ID = bloggroup_blog_ID 286 AND bloggroup_group_ID = {$Group->ID} ) 287 WHERE "; 288 289 if( $permname != 'blog_admin' ) 290 { // Only the admin perm is not convered by being the owner of the blog: 291 $sql .= "blog_owner_user_ID = {$user_ID} "; 292 } 293 294 switch( $permname ) 295 { 296 case 'blog_ismember': 297 $sql .= "OR bloguser_ismember <> 0 298 OR bloggroup_ismember <> 0"; 299 break; 300 301 case 'blog_post_statuses': 302 $sql .= "OR bloguser_perm_poststatuses <> '' 303 OR bloggroup_perm_poststatuses <> ''"; 304 break; 305 306 case 'stats': 307 $permname = 'blog_properties'; // TEMP 308 case 'blog_cats': 309 case 'blog_properties': 310 case 'blog_admin': 311 case 'blog_comments': 312 case 'blog_media_browse': 313 $short_permname = substr( $permname, 5 ); 314 $sql .= "OR bloguser_perm_{$short_permname} <> 0 315 OR bloggroup_perm_{$short_permname} <> 0"; 316 break; 317 318 default: 319 debug_die( 'BlogCache::load_user_blogs() : Unsupported perm ['.$permname.']!' ); 320 } 321 322 $sql .= " ORDER BY {$this->dbprefix}{$order_by}"; 323 324 if( $limit ) 325 { 326 $sql .= " LIMIT {$limit}"; 327 } 328 329 foreach( $DB->get_results( $sql, OBJECT, 'Load user blog list' ) as $row ) 330 { 331 // Instantiate a custom object 332 $this->instantiate( $row ); 333 } 334 335 return $DB->get_col( NULL, 0 ); 336 } 337 338 339 /** 340 * Returns form option list with cache contents 341 * 342 * Loads the whole cache! 343 * 344 * @param integer selected ID 345 * @param boolean provide a choice for "none" with ID 0 346 */ 347 function get_option_list( $default = 0, $allow_none = false, $method = 'get_name' ) 348 { 349 // We force a full load! 350 $this->load_all(); 351 352 return parent::get_option_list( $default, $allow_none, $method ); 353 } 354 } 355 356 357 /* 358 * $Log: _blogcache.class.php,v $ 359 * Revision 1.1 2007/06/25 10:59:32 fplanque 360 * MODULES (refactored MVC) 361 * 362 * Revision 1.25 2007/05/31 03:02:23 fplanque 363 * Advanced perms now disabled by default (simpler interface). 364 * Except when upgrading. 365 * Enable advanced perms in blog settings -> features 366 * 367 * Revision 1.24 2007/05/30 01:18:56 fplanque 368 * blog owner gets all permissions except advanced/admin settings 369 * 370 * Revision 1.23 2007/05/29 01:17:20 fplanque 371 * advanced admin blog settings are now restricted by a special permission 372 * 373 * Revision 1.22 2007/05/09 01:58:57 fplanque 374 * Widget to display other blogs from same owner 375 * 376 * Revision 1.21 2007/05/09 01:00:24 fplanque 377 * optimized querying for blog lists 378 * 379 * Revision 1.20 2007/04/26 00:11:05 fplanque 380 * (c) 2007 381 * 382 * Revision 1.19 2007/03/25 15:07:38 fplanque 383 * multiblog fixes 384 * 385 * Revision 1.18 2006/12/17 23:44:35 fplanque 386 * minor cleanup 387 * 388 * Revision 1.17 2006/12/07 23:13:10 fplanque 389 * @var needs to have only one argument: the variable type 390 * Otherwise, I can't code! 391 * 392 * Revision 1.16 2006/12/06 18:04:23 fplanque 393 * doc 394 * 395 * Revision 1.15 2006/12/05 01:35:27 blueyed 396 * Hooray for less complexity and the 8th param for DataObjectCache() 397 * 398 * Revision 1.14 2006/12/05 00:34:39 blueyed 399 * Implemented custom "None" option text in DataObjectCache; Added for $ItemStatusCache, $GroupCache, UserCache and BlogCache; Added custom text for Item::priority_options() 400 * 401 * Revision 1.13 2006/11/24 18:27:23 blueyed 402 * Fixed link to b2evo CVS browsing interface in file docblocks 403 */ 404 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 23:58:50 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |