wfdownloads_createLogo()   B
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 30
nc 10
nop 1
dl 0
loc 49
rs 8.5066
c 0
b 0
f 0
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
 * Wfdownloads module
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @package         wfdownload
18
 * @since           3.23
19
 * @author          Xoops Development Team
20
 */
21
22
use XoopsModules\Wfdownloads\{
23
    Helper,
24
    Utility
25
};
26
use Xmf\Module\Admin;
27
28
/** @var Helper $helper */
29
/** @var Utility $utility */
30
31
$currentFile = basename(__FILE__);
32
require_once __DIR__ . '/admin_header.php';
33
34
if ('submit' === @$_POST['op']) {
35
    if (!$GLOBALS['xoopsSecurity']->check()) {
36
        redirect_header($currentFile, 3, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
37
    }
38
39
    $cloneDirname = $_POST['clonedirname'];
40
41
    // Check if name is valid
42
    if (empty($cloneDirname) || preg_match('/[^a-zA-Z0-9\_\-]/', $cloneDirname)) {
43
        redirect_header($currentFile, 3, sprintf(_AM_WFDOWNLOADS_CLONE_INVALIDNAME, $cloneDirname));
44
    }
45
    // Check wether the cloned module exists or not
46
    if ($cloneDirname && is_dir(XOOPS_ROOT_PATH . '/modules/' . $cloneDirname)) {
47
        redirect_header($currentFile, 3, sprintf(_AM_WFDOWNLOADS_CLONE_EXISTS, $cloneDirname));
48
    }
49
    // Check dirname length for template file name length issues (template file name cannot be longer than 50 chars)
50
    if (mb_strlen($cloneDirname) > 18) {
51
        redirect_header($currentFile, 3, sprintf(_AM_WFDOWNLOADS_CLONE_TOOLONG, $cloneDirname));
52
    }
53
54
    $patterns = [
55
        mb_strtolower(WFDOWNLOADS_DIRNAME)          => mb_strtolower($cloneDirname),
0 ignored issues
show
Bug introduced by
The constant WFDOWNLOADS_DIRNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
        mb_strtoupper(WFDOWNLOADS_DIRNAME)          => mb_strtoupper($cloneDirname),
57
        ucfirst(mb_strtolower(WFDOWNLOADS_DIRNAME)) => ucfirst(mb_strtolower($cloneDirname)),
58
    ];
59
60
    $patKeys   = array_keys($patterns);
61
    $patValues = array_values($patterns);
62
    wfdownloads_cloneFileDir(WFDOWNLOADS_ROOT_PATH);
0 ignored issues
show
Bug introduced by
The constant WFDOWNLOADS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
63
    $logocreated = wfdownloads_createLogo(mb_strtolower($cloneDirname));
64
65
    $message = '';
66
    if (is_dir(XOOPS_ROOT_PATH . '/modules/' . mb_strtolower($cloneDirname))) {
67
        $message .= sprintf(_AM_WFDOWNLOADS_CLONE_CONGRAT, "<a href='" . XOOPS_URL . "/modules/system/admin.php?fct=modulesadmin&op=installlist'>" . ucfirst(mb_strtolower($cloneDirname)) . '</a>') . "<br>\n";
68
        if (!$logocreated) {
69
            $message .= _AM_WFDOWNLOADS_CLONE_IMAGEFAIL;
70
        }
71
    } else {
72
        $message .= _AM_WFDOWNLOADS_CLONE_FAIL;
73
    }
74
75
    Utility::getCpHeader();
76
    $adminObject = Admin::getInstance();
77
    $adminObject->displayNavigation($currentFile);
78
    echo $message;
79
    require_once __DIR__ . '/admin_footer.php';
80
    exit();
81
}
82
Utility::getCpHeader();
83
$adminObject = Admin::getInstance();
84
$adminObject->displayNavigation($currentFile);
85
xoops_load('XoopsFormLoader');
86
$form              = new XoopsThemeForm(sprintf(_AM_WFDOWNLOADS_CLONE_TITLE, $helper->getModule()->getVar('name', 'E')), 'clone', $currentFile, 'post', true);
87
$cloneDirname_text = new XoopsFormText(_AM_WFDOWNLOADS_CLONE_NAME, 'clonedirname', 18, 18, '');
88
$cloneDirname_text->setDescription(_AM_WFDOWNLOADS_CLONE_NAME_DSC);
89
$form->addElement($cloneDirname_text, true);
90
$form->addElement(new XoopsFormHidden('op', 'submit'));
91
$form->addElement(new XoopsFormButton('', '', _SUBMIT, 'submit'));
92
$form->display();
93
require_once __DIR__ . '/admin_footer.php';
94
exit();
95
96
// recursive clonning script
97
/**
98
 * @param $path
99
 */
100
function wfdownloads_cloneFileDir($path)
101
{
102
    global $patKeys;
103
    global $patValues;
104
105
    $newPath = str_replace($patKeys[0], $patValues[0], $path);
106
107
    if (is_dir($path)) {
108
        // create new dir
109
        if (!mkdir($newPath) && !is_dir($newPath)) {
110
            throw new RuntimeException('The directory ' . $newPath . ' could not be created.');
111
        }
112
113
        // check all files in dir, and process it
114
        if (false !== ($handle = opendir($path))) {
115
            while (false !== ($file = readdir($handle))) {
116
                if ('.' !== $file && '..' !== $file && '.svn' !== $file) {
117
                    wfdownloads_cloneFileDir("{$path}/{$file}");
118
                }
119
            }
120
            closedir($handle);
121
        }
122
    } else {
123
        if (preg_match('/(.jpg|.gif|.png|.zip|.ttf)$/i', $path)) {
124
            // image
125
            copy($path, $newPath);
126
        } else {
127
            // file, read it
128
            $content = file_get_contents($path);
129
            $content = str_replace($patKeys, $patValues, $content);
130
            file_put_contents($newPath, $content);
131
        }
132
    }
133
}
134
135
/**
136
 * @param $dirname
137
 *
138
 * @return bool
139
 */
140
function wfdownloads_createLogo($dirname)
141
{
142
    $helper = Helper::getInstance();
143
    // Check extension/functions
144
    if (!extension_loaded('gd')) {
145
        return false;
146
    }
147
    $required_functions = [
148
        'imagecreatetruecolor',
149
        'imagecolorallocate',
150
        'imagefilledrectangle',
151
        'imagejpeg',
152
        'imagedestroy',
153
        'imageftbbox',
154
    ];
155
    foreach ($required_functions as $func) {
156
        if (!function_exists($func)) {
157
            return false;
158
        }
159
    }
160
161
    // Check original image/font
162
    if (!file_exists($imageBase = XOOPS_ROOT_PATH . "/modules/{$dirname}/assets/images/module_logo_blank.png")) {
163
        return false;
164
    }
165
    if (!file_exists($font = XOOPS_ROOT_PATH . "/modules/{$helper->getModule()->dirname()}/assets/images/VeraBd.ttf")) {
166
        return false;
167
    }
168
    // Create image
169
    $imageModule = imagecreatefrompng($imageBase);
170
    // Erase old text
171
    if ($imageModule) {
0 ignored issues
show
introduced by
$imageModule is of type false|resource, thus it always evaluated to false.
Loading history...
172
        $greyColor = imagecolorallocate($imageModule, 237, 237, 237);
173
174
        imagefilledrectangle($imageModule, 5, 35, 85, 46, $greyColor);
175
        // Write text
176
        $textColor       = imagecolorallocate($imageModule, 0, 0, 0);
177
        $space_to_border = (80 - mb_strlen($dirname) * 6.5) / 2;
178
        imagefttext($imageModule, 8.5, 0, $space_to_border, 45, $textColor, $font, ucfirst($dirname), []);
179
        // Set transparency color
180
        $whiteColor = imagecolorallocatealpha($imageModule, 255, 255, 255, 127);
181
        imagefill($imageModule, 0, 0, $whiteColor);
182
        imagecolortransparent($imageModule, $whiteColor);
183
        // Save new image
184
        imagepng($imageModule, XOOPS_ROOT_PATH . "/modules/{$dirname}/assets/images/module_logo.png");
185
        imagedestroy($imageModule);
186
    }
187
188
    return true;
189
}
190