-
Notifications
You must be signed in to change notification settings - Fork 95
/
WebController.php
624 lines (558 loc) · 24.9 KB
/
WebController.php
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
102
103
104
105
106
107
108
109
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php
/**
* Importing the dependencies.
*/
use \Punic\Language;
/**
* WebController is an extension of the Controller that handles all
* the requests originating from the view of the website.
*/
class WebController extends Controller
{
/**
* Provides access to the templating engine.
* @property object $twig the twig templating engine.
*/
public $twig;
public $honeypot;
/**
* Constructor for the WebController.
* @param Model $model
*/
public function __construct($model)
{
parent::__construct($model);
// initialize Twig templates
$tmpDir = $model->getConfig()->getTemplateCache();
// check if the cache pointed by config.ttl exists, if not we create it.
if (!file_exists($tmpDir)) {
mkdir($tmpDir);
}
// specify where to look for templates and cache
$loader = new Twig_Loader_Filesystem('view');
// initialize Twig environment
$this->twig = new Twig_Environment($loader, array('cache' => $tmpDir,'auto_reload' => true));
$this->twig->addExtension(new Twig_Extensions_Extension_I18n());
// used for setting the base href for the relative urls
$this->twig->addGlobal("BaseHref", $this->getBaseHref());
// pass the GlobalConfig object to templates so they can access configuration
$this->twig->addGlobal("GlobalConfig", $this->model->getConfig());
// setting the list of properties to be displayed in the search results
$this->twig->addGlobal("PreferredProperties", array('skos:prefLabel', 'skos:narrower', 'skos:broader', 'skosmos:memberOf', 'skos:altLabel', 'skos:related'));
// register a Twig filter for generating URLs for vocabulary resources (concepts and groups)
$this->twig->addFilter(new Twig_SimpleFilter('link_url', array($this, 'linkUrlFilter')));
// register a Twig filter for generating strings from language codes with CLDR
$langFilter = new Twig_SimpleFilter('lang_name', function ($langcode, $lang) {
return Language::getName($langcode, $lang);
});
$this->twig->addFilter($langFilter);
// create the honeypot
$this->honeypot = new \Honeypot();
if (!$this->model->getConfig()->getHoneypotEnabled()) {
$this->honeypot->disable();
}
$this->twig->addGlobal('honeypot', $this->honeypot);
}
/**
* Guess the language of the user. Return a language string that is one
* of the supported languages defined in the $LANGUAGES setting, e.g. "fi".
* @param Request $request HTTP request
* @param string $vocid identifier for the vocabulary eg. 'yso'.
* @return string returns the language choice as a numeric string value
*/
public function guessLanguage($request, $vocid = null)
{
// 1. select language based on SKOSMOS_LANGUAGE cookie
$languageCookie = $request->getCookie('SKOSMOS_LANGUAGE');
if ($languageCookie) {
return $languageCookie;
}
// 2. if vocabulary given, select based on the default language of the vocabulary
if ($vocid !== null && $vocid !== '') {
try {
$vocab = $this->model->getVocabulary($vocid);
return $vocab->getConfig()->getDefaultLanguage();
} catch (Exception $e) {
// vocabulary id not found, move on to the next selection method
}
}
// 3. select language based on Accept-Language header
header('Vary: Accept-Language'); // inform caches that a decision was made based on Accept header
$this->negotiator = new \Negotiation\LanguageNegotiator();
$langcodes = array_keys($this->languages);
// using a random language from the configured UI languages when there is no accept language header set
$acceptLanguage = $request->getServerConstant('HTTP_ACCEPT_LANGUAGE') ? $request->getServerConstant('HTTP_ACCEPT_LANGUAGE') : $langcodes[0];
$bestLang = $this->negotiator->getBest($acceptLanguage, $langcodes);
if (isset($bestLang) && in_array($bestLang->getValue(), $langcodes)) {
return $bestLang->getValue();
}
// show default site or prompt for language
return $langcodes[0];
}
/**
* Determines a css class that controls width and positioning of the vocabulary list element.
* The layout is wider if the left/right box templates have not been provided.
* @return string css class for the container eg. 'voclist-wide' or 'voclist-right'
*/
private function listStyle() {
$left = file_exists('view/left.inc');
$right = file_exists('view/right.inc');
$ret = 'voclist';
if (!$left && !$right) {
$ret .= '-wide';
} else if (!($left && $right) && ($right || $left)) {
$ret .= ($right) ? '-left' : '-right';
}
return $ret;
}
/**
* Loads and renders the view containing all the vocabularies.
* @param Request $request
*/
public function invokeVocabularies($request)
{
// set language parameters for gettext
$this->setLanguageProperties($request->getLang());
// load template
$template = $this->twig->loadTemplate('landing.twig');
// set template variables
$categoryLabel = $this->model->getClassificationLabel($request->getLang());
$sortedVocabs = $this->model->getVocabularyList(false, true);
$langList = $this->model->getLanguages($request->getLang());
$listStyle = $this->listStyle();
// render template
echo $template->render(
array(
'sorted_vocabs' => $sortedVocabs,
'category_label' => $categoryLabel,
'languages' => $this->languages,
'lang_list' => $langList,
'request' => $request,
'list_style' => $listStyle
));
}
/**
* Invokes the concept page of a single concept in a specific vocabulary.
*
* @param Request $request
*/
public function invokeVocabularyConcept(Request $request)
{
$lang = $request->getLang();
$this->setLanguageProperties($lang);
$vocab = $request->getVocab();
$langcodes = $vocab->getConfig()->getShowLangCodes();
$uri = $vocab->getConceptURI($request->getUri()); // make sure it's a full URI
$results = $vocab->getConceptInfo($uri, $request->getContentLang());
if (!$results) {
$this->invokeGenericErrorPage($request);
return;
}
if ($this->notModified($results[0])) {
return;
}
$customLabels = $vocab->getConfig()->getPropertyLabelOverrides();
$pluginParameters = json_encode($vocab->getConfig()->getPluginParameters());
$template = (in_array('skos:Concept', $results[0]->getType()) || in_array('skos:ConceptScheme', $results[0]->getType())) ? $this->twig->loadTemplate('concept-info.twig') : $this->twig->loadTemplate('group-contents.twig');
$crumbs = $vocab->getBreadCrumbs($request->getContentLang(), $uri);
echo $template->render(array(
'search_results' => $results,
'vocab' => $vocab,
'concept_uri' => $uri,
'languages' => $this->languages,
'explicit_langcodes' => $langcodes,
'bread_crumbs' => $crumbs['breadcrumbs'],
'combined' => $crumbs['combined'],
'request' => $request,
'plugin_params' => $pluginParameters,
'custom_labels' => $customLabels)
);
}
/**
* Invokes the feedback page with information of the users current vocabulary.
*/
public function invokeFeedbackForm($request)
{
$template = $this->twig->loadTemplate('feedback.twig');
$this->setLanguageProperties($request->getLang());
$vocabList = $this->model->getVocabularyList(false);
$vocab = $request->getVocab();
$feedbackSent = false;
if ($request->getQueryParamPOST('message')) {
$feedbackSent = true;
$feedbackMsg = $request->getQueryParamPOST('message');
$feedbackName = $request->getQueryParamPOST('name', 255);
$feedbackEmail = $request->getQueryParamPOST('email', 255);
$msgSubject = $request->getQueryParamPOST('msgsubject', 255);
$feedbackVocab = $request->getQueryParamPOST('vocab');
$feedbackVocabEmail = ($feedbackVocab !== null && $feedbackVocab !== '') ?
$this->model->getVocabulary($feedbackVocab)->getConfig()->getFeedbackRecipient() : null;
// if the hidden field has been set a value we have found a spam bot
// and we do not actually send the message.
if ($this->honeypot->validateHoneypot($request->getQueryParamPOST('item-description')) &&
$this->honeypot->validateHoneytime($request->getQueryParamPOST('user-captcha'), $this->model->getConfig()->getHoneypotTime())) {
$this->sendFeedback($request, $feedbackMsg, $msgSubject, $feedbackName, $feedbackEmail, $feedbackVocab, $feedbackVocabEmail);
}
}
echo $template->render(
array(
'languages' => $this->languages,
'vocab' => $vocab,
'vocabList' => $vocabList,
'feedback_sent' => $feedbackSent,
'request' => $request,
));
}
private function createFeedbackHeaders($fromName, $fromEmail, $toMail, $sender)
{
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
if (!empty($toMail)) {
$headers .= "Cc: " . $this->model->getConfig()->getFeedbackAddress() . "\r\n";
}
if (!empty($fromEmail)) {
$headers .= "Reply-To: $fromName <$fromEmail>\r\n";
}
$service = $this->model->getConfig()->getServiceName();
return $headers . "From: $fromName via $service <$sender>";
}
/**
* Sends the user entered message through the php's mailer.
* @param string $message content given by user.
* @param string $messageSubject subject line given by user.
* @param string $fromName senders own name.
* @param string $fromEmail senders email address.
* @param string $fromVocab which vocabulary is the feedback related to.
*/
public function sendFeedback($request, $message, $messageSubject, $fromName = null, $fromEmail = null, $fromVocab = null, $toMail = null)
{
$toAddress = ($toMail) ? $toMail : $this->model->getConfig()->getFeedbackAddress();
$messageSubject = "[" . $this->model->getConfig()->getServiceName() . "] " . $messageSubject;
if ($fromVocab !== null && $fromVocab !== '') {
$message = 'Feedback from vocab: ' . strtoupper($fromVocab) . "<br />" . $message;
}
$envelopeSender = $this->model->getConfig()->getFeedbackEnvelopeSender();
// determine the sender address of the message
$sender = $this->model->getConfig()->getFeedbackSender();
if (empty($sender)) $sender = $envelopeSender;
if (empty($sender)) $sender = $this->model->getConfig()->getFeedbackAddress();
// determine sender name - default to "anonymous user" if not given by user
if (empty($fromName)) $fromName = "anonymous user";
$headers = $this->createFeedbackHeaders($fromName, $fromEmail, $toMail, $sender);
$params = empty($envelopeSender) ? '' : "-f $envelopeSender";
// adding some information about the user for debugging purposes.
$message = $message . "<br /><br /> Debugging information:"
. "<br />Timestamp: " . date(DATE_RFC2822)
. "<br />User agent: " . $request->getServerConstant('HTTP_USER_AGENT')
. "<br />Referer: " . $request->getServerConstant('HTTP_REFERER');
try {
mail($toAddress, $messageSubject, $message, $headers, $params);
} catch (Exception $e) {
header("HTTP/1.0 404 Not Found");
$template = $this->twig->loadTemplate('error-page.twig');
if ($this->model->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
echo $template->render(
array(
'languages' => $this->languages,
));
return;
}
}
/**
* Invokes the about page for the Skosmos service.
*/
public function invokeAboutPage($request)
{
$template = $this->twig->loadTemplate('about.twig');
$this->setLanguageProperties($request->getLang());
$url = $request->getServerConstant('HTTP_HOST');
echo $template->render(
array(
'languages' => $this->languages,
'server_instance' => $url,
'request' => $request,
));
}
/**
* Invokes the search for concepts in all the available ontologies.
*/
public function invokeGlobalSearch($request)
{
$lang = $request->getLang();
$template = $this->twig->loadTemplate('vocab-search-listing.twig');
$this->setLanguageProperties($lang);
$parameters = new ConceptSearchParameters($request, $this->model->getConfig());
$vocabs = $request->getQueryParam('vocabs'); # optional
// convert to vocids array to support multi-vocabulary search
$vocids = ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null;
$vocabObjects = array();
if ($vocids) {
foreach($vocids as $vocid) {
try {
$vocabObjects[] = $this->model->getVocabulary($vocid);
} catch (ValueError $e) {
// fail fast with an error page if the vocabulary cannot be found
if ($this->model->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
header("HTTP/1.0 400 Bad Request");
$this->invokeGenericErrorPage($request, $e->getMessage());
return;
}
}
}
$parameters->setVocabularies($vocabObjects);
$counts = null;
$searchResults = null;
$errored = false;
try {
$countAndResults = $this->model->searchConceptsAndInfo($parameters);
$counts = $countAndResults['count'];
$searchResults = $countAndResults['results'];
} catch (Exception $e) {
$errored = true;
header("HTTP/1.0 500 Internal Server Error");
if ($this->model->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
}
$vocabList = $this->model->getVocabularyList();
$sortedVocabs = $this->model->getVocabularyList(false, true);
$langList = $this->model->getLanguages($lang);
echo $template->render(
array(
'search_count' => $counts,
'languages' => $this->languages,
'search_results' => $searchResults,
'rest' => $parameters->getOffset()>0,
'global_search' => true,
'search_failed' => $errored,
'term' => $request->getQueryParamRaw('q'),
'lang_list' => $langList,
'vocabs' => str_replace(' ', '+', $vocabs),
'vocab_list' => $vocabList,
'sorted_vocabs' => $sortedVocabs,
'request' => $request,
'parameters' => $parameters
));
}
/**
* Invokes the search for a single vocabs concepts.
*/
public function invokeVocabularySearch($request)
{
$template = $this->twig->loadTemplate('vocab-search-listing.twig');
$this->setLanguageProperties($request->getLang());
$vocab = $request->getVocab();
$searchResults = null;
try {
$vocabTypes = $this->model->getTypes($request->getVocabid(), $request->getLang());
} catch (Exception $e) {
header("HTTP/1.0 500 Internal Server Error");
if ($this->model->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
echo $template->render(
array(
'languages' => $this->languages,
'vocab' => $vocab,
'request' => $request,
'search_results' => $searchResults
));
return;
}
$langcodes = $vocab->getConfig()->getShowLangCodes();
$parameters = new ConceptSearchParameters($request, $this->model->getConfig());
try {
$countAndResults = $this->model->searchConceptsAndInfo($parameters);
$counts = $countAndResults['count'];
$searchResults = $countAndResults['results'];
} catch (Exception $e) {
header("HTTP/1.0 404 Not Found");
if ($this->model->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
echo $template->render(
array(
'languages' => $this->languages,
'vocab' => $vocab,
'term' => $request->getQueryParam('q'),
'search_results' => $searchResults
));
return;
}
echo $template->render(
array(
'languages' => $this->languages,
'vocab' => $vocab,
'search_results' => $searchResults,
'search_count' => $counts,
'rest' => $parameters->getOffset()>0,
'limit_parent' => $parameters->getParentLimit(),
'limit_type' => $request->getQueryParam('type') ? explode('+', $request->getQueryParam('type')) : null,
'limit_group' => $parameters->getGroupLimit(),
'limit_scheme' => $request->getQueryParam('scheme') ? explode('+', $request->getQueryParam('scheme')) : null,
'group_index' => $vocab->listConceptGroups($request->getContentLang()),
'parameters' => $parameters,
'term' => $request->getQueryParamRaw('q'),
'types' => $vocabTypes,
'explicit_langcodes' => $langcodes,
'request' => $request,
));
}
/**
* Invokes the alphabetical listing for a specific vocabulary.
*/
public function invokeAlphabeticalIndex($request)
{
$lang = $request->getLang();
$this->setLanguageProperties($lang);
$template = $this->twig->loadTemplate('alphabetical-index.twig');
$vocab = $request->getVocab();
$offset = ($request->getQueryParam('offset') && is_numeric($request->getQueryParam('offset')) && $request->getQueryParam('offset') >= 0) ? $request->getQueryParam('offset') : 0;
if ($request->getQueryParam('limit')) {
$count = $request->getQueryParam('limit');
} else {
$count = ($offset > 0) ? null : 250;
}
$contentLang = $request->getContentLang();
$allAtOnce = $vocab->getConfig()->getAlphabeticalFull();
if (!$allAtOnce) {
$letters = $vocab->getAlphabet($contentLang);
$letter = $request->getLetter();
if ($letter === '' && isset($letters[0])) {
$letter = $letters[0];
}
$searchResults = $vocab->searchConceptsAlphabetical($letter, $count, $offset, $contentLang);
} else {
$letters = null;
$searchResults = $vocab->searchConceptsAlphabetical('*', null, null, $contentLang);
}
$request->setContentLang($contentLang);
echo $template->render(
array(
'languages' => $this->languages,
'vocab' => $vocab,
'alpha_results' => $searchResults,
'letters' => $letters,
'all_letters' => $allAtOnce,
'request' => $request,
));
}
/**
* Invokes the vocabulary group index page template.
* @param boolean $stats set to true to get vocabulary statistics visible.
*/
public function invokeGroupIndex($request, $stats = false)
{
$lang = $request->getLang();
$this->setLanguageProperties($lang);
$template = $this->twig->loadTemplate('group-index.twig');
$vocab = $request->getVocab();
echo $template->render(
array(
'languages' => $this->languages,
'stats' => $stats,
'vocab' => $vocab,
'request' => $request,
));
}
/**
* Loads and renders the view containing a specific vocabulary.
*/
public function invokeVocabularyHome($request)
{
$lang = $request->getLang();
// set language parameters for gettext
$this->setLanguageProperties($lang);
$vocab = $request->getVocab();
$defaultView = $vocab->getConfig()->getDefaultSidebarView();
// load template
if ($defaultView === 'groups') {
$this->invokeGroupIndex($request, true);
return;
} else if ($defaultView === 'new') {
$this->invokeChangeList($request);
}
$pluginParameters = json_encode($vocab->getConfig()->getPluginParameters());
$template = $this->twig->loadTemplate('vocab.twig');
echo $template->render(
array(
'languages' => $this->languages,
'vocab' => $vocab,
'search_letter' => 'A',
'active_tab' => $defaultView,
'request' => $request,
'plugin_params' => $pluginParameters
));
}
/**
* Invokes a very generic errorpage.
*/
public function invokeGenericErrorPage($request, $message = null)
{
$this->setLanguageProperties($request->getLang());
header("HTTP/1.0 404 Not Found");
$template = $this->twig->loadTemplate('error-page.twig');
echo $template->render(
array(
'languages' => $this->languages,
'request' => $request,
'vocab' => $request->getVocab(),
'message' => $message,
'requested_page' => filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_FULL_SPECIAL_CHARS),
));
}
/**
* Loads and renders the view containing a list of recent changes in the vocabulary.
* @param Request $request
*/
public function invokeChangeList($request, $prop='dc:created')
{
$offset = ($request->getQueryParam('offset') && is_numeric($request->getQueryParam('offset')) && $request->getQueryParam('offset') >= 0) ? $request->getQueryParam('offset') : 0;
$limit = ($request->getQueryParam('limit') && is_numeric($request->getQueryParam('limit')) && $request->getQueryParam('limit') >= 0) ? $request->getQueryParam('limit') : 200;
$changeList = $this->getChangeList($request, $prop, $offset, $limit);
$bydate = $this->formatChangeList($changeList, $request->getLang());
// load template
$template = $this->twig->loadTemplate('changes.twig');
// render template
echo $template->render(
array(
'vocab' => $request->getVocab(),
'languages' => $this->languages,
'request' => $request,
'changeList' => $bydate)
);
}
/**
* Gets the list of newest concepts for a vocabulary according to timestamp indicated by a property
* @param Request $request
* @param string $prop the name of the property eg. 'dc:modified'.
* @param int $offset starting index offset
* @param int $limit maximum number of concepts to return
* @return Array list of concepts
*/
public function getChangeList($request, $prop, $offset=0, $limit=200)
{
// set language parameters for gettext
$this->setLanguageProperties($request->getLang());
return $request->getVocab()->getChangeList($prop, $request->getContentLang(), $offset, $limit);
}
/**
* Formats the list of concepts as labels arranged by modification month
* @param Array $changeList
* @param string $lang the language for displaying dates in the change list
* @return array list of concepts as labels by month
*/
public function formatChangeList($changeList, $lang)
{
$formatByDate = array();
foreach($changeList as $concept) {
$concept['datestring'] = Punic\Calendar::formatDate($concept['date'], 'medium', $lang);
$formatByDate[Punic\Calendar::getMonthName($concept['date'], 'wide', $lang, true) . Punic\Calendar::format($concept['date'], ' y', $lang) ][strtolower($concept['prefLabel'])] = $concept;
}
return $formatByDate;
}
}