Passed
Branch master (7e303a)
by Michael
02:15
created

PublisherClone   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 48
dl 0
loc 97
rs 10
c 1
b 1
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
B cloneFileFolder() 0 32 8
B createLogo() 0 50 6
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
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
14
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @package         Publisher
16
 * @since           1.0
17
 * @author          trabis <[email protected]>
18
 */
19
20
use Xmf\Request;
21
use XoopsModules\Publisher;
22
23
require_once __DIR__ . '/admin_header.php';
24
25
Publisher\Utility::cpHeader();
26
//publisher_adminMenu(-1, _AM_PUBLISHER_CLONE);
27
Publisher\Utility::openCollapsableBar('clone', 'cloneicon', _AM_PUBLISHER_CLONE, _AM_PUBLISHER_CLONE_DSC);
28
29
if ('submit' === Request::getString('op', '', 'POST')) {
30
    if (!$GLOBALS['xoopsSecurity']->check()) {
31
        redirect_header('clone.php', 3, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
32
        //        exit();
33
    }
34
35
    //    $clone = $_POST['clone'];
36
    $clone = Request::getString('clone', '', 'POST');
37
38
    //check if name is valid
39
    if (empty($clone) || preg_match('/[^a-zA-Z0-9\_\-]/', $clone)) {
40
        redirect_header('clone.php', 3, sprintf(_AM_PUBLISHER_CLONE_INVALIDNAME, $clone));
41
        //        exit();
42
    }
43
44
    // Check wether the cloned module exists or not
45
    if ($clone && is_dir($GLOBALS['xoops']->path('modules/' . $clone))) {
46
        redirect_header('clone.php', 3, sprintf(_AM_PUBLISHER_CLONE_EXISTS, $clone));
47
    }
48
49
    $patterns = [
50
        mb_strtolower(PUBLISHER_DIRNAME)          => mb_strtolower($clone),
1 ignored issue
show
Bug introduced by
The constant PUBLISHER_DIRNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
51
        mb_strtoupper(PUBLISHER_DIRNAME)          => mb_strtoupper($clone),
52
        ucfirst(mb_strtolower(PUBLISHER_DIRNAME)) => ucfirst(mb_strtolower($clone)),
53
    ];
54
55
    $patKeys   = array_keys($patterns);
56
    $patValues = array_values($patterns);
57
    PublisherClone::cloneFileFolder(PUBLISHER_ROOT_PATH);
1 ignored issue
show
Bug introduced by
The constant PUBLISHER_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
58
    $logocreated = PublisherClone::createLogo(mb_strtolower($clone));
59
60
    $msg = '';
61
    if (is_dir($GLOBALS['xoops']->path('modules/' . mb_strtolower($clone)))) {
62
        $msg .= sprintf(_AM_PUBLISHER_CLONE_CONGRAT, "<a href='" . XOOPS_URL . "/modules/system/admin.php?fct=modulesadmin'>" . ucfirst(mb_strtolower($clone)) . '</a>') . "<br>\n";
63
        if (!$logocreated) {
64
            $msg .= _AM_PUBLISHER_CLONE_IMAGEFAIL;
65
        }
66
    } else {
67
        $msg .= _AM_PUBLISHER_CLONE_FAIL;
68
    }
69
    echo $msg;
70
} else {
71
    require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
72
    $form  = new \XoopsThemeForm(sprintf(_AM_PUBLISHER_CLONE_TITLE, $helper->getModule()->getVar('name', 'E')), 'clone', 'clone.php', 'post', true);
73
    $clone = new \XoopsFormText(_AM_PUBLISHER_CLONE_NAME, 'clone', 20, 20, '');
74
    $clone->setDescription(_AM_PUBLISHER_CLONE_NAME_DSC);
75
    $form->addElement($clone, true);
76
    $form->addElement(new \XoopsFormHidden('op', 'submit'));
77
    $form->addElement(new \XoopsFormButton('', '', _SUBMIT, 'submit'));
78
    $form->display();
79
}
80
81
// End of collapsable bar
82
Publisher\Utility::closeCollapsableBar('clone', 'cloneicon');
83
84
require_once __DIR__ . '/admin_footer.php';
85
86
// work around for PHP < 5.0.x
87
/*
88
if (!function_exists('file_put_contents')) {
89
    function file_put_contents($filename, $data, $file_append = false)
90
    {
91
        if ($fp == fopen($filename, (!$file_append ? 'w+' : 'a+'))) {
92
            fwrite($fp, $data);
93
            fclose($fp);
94
        }
95
    }
96
}
97
*/
98
99
/**
100
 * Class PublisherClone
101
 */
102
class PublisherClone
103
{
104
    // recursive cloning script
105
    /**
106
     * @param $path
107
     */
108
    public static function cloneFileFolder($path)
109
    {
110
        global $patKeys;
111
        global $patValues;
112
113
        $newPath = str_replace($patKeys[0], $patValues[0], $path);
114
115
        if (is_dir($path)) {
116
            // create new dir
117
            if (!mkdir($newPath) && !is_dir($newPath)) {
118
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $newPath));
119
            }
120
121
            // check all files in dir, and process it
122
            if ($handle = opendir($path)) {
123
                while (false !== ($file = readdir($handle))) {
124
                    if (0 !== mb_strpos($file, '.')) {
125
                        self::cloneFileFolder("{$path}/{$file}");
126
                    }
127
                }
128
                closedir($handle);
129
            }
130
        } else {
131
            $noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf'];
132
            if (in_array(mb_strtolower(pathinfo($path, PATHINFO_EXTENSION)), $noChangeExtensions, true)) {
133
                // image
134
                copy($path, $newPath);
135
            } else {
136
                // file, read it
137
                $content = file_get_contents($path);
138
                $content = str_replace($patKeys, $patValues, $content);
139
                file_put_contents($newPath, $content);
140
            }
141
        }
142
    }
143
144
    /**
145
     * @param $dirname
146
     *
147
     * @return bool
148
     */
149
    public static function createLogo($dirname)
150
    {
151
        if (!extension_loaded('gd')) {
152
            return false;
153
        }
154
        $requiredFunctions = [
155
            'imagecreatefrompng',
156
            'imagecolorallocate',
157
            'imagefilledrectangle',
158
            'imagepng',
159
            'imagedestroy',
160
            'imagefttext',
161
            'imagealphablending',
162
            'imagesavealpha',
163
        ];
164
        foreach ($requiredFunctions as $func) {
165
            if (!function_exists($func)) {
166
                return false;
167
            }
168
        }
169
        //            unset($func);
170
171
        if (!file_exists($imageBase = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'))
172
            || !file_exists($font = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/VeraBd.ttf'))) {
173
            return false;
174
        }
175
176
        $imageModule = imagecreatefrompng($imageBase);
177
        // save existing alpha channel
178
        imagealphablending($imageModule, false);
179
        imagesavealpha($imageModule, true);
180
181
        //Erase old text
182
        $greyColor = imagecolorallocate($imageModule, 237, 237, 237);
183
        imagefilledrectangle($imageModule, 5, 35, 85, 46, $greyColor);
184
185
        // Write text
186
        $textColor     = imagecolorallocate($imageModule, 0, 0, 0);
187
        $spaceToBorder = (80 - mb_strlen($dirname) * 6.5) / 2;
188
        imagefttext($imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, ucfirst($dirname), []);
0 ignored issues
show
Bug introduced by
$spaceToBorder of type double is incompatible with the type integer expected by parameter $x of imagefttext(). ( Ignorable by Annotation )

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

188
        imagefttext($imageModule, 8.5, 0, /** @scrutinizer ignore-type */ $spaceToBorder, 45, $textColor, $font, ucfirst($dirname), []);
Loading history...
189
190
        // Set transparency color
191
        //$white = imagecolorallocatealpha($imageModule, 255, 255, 255, 127);
192
        //imagefill($imageModule, 0, 0, $white);
193
        //imagecolortransparent($imageModule, $white);
194
195
        imagepng($imageModule, $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'));
196
        imagedestroy($imageModule);
197
198
        return true;
199
    }
200
}
201