eZ Publish  [trunk]
pdf.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00004  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00005  * @version //autogentag//
00006  * @package kernel
00007  */
00008 
00009 $NodeID = $Params['NodeID'];
00010 $Module = $Params['Module'];
00011 $LanguageCode = $Params['Language'];
00012 $Offset = $Params['Offset'];
00013 $Year = $Params['Year'];
00014 $Month = $Params['Month'];
00015 $Day = $Params['Day'];
00016 
00017 if ( isset( $Params['UserParameters'] ) )
00018 {
00019     $UserParameters = $Params['UserParameters'];
00020 }
00021 else
00022 {
00023     $UserParameters = array();
00024 }
00025 
00026 if ( $Offset )
00027     $Offset = (int) $Offset;
00028 if ( $Year )
00029     $Year = (int) $Year;
00030 if ( $Month )
00031     $Month = (int) $Month;
00032 if ( $Day )
00033     $Day = (int) $Day;
00034 
00035 if ( $NodeID < 2 )
00036     $NodeID = 2;
00037 
00038 if ( !is_numeric( $Offset ) )
00039     $Offset = 0;
00040 
00041 $ini = eZINI::instance();
00042 $viewCacheEnabled = ( $ini->variable( 'ContentSettings', 'ViewCaching' ) == 'enabled' );
00043 if ( isset( $Params['ViewCache'] ) )
00044     $viewCacheEnabled = $Params['ViewCache'];
00045 
00046 $collectionAttributes = false;
00047 if ( isset( $Params['CollectionAttributes'] ) )
00048     $collectionAttributes = $Params['CollectionAttributes'];
00049 
00050 $validation = array( 'processed' => false,
00051                      'attributes' => array() );
00052 if ( isset( $Params['AttributeValidation'] ) )
00053     $validation = $Params['AttributeValidation'];
00054 
00055 // Check if read operations should be used
00056 $workflowINI = eZINI::instance( 'workflow.ini' );
00057 $operationList = $workflowINI->variableArray( 'OperationSettings', 'AvailableOperations' );
00058 $operationList = array_unique( array_merge( $operationList, $workflowINI->variable( 'OperationSettings', 'AvailableOperationList' ) ) );
00059 if ( in_array( 'content_read', $operationList ) )
00060 {
00061     $useTriggers = true;
00062 }
00063 else
00064 {
00065     $useTriggers = false;
00066 }
00067 
00068 $res = eZTemplateDesignResource::instance();
00069 $keys = $res->keys();
00070 if ( isset( $keys['layout'] ) )
00071     $layout = $keys['layout'];
00072 else
00073     $layout = false;
00074 
00075 $viewParameters = array( 'offset' => $Offset,
00076                          'year' => $Year,
00077                          'month' => $Month,
00078                          'day' => $Day );
00079 
00080 $viewParameters = array_merge( $viewParameters, $UserParameters );
00081 
00082 // Should we load the cache now, or check operation
00083 if ( $viewCacheEnabled && ( $useTriggers == false ) )
00084 {
00085     // Note: this code is duplicate, see about 100 lines down
00086     $cacheInfo = eZContentObject::cacheInfo( $Params );
00087     $language = $cacheInfo['language'];
00088     $roleList = $cacheInfo['role_list'];
00089     $discountList = $cacheInfo['discount_list'];
00090     $designSetting = eZTemplateDesignResource::designSetting( 'site' );
00091     if ( eZContentCache::exists( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout,
00092                                  array( 'view_parameters' => $viewParameters ) ) )
00093     {
00094         $cachePathInfo = eZContentCache::cachePathInfo( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout, false,
00095                                                         array( 'view_parameters' => $viewParameters ) );
00096 
00097         contentPDFPassthrough( $cachePathInfo['path'] );
00098     }
00099 }
00100 
00101 $user = eZUser::currentUser();
00102 
00103 eZDebugSetting::addTimingPoint( 'kernel-content-pdf', 'Operation start' );
00104 
00105 $operationResult = eZOperationHandler::execute( 'content', 'read', array( 'node_id' => $NodeID,
00106                                                                           'user_id' => $user->id(),
00107                                                                           'language_code' => $LanguageCode ), null, $useTriggers );
00108 eZDebugSetting::writeDebug( 'kernel-content-pdf', $operationResult, 'operationResult' );
00109 eZDebugSetting::addTimingPoint( 'kernel-content-pdf', 'Operation end' );
00110 
00111 eZDebugSetting::writeDebug( 'kernel-content-pdf', $NodeID, 'Fetching node' );
00112 
00113 switch( $operationResult['status'] )
00114 {
00115     case eZModuleOperationInfo::STATUS_CONTINUE:
00116     {
00117         if ( ( $operationResult != null ) &&
00118              ( !isset( $operationResult['result'] ) ) &&
00119              ( !isset( $operationResult['redirect_url'] ) || $operationResult['redirect_url'] == null ) )
00120         {
00121             if ( $viewCacheEnabled )
00122             {
00123                 // Note: this code is duplicate, see about 100 lines up
00124                 $cacheInfo = eZContentObject::cacheInfo( $Params );
00125                 $language = $cacheInfo['language'];
00126                 $roleList = $cacheInfo['role_list'];
00127                 $discountList = $cacheInfo['discount_list'];
00128                 $designSetting = eZTemplateDesignResource::designSetting( 'site' );
00129                 if ( eZContentCache::exists( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout,
00130                                              array( 'view_parameters' => $viewParameters ) ) )
00131                 {
00132                     $cachePathInfo = eZContentCache::cachePathInfo( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout, false,
00133                                                                     array( 'view_parameters' => $viewParameters ) );
00134                     contentPDFPassthrough( $cachePathInfo['path'] );
00135                 }
00136             }
00137 
00138             if ( isset( $operationResult['object'] ) )
00139             {
00140                 $object = $operationResult[ 'object' ];
00141             }
00142             else
00143             {
00144                 return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
00145             }
00146 
00147             if ( !( $object instanceof eZContentObject ) )
00148                 return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
00149 
00150             $node = $operationResult[ 'node' ];
00151 
00152             if ( $node === null )
00153                 return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
00154 
00155             if ( $object === null )
00156                 return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
00157 
00158             if ( !$object->attribute( 'can_read' ) )
00159                 return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
00160 
00161             if ( !$node->attribute( 'can_pdf' ) )
00162                 return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
00163 
00164             if ( $node->attribute( 'is_invisible' ) && !eZContentObjectTreeNode::showInvisibleNodes() )
00165                 return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
00166 
00167 
00168             $cachePathInfo = eZContentCache::cachePathInfo( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout, false,
00169                                                             array( 'view_parameters' => $viewParameters ) );
00170             $node = eZContentObjectTreeNode::fetch( $NodeID );
00171 
00172             contentPDFGenerate( $cachePathInfo['path'] , $node, false, $viewCacheEnabled, $LanguageCode, $viewParameters );
00173 
00174             if ( $viewCacheEnabled  )
00175             {
00176                 eZDebugSetting::writeDebug( 'kernel-content-pdf-cache', 'cache written', 'content/pdf' );
00177             }
00178 
00179             contentPDFPassthrough( $cachePathInfo['path'] );
00180         }
00181     }break;
00182     case eZModuleOperationInfo::STATUS_HALTED:
00183     {
00184         if (  isset( $operationResult['redirect_url'] ) )
00185         {
00186             $Module->redirectTo( $operationResult['redirect_url'] );
00187             return;
00188         }
00189         else if ( isset( $operationResult['result'] ) )
00190         {
00191             $result = $operationResult['result'];
00192             $resultContent = false;
00193             if ( is_array( $result ) )
00194             {
00195                 if ( isset( $result['content'] ) )
00196                     $resultContent = $result['content'];
00197                 if ( isset( $result['path'] ) )
00198                     $Result['path'] = $result['path'];
00199             }
00200             else
00201                 $resultContent = $result;
00202             $Result['content'] = $resultContent;
00203         }
00204     }break;
00205     case eZModuleOperationInfo::STATUS_CANCELLED:
00206     {
00207         $Result = array();
00208         $Result['content'] = 'Content PDF view cancelled<br/>';
00209     }
00210 }
00211 
00212 
00213 /*!
00214   Passthrough PDF cache file, and exit cleanly
00215 */
00216 function contentPDFPassthrough( $cacheFile )
00217 {
00218     $file = eZClusterFileHandler::instance( $cacheFile );
00219 
00220     if( !$file->exists() )
00221     {
00222         eZDebug::writeError( "Cache-file for pdf doesn't exist", 'content::pdf::contentPDFPassthrough' );
00223         return;
00224     }
00225 
00226     $file->fetch( true );
00227 
00228     ob_clean();
00229 
00230     header( 'Pragma: ' );
00231     header( 'Cache-Control: ' );
00232     /* Set cache time out to 10 seconds, this should be good enough to work around an IE bug */
00233     header( "Expires: ". gmdate( 'D, d M Y H:i:s', time() + 10 ) . ' GMT' );
00234     header( 'X-Powered-By: eZ Publish' );
00235 
00236     header( 'Content-Length: '. $file->size() );
00237     header( 'Content-Type: application/pdf' );
00238     header( 'Content-Transfer-Encoding: binary' );
00239     header( 'Accept-Ranges: bytes' );
00240 
00241     ob_end_clean();
00242 
00243     $fp = @fopen( $cacheFile, 'r' );
00244     @fpassthru( $fp );
00245     fclose( $fp );
00246 
00247     eZExecution::cleanExit();
00248 }
00249 
00250 /*!
00251   generate PDF, and output stream.
00252 */
00253 function contentPDFGenerate( $cacheFile,
00254                              $node,
00255                              $object = false,
00256                              $viewCacheEnabled = true,
00257                              $languageCode = false,
00258                              $viewParameters = array() )
00259 {
00260     if ( $languageCode )
00261     {
00262         $node->setCurrentLanguage( $languageCode );
00263     }
00264 
00265     if( $object == false )
00266     {
00267         $object = $node->attribute( 'object' );
00268     }
00269 
00270     $res = eZTemplateDesignResource::instance();
00271     $res->setKeys( array( array( 'object', $node->attribute( 'contentobject_id' ) ),
00272                           array( 'remote_id', $object->attribute( 'remote_id' ) ),
00273                           array( 'node_remote_id', $node->attribute( 'remote_id' ) ),
00274                           array( 'section', $object->attribute( 'section_id' ) ),
00275                           array( 'node', $node->attribute( 'node_id' ) ),
00276                           array( 'parent_node', $node->attribute( 'parent_node_id' ) ),
00277                           array( 'class', $object->attribute( 'contentclass_id' ) ),
00278                           array( 'depth', $node->attribute( 'depth' ) ),
00279                           array( 'url_alias', $node->attribute( 'url_alias' ) ),
00280                           array( 'class_group', $object->attribute( 'match_ingroup_id_list' ) ),
00281                           array( 'class_identifier', $object->attribute( 'class_identifier' ) ) ) );
00282 
00283     $tpl = eZTemplate::factory();
00284 
00285     $tpl->setVariable( 'view_parameters', $viewParameters );
00286 
00287     $tpl->setVariable( 'node', $node );
00288     $tpl->setVariable( 'generate_toc', 0 );
00289 
00290     $tpl->setVariable( 'tree_traverse', 0 );
00291     $tpl->setVariable( 'class_array', 0 );
00292     $tpl->setVariable( 'show_frontpage', 0 );
00293 
00294     if ( $viewCacheEnabled )
00295     {
00296         $tpl->setVariable( 'generate_file', 1 );
00297         $tpl->setVariable( 'filename', $cacheFile );
00298     }
00299     else
00300     {
00301         $tpl->setVariable( 'generate_file', 0 );
00302         $tpl->setVariable( 'generate_stream', 1 );
00303     }
00304 
00305     $textElements = array();
00306     $uri = 'design:node/view/pdf.tpl';
00307     $tpl->setVariable( 'pdf_root_template', 1 );
00308     eZTemplateIncludeFunction::handleInclude( $textElements, $uri, $tpl, '', '' );
00309     $pdf_definition = implode( '', $textElements );
00310 
00311     $pdf_definition = str_replace( array( ' ',
00312                                           "\r\n",
00313                                           "\t",
00314                                           "\n" ),
00315                                    '',
00316                                    $pdf_definition );
00317     $tpl->setVariable( 'pdf_definition', $pdf_definition );
00318 
00319     $uri = 'design:node/view/execute_pdf.tpl';
00320     $textElements = '';
00321     eZTemplateIncludeFunction::handleInclude( $textElements, $uri, $tpl, '', '' );
00322 }
00323 ?>