Passed
Push — master ( c8a39b...1ce464 )
by Goffy
03:45
created

Devtools::function_tabreplacer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
1
<?php
2
3
namespace XoopsModules\Modulebuilder;
4
5
/*
6
 Utility Class Definition
7
8
 You may not change or alter any portion of this comment or credits of
9
 supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit
11
 authors.
12
13
 This program is distributed in the hope that it will be useful, but
14
 WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
18
/**
19
 * Module:  modulebuilder
20
 *
21
 * @package      \module\modulebuilder\class
22
 * @license      http://www.fsf.org/copyleft/gpl.html GNU public license
23
 * @copyright    https://xoops.org 2001-2017 &copy; XOOPS Project
24
 * @author       ZySpec <[email protected]>
25
 * @author       Mamba <[email protected]>
26
 * @since        
27
 */
28
29
use XoopsModules\Modulebuilder;
30
31
/**
32
 * Class Devtools
33
 */
34
class Devtools
35
{
36
    /* function to add function qualifier to module */
37
    /**
38
     * @param $src_path
39
     * @param $dst_path
40
     * @param $moduleName
41
     */
42
    public static function function_qualifier($src_path, $dst_path, $moduleName) {
43
44
        $functions = [];
45
        $constants = [];
46
47
        //php functions
48
        $functions[] = [
49
            'array_diff',
50
            'array_filter',
51
            'array_key_exists',
52
            'array_keys',
53
            'array_search',
54
            'array_slice',
55
            'array_unshift',
56
            'array_values',
57
            'array_flip',
58
            'assert',
59
            'basename',
60
            'boolval',
61
            'call_user_func',
62
            'call_user_func_array',
63
            'chr',
64
            'class_exists',
65
            'closedir',
66
            'constant',
67
            'copy',
68
            'count',
69
            'curl_close',
70
            'curl_error',
71
            'curl_exec',
72
            'curl_file_create',
73
            'curl_getinfo',
74
            'curl_init',
75
            'curl_setopt',
76
            'define',
77
            'defined',
78
            'dirname',
79
            'doubleval',
80
            'explode',
81
            'extension_loaded',
82
            'file_exists',
83
            'finfo_open',
84
            'floatval',
85
            'floor',
86
            'formatTimestamp',
87
            'func_get_args',
88
            'func_num_args',
89
            'function_exists',
90
            'get_called_class',
91
            'get_class',
92
            'getimagesize',
93
            'gettype',
94
            'imagecopyresampled',
95
            'imagecreatefromgif',
96
            'imagecreatefromjpeg',
97
            'imagecreatefrompng',
98
            'imagecreatefromstring',
99
            'imagecreatetruecolor',
100
            'imagedestroy',
101
            'imagegif',
102
            'imagejpeg',
103
            'imagepng',
104
            'imagerotate',
105
            'imagesx',
106
            'imagesy',
107
            'implode',
108
            'in_array',
109
            'ini_get',
110
            'intval',
111
            'include',
112
            'is_array',
113
            'is_bool',
114
            'is_callable',
115
            'is_dir',
116
            'is_double',
117
            'is_float',
118
            'is_int',
119
            'is_integer',
120
            'is_link',
121
            'is_long',
122
            'is_null',
123
            'is_object',
124
            'is_real',
125
            'is_resource',
126
            'is_string',
127
            'json_decode',
128
            'json_encode',
129
            'mime_content_type',
130
            'mkdir',
131
            'opendir',
132
            'ord',
133
            'pathinfo',
134
            'preg_match',
135
            'preg_match_all',
136
            'preg_replace',
137
            'readdir',
138
            'readlink',
139
            'redirect_header',
140
            'rename',
141
            'require',
142
            'rmdir',
143
            'round',
144
            'scandir',
145
            'sprintf',
146
            'str_replace',
147
            'strip_tags',
148
            'strlen',
149
            'strpos',
150
            'strtotime',
151
            'strval',
152
            'substr',
153
            'symlink',
154
            'time',
155
            'trigger_error',
156
            'trim',
157
            'ucfirst',
158
            'unlink',
159
            'version_compare',
160
            'mb_strtoupper',
161
            'mb_strtolower',
162
            'mb_strpos',
163
            'mb_strlen',
164
            'mb_strrpos',
165
        ];
166
167
        // xoops functions
168
        $functions[] = [
169
            'xoops_getHandler',
170
            'xoops_load',
171
            'xoops_loadLanguage',
172
        ];
173
        // xoops const
174
        $constants[] = [
175
            'XOBJ_DTYPE_',
176
            'XOOPS_URL',
177
            'XOOPS_ROOT_PATH',
178
            'XOOPS_GROUP_',
179
        ];
180
181
        $moduleNameUpper = \mb_strtoupper($moduleName);
182
        // module language defines
183
        $constants[] = [
184
            '_AM_' . $moduleNameUpper .'_',
185
            '_MI_' . $moduleNameUpper .'_',
186
            '_MB_' . $moduleNameUpper .'_',
187
            '_MA_' . $moduleNameUpper .'_',
188
        ];
189
190
        // repair known errors
191
        $errors = [
192
            'substr_\count('   => 'substr_count(',
193
            'micro\time('   => 'microtime(',
194
            'strto\time('   => 'strtotime(',
195
            'mb_\strlen('   => 'mb_strlen(',
196
            'mb_\substr('   => 'mb_substr(',
197
            'x\copy'        => 'xcopy',
198
            'r\rmdir'       => 'rrmdir',
199
            'r\copy'        => 'rcopy',
200
            'r\trim'        => 'rtrim',
201
            'l\trim'        => 'ltrim',
202
            '\dirname()'    => 'dirname()',
203
            'assw\ord'      => 'assword',
204
            'mb_\strpos'    => 'mb_strpos',
205
            'image\copy('   => 'imagecopy(',
206
            '<{if \count('  => '<{if count(',
207
            'define(\_'     => 'define(_',
208
            "'\_AM_" . $moduleNameUpper .'_' => "'_AM_" . $moduleNameUpper .'_',
209
            "'\_MI_" . $moduleNameUpper .'_' => "'_MI_" . $moduleNameUpper .'_',
210
            "'\_MB_" . $moduleNameUpper .'_' => "'_MB_" . $moduleNameUpper .'_',
211
            "'\_MA_" . $moduleNameUpper .'_' => "'_MA_" . $moduleNameUpper .'_',
212
        ];
213
214
        $patterns = [];
215
        foreach ($functions as $function) {
216
            //reset existing in order to avoid double \\
217
            foreach ($function as $item) {
218
                $patterns['\\' . $item . '('] = $item . '(';
219
            }
220
            //apply now for all
221
            foreach ($function as $item) {
222
                $patterns[$item . '('] = '\\' . $item . '(';
223
            }
224
        }
225
        foreach ($constants as $constant) {
226
            //reset existing in order to avoid double \\
227
            foreach ($constant as $item) {
228
                $patterns['\\' . $item ] = $item;
229
            }
230
            //apply now for all
231
            foreach ($constant as $item) {
232
                $patterns[$item] = '\\' . $item;
233
            }
234
        }
235
236
        //add errors
237
        foreach ($errors as $key => $value) {
238
            $patterns[$key] = $value;
239
        }
240
241
        $patKeys   = \array_keys($patterns);
242
        $patValues = \array_values($patterns);
243
        Devtools::cloneFileFolder($src_path, $dst_path, $patKeys, $patValues);
244
245
    }
246
247
    /* function to add function qualifier to module */
248
    /**
249
     * @param $src_path
250
     * @param $dst_path
251
     * @param $moduleName
252
     */
253
    public static function function_tabreplacer($src_path, $dst_path, $moduleName) {
0 ignored issues
show
Unused Code introduced by
The parameter $moduleName is not used and could be removed. ( Ignorable by Annotation )

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

253
    public static function function_tabreplacer($src_path, $dst_path, /** @scrutinizer ignore-unused */ $moduleName) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
254
        $patKeys   = [];
255
        $patValues = [];
256
        Devtools::cloneFileFolder($src_path, $dst_path, $patKeys, $patValues, true);
257
    }
258
259
    // recursive cloning script
260
    /**
261
     * @param $src_path
262
     * @param $dst_path
263
     * @param array $patKeys
264
     * @param array $patValues
265
     * @param bool  $replaceTabs
266
     */
267
    public static function cloneFileFolder($src_path, $dst_path, $patKeys = [], $patValues =[], $replaceTabs = false)
268
    {
269
        // open the source directory
270
        $dir = \opendir($src_path);
271
        // Make the destination directory if not exist
272
        @\mkdir($dst_path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

272
        /** @scrutinizer ignore-unhandled */ @\mkdir($dst_path);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
273
        // Loop through the files in source directory
274
        while( $file = \readdir($dir) ) {
275
            if (( $file != '.' ) && ( $file != '..' )) {
276
                if ( \is_dir($src_path . '/' . $file) ) {
277
                    // Recursively calling custom copy function for sub directory
278
                    Devtools::cloneFileFolder($src_path . '/' . $file, $dst_path . '/' . $file, $patKeys, $patValues, $replaceTabs);
279
                } else {
280
                    Devtools::cloneFile($src_path . '/' . $file, $dst_path . '/' . $file, $patKeys, $patValues, $replaceTabs);
281
                }
282
            }
283
        }
284
        \closedir($dir);
285
    }
286
287
    /**
288
     * @param $src_file
289
     * @param $dst_file
290
     * @param array $patKeys
291
     * @param array $patValues
292
     * @param array $patValues
293
     * @param bool  $replaceTabs
294
     */
295
    private static function cloneFile($src_file, $dst_file, $patKeys = [], $patValues =[], $replaceTabs = false)
296
    {
297
        $replace_code = false;
298
        $changeExtensions = ['php'];
299
        if (\in_array(\mb_strtolower(\pathinfo($src_file, PATHINFO_EXTENSION)), $changeExtensions)) {
0 ignored issues
show
Bug introduced by
It seems like pathinfo($src_file, Xoop...der\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

299
        if (\in_array(\mb_strtolower(/** @scrutinizer ignore-type */ \pathinfo($src_file, PATHINFO_EXTENSION)), $changeExtensions)) {
Loading history...
300
            $replace_code = true;
301
        }
302
        if (\strpos( $dst_file, basename(__FILE__)) > 0) {
303
            //skip myself
304
            $replace_code = false;
305
        }
306
        if ($replace_code) {
307
            // file, read it and replace text
308
            $content = \file_get_contents($src_file);
309
            if ($replaceTabs) {
310
                $content = \preg_replace("/[\t]+/", "    ", $content);
311
            } else {
312
                $content = \str_replace($patKeys, $patValues, $content);
313
            }
314
            //check file name whether it contains replace code
315
            $path_parts = \pathinfo($dst_file);
316
            $path = $path_parts['dirname'];
317
            $file =  $path_parts['basename'];
318
            $dst_file = $path . '/' . \str_replace($patKeys, $patValues, $file);
319
            \file_put_contents($dst_file, $content);
320
        } else {
321
            \copy($src_file, $dst_file);
322
        }
323
    }
324
325
    /**
326
     * get form with all existing modules
327
     * @param bool $action
328
     * @return \XoopsThemeForm
329
     */
330
    public static function getFormModulesFq($action = false)
331
    {
332
        if (!$action) {
333
            $action = $_SERVER['REQUEST_URI'];
334
        }
335
        // Get Theme Form
336
        \xoops_load('XoopsFormLoader');
337
        $form = new \XoopsSimpleForm('', 'form', $action, 'post', true);
338
        $form->setExtra('enctype="multipart/form-data"');
339
        // Form Select Module
340
        $modulesSelect = new \XoopsFormSelect(\_AM_MODULEBUILDER_DEVTOOLS_FQ_MODULE, 'fq_module', '');
341
        $modulesArr   = \XoopsLists::getModulesList();
342
        $modulesSelect->addOption('', ' ');
343
        foreach ($modulesArr as $mod) {
344
            $modulesSelect->addOption($mod, $mod);
345
        }
346
        $form->addElement($modulesSelect, true);
347
        // To Save
348
        $form->addElement(new \XoopsFormHidden('op', 'fq'));
349
        $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false));
350
351
        return $form;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $form returns the type XoopsSimpleForm which is incompatible with the documented return type XoopsThemeForm.
Loading history...
352
    }
353
354
    /**
355
     * get form with all existing modules
356
     * @param bool $action
357
     * @return \XoopsThemeForm
358
     */
359
    public static function getFormModulesCl($action = false)
360
    {
361
        if (!$action) {
362
            $action = $_SERVER['REQUEST_URI'];
363
        }
364
        // Get Theme Form
365
        \xoops_load('XoopsFormLoader');
366
        $form = new \XoopsSimpleForm('', 'form', $action, 'post', true);
367
        $form->setExtra('enctype="multipart/form-data"');
368
        // Form Select Module
369
        $modulesSelect = new \XoopsFormSelect(\_AM_MODULEBUILDER_DEVTOOLS_CL_MODULE, 'cl_module', '');
370
        $modulesArr   = \XoopsLists::getModulesList();
371
        $modulesSelect->addOption('', ' ');
372
        foreach ($modulesArr as $mod) {
373
            $modulesSelect->addOption($mod, $mod);
374
        }
375
        $form->addElement($modulesSelect, true);
376
        // To Save
377
        $form->addElement(new \XoopsFormHidden('op', 'check_lang'));
378
        $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false));
379
380
        return $form;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $form returns the type XoopsSimpleForm which is incompatible with the documented return type XoopsThemeForm.
Loading history...
381
    }
382
383
    /**
384
     * get form with all existing modules
385
     * @param bool $action
386
     * @return \XoopsThemeForm
387
     */
388
    public static function getFormModulesTab($action = false)
389
    {
390
        if (!$action) {
391
            $action = $_SERVER['REQUEST_URI'];
392
        }
393
        // Get Theme Form
394
        \xoops_load('XoopsFormLoader');
395
        $form = new \XoopsSimpleForm('', 'form', $action, 'post', true);
396
        $form->setExtra('enctype="multipart/form-data"');
397
        // Form Select Module
398
        $modulesSelect = new \XoopsFormSelect(\_AM_MODULEBUILDER_DEVTOOLS_TAB_MODULE, 'tab_module', '');
399
        $modulesArr   = \XoopsLists::getModulesList();
400
        $modulesSelect->addOption('', ' ');
401
        foreach ($modulesArr as $mod) {
402
            $modulesSelect->addOption($mod, $mod);
403
        }
404
        $form->addElement($modulesSelect, true);
405
        // To Save
406
        $form->addElement(new \XoopsFormHidden('op', 'tab_replacer'));
407
        $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false));
408
409
        return $form;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $form returns the type XoopsSimpleForm which is incompatible with the documented return type XoopsThemeForm.
Loading history...
410
    }
411
}
412