|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Handles all the requests from the user and changes the view accordingly. |
|
5
|
|
|
*/ |
|
6
|
|
|
class Controller |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* The controller has to know the model to access the data stored there. |
|
10
|
|
|
* @param $model contains the Model object. |
|
11
|
|
|
*/ |
|
12
|
|
|
public $model; |
|
13
|
|
|
|
|
14
|
|
|
protected $negotiator; |
|
15
|
|
|
|
|
16
|
|
|
protected $languages; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Initializes the Model object. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($model) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->model = $model; |
|
24
|
|
|
$this->negotiator = new \Negotiation\Negotiator(); |
|
25
|
|
|
|
|
26
|
|
|
// Specify the location of the translation tables |
|
27
|
|
|
bindtextdomain('skosmos', 'resource/translations'); |
|
28
|
|
|
bind_textdomain_codeset('skosmos', 'UTF-8'); |
|
29
|
|
|
|
|
30
|
|
|
// Choose domain for translations |
|
31
|
|
|
textdomain('skosmos'); |
|
32
|
|
|
|
|
33
|
|
|
// Build arrays of language information, with 'locale' and 'name' keys |
|
34
|
|
|
$this->languages = array(); |
|
35
|
|
|
foreach ($this->model->getConfig()->getLanguages() as $langcode => $locale) { |
|
36
|
|
|
$this->languages[$langcode] = array('locale' => $locale); |
|
37
|
|
|
$this->setLanguageProperties($langcode); |
|
38
|
|
|
$this->languages[$langcode]['name'] = gettext('in_this_language'); |
|
39
|
|
|
$this->languages[$langcode]['lemma'] = Punic\Language::getName($langcode, $langcode); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Sets the locale language properties from the parameter (used by gettext and some Model classes). |
|
45
|
|
|
* @param string $lang language parameter eg. 'fi' for Finnish. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setLanguageProperties($lang) |
|
48
|
|
|
{ |
|
49
|
|
|
if (array_key_exists($lang, $this->languages)) { |
|
50
|
|
|
$locale = $this->languages[$lang]['locale']; |
|
51
|
|
|
putenv("LANGUAGE=$locale"); |
|
52
|
|
|
putenv("LC_ALL=$locale"); |
|
53
|
|
|
setlocale(LC_ALL, $locale); |
|
54
|
|
|
} else { |
|
55
|
|
|
trigger_error("Unsupported language '$lang', not setting locale", E_USER_WARNING); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Negotiate a MIME type according to the proposed format, the list of valid |
|
61
|
|
|
* formats, and an optional proposed format. |
|
62
|
|
|
* As a side effect, set the HTTP Vary header if a choice was made based on |
|
63
|
|
|
* the Accept header. |
|
64
|
|
|
* @param array $choices possible MIME types as strings |
|
65
|
|
|
* @param string $accept HTTP Accept header value |
|
66
|
|
|
* @param string $format proposed format |
|
67
|
|
|
* @return string selected format, or null if negotiation failed |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function negotiateFormat($choices, $accept, $format) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($format) { |
|
72
|
|
|
if (!in_array($format, $choices)) { |
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
return $format; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// if there was no proposed format, negotiate a suitable format |
|
79
|
|
|
header('Vary: Accept'); // inform caches that a decision was made based on Accept header |
|
80
|
|
|
$best = $this->negotiator->getBest($accept, $choices); |
|
81
|
|
|
$format = ($best !== null) ? $best->getValue() : null; |
|
82
|
|
|
return $format; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function guessBaseHref() |
|
86
|
|
|
{ |
|
87
|
|
|
$script_name = filter_input(INPUT_SERVER, 'SCRIPT_NAME', FILTER_SANITIZE_STRING); |
|
88
|
|
|
$script_filename = filter_input(INPUT_SERVER, 'SCRIPT_FILENAME', FILTER_SANITIZE_STRING); |
|
89
|
|
|
$script_filename = realpath($script_filename); // resolve any symlinks (see #274) |
|
90
|
|
|
$script_filename = str_replace("\\", "/", $script_filename); // fixing windows paths with \ (see #309) |
|
91
|
|
|
$base_dir = __DIR__; // Absolute path to your installation, ex: /var/www/mywebsite |
|
92
|
|
|
$base_dir = str_replace("\\", "/", $base_dir); // fixing windows paths with \ (see #309) |
|
93
|
|
|
$doc_root = preg_replace("!{$script_name}$!", '', $script_filename); |
|
94
|
|
|
$base_url = preg_replace("!^{$doc_root}!", '', $base_dir); |
|
95
|
|
|
$base_url = str_replace('/controller', '/', $base_url); |
|
96
|
|
|
$protocol = filter_input(INPUT_SERVER, 'HTTPS', FILTER_SANITIZE_STRING) === null ? 'http' : 'https'; |
|
97
|
|
|
$port = filter_input(INPUT_SERVER, 'SERVER_PORT', FILTER_SANITIZE_STRING); |
|
98
|
|
|
$disp_port = ($port == 80 || $port == 443) ? '' : ":$port"; |
|
99
|
|
|
$domain = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_STRING); |
|
100
|
|
|
$full_url = "$protocol://{$domain}{$disp_port}{$base_url}"; |
|
101
|
|
|
return $full_url; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getBaseHref() |
|
105
|
|
|
{ |
|
106
|
|
|
return ($this->model->getConfig()->getBaseHref() !== null) ? $this->model->getConfig()->getBaseHref() : $this->guessBaseHref(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Echos an error message when the request can't be fulfilled. |
|
111
|
|
|
* @param string $code |
|
112
|
|
|
* @param string $status |
|
113
|
|
|
* @param string $message |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function returnError($code, $status, $message) |
|
116
|
|
|
{ |
|
117
|
|
|
header("HTTP/1.0 $code $status"); |
|
118
|
|
|
header("Content-type: text/plain; charset=utf-8"); |
|
119
|
|
|
echo "$code $status : $message"; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* If the $modifiedDate is a valid DateTime, and if the $_SERVER variable contains the right info, and |
|
124
|
|
|
* if the $modifiedDate is not more recent than the latest value in $_SERVER, then this function sets the |
|
125
|
|
|
* HTTP 304 not modified and returns true.. |
|
126
|
|
|
* |
|
127
|
|
|
* If the $modifiedDate is still valid, then it sets the Last-Modified header, to be used by the browser for |
|
128
|
|
|
* subsequent requests, and returns false. |
|
129
|
|
|
* |
|
130
|
|
|
* Otherwise, it returns false. |
|
131
|
|
|
* |
|
132
|
|
|
* @param DateTime $modifiedDate the last modified date to be compared against server's modified since information |
|
133
|
|
|
* @return bool whether it sent the HTTP 304 not modified headers or not (useful for sending the response without |
|
134
|
|
|
* further actions) |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function sendNotModifiedHeader($modifiedDate): bool |
|
137
|
|
|
{ |
|
138
|
|
|
if ($modifiedDate) { |
|
139
|
|
|
$ifModifiedSince = $this->getIfModifiedSince(); |
|
140
|
|
|
$this->sendHeader("Last-Modified: " . $modifiedDate->format('Y-m-d H:i:s')); |
|
141
|
|
|
if (!is_null($ifModifiedSince) && $ifModifiedSince >= $modifiedDate) { |
|
142
|
|
|
$this->sendHeader("HTTP/1.0 304 Not Modified"); |
|
143
|
|
|
return true; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return DateTime|null a DateTime object if the value exists in the $_SERVER variable, null otherwise |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function getIfModifiedSince() |
|
153
|
|
|
{ |
|
154
|
|
|
return isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) ? strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) : null; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Sends HTTP headers. Simply calls PHP built-in header function. But being |
|
159
|
|
|
* a function here, it can easily be tested/mocked. |
|
160
|
|
|
* |
|
161
|
|
|
* @param $header string header to be sent |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function sendHeader($header) |
|
164
|
|
|
{ |
|
165
|
|
|
header($header); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|