Guest User

Untitled

a guest
May 26th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. <?php
  2. class Template
  3. {
  4. public $vars;
  5. public $tpldirs;
  6. public $xhtml = true;
  7. public $wrapper = "wrapper.php";
  8. public $charset = "utf-8";
  9. public function __construct()
  10. {
  11. $this->var = &$this->vars;
  12. }
  13. public function set($var,$val)
  14. {
  15. if(is_array($var))
  16. {
  17. foreach($var as $k => $v)
  18. { $this->vars[$var] = $val[$k]; }
  19. }
  20. else
  21. {
  22. $this->vars[$var] = $val;
  23. }
  24. }
  25. public function get($var)
  26. {
  27. return $this->vars[$var];
  28. }
  29. public function fetch($file)
  30. {
  31. if(is_array($this->vars)) { foreach($this->vars as $var => $val) { ${$var} = $val; } }
  32. ob_start();
  33. $found = false;
  34. if(!is_array($this->tpldir)) { $this->tpldir = array($this->tpldir); }
  35. foreach($this->tpldir as $dir)
  36. {
  37. if(substr($dir,strlen($dir)-1,1) != "/") { $dir .= "/"; }
  38. if(is_file($dir.$file)) { require_once($dir.$file);$found = true; }
  39. }
  40. if(!$found) { ob_end_clean();return false; }
  41. $content = ob_get_contents();
  42. ob_end_clean();
  43. return $content;
  44. }
  45. public function display($page,$content_type = null)
  46. {
  47. if($content_type == null && $this->xhtml)
  48. {
  49. if(strpos($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") !== FALSE || $_SERVER["HTTP_ACCEPT"] == "")
  50. {
  51. $content_type = "application/xhtml+xml;charset=utf-8";
  52. } else {
  53. $content_type = "text/html;charset=utf-8";
  54. }
  55. }
  56. list($this->mime,$charset) = explode(";",$content_type,2);
  57. list(,$this->charset) = explode("=",$charset,2);
  58. $content = $this->fetch($page);
  59. if(!$content) { $content = $this->fetch($page.".php"); }
  60. if(!$content) { $this->content = $page; } else { $this->content = $content; }
  61.  
  62. header("Content-Type: {$content_type}");
  63.  
  64. echo($this->fetch($this->wrapper));
  65.  
  66. }
  67. public function is_ajax()
  68. {
  69. if
  70. (
  71. (isset($_GET["_ajax"]) && intval($_GET["_ajax"]))
  72. ||
  73. (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'))
  74. )
  75. { return true; }
  76. else
  77. { return false; }
  78. }
  79. public function ajax_print($array,$die = false)
  80. {
  81. header("Cache-Control: no-cache, must-revalidate");
  82. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  83. header("Content-Type: application/json");
  84. if(isset($_REQUEST["callback"]))
  85. {
  86. header("Content-Type: text/javascript;charset=utf-8");
  87. echo($_REQUEST["callback"]."(");
  88. }
  89. echo(json_encode($data));
  90. if(isset($_REQUEST["callback"]))
  91. {
  92. echo(");");
  93. }
  94. if($die) { die(); }
  95. return true;
  96. }
  97. }
Add Comment
Please, Sign In to add comment