PageRenderTime 69ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/xoops_trust_path/libs/altsys/compilehookadmin.php

http://xoopscube-modules.googlecode.com/
PHP | 281 lines | 184 code | 59 blank | 38 comment | 35 complexity | dc3b5279b92b621d6b517aae2131022a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. // ------------------------------------------------------------------------- //
  3. // compilehookadmin.php (altsys) //
  4. // - XOOPS templates admin module - //
  5. // GIJOE <http://www.peak.ne.jp/> //
  6. // ------------------------------------------------------------------------- //
  7. require_once dirname(__FILE__).'/class/AltsysBreadcrumbs.class.php' ;
  8. include_once dirname(__FILE__).'/include/gtickets.php' ;
  9. include_once dirname(__FILE__).'/include/altsys_functions.php' ;
  10. // this page can be called only from altsys
  11. // if( $xoopsModule->getVar('dirname') != 'altsys' ) die( 'this page can be called only from altsys' ) ;
  12. // language file
  13. altsys_include_language_file( 'compilehookadmin' ) ;
  14. //
  15. // DEFINITIONS
  16. //
  17. $compile_hooks = array(
  18. 'enclosebycomment' => array(
  19. 'pre' => '<!-- begin altsys_tplsadmin %s -->' ,
  20. 'post' => '<!-- end altsys_tplsadmin %s -->' ,
  21. 'success_msg' => _TPLSADMIN_FMT_MSG_ENCLOSEBYCOMMENT ,
  22. 'dt' => _TPLSADMIN_DT_ENCLOSEBYCOMMENT ,
  23. 'dd' => _TPLSADMIN_DD_ENCLOSEBYCOMMENT ,
  24. 'conf_msg' => _TPLSADMIN_CNF_ENCLOSEBYCOMMENT ,
  25. 'skip_theme' => true ,
  26. ) ,
  27. 'enclosebybordereddiv' => array(
  28. 'pre' => '<div class="altsys_tplsadmin_frame" style="border:1px solid black;word-wrap:break-word;">' ,
  29. 'post' => '<br /><a href="'.XOOPS_URL.'/modules/altsys/admin/index.php?mode=admin&amp;lib=altsys&amp;page=mytplsform&amp;tpl_file=%1$s" style="color:red;">Edit:<br />%1$s</a></div>' ,
  30. 'success_msg' => _TPLSADMIN_FMT_MSG_ENCLOSEBYBORDEREDDIV ,
  31. 'dt' => _TPLSADMIN_DT_ENCLOSEBYBORDEREDDIV ,
  32. 'dd' => _TPLSADMIN_DD_ENCLOSEBYBORDEREDDIV ,
  33. 'conf_msg' => _TPLSADMIN_CNF_ENCLOSEBYBORDEREDDIV ,
  34. 'skip_theme' => true ,
  35. ) ,
  36. 'hooksavevars' => array(
  37. 'pre' => '<?php include_once "'.XOOPS_TRUST_PATH.'/libs/altsys/include/compilehook.inc.php" ; tplsadmin_save_tplsvars(\'%s\',$this) ; ?>' ,
  38. 'post' => '' ,
  39. 'success_msg' => _TPLSADMIN_FMT_MSG_HOOKSAVEVARS ,
  40. 'dt' => _TPLSADMIN_DT_HOOKSAVEVARS ,
  41. 'dd' => _TPLSADMIN_DD_HOOKSAVEVARS ,
  42. 'conf_msg' => _TPLSADMIN_CNF_HOOKSAVEVARS ,
  43. 'skip_theme' => false ,
  44. ) ,
  45. 'removehooks' => array(
  46. 'pre' => '' ,
  47. 'post' => '' ,
  48. 'success_msg' => _TPLSADMIN_FMT_MSG_REMOVEHOOKS ,
  49. 'dt' => _TPLSADMIN_DT_REMOVEHOOKS ,
  50. 'dd' => _TPLSADMIN_DD_REMOVEHOOKS ,
  51. 'conf_msg' => _TPLSADMIN_CNF_REMOVEHOOKS ,
  52. 'skip_theme' => false ,
  53. ) ,
  54. ) ;
  55. //
  56. // EXECUTE STAGE
  57. //
  58. // clearing files in templates_c/
  59. if( ! empty( $_POST['clearcache'] ) || ! empty( $_POST['cleartplsvars'] ) ) {
  60. // Ticket Check
  61. if ( ! $xoopsGTicket->check() ) {
  62. redirect_header(XOOPS_URL.'/',3,$xoopsGTicket->getErrors());
  63. }
  64. if( $handler = opendir( XOOPS_COMPILE_PATH . '/' ) ) {
  65. while( ( $file = readdir( $handler ) ) !== false ) {
  66. if( ! empty( $_POST['clearcache'] ) ) {
  67. // judging template cache '*.php'
  68. if( substr( $file , -4 ) !== '.php' ) continue ;
  69. } else {
  70. // judging tplsvars cache 'tplsvars_*'
  71. if( substr( $file , 0 , 9 ) !== 'tplsvars_' ) continue ;
  72. }
  73. $file_path = XOOPS_COMPILE_PATH . '/' . $file ;
  74. @unlink( $file_path ) ;
  75. }
  76. redirect_header( '?mode=admin&lib=altsys&page=compilehookadmin' , 1 , _TPLSADMIN_MSG_CLEARCACHE ) ;
  77. exit ;
  78. } else {
  79. die( 'XOOPS_COMPILE_PATH cannot be opened' ) ;
  80. }
  81. }
  82. // append hooking commands
  83. foreach( $compile_hooks as $command => $compile_hook ) {
  84. if( ! empty( $_POST[$command] ) ) {
  85. // Ticket Check
  86. if ( ! $xoopsGTicket->check() ) {
  87. redirect_header(XOOPS_URL.'/',3,$xoopsGTicket->getErrors());
  88. }
  89. if( $handler = opendir( XOOPS_COMPILE_PATH . '/' ) ) {
  90. $file_count = 0 ;
  91. while( ( $file = readdir( $handler ) ) !== false ) {
  92. // skip /. /.. and hidden files
  93. if( $file{0} == '.' ) continue ;
  94. // skip if the extension is not .html.php
  95. if( substr( $file , -9 ) != '.html.php' ) continue ;
  96. // skip theme.html when comment-mode or div-mode
  97. if( $compile_hook['skip_theme'] && substr( $file , -15 ) == '%theme.html.php' ) $skip_mode = true ;
  98. else $skip_mode = false ;
  99. $file_path = XOOPS_COMPILE_PATH . '/' . $file ;
  100. $file_bodies = file( $file_path ) ;
  101. // remove lines inserted by compilehookadmin
  102. if( strstr( $file_bodies[0] , 'altsys' ) ) {
  103. array_shift( $file_bodies ) ;
  104. }
  105. if( strstr( $file_bodies[count($file_bodies)-1] , 'altsys' ) ) {
  106. array_pop( $file_bodies ) ;
  107. $file_bodies[count($file_bodies)-1] = rtrim( $file_bodies[count($file_bodies)-1] ) ;
  108. }
  109. // get the name of the source template from Smarty's comment
  110. if( preg_match( '/compiled from (\S+)/' , $file_bodies[1] , $regs ) ) {
  111. $tpl_name = $regs[1] ;
  112. } else {
  113. $tpl_name = '__FILE__' ;
  114. }
  115. $fw = fopen( $file_path , 'w' ) ;
  116. // insert "pre" command before the compiled cache
  117. if( $compile_hook['pre'] && ! $skip_mode ) {
  118. fwrite( $fw , sprintf( $compile_hook['pre'] , htmlspecialchars( $tpl_name , ENT_QUOTES ) ) . "\r\n" ) ;
  119. }
  120. // rest of template cache
  121. foreach( $file_bodies as $line ) {
  122. fwrite( $fw , $line ) ;
  123. }
  124. // insert "post" command after the compiled cache
  125. if( $compile_hook['post'] && ! $skip_mode ) {
  126. fwrite( $fw , "\r\n" . sprintf( $compile_hook['post'] , htmlspecialchars( $tpl_name , ENT_QUOTES ) ) ) ;
  127. }
  128. fclose( $fw ) ;
  129. $file_count ++ ;
  130. }
  131. if( $file_count > 0 ) {
  132. redirect_header( '?mode=admin&lib=altsys&page=compilehookadmin' , 3 , sprintf( $compile_hook['success_msg'] , $file_count ) ) ;
  133. exit ;
  134. } else {
  135. redirect_header( '?mode=admin&lib=altsys&page=compilehookadmin' , 3 , _TPLSADMIN_MSG_CREATECOMPILECACHEFIRST ) ;
  136. exit ;
  137. }
  138. } else {
  139. die( 'XOOPS_COMPILE_PATH cannot be opened' ) ;
  140. }
  141. }
  142. }
  143. //
  144. // EXAMINE STAGE
  145. //
  146. // count template vars & compiled caches
  147. $compiledcache_num = 0 ;
  148. $tplsvars_num = 0 ;
  149. if( $handler = opendir( XOOPS_COMPILE_PATH . '/' ) ) {
  150. while( ( $file = readdir( $handler ) ) !== false ) {
  151. if( strncmp( $file , 'tplsvars_' , 9 ) === 0 ) $tplsvars_num ++ ;
  152. else if( substr( $file , -4 ) === '.php' ) $compiledcache_num ++ ;
  153. }
  154. }
  155. // get tplsets
  156. $sql = "SELECT tplset_name,COUNT(distinct tpl_file) FROM ".$xoopsDB->prefix("tplset")." LEFT JOIN ".$xoopsDB->prefix("tplfile")." ON tplset_name=tpl_tplset GROUP BY tpl_tplset ORDER BY tpl_tplset='default' DESC,tpl_tplset" ;
  157. $srs = $xoopsDB->query($sql);
  158. $tplset_options = "<option value=''>----</option>\n" ;
  159. while( list( $tplset , $tpl_count ) = $xoopsDB->fetchRow( $srs ) ) {
  160. $tplset4disp = htmlspecialchars( $tplset , ENT_QUOTES ) ;
  161. $tplset_options .= "<option value='$tplset4disp'>$tplset4disp ($tpl_count)</option>\n" ;
  162. }
  163. //
  164. // FORM STAGE
  165. //
  166. xoops_cp_header() ;
  167. // mymenu
  168. altsys_include_mymenu() ;
  169. // breadcrumbs
  170. $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
  171. $breadcrumbsObj->appendPath( XOOPS_URL.'/modules/altsys/admin/index.php?mode=admin&amp;lib=altsys&amp;page=compilehookadmin' , _MI_ALTSYS_MENU_COMPILEHOOKADMIN ) ;
  172. echo "
  173. <div class='adminnavi'>"._MI_ALTSYS_MENU_COMPILEHOOKADMIN."</div>
  174. <form action='?mode=admin&amp;lib=altsys&amp;page=compilehookadmin' method='post'>
  175. \n";
  176. foreach( $compile_hooks as $command => $compile_hook ) {
  177. echo "
  178. <h4>{$compile_hook['dt']}</h4>
  179. <div class='confirm'>{$compile_hook['dd']}</div>
  180. <p align='center'><input type='submit' name='$command' id='$command' value='"._GO."' onclick='return confirm(\"{$compile_hook['conf_msg']}\");' style='width:120px;' /></p>
  181. <br />
  182. <hr />
  183. \n";
  184. }
  185. echo "
  186. <h4>"._TPLSADMIN_NUMCAP_COMPILEDCACHES.": $compiledcache_num <input type='submit' name='clearcache' value='"._DELETE."' onclick='return confirm(\""._TPLSADMIN_CNF_DELETEOK."\");' /></h4>
  187. <h4>"._TPLSADMIN_NUMCAP_TPLSVARS.": $tplsvars_num <input type='submit' name='cleartplsvars' value='"._DELETE."' onclick='return confirm(\""._TPLSADMIN_CNF_DELETEOK."\");' /></h4>
  188. ".$xoopsGTicket->getTicketHtml( __LINE__ )."
  189. </form>
  190. <br />
  191. <hr />
  192. <form action='?mode=admin&amp;lib=altsys&amp;page=get_tplsvarsinfo' method='post' target='_blank'>
  193. <h4>"._TPLSADMIN_DT_GETTPLSVARSINFO_DW."</h4>
  194. <div class='tips'>"._TPLSADMIN_DD_GETTPLSVARSINFO_DW."</div>
  195. <p align='center'><input type='submit' name='as_dw_extension_zip' value='zip' />
  196. <input type='submit' name='as_dw_extension_tgz' value='tar.gz' />
  197. </p>
  198. </form>
  199. <br />
  200. <hr />
  201. <form action='?mode=admin&amp;lib=altsys&amp;page=get_templates' method='post' target='_blank'>
  202. <h4>"._TPLSADMIN_DT_GETTEMPLATES."</h4>
  203. <div class='tips'>"._TPLSADMIN_DD_GETTEMPLATES."</div>
  204. <p align='center'><select name='tplset'>$tplset_options</select>
  205. <input type='submit' name='download_zip' value='zip' />
  206. <input type='submit' name='download_tgz' value='tar.gz' />
  207. </p>
  208. </form>
  209. <br />
  210. <hr />
  211. <form action='?mode=admin&amp;lib=altsys&amp;page=put_templates' method='post' enctype='multipart/form-data'>
  212. <h4>"._TPLSADMIN_DT_PUTTEMPLATES."</h4>
  213. <div class='tips'>"._TPLSADMIN_DD_PUTTEMPLATES."</div>
  214. <p align='center'><select name='tplset'>$tplset_options</select>
  215. <input type='file' name='tplset_archive' size='60' />
  216. <input type='submit' value='"._SUBMIT."' />
  217. </p>
  218. </form>
  219. <br />
  220. \n" ;
  221. xoops_cp_footer() ;
  222. ?>