PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/content/pdf.php

https://github.com/lserwatka/ezpublish
PHP | 344 lines | 275 code | 36 blank | 33 comment | 40 complexity | 3b00273c34ec90acfa6aaa383582723e MD5 | raw file
  1. <?php
  2. //
  3. // Created on: <19-Jan-2004 20:18:59 kk>
  4. //
  5. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  6. // SOFTWARE NAME: eZ Publish
  7. // SOFTWARE RELEASE: 4.1.x
  8. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  9. // SOFTWARE LICENSE: GNU General Public License v2.0
  10. // NOTICE: >
  11. // This program is free software; you can redistribute it and/or
  12. // modify it under the terms of version 2.0 of the GNU General
  13. // Public License as published by the Free Software Foundation.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of version 2.0 of the GNU General
  21. // Public License along with this program; if not, write to the Free
  22. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. // MA 02110-1301, USA.
  24. //
  25. //
  26. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  27. //
  28. $NodeID = $Params['NodeID'];
  29. $Module = $Params['Module'];
  30. $LanguageCode = $Params['Language'];
  31. $Offset = $Params['Offset'];
  32. $Year = $Params['Year'];
  33. $Month = $Params['Month'];
  34. $Day = $Params['Day'];
  35. if ( isset( $Params['UserParameters'] ) )
  36. {
  37. $UserParameters = $Params['UserParameters'];
  38. }
  39. else
  40. {
  41. $UserParameters = array();
  42. }
  43. if ( $Offset )
  44. $Offset = (int) $Offset;
  45. if ( $Year )
  46. $Year = (int) $Year;
  47. if ( $Month )
  48. $Month = (int) $Month;
  49. if ( $Day )
  50. $Day = (int) $Day;
  51. if ( $NodeID < 2 )
  52. $NodeID = 2;
  53. if ( !is_numeric( $Offset ) )
  54. $Offset = 0;
  55. $ini = eZINI::instance();
  56. $viewCacheEnabled = ( $ini->variable( 'ContentSettings', 'ViewCaching' ) == 'enabled' );
  57. if ( isset( $Params['ViewCache'] ) )
  58. $viewCacheEnabled = $Params['ViewCache'];
  59. $collectionAttributes = false;
  60. if ( isset( $Params['CollectionAttributes'] ) )
  61. $collectionAttributes = $Params['CollectionAttributes'];
  62. $validation = array( 'processed' => false,
  63. 'attributes' => array() );
  64. if ( isset( $Params['AttributeValidation'] ) )
  65. $validation = $Params['AttributeValidation'];
  66. // Check if read operations should be used
  67. $workflowINI = eZINI::instance( 'workflow.ini' );
  68. $operationList = $workflowINI->variableArray( 'OperationSettings', 'AvailableOperations' );
  69. $operationList = array_unique( array_merge( $operationList, $workflowINI->variable( 'OperationSettings', 'AvailableOperationList' ) ) );
  70. if ( in_array( 'content_read', $operationList ) )
  71. {
  72. $useTriggers = true;
  73. }
  74. else
  75. {
  76. $useTriggers = false;
  77. }
  78. $res = eZTemplateDesignResource::instance();
  79. $keys = $res->keys();
  80. if ( isset( $keys['layout'] ) )
  81. $layout = $keys['layout'];
  82. else
  83. $layout = false;
  84. $viewParameters = array( 'offset' => $Offset,
  85. 'year' => $Year,
  86. 'month' => $Month,
  87. 'day' => $Day );
  88. $viewParameters = array_merge( $viewParameters, $UserParameters );
  89. // Should we load the cache now, or check operation
  90. if ( $viewCacheEnabled && ( $useTriggers == false ) )
  91. {
  92. // Note: this code is duplicate, see about 100 lines down
  93. $cacheInfo = eZContentObject::cacheInfo( $Params );
  94. $language = $cacheInfo['language'];
  95. $roleList = $cacheInfo['role_list'];
  96. $discountList = $cacheInfo['discount_list'];
  97. $designSetting = eZTemplateDesignResource::designSetting( 'site' );
  98. if ( eZContentCache::exists( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout,
  99. array( 'view_parameters' => $viewParameters ) ) )
  100. {
  101. $cachePathInfo = eZContentCache::cachePathInfo( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout, false,
  102. array( 'view_parameters' => $viewParameters ) );
  103. contentPDFPassthrough( $cachePathInfo['path'] );
  104. }
  105. }
  106. $user = eZUser::currentUser();
  107. eZDebugSetting::addTimingPoint( 'kernel-content-pdf', 'Operation start' );
  108. $operationResult = eZOperationHandler::execute( 'content', 'read', array( 'node_id' => $NodeID,
  109. 'user_id' => $user->id(),
  110. 'language_code' => $LanguageCode ), null, $useTriggers );
  111. eZDebugSetting::writeDebug( 'kernel-content-pdf', $operationResult, 'operationResult' );
  112. eZDebugSetting::addTimingPoint( 'kernel-content-pdf', 'Operation end' );
  113. eZDebugSetting::writeDebug( 'kernel-content-pdf', $NodeID, 'Fetching node' );
  114. switch( $operationResult['status'] )
  115. {
  116. case eZModuleOperationInfo::STATUS_CONTINUE:
  117. {
  118. if ( ( $operationResult != null ) &&
  119. ( !isset( $operationResult['result'] ) ) &&
  120. ( !isset( $operationResult['redirect_url'] ) || $operationResult['redirect_url'] == null ) )
  121. {
  122. if ( $viewCacheEnabled )
  123. {
  124. // Note: this code is duplicate, see about 100 lines up
  125. $cacheInfo = eZContentObject::cacheInfo( $Params );
  126. $language = $cacheInfo['language'];
  127. $roleList = $cacheInfo['role_list'];
  128. $discountList = $cacheInfo['discount_list'];
  129. $designSetting = eZTemplateDesignResource::designSetting( 'site' );
  130. if ( eZContentCache::exists( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout,
  131. array( 'view_parameters' => $viewParameters ) ) )
  132. {
  133. $cachePathInfo = eZContentCache::cachePathInfo( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout, false,
  134. array( 'view_parameters' => $viewParameters ) );
  135. contentPDFPassthrough( $cachePathInfo['path'] );
  136. }
  137. }
  138. if ( isset( $operationResult['object'] ) )
  139. {
  140. $object = $operationResult[ 'object' ];
  141. }
  142. else
  143. {
  144. return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
  145. }
  146. if ( !( $object instanceof eZContentObject ) )
  147. return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
  148. $node = $operationResult[ 'node' ];
  149. if ( $node === null )
  150. return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
  151. if ( $object === null )
  152. return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
  153. if ( !$object->attribute( 'can_read' ) )
  154. return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
  155. if ( !$node->attribute( 'can_pdf' ) )
  156. return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
  157. if ( $node->attribute( 'is_invisible' ) && !eZContentObjectTreeNode::showInvisibleNodes() )
  158. return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
  159. $cachePathInfo = eZContentCache::cachePathInfo( $designSetting, $NodeID, 'pdf', $language, $Offset, $roleList, $discountList, $layout, false,
  160. array( 'view_parameters' => $viewParameters ) );
  161. $node = eZContentObjectTreeNode::fetch( $NodeID );
  162. contentPDFGenerate( $cachePathInfo['path'] , $node, false, $viewCacheEnabled, $LanguageCode, $viewParameters );
  163. if ( $viewCacheEnabled )
  164. {
  165. eZDebugSetting::writeDebug( 'kernel-content-pdf-cache', 'cache written', 'content/pdf' );
  166. }
  167. contentPDFPassthrough( $cachePathInfo['path'] );
  168. }
  169. }break;
  170. case eZModuleOperationInfo::STATUS_HALTED:
  171. {
  172. if ( isset( $operationResult['redirect_url'] ) )
  173. {
  174. $Module->redirectTo( $operationResult['redirect_url'] );
  175. return;
  176. }
  177. else if ( isset( $operationResult['result'] ) )
  178. {
  179. $result = $operationResult['result'];
  180. $resultContent = false;
  181. if ( is_array( $result ) )
  182. {
  183. if ( isset( $result['content'] ) )
  184. $resultContent = $result['content'];
  185. if ( isset( $result['path'] ) )
  186. $Result['path'] = $result['path'];
  187. }
  188. else
  189. $resultContent = $result;
  190. $Result['content'] = $resultContent;
  191. }
  192. }break;
  193. case eZModuleOperationInfo::STATUS_CANCELLED:
  194. {
  195. $Result = array();
  196. $Result['content'] = 'Content PDF view cancelled<br/>';
  197. }
  198. }
  199. /*!
  200. Passthrough PDF cache file, and exit cleanly
  201. */
  202. function contentPDFPassthrough( $cacheFile )
  203. {
  204. $file = eZClusterFileHandler::instance( $cacheFile );
  205. if( !$file->exists() )
  206. {
  207. eZDebug::writeEror( "Cache-file for pdf doesn't exist", 'content::pdf::contentPDFPassthrough' );
  208. return;
  209. }
  210. $file->fetch( true );
  211. ob_clean();
  212. header( 'Pragma: ' );
  213. header( 'Cache-Control: ' );
  214. /* Set cache time out to 10 seconds, this should be good enough to work around an IE bug */
  215. header( "Expires: ". gmdate( 'D, d M Y H:i:s', time() + 10 ) . ' GMT' );
  216. header( 'X-Powered-By: eZ Publish' );
  217. header( 'Content-Length: '. $file->size() );
  218. header( 'Content-Type: application/pdf' );
  219. header( 'Content-Transfer-Encoding: binary' );
  220. header( 'Accept-Ranges: bytes' );
  221. ob_end_clean();
  222. $fp = @fopen( $cacheFile, 'r' );
  223. @fpassthru( $fp );
  224. fclose( $fp );
  225. eZExecution::cleanExit();
  226. }
  227. /*!
  228. generate PDF, and output stream.
  229. */
  230. function contentPDFGenerate( $cacheFile,
  231. $node,
  232. $object = false,
  233. $viewCacheEnabled = true,
  234. $languageCode = false,
  235. $viewParameters = array() )
  236. {
  237. if ( $languageCode )
  238. {
  239. $node->setCurrentLanguage( $languageCode );
  240. }
  241. if( $object == false )
  242. {
  243. $object = $node->attribute( 'object' );
  244. }
  245. $res = eZTemplateDesignResource::instance();
  246. $res->setKeys( array( array( 'object', $node->attribute( 'contentobject_id' ) ),
  247. array( 'remote_id', $object->attribute( 'remote_id' ) ),
  248. array( 'node_remote_id', $node->attribute( 'remote_id' ) ),
  249. array( 'section', $object->attribute( 'section_id' ) ),
  250. array( 'node', $node->attribute( 'node_id' ) ),
  251. array( 'parent_node', $node->attribute( 'parent_node_id' ) ),
  252. array( 'class', $object->attribute( 'contentclass_id' ) ),
  253. array( 'depth', $node->attribute( 'depth' ) ),
  254. array( 'url_alias', $node->attribute( 'url_alias' ) ),
  255. array( 'class_group', $object->attribute( 'match_ingroup_id_list' ) ),
  256. array( 'class_identifier', $object->attribute( 'class_identifier' ) ) ) );
  257. $tpl = eZTemplate::factory();
  258. $tpl->setVariable( 'view_parameters', $viewParameters );
  259. $tpl->setVariable( 'node', $node );
  260. $tpl->setVariable( 'generate_toc', 0 );
  261. $tpl->setVariable( 'tree_traverse', 0 );
  262. $tpl->setVariable( 'class_array', 0 );
  263. $tpl->setVariable( 'show_frontpage', 0 );
  264. if ( $viewCacheEnabled )
  265. {
  266. $tpl->setVariable( 'generate_file', 1 );
  267. $tpl->setVariable( 'filename', $cacheFile );
  268. }
  269. else
  270. {
  271. $tpl->setVariable( 'generate_file', 0 );
  272. $tpl->setVariable( 'generate_stream', 1 );
  273. }
  274. $textElements = array();
  275. $uri = 'design:node/view/pdf.tpl';
  276. $tpl->setVariable( 'pdf_root_template', 1 );
  277. eZTemplateIncludeFunction::handleInclude( $textElements, $uri, $tpl, '', '' );
  278. $pdf_definition = implode( '', $textElements );
  279. $pdf_definition = str_replace( array( ' ',
  280. "\r\n",
  281. "\t",
  282. "\n" ),
  283. '',
  284. $pdf_definition );
  285. $tpl->setVariable( 'pdf_definition', $pdf_definition );
  286. $uri = 'design:node/view/execute_pdf.tpl';
  287. $textElements = '';
  288. eZTemplateIncludeFunction::handleInclude( $textElements, $uri, $tpl, '', '' );
  289. }
  290. ?>