/class/smarty/xoops_plugins/resource.db.php

https://gitlab.com/VoyaTrax/vtCMS · PHP · 87 lines · 62 code · 10 blank · 15 comment · 13 complexity · fa099839f245f2e89454f3a532493c0e MD5 · raw file

  1. <?php
  2. /*
  3. * Smarty plugin
  4. * -------------------------------------------------------------
  5. * File: resource.db.php
  6. * Type: resource
  7. * Name: db
  8. * Purpose: Fetches templates from a database
  9. * -------------------------------------------------------------
  10. */
  11. function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty) {
  12. if ( !$tpl = smarty_resource_db_tplinfo( $tpl_name ) ) {
  13. return false;
  14. }
  15. if ( is_object( $tpl ) ) {
  16. $tpl_source = $tpl->getVar( 'tpl_source', 'n' );
  17. } else {
  18. $fp = fopen( $tpl, 'r' );
  19. $tpl_source = fread( $fp, filesize( $tpl ) );
  20. fclose( $fp );
  21. }
  22. return true;
  23. }
  24. function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty) {
  25. if ( !$tpl = smarty_resource_db_tplinfo( $tpl_name ) ) {
  26. return false;
  27. }
  28. if ( is_object( $tpl ) ) {
  29. $tpl_timestamp = $tpl->getVar( 'tpl_lastmodified', 'n' );
  30. } else {
  31. $tpl_timestamp = filemtime( $tpl );
  32. }
  33. return true;
  34. }
  35. function smarty_resource_db_secure($tpl_name, &$smarty)
  36. {
  37. // assume all templates are secure
  38. return true;
  39. }
  40. function smarty_resource_db_trusted($tpl_name, &$smarty)
  41. {
  42. // not used for templates
  43. }
  44. function smarty_resource_db_tplinfo( $tpl_name ) {
  45. static $cache = array();
  46. global $xoopsConfig;
  47. if ( isset( $cache[$tpl_name] ) ) {
  48. return $cache[$tpl_name];
  49. }
  50. $tplset = $xoopsConfig['template_set'];
  51. $theme = isset( $xoopsConfig['theme_set'] ) ? $xoopsConfig['theme_set'] : 'default';
  52. $tplfile_handler =& xoops_gethandler('tplfile');
  53. // If we're not using the "default" template set, then get the templates from the DB
  54. if ( $tplset != "default" ) {
  55. $tplobj = $tplfile_handler->find( $tplset, null, null, null, $tpl_name, true);
  56. if ( count( $tplobj ) ) {
  57. return $cache[$tpl_name] = $tplobj[0];
  58. }
  59. }
  60. // If we'using the default tplset, get the template from the filesystem
  61. $tplobj = $tplfile_handler->find( "default", null, null, null, $tpl_name);
  62. if ( !count( $tplobj ) ) {
  63. return $cache[$tpl_name] = false;
  64. }
  65. $tplobj = $tplobj[0];
  66. $module = $tplobj->getVar( 'tpl_module', 'n' );
  67. $type = $tplobj->getVar( 'tpl_type', 'n' );
  68. $blockpath = ( $type == 'block' ) ? 'blocks/' : '';
  69. // First, check for an overloaded version within the theme folder
  70. $filepath = XOOPS_THEME_PATH . "/$theme/modules/$module/$blockpath$tpl_name";
  71. if ( !file_exists( $filepath ) ) {
  72. // If no custom version exists, get the tpl from its default location
  73. $filepath = XOOPS_ROOT_PATH . "/modules/$module/templates/$blockpath$tpl_name";
  74. }
  75. return $cache[$tpl_name] = $filepath;
  76. }
  77. ?>