smarty.inc.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. require_once ("$incpath/smarty/libs/Smarty.class.php");
  3. /**
  4. * Turn on sanitisation of all data by default so it's not possible for XSS flaws to occur in PFA
  5. */
  6. class PFASmarty {
  7. protected $template = null;
  8. public function __construct() {
  9. $this->template = new Smarty();
  10. //$this->template->debugging = true;
  11. $incpath = dirname(__FILE__);
  12. $this->template->template_dir = $incpath.'/templates';
  13. $this->template->compile_dir = $incpath.'/templates_c';
  14. $this->template->config_dir = $incpath.'/'.$this->template->config_dir[0];
  15. }
  16. public function assign($key, $value, $sanitise = true) {
  17. $this->template->assign("RAW_$key", $value);
  18. if($sanitise == false) {
  19. return $this->template->assign($key, $value);
  20. }
  21. $clean = $this->sanitise($value);
  22. /* we won't run the key through sanitise() here... some might argue we should */
  23. return $this->template->assign($key, $clean);
  24. }
  25. public function display($template) {
  26. header ("Expires: Sun, 16 Mar 2003 05:00:00 GMT");
  27. header ("Last-Modified: " . gmdate ("D, d M Y H:i:s") . " GMT");
  28. header ("Cache-Control: no-store, no-cache, must-revalidate");
  29. header ("Cache-Control: post-check=0, pre-check=0", false);
  30. header ("Pragma: no-cache");
  31. header ("Content-Type: text/html; charset=UTF-8");
  32. $this->template->display($template);
  33. unset($_SESSION['flash']); # cleanup flash messages
  34. }
  35. /**
  36. * Recursive cleaning of data, using htmlentities - this assumes we only ever output to HTML and we're outputting in UTF-8 charset
  37. *
  38. * @param mixed $data - array or primitive type; objects not supported.
  39. * @return mixed $data
  40. * */
  41. public function sanitise($data) {
  42. if(!is_array($data)) {
  43. return htmlentities($data, ENT_QUOTES, 'UTF-8', false);
  44. }
  45. if(is_array($data)) {
  46. $clean = array();
  47. foreach($data as $key => $value) {
  48. /* as this is a nested data structure it's more likely we'll output the key too (at least in my opinion, so we'll sanitise it too */
  49. $clean[$this->sanitise($key)] = $this->sanitise($value);
  50. }
  51. return $clean;
  52. }
  53. }
  54. }
  55. $smarty = new PFASmarty();
  56. if (!isset($rel_path)) $rel_path = ''; # users/* sets this to '../'
  57. $CONF['theme_css'] = $rel_path . htmlentities($CONF['theme_css']);
  58. if (!empty($CONF['theme_custom_css'])) $CONF['theme_custom_css'] = $rel_path . htmlentities($CONF['theme_custom_css']);
  59. $CONF['theme_logo'] = $rel_path . htmlentities($CONF['theme_logo']);
  60. $smarty->assign ('CONF', $CONF);
  61. $smarty->assign ('PALANG', $PALANG);
  62. $smarty->assign('url_domain', '');
  63. //*** footer.tpl
  64. $smarty->assign ('version', $version);
  65. //*** menu.tpl
  66. $smarty->assign ('boolconf_alias_domain', Config::bool('alias_domain'));
  67. $smarty->assign ('authentication_has_role', array ('global_admin' => authentication_has_role ('global-admin'), 'admin' => authentication_has_role ('admin'), 'user' => authentication_has_role ('user')));
  68. function select_options($aValues, $aSelected) {
  69. $ret_val = '';
  70. foreach ($aValues as $val) {
  71. $ret_val .= '<option value="'.htmlentities($val).'"';
  72. if (in_array ($val, $aSelected))
  73. $ret_val .= ' selected="selected"';
  74. $ret_val .= '>'.htmlentities($val).'</option>';
  75. }
  76. return $ret_val;
  77. }
  78. function eval_size ($aSize) {
  79. if ($aSize == 0) {$ret_val = Config::Lang('pOverview_unlimited'); }
  80. elseif ($aSize < 0) {$ret_val = Config::Lang('pOverview_disabled'); }
  81. else {$ret_val = $aSize; }
  82. return $ret_val;
  83. }
  84. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
  85. ?>