Completed
Branch master (d81c7d)
by Michael
04:25
created

PublisherClone::cloneFileFolder()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
cc 6
eloc 19
c 5
b 1
f 1
nc 6
nop 1
dl 0
loc 33
rs 8.439
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 99 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(_AM_PUBLISHER_CLONE_CONGRAT, "<a href='" . XOOPS_URL . "/modules/system/admin.php?fct=modulesadmin'>" . ucfirst(strtolower($clone)) . '</a>') . "<br>\n";
60
        if (!$logocreated) {
61
            $msg .= _AM_PUBLISHER_CLONE_IMAGEFAIL;
62
        }
63
    } else {
64
        $msg .= _AM_PUBLISHER_CLONE_FAIL;
65
    }
66
    echo $msg;
67
} else {
68
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
69
    $form  = new XoopsThemeForm(sprintf(_AM_PUBLISHER_CLONE_TITLE, $publisher->getModule()->getVar('name', 'E')), 'clone', 'clone.php', 'post', true);
70
    $clone = new XoopsFormText(_AM_PUBLISHER_CLONE_NAME, 'clone', 20, 20, '');
71
    $clone->setDescription(_AM_PUBLISHER_CLONE_NAME_DSC);
72
    $form->addElement($clone, true);
73
    $form->addElement(new XoopsFormHidden('op', 'submit'));
74
    $form->addElement(new XoopsFormButton('', '', _SUBMIT, 'submit'));
75
    $form->display();
76
}
77
78
// End of collapsable bar
79
publisherCloseCollapsableBar('clone', 'cloneicon');
80
81
include_once __DIR__ . '/admin_footer.php';
82
83
// 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...
84
/*
85
if (!function_exists('file_put_contents')) {
86
    function file_put_contents($filename, $data, $file_append = false)
87
    {
88
        if ($fp == fopen($filename, (!$file_append ? 'w+' : 'a+'))) {
89
            fwrite($fp, $data);
90
            fclose($fp);
91
        }
92
    }
93
}
94
*/
95
96
/**
97
 * Class PublisherClone
98
 */
99
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...
100
{
101
    // recursive cloning script
102
    /**
103
     * @param $path
104
     */
105
    public static function cloneFileFolder($path)
106
    {
107
        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...
108
        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...
109
110
        $newPath = str_replace($patKeys[0], $patValues[0], $path);
111
112
        if (is_dir($path)) {
113
            // create new dir
114
            mkdir($newPath);
115
116
            // check all files in dir, and process it
117
            if ($handle = opendir($path)) {
118
                while (($file = readdir($handle)) !== false) {
119
                    if (substr($file, 0, 1) !== '.') {
120
                        self::cloneFileFolder("{$path}/{$file}");
121
                    }
122
                }
123
                closedir($handle);
124
            }
125
        } else {
126
            $noChangeExtensions = array('jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf');
127
            if (in_array(strtolower(pathinfo($path, PATHINFO_EXTENSION)), $noChangeExtensions)) {
128
                // image
129
                copy($path, $newPath);
130
            } else {
131
                // file, read it
132
                $content = file_get_contents($path);
133
                $content = str_replace($patKeys, $patValues, $content);
134
                file_put_contents($newPath, $content);
135
            }
136
        }
137
    }
138
139
    /**
140
     * @param $dirname
141
     *
142
     * @return bool
143
     */
144
    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...
145
    {
146
        if (!extension_loaded('gd')) {
147
            return false;
148
        } else {
149
            $requiredFunctions = array(
150
                'imagecreatefrompng',
151
                'imagecolorallocate',
152
                'imagefilledrectangle',
153
                'imagepng',
154
                'imagedestroy',
155
                'imagefttext',
156
                'imagealphablending',
157
                'imagesavealpha'
158
            );
159
            foreach ($requiredFunctions as $func) {
160
                if (!function_exists($func)) {
161
                    return false;
162
                }
163
            }
164
            //            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...
165
        }
166
167
        if (!file_exists($imageBase = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logo_module.png'))
168
            || !file_exists($font = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/VeraBd.ttf'))
169
        ) {
170
            return false;
171
        }
172
173
        $imageModule = imagecreatefrompng($imageBase);
174
        // save existing alpha channel
175
        imagealphablending($imageModule, false);
176
        imagesavealpha($imageModule, true);
177
178
        //Erase old text
179
        $greyColor = imagecolorallocate($imageModule, 237, 237, 237);
180
        imagefilledrectangle($imageModule, 5, 35, 85, 46, $greyColor);
181
182
        // Write text
183
        $textColor     = imagecolorallocate($imageModule, 0, 0, 0);
184
        $spaceToBorder = (80 - strlen($dirname) * 6.5) / 2;
185
        imagefttext($imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, ucfirst($dirname), array());
186
187
        // Set transparency color
188
        //$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...
189
        //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...
190
        //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...
191
192
        imagepng($imageModule, $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logo_module.png'));
193
        imagedestroy($imageModule);
194
195
        return true;
196
    }
197
}
198