Passed
Push — master ( 8df30f...c5c65a )
by Goffy
03:58
created

createLogo()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 28
nc 6
nop 1
dl 0
loc 50
rs 8.8497
c 1
b 0
f 1
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
/**
13
 * Modulebuilder module for xoops
14
 *
15
 * @copyright      module for xoops
16
 * @license        GPL 2.0 or later
17
 * @package        modulebuilder
18
 * @since          1.0
19
 * @min_xoops      2.5.11
20
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
21
 * @version        $Id: 1.0 images.php 1 Mon 2018-03-19 10:04:51Z XOOPS Project (www.xoops.org) $
22
 */
23
24
use Xmf\Request;
25
26
require __DIR__ . '/header.php';
27
// It recovered the value of argument op in URL$
28
$op = Request::getString('op', 'list');
29
30
$templateMain = 'modulebuilder_admin_clone.tpl';
31
32
switch ($op) {
33
    case 'list':
34
    default:
35
        $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('clone.php'));
36
        require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
37
        $form  = new \XoopsThemeForm(\sprintf(\_AM_MODULEBUILDER_CLONE_TITLE, $helper->getModule()->getVar('name', 'E')), 'clone', 'clone.php', 'post', true);
0 ignored issues
show
Bug introduced by
The constant _AM_MODULEBUILDER_CLONE_TITLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
        $clone = new \XoopsFormText(\_AM_MODULEBUILDER_CLONE_NAME, 'clone', 20, 20, '');
0 ignored issues
show
Bug introduced by
The constant _AM_MODULEBUILDER_CLONE_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
        $clone->setDescription(\_AM_MODULEBUILDER_CLONE_NAME_DSC);
0 ignored issues
show
Bug introduced by
The constant _AM_MODULEBUILDER_CLONE_NAME_DSC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40
        $form->addElement($clone, true);
41
        $form->addElement(new \XoopsFormHidden('op', 'submit'));
42
        $form->addElement(new \XoopsFormButton('', '', \_SUBMIT, 'submit'));
43
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
44
45
        break;
46
    case 'submit':
47
        // Security Check
48
        if (!$GLOBALS['xoopsSecurity']->check()) {
49
            \redirect_header('clone.php', 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
50
        }
51
        $clone = Request::getString('clone', '', 'POST');
52
        //check if name is valid
53
        if (empty($clone) || \preg_match('/[^a-zA-Z0-9\_\-]/', $clone)) {
54
            \redirect_header('clone.php', 3, \sprintf(\_AM_MODULEBUILDER_CLONE_INVALIDNAME, $clone));
0 ignored issues
show
Bug introduced by
The constant _AM_MODULEBUILDER_CLONE_INVALIDNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
        }
56
57
        // Check wether the cloned module exists or not
58
        $dirClone = $GLOBALS['xoops']->path('modules/' . $clone);
59
        if ($clone && \is_dir($dirClone)) {
60
            \redirect_header('clone.php', 3, \sprintf(\_AM_MODULEBUILDER_CLONE_EXISTS, $clone));
0 ignored issues
show
Bug introduced by
The constant _AM_MODULEBUILDER_CLONE_EXISTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
61
        }
62
63
        $patterns = [
64
            \mb_strtolower(\MODULEBUILDER_DIRNAME)          => \mb_strtolower($clone),
0 ignored issues
show
Bug introduced by
The constant MODULEBUILDER_DIRNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
            \mb_strtoupper(\MODULEBUILDER_DIRNAME)          => \mb_strtoupper($clone),
66
            \ucfirst(\mb_strtolower(\MODULEBUILDER_DIRNAME)) => \ucfirst(\mb_strtolower($clone)),
67
        ];
68
69
        $patKeys   = \array_keys($patterns);
70
        $patValues = \array_values($patterns);
71
        cloneFileFolder(\MODULEBUILDER_PATH);
0 ignored issues
show
Bug introduced by
The constant MODULEBUILDER_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
72
        $logocreated = createLogo(\mb_strtolower($clone));
73
74
        //change module name in modinfo.php
75
        // file, read it
76
        $content = file_get_contents($dirClone . '/language/english/modinfo.php');
77
        $content = \str_replace('Modulebuilder', \mb_strtolower($clone), $content);
78
        file_put_contents($dirClone . '/language/english/modinfo.php', $content);
79
80
        $msg = '';
81
        if (\is_dir($GLOBALS['xoops']->path('modules/' . \mb_strtolower($clone)))) {
82
            $msg .= \sprintf(\_AM_MODULEBUILDER_CLONE_CONGRAT, "<a href='" . \XOOPS_URL . "/modules/system/admin.php?fct=modulesadmin&op=installlist'>" . \ucfirst(\mb_strtolower($clone)) . '</a>') . "<br>\n";
0 ignored issues
show
Bug introduced by
The constant _AM_MODULEBUILDER_CLONE_CONGRAT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
83
            if (!$logocreated) {
84
                $msg .= \_AM_MODULEBUILDER_CLONE_IMAGEFAIL;
0 ignored issues
show
Bug introduced by
The constant _AM_MODULEBUILDER_CLONE_IMAGEFAIL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
85
            }
86
        } else {
87
            $msg .= \_AM_MODULEBUILDER_CLONE_FAIL;
0 ignored issues
show
Bug introduced by
The constant _AM_MODULEBUILDER_CLONE_FAIL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
88
        }
89
        $GLOBALS['xoopsTpl']->assign('result', $msg);
90
        break;
91
}
92
require __DIR__ . '/footer.php';
93
94
// recursive cloning script
95
/**
96
 * @param $path
97
 */
98
function cloneFileFolder($path)
99
{
100
    global $patKeys;
101
    global $patValues;
102
103
    //remove \XOOPS_ROOT_PATH and add after replace, otherwise there can be a bug if \XOOPS_ROOT_PATH contains same pattern
104
    $newPath = \XOOPS_ROOT_PATH . \str_replace($patKeys[0], $patValues[0], \substr($path, \strlen(\XOOPS_ROOT_PATH)));
105
106
    if (\is_dir($path)) {
107
        // create new dir
108
        if (!\mkdir($newPath) && !\is_dir($newPath)) {
109
            throw new \RuntimeException(\sprintf('Directory "%s" was not created', $newPath));
110
        }
111
112
        // check all files in dir, and process it
113
        $handle = \opendir($path);
114
        if ($handle) {
0 ignored issues
show
introduced by
$handle is of type resource, thus it always evaluated to false.
Loading history...
115
            while (false !== ($file = \readdir($handle))) {
116
                if (0 !== mb_strpos($file, '.')) {
117
                    cloneFileFolder("{$path}/{$file}");
118
                }
119
            }
120
            \closedir($handle);
121
        }
122
    } else {
123
        $noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf'];
124
        if (\in_array(\mb_strtolower(\pathinfo($path, \PATHINFO_EXTENSION)), $noChangeExtensions, true)) {
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

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