cloneFileFolder()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
c 0
b 0
f 0
nc 7
nop 1
dl 0
loc 34
rs 8.4444
1
<?php declare(strict_types=1);
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
/**
13
 * Wgfilemanager module for xoops
14
 *
15
 * @copyright      module for xoops
16
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
18
 * @version        $Id: 1.0 images.php 1 Mon 2018-03-19 10:04:51Z XOOPS Project (www.xoops.org) $
19
 */
20
21
use Xmf\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
22
23
require __DIR__ . '/header.php';
24
// It recovered the value of argument op in URL$
25
$op = Request::getString('op', 'list');
26
27
$templateMain = 'wgfilemanager_admin_clone.tpl';
28
29
switch ($op) {
30
    case 'list':
31
    default:
32
        $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('clone.php'));
33
        require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
34
        $form  = new \XoopsThemeForm(\sprintf(\_AM_WGFILEMANAGER_CLONE_TITLE, $helper->getModule()->getVar('name', 'E')), 'clone', 'clone.php', 'post', true);
35
        $clone = new \XoopsFormText(\_AM_WGFILEMANAGER_CLONE_NAME, 'clone', 20, 20, '');
36
        $clone->setDescription(\_AM_WGFILEMANAGER_CLONE_NAME_DSC);
37
        $form->addElement($clone, true);
38
        $form->addElement(new \XoopsFormHidden('op', 'submit'));
39
        $form->addElement(new \XoopsFormButton('', '', \_SUBMIT, 'submit'));
40
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
41
42
        break;
43
    case 'submit':
44
        // Security Check
45
        if (!$GLOBALS['xoopsSecurity']->check()) {
46
            \redirect_header('clone.php', 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
47
        }
48
        $clone = Request::getString('clone', '', 'POST');
49
        //check if name is valid
50
        if (empty($clone) || \preg_match('/[^a-zA-Z0-9\_\-]/', $clone)) {
51
            \redirect_header('clone.php', 3, \sprintf(\_AM_WGFILEMANAGER_CLONE_INVALIDNAME, $clone));
52
        }
53
54
        // Check wether the cloned module exists or not
55
        $dirClone = $GLOBALS['xoops']->path('modules/' . $clone);
56
        if ($clone && \is_dir($dirClone)) {
57
            \redirect_header('clone.php', 3, \sprintf(\_AM_WGFILEMANAGER_CLONE_EXISTS, $clone));
58
        }
59
60
        $patterns = [
61
            \mb_strtolower(\WGFILEMANAGER_DIRNAME)           => \mb_strtolower($clone),
62
            \mb_strtoupper(\WGFILEMANAGER_DIRNAME)           => \mb_strtoupper($clone),
63
            \ucfirst(\mb_strtolower(\WGFILEMANAGER_DIRNAME)) => \ucfirst(\mb_strtolower($clone)),
64
        ];
65
66
        $patKeys   = \array_keys($patterns);
67
        $patValues = \array_values($patterns);
68
        cloneFileFolder(\WGFILEMANAGER_PATH);
69
        $logocreated = createLogo(\mb_strtolower($clone));
70
71
        //change module name in modinfo.php
72
        // file, read it
73
        $content = \file_get_contents($dirClone . '/language/english/modinfo.php');
74
        $content = \str_replace('Wgfilemanager', \mb_strtolower($clone), $content);
75
        file_put_contents($dirClone . '/language/english/modinfo.php', $content);
76
77
        $msg = '';
78
        if (\is_dir($GLOBALS['xoops']->path('modules/' . \mb_strtolower($clone)))) {
79
            $msg .= \sprintf(\_AM_WGFILEMANAGER_CLONE_CONGRAT, "<a href='" . \XOOPS_URL . "/modules/system/admin.php?fct=modulesadmin&op=installlist'>" . \ucfirst(\mb_strtolower($clone)) . '</a>') . "<br>\n";
80
            if (!$logocreated) {
81
                $msg .= \_AM_WGFILEMANAGER_CLONE_IMAGEFAIL;
82
            }
83
        } else {
84
            $msg .= \_AM_WGFILEMANAGER_CLONE_FAIL;
85
        }
86
        $GLOBALS['xoopsTpl']->assign('result', $msg);
87
        break;
88
}
89
require __DIR__ . '/footer.php';
90
91
// recursive cloning script
92
/**
93
 * @param $path
94
 */
95
function cloneFileFolder($path): void
96
{
97
    global $patKeys;
98
    global $patValues;
99
100
    //remove \XOOPS_ROOT_PATH and add after replace, otherwise there can be a bug if \XOOPS_ROOT_PATH contains same pattern
101
    $newPath = \XOOPS_ROOT_PATH . \str_replace($patKeys[0], $patValues[0], \mb_substr($path, \mb_strlen(\XOOPS_ROOT_PATH)));
102
103
    if (\is_dir($path)) {
104
        // create new dir
105
        if (!\mkdir($newPath) && !\is_dir($newPath)) {
106
            throw new \RuntimeException(\sprintf('Directory "%s" was not created', $newPath));
107
        }
108
109
        // check all files in dir, and process it
110
        $handle = \opendir($path);
111
        if ($handle) {
0 ignored issues
show
introduced by
$handle is of type resource, thus it always evaluated to false.
Loading history...
112
            while (false !== ($file = \readdir($handle))) {
113
                if (0 !== \mb_strpos($file, '.')) {
114
                    cloneFileFolder("$path/$file");
115
                }
116
            }
117
            \closedir($handle);
118
        }
119
    } else {
120
        $noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf'];
121
        if (\in_array(\mb_strtolower(\pathinfo($path, \PATHINFO_EXTENSION)), $noChangeExtensions)) {
0 ignored issues
show
Bug introduced by
It seems like pathinfo($path, 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

121
        if (\in_array(\mb_strtolower(/** @scrutinizer ignore-type */ \pathinfo($path, \PATHINFO_EXTENSION)), $noChangeExtensions)) {
Loading history...
122
            // image
123
            \copy($path, $newPath);
124
        } else {
125
            // file, read it
126
            $content = \file_get_contents($path);
127
            $content = \str_replace($patKeys, $patValues, $content);
128
            \file_put_contents($newPath, $content);
129
        }
130
    }
131
}
132
133
/**
134
 * @param $dirname
135
 *
136
 * @return bool
137
 */
138
function createLogo($dirname)
139
{
140
    if (!\extension_loaded('gd')) {
141
        return false;
142
    }
143
    $requiredFunctions = [
144
        'imagecreatefrompng',
145
        'imagecolorallocate',
146
        'imagefilledrectangle',
147
        'imagepng',
148
        'imagedestroy',
149
        'imagefttext',
150
        'imagealphablending',
151
        'imagesavealpha',
152
    ];
153
    foreach ($requiredFunctions as $func) {
154
        if (!\function_exists($func)) {
155
            return false;
156
        }
157
    }
158
    //            unset($func);
159
160
    if (!\file_exists($imageBase = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'))
161
        || !\file_exists($font = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/VeraBd.ttf'))) {
162
        return false;
163
    }
164
165
    $imageModule = \imagecreatefrompng($imageBase);
166
    // save existing alpha channel
167
    \imagealphablending($imageModule, false);
168
    \imagesavealpha($imageModule, true);
169
170
    //Erase old text
171
    $greyColor = \imagecolorallocate($imageModule, 237, 237, 237);
172
    \imagefilledrectangle($imageModule, 5, 35, 85, 46, $greyColor);
173
174
    // Write text
175
    $textColor     = \imagecolorallocate($imageModule, 0, 0, 0);
176
    $spaceToBorder = (int)((80 - \mb_strlen($dirname) * 6.5) / 2);
177
    \imagefttext($imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, \ucfirst($dirname), []);
178
179
    // Set transparency color
180
    //$white = imagecolorallocatealpha($imageModule, 255, 255, 255, 127);
181
    //imagefill($imageModule, 0, 0, $white);
182
    //imagecolortransparent($imageModule, $white);
183
184
    \imagepng($imageModule, $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'));
185
    \imagedestroy($imageModule);
186
187
    return true;
188
}
189