Completed
Push — master ( 76bb96...54c2fa )
by Patrick
16s
created

Settings::getLDAPSetting()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 5
nop 3
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
1
<?php
2
/**
3
 * Settings class
4
 *
5
 * This file describes the Settings Singleton
6
 *
7
 * PHP version 5 and 7
8
 *
9
 * @author Patrick Boyd / [email protected]
10
 * @copyright Copyright (c) 2017, Austin Artistic Reconstruction
11
 * @license http://www.apache.org/licenses/ Apache 2.0 License
12
 */
13
14
/**
15
 * A Singleton class to abstract access to various settings
16
 *
17
 * This class is the primary method to access sensative information such as DB logins
18
 */
19
class Settings extends \Singleton
20
{
21
    /**
22
     * Load the settings file
23
     *
24
     * @see \Singleton::getInstance()
25
     */ 
26
    protected function __construct()
27
    {
28
        if(isset($GLOBALS['FLIPSIDE_SETTINGS_LOC']))
29
        {
30
            require $GLOBALS['FLIPSIDE_SETTINGS_LOC'].'/class.FlipsideSettings.php';
31
            return;
32
        }
33
        if(is_readable('/var/www/secure_settings/class.FlipsideSettings.php'))
34
        {
35
            require '/var/www/secure_settings/class.FlipsideSettings.php';
36
            return;
37
        }
38
    }
39
40
    /**
41
     * Instantiate all classes pointed to by the specified property name and return
42
     *
43
     * @param string $propName The property name in the settings file
44
     *
45
     * @return array An array of classes or empty array if not set
46
     */
47
    public function getClassesByPropName($propName)
48
    {
49
        $ret = array();
50
        if(isset(FlipsideSettings::$$propName))
51
        {
52
            $prop = FlipsideSettings::$$propName;
53
            $keys = array_keys($prop);
54
            $count = count($keys);
55
            for($i = 0; $i < $count; $i++)
56
            {
57
                $class = $keys[$i];
58
                array_push($ret, new $class($prop[$keys[$i]]));
59
            }
60
        }
61
        return $ret;
62
    }
63
64
    /**
65
     * Get the \Data\DataSet parameters by name
66
     *
67
     * @param string $dataSetName The name of the dataset
68
     *
69
     * @return array|false An array of properties or false if no such dataset
70
     */
71
    public function getDataSetData($dataSetName)
72
    {
73
        if(!isset(FlipsideSettings::$dataset) || !isset(FlipsideSettings::$dataset[$dataSetName]))
74
        {
75
            return false;
76
        }
77
        return FlipsideSettings::$dataset[$dataSetName];
78
    }
79
80
    /**
81
     * Get the property from the settings file
82
     *
83
     * @param string $propName The name of the property in the settings file
84
     * @param mixed $default The default value to return if not set
85
     *
86
     * @return mixed The value from the settings file or the value of $default it not set
87
     */
88
    public function getGlobalSetting($propName, $default = false)
89
    {
90
        if(isset(FlipsideSettings::$global) && isset(FlipsideSettings::$global[$propName]))
91
        {
92
            return FlipsideSettings::$global[$propName];
93
        }
94
        return $default;
95
    }
96
97
    /**
98
     * Get the site links for the left hand side of the header
99
     *
100
     * @return array The array of links or an empty array if not set
101
     */
102
    public function getSiteLinks()
103
    {
104
        if(isset(FlipsideSettings::$sites))
105
        {
106
            return FlipsideSettings::$sites;
107
        }
108
        return array();
109
    }
110
111
    /**
112
     * Get the ldap connection string specified in the settings file
113
     *
114
     * @param mixed $default The default value to return if not set
115
     *
116
     * @return mixed The LDAP connection string or $default if not set
117
     */
118
    private function getLDAPHost($default)
119
    {
120
        if(!isset(FlipsideSettings::$ldap) || !isset(FlipsideSettings::$ldap['host']))
121
        {
122
            return $default;
123
        }
124
        if(isset(\FlipsideSettings::$ldap['proto']))
125
        {
126
            return \FlipsideSettings::$ldap['proto'].'://'.\FlipsideSettings::$ldap['host'];
127
        }
128
        return \FlipsideSettings::$ldap['host'];
129
    }
130
131
    /**
132
     * Get the specified ldap setting from the settings file
133
     *
134
     * @param string $propName The property name to retrieve
135
     * @param boolean $ldapAuth Is the value authentication or other
136
     * @param mixed $default The default value to return if not set
137
     *
138
     * @return mixed The value from the settings file
139
     */
140
    public function getLDAPSetting($propName, $ldapAuth = false, $default = false)
141
    {
142
        switch($propName)
143
        {
144
            case 'host':
145
                return $this->getLDAPHost($default);
146
            default:
147
                if($ldapAuth === false)
148
                {
149
                    if(isset(FlipsideSettings::$ldap) && isset(FlipsideSettings::$ldap[$propName]))
150
                    {
151
                        return FlipsideSettings::$ldap[$propName];
152
                    }
153
                    return $default;
154
                }
155
                if(isset(FlipsideSettings::$ldap_auth) && isset(FlipsideSettings::$ldap_auth[$propName]))
156
                {
157
                    return FlipsideSettings::$ldap_auth[$propName];
158
                }
159
                return $default;
160
        }
161
    }
162
}
163