Preferences::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace XoopsModules\Xoosocialnetwork;
4
5
/**
6
 * Xoopreferences : Preferences Manager
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @package         Xoosocialnetwork
18
 * @since           2.6.0
19
 * @author          Laurent JEN (Aka DuGris)
20
21
 */
22
23
/**
24
 * Class Preferences
25
 */
26
class Preferences
27
{
28
    public $config = [];
29
    public $basicConfig = [];
30
    public $configPath;
31
    public $configFile;
32
    private $moduleDirName = 'xoosocialnetwork';
33
34
    protected function __construct()
35
    {
36
        $xoops = \Xoops::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $xoops is dead and can be removed.
Loading history...
37
        $this->configFile = 'config.' . $this->moduleDirName . '.php';
38
39
        $this->configPath = \XoopsBaseConfig::get('var-path') . '/configs/' . $this->moduleDirName . '/';
40
41
        $this->basicConfig = $this->loadBasicConfig();
42
        $this->config = @$this->loadConfig();
43
44
        if (count($this->config) != count($this->basicConfig)) {
45
            $this->config = array_merge($this->basicConfig, $this->config);
46
            $this->writeConfig($this->config);
47
        }
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53
    public static function getInstance()
54
    {
55
        static $instance;
56
        if (null === $instance) {
57
            $class = __CLASS__;
58
            $instance = new $class();
59
        }
60
61
        return $instance;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getConfig()
68
    {
69
        return $this->config;
70
    }
71
72
    /**
73
     * Xoosocialnetwork\Preferences::loadConfig()
74
     *
75
     * @return array
76
     */
77
    public function loadConfig()
78
    {
79
        if (!$config = $this->readConfig()) {
80
            $config = $this->loadBasicConfig();
81
            $this->writeConfig($config);
82
        }
83
84
        return $config;
85
    }
86
87
    /**
88
     * Xoosocialnetwork\Preferences::loadBasicConfig()
89
     *
90
     * @return array
91
     */
92
    public function loadBasicConfig()
93
    {
94
        if (file_exists($file_path = dirname(__DIR__) . '/include/' . $this->configFile)) {
95
            $config = include $file_path;
96
        }
97
98
        return $config;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config does not seem to be defined for all execution paths leading up to this point.
Loading history...
99
    }
100
101
    /**
102
     * Xoosocialnetwork\Preferences::readConfig()
103
     *
104
     * @return array
105
     */
106
    public function readConfig()
107
    {
108
        $file_path = $this->configPath . $this->configFile;
109
        \XoopsLoad::load('XoopsFile');
110
        $file = \XoopsFile::getHandler('file', $file_path);
111
112
        return eval(@$file->read());
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
113
    }
114
115
    /**
116
     * Xoosocialnetwork\Preferences::writeConfig()
117
     *
118
     * @param  array $config
119
     * @return bool|null
120
     * @internal param string $filename
121
     */
122
    public function writeConfig($config)
123
    {
124
        if ($this->createPath($this->configPath)) {
125
            $file_path = $this->configPath . $this->configFile;
126
            \XoopsLoad::load('XoopsFile');
127
            $file = \XoopsFile::getHandler('file', $file_path);
128
129
            return $file->write('return ' . var_export($config, true) . ';');
0 ignored issues
show
Bug introduced by
The method write() does not exist on XoopsFolderHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
            return $file->/** @scrutinizer ignore-call */ write('return ' . var_export($config, true) . ';');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
        }
131
132
        return null;
133
    }
134
135
    /**
136
     * @param              $pathname
137
     * @param mixed|string $pathout
138
     * @return bool
139
     */
140
    private function createPath($pathname, $pathout = XOOPS_ROOT_PATH)
141
    {
142
        $xoops = \Xoops::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $xoops is dead and can be removed.
Loading history...
143
        $pathname = mb_substr($pathname, mb_strlen(\XoopsBaseConfig::get('root-path')));
144
        $pathname = str_replace(DIRECTORY_SEPARATOR, '/', $pathname);
145
146
        $dest = $pathout;
147
        $paths = explode('/', $pathname);
148
149
        foreach ($paths as $path) {
150
            if (!empty($path)) {
151
                $dest = $dest . '/' . $path;
152
                if (!is_dir($dest)) {
153
                    if (!mkdir($dest, 0755) && !is_dir($dest)) {
154
                        return false;
155
                    }
156
                    $this->writeIndex(\XoopsBaseConfig::get('uploads-path'), 'index.html', $dest);
157
                }
158
            }
159
        }
160
161
        return true;
162
    }
163
164
    /**
165
     * @param $folder_in
166
     * @param $source_file
167
     * @param $folder_out
168
     * @return bool
169
     */
170
    private function writeIndex($folder_in, $source_file, $folder_out)
171
    {
172
        if (!is_dir($folder_out)) {
173
            if (!$this->createPath($folder_out)) {
174
                return false;
175
            }
176
        }
177
178
        // Simple copy for a file
179
        if (is_file($folder_in . '/' . $source_file)) {
180
            return copy($folder_in . '/' . $source_file, $folder_out . '/' . basename($source_file));
181
        }
182
183
        return false;
184
    }
185
}
186