PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/AkActionView/AkPhpTemplateHandler.php

http://akelosframework.googlecode.com/
PHP | 167 lines | 123 code | 30 blank | 14 comment | 18 complexity | ecb0c02e9a324cbdfe03ab37ad73c1af MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Akelos Framework - http://www.akelos.org |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
  7. // | Released under the GNU Lesser General Public License, see LICENSE.txt|
  8. // +----------------------------------------------------------------------+
  9. /**
  10. * @package AkelosFramework
  11. * @subpackage AkActionView
  12. * @author Bermi Ferrer <bermi a.t akelos c.om>
  13. * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  14. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  15. */
  16. defined('AK_DEFAULT_TEMPLATE_ENGINE') ? null : define('AK_DEFAULT_TEMPLATE_ENGINE', 'AkSintags');
  17. defined('AK_TEMPLATE_SECURITY_CHECK') ? null : define('AK_TEMPLATE_SECURITY_CHECK', true);
  18. defined('AK_PHP_CODE_SANITIZER_FOR_TEMPLATE_HANDLER')? null : define('AK_PHP_CODE_SANITIZER_FOR_TEMPLATE_HANDLER', 'AkPhpCodeSanitizer');
  19. class AkPhpTemplateHandler
  20. {
  21. var $_options = array();
  22. var $_AkActionView;
  23. var $_templateEngine = AK_DEFAULT_TEMPLATE_ENGINE;
  24. var $_codeSanitizerClass = AK_PHP_CODE_SANITIZER_FOR_TEMPLATE_HANDLER;
  25. function AkPhpTemplateHandler(&$AkActionView)
  26. {
  27. $this->_AkActionView =& $AkActionView;
  28. }
  29. function render(&$____code, $____local_assigns, $____file_path)
  30. {
  31. $this->_options['variables'] = $____local_assigns;
  32. $this->_options['code'] =& $____code;
  33. $this->_options['functions'] = array('');
  34. $this->_options['file_path'] = $____file_path;
  35. if($this->_templateNeedsCompilation()){
  36. if(!class_exists($this->_templateEngine)){
  37. require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'TemplateEngines'.DS.$this->_templateEngine.'.php');
  38. }
  39. $____template_engine_name = $this->_templateEngine;
  40. $TemplateEngine =& new $____template_engine_name();
  41. $TemplateEngine->init(array(
  42. 'code' => $____code,
  43. ));
  44. $____code = $TemplateEngine->toPhp();
  45. if($____code === false){
  46. trigger_error(join("\n",$TemplateEngine->getErrors()), E_USER_ERROR);
  47. return false;
  48. }
  49. if(AK_TEMPLATE_SECURITY_CHECK && $this->_templateNeedsValidation()){
  50. if(!$this->_assertForValidTemplate()){
  51. return false;
  52. }
  53. }
  54. $this->_saveCompiledTemplate();
  55. }
  56. (array)$____local_assigns;
  57. extract($____local_assigns, EXTR_SKIP);
  58. ob_start();
  59. include($this->_getCompiledTemplatePath());
  60. !empty($shared) ? $this->_AkActionView->addSharedAttributes($shared) : null;
  61. return ob_get_clean();
  62. }
  63. function _assertForValidTemplate()
  64. {
  65. static $CodeSanitizer;
  66. if(empty($CodeSanitizer)){
  67. if($this->_codeSanitizerClass == 'AkPhpCodeSanitizer'){
  68. require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkPhpCodeSanitizer.php');
  69. }
  70. $class = $this->_codeSanitizerClass;
  71. $CodeSanitizer = new $class();
  72. }
  73. $CodeSanitizer->setOptions($this->_options);
  74. return $CodeSanitizer->isCodeSecure();
  75. }
  76. function _templateNeedsCompilation()
  77. {
  78. if(!file_exists($this->_getCompiledTemplatePath())){
  79. return true;
  80. }
  81. $tpl_time = @filemtime($this->_getTemplatePath());
  82. $compiled_tpl_time = filemtime($this->_getCompiledTemplatePath());
  83. if($tpl_time > $compiled_tpl_time){
  84. return true;
  85. }
  86. return false;
  87. }
  88. function _templateNeedsValidation()
  89. {
  90. return true;
  91. }
  92. function _getTemplateBasePath()
  93. {
  94. if(empty($this->_options['template_base_path'])){
  95. $template_file_name = $this->_getTemplateFilename();
  96. if(!empty($template_file_name)){
  97. $this->_options['template_base_path'] = rtrim(str_replace($template_file_name,'',$this->_options['file_path']),'\/');
  98. if(defined('AK_COMPILED_VIEWS_DIR') && !strstr($this->_options['template_base_path'], AK_TMP_DIR)){
  99. $this->_options['template_base_path'] = str_replace(AK_BASE_DIR, AK_COMPILED_VIEWS_DIR, $this->_options['template_base_path']);
  100. }
  101. }else{
  102. $this->_options['template_base_path'] = AK_BASE_DIR.DS.'tmp';
  103. }
  104. }
  105. return $this->_options['template_base_path'];
  106. }
  107. function _getTemplatePath()
  108. {
  109. return $this->_options['file_path'];
  110. }
  111. function _getTemplateFilename()
  112. {
  113. $this->_options['template_filename'] = empty($this->_options['template_filename']) && preg_match('/[^\/^\\\]+$/',$this->_options['file_path'],$match) ? $match[0] : @$this->_options['template_filename'];
  114. return $this->_options['template_filename'];
  115. }
  116. function _getCompiledTemplateBasePath()
  117. {
  118. if(empty($this->_options['compiled_template_base_path'])){
  119. $this->_options['compiled_template_base_path'] = $this->_getTemplateBasePath().DS.'compiled';
  120. }
  121. return $this->_options['compiled_template_base_path'];
  122. }
  123. function _getCompiledTemplatePath()
  124. {
  125. if(empty($this->_options['compiled_file_name'])){
  126. $template_filename = $this->_getTemplateFilename();
  127. $this->_options['compiled_file_name'] = $this->_getCompiledTemplateBasePath().DS.
  128. (empty($template_filename) ? 'tpl_'.md5($this->_options['code']) : $template_filename).'.php';
  129. }
  130. return $this->_options['compiled_file_name'];
  131. }
  132. function _saveCompiledTemplate()
  133. {
  134. Ak::file_put_contents($this->_getCompiledTemplatePath(),$this->_options['code']);
  135. }
  136. }
  137. ?>