Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php declare(strict_types=1);
// UTF-8 marker äöüÄÖÜ߀
/**
* Class Page for the exercises of the EWA lecture
* Demonstrates use of PHP including class and OO.
* Implements Zend coding standards.
* Generate documentation with Doxygen or phpdoc
*
* PHP Version 7.4
*
* @file Page.php
* @package Page Templates
* @author Bernhard Kreling, <bernhard.kreling@h-da.de>
* @author Ralf Hahn, <ralf.hahn@h-da.de>
* @version 3.1
*/
/**
* This abstract class is a common base class for all
* HTML-pages to be created.
* It manages access to the database and provides operations
* for outputting header and footer of a page.
* Specific pages have to inherit from that class.
* Each derived class can use these operations for accessing the database
* and for creating the generic parts of a HTML-page.
*
* @author Bernhard Kreling, <bernhard.kreling@h-da.de>
* @author Ralf Hahn, <ralf.hahn@h-da.de>
*/
abstract class Page
{
// --- ATTRIBUTES ---
/**
* Reference to the MySQLi-Database that can be used
* by all operations of the class or inherited classes.
*/
protected MySQLi $_database;
// --- OPERATIONS ---
/**
* Connects to DB and stores
* the connection in member $_database.
* Needs name of DB, user, password.
*/
protected function __construct()
{
error_reporting(E_ALL);
$host = "localhost";
/********************************************/
// This code switches from the the local installation (XAMPP) to the docker installation
if (gethostbyname('mariadb') != "mariadb") { // mariadb is known?
$host = "mariadb";
}
/********************************************/
$this->_database = new MySQLi($host, "public", "public", "pizzaservice");
if ($this->_database->connect_errno) {
throw new Exception("Connect failed: " . $this->_database->connect_errno);
}
// set charset to UTF8!!
if (!$this->_database->set_charset("utf8")) {
throw new Exception($this->_database->error);
}
}
public function __destruct() // Closes the DB connection
{
$this->_database->close();
}
/**
* Generates the header section of the page.
* i.e. starting from the content type up to the body-tag.
* Takes care that all strings passed from outside
* are converted to safe HTML by htmlspecialchars.
*
* @param string $title $title is the text to be used as title of the page
* @param string $jsFile path to a java script file to be included, default is "" i.e. no java script file
* @param bool $autoreload true: auto reload the page every 5 s, false: not auto reload
* @return void
*/
protected function generatePageHeader(string $title = "", string $jsFile = "", bool $autoreload = false):void
{
$title = htmlspecialchars($title);
header("Content-type: text/html; charset=UTF-8");
//--------------------------------
if ($autoreload) {
header("Refresh: 10;");
}
//-------------------------------------
echo <<< HTML
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
HTML;
if (is_file($jsFile)) {
echo "<script src='{$jsFile}'></script>\n";
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
}
}
/**
* Outputs the end of the HTML-file i.e. </body> etc.
* @return void
*/
protected function generatePageFooter():void
{
echo <<< HTML
</body>
</html>
HTML;
}
/**
* Processes the data that comes in via GET or POST.
* If every derived page is supposed to do something common
* with submitted data do it here.
* E.g. checking the settings of PHP that
* influence passing the parameters (e.g. magic_quotes).
* @return void
*/
protected function processReceivedData():void
{
}
} // end of class
// Zend standard does not like closing php-tag!
// PHP doesn't require the closing tag (it is assumed when the file ends).
// Not specifying the closing ? > helps to prevent accidents
// like additional whitespace which will cause session
// initialization to fail ("headers already sent").
//? >