Passed
Push — master ( 10bee5...4004ec )
by Stefan
04:54
created

Skinjob   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 91
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
C findResourceUrl() 0 35 8
1
<?php
2
3
/*
4
 * ******************************************************************************
5
 * Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 
6
 * and GN4-2 consortia
7
 *
8
 * License: see the web/copyright.php file in the file structure
9
 * ******************************************************************************
10
 */
11
12
/**
13
 * This file contains a class for handling switching between skin frontends.
14
 *
15
 * @package Developer
16
 */
17
/**
18
 * 
19
 */
20
21
namespace web\lib\user;
22
23
use \Exception;
24
25
/**
26
 * This class handles user UI skin handling.
27
 * 
28
 * @author Stefan Winter <[email protected]>
29
 */
30
class Skinjob {
31
32
    /**
33
     * The skin that was selected
34
     * 
35
     * @var string
36
     */
37
    public $skin;
38
39
    /**
40
     * the custom displayable variant of the term 'federation'
41
     * 
42
     * @var string
43
     */
44
    public $nomenclature_fed;
45
46
    /**
47
     * the custom displayable variant of the term 'institution'
48
     * 
49
     * @var string
50
     */
51
    public $nomenclature_inst;
52
    
53
    /**
54
     * Initialise the skin.
55
     * 
56
     * @param string $selectedSkin the name of the skin to use
57
     */
58
    public function __construct($selectedSkin = NULL) {
59
        // input may have been garbage. Sanity-check and fall back to default skin if needed
60
        $actualSkin = CONFIG['APPEARANCE']['skins'][0];
61
        if (in_array($selectedSkin, CONFIG['APPEARANCE']['skins'])) {
62
            $correctIndex = array_search($selectedSkin, CONFIG['APPEARANCE']['skins']);
63
            $actualSkin = CONFIG['APPEARANCE']['skins'][$correctIndex];
64
        }
65
66
        $this->skin = $actualSkin;
67
        $_SESSION['skin'] = $actualSkin;
68
69
        $cat = new \core\CAT();
70
        
71
        $this->nomenclature_fed = $cat->nomenclature_fed;
72
        $this->nomenclature_inst = $cat->nomenclature_inst;
73
    }
74
75
    /**
76
     * constructs a URL to the main resources directories. Searches for the file
77
     * first in the current skin's resource dir, then falls back to the global
78
     * resources dir, or returns FALSE if the requested file could not be found
79
     * at either location.
80
     * 
81
     * @param string $resourcetype which type of resource do we need a URL for?
82
     * @param string $filename the name of the file being searched.
83
     * @return string|boolean the URL to the resource, or FALSE if this file does not exist
84
     * @throws Exception if something went wrong during the URL construction
85
     */
86
    public function findResourceUrl($resourcetype, $filename) {
87
        switch ($resourcetype) {
88
            case "CSS":
89
                $path = "/resources/css/";
90
                break;
91
            case "IMAGES":
92
                $path = "/resources/images/";
93
                break;
94
            case "BASE":
95
                $path = "/";
96
                break;
97
            case "EXTERNAL":
98
                $path = "/external/";
99
                break;
100
            default:
101
                throw new Exception("findResourceUrl: unknown type of resource requested");
102
        }
103
104
        // does the file exist in the current skin's directory? Has precedence
105
        if (file_exists(__DIR__ . "/../../skins/" . $this->skin . $path . $filename)) {
106
            $extrapath = "/skins/" . $this->skin;
107
        } elseif (file_exists(__DIR__ . "/../../" . $path . $filename)) {
108
            $extrapath = "";
109
        } else {
110
            return FALSE;
111
        }
112
113
        $validator = new \web\lib\common\InputValidation();
114
        $host = $validator->hostname($_SERVER['SERVER_NAME']); 
115
        if ($host === FALSE) {
116
            throw new Exception("We don't know our own hostname?!");
117
        }
118
        $url = "//" . $host; // omitting http or https means "on same protocol"
119
        
120
        return htmlspecialchars($url . CONFIG['PATHS']['cat_base_url'] . $extrapath . $path . $filename, ENT_QUOTES);
121
    }
122
123
}
124