Issues (3083)

xoops/xmf/src/Module/Helper/GenericHelper.php (1 issue)

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xmf\Module\Helper;
13
14
use Xmf\Language;
15
16
/**
17
 * GenericHelper implements a Xoops 2.6 Xoops\Module\Helper\HelperAbstract.
18
 * We use it pre 2.6 systems, so we can encapsulate many of the changes
19
 * needed to make modules more compatible with 2.6 in these methods.
20
 * The most common deprecated warnings can be avoided by using module
21
 * helper methods.
22
 *
23
 * @category  Xmf\Module\Helper\GenericHelper
24
 * @package   Xmf
25
 * @author    trabis <[email protected]>
26
 * @author    Richard Griffith <[email protected]>
27
 * @copyright 2016-2023 XOOPS Project (https://xoops.org)
28
 * @license   GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
29
 * @link      https://xoops.org
30
 */
31
abstract class GenericHelper extends AbstractHelper
32
{
33
    /**
34
     * @var \XoopsModule
35
     * @deprecated - use $module -- will be removed
36
     */
37
    protected $object;
38
39
    /**
40
     * @var array of XoopsObjectHandler|XoopsPersistableObjectHandler
41
     */
42
    protected $handlers;
43
44
    /**
45
     * @var array config items
46
     */
47
    protected $configs;
48
49
    /**
50
     * Initialize parent::__construct calls this after verifying module object.
51
     *
52
     * @return void
53
     */
54
    public function init()
55
    {
56
        $this->object = $this->module; // for BC only
0 ignored issues
show
Deprecated Code introduced by
The property Xmf\Module\Helper\GenericHelper::$object has been deprecated: - use $module -- will be removed ( Ignorable by Annotation )

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

56
        /** @scrutinizer ignore-deprecated */ $this->object = $this->module; // for BC only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
57
    }
58
59
    /**
60
     * get the module object
61
     *
62
     * @return \XoopsModule
63
     */
64
    public function getModule()
65
    {
66
        if ($this->module === null) {
67
            $this->initObject();
68
        }
69
        if (!is_object($this->module)) {
70
            $this->addLog("ERROR :: Module '{$this->dirname}' does not exist");
71
        }
72
73
        return $this->module;
74
    }
75
76
    /**
77
     * get a module config item
78
     *
79
     * @param string $name    name of config item, or blank for all items
80
     * @param mixed  $default default value to return if config $name is not set
81
     *
82
     * @return mixed string config item, array of config items,
83
     *                or null if config not found
84
     */
85
    public function getConfig($name = null, $default = null)
86
    {
87
        if ($this->configs === null) {
88
            $this->initConfig();
89
        }
90
        if (empty($name)) {
91
            $this->addLog("Getting all config");
92
93
            return $this->configs;
94
        }
95
96
        if (!isset($this->configs[$name])) {
97
            $this->addLog("ERROR :: Config '{$name}' does not exist");
98
            return $default;
99
        }
100
101
        $this->addLog("Getting config '{$name}' : " . $this->serializeForHelperLog($this->configs[$name]));
102
103
        return $this->configs[$name];
104
    }
105
106
    /**
107
     * Get an Object Handler
108
     *
109
     * @param string $name name of handler to load
110
     *
111
     * @return bool|XoopsObjectHandler|XoopsPersistableObjectHandler
112
     */
113
    public function getHandler($name)
114
    {
115
        $ret = false;
116
        $name = strtolower($name);
117
        if (!isset($this->handlers[$name])) {
118
            $this->initHandler($name);
119
        }
120
121
        if (!isset($this->handlers[$name])) {
122
            $this->addLog("ERROR :: Handler '{$name}' does not exist");
123
        } else {
124
            $this->addLog("Getting handler '{$name}'");
125
            $ret = $this->handlers[$name];
126
        }
127
128
        return $ret;
129
    }
130
131
    /**
132
     * get a module object
133
     *
134
     * @return void
135
     */
136
    protected function initObject()
137
    {
138
        global $xoopsModule;
139
        if (isset($xoopsModule) && is_object($xoopsModule)
140
            && $xoopsModule->getVar('dirname') === $this->dirname
141
        ) {
142
            $this->module = $xoopsModule;
143
        } else {
144
            /** @var \XoopsModuleHandler $module_handler */
145
            $module_handler = xoops_getHandler('module');
146
            $this->module = $module_handler->getByDirname($this->dirname);
147
        }
148
        $this->addLog('INIT MODULE OBJECT');
149
    }
150
151
    /**
152
     * get module configs
153
     *
154
     * @return void
155
     */
156
    protected function initConfig()
157
    {
158
        $this->addLog('INIT CONFIG');
159
        global $xoopsModule;
160
        if (isset($xoopsModule) && is_object($xoopsModule)
161
            && $xoopsModule->getVar('dirname') === $this->dirname
162
        ) {
163
            global $xoopsModuleConfig;
164
            $this->configs = $xoopsModuleConfig;
165
        } else {
166
            /** @var \XoopsConfigHandler $config_handler */
167
            $config_handler = xoops_getHandler('config');
168
            $this->configs = $config_handler->getConfigsByCat(0, $this->getModule()->getVar('mid'));
169
        }
170
    }
171
172
    /**
173
     * get a handler instance and store in $this->_handlers
174
     *
175
     * @param string $name name of handler to load
176
     *
177
     * @return void
178
     */
179
    protected function initHandler($name)
180
    {
181
        $this->addLog('INIT ' . $name . ' HANDLER');
182
183
        if (!isset($this->handlers[$name])) {
184
            $hnd_file = XOOPS_ROOT_PATH . "/modules/{$this->dirname}/class/{$name}.php";
185
            if (file_exists($hnd_file)) {
186
                include_once $hnd_file;
187
            }
188
            $class = ucfirst(strtolower($this->dirname))
189
                . ucfirst(strtolower($name)) . 'Handler';
190
            if (class_exists($class)) {
191
                $db = \XoopsDatabaseFactory::getDatabaseConnection();
192
                $this->handlers[$name] = new $class($db);
193
                $this->addLog("Loading class '{$class}'");
194
            } else {
195
                $this->addLog("ERROR :: Class '{$class}' could not be loaded");
196
            }
197
        }
198
    }
199
200
    /**
201
     * load a language file for this module
202
     *
203
     * @param string $name basename of language file (i.e. 'admin')
204
     *
205
     * @return bool
206
     */
207
    public function loadLanguage($name)
208
    {
209
        if ($ret = Language::load($name, $this->dirname)) {
210
            $this->addLog("Loading language '{$name}'");
211
        } else {
212
            $this->addLog("ERROR :: Language '{$name}' could not be loaded");
213
        }
214
215
        return $ret;
216
    }
217
218
    /**
219
     * Is this the currently active module?
220
     *
221
     * @return bool
222
     */
223
    public function isCurrentModule()
224
    {
225
        if ($GLOBALS['xoopsModule']->getVar('dirname') === $this->dirname) {
226
            return true;
227
        }
228
229
        return false;
230
    }
231
232
    /**
233
     * Does user have admin rights to this module?
234
     *
235
     * @return bool true is user has admin right, else false
236
     */
237
    public function isUserAdmin()
238
    {
239
        return (isset($GLOBALS['xoopsUser']) && $GLOBALS['xoopsUser'] instanceof \XoopsUser)
240
            ? $GLOBALS['xoopsUser']->isAdmin($this->getModule()->getVar('mid')) : false;
241
    }
242
243
    /**
244
     * Return absolute URL for a module relative URL
245
     *
246
     * @param string $url module relative URL
247
     *
248
     * @return string
249
     */
250
    public function url($url = '')
251
    {
252
        return XOOPS_URL . '/modules/' . $this->dirname . '/' . $url;
253
    }
254
255
    /**
256
     * Return absolute filesystem path for a module relative path
257
     *
258
     * @param string $path module relative file system path
259
     *
260
     * @return string
261
     */
262
    public function path($path = '')
263
    {
264
        return XOOPS_ROOT_PATH . '/modules/' . $this->dirname . '/' . $path;
265
    }
266
267
    /**
268
     * Redirect the user to a page within this module
269
     *
270
     * @param string $url     module relative url (i.e. index.php)
271
     * @param int    $time    time in seconds to show redirect message
272
     * @param string $message redirect message
273
     *
274
     * @return void
275
     */
276
    public function redirect($url, $time = 3, $message = '')
277
    {
278
        redirect_header($this->url($url), $time, $message);
279
    }
280
281
    /**
282
     * Return absolute URL for a module relative upload file
283
     *
284
     * Uploads may be stored in special directories for many reasons,
285
     * such as permissions, security, replication and directory balancing
286
     * Rather than build their own URL's, modules should use this method
287
     * which will know how to reference the upload now and in the future.
288
     *
289
     * @param string $url module relative upload URL
290
     *
291
     * @return string
292
     */
293
    public function uploadUrl($url = '')
294
    {
295
        return XOOPS_UPLOAD_URL . '/' . $this->dirname . '/' . $url;
296
    }
297
298
    /**
299
     * Return absolute filesystem path for a module relative upload file
300
     *
301
     * Uploads may be stored in special directories for many reasons,
302
     * such as permissions, security, replication and directory balancing
303
     * Rather than build their own URL's, modules should use this method
304
     * which will know how to reference the upload now and in the future.
305
     *
306
     * @param string $path module relative upload file path
307
     *
308
     * @return string
309
     */
310
    public function uploadPath($path = '')
311
    {
312
        return XOOPS_UPLOAD_PATH . '/' . $this->dirname . '/' . $path;
313
    }
314
}
315