Passed
Push — master ( 11a5c5...26a351 )
by Goffy
04:01
created

Devtools::getFormModulesFq()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 22
rs 9.7998
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
37
    public static function function_qualifier($src_path, $dst_path, $moduleName) {
38
39
        $functions = [];
40
        $constants = [];
41
42
        //php functions
43
        $functions[] = [
44
            'array_diff',
45
            'array_filter',
46
            'array_key_exists',
47
            'array_keys',
48
            'array_search',
49
            'array_slice',
50
            'array_unshift',
51
            'array_values',
52
            'array_flip',
53
            'assert',
54
            'basename',
55
            'boolval',
56
            'call_user_func',
57
            'call_user_func_array',
58
            'chr',
59
            'class_exists',
60
            'closedir',
61
            'constant',
62
            'copy',
63
            'count',
64
            'curl_close',
65
            'curl_error',
66
            'curl_exec',
67
            'curl_file_create',
68
            'curl_getinfo',
69
            'curl_init',
70
            'curl_setopt',
71
            'define',
72
            'defined',
73
            'dirname',
74
            'doubleval',
75
            'explode',
76
            'extension_loaded',
77
            'file_exists',
78
            'finfo_open',
79
            'floatval',
80
            'floor',
81
            'formatTimestamp',
82
            'func_get_args',
83
            'func_num_args',
84
            'function_exists',
85
            'get_called_class',
86
            'get_class',
87
            'getimagesize',
88
            'gettype',
89
            'imagecopyresampled',
90
            'imagecreatefromgif',
91
            'imagecreatefromjpeg',
92
            'imagecreatefrompng',
93
            'imagecreatefromstring',
94
            'imagecreatetruecolor',
95
            'imagedestroy',
96
            'imagegif',
97
            'imagejpeg',
98
            'imagepng',
99
            'imagerotate',
100
            'imagesx',
101
            'imagesy',
102
            'implode',
103
            'in_array',
104
            'ini_get',
105
            'intval',
106
            'is_array',
107
            'is_bool',
108
            'is_callable',
109
            'is_dir',
110
            'is_double',
111
            'is_float',
112
            'is_int',
113
            'is_integer',
114
            'is_link',
115
            'is_long',
116
            'is_null',
117
            'is_object',
118
            'is_real',
119
            'is_resource',
120
            'is_string',
121
            'json_decode',
122
            'json_encode',
123
            'mime_content_type',
124
            'mkdir',
125
            'opendir',
126
            'ord',
127
            'pathinfo',
128
            'preg_match',
129
            'preg_match_all',
130
            'preg_replace',
131
            'readdir',
132
            'readlink',
133
            'redirect_header',
134
            'rename',
135
            'rmdir',
136
            'round',
137
            'scandir',
138
            'sprintf',
139
            'str_replace',
140
            'strip_tags',
141
            'strlen',
142
            'strpos',
143
            'strtotime',
144
            'strval',
145
            'substr',
146
            'symlink',
147
            'time',
148
            'trigger_error',
149
            'trim',
150
            'ucfirst',
151
            'unlink',
152
            'version_compare',
153
            'mb_strtoupper',
154
            'mb_strtolower',
155
            'mb_strpos',
156
            'mb_strlen',
157
            'mb_strrpos',
158
        ];
159
160
        // xoops functions
161
        $functions[] = [
162
            'xoops_getHandler',
163
            'xoops_load',
164
            'xoops_loadLanguage',
165
        ];
166
        // xoops const
167
        $constants[] = [
168
            'XOBJ_DTYPE_',
169
            'XOOPS_URL',
170
            'XOOPS_ROOT_PATH',
171
            'XOOPS_GROUP_',
172
        ];
173
174
        $moduleNameUpper = \mb_strtoupper($moduleName);
175
        // module language defines
176
        $constants[] = [
177
            '_AM_' . $moduleNameUpper .'_',
178
            '_MI_' . $moduleNameUpper .'_',
179
            '_MB_' . $moduleNameUpper .'_',
180
            '_MA_' . $moduleNameUpper .'_',
181
        ];
182
183
        // repair known errors
184
        $errors = [
185
            'strto\time('   => 'strtotime(',
186
            'mb_\strlen('   => 'mb_strlen(',
187
            'mb_\substr('   => 'mb_substr(',
188
            'x\copy'        => 'xcopy',
189
            'r\rmdir'       => 'rrmdir',
190
            'r\copy'        => 'rcopy',
191
            '\dirname()'    => 'dirname()',
192
            'assw\ord'      => 'assword',
193
            'mb_\strpos'    => 'mb_strpos',
194
            'image\copy('   => 'imagecopy(',
195
            '<{if \count('  => '<{if count(',
196
            'define(\_'     => 'define(_',
197
            "'\_AM_" . $moduleNameUpper .'_' => "'_AM_" . $moduleNameUpper .'_',
198
            "'\_MI_" . $moduleNameUpper .'_' => "'_MI_" . $moduleNameUpper .'_',
199
            "'\_MB_" . $moduleNameUpper .'_' => "'_MB_" . $moduleNameUpper .'_',
200
            "'\_MA_" . $moduleNameUpper .'_' => "'_MA_" . $moduleNameUpper .'_',
201
        ];
202
203
        $patterns = [];
204
        foreach ($functions as $function) {
205
            //reset existing in order to avoid double \\
206
            foreach ($function as $item) {
207
                $patterns['\\' . $item . '('] = $item . '(';
208
            }
209
            //apply now for all
210
            foreach ($function as $item) {
211
                $patterns[$item . '('] = '\\' . $item . '(';
212
            }
213
        }
214
        foreach ($constants as $constant) {
215
            //reset existing in order to avoid double \\
216
            foreach ($constant as $item) {
217
                $patterns['\\' . $item ] = $item;
218
            }
219
            //apply now for all
220
            foreach ($constant as $item) {
221
                $patterns[$item] = '\\' . $item;
222
            }
223
        }
224
225
        //add errors
226
        foreach ($errors as $key => $value) {
227
            $patterns[$key] = $value;
228
        }
229
230
        $patKeys   = \array_keys($patterns);
231
        $patValues = \array_values($patterns);
232
        Devtools::cloneFileFolder($src_path, $dst_path, $patKeys, $patValues);
233
234
    }
235
236
    // recursive cloning script
237
    /**
238
     * @param $src_path
239
     * @param $dst_path
240
     * @param array $patKeys
241
     * @param array $patValues
242
     */
243
    public static function cloneFileFolder($src_path, $dst_path, $patKeys = [], $patValues =[])
244
    {
245
        // open the source directory
246
        $dir = \opendir($src_path);
247
        // Make the destination directory if not exist
248
        @\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

248
        /** @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...
249
        // Loop through the files in source directory
250
        while( $file = \readdir($dir) ) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, 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

250
        while( $file = \readdir(/** @scrutinizer ignore-type */ $dir) ) {
Loading history...
251
            if (( $file != '.' ) && ( $file != '..' )) {
252
                if ( \is_dir($src_path . '/' . $file) ) {
253
                    // Recursively calling custom copy function for sub directory
254
                    Devtools::cloneFileFolder($src_path . '/' . $file, $dst_path . '/' . $file, $patKeys, $patValues);
255
                } else {
256
                    Devtools::cloneFile($src_path . '/' . $file, $dst_path . '/' . $file, $patKeys, $patValues);
257
                }
258
            }
259
        }
260
        \closedir($dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, 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

260
        \closedir(/** @scrutinizer ignore-type */ $dir);
Loading history...
261
    }
262
263
    /**
264
     * @param $src_file
265
     * @param $dst_file
266
     * @param array $patKeys
267
     * @param array $patValues
268
     */
269
    private static function cloneFile($src_file, $dst_file, $patKeys = [], $patValues =[])
270
    {
271
        $replace_code = false;
272
        $changeExtensions = ['php'];
273
        if (\in_array(\mb_strtolower(\pathinfo($src_file, PATHINFO_EXTENSION)), $changeExtensions)) {
274
            $replace_code = true;
275
        }
276
        if (\strpos( $dst_file, basename(__FILE__)) > 0) {
277
            //skip myself
278
            $replace_code = false;
279
        }
280
        if ($replace_code) {
281
            // file, read it and replace text
282
            $content = \file_get_contents($src_file);
283
            $content = \str_replace($patKeys, $patValues, $content);
284
            //check file name whether it contains replace code
285
            $path_parts = \pathinfo($dst_file);
286
            $path = $path_parts['dirname'];
287
            $file =  $path_parts['basename'];
288
            $dst_file = $path . '/' . \str_replace($patKeys, $patValues, $file);
289
            \file_put_contents($dst_file, $content);
290
        } else {
291
            \copy($src_file, $dst_file);
292
        }
293
    }
294
295
    /**
296
     * get form with all existing modules
297
     * @param bool $action
298
     * @return \XoopsThemeForm
299
     */
300
    public static function getFormModulesFq($action = false)
301
    {
302
        if (!$action) {
303
            $action = $_SERVER['REQUEST_URI'];
304
        }
305
        // Get Theme Form
306
        \xoops_load('XoopsFormLoader');
307
        $form = new \XoopsSimpleForm('', 'form', $action, 'post', true);
308
        $form->setExtra('enctype="multipart/form-data"');
309
        // Form Table projects
310
        $resPro_idSelect = new \XoopsFormSelect(\_AM_MODULEBUILDER_DEVTOOLS_FQ_MODULE, 'fq_module', '');
311
        $modulesArr   = \XoopsLists::getModulesList();
312
        $resPro_idSelect->addOption('', ' ');
313
        foreach ($modulesArr as $mod) {
314
            $resPro_idSelect->addOption($mod, $mod);
315
        }
316
        $form->addElement($resPro_idSelect, true);
317
        // To Save
318
        $form->addElement(new \XoopsFormHidden('op', 'fq'));
319
        $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false));
320
321
        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...
322
    }
323
    /**
324
     * get form with all existing modules
325
     * @param bool $action
326
     * @return \XoopsThemeForm
327
     */
328
    public static function getFormModulesCl($action = false)
329
    {
330
        if (!$action) {
331
            $action = $_SERVER['REQUEST_URI'];
332
        }
333
        // Get Theme Form
334
        \xoops_load('XoopsFormLoader');
335
        $form = new \XoopsSimpleForm('', 'form', $action, 'post', true);
336
        $form->setExtra('enctype="multipart/form-data"');
337
        // Form Table projects
338
        $resPro_idSelect = new \XoopsFormSelect(\_AM_MODULEBUILDER_DEVTOOLS_CL_MODULE, 'cl_module', '');
339
        $modulesArr   = \XoopsLists::getModulesList();
340
        $resPro_idSelect->addOption('', ' ');
341
        foreach ($modulesArr as $mod) {
342
            $resPro_idSelect->addOption($mod, $mod);
343
        }
344
        $form->addElement($resPro_idSelect, true);
345
        // To Save
346
        $form->addElement(new \XoopsFormHidden('op', 'check_lang'));
347
        $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false));
348
349
        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...
350
    }
351
}
352