|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2012-2013 Aalto University and University of Helsinki |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* see LICENSE.txt for more information |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Handles all the requests from the user and changes the view accordingly. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Controller |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The controller has to know the model to access the data stored there. |
|
15
|
|
|
* @param $model contains the Model object. |
|
16
|
|
|
*/ |
|
17
|
|
|
public $model; |
|
18
|
|
|
|
|
19
|
|
|
protected $negotiator; |
|
20
|
|
|
|
|
21
|
|
|
protected $languages; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Initializes the Model object. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($model) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->model = $model; |
|
29
|
|
|
$this->negotiator = new \Negotiation\FormatNegotiator(); |
|
30
|
|
|
|
|
31
|
|
|
// Specify the location of the translation tables |
|
32
|
|
|
bindtextdomain('skosmos', 'resource/translations'); |
|
33
|
|
|
bind_textdomain_codeset('skosmos', 'UTF-8'); |
|
34
|
|
|
|
|
35
|
|
|
// Choose domain for translations |
|
36
|
|
|
textdomain('skosmos'); |
|
37
|
|
|
|
|
38
|
|
|
// Build arrays of language information, with 'locale' and 'name' keys |
|
39
|
|
|
$this->languages = array(); |
|
40
|
|
|
foreach ($this->model->getConfig()->getLanguages() as $langcode => $locale) { |
|
41
|
|
|
$this->languages[$langcode] = array('locale' => $locale); |
|
42
|
|
|
$this->setLanguageProperties($langcode); |
|
43
|
|
|
$this->languages[$langcode]['name'] = gettext('in_this_language'); |
|
44
|
|
|
$this->languages[$langcode]['lemma'] = Punic\Language::getName($langcode, $langcode); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets the locale language properties from the parameter (used by gettext and some Model classes). |
|
50
|
|
|
* @param string $lang language parameter eg. 'fi' for Finnish. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setLanguageProperties($lang) |
|
53
|
|
|
{ |
|
54
|
|
|
if (array_key_exists($lang, $this->languages)) { |
|
55
|
|
|
$locale = $this->languages[$lang]['locale']; |
|
56
|
|
|
putenv("LC_ALL=$locale"); |
|
57
|
|
|
setlocale(LC_ALL, $locale); |
|
58
|
|
|
} else { |
|
59
|
|
|
trigger_error("Unsupported language '$lang', not setting locale", E_USER_WARNING); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Negotiate a MIME type according to the proposed format, the list of valid |
|
65
|
|
|
* formats, and an optional proposed format. |
|
66
|
|
|
* As a side effect, set the HTTP Vary header if a choice was made based on |
|
67
|
|
|
* the Accept header. |
|
68
|
|
|
* @param array $choices possible MIME types as strings |
|
69
|
|
|
* @param string $accept HTTP Accept header value |
|
70
|
|
|
* @param string $format proposed format |
|
71
|
|
|
* @return string selected format, or null if negotiation failed |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function negotiateFormat($choices, $accept, $format) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($format) { |
|
76
|
|
|
if (!in_array($format, $choices)) { |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
return $format; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// if there was no proposed format, negotiate a suitable format |
|
83
|
|
|
header('Vary: Accept'); // inform caches that a decision was made based on Accept header |
|
84
|
|
|
$best = $this->negotiator->getBest($accept, $choices); |
|
85
|
|
|
$format = ($best !== null) ? $best->getValue() : null; |
|
86
|
|
|
return $format; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function guessBaseHref() |
|
90
|
|
|
{ |
|
91
|
|
|
$script_name = filter_input(INPUT_SERVER, 'SCRIPT_NAME', FILTER_SANITIZE_STRING); |
|
92
|
|
|
$script_filename = filter_input(INPUT_SERVER, 'SCRIPT_FILENAME', FILTER_SANITIZE_STRING); |
|
93
|
|
|
$script_filename = realpath($script_filename); // resolve any symlinks (see #274) |
|
94
|
|
|
$script_filename = str_replace("\\", "/", $script_filename); // fixing windows paths with \ (see #309) |
|
95
|
|
|
$base_dir = __DIR__; // Absolute path to your installation, ex: /var/www/mywebsite |
|
96
|
|
|
$base_dir = str_replace("\\", "/", $base_dir); // fixing windows paths with \ (see #309) |
|
97
|
|
|
$doc_root = preg_replace("!{$script_name}$!", '', $script_filename); |
|
98
|
|
|
$base_url = preg_replace("!^{$doc_root}!", '', $base_dir); |
|
99
|
|
|
$base_url = str_replace('/controller', '/', $base_url); |
|
100
|
|
|
$protocol = filter_input(INPUT_SERVER, 'HTTPS', FILTER_SANITIZE_STRING) === null ? 'http' : 'https'; |
|
101
|
|
|
$port = filter_input(INPUT_SERVER, 'SERVER_PORT', FILTER_SANITIZE_STRING); |
|
102
|
|
|
$disp_port = ($protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443) ? '' : ":$port"; |
|
103
|
|
|
$domain = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_STRING); |
|
104
|
|
|
$full_url = "$protocol://{$domain}{$disp_port}{$base_url}"; |
|
105
|
|
|
return $full_url; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function getBaseHref() |
|
109
|
|
|
{ |
|
110
|
|
|
return ($this->model->getConfig()->getBaseHref() !== null) ? $this->model->getConfig()->getBaseHref() : $this->guessBaseHref(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.