psf
 All Classes Functions Variables
htmlpage.php
1 <?php
2 
3 // Part of simple php framework (spf)
4 
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 
15 // Copyright (c) Petr Bena <petr@bena.rocks> 2015 - 2017
16 
17 if (!defined("PSF_ENTRY_POINT"))
18  die("Not a valid psf entry point");
19 
20 require_once (dirname(__FILE__) . "/../default_config.php");
21 require_once (dirname(__FILE__) . "/html/container.php");
22 require_once (dirname(__FILE__) . "/html/primitive_object.php");
23 require_once (dirname(__FILE__) . "/csspage.php");
24 
25 
32 class HtmlPage extends HtmlContainer
33 {
35  public $Title;
37  public $Body;
39  public $UseTidy = false;
40  public $Language;
41  public $TextEncoding;
42  public $CssFile = NULL;
43  public $InternalCss = array();
44  public $ExternalCss = array();
45  public $Style = NULL;
46  public $ExternalJs = array();
47  public $Prefix_Head = '';
48  public $Suffix_Head = '';
49  public $InternalJs = array();
50  public $HtmlVersion = 5;
51  public $Encoding = "UTF-8";
52  public $AutoRefresh = 0;
53  public $Header_Meta = array();
54 
55  function __construct($_title, $_parent = NULL)
56  {
57  parent::__construct($_parent);
58  global $psf_language, $psf_encoding;
59  $this->Style = new CssPage();
60  $this->TextEncoding = $psf_encoding;
61  $this->Language = $psf_language;
62  $this->Title = $_title;
63  }
64 
65  private function getHeader()
66  {
67  $_header = "<!DOCTYPE html>\n";
68  $_header .= "<html>\n";
69  $_header .= " <head>\n";
70  $_header .= $this->Prefix_Head;
71  if ($this->HtmlVersion == 4)
72  $_header .= " <meta http-equiv=\"Content-Type\" content=\"text/html;charset=$this->Encoding\">\n";
73  else if ($this->HtmlVersion > 4)
74  $_header .= " <meta charset=\"$this->Encoding\">\n";
75  else
76  $_header .= " <!-- Unsupported html version: $this->HtmlVersion -->\n";
77  $_header .= " <meta http-equiv=\"Content-Language\" content=\"$this->Language\">\n";
78  if ($this->AutoRefresh > 0)
79  $_header .= " <meta http-equiv=\"refresh\" content=\"" . $this->AutoRefresh . "\">\n";
80  foreach ($this->Header_Meta as $key => $value)
81  $_header .= " <meta name=\"" . $key . "\" content=\"" . $value . "\">\n";
82  $_header .= " <title>$this->Title</title>\n";
83  foreach ($this->ExternalCss as $style)
84  $_header .= " <link rel='stylesheet' type='text/css' href='$style'>\n";
85  foreach ($this->ExternalJs as $js)
86  $_header .= " <script type='text/javascript' src='$js'></script>\n";
87  foreach ($this->InternalJs as $script)
88  {
89  $_header .= " <script type=\"text/javascript\">\n";
90  $_header .= psf_indent_text($script, 6);
91  $_header .= " </script>\n";
92  }
93  if ($this->CssFile !== NULL)
94  $_header .= " <link rel='stylesheet' type='text/css' href='$this->CssFile'>\n";
95  foreach ($this->InternalCss as $style)
96  {
97  $_header .= " <style>\n";
98  $_header .= psf_indent_text($style, 6);
99  $_header .= " </style>\n";
100  }
101  if ($this->Style !== NULL)
102  {
103  $_header .= " <style>\n";
104  $_header .= $this->Style->FetchCss(8);
105  $_header .= " </style>\n";
106  }
107  $_header .= $this->Suffix_Head;
108  $_header .= " </head>\n";
109  return $_header;
110  }
111 
113  public function AppendHeader($text, $level = 1)
114  {
115  $this->AppendHtmlLine("<h$level>" . htmlspecialchars($text) . "</h$level>");
116  }
117 
118  private function getBody()
119  {
120  $indent = 4;
121  $_b = " <body>\n";
122  $_b .= $this->Body;
123  foreach ($this->Items as $html)
124  {
125  // Convert the object to html
126  if ($this->Indent && $html->Indent)
127  $_b .= psf_indent_text($html->ToHtml(), $indent);
128  else
129  $_b .= $html->ToHtml() . "\n";
130  }
131  $_b .= " </body>\n";
132  return $_b;
133  }
134 
135  private function getFooter()
136  {
137  $_f = "</html>\n";
138  return $_f;
139  }
140 
142  public function PrintHtml()
143  {
144  echo $this->ToHtml();
145  return true;
146  }
147 
149  public function ToHtml()
150  {
151  // we first precache whole content in buffer, because if there were some exceptions, we don't want to get only partial html text
152  $_header = $this->getHeader();
153  $_body = $this->getBody();
154  $_footer = $this->getFooter();
155  if ($this->UseTidy)
156  {
157  $tidy = new tidy;
158  $config = array( 'indent' => true, 'wrap' => 800 );
159  $tidy->parseString($_header . $_body . $_footer, $config, 'utf8');
160  return tidy_get_output($tidy);
161  } else
162  {
163  return ($_header . $_body . $_footer);
164  }
165  }
166 }
167