Completed
Push — master ( d60455...0d0ae3 )
by Michael
04:10
created

PublisherClone::createLogo()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 48
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 1 Features 0
Metric Value
cc 6
eloc 25
c 11
b 1
f 0
nc 6
nop 1
dl 0
loc 48
rs 8.551
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 109 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
include_once __DIR__ . '/admin_header.php';
21
22
publisherCpHeader();
23
//publisher_adminMenu(-1, _AM_PUBLISHER_CLONE);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
publisherOpenCollapsableBar('clone', 'cloneicon', _AM_PUBLISHER_CLONE, _AM_PUBLISHER_CLONE_DSC);
25
26
if ('submit' === XoopsRequest::getString('op', '', 'POST')) {
27
    if (!$GLOBALS['xoopsSecurity']->check()) {
28
        redirect_header('clone.php', 3, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
29
        //        exit();
30
    }
31
32
    //    $clone = $_POST['clone'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
    $clone = XoopsRequest::getString('clone', '', 'POST');
34
35
    //check if name is valid
36
    if (empty($clone) || preg_match('/[^a-zA-Z0-9\_\-]/', $clone)) {
37
        redirect_header('clone.php', 3, sprintf(_AM_PUBLISHER_CLONE_INVALIDNAME, $clone));
38
        //        exit();
39
    }
40
41
    // Check wether the cloned module exists or not
42
    if ($clone && is_dir($GLOBALS['xoops']->path('modules/' . $clone))) {
43
        redirect_header('clone.php', 3, sprintf(_AM_PUBLISHER_CLONE_EXISTS, $clone));
44
    }
45
46
    $patterns = array(
47
        strtolower(PUBLISHER_DIRNAME)          => strtolower($clone),
48
        strtoupper(PUBLISHER_DIRNAME)          => strtoupper($clone),
49
        ucfirst(strtolower(PUBLISHER_DIRNAME)) => ucfirst(strtolower($clone))
50
    );
51
52
    $patKeys   = array_keys($patterns);
53
    $patValues = array_values($patterns);
54
    PublisherClone::cloneFileFolder(PUBLISHER_ROOT_PATH);
55
    $logocreated = PublisherClone::createLogo(strtolower($clone));
56
57
    $msg = '';
58
    if (is_dir($GLOBALS['xoops']->path('modules/' . strtolower($clone)))) {
59
        $msg .= sprintf(
60
            _AM_PUBLISHER_CLONE_CONGRAT,
61
            "<a href='" . XOOPS_URL . "/modules/system/admin.php?fct=modulesadmin'>"
62
            . ucfirst(strtolower($clone)) . '</a>'
63
        ) . "<br>\n";
64
        if (!$logocreated) {
65
            $msg .= _AM_PUBLISHER_CLONE_IMAGEFAIL;
66
        }
67
    } else {
68
        $msg .= _AM_PUBLISHER_CLONE_FAIL;
69
    }
70
    echo $msg;
71
} else {
72
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
73
    $form  = new XoopsThemeForm(
74
        sprintf(_AM_PUBLISHER_CLONE_TITLE, $publisher->getModule()->getVar('name', 'E')),
75
        'clone',
76
        'clone.php',
77
        'post',
78
        true
79
    );
80
    $clone = new XoopsFormText(_AM_PUBLISHER_CLONE_NAME, 'clone', 20, 20, '');
81
    $clone->setDescription(_AM_PUBLISHER_CLONE_NAME_DSC);
82
    $form->addElement($clone, true);
83
    $form->addElement(new XoopsFormHidden('op', 'submit'));
84
    $form->addElement(new XoopsFormButton('', '', _SUBMIT, 'submit'));
85
    $form->display();
86
}
87
88
// End of collapsable bar
89
publisherCloseCollapsableBar('clone', 'cloneicon');
90
91
include_once __DIR__ . '/admin_footer.php';
92
93
// work around for PHP < 5.0.x
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
/*
95
if (!function_exists('file_put_contents')) {
96
    function file_put_contents($filename, $data, $file_append = false)
97
    {
98
        if ($fp == fopen($filename, (!$file_append ? 'w+' : 'a+'))) {
99
            fwrite($fp, $data);
100
            fclose($fp);
101
        }
102
    }
103
}
104
*/
105
106
/**
107
 * Class PublisherClone
108
 */
109
class PublisherClone
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
110
{
111
    // recursive cloning script
112
    /**
113
     * @param $path
114
     */
115
    public static function cloneFileFolder($path)
116
    {
117
        global $patKeys;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
118
        global $patValues;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
119
120
        $newPath = str_replace($patKeys[0], $patValues[0], $path);
121
122
        if (is_dir($path)) {
123
            // create new dir
124
            mkdir($newPath);
125
126
            // check all files in dir, and process it
127
            if ($handle = opendir($path)) {
128
                while (($file = readdir($handle)) !== false) {
129
                    if (substr($file, 0, 1) !== '.') {
130
                        self::cloneFileFolder("{$path}/{$file}");
131
                    }
132
                }
133
                closedir($handle);
134
            }
135
        } else {
136
            $noChangeExtensions = array('jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf');
137
            if (in_array(strtolower(pathinfo($path, PATHINFO_EXTENSION)), $noChangeExtensions)) {
138
                // image
139
                copy($path, $newPath);
140
            } else {
141
                // file, read it
142
                $content = file_get_contents($path);
143
                $content = str_replace($patKeys, $patValues, $content);
144
                file_put_contents($newPath, $content);
145
            }
146
        }
147
    }
148
149
    /**
150
     * @param $dirname
151
     *
152
     * @return bool
153
     */
154
    public static function createLogo($dirname)
0 ignored issues
show
Coding Style introduced by
createLogo uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
155
    {
156
        if (!extension_loaded('gd')) {
157
            return false;
158
        } else {
159
            $requiredFunctions = array(
160
                'imagecreatefrompng', 'imagecolorallocate', 'imagefilledrectangle',
161
                'imagepng', 'imagedestroy', 'imagefttext',
162
                'imagealphablending', 'imagesavealpha',
163
            );
164
            foreach ($requiredFunctions as $func) {
165
                if (!function_exists($func)) {
166
                    return false;
167
                }
168
            }
169
            //            unset($func);
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
170
        }
171
172
        if (!file_exists($imageBase = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logo_module.png')) ||
173
            !file_exists($font = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/VeraBd.ttf'))
174
        ) {
175
            return false;
176
        }
177
178
        $imageModule = imagecreatefrompng($imageBase);
179
        // save existing alpha channel
180
        imagealphablending($imageModule, false);
181
        imagesavealpha($imageModule, true);
182
183
        //Erase old text
184
        $greyColor = imagecolorallocate($imageModule, 237, 237, 237);
185
        imagefilledrectangle($imageModule, 5, 35, 85, 46, $greyColor);
186
187
        // Write text
188
        $textColor     = imagecolorallocate($imageModule, 0, 0, 0);
189
        $spaceToBorder = (80 - strlen($dirname) * 6.5) / 2;
190
        imagefttext($imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, ucfirst($dirname), array());
191
192
        // Set transparency color
193
        //$white = imagecolorallocatealpha($imageModule, 255, 255, 255, 127);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
194
        //imagefill($imageModule, 0, 0, $white);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
195
        //imagecolortransparent($imageModule, $white);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
196
197
        imagepng($imageModule, $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logo_module.png'));
198
        imagedestroy($imageModule);
199
200
        return true;
201
    }
202
}
203