/kernel/visual/templateedit.php

https://bitbucket.org/crevillo/enetcall · PHP · 237 lines · 181 code · 33 blank · 23 comment · 28 complexity · 6b9aaf3effa1243b66c6e7bef5f446cc MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  4. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  5. * @version 2012.4
  6. * @package kernel
  7. */
  8. $http = eZHTTPTool::instance();
  9. $module = $Params['Module'];
  10. $parameters = $Params["Parameters"];
  11. if ( $http->hasPostVariable( 'Cancel' ) )
  12. {
  13. return $Module->redirectTo( $http->postVariable( 'RedirectToURI' ) );
  14. }
  15. $ini = eZINI::instance();
  16. $tpl = eZTemplate::factory();
  17. $Result = array();
  18. $Result['path'] = array( array( 'url' => false,
  19. 'text' => ezpI18n::tr( 'kernel/design', 'Template edit' ) ) );
  20. $template = "";
  21. $i = 0;
  22. foreach ( $parameters as $param )
  23. {
  24. if ( $i > 0 )
  25. $template .= "/";
  26. $template .= "$param";
  27. $i++;
  28. }
  29. $siteAccess = $Params['SiteAccess'];
  30. if( $siteAccess )
  31. $http->setSessionVariable( 'eZTemplateAdminCurrentSiteAccess', $siteAccess );
  32. else
  33. $siteAccess = $http->sessionVariable( 'eZTemplateAdminCurrentSiteAccess' );
  34. $overrideArray = eZTemplateDesignResource::overrideArray( $siteAccess );
  35. // Check if template already exists
  36. $isExistingTemplate = false;
  37. foreach ( $overrideArray as $overrideSetting )
  38. {
  39. if ( $overrideSetting['base_dir'] . $overrideSetting['template'] == $template )
  40. {
  41. $isExistingTemplate = true;
  42. break;
  43. }
  44. elseif ( isset( $overrideSetting['custom_match'] ) )
  45. {
  46. foreach ( $overrideSetting['custom_match'] as $customMatch )
  47. {
  48. if ( $customMatch['match_file'] == $template )
  49. {
  50. $isExistingTemplate = true;
  51. break 2;
  52. }
  53. }
  54. }
  55. }
  56. if ( $isExistingTemplate == false )
  57. {
  58. $tpl->setVariable( 'template', $template );
  59. $tpl->setVariable( 'template_exists', false );
  60. $tpl->setVariable( 'original_template', false );
  61. $tpl->setVariable( 'site_access', $siteAccess );
  62. $Result['content'] = $tpl->fetch( "design:visual/templateedit_error.tpl" );
  63. return;
  64. }
  65. // Find the main template for this override
  66. $originalTemplate = false;
  67. foreach ( $overrideArray as $overrideSetting )
  68. {
  69. if ( isset( $overrideSetting['custom_match'] ) )
  70. {
  71. foreach ( $overrideSetting['custom_match'] as $customMatch )
  72. {
  73. if ( $customMatch['match_file'] == $template )
  74. {
  75. $originalTemplate = $overrideSetting['template'];
  76. break 2;
  77. }
  78. }
  79. }
  80. }
  81. /* Check if we need to do characterset conversions for editting and saving
  82. * templates. */
  83. $templateConfig = eZINI::instance( 'template.ini' );
  84. $i18nConfig = eZINI::instance( 'i18n.ini' );
  85. /* First we check the HTML Output Charset */
  86. $outputCharset = eZTextCodec::internalCharset();
  87. if ( $module->isCurrentAction( 'Save' ) )
  88. {
  89. if ( $http->hasPostVariable( 'TemplateContent' ) )
  90. {
  91. $templateContent = $http->postVariable( 'TemplateContent' );
  92. if ( $templateConfig->variable( 'CharsetSettings', 'AutoConvertOnSave') == 'enabled' )
  93. {
  94. $outputCharset = eZCharsetInfo::realCharsetCode( $outputCharset );
  95. if ( preg_match( '|{\*\?template.*charset=([a-zA-Z0-9-]*).*\?\*}|', $templateContent, $matches ) )
  96. {
  97. $templateContent = preg_replace( '|({\*\?template.*charset=)[a-zA-Z0-9-]*(.*\?\*})|',
  98. '\\1'. $outputCharset. '\\2',
  99. $templateContent );
  100. }
  101. else
  102. {
  103. $templateCharset = eZCharsetInfo::realCharsetCode( $templateConfig->variable( 'CharsetSettings', 'DefaultTemplateCharset') );
  104. if ( $templateCharset != $outputCharset )
  105. $templateContent = "{*?template charset=$outputCharset?*}\n" . $templateContent;
  106. }
  107. }
  108. else
  109. {
  110. /* Here we figure out the characterset of the template. If there is a charset
  111. * associated with the template in the header we use that one, if not we fall
  112. * back to the INI setting "DefaultTemplateCharset". */
  113. if ( preg_match( '|{\*\?template.*charset=([a-zA-Z0-9-]*).*\?\*}|', $templateContent, $matches ) )
  114. {
  115. $templateCharset = $matches[1];
  116. }
  117. else
  118. {
  119. $templateCharset = $templateConfig->variable( 'CharsetSettings', 'DefaultTemplateCharset');
  120. }
  121. /* If we're saving a template after editting we need to convert it to the template's
  122. * Charset. */
  123. $codec = eZTextCodec::instance( $outputCharset, $templateCharset, false );
  124. if ( $codec )
  125. {
  126. $templateContent = $codec->convertString( $templateContent );
  127. }
  128. }
  129. $fp = fopen( $template, 'w' );
  130. if ( $fp )
  131. {
  132. fwrite( $fp, $templateContent );
  133. }
  134. fclose( $fp );
  135. $siteConfig = eZINI::instance( 'site.ini' );
  136. $filePermissions = $siteConfig->variable( 'FileSettings', 'StorageFilePermissions');
  137. chmod( $template, octdec( $filePermissions ) );
  138. // Expire content view cache
  139. eZContentCacheManager::clearAllContentCache();
  140. $module->redirectTo( '/visual/templateview'. $originalTemplate );
  141. return eZModule::HOOK_STATUS_CANCEL_RUN;
  142. }
  143. }
  144. if ( $module->isCurrentAction( 'Discard' ) )
  145. {
  146. $module->redirectTo( '/visual/templateview'. $originalTemplate );
  147. return eZModule::HOOK_STATUS_CANCEL_RUN;
  148. }
  149. // get the content of the template
  150. $fileName = $template;
  151. if ( !is_readable( $fileName ) )
  152. {
  153. $tpl->setVariable( 'template', $template );
  154. $tpl->setVariable( 'template_exists', true );
  155. $tpl->setVariable( 'original_template', $originalTemplate );
  156. $tpl->setVariable( 'is_readable', false );
  157. $tpl->setVariable( 'site_access', $siteAccess );
  158. $Result['content'] = $tpl->fetch( "design:visual/templateedit_error.tpl" );
  159. return;
  160. }
  161. if ( !is_writable( $fileName ) )
  162. {
  163. if ( $http->hasPostVariable( 'OpenReadOnly' ) )
  164. {
  165. $tpl->setVariable( 'is_writable', false );
  166. }
  167. else
  168. {
  169. $tpl->setVariable( 'template', $template );
  170. $tpl->setVariable( 'template_exists', true );
  171. $tpl->setVariable( 'original_template', $originalTemplate );
  172. $tpl->setVariable( 'is_readable', true );
  173. $tpl->setVariable( 'site_access', $siteAccess );
  174. $Result['content'] = $tpl->fetch( "design:visual/templateedit_error.tpl" );
  175. return;
  176. }
  177. }
  178. else
  179. {
  180. $tpl->setVariable( 'is_writable', true );
  181. }
  182. $templateContent = file_get_contents( $fileName );
  183. /* Here we figure out the characterset of the template. If there is a charset
  184. * associated with the template in the header we use that one, if not we fall
  185. * back to the INI setting "DefaultTemplateCharset". */
  186. if ( preg_match('|{\*\?template.*charset=([a-zA-Z0-9-]*).*\?\*}|', $templateContent, $matches ) )
  187. {
  188. $templateCharset = $matches[1];
  189. }
  190. else
  191. {
  192. $templateCharset = $templateConfig->variable( 'CharsetSettings', 'DefaultTemplateCharset');
  193. }
  194. /* If we're loading a template for editting we need to convert it to the HTTP
  195. * Charset. */
  196. $codec = eZTextCodec::instance( $templateCharset, $outputCharset, false );
  197. if ( $codec )
  198. {
  199. $templateContent = $codec->convertString( $templateContent );
  200. }
  201. $tpl->setVariable( 'template', $template );
  202. $tpl->setVariable( 'template_content', $templateContent );
  203. $Result['content'] = $tpl->fetch( "design:visual/templateedit.tpl" );
  204. ?>