Preferences::prepare2Save()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 8
nop 2
dl 0
loc 18
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Xooghost;
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         Xooghost
18
 * @since           2.6.0
19
 * @author          Laurent JEN (Aka DuGris)
20
 */
21
22
/**
23
 * Class Preferences
24
 */
25
class Preferences
26
{
27
    public $config = [];
28
    public $basicConfig = [];
29
    public $configPath;
30
    public $configFile;
31
    private $moduleDirName = 'xooghost';
32
33
    public function __construct()
34
    {
35
        $this->configFile = 'config.' . $this->moduleDirName . '.php';
36
37
        $this->configPath = \XoopsBaseConfig::get('var-path') . '/configs/' . $this->moduleDirName . '/';
38
39
        $this->basicConfig = $this->loadBasicConfig();
40
        $this->config = @$this->loadConfig();
41
42
        if (count($this->config) != count($this->basicConfig)) {
43
            $this->config = array_merge($this->basicConfig, $this->config);
44
            $this->writeConfig($this->config);
45
        }
46
    }
47
48
    /**
49
     * @return mixed
50
     */
51
    public static function getInstance()
52
    {
53
        static $instance;
54
        if (!isset($instance)) {
55
            $class = __CLASS__;
56
            $instance = new $class();
57
        }
58
59
        return $instance;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getConfig()
66
    {
67
        return $this->config;
68
    }
69
70
    /**
71
     * Preferences::loadConfig()
72
     *
73
     * @return array
74
     */
75
    public function loadConfig()
76
    {
77
        if (!$config = $this->readConfig()) {
78
            $config = $this->loadBasicConfig();
79
            $this->writeConfig($config);
80
        }
81
82
        return $config;
83
    }
84
85
    /**
86
     * Preferences::loadBasicConfig()
87
     *
88
     * @return array
89
     */
90
    public function loadBasicConfig()
91
    {
92
        if (file_exists($file_path = dirname(__DIR__) . '/include/' . $this->configFile)) {
93
            $config = include $file_path;
94
        }
95
96
        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...
97
    }
98
99
    /**
100
     * Preferences::readConfig()
101
     *
102
     * @return array
103
     */
104
    public function readConfig()
105
    {
106
        $file_path = $this->configPath . $this->configFile;
107
        \XoopsLoad::load('XoopsFile');
108
        $file = \XoopsFile::getHandler('file', $file_path);
109
110
        return eval(@$file->read());
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
111
    }
112
113
    /**
114
     * Preferences::writeConfig()
115
     *
116
     * @param  array $config
117
     *
118
     * @return bool|null
119
     * @internal param string $filename
120
     */
121
    public function writeConfig($config)
122
    {
123
        if ($this->createPath($this->configPath)) {
124
            $file_path = $this->configPath . $this->configFile;
125
            \XoopsLoad::load('XoopsFile');
126
            $file = \XoopsFile::getHandler('file', $file_path);
127
128
            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

128
            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...
129
        }
130
131
        return null;
132
    }
133
134
    /**
135
     * @param              $pathname
136
     * @param mixed|string $pathout
137
     *
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
     *
169
     * @return bool
170
     */
171
    private function writeIndex($folder_in, $source_file, $folder_out)
172
    {
173
        if (!is_dir($folder_out)) {
174
            if (!$this->createPath($folder_out)) {
175
                return false;
176
            }
177
        }
178
179
        // Simple copy for a file
180
        if (is_file($folder_in . '/' . $source_file)) {
181
            return copy($folder_in . '/' . $source_file, $folder_out . '/' . basename($source_file));
182
        }
183
184
        return false;
185
    }
186
187
    /**
188
     * @param null $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
189
     * @param bool $module
190
     *
191
     * @return array
192
     */
193
    public function prepare2Save($data = null, $module = true)
194
    {
195
        if (!isset($data)) {
196
            $data = $_POST;
197
        }
198
199
        $config = [];
200
        foreach (array_keys($data) as $k) {
0 ignored issues
show
Bug introduced by
$data of type null is incompatible with the type array expected by parameter $input of array_keys(). ( Ignorable by Annotation )

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

200
        foreach (array_keys(/** @scrutinizer ignore-type */ $data) as $k) {
Loading history...
201
            if (is_array($data[$k])) {
202
                $config[$k] = $this->prepare2Save($data[$k], false);
203
            } else {
204
                if (!$module || false !== mb_strpos($k, $this->moduleDirName . '_')) {
205
                    $config[$k] = $data[$k];
206
                }
207
            }
208
        }
209
210
        return $config;
211
    }
212
}
213