|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZNodeviewfunctions class 00004 // 00005 // Created on: <20-Apr-2004 11:57:36 bf> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! 00032 \class eZNodeviewfunctions eznodeviewfunctions.php 00033 \brief The class eZNodeviewfunctions does 00034 00035 */ 00036 00037 class eZNodeviewfunctions 00038 { 00039 // Deprecated function for generating the view cache 00040 static function generateNodeView( $tpl, $node, $object, $languageCode, $viewMode, $offset, 00041 $cacheDir, $cachePath, $viewCacheEnabled, 00042 $viewParameters = array( 'offset' => 0, 'year' => false, 'month' => false, 'day' => false ), 00043 $collectionAttributes = false, $validation = false ) 00044 { 00045 require_once( 'kernel/classes/ezclusterfilehandler.php' ); 00046 $cacheFile = eZClusterFileHandler::instance( $cachePath ); 00047 $args = compact( "tpl", "node", "object", "languageCode", "viewMode", "offset", 00048 "viewCacheEnabled", 00049 "viewParameters", 00050 "collectionAttributes", "validation" ); 00051 $Result = $cacheFile->processCache( null, // no retrieve, only generate is called 00052 array( 'eZNodeviewfunctions', 'generateCallback' ), 00053 null, 00054 null, 00055 $args ); 00056 return $Result; 00057 } 00058 00059 // Note: This callback is needed to generate the array which is returned 00060 // back to eZClusterFileHandler for processing. 00061 static function generateCallback( $file, $args ) 00062 { 00063 extract( $args ); 00064 00065 $res = eZNodeViewFunctions::generateNodeViewData( $tpl, $node, $object, $languageCode, $viewMode, $offset, 00066 $viewParameters, $collectionAttributes, $validation ); 00067 00068 00069 // Check if cache time = 0 (viewcache disabled) 00070 $store = $res['cache_ttl'] != 0; 00071 // or if explicitly turned off 00072 if ( !$viewCacheEnabled ) 00073 $store = false; 00074 $retval = array( 'content' => $res, 00075 'scope' => 'viewcache', 00076 'store' => $store ); 00077 if ( $store ) 00078 $retval['binarydata'] = serialize( $res ); 00079 00080 return $retval; 00081 } 00082 00083 static function generateNodeViewData( $tpl, $node, $object, $languageCode, $viewMode, $offset, 00084 $viewParameters = array( 'offset' => 0, 'year' => false, 'month' => false, 'day' => false ), 00085 $collectionAttributes = false, $validation = false ) 00086 { 00087 //include_once( 'kernel/classes/ezsection.php' ); 00088 eZSection::setGlobalID( $object->attribute( 'section_id' ) ); 00089 00090 $section = eZSection::fetch( $object->attribute( 'section_id' ) ); 00091 if ( $section ) 00092 $navigationPartIdentifier = $section->attribute( 'navigation_part_identifier' ); 00093 else 00094 $navigationPartIdentifier = null; 00095 00096 $keyArray = array( array( 'object', $object->attribute( 'id' ) ), 00097 array( 'node', $node->attribute( 'node_id' ) ), 00098 array( 'parent_node', $node->attribute( 'parent_node_id' ) ), 00099 array( 'class', $object->attribute( 'contentclass_id' ) ), 00100 array( 'class_identifier', $node->attribute( 'class_identifier' ) ), 00101 array( 'view_offset', $offset ), 00102 array( 'viewmode', $viewMode ), 00103 array( 'navigation_part_identifier', $navigationPartIdentifier ), 00104 array( 'depth', $node->attribute( 'depth' ) ), 00105 array( 'url_alias', $node->attribute( 'url_alias' ) ), 00106 array( 'class_group', $object->attribute( 'match_ingroup_id_list' ) ) ); 00107 00108 $parentClassID = false; 00109 $parentClassIdentifier = false; 00110 $parentNode = $node->attribute( 'parent' ); 00111 if ( is_object( $parentNode ) ) 00112 { 00113 $parentObject = $parentNode->attribute( 'object' ); 00114 if ( is_object( $parentObject ) ) 00115 { 00116 $parentClass = $parentObject->contentClass(); 00117 if ( is_object( $parentClass ) ) 00118 { 00119 $parentClassID = $parentClass->attribute( 'id' ); 00120 $parentClassIdentifier = $parentClass->attribute( 'identifier' ); 00121 00122 $keyArray[] = array( 'parent_class', $parentClassID ); 00123 $keyArray[] = array( 'parent_class_identifier', $parentClassIdentifier ); 00124 } 00125 } 00126 } 00127 00128 $res = eZTemplateDesignResource::instance(); 00129 $res->setKeys( $keyArray ); 00130 00131 if ( $languageCode ) 00132 { 00133 $oldLanguageCode = $node->currentLanguage(); 00134 $node->setCurrentLanguage( $languageCode ); 00135 } 00136 00137 $tpl->setVariable( 'node', $node ); 00138 $tpl->setVariable( 'viewmode', $viewMode ); 00139 $tpl->setVariable( 'language_code', $languageCode ); 00140 $tpl->setVariable( 'view_parameters', $viewParameters ); 00141 $tpl->setVariable( 'collection_attributes', $collectionAttributes ); 00142 $tpl->setVariable( 'validation', $validation ); 00143 $tpl->setVariable( 'persistent_variable', false ); 00144 00145 $parents = $node->attribute( 'path' ); 00146 00147 $path = array(); 00148 $titlePath = array(); 00149 foreach ( $parents as $parent ) 00150 { 00151 $path[] = array( 'text' => $parent->attribute( 'name' ), 00152 'url' => '/content/view/full/' . $parent->attribute( 'node_id' ), 00153 'url_alias' => $parent->attribute( 'url_alias' ), 00154 'node_id' => $parent->attribute( 'node_id' ) ); 00155 } 00156 00157 $titlePath = $path; 00158 $path[] = array( 'text' => $object->attribute( 'name' ), 00159 'url' => false, 00160 'url_alias' => false, 00161 'node_id' => $node->attribute( 'node_id' ) ); 00162 00163 $titlePath[] = array( 'text' => $object->attribute( 'name' ), 00164 'url' => false, 00165 'url_alias' => false ); 00166 00167 $tpl->setVariable( 'node_path', $path ); 00168 00169 $Result = array(); 00170 $Result['content'] = $tpl->fetch( 'design:node/view/' . $viewMode . '.tpl' ); 00171 $Result['view_parameters'] = $viewParameters; 00172 $Result['path'] = $path; 00173 $Result['title_path'] = $titlePath; 00174 $Result['section_id'] = $object->attribute( 'section_id' ); 00175 $Result['node_id'] = $node->attribute( 'node_id' ); 00176 $Result['navigation_part'] = $navigationPartIdentifier; 00177 00178 $contentInfoArray = array(); 00179 $contentInfoArray['object_id'] = $object->attribute( 'id' ); 00180 $contentInfoArray['node_id'] = $node->attribute( 'node_id' ); 00181 $contentInfoArray['parent_node_id'] = $node->attribute( 'parent_node_id' ); 00182 $contentInfoArray['class_id'] = $object->attribute( 'contentclass_id' ); 00183 $contentInfoArray['class_identifier'] = $node->attribute( 'class_identifier' ); 00184 $contentInfoArray['offset'] = $offset; 00185 $contentInfoArray['viewmode'] = $viewMode; 00186 $contentInfoArray['navigation_part_identifier'] = $navigationPartIdentifier; 00187 $contentInfoArray['node_depth'] = $node->attribute( 'depth' ); 00188 $contentInfoArray['url_alias'] = $node->attribute( 'url_alias' ); 00189 $contentInfoArray['persistent_variable'] = false; 00190 if ( $tpl->variable( 'persistent_variable' ) !== false ) 00191 { 00192 $contentInfoArray['persistent_variable'] = $tpl->variable( 'persistent_variable' ); 00193 $keyArray[] = array( 'persistent_variable', $contentInfoArray['persistent_variable'] ); 00194 $res->setKeys( $keyArray ); 00195 } 00196 $contentInfoArray['class_group'] = $object->attribute( 'match_ingroup_id_list' ); 00197 $contentInfoArray['parent_class_id'] = $parentClassID; 00198 $contentInfoArray['parent_class_identifier'] = $parentClassIdentifier; 00199 00200 $Result['content_info'] = $contentInfoArray; 00201 00202 // Store which templates were used to make this cache. 00203 $Result['template_list'] = $tpl->templateFetchList(); 00204 00205 // Check if time to live is set in template 00206 if ( $tpl->hasVariable( 'cache_ttl' ) ) 00207 { 00208 $cacheTTL = $tpl->variable( 'cache_ttl' ); 00209 } 00210 00211 if ( !isset( $cacheTTL ) ) 00212 { 00213 $cacheTTL = -1; 00214 } 00215 00216 $Result['cache_ttl'] = $cacheTTL; 00217 00218 if ( $languageCode ) 00219 { 00220 $node->setCurrentLanguage( $oldLanguageCode ); 00221 } 00222 00223 return $Result; 00224 } 00225 00226 static function generateViewCacheFile( $user, $nodeID, $offset, $layout, $language, $viewMode, $viewParameters = false, $cachedViewPreferences = false ) 00227 { 00228 //include_once( 'kernel/classes/ezuserdiscountrule.php' ); 00229 //include_once( 'kernel/classes/ezpreferences.php' ); 00230 00231 $limitedAssignmentValueList = $user->limitValueList(); 00232 $roleList = $user->roleIDList(); 00233 $discountList = eZUserDiscountRule::fetchIDListByUserID( $user->attribute( 'contentobject_id' ) ); 00234 00235 if ( !$language ) 00236 { 00237 $language = false; 00238 } 00239 $currentSiteAccess = $GLOBALS['eZCurrentAccess']['name']; 00240 00241 $cacheHashArray = array( $nodeID, 00242 $viewMode, 00243 $language, 00244 $offset, 00245 $layout, 00246 implode( '.', $roleList ), 00247 implode( '.', $limitedAssignmentValueList), 00248 implode( '.', $discountList ), 00249 eZSys::indexFile() ); 00250 00251 // add access type to cache hash if current access is uri (so uri and host doesn't share cache) 00252 if ( $GLOBALS['eZCurrentAccess']['type'] === EZ_ACCESS_TYPE_URI ) 00253 { 00254 $cacheHashArray[] = EZ_ACCESS_TYPE_URI; 00255 } 00256 00257 // Make the cache unique for every case of view parameters 00258 if ( $viewParameters ) 00259 { 00260 $vpString = ""; 00261 ksort( $viewParameters ); 00262 foreach ( $viewParameters as $key => $value ) 00263 { 00264 if ( !$key ) 00265 continue; 00266 $vpString .= 'vp:' . $key . '=' . $value; 00267 } 00268 $cacheHashArray[] = $vpString; 00269 } 00270 00271 // Make the cache unique for every case of the preferences 00272 if ( $cachedViewPreferences === false ) 00273 { 00274 $siteIni = eZINI::instance( ); 00275 $depPreferences = $siteIni->variable( 'ContentSettings', 'CachedViewPreferences' ); 00276 } 00277 else 00278 { 00279 $depPreferences = $cachedViewPreferences; 00280 } 00281 if ( isset ( $depPreferences[$viewMode] ) ) 00282 { 00283 $depPreferences = explode( ';', $depPreferences[$viewMode] ); 00284 $pString = ""; 00285 // Fetch preferences for the specified user 00286 $preferences = eZPreferences::values( $user ); 00287 foreach( $depPreferences as $pref ) 00288 { 00289 $pref = explode( '=', $pref ); 00290 if ( isset( $pref[0] ) ) 00291 { 00292 if ( isset( $preferences[$pref[0]] ) ) 00293 $pString .= 'p:' . $pref[0] . '='. $preferences[$pref[0]]. ';'; 00294 else if ( isset( $pref[1] ) ) 00295 $pString .= 'p:' . $pref[0] . '='. $pref[1]. ';'; 00296 } 00297 } 00298 $cacheHashArray[] = $pString; 00299 } 00300 00301 $ini = eZINI::instance(); 00302 00303 $cacheFile = $nodeID . '-' . md5( implode( '-', $cacheHashArray ) ) . '.cache'; 00304 $extraPath = eZDir::filenamePath( $nodeID ); 00305 $cacheDir = eZDir::path( array( eZSys::cacheDirectory(), $ini->variable( 'ContentSettings', 'CacheDir' ), $currentSiteAccess, $extraPath ) ); 00306 $cachePath = eZDir::path( array( $cacheDir, $cacheFile ) ); 00307 00308 return array( 'cache_path' => $cachePath, 00309 'cache_dir' => $cacheDir, 00310 'cache_file' => $cacheFile ); 00311 } 00312 00313 00314 static function contentViewRetrieve( $file, $mtime, $args ) 00315 { 00316 extract( $args ); 00317 00318 $cacheExpired = false; 00319 00320 // Read Cache file 00321 if ( !eZContentObject::isCacheExpired( $mtime ) ) 00322 { 00323 // $contents = $cacheFile->fetchContents(); 00324 $contents = file_get_contents( $file ); 00325 $Result = unserialize( $contents ); 00326 00327 // Check if cache has expired when cache_ttl is set 00328 $cacheTTL = isset( $Result['cache_ttl'] ) ? $Result['cache_ttl'] : -1; 00329 if ( $cacheTTL > 0 ) 00330 { 00331 $expiryTime = $mtime + $cacheTTL; 00332 if ( time() > $expiryTime ) 00333 { 00334 $cacheExpired = true; 00335 $expiryReason = 'Content cache is expired by cache_ttl=' . $cacheTTL; 00336 } 00337 } 00338 00339 // Check if template source files are newer, but only if the cache is not expired 00340 if ( !$cacheExpired ) 00341 { 00342 $developmentModeEnabled = $ini->variable( 'TemplateSettings', 'DevelopmentMode' ) == 'enabled'; 00343 // Only do filemtime checking when development mode is enabled. 00344 if ( $developmentModeEnabled && 00345 isset( $Result['template_list'] ) ) // And only if there is a list stored in the cache 00346 { 00347 foreach ( $Result['template_list'] as $templateFile ) 00348 { 00349 if ( @filemtime( $templateFile ) > $mtime ) 00350 { 00351 $cacheExpired = true; 00352 $expiryReason = "Content cache is expired by template file '" . $templateFile . "'"; 00353 break; 00354 } 00355 } 00356 } 00357 } 00358 00359 if ( !$cacheExpired ) 00360 { 00361 $keyArray = array( array( 'object', $Result['content_info']['object_id'] ), 00362 array( 'node', $Result['content_info']['node_id'] ), 00363 array( 'parent_node', $Result['content_info']['parent_node_id'] ), 00364 array( 'class', $Result['content_info']['class_id'] ), 00365 array( 'view_offset', $Result['content_info']['offset'] ), 00366 array( 'navigation_part_identifier', $Result['content_info']['navigation_part_identifier'] ), 00367 array( 'viewmode', $Result['content_info']['viewmode'] ), 00368 array( 'depth', $Result['content_info']['node_depth'] ), 00369 array( 'url_alias', $Result['content_info']['url_alias'] ), 00370 array( 'persistent_variable', $Result['content_info']['persistent_variable'] ), 00371 array( 'class_group', $Result['content_info']['class_group'] ), 00372 array( 'parent_class_id', $Result['content_info']['parent_class_id'] ), 00373 array( 'parent_class_identifier', $Result['content_info']['parent_class_identifier'] ) ); 00374 00375 if ( isset( $Result['content_info']['class_identifier'] ) ) 00376 $keyArray[] = array( 'class_identifier', $Result['content_info']['class_identifier'] ); 00377 00378 $res = eZTemplateDesignResource::instance(); 00379 $res->setKeys( $keyArray ); 00380 00381 // set section id 00382 //include_once( 'kernel/classes/ezsection.php' ); 00383 eZSection::setGlobalID( $Result['section_id'] ); 00384 00385 return $Result; 00386 } 00387 } 00388 else 00389 { 00390 $expiryReason = 'Content cache is expired by eZContentObject::isCacheExpired(' . $mtime . ")"; 00391 } 00392 00393 // Cache is expired so return specialized cluster object 00394 if ( !isset( $expiryReason ) ) 00395 $expiryReason = 'Content cache is expired'; 00396 //include_once( 'kernel/classes/ezclusterfilefailure.php' ); 00397 return new eZClusterFileFailure( 1, $expiryReason ); 00398 } 00399 00400 static function contentViewGenerate( $file, $args ) 00401 { 00402 extract( $args ); 00403 $node = eZContentObjectTreeNode::fetch( $NodeID ); 00404 00405 if ( !is_object( $node ) ) 00406 { 00407 return array( 'content' => $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' ), 00408 'store' => false ); 00409 } 00410 00411 $object = $node->attribute( 'object' ); 00412 00413 if ( !is_object( $object ) ) 00414 { 00415 return array( 'content' => $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' ), 00416 'store' => false ); 00417 } 00418 00419 if ( !$object instanceof eZContentObject ) 00420 { 00421 return array( 'content' => $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' ), 00422 'store' => false ); 00423 } 00424 if ( $node === null ) 00425 { 00426 return array( 'content' => $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' ), 00427 'store' => false ); 00428 } 00429 00430 if ( $object === null ) 00431 { 00432 return array( 'content' => $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' ), 00433 'store' => false ); 00434 } 00435 00436 if ( $node->attribute( 'is_invisible' ) && !eZContentObjectTreeNode::showInvisibleNodes() ) 00437 { 00438 return array( 'content' => $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' ), 00439 'store' => false ); 00440 } 00441 00442 // if ( !$object->attribute( 'can_read' ) ) 00443 if ( !$object->canRead() ) 00444 { 00445 return array( 'content' => $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 00446 'kernel', 00447 array( 'AccessList' => $object->accessList( 'read' ) ) ), 00448 'store' => false ); 00449 } 00450 00451 $Result = eZNodeviewfunctions::generateNodeViewData( $tpl, $node, $object, 00452 $LanguageCode, $ViewMode, $Offset, 00453 $viewParameters, $collectionAttributes, 00454 $validation ); 00455 $retval = array( 'content' => $Result, 00456 'scope' => 'viewcache', 00457 'store' => $Result['cache_ttl'] != 0 ); 00458 if ( $file !== false && $retval['store'] ) 00459 $retval['binarydata'] = serialize( $Result ); 00460 return $retval; 00461 } 00462 } 00463 00464 ?>