Preferences::loadBasicConfig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Xoositemap;
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         Xoositemap
18
 * @since           2.6.0
19
 * @author          Laurent JEN (Aka DuGris)
20
21
 */
22
class Preferences
23
{
24
    public $config = [];
25
    public $basicConfig = [];
26
    public  $configPath;
27
    public  $configFile;
28
    private $moduleDirName = 'xoositemap';
29
30
    /**
31
     * Xoositemap\Preferences constructor.
32
     */
33
    public function __construct()
34
    {
35
        $xoops = \Xoops::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $xoops is dead and can be removed.
Loading history...
36
        $this->configFile = 'config.' . $this->moduleDirName . '.php';
37
38
        $this->configPath = \XoopsBaseConfig::get('var-path') . '/configs/' . $this->moduleDirName . '/';
39
40
        $this->basicConfig = $this->loadBasicConfig();
41
        $this->config      = @$this->loadConfig();
42
43
        if (count($this->config) != count($this->basicConfig)) {
44
            $this->config = array_merge($this->basicConfig, $this->config);
45
            $this->writeConfig($this->config);
46
        }
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public static function getInstance()
53
    {
54
        static $instance;
55
        if (!isset($instance)) {
56
            $class    = __CLASS__;
57
            $instance = new $class();
58
        }
59
60
        return $instance;
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function getConfig()
67
    {
68
        return $this->config;
69
    }
70
71
    /**
72
     * Xoositemap\Preferences::loadConfig()
73
     *
74
     * @return array
75
     */
76
    public function loadConfig()
77
    {
78
        if (!$config = $this->readConfig()) {
79
            $config = $this->loadBasicConfig();
80
            $this->writeConfig($config);
81
        }
82
83
        return $config;
84
    }
85
86
    /**
87
     * Xoositemap\Preferences::loadBasicConfig()
88
     *
89
     * @return array
90
     */
91
    public function loadBasicConfig()
92
    {
93
        if (file_exists($file_path = dirname(__DIR__) . '/include/' . $this->configFile)) {
94
            $config = include $file_path;
95
        }
96
97
        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...
98
    }
99
100
    /**
101
     * Xoositemap\Preferences::readConfig()
102
     *
103
     * @return array
104
     */
105
    public function readConfig()
106
    {
107
        $file_path = $this->configPath . $this->configFile;
108
        \XoopsLoad::load('XoopsFile');
109
        $file = \XoopsFile::getHandler('file', $file_path);
110
111
        return eval(@$file->read());
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
112
    }
113
114
    /**
115
     * Xoositemap\Preferences::writeConfig()
116
     *
117
     * @param  array $config
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
     * @return bool
138
     */
139
    private function createPath($pathname, $pathout = XOOPS_ROOT_PATH)
140
    {
141
        $xoops = \Xoops::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $xoops is dead and can be removed.
Loading history...
142
        $pathname = mb_substr($pathname, mb_strlen(\XoopsBaseConfig::get('root-path')));
143
        $pathname = str_replace(DIRECTORY_SEPARATOR, '/', $pathname);
144
145
        $dest  = $pathout;
146
        $paths = explode('/', $pathname);
147
148
        foreach ($paths as $path) {
149
            if (!empty($path)) {
150
                $dest = $dest . '/' . $path;
151
                if (!is_dir($dest)) {
152
                    if (!mkdir($dest, 0755) && !is_dir($dest)) {
153
                        return false;
154
                    }
155
                    $this->writeIndex(\XoopsBaseConfig::get('uploads-path'), 'index.html', $dest);
156
                }
157
            }
158
        }
159
160
        return true;
161
    }
162
163
    /**
164
     * @param $folder_in
165
     * @param $source_file
166
     * @param $folder_out
167
     * @return bool
168
     */
169
    private function writeIndex($folder_in, $source_file, $folder_out)
170
    {
171
        if (!is_dir($folder_out)) {
172
            if (!$this->createPath($folder_out)) {
173
                return false;
174
            }
175
        }
176
177
        // Simple copy for a file
178
        if (is_file($folder_in . '/' . $source_file)) {
179
            return copy($folder_in . '/' . $source_file, $folder_out . '/' . basename($source_file));
180
        }
181
182
        return false;
183
    }
184
185
    /**
186
     * @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...
187
     * @param bool|true $module
188
     * @return array
189
     */
190
    public function prepare2Save($data = null, $module = true)
191
    {
192
        if (!isset($data)) {
193
            $data = $_POST;
194
        }
195
196
        $config = [];
197
        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

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