psf
htmlpage.php
1 <?php
2 
3 // Part of php simple framework (psf)
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 - 2018
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 lang=\"$this->Language\">\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  if ($this->AutoRefresh > 0)
78  $_header .= " <meta http-equiv=\"refresh\" content=\"" . $this->AutoRefresh . "\">\n";
79  foreach ($this->Header_Meta as $key => $value)
80  $_header .= " <meta name=\"" . $key . "\" content=\"" . $value . "\">\n";
81  $_header .= " <title>$this->Title</title>\n";
82  foreach ($this->ExternalCss as $style)
83  $_header .= " <link rel='stylesheet' type='text/css' href='$style'>\n";
84  foreach ($this->ExternalJs as $js)
85  $_header .= " <script src='$js'></script>\n";
86  foreach ($this->InternalJs as $script)
87  {
88  $_header .= " <script type=\"text/javascript\">\n";
89  $_header .= psf_indent_text($script, 6);
90  $_header .= " </script>\n";
91  }
92  if ($this->CssFile !== NULL)
93  $_header .= " <link rel='stylesheet' type='text/css' href='$this->CssFile'>\n";
94  foreach ($this->InternalCss as $style)
95  {
96  $_header .= " <style>\n";
97  $_header .= psf_indent_text($style, 6);
98  $_header .= " </style>\n";
99  }
100  if ($this->Style !== NULL)
101  {
102  $_header .= " <style>\n";
103  $_header .= $this->Style->FetchCss(8);
104  $_header .= " </style>\n";
105  }
106  $_header .= $this->Suffix_Head;
107  $_header .= " </head>\n";
108  return $_header;
109  }
110 
111  private function getBody()
112  {
113  $indent = 4;
114  $_b = " <body>\n";
115  $_b .= $this->Body;
116  foreach ($this->Items as $html)
117  {
118  // Convert the object to html
119  if ($this->Indent && $html->Indent)
120  $_b .= psf_indent_text($html->ToHtml(), $indent);
121  else
122  $_b .= $html->ToHtml() . "\n";
123  }
124  $_b .= " </body>\n";
125  return $_b;
126  }
127 
128  private function getFooter()
129  {
130  $_f = "</html>\n";
131  return $_f;
132  }
133 
135  public function PrintHtml()
136  {
137  echo $this->ToHtml();
138  return true;
139  }
140 
142  public function ToHtml()
143  {
144  // we first precache whole content in buffer, because if there were some exceptions, we don't want to get only partial html text
145  $_header = $this->getHeader();
146  $_body = $this->getBody();
147  $_footer = $this->getFooter();
148  if ($this->UseTidy)
149  {
150  $tidy = new tidy;
151  $config = array( 'indent' => true, 'wrap' => 800 );
152  $tidy->parseString($_header . $_body . $_footer, $config, 'utf8');
153  return tidy_get_output($tidy);
154  } else
155  {
156  return ($_header . $_body . $_footer);
157  }
158  }
159 }
160 
$Body
Body of a page (not a full html source code, but user defined body), in most cases you never need to ...
Definition: htmlpage.php:37
$UseTidy
If enabled php module "tidy" will be used to format the output source code, it needs to be installed ...
Definition: htmlpage.php:39
PrintHtml()
Prints a html source code of a page into stdout.
Definition: htmlpage.php:135
Represent a single Html page.
Definition: htmlpage.php:32
$Title
Title of the page.
Definition: htmlpage.php:35
Represent a single Html container, usually used by htmlpage or htmltable or any other element that is...
Definition: container.php:26
ToHtml()
Return whole html page as a string.
Definition: htmlpage.php:142